> From: Tian, Kevin <kevin.tian@xxxxxxxxx> > Sent: Wednesday, February 15, 2023 10:05 AM > > > From: Zhao, Yan Y <yan.y.zhao@xxxxxxxxx> > > Sent: Wednesday, February 15, 2023 7:39 AM > > > > On Mon, Feb 13, 2023 at 07:13:47AM -0800, Yi Liu wrote: > > ... > > > +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_claim_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); > > > + else if (df->noiommu) > > > + dev_warn(device->dev, "vfio-noiommu device used by user > " > > > + "(%s:%d)\n", current->comm, > task_pid_nr(current)); > > > > IMHO, the "smp_store_release(&df->access_granted, true);" in patch 7 > > should be moved to here when bind is indeed successful. > > > > yes. in that case patch7 should put release in vfio_device_group_open() > and then add a new release here. Right. This needs to be set in the caller instead of the vfio_device_open(). Done in the latest branch on github. Regards, Yi Liu