This adds ioctl for userspace to bind device cdev fd to iommufd.

    VFIO_DEVICE_BIND_IOMMUFD: bind device to an iommufd, hence gain DMA
                              control provided by the iommufd. open_device
                              op is called after bind_iommufd op.
                              VFIO no iommu mode is indicated by passing
                              VFIO_NOIOMMU_FD (-1) as iommufd value.

Reviewed-by: Kevin Tian <kevin.t...@intel.com>
Tested-by: Terrence Xu <terrence...@intel.com>
Tested-by: Nicolin Chen <nicol...@nvidia.com>
Tested-by: Yanting Jiang <yanting.ji...@intel.com>
Signed-off-by: Yi Liu <yi.l....@intel.com>
---
 drivers/vfio/device_cdev.c | 152 +++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio.h        |  13 ++++
 drivers/vfio/vfio_main.c   |   5 ++
 include/uapi/linux/vfio.h  |  31 ++++++++
 4 files changed, 201 insertions(+)

diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c
index 1c640016a824..e3c51d2f5732 100644
--- a/drivers/vfio/device_cdev.c
+++ b/drivers/vfio/device_cdev.c
@@ -3,6 +3,7 @@
  * Copyright (c) 2023 Intel Corporation.
  */
 #include <linux/vfio.h>
+#include <linux/iommufd.h>
 
 #include "vfio.h"
 
@@ -44,6 +45,157 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct 
file *filep)
        return ret;
 }
 
+static void vfio_device_get_kvm_safe(struct vfio_device_file *df)
+{
+       spin_lock(&df->kvm_ref_lock);
+       if (df->kvm)
+               _vfio_device_get_kvm_safe(df->device, df->kvm);
+       spin_unlock(&df->kvm_ref_lock);
+}
+
+void vfio_device_cdev_close(struct vfio_device_file *df)
+{
+       struct vfio_device *device = df->device;
+
+       /*
+        * In the time of close, there is no contention with another one
+        * changing this flag.  So read df->access_granted without lock
+        * and no smp_load_acquire() is ok.
+        */
+       if (!df->access_granted)
+               return;
+
+       mutex_lock(&device->dev_set->lock);
+       vfio_device_close(df);
+       vfio_device_put_kvm(device);
+       if (df->iommufd)
+               iommufd_ctx_put(df->iommufd);
+       device->cdev_opened = false;
+       mutex_unlock(&device->dev_set->lock);
+       vfio_device_unblock_group(device);
+}
+
+static int vfio_device_cdev_probe_noiommu(struct vfio_device *device)
+{
+       if (!capable(CAP_SYS_RAWIO))
+               return -EPERM;
+
+       if (!device->noiommu)
+               return -EINVAL;
+
+       return 0;
+}
+
+static struct iommufd_ctx *vfio_get_iommufd_from_fd(int fd)
+{
+       struct iommufd_ctx *iommufd;
+       struct fd f;
+
+       f = fdget(fd);
+       if (!f.file)
+               return ERR_PTR(-EBADF);
+
+       iommufd = iommufd_ctx_from_file(f.file);
+
+       fdput(f);
+       return iommufd;
+}
+
+long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df,
+                                   struct vfio_device_bind_iommufd __user *arg)
+{
+       struct vfio_device *device = df->device;
+       struct vfio_device_bind_iommufd bind;
+       unsigned long minsz;
+       bool is_noiommu;
+       int ret;
+
+       static_assert(__same_type(arg->out_devid, df->devid));
+
+       minsz = offsetofend(struct vfio_device_bind_iommufd, out_devid);
+
+       if (copy_from_user(&bind, arg, minsz))
+               return -EFAULT;
+
+       if (bind.argsz < minsz || bind.flags)
+               return -EINVAL;
+
+       /* BIND_IOMMUFD only allowed for cdev fds */
+       if (df->group)
+               return -EINVAL;
+
+       ret = vfio_device_block_group(device);
+       if (ret)
+               return ret;
+
+       is_noiommu = bind.iommufd == VFIO_NOIOMMU_FD;
+
+       mutex_lock(&device->dev_set->lock);
+       /* one device cannot be bound twice */
+       if (df->access_granted) {
+               ret = -EINVAL;
+               goto out_unlock;
+       }
+
+       if (is_noiommu) {
+               ret = vfio_device_cdev_probe_noiommu(device);
+               if (ret)
+                       goto out_unlock;
+       } else {
+               df->iommufd = vfio_get_iommufd_from_fd(bind.iommufd);
+               if (IS_ERR(df->iommufd)) {
+                       ret = PTR_ERR(df->iommufd);
+                       df->iommufd = NULL;
+                       goto out_unlock;
+               }
+       }
+
+       /*
+        * Before the device open, get the KVM pointer currently
+        * associated with the device file (if there is) and obtain
+        * a reference.  This reference is held until device closed.
+        * Save the pointer in the device for use by drivers.
+        */
+       vfio_device_get_kvm_safe(df);
+
+       ret = vfio_device_open(df);
+       if (ret)
+               goto out_put_kvm;
+
+       if (is_noiommu) {
+               dev_warn(device->dev, "device is bound to vfio-noiommu by user "
+                        "(%s:%d)\n", current->comm, task_pid_nr(current));
+       } else {
+               ret = copy_to_user(&arg->out_devid, &df->devid,
+                                  sizeof(df->devid)) ? -EFAULT : 0;
+               if (ret)
+                       goto out_close_device;
+       }
+
+       /*
+        * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
+        * read/write/mmap
+        */
+       smp_store_release(&df->access_granted, true);
+       device->cdev_opened = true;
+       mutex_unlock(&device->dev_set->lock);
+
+       return 0;
+
+out_close_device:
+       vfio_device_close(df);
+out_put_kvm:
+       vfio_device_put_kvm(device);
+       if (!is_noiommu) {
+               iommufd_ctx_put(df->iommufd);
+               df->iommufd = NULL;
+       }
+out_unlock:
+       mutex_unlock(&device->dev_set->lock);
+       vfio_device_unblock_group(device);
+       return ret;
+}
+
 static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
 {
        return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index 3a8fd0e32f59..ace3d52b0928 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -281,6 +281,9 @@ static inline void vfio_device_del(struct vfio_device 
*device)
 
 void vfio_init_device_cdev(struct vfio_device *device);
 int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep);
