From: Alex Williamson <alex.williamson@redhat.com>
To: alex.williamson@redhat.com
Cc: Jason Gunthorpe <jgg@nvidia.com>,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
jgg@nvidia.com, peterx@redhat.com
Subject: [PATCH 1/7] vfio: Create vfio_fs_type with inode per device
Date: Thu, 05 Aug 2021 11:07:09 -0600 [thread overview]
Message-ID: <162818322947.1511194.6035266132085405252.stgit@omen> (raw)
In-Reply-To: <162818167535.1511194.6614962507750594786.stgit@omen>
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 | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/vfio.h | 1 +
2 files changed, 58 insertions(+)
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 02cc51ce6891..b88de89bda31 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -21,8 +21,10 @@
#include <linux/list.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
+#include <linux/mount.h>
#include <linux/mutex.h>
#include <linux/pci.h>
+#include <linux/pseudo_fs.h>
#include <linux/rwsem.h>
#include <linux/sched.h>
#include <linux/slab.h>
@@ -37,6 +39,14 @@
#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
#define DRIVER_DESC "VFIO - User Level meta-driver"
+/*
+ * Not exposed via UAPI
+ *
+ * XXX Adopt the following when available:
+ * https://lore.kernel.org/lkml/20210309155348.974875-1-hch@lst.de/
+ */
+#define VFIO_MAGIC 0x5646494f /* "VFIO" */
+
static struct vfio {
struct class *class;
struct list_head iommu_drivers_list;
@@ -46,6 +56,8 @@ static struct vfio {
struct mutex group_lock;
struct cdev group_cdev;
dev_t group_devt;
+ struct vfsmount *vfio_fs_mnt;
+ int vfio_fs_cnt;
} vfio;
struct vfio_iommu_driver {
@@ -519,6 +531,35 @@ 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.vfio_fs_mnt, &vfio.vfio_fs_cnt);
+ if (ret)
+ return ERR_PTR(ret);
+
+ inode = alloc_anon_inode(vfio.vfio_fs_mnt->mnt_sb);
+ if (IS_ERR(inode))
+ simple_release_fs(&vfio.vfio_fs_mnt, &vfio.vfio_fs_cnt);
+
+ return inode;
+}
+
/**
* Device objects - create, release, get, put, search
*/
@@ -783,6 +824,12 @@ int vfio_register_group_dev(struct vfio_device *device)
return -EBUSY;
}
+ device->inode = vfio_fs_inode_new();
+ if (IS_ERR(device->inode)) {
+ vfio_group_put(group);
+ return PTR_ERR(device->inode);
+ }
+
/* Our reference on group is moved to the device */
device->group = group;
@@ -907,6 +954,9 @@ void vfio_unregister_group_dev(struct vfio_device *device)
group->dev_counter--;
mutex_unlock(&group->device_lock);
+ iput(device->inode);
+ simple_release_fs(&vfio.vfio_fs_mnt, &vfio.vfio_fs_cnt);
+
/*
* In order to support multiple devices per group, devices can be
* plucked from the group while other devices in the group are still
@@ -1411,6 +1461,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);
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index a2c5b30e1763..90bcc2e9c8eb 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -24,6 +24,7 @@ struct vfio_device {
refcount_t refcount;
struct completion comp;
struct list_head group_next;
+ struct inode *inode;
};
/**
next prev parent reply other threads:[~2021-08-05 17:07 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-05 17:06 [PATCH 0/7] vfio: device fd address space and vfio-pci mmap invalidation cleanup Alex Williamson
2021-08-05 17:07 ` Alex Williamson [this message]
2021-08-10 8:43 ` [PATCH 1/7] vfio: Create vfio_fs_type with inode per device Christoph Hellwig
2021-08-10 14:52 ` Alex Williamson
2021-08-10 14:57 ` Christoph Hellwig
2021-08-10 18:49 ` Peter Xu
2021-08-10 21:16 ` Alex Williamson
2021-08-10 22:18 ` Peter Xu
2021-08-05 17:07 ` [PATCH 2/7] vfio: Export unmap_mapping_range() wrapper Alex Williamson
2021-08-10 8:45 ` Christoph Hellwig
2021-08-10 18:56 ` Peter Xu
2021-08-05 17:07 ` [PATCH 3/7] vfio/pci: Use vfio_device_unmap_mapping_range() Alex Williamson
2021-08-06 1:04 ` Jason Gunthorpe
2021-08-06 20:17 ` Alex Williamson
2021-08-10 8:51 ` Christoph Hellwig
2021-08-10 11:57 ` Jason Gunthorpe
2021-08-10 12:55 ` Christoph Hellwig
2021-08-10 21:50 ` Alex Williamson
2021-08-11 11:57 ` Jason Gunthorpe
2021-08-10 8:53 ` Christoph Hellwig
2021-08-10 19:02 ` Peter Xu
2021-08-10 20:51 ` Alex Williamson
2021-08-10 18:48 ` Peter Xu
2021-08-10 19:59 ` Alex Williamson
2021-08-10 20:20 ` Peter Xu
2021-08-05 17:07 ` [PATCH 4/7] vfio,vfio-pci: Add vma to pfn callback Alex Williamson
2021-08-06 1:01 ` Jason Gunthorpe
2021-08-10 9:00 ` Christoph Hellwig
2021-08-10 9:00 ` Christoph Hellwig
2021-08-05 17:08 ` [PATCH 5/7] mm/interval_tree.c: Export vma interval tree iterators Alex Williamson
2021-08-05 17:08 ` [PATCH 6/7] vfio: Add vfio_device_io_remap_mapping_range() Alex Williamson
2021-08-10 9:04 ` Christoph Hellwig
2021-08-05 17:08 ` [PATCH 7/7] vfio/pci: Remove map-on-fault behavior Alex Williamson
2021-08-10 9:11 ` Christoph Hellwig
2021-08-10 15:04 ` Alex Williamson
2021-08-10 20:54 ` Peter Xu
2021-08-10 21:45 ` Alex Williamson
2021-08-10 22:27 ` Peter Xu
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=162818322947.1511194.6035266132085405252.stgit@omen \
--to=alex.williamson@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).