mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yan Zhao <yan.y.zhao@intel.com>
To: <kvm@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <alex.williamson@redhat.com>, <kevin.tian@intel.com>,
	<jgg@nvidia.com>, <peterx@redhat.com>, <ajones@ventanamicro.com>
Subject: Re: [PATCH] vfio: Reuse file f_inode as vfio device inode
Date: Thu, 20 Jun 2024 18:14:42 +0800	[thread overview]
Message-ID: <ZnQBEmjEFoO39rRO@yzhao56-desk.sh.intel.com> (raw)
In-Reply-To: <20240617095332.30543-1-yan.y.zhao@intel.com>

On Mon, Jun 17, 2024 at 05:53:32PM +0800, Yan Zhao wrote:
...
> diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c
> index ded364588d29..aaef188003b6 100644
> --- a/drivers/vfio/group.c
> +++ b/drivers/vfio/group.c
> @@ -268,31 +268,14 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
>  	if (ret)
>  		goto err_free;
>  
> -	/*
> -	 * We can't use anon_inode_getfd() because we need to modify
> -	 * the f_mode flags directly to allow more than just ioctls
> -	 */
> -	filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
> -				   df, O_RDWR);
> +	filep = vfio_device_get_pseudo_file(device);
If getting an inode from vfio_fs_type is not a must, maybe we could use
anon_inode_create_getfile() here?
Then changes to group.c and vfio_main.c can be simplified as below:

diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c
index ded364588d29..7f2f7871403f 100644
--- a/drivers/vfio/group.c
+++ b/drivers/vfio/group.c
@@ -269,29 +269,22 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
                goto err_free;

        /*
-        * We can't use anon_inode_getfd() because we need to modify
-        * the f_mode flags directly to allow more than just ioctls
+        * Get a unique inode from anon_inodefs
         */
-       filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
-                                  df, O_RDWR);
+       filep = anon_inode_create_getfile("[vfio-device]", &vfio_device_fops, df,
+                                         O_RDWR, NULL);
        if (IS_ERR(filep)) {
                ret = PTR_ERR(filep);
                goto err_close_device;
        }
-
-       /*
-        * TODO: add an anon_inode interface to do this.
-        * Appears to be missing by lack of need rather than
-        * explicitly prevented.  Now there's need.
-        */
        filep->f_mode |= (FMODE_PREAD | FMODE_PWRITE);

        /*
-        * Use the pseudo fs inode on the device to link all mmaps
-        * to the same address space, allowing us to unmap all vmas
-        * associated to this device using unmap_mapping_range().
+        * mmaps are linked to the address space of the filep->f_inode.
+        * Save the inode in device->inode to allow unmap_mapping_range() to
+        * unmap all vmas.
         */
-       filep->f_mapping = device->inode->i_mapping;
+       device->inode = filep->f_inode;

        if (device->group->type == VFIO_NO_IOMMU)
                dev_warn(device->dev, "vfio-noiommu device opened by user "

diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index a5a62d9d963f..c9dac788411b 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -192,8 +192,6 @@ static void vfio_device_release(struct device *dev)
        if (device->ops->release)
                device->ops->release(device);

-       iput(device->inode);
-       simple_release_fs(&vfio.vfs_mount, &vfio.fs_count);
        kvfree(device);
 }

@@ -248,22 +246,6 @@ static struct file_system_type vfio_fs_type = {
        .kill_sb = kill_anon_super,
 };

-static struct inode *vfio_fs_inode_new(void)
-{
-       struct inode *inode;
-       int ret;
-
-       ret = simple_pin_fs(&vfio_fs_type, &vfio.vfs_mount, &vfio.fs_count);
-       if (ret)
-               return ERR_PTR(ret);
-
-       inode = alloc_anon_inode(vfio.vfs_mount->mnt_sb);
-       if (IS_ERR(inode))
-               simple_release_fs(&vfio.vfs_mount, &vfio.fs_count);
-
-       return inode;
-}
-
 /*
  * Initialize a vfio_device so it can be registered to vfio core.
  */
@@ -282,11 +264,6 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev,
        init_completion(&device->comp);
        device->dev = dev;
        device->ops = ops;
-       device->inode = vfio_fs_inode_new();
-       if (IS_ERR(device->inode)) {
-               ret = PTR_ERR(device->inode);
-               goto out_inode;
-       }

        if (ops->init) {
                ret = ops->init(device);
@@ -301,9 +278,6 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev,
        return 0;

 out_uninit:
-       iput(device->inode);
-       simple_release_fs(&vfio.vfs_mount, &vfio.fs_count);
-out_inode:
        vfio_release_device_set(device);
        ida_free(&vfio.device_ida, device->index);
        return ret;


> diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
> index 50128da18bca..1f8915f79fbb 100644
> --- a/drivers/vfio/vfio.h
> +++ b/drivers/vfio/vfio.h
> @@ -35,6 +35,7 @@ struct vfio_device_file *
>  vfio_allocate_device_file(struct vfio_device *device);
>  
>  extern const struct file_operations vfio_device_fops;
> +struct file *vfio_device_get_pseudo_file(struct vfio_device *device);
>  
>  #ifdef CONFIG_VFIO_NOIOMMU
>  extern bool vfio_noiommu __read_mostly;
> @@ -420,6 +421,7 @@ static inline void vfio_cdev_cleanup(void)
>  {
>  }
>  #endif /* CONFIG_VFIO_DEVICE_CDEV */
> +struct file *vfio_device_get_pseduo_file(struct vfio_device *device);
Sorry, this line was included by mistake.

  reply	other threads:[~2024-06-20 10:16 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-17  9:53 Yan Zhao
2024-06-20 10:14 ` Yan Zhao [this message]
2024-06-26  8:36   ` Tian, Kevin
2024-06-26  9:11     ` Yan Zhao
2024-06-26 13:35 ` Jason Gunthorpe
2024-06-26 23:55   ` Tian, Kevin
2024-06-27 12:26     ` Jason Gunthorpe
2024-06-27  0:17   ` Tian, Kevin
2024-06-27  9:51     ` Yan Zhao
2024-06-27 12:42       ` Jason Gunthorpe
2024-06-28  5:21         ` Yan Zhao
2024-06-28  9:48           ` Yi Liu
2024-06-28 15:28             ` Yan Zhao
2024-06-30  7:06               ` Yi Liu
2024-07-01  1:47                 ` Yan Zhao
2024-07-01  5:44                   ` Yi Liu
2024-07-01  5:48                     ` Yan Zhao
2024-07-10 14:40                       ` Jason Gunthorpe
2024-07-12  5:19                         ` Yan Zhao
2024-07-12  6:14                           ` Yi Liu
2024-07-01  7:54 ` Yi Liu
2024-07-01 11:29   ` Yan Zhao

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=ZnQBEmjEFoO39rRO@yzhao56-desk.sh.intel.com \
    --to=yan.y.zhao@intel.com \
    --cc=ajones@ventanamicro.com \
    --cc=alex.williamson@redhat.com \
    --cc=jgg@nvidia.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterx@redhat.com \
    /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®