Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
On Tue, Oct 29, 2024 at 12:30:00PM -0700, Nicolin Chen wrote: > > iommufd_device_unbind() can't fail, and if the object can't be > > destroyed because it has an elevated long term refcount it WARN's: > > > > > > ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM); > > > > /* > > * If there is a bug and we couldn't destroy the object then we did put > > * back the caller's users refcount and will eventually try to free it > > * again during close. > > */ > > WARN_ON(ret); > > > > So you cannot take long term references on kernel owned objects. Only > > userspace owned objects. > > OK. I think I had got this part. Gao ran into this WARN_ON at v3, > so I added iommufd_object_remove(vdev_id) in unbind() prior to > this iommufd_object_destroy_user(idev->ictx, &idev->obj). Oh I see, so the fix to that is to not take a longterm reference, not to try to destroy a vdev. The alternative ould be to try to unlink the idev from the vdev and leave a zombie vdev, but that didn't look so nice to implement. If we need it we can do it later Jason
Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
On Tue, Oct 29, 2024 at 03:48:01PM -0300, Jason Gunthorpe wrote: > On Tue, Oct 29, 2024 at 10:29:56AM -0700, Nicolin Chen wrote: > > On Tue, Oct 29, 2024 at 12:58:24PM -0300, Jason Gunthorpe wrote: > > > On Fri, Oct 25, 2024 at 04:50:30PM -0700, Nicolin Chen wrote: > > > > diff --git a/drivers/iommu/iommufd/device.c > > > > b/drivers/iommu/iommufd/device.c > > > > index 5fd3dd420290..e50113305a9c 100644 > > > > --- a/drivers/iommu/iommufd/device.c > > > > +++ b/drivers/iommu/iommufd/device.c > > > > @@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, > > > > IOMMUFD); > > > > */ > > > > void iommufd_device_unbind(struct iommufd_device *idev) > > > > { > > > > + u32 vdev_id = 0; > > > > + > > > > + /* idev->vdev object should be destroyed prior, yet just in > > > > case.. */ > > > > + mutex_lock(&idev->igroup->lock); > > > > + if (idev->vdev) > > > > > > Then should it have a WARN_ON here? > > > > It'd be a user space mistake that forgot to call the destroy ioctl > > to the object, in which case I recall kernel shouldn't WARN_ON? > > But you can't get here because: > > refcount_inc(&idev->obj.users); > > And kernel doesn't destroy objects with elevated ref counts? Hmm, this is not a ->destroy() but iommufd_device_unbind called by VFIO. And we actually ran into this routine when QEMU didn't destroy vdev. So, I added this chunk. The iommufd_object_remove(vdev_id) here would destroy the vdev where its destroy() does refcount_dec(&idev->obj.users). Then, the following iommufd_object_destroy_user(.., &idev->obj) will succeed. With that said, let's just mandate userspace to destroy vdev. > > > > + vdev_id = idev->vdev->obj.id; > > > > + mutex_unlock(&idev->igroup->lock); > > > > + /* Relying on xa_lock against a race with iommufd_destroy() */ > > > > + if (vdev_id) > > > > + iommufd_object_remove(idev->ictx, NULL, vdev_id, 0); > > > > > > That doesn't seem right, iommufd_object_remove() should never be used > > > to destroy an object that userspace created with an IOCTL, in fact > > > that just isn't allowed. > > > > It was for our auto destroy feature. > > auto domains are "hidden" hwpts that are kernel managed. They are not > "userspace created". > > "Usespace created" objects are ones that userspace is expected to call > destroy on. OK. I misunderstood that. > If you destroy them behind the scenes in the kerenl then the objecd ID > can be reallocated for something else and when userspace does DESTROY > on the ID it thought was still allocated it will malfunction. > > So, only userspace can destroy objects that userspace created. I see. That makes sense. > > If user space forgot to destroy the object while trying to unplug > > the device from VM. This saves the day. > > No, it should/does fail destroy of the VIOMMU object because the users > refcount is elevated. The vIOMMU object is refcount_dec also from the unbind() calling remove(). But anyway, we aligned that userspace should destroy it explicitly. > > > Ugh, there is worse here, we can't hold a long term reference on a > > > kernel owned object: > > > > > > idev->vdev = vdev; > > > refcount_inc(&idev->obj.users); > > > > > > As it prevents the kernel from disconnecting it. > > > > Hmm, mind elaborating? I think the iommufd_fops_release() would > > xa_for_each the object list that destroys the vdev object first > > then this idev (and viommu too)? > > iommufd_device_unbind() can't fail, and if the object can't be > destroyed because it has an elevated long term refcount it WARN's: > > > ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM); > > /* >* If there is a bug and we couldn't destroy the object then we did put >* back the caller's users refcount and will eventually try to free it >* again during close. >*/ > WARN_ON(ret); > > So you cannot take long term references on kernel owned objects. Only > userspace owned objects. OK. I think I had got this part. Gao ran into this WARN_ON at v3, so I added iommufd_object_remove(vdev_id) in unbind() prior to this iommufd_object_destroy_user(idev->ictx, &idev->obj). > > OK. If user space forgot to destroy its vdev while unplugging the > > device, it would not be allowed to hotplug another device (or the > > same device) back to the same slot having the same RID, since the > > RID on the vIOMMU would be occupied by the undestroyed vdev. > > Yes, that seems correct and obvious to me. Until the vdev is > explicitly destroyed the ID is in-use. > > Good userspace should destroy the iommufd vDEVICE object before > closing the VFIO file descriptor. > > If it doesn't, then the VDEVICE object remains even though the VFIO it > was linked to is gone. I see. Thanks Nicolin
Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
On Tue, Oct 29, 2024 at 10:29:56AM -0700, Nicolin Chen wrote: > On Tue, Oct 29, 2024 at 12:58:24PM -0300, Jason Gunthorpe wrote: > > On Fri, Oct 25, 2024 at 04:50:30PM -0700, Nicolin Chen wrote: > > > diff --git a/drivers/iommu/iommufd/device.c > > > b/drivers/iommu/iommufd/device.c > > > index 5fd3dd420290..e50113305a9c 100644 > > > --- a/drivers/iommu/iommufd/device.c > > > +++ b/drivers/iommu/iommufd/device.c > > > @@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); > > > */ > > > void iommufd_device_unbind(struct iommufd_device *idev) > > > { > > > + u32 vdev_id = 0; > > > + > > > + /* idev->vdev object should be destroyed prior, yet just in case.. */ > > > + mutex_lock(&idev->igroup->lock); > > > + if (idev->vdev) > > > > Then should it have a WARN_ON here? > > It'd be a user space mistake that forgot to call the destroy ioctl > to the object, in which case I recall kernel shouldn't WARN_ON? But you can't get here because: refcount_inc(&idev->obj.users); And kernel doesn't destroy objects with elevated ref counts? > > > + vdev_id = idev->vdev->obj.id; > > > + mutex_unlock(&idev->igroup->lock); > > > + /* Relying on xa_lock against a race with iommufd_destroy() */ > > > + if (vdev_id) > > > + iommufd_object_remove(idev->ictx, NULL, vdev_id, 0); > > > > That doesn't seem right, iommufd_object_remove() should never be used > > to destroy an object that userspace created with an IOCTL, in fact > > that just isn't allowed. > > It was for our auto destroy feature. auto domains are "hidden" hwpts that are kernel managed. They are not "userspace created". "Usespace created" objects are ones that userspace is expected to call destroy on. If you destroy them behind the scenes in the kerenl then the objecd ID can be reallocated for something else and when userspace does DESTROY on the ID it thought was still allocated it will malfunction. So, only userspace can destroy objects that userspace created. > If user space forgot to destroy the object while trying to unplug > the device from VM. This saves the day. No, it should/does fail destroy of the VIOMMU object because the users refcount is elevated. > > Ugh, there is worse here, we can't hold a long term reference on a > > kernel owned object: > > > > idev->vdev = vdev; > > refcount_inc(&idev->obj.users); > > > > As it prevents the kernel from disconnecting it. > > Hmm, mind elaborating? I think the iommufd_fops_release() would > xa_for_each the object list that destroys the vdev object first > then this idev (and viommu too)? iommufd_device_unbind() can't fail, and if the object can't be destroyed because it has an elevated long term refcount it WARN's: ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM); /* * If there is a bug and we couldn't destroy the object then we did put * back the caller's users refcount and will eventually try to free it * again during close. */ WARN_ON(ret); So you cannot take long term references on kernel owned objects. Only userspace owned objects. > OK. If user space forgot to destroy its vdev while unplugging the > device, it would not be allowed to hotplug another device (or the > same device) back to the same slot having the same RID, since the > RID on the vIOMMU would be occupied by the undestroyed vdev. Yes, that seems correct and obvious to me. Until the vdev is explicitly destroyed the ID is in-use. Good userspace should destroy the iommufd vDEVICE object before closing the VFIO file descriptor. If it doesn't, then the VDEVICE object remains even though the VFIO it was linked to is gone. Jason
Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
On Tue, Oct 29, 2024 at 12:58:24PM -0300, Jason Gunthorpe wrote: > On Fri, Oct 25, 2024 at 04:50:30PM -0700, Nicolin Chen wrote: > > diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c > > index 5fd3dd420290..e50113305a9c 100644 > > --- a/drivers/iommu/iommufd/device.c > > +++ b/drivers/iommu/iommufd/device.c > > @@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); > > */ > > void iommufd_device_unbind(struct iommufd_device *idev) > > { > > + u32 vdev_id = 0; > > + > > + /* idev->vdev object should be destroyed prior, yet just in case.. */ > > + mutex_lock(&idev->igroup->lock); > > + if (idev->vdev) > > Then should it have a WARN_ON here? It'd be a user space mistake that forgot to call the destroy ioctl to the object, in which case I recall kernel shouldn't WARN_ON? > > + vdev_id = idev->vdev->obj.id; > > + mutex_unlock(&idev->igroup->lock); > > + /* Relying on xa_lock against a race with iommufd_destroy() */ > > + if (vdev_id) > > + iommufd_object_remove(idev->ictx, NULL, vdev_id, 0); > > That doesn't seem right, iommufd_object_remove() should never be used > to destroy an object that userspace created with an IOCTL, in fact > that just isn't allowed. It was for our auto destroy feature. If user space forgot to destroy the object while trying to unplug the device from VM. This saves the day. > Ugh, there is worse here, we can't hold a long term reference on a > kernel owned object: > > idev->vdev = vdev; > refcount_inc(&idev->obj.users); > > As it prevents the kernel from disconnecting it. Hmm, mind elaborating? I think the iommufd_fops_release() would xa_for_each the object list that destroys the vdev object first then this idev (and viommu too)? > I came up with this that seems like it will work. Maybe we will need > to improve it later. Instead of using the idev, just keep the raw > struct device. We can hold a refcount on the struct device without > races. There is no need for the idev igroup lock since the xa_lock > does everything we need. OK. If user space forgot to destroy its vdev while unplugging the device, it would not be allowed to hotplug another device (or the same device) back to the same slot having the same RID, since the RID on the vIOMMU would be occupied by the undestroyed vdev. If we decide to do so, I think we should highlight this somewhere in the doc. Thanks Nicolin
Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
On Fri, Oct 25, 2024 at 04:50:30PM -0700, Nicolin Chen wrote: > +/** > + * struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC) > + * @size: sizeof(struct iommu_vdevice_alloc) > + * @viommu_id: vIOMMU ID to associate with the virtual device > + * @dev_id: The pyhsical device to allocate a virtual instance on the vIOMMU > + * @__reserved: Must be 0 > + * @virt_id: Virtual device ID per vIOMMU, e.g. vSID of ARM SMMUv3, vDeviceID > + * of AMD IOMMU, and vID of a nested Intel VT-d to a Context Table. > + * @out_vdevice_id: Output virtual instance ID for the allocated object How about: @out_vdevice_id: Object handle for the vDevice. Pass to IOMMU_DESTORY > + * Allocate a virtual device instance (for a physical device) against a > vIOMMU. > + * This instance holds the device's information (related to its vIOMMU) in a > VM. > + */ > +struct iommu_vdevice_alloc { > + __u32 size; > + __u32 viommu_id; > + __u32 dev_id; > + __u32 __reserved; > + __aligned_u64 virt_id; > + __u32 out_vdevice_id; > + __u32 __reserved2; Lets not have two u32 reserved, put the out_vdevice_id above virt_id > diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c > index 5fd3dd420290..e50113305a9c 100644 > --- a/drivers/iommu/iommufd/device.c > +++ b/drivers/iommu/iommufd/device.c > @@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); > */ > void iommufd_device_unbind(struct iommufd_device *idev) > { > + u32 vdev_id = 0; > + > + /* idev->vdev object should be destroyed prior, yet just in case.. */ > + mutex_lock(&idev->igroup->lock); > + if (idev->vdev) Then should it have a WARN_ON here? > + vdev_id = idev->vdev->obj.id; > + mutex_unlock(&idev->igroup->lock); > + /* Relying on xa_lock against a race with iommufd_destroy() */ > + if (vdev_id) > + iommufd_object_remove(idev->ictx, NULL, vdev_id, 0); That doesn't seem right, iommufd_object_remove() should never be used to destroy an object that userspace created with an IOCTL, in fact that just isn't allowed. Ugh, there is worse here, we can't hold a long term reference on a kernel owned object: idev->vdev = vdev; refcount_inc(&idev->obj.users); As it prevents the kernel from disconnecting it. I came up with this that seems like it will work. Maybe we will need to improve it later. Instead of using the idev, just keep the raw struct device. We can hold a refcount on the struct device without races. There is no need for the idev igroup lock since the xa_lock does everything we need. diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index e50113305a9c47..5fd3dd42029015 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -277,17 +277,6 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); */ void iommufd_device_unbind(struct iommufd_device *idev) { - u32 vdev_id = 0; - - /* idev->vdev object should be destroyed prior, yet just in case.. */ - mutex_lock(&idev->igroup->lock); - if (idev->vdev) - vdev_id = idev->vdev->obj.id; - mutex_unlock(&idev->igroup->lock); - /* Relying on xa_lock against a race with iommufd_destroy() */ - if (vdev_id) - iommufd_object_remove(idev->ictx, NULL, vdev_id, 0); - iommufd_object_destroy_user(idev->ictx, &idev->obj); } EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD); diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c index 9849474f429f98..6e870bce2a0cd0 100644 --- a/drivers/iommu/iommufd/driver.c +++ b/drivers/iommu/iommufd/driver.c @@ -46,6 +46,6 @@ struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu, lockdep_assert_held(&viommu->vdevs.xa_lock); vdev = xa_load(&viommu->vdevs, vdev_id); - return vdev ? vdev->idev->dev : NULL; + return vdev ? vdev->dev : NULL; } EXPORT_SYMBOL_NS_GPL(iommufd_viommu_find_dev, IOMMUFD); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 365cf5a56cdf20..275f954235940c 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -152,9 +152,6 @@ static inline void iommufd_put_object(struct iommufd_ctx *ictx, wake_up_interruptible_all(&ictx->destroy_wait); } -int iommufd_verify_unfinalized_object(struct iommufd_ctx *ictx, - struct iommufd_object *to_verify); - void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj); void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx, struct iommufd_object *obj); @@ -391,7 +388,6 @@ struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; struct iommufd_group *igroup; - struct iommufd_vdevice *vdev; struct list_head group_item; /*
Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
On Mon, Oct 28, 2024 at 03:11:32AM +, Tian, Kevin wrote: > > From: Nicolin Chen > > Sent: Saturday, October 26, 2024 7:51 AM > > > > + > > +/** > > + * struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC) > > + * @size: sizeof(struct iommu_vdevice_alloc) > > + * @viommu_id: vIOMMU ID to associate with the virtual device > > + * @dev_id: The pyhsical device to allocate a virtual instance on the > > vIOMMU > > s/pyhsical/physical/, or just say 'iommufd device" Ack for "physical", aligning with other @dev_id lines. > > + > > +int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) > > +{ > > + struct iommu_vdevice_alloc *cmd = ucmd->cmd; > > + struct iommufd_vdevice *vdev, *curr; > > + struct iommufd_viommu *viommu; > > + struct iommufd_device *idev; > > + u64 virt_id = cmd->virt_id; > > + int rc = 0; > > + > > + if (virt_id > ULONG_MAX) > > + return -EINVAL; > > + > > + viommu = iommufd_get_viommu(ucmd, cmd->viommu_id); > > + if (IS_ERR(viommu)) > > + return PTR_ERR(viommu); > > + > > + idev = iommufd_get_device(ucmd, cmd->dev_id); > > + if (IS_ERR(idev)) { > > + rc = PTR_ERR(idev); > > + goto out_put_viommu; > > + } > > + > > + mutex_lock(&idev->igroup->lock); > > + if (idev->vdev) { > > + rc = -EEXIST; > > + goto out_unlock_igroup; > > + } > > + > > + vdev = iommufd_object_alloc(ucmd->ictx, vdev, > > IOMMUFD_OBJ_VDEVICE); > > + if (IS_ERR(vdev)) { > > + rc = PTR_ERR(vdev); > > + goto out_unlock_igroup; > > + } > > + > > also need to check that the device and the viommu are associated > to a same physical iommu. Ack. Will add this prior to mutex_lock(&idev->igroup->lock); + if (viommu->iommu_dev != __iommu_get_iommu_dev(idev->dev)) { + rc = -EINVAL; + goto out_put_idev; + } Thanks! Nicolin
RE: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
> From: Nicolin Chen > Sent: Saturday, October 26, 2024 7:51 AM > > + > +/** > + * struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC) > + * @size: sizeof(struct iommu_vdevice_alloc) > + * @viommu_id: vIOMMU ID to associate with the virtual device > + * @dev_id: The pyhsical device to allocate a virtual instance on the > vIOMMU s/pyhsical/physical/, or just say 'iommufd device" > + > +int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) > +{ > + struct iommu_vdevice_alloc *cmd = ucmd->cmd; > + struct iommufd_vdevice *vdev, *curr; > + struct iommufd_viommu *viommu; > + struct iommufd_device *idev; > + u64 virt_id = cmd->virt_id; > + int rc = 0; > + > + if (virt_id > ULONG_MAX) > + return -EINVAL; > + > + viommu = iommufd_get_viommu(ucmd, cmd->viommu_id); > + if (IS_ERR(viommu)) > + return PTR_ERR(viommu); > + > + idev = iommufd_get_device(ucmd, cmd->dev_id); > + if (IS_ERR(idev)) { > + rc = PTR_ERR(idev); > + goto out_put_viommu; > + } > + > + mutex_lock(&idev->igroup->lock); > + if (idev->vdev) { > + rc = -EEXIST; > + goto out_unlock_igroup; > + } > + > + vdev = iommufd_object_alloc(ucmd->ictx, vdev, > IOMMUFD_OBJ_VDEVICE); > + if (IS_ERR(vdev)) { > + rc = PTR_ERR(vdev); > + goto out_unlock_igroup; > + } > + also need to check that the device and the viommu are associated to a same physical iommu. > + rc = iommufd_verify_unfinalized_object(ucmd->ictx, &vdev->obj); > + if (rc) { > + kfree(vdev); > + goto out_unlock_igroup; > + } > + > + vdev->idev = idev; > + vdev->id = virt_id; > + vdev->viommu = viommu; > + > + idev->vdev = vdev; > + refcount_inc(&idev->obj.users); > + refcount_inc(&viommu->obj.users); > + > + curr = xa_cmpxchg(&viommu->vdevs, virt_id, NULL, vdev, > GFP_KERNEL); > + if (curr) { > + rc = xa_err(curr) ?: -EBUSY; > + goto out_abort; > + } > + > + cmd->out_vdevice_id = vdev->obj.id; > + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); > + if (rc) > + goto out_abort; > + iommufd_object_finalize(ucmd->ictx, &vdev->obj); > + goto out_unlock_igroup; > + > +out_abort: > + iommufd_object_abort_and_destroy(ucmd->ictx, &vdev->obj); > +out_unlock_igroup: > + mutex_unlock(&idev->igroup->lock); > + iommufd_put_object(ucmd->ictx, &idev->obj); > +out_put_viommu: > + iommufd_put_object(ucmd->ictx, &viommu->obj); > + return rc; > +} > -- > 2.43.0