From: Yi Liu <yi.l.liu@intel.com>
To: alex.williamson@redhat.com, jgg@nvidia.com
Cc: kevin.tian@intel.com, cohuck@redhat.com, eric.auger@redhat.com,
nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com,
chao.p.peng@linux.intel.com, yi.l.liu@intel.com,
yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com
Subject: [RFC 11/12] vfio: Add ioctls for device cdev iommufd
Date: Mon, 19 Dec 2022 00:47:17 -0800 [thread overview]
Message-ID: <20221219084718.9342-12-yi.l.liu@intel.com> (raw)
In-Reply-To: <20221219084718.9342-1-yi.l.liu@intel.com>
This adds two vfio device ioctls for userspace using iommufd on vfio
devices.
VFIO_DEVICE_BIND_IOMMUFD: bind device to an iommufd, hence gain DMA
control provided by the iommufd. VFIO no
iommu is indicated by passing a minus
fd value.
VFIO_DEVICE_ATTACH_IOMMUFD_PT: attach device to ioas, page tables
managed by iommufd. Attach can be
undo by passing IOMMUFD_INVALID_ID
to kernel.
The ioctls introduced here are just on par with existing VFIO.
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
drivers/vfio/vfio_main.c | 145 +++++++++++++++++++++++++++++++++--
include/uapi/linux/iommufd.h | 2 +
include/uapi/linux/vfio.h | 64 ++++++++++++++++
3 files changed, 206 insertions(+), 5 deletions(-)
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 4ba90b55ec44..d2a32b8a562b 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -34,6 +34,7 @@
#include <linux/interval_tree.h>
#include <linux/iova_bitmap.h>
#include <linux/iommufd.h>
+#include <uapi/linux/iommufd.h>
#include "vfio.h"
#define DRIVER_VERSION "0.3"
@@ -415,7 +416,7 @@ static int vfio_device_first_open(struct vfio_device_file *df,
if (!try_module_get(device->dev->driver->owner))
return -ENODEV;
- if (iommufd)
+ if (iommufd && !IS_ERR(iommufd))
ret = vfio_iommufd_bind(device, iommufd, dev_id, pt_id);
else
ret = vfio_device_group_use_iommu(device);
@@ -432,7 +433,7 @@ static int vfio_device_first_open(struct vfio_device_file *df,
err_unuse_iommu:
device->kvm = NULL;
- if (iommufd)
+ if (iommufd && !IS_ERR(iommufd))
vfio_iommufd_unbind(device);
else
vfio_device_group_unuse_iommu(device);
@@ -451,7 +452,7 @@ static void vfio_device_last_close(struct vfio_device_file *df)
if (device->ops->close_device)
device->ops->close_device(device);
device->kvm = NULL;
- if (iommufd)
+ if (iommufd && !IS_ERR(iommufd))
vfio_iommufd_unbind(device);
else
vfio_device_group_unuse_iommu(device);
@@ -495,11 +496,10 @@ int vfio_device_open(struct vfio_device_file *df,
return 0;
}
-void vfio_device_close(struct vfio_device_file *df)
+static void __vfio_device_close(struct vfio_device_file *df)
{
struct vfio_device *device = df->device;
- mutex_lock(&device->dev_set->lock);
/*
* Paired with smp_load_acquire() in vfio_device_fops::ioctl/
* read/write/mmap
@@ -511,6 +511,14 @@ void vfio_device_close(struct vfio_device_file *df)
device->single_open = false;
}
device->open_count--;
+}
+
+void vfio_device_close(struct vfio_device_file *df)
+{
+ struct vfio_device *device = df->device;
+
+ mutex_lock(&device->dev_set->lock);
+ __vfio_device_close(df);
mutex_unlock(&device->dev_set->lock);
}
@@ -592,6 +600,8 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
*/
if (!df->single_open)
vfio_device_group_close(df);
+ else
+ vfio_device_close(df);
kfree(df);
vfio_device_put_registration(device);
@@ -1054,6 +1064,124 @@ static int vfio_ioctl_device_feature(struct vfio_device *device,
}
}
+static long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df,
+ unsigned long arg)
+{
+ struct vfio_device *device = df->device;
+ struct vfio_device_bind_iommufd bind;
+ struct iommufd_ctx *iommufd;
+ unsigned long minsz;
+ struct fd f;
+ int ret;
+ bool access;
+
+ minsz = offsetofend(struct vfio_device_bind_iommufd, iommufd);
+
+ if (copy_from_user(&bind, (void __user *)arg, minsz))
+ return -EFAULT;
+
+ if (bind.argsz < minsz || bind.flags)
+ return -EINVAL;
+
+ if (!device->ops->bind_iommufd)
+ return -ENODEV;
+
+ /* iommufd < 0 means noiommu mode */
+ if (bind.iommufd < 0) {
+ if (!capable(CAP_SYS_RAWIO))
+ return -EPERM;
+ iommufd = ERR_PTR(-EINVAL);
+ } else {
+ f = fdget(bind.iommufd);
+ if (!f.file)
+ return -EBADF;
+
+ iommufd = iommufd_ctx_from_file(f.file);
+ if (IS_ERR(iommufd)) {
+ fdput(f);
+ return PTR_ERR(iommufd);
+ }
+ }
+
+ mutex_lock(&device->dev_set->lock);
+ /* Paired with smp_store_release() in vfio_device_open/close() */
+ access = smp_load_acquire(&df->access_granted);
+ if (access) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
+
+ /* df->kvm is supposed to be set in vfio_device_file_set_kvm() */
+ df->iommufd = iommufd;
+ ret = vfio_device_open(df, &bind.out_devid, NULL);
+ if (ret)
+ goto out_unlock;
+
+ ret = copy_to_user((void __user *)arg + minsz,
+ &bind.out_devid,
+ sizeof(bind.out_devid)) ? -EFAULT : 0;
+ if (ret)
+ goto out_close_device;
+
+ mutex_unlock(&device->dev_set->lock);
+ if (!IS_ERR(iommufd))
+ fdput(f);
+ else
+ dev_warn(device->dev, "vfio-noiommu device used by user "
+ "(%s:%d)\n", current->comm, task_pid_nr(current));
+ return 0;
+
+out_close_device:
+ __vfio_device_close(df);
+out_unlock:
+ df->iommufd = NULL;
+ mutex_unlock(&device->dev_set->lock);
+ if (!IS_ERR(iommufd))
+ fdput(f);
+ return ret;
+}
+
+static int vfio_ioctl_device_attach(struct vfio_device *device,
+ struct vfio_device_feature __user *arg)
+{
+ struct vfio_device_attach_iommufd_pt attach;
+ u32 pt_id;
+ int ret;
+
+ if (copy_from_user(&attach, (void __user *)arg, sizeof(attach)))
+ return -EFAULT;
+
+ if (attach.flags)
+ return -EINVAL;
+
+ if (!device->ops->bind_iommufd)
+ return -ENODEV;
+
+ mutex_lock(&device->dev_set->lock);
+ pt_id = attach.pt_id;
+ ret = vfio_iommufd_attach(device,
+ pt_id != IOMMUFD_INVALID_ID ? &pt_id : NULL);
+ if (ret)
+ goto out_unlock;
+
+ if (pt_id != IOMMUFD_INVALID_ID) {
+ ret = copy_to_user((void __user *)arg + offsetofend(
+ struct vfio_device_attach_iommufd_pt, flags),
+ &pt_id,
+ sizeof(pt_id)) ? -EFAULT : 0;
+ if (ret)
+ goto out_detach;
+ }
+ mutex_unlock(&device->dev_set->lock);
+ return 0;
+
+out_detach:
+ vfio_iommufd_attach(device, NULL);
+out_unlock:
+ mutex_unlock(&device->dev_set->lock);
+ return ret;
+}
+
static long vfio_device_fops_unl_ioctl(struct file *filep,
unsigned int cmd, unsigned long arg)
{
@@ -1062,6 +1190,9 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
bool access;
int ret;
+ if (cmd == VFIO_DEVICE_BIND_IOMMUFD)
+ return vfio_device_ioctl_bind_iommufd(df, arg);
+
/* Paired with smp_store_release() in vfio_device_open/close() */
access = smp_load_acquire(&df->access_granted);
if (!access)
@@ -1076,6 +1207,10 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
ret = vfio_ioctl_device_feature(device, (void __user *)arg);
break;
+ case VFIO_DEVICE_ATTACH_IOMMUFD_PT:
+ ret = vfio_ioctl_device_attach(device, (void __user *)arg);
+ break;
+
default:
if (unlikely(!device->ops->ioctl))
ret = -EINVAL;
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index 98ebba80cfa1..87680274c01b 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -9,6 +9,8 @@
#define IOMMUFD_TYPE (';')
+#define IOMMUFD_INVALID_ID 0 /* valid ID starts from 1 */
+
/**
* DOC: General ioctl format
*
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index d7d8e0922376..d5cb347c0763 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -190,6 +190,70 @@ struct vfio_group_status {
/* --------------- IOCTLs for DEVICE file descriptors --------------- */
+/*
+ * VFIO_DEVICE_BIND_IOMMUFD - _IOR(VFIO_TYPE, VFIO_BASE + 19,
+ * struct vfio_device_bind_iommufd)
+ *
+ * Bind a vfio_device to the specified iommufd and an ioas or a hardware
+ * page table.
+ *
+ * The user should provide a device cookie when calling this ioctl. The
+ * cookie is carried only in event e.g. I/O fault reported to userspace
+ * via iommufd. The user should use devid returned by this ioctl to mark
+ * the target device in other ioctls (e.g. capability query via iommufd).
+ *
+ * User is not allowed to access the device before the binding operation
+ * is completed.
+ *
+ * Unbind is automatically conducted when device fd is closed.
+ *
+ * @argsz: user filled size of this data.
+ * @flags: reserved for future extension.
+ * @dev_cookie: a per device cookie provided by userspace.
+ * @iommufd: iommufd to bind. iommufd < 0 means noiommu.
+ * @out_devid: the device id generated by this bind.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_bind_iommufd {
+ __u32 argsz;
+ __u32 flags;
+ __aligned_u64 dev_cookie;
+ __s32 iommufd;
+ __u32 out_devid;
+};
+
+#define VFIO_DEVICE_BIND_IOMMUFD _IO(VFIO_TYPE, VFIO_BASE + 19)
+
+/*
+ * VFIO_DEVICE_ATTACH_IOMMUFD_PT - _IOW(VFIO_TYPE, VFIO_BASE + 20,
+ * struct vfio_device_attach_iommufd_pt)
+ *
+ * Attach a vfio device to an iommufd address space specified by IOAS
+ * id or hardware page table id.
+ *
+ * Available only after a device has been bound to iommufd via
+ * VFIO_DEVICE_BIND_IOMMUFD
+ *
+ * Undo by passing pt_id == IOMMUFD_INVALID_ID
+ *
+ * @argsz: user filled size of this data.
+ * @flags: must be 0.
+ * @pt_id: Input the target id, can be an ioas or a hwpt allocated
+ * via iommufd subsystem, and output the attached pt_id. It
+ * be the ioas, hwpt itself or an hwpt created by kernel
+ * during the attachment.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_attach_iommufd_pt {
+ __u32 argsz;
+ __u32 flags;
+ __u32 pt_id;
+};
+
+#define VFIO_DEVICE_ATTACH_IOMMUFD_PT _IO(VFIO_TYPE, VFIO_BASE + 20)
+
/**
* VFIO_DEVICE_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 7,
* struct vfio_device_info)
--
2.34.1
next prev parent reply other threads:[~2022-12-19 8:48 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-19 8:47 [RFC 00/12] Add vfio_device cdev for iommufd support Yi Liu
2022-12-19 8:47 ` [RFC 01/12] vfio: Allocate per device file structure Yi Liu
2022-12-21 3:57 ` Tian, Kevin
2022-12-21 6:46 ` Yi Liu
2022-12-19 8:47 ` [RFC 02/12] vfio: Refine vfio file kAPIs Yi Liu
2022-12-19 8:47 ` [RFC 03/12] vfio: Accept vfio device file in the driver facing kAPI Yi Liu
2022-12-21 4:07 ` Tian, Kevin
2022-12-21 7:02 ` Yi Liu
2023-01-04 18:25 ` Jason Gunthorpe
2023-01-09 4:13 ` Tian, Kevin
2022-12-19 8:47 ` [RFC 04/12] kvm/vfio: Rename kvm_vfio_group to prepare for accepting vfio device fd Yi Liu
2022-12-19 8:47 ` [RFC 05/12] kvm/vfio: Accept vfio device file from userspace Yi Liu
2023-01-06 14:32 ` Jason Gunthorpe
2023-01-06 14:46 ` Yi Liu
2023-01-06 14:55 ` Jason Gunthorpe
2023-01-06 15:04 ` Yi Liu
2023-01-06 16:08 ` Jason Gunthorpe
2023-01-09 4:17 ` Tian, Kevin
2023-01-09 4:26 ` Yi Liu
2022-12-19 8:47 ` [RFC 06/12] vfio: Pass struct vfio_device_file * to vfio_device_open/close() Yi Liu
2022-12-21 4:10 ` Tian, Kevin
2022-12-21 7:04 ` Yi Liu
2022-12-19 8:47 ` [RFC 07/12] vfio: Block device access via device fd until device is opened Yi Liu
2022-12-21 4:18 ` Tian, Kevin
2023-01-04 20:47 ` Jason Gunthorpe
2022-12-19 8:47 ` [RFC 08/12] vfio: Add infrastructure for bind_iommufd and attach Yi Liu
2023-01-09 5:46 ` Tian, Kevin
2023-01-09 13:21 ` Jason Gunthorpe
2023-01-10 2:53 ` Tian, Kevin
2022-12-19 8:47 ` [RFC 09/12] vfio: Make vfio_device_open() exclusive between group path and device cdev path Yi Liu
2023-01-09 6:03 ` Tian, Kevin
2022-12-19 8:47 ` [RFC 10/12] vfio: Add cdev for vfio_device Yi Liu
2023-01-09 6:54 ` Tian, Kevin
2022-12-19 8:47 ` Yi Liu [this message]
2023-01-09 7:47 ` [RFC 11/12] vfio: Add ioctls for device cdev iommufd Tian, Kevin
2023-01-09 14:14 ` Jason Gunthorpe
2023-01-09 15:07 ` Yi Liu
2023-01-09 15:12 ` Jason Gunthorpe
2023-01-09 15:20 ` Yi Liu
2023-01-09 14:55 ` Yi Liu
2023-01-10 2:57 ` Tian, Kevin
2022-12-19 8:47 ` [RFC 12/12] vfio: Compile group optionally Yi Liu
2022-12-19 8:51 ` [RFC 00/12] Add vfio_device cdev for iommufd support Yi Liu
2023-01-04 15:23 ` Yi Liu
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=20221219084718.9342-12-yi.l.liu@intel.com \
--to=yi.l.liu@intel.com \
--cc=alex.williamson@redhat.com \
--cc=chao.p.peng@linux.intel.com \
--cc=cohuck@redhat.com \
--cc=eric.auger@redhat.com \
--cc=jasowang@redhat.com \
--cc=jgg@nvidia.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=mjrosato@linux.ibm.com \
--cc=nicolinc@nvidia.com \
--cc=peterx@redhat.com \
--cc=yi.y.sun@linux.intel.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