[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aFzTm76sdoYJ9hpG@yilunxu-OptiPlex-7050>
Date: Thu, 26 Jun 2025 12:59:07 +0800
From: Xu Yilun <yilun.xu@...ux.intel.com>
To: "Tian, Kevin" <kevin.tian@...el.com>
Cc: "jgg@...dia.com" <jgg@...dia.com>, "jgg@...pe.ca" <jgg@...pe.ca>,
"will@...nel.org" <will@...nel.org>,
"aneesh.kumar@...nel.org" <aneesh.kumar@...nel.org>,
"iommu@...ts.linux.dev" <iommu@...ts.linux.dev>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"joro@...tes.org" <joro@...tes.org>,
"robin.murphy@....com" <robin.murphy@....com>,
"shuah@...nel.org" <shuah@...nel.org>,
"nicolinc@...dia.com" <nicolinc@...dia.com>,
"aik@....com" <aik@....com>,
"Williams, Dan J" <dan.j.williams@...el.com>,
"baolu.lu@...ux.intel.com" <baolu.lu@...ux.intel.com>,
"Xu, Yilun" <yilun.xu@...el.com>
Subject: Re: [PATCH v2 3/4] iommufd: Destroy vdevice on idevice destroy
On Tue, Jun 24, 2025 at 08:22:11AM +0000, Tian, Kevin wrote:
> > From: Xu Yilun <yilun.xu@...ux.intel.com>
> > Sent: Monday, June 23, 2025 5:50 PM
> >
> > +static void iommufd_device_remove_vdev(struct iommufd_device *idev)
> > +{
> > + bool vdev_removing = false;
> > +
> > + mutex_lock(&idev->igroup->lock);
> > + if (idev->vdev) {
> > + struct iommufd_vdevice *vdev;
> > +
> > + vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
> > + if (IS_ERR(vdev)) {
> > + /* vdev is removed from xarray, but is not
> > destroyed/freed */
> > + vdev_removing = true;
> > + goto unlock;
> > + }
> > +
> > + /* Should never happen */
> > + if (WARN_ON(vdev != idev->vdev)) {
> > + idev->vdev = NULL;
> > + iommufd_put_object(idev->ictx, &vdev->obj);
> > + goto unlock;
> > + }
> > +
> > + /*
> > + * vdev cannot be destroyed after refcount_inc, safe to
> > release
>
> "vdev cannot be destroyed by userspace"
>
> > + * idev->igroup->lock and use idev->vdev afterward.
> > + */
> > + refcount_inc(&idev->vdev->obj.users);
> > + iommufd_put_object(idev->ictx, &idev->vdev->obj);
>
> s/idev->vdev/vdev/
Will adopt these fixes.
>
> > @@ -124,18 +139,28 @@ int iommufd_vdevice_alloc_ioctl(struct
> > iommufd_ucmd *ucmd)
> > goto out_put_idev;
> > }
> >
> > + 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_put_idev;
> > + goto out_unlock_igroup;
> > }
> >
> > + /* vdev can't outlive idev, vdev->idev is always valid, need no refcnt
> > */
> > + vdev->idev = idev;
> > vdev->ictx = ucmd->ictx;
> > vdev->id = virt_id;
> > vdev->dev = idev->dev;
> > get_device(idev->dev);
>
> this is not necessary now, as idevice already holds a reference to device
> and now vdevice cannot outlive idevice.
I agree.
Besides, Jason suggests use vdev->dev for iommufd_vdevice_abort() safe
reentrancy. I think we could change to use vdev->viommu.
@@ -93,10 +93,17 @@ void iommufd_vdevice_abort(struct iommufd_object *obj)
lockdep_assert_held(&idev->igroup->lock);
+ /*
+ * iommufd_vdevice_abort() could be reentrant, by
+ * iommufd_device_unbind() or by iommufd_destroy(). Cleanup only once.
+ */
+ if (!viommu)
+ return;
+
/* xa_cmpxchg is okay to fail if alloc failed xa_cmpxchg previously */
xa_cmpxchg(&viommu->vdevs, vdev->id, vdev, NULL, GFP_KERNEL);
refcount_dec(&viommu->obj.users);
- put_device(vdev->dev);
+ vdev->viommu = NULL;
idev->vdev = NULL;
Thanks,
Yilun
Powered by blists - more mailing lists