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@xxxxxxxxx> --- 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