+void vfio_device_cdev_close(struct vfio_device_file *df);
+long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df,
+                                   struct vfio_device_bind_iommufd __user 
*arg);
 int vfio_cdev_init(struct class *device_class);
 void vfio_cdev_cleanup(void);
 #else
@@ -304,6 +307,16 @@ static inline int vfio_device_fops_cdev_open(struct inode 
*inode,
        return 0;
 }
 
+static inline void vfio_device_cdev_close(struct vfio_device_file *df)
+{
+}
+
+static inline long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df,
+                                                 struct 
vfio_device_bind_iommufd __user *arg)
+{
+       return -EOPNOTSUPP;
+}
+
 static inline int vfio_cdev_init(struct class *device_class)
 {
        return 0;
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 61f56d9b5e5f..0611e29e41a9 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -565,6 +565,8 @@ static int vfio_device_fops_release(struct inode *inode, 
struct file *filep)
 
        if (df->group)
                vfio_device_group_close(df);
+       else
+               vfio_device_cdev_close(df);
 
        vfio_device_put_registration(device);
 
@@ -1138,6 +1140,9 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
        struct vfio_device *device = df->device;
        int ret;
 
+       if (cmd == VFIO_DEVICE_BIND_IOMMUFD)
+               return vfio_device_ioctl_bind_iommufd(df, (void __user *)arg);
+
        /* Paired with smp_store_release() following vfio_device_open() */
        if (!smp_load_acquire(&df->access_granted))
                return -EINVAL;
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 5a34364e3b94..84d76630dd60 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -194,6 +194,37 @@ 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.
+ *
+ * User is restricted from accessing 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.
+ * @iommufd:    iommufd to bind. VFIO_NOIOMMU_FD means noiommu.
+ * @out_devid:  the device id generated by this bind. This field is valid
+ *             as long as the input @iommufd is valid. Otherwise, it is
+ *             meaningless. devid is a handle for this device and can be
+ *             used in IOMMUFD commands.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_bind_iommufd {
+       __u32           argsz;
+       __u32           flags;
+       __s32           iommufd;
+#define VFIO_NOIOMMU_FD        (-1)
+       __u32           out_devid;
+};
+
+#define VFIO_DEVICE_BIND_IOMMUFD       _IO(VFIO_TYPE, VFIO_BASE + 19)
+
 /**
  * VFIO_DEVICE_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 7,
  *                                             struct vfio_device_info)
-- 
2.34.1

Reply via email to