mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xu Yilun <yilun.xu@linux.intel.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: "Tian, Kevin" <kevin.tian@intel.com>,
	"will@kernel.org" <will@kernel.org>,
	"aneesh.kumar@kernel.org" <aneesh.kumar@kernel.org>,
	"iommu@lists.linux.dev" <iommu@lists.linux.dev>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"joro@8bytes.org" <joro@8bytes.org>,
	"robin.murphy@arm.com" <robin.murphy@arm.com>,
	"shuah@kernel.org" <shuah@kernel.org>,
	"nicolinc@nvidia.com" <nicolinc@nvidia.com>,
	"aik@amd.com" <aik@amd.com>,
	"Williams, Dan J" <dan.j.williams@intel.com>,
	"baolu.lu@linux.intel.com" <baolu.lu@linux.intel.com>,
	"Xu, Yilun" <yilun.xu@intel.com>
Subject: Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy
Date: Mon, 7 Jul 2025 18:58:45 +0800	[thread overview]
Message-ID: <aGuoZUaNOgclC2uU@yilunxu-OptiPlex-7050> (raw)
In-Reply-To: <20250702124042.GD1051729@nvidia.com>

On Wed, Jul 02, 2025 at 09:40:42AM -0300, Jason Gunthorpe wrote:
> On Wed, Jul 02, 2025 at 09:13:50AM +0000, Tian, Kevin wrote:
> > > > Yes, you can't touch idev inside the destroy function at all, under
> > > > any version. idev is only valid if you have a refcount on vdev.
> > > >
> > > > But why are you touching this lock? Arrange things so abort doesn't
> > > > touch the idev??
> > > 
> > > idev has a pointer idev->vdev to track the vdev's lifecycle.
> > > idev->igroup->lock protects the pointer. At the end of
> > > iommufd_vdevice_destroy() this pointer should be NULLed so that idev
> > > knows vdev is really destroyed.
> 
> Well, that is destroy, not abort, but OK, there is an issue with
> destroy.
> 
> > but comparing to that I'd prefer to the original wait approach...
> 
> Okay, but lets try to keep the wait hidden inside the refcounting..
> 
> The issue here is we don't hold a refcount on idev while working with
> idev. Let's fix that and then things should work properly?
> 
> Maybe something like this:
> 
> diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
> index 4e781aa9fc6329..9174fa7c972b80 100644
> --- a/drivers/iommu/iommufd/device.c
> +++ b/drivers/iommu/iommufd/device.c
> @@ -178,12 +178,20 @@ static void iommufd_device_remove_vdev(struct iommufd_device *idev)
>  	mutex_unlock(&idev->igroup->lock);
>  }
>  
> +void iommufd_device_pre_destroy(struct iommufd_object *obj)
> +{
> +	struct iommufd_device *idev =
> +		container_of(obj, struct iommufd_device, obj);
> +
> +	/* Release the short term users on this */
> +	iommufd_device_remove_vdev(idev);
> +}
> +
>  void iommufd_device_destroy(struct iommufd_object *obj)
>  {
>  	struct iommufd_device *idev =
>  		container_of(obj, struct iommufd_device, obj);
>  
> -	iommufd_device_remove_vdev(idev);
>  	iommu_device_release_dma_owner(idev->dev);
>  	iommufd_put_group(idev->igroup);
>  	if (!iommufd_selftest_is_mock_dev(idev->dev))
> diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
> index b2e8e106c16158..387c630fdabfbd 100644
> --- a/drivers/iommu/iommufd/main.c
> +++ b/drivers/iommu/iommufd/main.c
> @@ -151,6 +151,9 @@ static int iommufd_object_dec_wait_shortterm(struct iommufd_ctx *ictx,
>  	if (refcount_dec_and_test(&to_destroy->shortterm_users))
>  		return 0;
>  
> +	if (iommufd_object_ops[to_destroy->type].pre_destroy)
> +		iommufd_object_ops[to_destroy->type].pre_destroy(to_destroy);
> +
>  	if (wait_event_timeout(ictx->destroy_wait,
>  			       refcount_read(&to_destroy->shortterm_users) ==i 0,

I still see concerns here. The pre_destroy() and wait_event is before the
idev's users count decreasing to 0, which means a new user could get in.
The worse thing is the new user could hold the shortterm_users until
pre_destroy(), but there isn't a second pre_destroy()...

I think extending the life of shortterm_users at runtime makes things
complex. My idea is keep the original definition of shortterm_users at
runtime, only repurpose it as a wait completion after users refcount == 0.
I.e. idev increase its own shortterm_users, waiting for vdev to decrease
it.

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 9876bf70980a..49b787bc0688 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -147,8 +147,15 @@ static void iommufd_device_remove_vdev(struct iommufd_device *idev)
                goto out_unlock;

        vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
-       if (IS_ERR(vdev))
+       if (IS_ERR(vdev)) {
+               /*
+               * vdev is removed from xarray by userspace, but is not
+               * destroyed. We should wait for vdev real destruction before
+               * idev destruction.
+               */
+               iommufd_object_destroy_wait_prepare(&idev->obj);
                goto out_unlock;
+       }

        /* Should never happen */
        if (WARN_ON(vdev != idev->vdev)) {
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 08f3c66366ea..6620006df8d1 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -180,6 +180,47 @@ static inline void iommufd_put_object(struct iommufd_ctx *ictx,
                wake_up_interruptible_all(&ictx->destroy_wait);
 }

+/*
+ * The iommufd_object_destroy_wait_xx() are used to synchronize the last minute
+ * destructions of interrelated objects. E.g. the vdev's destroy() should
+ * always be called before idev's destroy().
+ *
+ * iommufd_object_destroy_wait_prepare() should only be called by
+ * later-destroyer in its pre_destroy() ops, where the users & shortterm_users
+ * refcounts are all zeroed. I.e. no new user is possible. Use "users == 0 &&
+ * shortterm_users == 1" as the "destroy wait" state, as this combination
+ * is not used in runtime usecase and it also prevents new users.
+ *
+ * The later-destroyer should use their own locking to ensure the
+ * first-destroyer still exists when calling this function.
+ */
+static inline void
+iommufd_object_destroy_wait_prepare(struct iommufd_object *to_destroy)
+{
+       if (WARN_ON(refcount_read(&to_destroy->users) != 0 ||
+                   refcount_read(&to_destroy->shortterm_users) != 0))
+               return;
+
+       refcount_set(&to_destroy->shortterm_users, 1);
+}
+
+/*
+ * iommufd_object_destroy_wait_complete() should be called by first-destroyer
+ * in its destroy() ops. The first-destroyer should not access the
+ * later-destroyer after calling this function.
+ */
+static inline void
+iommufd_object_destroy_wait_complete(struct iommufd_ctx *ictx,
+                                    struct iommufd_object *to_destroy)
+{
+       if (refcount_read(&to_destroy->users) != 0 ||
+           refcount_read(&to_destroy->shortterm_users) != 1)
+               return;
+
+       refcount_dec(&to_destroy->shortterm_users);
+       wake_up_interruptible_all(&ictx->destroy_wait);
+}
+
 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);
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index fbccfbd8fb03..148218293f55 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -96,15 +96,37 @@ struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id,
        return obj;
 }

+static int iommufd_object_pre_destroy_wait(struct iommufd_ctx *ictx,
+                                          struct iommufd_object *to_destroy)
+{
+       if (!iommufd_object_ops[to_destroy->type].pre_destroy)
+               return 0;
+
+       /*
+        * pre_destroy() may put the object in destroy wait state (users == 0
+        * && shortterm_users == 1). Another thread completes the waiting by
+        * decreasing the shortterm_users back to 0. The helpers
+        * iommufd_object_destroy_wait_prepare() and
+        * iommufd_object_destroy_wait_complete() should be used for this
+        * purpose.
+        */
+       iommufd_object_ops[to_destroy->type].pre_destroy(to_destroy);
+       if (wait_event_timeout(ictx->destroy_wait,
+                              refcount_read(&to_destroy->shortterm_users) ==
+                                       0,
+                              msecs_to_jiffies(60000)))
+               return 0;
+
+       pr_crit("Time out waiting for iommufd object to become free\n");
+       return -EBUSY;
+}
+
 static int iommufd_object_dec_wait_shortterm(struct iommufd_ctx *ictx,
                                             struct iommufd_object *to_destroy)
 {
        if (refcount_dec_and_test(&to_destroy->shortterm_users))
                return 0;

-       if (iommufd_object_ops[to_destroy->type].pre_destroy)
-               iommufd_object_ops[to_destroy->type].pre_destroy(to_destroy);
-
        if (wait_event_timeout(ictx->destroy_wait,
                                refcount_read(&to_destroy->shortterm_users) ==
                                        0,
@@ -184,6 +206,10 @@ int iommufd_object_remove(struct iommufd_ctx *ictx,
                ret = iommufd_object_dec_wait_shortterm(ictx, obj);
                if (WARN_ON(ret))
                        return ret;
+       } else {
+               ret = iommufd_object_pre_destroy_wait(ictx, obj);
+               if (WARN_ON(ret))
+                       return ret;
        }

        iommufd_object_ops[obj->type].destroy(obj);
diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
index d49e8f0c8bb3..a7feefd5e472 100644
--- a/drivers/iommu/iommufd/viommu.c
+++ b/drivers/iommu/iommufd/viommu.c
@@ -103,11 +103,12 @@ void iommufd_vdevice_destroy(struct iommufd_object *obj)
 {
        struct iommufd_vdevice *vdev =
                container_of(obj, struct iommufd_vdevice, obj);
+       struct iommufd_device *idev = vdev->idev;

-       mutex_lock(&vdev->idev->igroup->lock);
+       mutex_lock(&idev->igroup->lock);
        iommufd_vdevice_abort(obj);
-       mutex_unlock(&vdev->idev->igroup->lock);
-       iommufd_put_object(vdev->viommu->ictx, &vdev->idev->obj);
+       mutex_unlock(&idev->igroup->lock);
+       iommufd_object_destroy_wait_complete(idev->ictx, &idev->obj);
 }

 int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
@@ -185,8 +186,7 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
 out_unlock_igroup:
        mutex_unlock(&idev->igroup->lock);
 out_put_idev:
-       if (rc)
-               iommufd_put_object(ucmd->ictx, &idev->obj);
+       iommufd_put_object(ucmd->ictx, &idev->obj);
 out_put_viommu:
        iommufd_put_object(ucmd->ictx, &viommu->obj);
        return rc;

  parent reply	other threads:[~2025-07-07 11:07 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-27  3:38 [PATCH v3 0/5] iommufd: Destroy vdevice on device unbind Xu Yilun
2025-06-27  3:38 ` [PATCH v3 1/5] iommufd: Add iommufd_object_tombstone_user() helper Xu Yilun
2025-06-30  3:08   ` Baolu Lu
2025-06-30  7:24     ` Xu Yilun
2025-06-30  5:52   ` Tian, Kevin
2025-06-30  6:41     ` Xu Yilun
2025-06-30 19:50   ` Nicolin Chen
2025-07-08  8:45     ` Xu Yilun
2025-06-27  3:38 ` [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy Xu Yilun
2025-06-30  5:08   ` Baolu Lu
2025-07-08  8:34     ` Xu Yilun
2025-06-30  6:27   ` Tian, Kevin
2025-06-30 10:18     ` Xu Yilun
2025-06-30 14:50       ` Jason Gunthorpe
2025-07-01  9:19         ` Xu Yilun
2025-07-01 12:13           ` Jason Gunthorpe
2025-07-02  2:23             ` Xu Yilun
2025-07-02  9:13               ` Tian, Kevin
2025-07-02 12:40                 ` Jason Gunthorpe
2025-07-03  4:32                   ` Tian, Kevin
2025-07-03 11:21                     ` Jason Gunthorpe
2025-07-07 10:58                   ` Xu Yilun [this message]
2025-07-07 12:25                     ` Jason Gunthorpe
2025-07-07 19:41                       ` Xu Yilun
2025-06-30 19:34   ` Nicolin Chen
2025-07-08  8:12     ` Xu Yilun
2025-06-27  3:38 ` [PATCH v3 3/5] iommufd/vdevice: Remove struct device reference from struct vdevice Xu Yilun
2025-06-30  5:11   ` Baolu Lu
2025-07-04 15:06   ` Jason Gunthorpe
2025-06-27  3:38 ` [PATCH v3 4/5] iommufd/selftest: Explicitly skip tests for inapplicable variant Xu Yilun
2025-07-04 15:07   ` Jason Gunthorpe
2025-06-27  3:38 ` [PATCH v3 5/5] iommufd/selftest: Add coverage for vdevice tombstone Xu Yilun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aGuoZUaNOgclC2uU@yilunxu-OptiPlex-7050 \
    --to=yilun.xu@linux.intel.com \
    --cc=aik@amd.com \
    --cc=aneesh.kumar@kernel.org \
    --cc=baolu.lu@linux.intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.com \
    --cc=shuah@kernel.org \
    --cc=will@kernel.org \
    --cc=yilun.xu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®