linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: alex.williamson@redhat.com
Cc: cohuck@redhat.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, jgg@nvidia.com, peterx@redhat.com
Subject: [PATCH v1 01/14] vfio: Create vfio_fs_type with inode per device
Date: Mon, 08 Mar 2021 14:47:28 -0700	[thread overview]
Message-ID: <161524004828.3480.1817334832614722574.stgit@gimli.home> (raw)
In-Reply-To: <161523878883.3480.12103845207889888280.stgit@gimli.home>

By linking all the device fds we provide to userspace to an
address space through a new pseudo fs, we can use tools like
unmap_mapping_range() to zap all vmas associated with a device.

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
 drivers/vfio/vfio.c |   54 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 38779e6fd80c..abdf8d52a911 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -32,11 +32,18 @@
 #include <linux/vfio.h>
 #include <linux/wait.h>
 #include <linux/sched/signal.h>
+#include <linux/pseudo_fs.h>
+#include <linux/mount.h>
 
 #define DRIVER_VERSION	"0.3"
 #define DRIVER_AUTHOR	"Alex Williamson <alex.williamson@redhat.com>"
 #define DRIVER_DESC	"VFIO - User Level meta-driver"
 
+#define VFIO_MAGIC 0x5646494f /* "VFIO" */
+
+static int vfio_fs_cnt;
+static struct vfsmount *vfio_fs_mnt;
+
 static struct vfio {
 	struct class			*class;
 	struct list_head		iommu_drivers_list;
@@ -97,6 +104,7 @@ struct vfio_device {
 	struct vfio_group		*group;
 	struct list_head		group_next;
 	void				*device_data;
+	struct inode			*inode;
 };
 
 #ifdef CONFIG_VFIO_NOIOMMU
@@ -529,6 +537,34 @@ static struct vfio_group *vfio_group_get_from_dev(struct device *dev)
 	return group;
 }
 
+static int vfio_fs_init_fs_context(struct fs_context *fc)
+{
+	return init_pseudo(fc, VFIO_MAGIC) ? 0 : -ENOMEM;
+}
+
+static struct file_system_type vfio_fs_type = {
+	.name = "vfio",
+	.owner = THIS_MODULE,
+	.init_fs_context = vfio_fs_init_fs_context,
+	.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_fs_mnt, &vfio_fs_cnt);
+	if (ret)
+		return ERR_PTR(ret);
+
+	inode = alloc_anon_inode(vfio_fs_mnt->mnt_sb);
+	if (IS_ERR(inode))
+		simple_release_fs(&vfio_fs_mnt, &vfio_fs_cnt);
+
+	return inode;
+}
+
 /**
  * Device objects - create, release, get, put, search
  */
@@ -539,11 +575,19 @@ struct vfio_device *vfio_group_create_device(struct vfio_group *group,
 					     void *device_data)
 {
 	struct vfio_device *device;
+	struct inode *inode;
 
 	device = kzalloc(sizeof(*device), GFP_KERNEL);
 	if (!device)
 		return ERR_PTR(-ENOMEM);
 
+	inode = vfio_fs_inode_new();
+	if (IS_ERR(inode)) {
+		kfree(device);
+		return ERR_CAST(inode);
+	}
+	device->inode = inode;
+
 	kref_init(&device->kref);
 	device->dev = dev;
 	device->group = group;
@@ -574,6 +618,9 @@ static void vfio_device_release(struct kref *kref)
 
 	dev_set_drvdata(device->dev, NULL);
 
+	iput(device->inode);
+	simple_release_fs(&vfio_fs_mnt, &vfio_fs_cnt);
+
 	kfree(device);
 
 	/* vfio_del_group_dev may be waiting for this device */
@@ -1488,6 +1535,13 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
 	 */
 	filep->f_mode |= (FMODE_LSEEK | 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().
+	 */
+	filep->f_mapping = device->inode->i_mapping;
+
 	atomic_inc(&group->container_users);
 
 	fd_install(ret, filep);


  reply	other threads:[~2021-03-08 21:48 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-08 21:47 [PATCH v1 00/14] vfio: Device memory DMA mapping improvements Alex Williamson
2021-03-08 21:47 ` Alex Williamson [this message]
2021-03-09  8:36   ` [PATCH v1 01/14] vfio: Create vfio_fs_type with inode per device Christoph Hellwig
2021-04-09  4:54   ` 答复: " Zengtao (B)
2021-04-09 14:24     ` Alex Williamson
2021-04-09 17:32       ` Jason Gunthorpe
2021-04-12  4:03         ` 答复: " Zengtao (B)
2021-04-12  4:09       ` Zengtao (B)
2021-03-08 21:47 ` [PATCH v1 02/14] vfio: Update vfio_add_group_dev() API Alex Williamson
2021-03-10  7:48   ` Christoph Hellwig
2021-03-10 12:19     ` Jason Gunthorpe
2021-03-10 15:28       ` Alex Williamson
2021-03-11 11:23         ` Christoph Hellwig
2021-03-08 21:47 ` [PATCH v1 03/14] vfio: Export unmap_mapping_range() wrapper Alex Williamson
2021-03-08 21:48 ` [PATCH v1 04/14] vfio/pci: Use vfio_device_unmap_mapping_range() Alex Williamson
2021-03-08 21:48 ` [PATCH v1 05/14] vfio: Create a vfio_device from vma lookup Alex Williamson
2021-03-08 21:48 ` [PATCH v1 06/14] vfio: Add vma to pfn callback Alex Williamson
2021-03-09  0:33   ` Jason Gunthorpe
2021-03-08 21:48 ` [PATCH v1 07/14] vfio: Add a device notifier interface Alex Williamson
2021-03-09  0:46   ` Jason Gunthorpe
2021-03-09 15:45     ` Alex Williamson
2021-03-09 16:47       ` Jason Gunthorpe
2021-03-10  7:56   ` Christoph Hellwig
2021-03-19 22:25     ` Alex Williamson
2021-03-22 15:16       ` Christoph Hellwig
2021-03-08 21:48 ` [PATCH v1 08/14] vfio/pci: Notify on device release Alex Williamson
2021-03-08 21:48 ` [PATCH v1 09/14] vfio/type1: Refactor pfn_list clearing Alex Williamson
2021-03-10  8:01   ` Christoph Hellwig
2021-03-08 21:49 ` [PATCH v1 10/14] vfio/type1: Pass iommu and dma objects through to vaddr_get_pfn Alex Williamson
2021-03-08 21:49 ` [PATCH v1 11/14] vfio/type1: Register device notifier Alex Williamson
2021-03-10  8:03   ` Christoph Hellwig
2021-03-08 21:49 ` [PATCH v1 12/14] vfio/type1: Support batching of device mappings Alex Williamson
2021-03-09  1:04   ` Jason Gunthorpe
2021-03-08 21:49 ` [PATCH v1 13/14] vfio: Remove extern from declarations across vfio Alex Williamson
2021-03-09  0:21   ` Halil Pasic
2021-03-09  1:07   ` Jason Gunthorpe
2021-03-08 21:49 ` [PATCH v1 14/14] vfio: Cleanup use of bare unsigned Alex Williamson
2021-03-09  1:07   ` Jason Gunthorpe
2021-03-09  8:31     ` Christoph Hellwig
2021-03-09  1:06 ` [PATCH v1 00/14] vfio: Device memory DMA mapping improvements Jason Gunthorpe

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=161524004828.3480.1817334832614722574.stgit@gimli.home \
    --to=alex.williamson@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=jgg@nvidia.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;
as well as URLs for NNTP newsgroup(s).