> From: Zhao, Yan Y <yan.y.zhao@xxxxxxxxx> > Sent: Wednesday, February 22, 2023 3:53 PM > > On Mon, Feb 20, 2023 at 07:48:09PM -0800, Yi Liu wrote: > > 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 > > a negative iommufd value. > > > > Signed-off-by: Yi Liu <yi.l.liu@xxxxxxxxx> > > --- > > drivers/vfio/device_cdev.c | 137 > +++++++++++++++++++++++++++++++++++++ > > drivers/vfio/vfio.h | 17 ++++- > > drivers/vfio/vfio_main.c | 30 ++++++-- > > include/linux/iommufd.h | 6 ++ > > include/uapi/linux/vfio.h | 34 +++++++++ > > 5 files changed, 219 insertions(+), 5 deletions(-) > > > > diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c > > index 9e2c1ecaaf4f..be62f0a5687e 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" > > > > @@ -45,6 +46,142 @@ 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) > > + goto unlock; > > + > > + _vfio_device_get_kvm_safe(df->device, df->kvm); > > + > > +unlock: > > + spin_unlock(&df->kvm_ref_lock); > > +} > > + > > +void vfio_device_cdev_close(struct vfio_device_file *df) > > +{ > > + struct vfio_device *device = df->device; > > + > > + mutex_lock(&device->dev_set->lock); > > + if (!smp_load_acquire(&df->access_granted)) { > > + mutex_unlock(&device->dev_set->lock); > > + return; > > + } > > + vfio_device_close(df); > > + vfio_device_put_kvm(device); > > + if (df->iommufd) > > + iommufd_ctx_put(df->iommufd); > > + mutex_unlock(&device->dev_set->lock); > > + vfio_device_unblock_group(device); > > +} > > + > > +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 = NULL; > > + struct fd f; > > + unsigned long minsz; > > + int ret; > > + > > + minsz = offsetofend(struct vfio_device_bind_iommufd, out_devid); > > + > > + 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; > > + > > + ret = vfio_device_block_group(device); > > + if (ret) > > + return ret; > > + > > + mutex_lock(&device->dev_set->lock); > > + /* > > + * If already been bound to an iommufd, or already set noiommu > > + * then fail it. > > + */ > > + if (df->iommufd || df->noiommu) { > > + ret = -EINVAL; > > + goto out_unlock; > > + } > > + > > + /* iommufd < 0 means noiommu mode */ > > + if (bind.iommufd < 0) { > > + if (!capable(CAP_SYS_RAWIO)) { > > + ret = -EPERM; > > + goto out_unlock; > > + } > > + df->noiommu = true; > > + } else { > > + f = fdget(bind.iommufd); > > + if (!f.file) { > > + ret = -EBADF; > > + goto out_unlock; > > + } > > + iommufd = iommufd_ctx_from_file(f.file); > > + if (IS_ERR(iommufd)) { > > + ret = PTR_ERR(iommufd); > > + goto out_put_file; > > + } > > + } > > + > > + /* > > + * 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); > > + > > + df->iommufd = iommufd; > > + ret = vfio_device_open(df, &bind.out_devid, NULL); > > + if (ret) > > + goto out_put_kvm; > > + > > + ret = copy_to_user((void __user *)arg + > > + offsetofend(struct vfio_device_bind_iommufd, > iommufd), > > + &bind.out_devid, > > + sizeof(bind.out_devid)) ? -EFAULT : 0; > > + if (ret) > > + goto out_close_device; > > + > > + if (iommufd) > > + fdput(f); Here check f.file > > + else if (df->noiommu) > > + dev_warn(device->dev, "vfio-noiommu device used by user > " > > + "(%s:%d)\n", current->comm, > task_pid_nr(current)); > > + > > + /* > > + * Paired with smp_load_acquire() in vfio_device_fops::ioctl/ > > + * read/write/mmap > > + */ > > + smp_store_release(&df->access_granted, true); > > + mutex_unlock(&device->dev_set->lock); > > + > > + return 0; > > + > > +out_close_device: > > + vfio_device_close(df); > > +out_put_kvm: > > + df->iommufd = NULL; > > + df->noiommu = false; > > + vfio_device_put_kvm(device); > > +out_put_file: > > + if (iommufd) { > > + iommufd_ctx_put(iommufd); > > + fdput(f); > even if iommufd is NULL, still need to fdput(f) if f.file is true, right? Yes. it is. And also another line in above. 😊 may need to wrap the fdget() and iommufd_ctx_from_file() into a helper. So fdput is called in the helper. > > + } > > +out_unlock: > > + mutex_unlock(&device->dev_set->lock); > > + vfio_device_unblock_group(device); > > + return ret; > > +} > > +