Skip to contentSkip to conversation

iommufd: Destroy vdevice on device unbind

PATCH Superseded Xu Yilun 5 participants 32 messages Jun 27, 2025 – Jul 8, 2025

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Xu Yilun

author
#0 lore raw

v3 cover iommufd: Destroy vdevice on device unbind

It is to solve the lifecycle issue that vdevice may outlive idevice. It is a prerequisite for TIO, to ensure extra secure configurations (e.g. TSM Bind/Unbind) against vdevice could be rolled back on idevice unbind, so that VFIO could still work on the physical device without surprise.

Changelog:
v3:
- No bother clean each tombstone in iommufd_fops_release().
- Drop vdev->ictx initialization fix patch.
- Optimize control flow in iommufd_device_remove_vdev().
- Make iommufd_vdevice_abort() reentrant.
- Call iommufd_vdevice_abort() directly instead of waiting for it.
- Rephrase/fix some comments.
- A new patch to remove vdev->dev.
- A new patch to explicitly skip existing viommu tests for no_iommu.
- Also skip vdevice tombstone test for no_iommu.
- Allow me to add SoB from Aneesh.

v2: https://lore.kernel.org/linux-iommu/[email protected]/

v1/rfc: https://lore.kernel.org/linux-iommu/[email protected]/

The series is based on v6.16-rc1

Xu Yilun (5):
iommufd: Add iommufd_object_tombstone_user() helper
iommufd: Destroy vdevice on idevice destroy iommufd/vdevice: Remove struct device reference from struct vdevice iommufd/selftest: Explicitly skip tests for inapplicable variant iommufd/selftest: Add coverage for vdevice tombstone

 drivers/iommu/iommufd/device.c          |  42 +++
 drivers/iommu/iommufd/driver.c          |   4 +-
 drivers/iommu/iommufd/iommufd_private.h |  35 ++-
 drivers/iommu/iommufd/main.c            |  20 +-
 drivers/iommu/iommufd/viommu.c          |  47 ++-
 tools/testing/selftests/iommu/iommufd.c | 388 ++++++++++++------------
 6 files changed, 337 insertions(+), 199 deletions(-)

base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 1/5 iommufd: Add iommufd_object_tombstone_user() helper

Add the iommufd_object_tombstone_user() helper, which allows the caller to destroy an iommufd object created by userspace.

This is useful on some destroy paths when the kernel caller finds the object should have been removed by userspace but is still alive. With this helper, the caller destroys the object but leave the object ID reserved (so called tombstone). The tombstone prevents repurposing the object ID without awareness of the original user.

Since this happens for abnomal userspace behavior, for simplicity, the tombstoned object ID would be permanently leaked until iommufd_fops_release(). I.e. the original user gets an error when calling ioctl(IOMMU_DESTROY) on that ID.

The first use case would be to ensure the iommufd_vdevice can't outlive the associated iommufd_device.

---
 drivers/iommu/iommufd/iommufd_private.h | 23 ++++++++++++++++++++++-
 drivers/iommu/iommufd/main.c            | 19 ++++++++++++++++---
 2 files changed, 38 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +22 -1
index 9ccc83341f32..fbc9ef78d81f 100644
@@ -187,7 +187,8 @@ void iommufd_object_finalize(struct iommufd_ctx *ictx,
 			     struct iommufd_object *obj);
 
 enum {
-	REMOVE_WAIT_SHORTTERM = 1,
+	REMOVE_WAIT_SHORTTERM	= BIT(0),
+	REMOVE_OBJ_TOMBSTONE	= BIT(1),
 };
 int iommufd_object_remove(struct iommufd_ctx *ictx,
 			  struct iommufd_object *to_destroy, u32 id,
@@ -213,6 +214,26 @@ static inline void iommufd_object_destroy_user(struct iommufd_ctx *ictx,
 	WARN_ON(ret);
 }
 
+/*
+ * Similar to iommufd_object_destroy_user(), except that the object ID is left
+ * reserved/tombstoned.
+ */
+static inline void iommufd_object_tombstone_user(struct iommufd_ctx *ictx,
+						 struct iommufd_object *obj)
+{
+	int ret;
+
+	ret = iommufd_object_remove(ictx, obj, obj->id,
+				    REMOVE_WAIT_SHORTTERM | REMOVE_OBJ_TOMBSTONE);
+
+	/*
+	 * 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);
+}
+
 /*
  * The HWPT allocated by autodomains is used in possibly many devices and
  * is automatically destroyed when its refcount reaches zero.
drivers/iommu/iommufd/main.c +16 -3
index 3df468f64e7d..620923669b42 100644
@@ -167,7 +167,7 @@ int iommufd_object_remove(struct iommufd_ctx *ictx,
 		goto err_xa;
 	}
 
-	xas_store(&xas, NULL);
+	xas_store(&xas, (flags & REMOVE_OBJ_TOMBSTONE) ? XA_ZERO_ENTRY : NULL);
 	if (ictx->vfio_ioas == container_of(obj, struct iommufd_ioas, obj))
 		ictx->vfio_ioas = NULL;
 	xa_unlock(&ictx->objects);
@@ -239,6 +239,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp)
 	struct iommufd_sw_msi_map *next;
 	struct iommufd_sw_msi_map *cur;
 	struct iommufd_object *obj;
+	bool empty;
 
 	/*
 	 * The objects in the xarray form a graph of "users" counts, and we have
@@ -249,23 +250,35 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp)
 	 * until the entire list is destroyed. If this can't progress then there
 	 * is some bug related to object refcounting.
 	 */
-	while (!xa_empty(&ictx->objects)) {
+	for (;;) {
 		unsigned int destroyed = 0;
 		unsigned long index;
 
+		empty = true;
 		xa_for_each(&ictx->objects, index, obj) {
+			empty = false;
 			if (!refcount_dec_if_one(&obj->users))
 				continue;
+
 			destroyed++;
 			xa_erase(&ictx->objects, index);
 			iommufd_object_ops[obj->type].destroy(obj);
 			kfree(obj);
 		}
+
+		if (empty)
+			break;
+
 		/* Bug related to users refcount */
 		if (WARN_ON(!destroyed))
 			break;
 	}
-	WARN_ON(!xa_empty(&ictx->groups));
+
+	/*
+	 * There may be some tombstones left over from
+	 * iommufd_object_tombstone_user()
+	 */
+	xa_destroy(&ictx->groups);
 
 	mutex_destroy(&ictx->sw_msi_lock);
 	list_for_each_entry_safe(cur, next, &ictx->sw_msi_list, sw_msi_item)
Signature · 1 line
2.25.1
Reviewed-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] iommufd: Add iommufd_object_tombstone_user() helper

Quoted 90 lines · Xu Yilun

Add the iommufd_object_tombstone_user() helper, which allows the caller to destroy an iommufd object created by userspace.

This is useful on some destroy paths when the kernel caller finds the object should have been removed by userspace but is still alive. With this helper, the caller destroys the object but leave the object ID reserved (so called tombstone). The tombstone prevents repurposing the object ID without awareness of the original user.

Since this happens for abnomal userspace behavior, for simplicity, the tombstoned object ID would be permanently leaked until iommufd_fops_release(). I.e. the original user gets an error when calling ioctl(IOMMU_DESTROY) on that ID.

The first use case would be to ensure the iommufd_vdevice can't outlive the associated iommufd_device.

---
  drivers/iommu/iommufd/iommufd_private.h | 23 ++++++++++++++++++++++-
  drivers/iommu/iommufd/main.c            | 19 ++++++++++++++++---
  2 files changed, 38 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +22 -1
index 9ccc83341f32..fbc9ef78d81f 100644
@@ -187,7 +187,8 @@ void iommufd_object_finalize(struct iommufd_ctx *ictx,
  			     struct iommufd_object *obj);
 
  enum {
-	REMOVE_WAIT_SHORTTERM = 1,
+	REMOVE_WAIT_SHORTTERM	= BIT(0),
+	REMOVE_OBJ_TOMBSTONE	= BIT(1),
  };
  int iommufd_object_remove(struct iommufd_ctx *ictx,
  			  struct iommufd_object *to_destroy, u32 id,
@@ -213,6 +214,26 @@ static inline void iommufd_object_destroy_user(struct iommufd_ctx *ictx,
  	WARN_ON(ret);
  }
 
+/*
+ * Similar to iommufd_object_destroy_user(), except that the object ID is left
+ * reserved/tombstoned.
+ */
+static inline void iommufd_object_tombstone_user(struct iommufd_ctx *ictx,
+						 struct iommufd_object *obj)
+{
+	int ret;
+
+	ret = iommufd_object_remove(ictx, obj, obj->id,
+				    REMOVE_WAIT_SHORTTERM | REMOVE_OBJ_TOMBSTONE);
+
+	/*
+	 * 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);
+}
+
  /*
   * The HWPT allocated by autodomains is used in possibly many devices and
   * is automatically destroyed when its refcount reaches zero.
drivers/iommu/iommufd/main.c +2 -1
index 3df468f64e7d..620923669b42 100644
@@ -167,7 +167,7 @@ int iommufd_object_remove(struct iommufd_ctx *ictx,
  		goto err_xa;
  	}
 
-	xas_store(&xas, NULL);
+	xas_store(&xas, (flags & REMOVE_OBJ_TOMBSTONE) ? XA_ZERO_ENTRY : NULL);
  	if (ictx->vfio_ioas == container_of(obj, struct iommufd_ioas, obj))
  		ictx->vfio_ioas = NULL;
  	xa_unlock(&ictx->objects);
@@ -239,6 +239,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp)
  	struct iommufd_sw_msi_map *next;
  	struct iommufd_sw_msi_map *cur;
  	struct iommufd_object *obj;
+	bool empty;
 
  	/*
  	 * The objects in the xarray form a graph of "users" counts, and we have
@@ -249,23 +250,35 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp)
  	 * until the entire list is destroyed. If this can't progress then there

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode *inode, struct file *filp) · line 259

Quoted 10 lines
drivers/iommu/iommufd/main.c +3 -1
  	 * is some bug related to object refcounting.
  	 */
-	while (!xa_empty(&ictx->objects)) {
+	for (;;) {
  		unsigned int destroyed = 0;
  		unsigned long index;
 
+		empty = true;
  		xa_for_each(&ictx->objects, index, obj) {
+			empty = false;

People like me, who don't know the details of how xa_for_each() and xa_empty() work, will ask why "empty = xa_empty()" isn't used here. So it's better to add some comments to make it more readable.

/* XA_ZERO_ENTRY is treated as a null entry by xa_for_each(). */

Others look good to me.

Quoted 6 lines
drivers/iommu/iommufd/main.c +1 -0
  			if (!refcount_dec_if_one(&obj->users))
  				continue;
+
  			destroyed++;
  			xa_erase(&ictx->objects, index);
  			iommufd_object_ops[obj->type].destroy(obj);

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode *inode, struct file *filp) · line 275

Quoted 10 lines
drivers/iommu/iommufd/main.c +4 -0
  			kfree(obj);
  		}
+
+		if (empty)
+			break;
+
  		/* Bug related to users refcount */
  		if (WARN_ON(!destroyed))
  			break;
  	}

Thanks,
baolu

author
#3 lore raw

Re: [PATCH v3 1/5] iommufd: Add iommufd_object_tombstone_user() helper

Quoted 12 lines

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode *inode, struct file *filp) · line 259

Quoted 7 lines
drivers/iommu/iommufd/main.c +3 -1
-	while (!xa_empty(&ictx->objects)) {
+	for (;;) {
  		unsigned int destroyed = 0;
  		unsigned long index;
+		empty = true;
  		xa_for_each(&ictx->objects, index, obj) {
+			empty = false;

People like me, who don't know the details of how xa_for_each() and xa_empty() work, will ask why "empty = xa_empty()" isn't used here. So it's better to add some comments to make it more readable.

Quoted 1 line

/* XA_ZERO_ENTRY is treated as a null entry by xa_for_each(). */

Yes. I try to make it more elaborate:

+ /*

+                * xa_for_each() will not return tomestones (zeroed entries),
+                * which prevent the xarray being empty. So use an empty flag
+                * instead of xa_empty() to indicate all entries are either
+                * NULLed or tomestoned.
+                */
                empty = true;

Thanks,
Yilun

Quoted 4 lines

Others look good to me.

maintainer
#4 lore raw

RE: [PATCH v3 1/5] iommufd: Add iommufd_object_tombstone_user() helper

In reply to Xu Yilun

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode *inode, · line 243

Quoted 9 lines

From: Xu Yilun <[email protected]>
Sent: Friday, June 27, 2025 11:38 AM

drivers/iommu/iommufd/main.c +1 -0
@@ -239,6 +239,7 @@ static int iommufd_fops_release(struct inode *inode,
 struct file *filp)
 	struct iommufd_sw_msi_map *next;
 	struct iommufd_sw_msi_map *cur;
 	struct iommufd_object *obj;
+	bool empty;

move into for(;;) loop

Quoted 24 lines
(quoted diff) +0 -0
 	/*
 	 * The objects in the xarray form a graph of "users" counts, and we

have

drivers/iommu/iommufd/main.c +4 -1
@@ -249,23 +250,35 @@ static int iommufd_fops_release(struct inode
 *inode, struct file *filp)
 	 * until the entire list is destroyed. If this can't progress then there
 	 * is some bug related to object refcounting.
 	 */
-	while (!xa_empty(&ictx->objects)) {
+	for (;;) {
 		unsigned int destroyed = 0;
 		unsigned long index;
 
+		empty = true;
 		xa_for_each(&ictx->objects, index, obj) {
+			empty = false;
 			if (!refcount_dec_if_one(&obj->users))
 				continue;
+
 			destroyed++;
 			xa_erase(&ictx->objects, index);
 			iommufd_object_ops[obj->type].destroy(obj);
 			kfree(obj);

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode · line 269

Quoted 10 lines
drivers/iommu/iommufd/main.c +4 -1
 		}
+
+		if (empty)
+			break;
+
 		/* Bug related to users refcount */
 		if (WARN_ON(!destroyed))
 			break;
 	}
-	WARN_ON(!xa_empty(&ictx->groups));

I didn't get the rationale of this change. tombstone only means the object ID reserved, but all the destroy work for the object has been done, e.g. after all idevice objects are destroyed there should be no valid igroup entries in ictx->groups (and there is no tombstone state for igroup).

Is it a wrong change by misreading ictx->groups as ictx->objects?

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode *inode, struct file *filp) · line 282

Quoted 7 lines
drivers/iommu/iommufd/main.c +6 -0
+
+	/*
+	 * There may be some tombstones left over from
+	 * iommufd_object_tombstone_user()
+	 */
+	xa_destroy(&ictx->groups);
 

And here should be ictx->objects?

author
#5 lore raw

Re: [PATCH v3 1/5] iommufd: Add iommufd_object_tombstone_user() helper

Quoted 11 lines · Kevin

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode *inode, · line 243

Quoted 9 lines

From: Xu Yilun <[email protected]>
Sent: Friday, June 27, 2025 11:38 AM

drivers/iommu/iommufd/main.c +1 -0
@@ -239,6 +239,7 @@ static int iommufd_fops_release(struct inode *inode,
 struct file *filp)
 	struct iommufd_sw_msi_map *next;
 	struct iommufd_sw_msi_map *cur;
 	struct iommufd_object *obj;
+	bool empty;

move into for(;;) loop

Yes.

Quoted 42 lines
Quoted 25 lines
(quoted diff) +0 -0
 	/*
 	 * The objects in the xarray form a graph of "users" counts, and we

have

drivers/iommu/iommufd/main.c +4 -1
@@ -249,23 +250,35 @@ static int iommufd_fops_release(struct inode
 *inode, struct file *filp)
 	 * until the entire list is destroyed. If this can't progress then there
 	 * is some bug related to object refcounting.
 	 */
-	while (!xa_empty(&ictx->objects)) {
+	for (;;) {
 		unsigned int destroyed = 0;
 		unsigned long index;
 
+		empty = true;
 		xa_for_each(&ictx->objects, index, obj) {
+			empty = false;
 			if (!refcount_dec_if_one(&obj->users))
 				continue;
+
 			destroyed++;
 			xa_erase(&ictx->objects, index);
 			iommufd_object_ops[obj->type].destroy(obj);
 			kfree(obj);

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode · line 269

Quoted 10 lines
drivers/iommu/iommufd/main.c +4 -1
 		}
+
+		if (empty)
+			break;
+
 		/* Bug related to users refcount */
 		if (WARN_ON(!destroyed))
 			break;
 	}
-	WARN_ON(!xa_empty(&ictx->groups));

I didn't get the rationale of this change. tombstone only means the object ID reserved, but all the destroy work for the object has been done, e.g. after all idevice objects are destroyed there should be no valid igroup entries in ictx->groups (and there is no tombstone state for igroup).

Quoted 1 line

Is it a wrong change by misreading ictx->groups as ictx->objects?

Sorry, this is a true mistake.

Quoted 10 lines

drivers/iommu/iommufd/main.c static int iommufd_fops_release(struct inode *inode, struct file *filp) · line 282

Quoted 8 lines
drivers/iommu/iommufd/main.c +6 -0
+
+	/*
+	 * There may be some tombstones left over from
+	 * iommufd_object_tombstone_user()
+	 */
+	xa_destroy(&ictx->groups);
 

And here should be ictx->objects?

Yes, thanks for catching up.

- xa_destroy(&ictx->groups);
+ xa_destroy(&ictx->objects);
+
+ WARN_ON(!xa_empty(&ictx->groups));

Thanks,
Yilun

#6 lore raw

Re: [PATCH v3 1/5] iommufd: Add iommufd_object_tombstone_user() helper

In reply to Xu Yilun
Quoted 2 lines · Xu Yilun

Add the iommufd_object_tombstone_user() helper, which allows the caller to destroy an iommufd object created by userspace.

Should we describe it "partially destroy"?

Quoted 7 lines

This is useful on some destroy paths when the kernel caller finds the object should have been removed by userspace but is still alive. With this helper, the caller destroys the object but leave the object ID reserved (so called tombstone). The tombstone prevents repurposing the object ID without awareness of the original user.

Since this happens for abnomal userspace behavior, for simplicity, the

s/abnomal/abnormal

Nicolin

author Applied
#7 lore raw

Re: [PATCH v3 1/5] iommufd: Add iommufd_object_tombstone_user() helper

Quoted 5 lines · Nicolin Chen
Quoted 2 lines · Xu Yilun

Add the iommufd_object_tombstone_user() helper, which allows the caller to destroy an iommufd object created by userspace.

Should we describe it "partially destroy"?

I don't prefer "partially destroy", it gives the impression the object is still in memory, in fact with the helper it is totally destroyed and freed. Only the ID is reserved, but the ID actually has nothing to do with the object anymore.

Quoted 10 lines
Quoted 8 lines

This is useful on some destroy paths when the kernel caller finds the object should have been removed by userspace but is still alive. With this helper, the caller destroys the object but leave the object ID reserved (so called tombstone). The tombstone prevents repurposing the object ID without awareness of the original user.

Since this happens for abnomal userspace behavior, for simplicity, the

s/abnomal/abnormal

Applied, thanks for catching.

Quoted 2 lines

Nicolin

author
#8 lore raw

PATCH v3 2/5 iommufd: Destroy vdevice on idevice destroy

In reply to Xu Yilun

Destroy iommufd_vdevice (vdev) on iommufd_idevice (idev) destroy so that vdev can't outlive idev.

iommufd_device (idev) represents the physical device bound to iommufd, while the iommufd_vdevice (vdev) represents the virtual instance of the physical device in the VM. The lifecycle of the vdev should not be longer than idev. This doesn't cause real problem on existing use cases cause vdev doesn't impact the physical device, only provides virtualization information. But to extend vdev for Confidential Computing (CC), there are needs to do secure configuration for the vdev, e.g. TSM Bind/Unbind. These configurations should be rolled back on idev destroy, or the external driver (VFIO) functionality may be impact.

Building the association between idev & vdev requires the two objects pointing each other, but not referencing each other. This requires proper locking. This is done by reviving some of Nicolin's patch [1].

There are 3 cases on idev destroy:

1. vdev is already destroyed by userspace. No extra handling needed.
2. vdev is still alive. Use iommufd_object_tombstone_user() to destroy vdev and tombstone the vdev ID.
3. vdev is being destroyed by userspace. The vdev ID is already freed, but vdev destroy handler is not completed. Destroy the vdev immediately. To solve the racing with userspace destruction, make iommufd_vdevice_abort() reentrant.

[1]: https://lore.kernel.org/all/53025c827c44d68edb6469bfd940a8e8bc6147a5.1729897278.git.nicolinc@nvidia.com/

---
 drivers/iommu/iommufd/device.c          | 42 +++++++++++++++++++++++
 drivers/iommu/iommufd/iommufd_private.h | 11 +++++++
 drivers/iommu/iommufd/main.c            |  1 +
 drivers/iommu/iommufd/viommu.c          | 44 +++++++++++++++++++++++--
 4 files changed, 95 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/device.c +42 -0
index 86244403b532..0937d4989185 100644
@@ -137,11 +137,53 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx,
 	}
 }
 
+static void iommufd_device_remove_vdev(struct iommufd_device *idev)
+{
+	struct iommufd_vdevice *vdev;
+
+	mutex_lock(&idev->igroup->lock);
+	/* vdev has been completely destroyed by userspace */
+	if (!idev->vdev)
+		goto out_unlock;
+
+	vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
+	if (IS_ERR(vdev)) {
+		/*
+		 * vdev is removed from xarray by userspace, but is not
+		 * destroyed/freed. Since iommufd_vdevice_abort() is reentrant,
+		 * safe to destroy vdev here.
+		 */
+		iommufd_vdevice_abort(&idev->vdev->obj);
+		goto out_unlock;
+	}
+
+	/* Should never happen */
+	if (WARN_ON(vdev != idev->vdev)) {
+		iommufd_put_object(idev->ictx, &vdev->obj);
+		goto out_unlock;
+	}
+
+	/*
+	 * vdev is still alive. Hold a users refcount to prevent racing with
+	 * userspace destruction, then use iommufd_object_tombstone_user() to
+	 * destroy it and leave a tombstone.
+	 */
+	refcount_inc(&vdev->obj.users);
+	iommufd_put_object(idev->ictx, &vdev->obj);
+	mutex_unlock(&idev->igroup->lock);
+	iommufd_object_tombstone_user(idev->ictx, &vdev->obj);
+	return;
+
+out_unlock:
+	mutex_unlock(&idev->igroup->lock);
+}
+
 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))
drivers/iommu/iommufd/iommufd_private.h +11 -0
index fbc9ef78d81f..f58aa4439c53 100644
@@ -446,6 +446,7 @@ struct iommufd_device {
 	/* always the physical device */
 	struct device *dev;
 	bool enforce_cache_coherency;
+	struct iommufd_vdevice *vdev;
 };
 
 static inline struct iommufd_device *
@@ -621,6 +622,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);
 void iommufd_viommu_destroy(struct iommufd_object *obj);
 int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd);
 void iommufd_vdevice_destroy(struct iommufd_object *obj);
+void iommufd_vdevice_abort(struct iommufd_object *obj);
 
 struct iommufd_vdevice {
 	struct iommufd_object obj;
@@ -628,8 +630,17 @@ struct iommufd_vdevice {
 	struct iommufd_viommu *viommu;
 	struct device *dev;
 	u64 id; /* per-vIOMMU virtual ID */
+	struct iommufd_device *idev;
 };
 
+static inline struct iommufd_vdevice *
+iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id)
+{
+	return container_of(iommufd_get_object(ictx, id,
+					       IOMMUFD_OBJ_VDEVICE),
+			    struct iommufd_vdevice, obj);
+}
+
 #ifdef CONFIG_IOMMUFD_TEST
 int iommufd_test(struct iommufd_ucmd *ucmd);
 void iommufd_selftest_destroy(struct iommufd_object *obj);
drivers/iommu/iommufd/main.c +1 -0
index 620923669b42..64731b4fdbdf 100644
@@ -529,6 +529,7 @@ static const struct iommufd_object_ops iommufd_object_ops[] = {
 	},
 	[IOMMUFD_OBJ_VDEVICE] = {
 		.destroy = iommufd_vdevice_destroy,
+		.abort = iommufd_vdevice_abort,
 	},
 	[IOMMUFD_OBJ_VEVENTQ] = {
 		.destroy = iommufd_veventq_destroy,
drivers/iommu/iommufd/viommu.c +41 -3

83 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v3 2/5
#9 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 170 lines · Xu Yilun

Destroy iommufd_vdevice (vdev) on iommufd_idevice (idev) destroy so that vdev can't outlive idev.

iommufd_device (idev) represents the physical device bound to iommufd, while the iommufd_vdevice (vdev) represents the virtual instance of the physical device in the VM. The lifecycle of the vdev should not be longer than idev. This doesn't cause real problem on existing use cases cause vdev doesn't impact the physical device, only provides virtualization information. But to extend vdev for Confidential Computing (CC), there are needs to do secure configuration for the vdev, e.g. TSM Bind/Unbind. These configurations should be rolled back on idev destroy, or the external driver (VFIO) functionality may be impact.

Building the association between idev & vdev requires the two objects pointing each other, but not referencing each other. This requires proper locking. This is done by reviving some of Nicolin's patch [1].

There are 3 cases on idev destroy:

1. vdev is already destroyed by userspace. No extra handling needed.
2. vdev is still alive. Use iommufd_object_tombstone_user() to destroy vdev and tombstone the vdev ID.
3. vdev is being destroyed by userspace. The vdev ID is already freed, but vdev destroy handler is not completed. Destroy the vdev immediately. To solve the racing with userspace destruction, make iommufd_vdevice_abort() reentrant.

[1]:https://lore.kernel.org/ all/53025c827c44d68edb6469bfd940a8e8bc6147a5.1729897278.git.nicolinc@nvidia.com/

---
  drivers/iommu/iommufd/device.c          | 42 +++++++++++++++++++++++
  drivers/iommu/iommufd/iommufd_private.h | 11 +++++++
  drivers/iommu/iommufd/main.c            |  1 +
  drivers/iommu/iommufd/viommu.c          | 44 +++++++++++++++++++++++--
  4 files changed, 95 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/device.c +42 -0
index 86244403b532..0937d4989185 100644
@@ -137,11 +137,53 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx,
  	}
  }
 
+static void iommufd_device_remove_vdev(struct iommufd_device *idev)
+{
+	struct iommufd_vdevice *vdev;
+
+	mutex_lock(&idev->igroup->lock);
+	/* vdev has been completely destroyed by userspace */
+	if (!idev->vdev)
+		goto out_unlock;
+
+	vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
+	if (IS_ERR(vdev)) {
+		/*
+		 * vdev is removed from xarray by userspace, but is not
+		 * destroyed/freed. Since iommufd_vdevice_abort() is reentrant,
+		 * safe to destroy vdev here.
+		 */
+		iommufd_vdevice_abort(&idev->vdev->obj);
+		goto out_unlock;
+	}
+
+	/* Should never happen */
+	if (WARN_ON(vdev != idev->vdev)) {
+		iommufd_put_object(idev->ictx, &vdev->obj);
+		goto out_unlock;
+	}
+
+	/*
+	 * vdev is still alive. Hold a users refcount to prevent racing with
+	 * userspace destruction, then use iommufd_object_tombstone_user() to
+	 * destroy it and leave a tombstone.
+	 */
+	refcount_inc(&vdev->obj.users);
+	iommufd_put_object(idev->ictx, &vdev->obj);
+	mutex_unlock(&idev->igroup->lock);
+	iommufd_object_tombstone_user(idev->ictx, &vdev->obj);
+	return;
+
+out_unlock:
+	mutex_unlock(&idev->igroup->lock);
+}
+
  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))
drivers/iommu/iommufd/iommufd_private.h +11 -0
index fbc9ef78d81f..f58aa4439c53 100644
@@ -446,6 +446,7 @@ struct iommufd_device {
  	/* always the physical device */
  	struct device *dev;
  	bool enforce_cache_coherency;
+	struct iommufd_vdevice *vdev;
  };
 
  static inline struct iommufd_device *
@@ -621,6 +622,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);
  void iommufd_viommu_destroy(struct iommufd_object *obj);
  int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd);
  void iommufd_vdevice_destroy(struct iommufd_object *obj);
+void iommufd_vdevice_abort(struct iommufd_object *obj);
 
  struct iommufd_vdevice {
  	struct iommufd_object obj;
@@ -628,8 +630,17 @@ struct iommufd_vdevice {
  	struct iommufd_viommu *viommu;
  	struct device *dev;
  	u64 id; /* per-vIOMMU virtual ID */
+	struct iommufd_device *idev;
  };
 
+static inline struct iommufd_vdevice *
+iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id)
+{
+	return container_of(iommufd_get_object(ictx, id,
+					       IOMMUFD_OBJ_VDEVICE),
+			    struct iommufd_vdevice, obj);
+}
+
  #ifdef CONFIG_IOMMUFD_TEST
  int iommufd_test(struct iommufd_ucmd *ucmd);
  void iommufd_selftest_destroy(struct iommufd_object *obj);
drivers/iommu/iommufd/main.c +1 -0
index 620923669b42..64731b4fdbdf 100644
@@ -529,6 +529,7 @@ static const struct iommufd_object_ops iommufd_object_ops[] = {
  	},
  	[IOMMUFD_OBJ_VDEVICE] = {
  		.destroy = iommufd_vdevice_destroy,
+		.abort = iommufd_vdevice_abort,
  	},
  	[IOMMUFD_OBJ_VEVENTQ] = {
  		.destroy = iommufd_veventq_destroy,
drivers/iommu/iommufd/viommu.c +8 -1
index 01df2b985f02..632d1d7b8fd8 100644
@@ -84,16 +84,38 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
  	return rc;
  }
 
-void iommufd_vdevice_destroy(struct iommufd_object *obj)
+void iommufd_vdevice_abort(struct iommufd_object *obj)
  {
  	struct iommufd_vdevice *vdev =
  		container_of(obj, struct iommufd_vdevice, obj);
  	struct iommufd_viommu *viommu = vdev->viommu;
+	struct iommufd_device *idev = vdev->idev;
+
+	lockdep_assert_held(&idev->igroup->lock);
+
+	/*
+	 * iommufd_vdevice_abort() could be reentrant, by
+	 * iommufd_device_unbind() or by iommufd_destroy(). Cleanup only once.

drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 108

Quoted 10 lines
drivers/iommu/iommufd/viommu.c +5 -0
+	 */
+	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;

I feel it makes more sense to reorder the operations like this:

	vdev->viommu = NULL;
	vdev->idev = NULL;
	idev->vdev = NULL;
	put_device(vdev->dev);

put_device(vdev->dev) could potentially trigger other code paths that might attempt to reference vdev or its associated pointers. Therefore, it's safer to null all the relevant reference pointers before calling put_device().

Others look good to me,

Thanks,
baolu

author
#10 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 30 lines
Quoted 12 lines
drivers/iommu/iommufd/viommu.c +7 -1
-void iommufd_vdevice_destroy(struct iommufd_object *obj)
+void iommufd_vdevice_abort(struct iommufd_object *obj)
  {
  	struct iommufd_vdevice *vdev =
  		container_of(obj, struct iommufd_vdevice, obj);
  	struct iommufd_viommu *viommu = vdev->viommu;
+	struct iommufd_device *idev = vdev->idev;
+
+	lockdep_assert_held(&idev->igroup->lock);
+
+	/*
+	 * iommufd_vdevice_abort() could be reentrant, by

drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 108

Quoted 10 lines
drivers/iommu/iommufd/viommu.c +6 -0
+	 * 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;

I feel it makes more sense to reorder the operations like this:

	vdev->viommu = NULL;
	vdev->idev = NULL;
	idev->vdev = NULL;
	put_device(vdev->dev);
Quoted 4 lines

put_device(vdev->dev) could potentially trigger other code paths that might attempt to reference vdev or its associated pointers. Therefore, it's safer to null all the relevant reference pointers before calling put_device().

Yep. due to other changes, now I keep:

        xa_cmpxchg(&viommu->vdevs, vdev->id, vdev, NULL, GFP_KERNEL);
        refcount_dec(&viommu->obj.users);
+       idev->vdev = NULL;
        put_device(vdev->dev);

Anyway, vdev->dev would be completely dropped in next patch, so it doesn't matter much.

Thanks,
Yilun

Quoted 7 lines

Others look good to me,

Thanks,
baolu

maintainer
#11 lore raw

RE: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

In reply to Xu Yilun
Quoted 18 lines

From: Xu Yilun <[email protected]>
Sent: Friday, June 27, 2025 11:38 AM

drivers/iommu/iommufd/device.c +14 -0
+static void iommufd_device_remove_vdev(struct iommufd_device *idev)
+{
+	struct iommufd_vdevice *vdev;
+
+	mutex_lock(&idev->igroup->lock);
+	/* vdev has been completely destroyed by userspace */
+	if (!idev->vdev)
+		goto out_unlock;
+
+	vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
+	if (IS_ERR(vdev)) {
+		/*
+		 * vdev is removed from xarray by userspace, but is not
+		 * destroyed/freed. Since iommufd_vdevice_abort() is

reentrant,

drivers/iommu/iommufd/device.c static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, · line 158

Quoted 5 lines
drivers/iommu/iommufd/device.c +5 -0
+		 * safe to destroy vdev here.
+		 */
+		iommufd_vdevice_abort(&idev->vdev->obj);
+		goto out_unlock;
+	}

let's add a comment that vdev is still freed in iommufd_destroy() in this situation.

Quoted 14 lines
drivers/iommu/iommufd/viommu.c +8 -1
-void iommufd_vdevice_destroy(struct iommufd_object *obj)
+void iommufd_vdevice_abort(struct iommufd_object *obj)
 {
 	struct iommufd_vdevice *vdev =
 		container_of(obj, struct iommufd_vdevice, obj);
 	struct iommufd_viommu *viommu = vdev->viommu;
+	struct iommufd_device *idev = vdev->idev;
+
+	lockdep_assert_held(&idev->igroup->lock);
+
+	/*
+	 * iommufd_vdevice_abort() could be reentrant, by
+	 * iommufd_device_unbind() or by iommufd_destroy(). Cleanup only

once.

drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 101

Quoted 3 lines
drivers/iommu/iommufd/viommu.c +3 -0
+	 */
+	if (!viommu)
+		return;

Just check idev->vdev, to be consistent with the other path.

author
#12 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 18 lines · Kevin
Quoted 18 lines

From: Xu Yilun <[email protected]>
Sent: Friday, June 27, 2025 11:38 AM

drivers/iommu/iommufd/device.c +14 -0
+static void iommufd_device_remove_vdev(struct iommufd_device *idev)
+{
+	struct iommufd_vdevice *vdev;
+
+	mutex_lock(&idev->igroup->lock);
+	/* vdev has been completely destroyed by userspace */
+	if (!idev->vdev)
+		goto out_unlock;
+
+	vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
+	if (IS_ERR(vdev)) {
+		/*
+		 * vdev is removed from xarray by userspace, but is not
+		 * destroyed/freed. Since iommufd_vdevice_abort() is

reentrant,

Quoted 8 lines

drivers/iommu/iommufd/device.c static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, · line 158

Quoted 5 lines
drivers/iommu/iommufd/device.c +5 -0
+		 * safe to destroy vdev here.
+		 */
+		iommufd_vdevice_abort(&idev->vdev->obj);
+		goto out_unlock;
+	}

let's add a comment that vdev is still freed in iommufd_destroy() in this situation.

Yes.

Quoted 12 lines
Quoted 12 lines
drivers/iommu/iommufd/viommu.c +6 -1
-void iommufd_vdevice_destroy(struct iommufd_object *obj)
+void iommufd_vdevice_abort(struct iommufd_object *obj)
 {
 	struct iommufd_vdevice *vdev =
 		container_of(obj, struct iommufd_vdevice, obj);
 	struct iommufd_viommu *viommu = vdev->viommu;
+	struct iommufd_device *idev = vdev->idev;
+
+	lockdep_assert_held(&idev->igroup->lock);
+
+	/*
Quoted 8 lines

drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 101

Quoted 6 lines
(quoted diff) +2 -0
+	 * iommufd_vdevice_abort() could be reentrant, by
+	 * iommufd_device_unbind() or by iommufd_destroy(). Cleanup only

once.

drivers/iommu/iommufd/viommu.c +3 -0
+	 */
+	if (!viommu)
+		return;

Just check idev->vdev, to be consistent with the other path.

I think there is problem here. From your comments above, vdev could be aborted/destroyed by iommufd_destroy() again *after* idev is freed. That means in iommufd_vdevice_abort/destroy(), usage of vdev->idev or idev->vdev or vdev->idev->igroup->lock may be invalid.

I need to reconsider this, seems we need a dedicated vdev lock to synchronize concurrent vdev abort/destroy.

Thanks,
Yilun

maintainer
#13 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 2 lines · Xu Yilun

I need to reconsider this, seems we need a dedicated vdev lock to synchronize concurrent vdev abort/destroy.

It is not possible to be concurrent

destroy is only called once after it is no longer possible to call abort.

Jason

author
#14 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 9 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

I need to reconsider this, seems we need a dedicated vdev lock to synchronize concurrent vdev abort/destroy.

It is not possible to be concurrent

destroy is only called once after it is no longer possible to call abort.

I'm almost about to drop the "abort twice" idea. [1]

[1]: https://lore.kernel.org/linux-iommu/[email protected]/

See from the flow below,

T1. iommufd_device_unbind(idev)

	iommufd_device_destroy(obj)
		mutex_lock(&idev->igroup->lock)
 		iommufd_vdevice_abort(idev->vdev.obj)
		mutex_unlock(&idev->igroup->lock)
	kfree(obj)

T2. iommufd_destroy(vdev_id)

	iommufd_vdevice_destroy(obj)
		mutex_lock(&vdev->idev->igroup->lock)
		iommufd_vdevice_abort(obj);
		mutex_unlock(&vdev->idev->igroup->lock)
	kfree(obj)

iommufd_vdevice_destroy() will access idev->igroup->lock, but it is possible the idev is already freed at that time:

iommufd_destroy(vdev_id) iommufd_vdevice_destroy(obj)
iommufd_device_unbind(idev)
iommufd_device_destroy(obj) mutex_lock(&idev->igroup->lock)

                                            mutex_lock(&vdev->idev->igroup->lock) (wait)
  iommufd_vdevice_abort(idev->vdev.obj)
  mutex_unlock(&idev->igroup->lock)
  kfree(obj)
                                            mutex_lock(&vdev->idev->igroup->lock) (PANIC)
                                            iommufd_vdevice_abort(obj)
                                            ...

We also can't introduce some vdev side lock instead of idev side lock, vdev could also be freed right after iommufd_vdevice_destroy().

I think the only simple way is to let idev destruction wait for vdev destruction, that go back to the wait_event idea ...

Thanks,
Yilun

maintainer
#15 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 39 lines · Xu Yilun
Quoted 9 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

I need to reconsider this, seems we need a dedicated vdev lock to synchronize concurrent vdev abort/destroy.

It is not possible to be concurrent

destroy is only called once after it is no longer possible to call abort.

I'm almost about to drop the "abort twice" idea. [1]

[1]: https://lore.kernel.org/linux-iommu/[email protected]/

See from the flow below,

T1. iommufd_device_unbind(idev)

	iommufd_device_destroy(obj)
		mutex_lock(&idev->igroup->lock)
 		iommufd_vdevice_abort(idev->vdev.obj)
		mutex_unlock(&idev->igroup->lock)
	kfree(obj)

T2. iommufd_destroy(vdev_id)

	iommufd_vdevice_destroy(obj)
		mutex_lock(&vdev->idev->igroup->lock)
		iommufd_vdevice_abort(obj);
		mutex_unlock(&vdev->idev->igroup->lock)
	kfree(obj)

iommufd_vdevice_destroy() will access idev->igroup->lock, but it is possible the idev is already freed at that time:

iommufd_destroy(vdev_id) iommufd_vdevice_destroy(obj)
iommufd_device_unbind(idev)
iommufd_device_destroy(obj) mutex_lock(&idev->igroup->lock)

Quoted 7 lines
                                            mutex_lock(&vdev->idev->igroup->lock) (wait)
  iommufd_vdevice_abort(idev->vdev.obj)
  mutex_unlock(&idev->igroup->lock)
  kfree(obj)
                                            mutex_lock(&vdev->idev->igroup->lock) (PANIC)
                                            iommufd_vdevice_abort(obj)
                                            ...

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??

Jason

author
#16 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 48 lines · Jason Gunthorpe
Quoted 39 lines · Xu Yilun
Quoted 9 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

I need to reconsider this, seems we need a dedicated vdev lock to synchronize concurrent vdev abort/destroy.

It is not possible to be concurrent

destroy is only called once after it is no longer possible to call abort.

I'm almost about to drop the "abort twice" idea. [1]

[1]: https://lore.kernel.org/linux-iommu/[email protected]/

See from the flow below,

T1. iommufd_device_unbind(idev)

	iommufd_device_destroy(obj)
		mutex_lock(&idev->igroup->lock)
 		iommufd_vdevice_abort(idev->vdev.obj)
		mutex_unlock(&idev->igroup->lock)
	kfree(obj)

T2. iommufd_destroy(vdev_id)

	iommufd_vdevice_destroy(obj)
		mutex_lock(&vdev->idev->igroup->lock)
		iommufd_vdevice_abort(obj);
		mutex_unlock(&vdev->idev->igroup->lock)
	kfree(obj)

iommufd_vdevice_destroy() will access idev->igroup->lock, but it is possible the idev is already freed at that time:

iommufd_destroy(vdev_id) iommufd_vdevice_destroy(obj)
iommufd_device_unbind(idev)
iommufd_device_destroy(obj) mutex_lock(&idev->igroup->lock)

Quoted 7 lines
                                            mutex_lock(&vdev->idev->igroup->lock) (wait)
  iommufd_vdevice_abort(idev->vdev.obj)
  mutex_unlock(&idev->igroup->lock)
  kfree(obj)
                                            mutex_lock(&vdev->idev->igroup->lock) (PANIC)
                                            iommufd_vdevice_abort(obj)
                                            ...
Quoted 5 lines

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.

I haven't found a safer way for vdev to sync up its validness with idev w/o touching idev.

I was thinking of using vdev->idev and some vdev lock for tracking instead. Then iommufd_vdevice_abort() doesn't touch idev. But it is still the same, just switch to put idev in risk:

iommufd_destroy(vdev_id) iommufd_vdevice_destroy(obj)
iommufd_device_unbind(idev)
iommufd_device_destroy(obj)

                                               mutex_lock(&vdev->some_lock)
  mutex_lock(&idev->vdev->some_lock) (wait)
                                               iommufd_vdevice_abort(obj)
                                               mutex_unlock(&vdev->some_lock)
                                               kfree(obj)

mutex_lock(&idev->vdev->some_lock) (PANIC)
iommufd_vdevice_abort(idev->vdev.obj)
...

Thanks,
Yilun

maintainer
#17 lore raw

RE: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 72 lines

From: Xu Yilun <[email protected]>
Sent: Wednesday, July 2, 2025 10:24 AM

Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Xu Yilun
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Xu Yilun

I need to reconsider this, seems we need a dedicated vdev lock to synchronize concurrent vdev abort/destroy.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

It is not possible to be concurrent

destroy is only called once after it is no longer possible to call abort.

I'm almost about to drop the "abort twice" idea. [1]

[1]: https://lore.kernel.org/linux-

iommu/[email protected]/

Quoted 33 lines
Quoted 25 lines

See from the flow below,

T1. iommufd_device_unbind(idev)

	iommufd_device_destroy(obj)
		mutex_lock(&idev->igroup->lock)
 		iommufd_vdevice_abort(idev->vdev.obj)
		mutex_unlock(&idev->igroup->lock)
	kfree(obj)

T2. iommufd_destroy(vdev_id)

	iommufd_vdevice_destroy(obj)
		mutex_lock(&vdev->idev->igroup->lock)
		iommufd_vdevice_abort(obj);
		mutex_unlock(&vdev->idev->igroup->lock)
	kfree(obj)

iommufd_vdevice_destroy() will access idev->igroup->lock, but it is possible the idev is already freed at that time:

iommufd_destroy(vdev_id) iommufd_vdevice_destroy(obj)
iommufd_device_unbind(idev)
iommufd_device_destroy(obj) mutex_lock(&idev->igroup->lock)

Quoted 7 lines
                                            mutex_lock(&vdev->idev->igroup->lock) (wait)
  iommufd_vdevice_abort(idev->vdev.obj)
  mutex_unlock(&idev->igroup->lock)
  kfree(obj)
                                            mutex_lock(&vdev->idev->igroup->lock) (PANIC)
                                            iommufd_vdevice_abort(obj)
                                            ...
Quoted 5 lines

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.

I haven't found a safer way for vdev to sync up its validness with idev w/o touching idev.

I was thinking of using vdev->idev and some vdev lock for tracking instead. Then iommufd_vdevice_abort() doesn't touch idev. But it is still the same, just switch to put idev in risk:

Quoted 6 lines

iommufd_destroy(vdev_id) iommufd_vdevice_destroy(obj)
iommufd_device_unbind(idev)
iommufd_device_destroy(obj)

                                               mutex_lock(&vdev->some_lock)

mutex_lock(&idev->vdev->some_lock) (wait)

panic could happen here, between acquiring idev->vdev and mutex(vdev->some_lock) as vdev might be destroyed/freed in-between.

Quoted 7 lines
                                               iommufd_vdevice_abort(obj)
                                               mutex_unlock(&vdev->some_lock)
                                               kfree(obj)

mutex_lock(&idev->vdev->some_lock) (PANIC)
iommufd_vdevice_abort(idev->vdev.obj)
...

I cannot find a safe way either, except using certain global lock.

but comparing to that I'd prefer to the original wait approach...

maintainer
#18 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 10 lines · Kevin
Quoted 10 lines
Quoted 5 lines

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.

Quoted 1 line

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:

drivers/iommu/iommufd/device.c +9 -1
index 4e781aa9fc6329..9174fa7c972b80 100644
@@ -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))
drivers/iommu/iommufd/main.c +4 -0
index b2e8e106c16158..387c630fdabfbd 100644
@@ -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) == 0,
 			       msecs_to_jiffies(60000)))
@@ -567,6 +570,7 @@ static const struct iommufd_object_ops iommufd_object_ops[] = {
 		.destroy = iommufd_access_destroy_object,
 	},
 	[IOMMUFD_OBJ_DEVICE] = {
+		.pre_destroy = iommufd_device_pre_destroy,
 		.destroy = iommufd_device_destroy,
 	},
 	[IOMMUFD_OBJ_FAULT] = {
drivers/iommu/iommufd/viommu.c +12 -4
index 9451a311745f7b..cbf99daa7dc25d 100644
@@ -135,6 +135,7 @@ void iommufd_vdevice_destroy(struct iommufd_object *obj)
 	mutex_lock(&vdev->idev->igroup->lock);
 	iommufd_vdevice_abort(obj);
 	mutex_unlock(&vdev->idev->igroup->lock);
+	iommufd_put_object(vdev->viommu->ictx, &vdev->idev->obj);
 }
 
 int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
@@ -180,13 +181,19 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	vdev->id = virt_id;
 	vdev->viommu = viommu;
 	refcount_inc(&viommu->obj.users);
+
+	/*
+	 * A reference is held on the idev so long as we have a pointer.
+	 * iommufd_device_pre_destroy() will revoke it before the real
+	 * destruction.
+	 */
+	vdev->idev = idev;
+
 	/*
 	 * iommufd_device_destroy() waits until idev->vdev is NULL before
 	 * freeing the idev, which only happens once the vdev is finished
-	 * destruction. Thus we do not need refcounting on either idev->vdev or
-	 * vdev->idev.
+	 * destruction.
 	 */
-	vdev->idev = idev;
 	idev->vdev = vdev;
 
 	curr = xa_cmpxchg(&viommu->vdevs, virt_id, NULL, vdev, GFP_KERNEL);
@@ -207,7 +214,8 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
 out_unlock_igroup:
 	mutex_unlock(&idev->igroup->lock);
 out_put_idev:
-	iommufd_put_object(ucmd->ictx, &idev->obj);
+	if (rc)
+		iommufd_put_object(ucmd->ictx, &idev->obj);
 out_put_viommu:
 	iommufd_put_object(ucmd->ictx, &viommu->obj);
 	return rc;
maintainer
#19 lore raw

RE: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 123 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, July 2, 2025 8:41 PM

Quoted 10 lines · Kevin
Quoted 10 lines
Quoted 5 lines

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.

Quoted 1 line

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:

drivers/iommu/iommufd/device.c +9 -1
index 4e781aa9fc6329..9174fa7c972b80 100644
@@ -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);
drivers/iommu/iommufd/device.c +0 -0
 	if (!iommufd_selftest_is_mock_dev(idev->dev))
drivers/iommu/iommufd/main.c +3 -0
index b2e8e106c16158..387c630fdabfbd 100644
@@ -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)
== 0,

drivers/iommu/iommufd/main.c +1 -0
 			       msecs_to_jiffies(60000)))
@@ -567,6 +570,7 @@ static const struct iommufd_object_ops
 iommufd_object_ops[] = {
 		.destroy = iommufd_access_destroy_object,
 	},
 	[IOMMUFD_OBJ_DEVICE] = {
+		.pre_destroy = iommufd_device_pre_destroy,
 		.destroy = iommufd_device_destroy,
 	},
drivers/iommu/iommufd/main.c +0 -0
 	[IOMMUFD_OBJ_FAULT] = {
drivers/iommu/iommufd/viommu.c +1 -0
index 9451a311745f7b..cbf99daa7dc25d 100644
@@ -135,6 +135,7 @@ void iommufd_vdevice_destroy(struct
 iommufd_object *obj)
 	mutex_lock(&vdev->idev->igroup->lock);
 	iommufd_vdevice_abort(obj);
 	mutex_unlock(&vdev->idev->igroup->lock);
+	iommufd_put_object(vdev->viommu->ictx, &vdev->idev->obj);
 }
 

int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)

drivers/iommu/iommufd/viommu.c +8 -1
@@ -180,13 +181,19 @@ int iommufd_vdevice_alloc_ioctl(struct
 iommufd_ucmd *ucmd)
 	vdev->id = virt_id;
 	vdev->viommu = viommu;
 	refcount_inc(&viommu->obj.users);
+
+	/*
+	 * A reference is held on the idev so long as we have a pointer.
+	 * iommufd_device_pre_destroy() will revoke it before the real
+	 * destruction.
+	 */
+	vdev->idev = idev;
+
 	/*
 	 * iommufd_device_destroy() waits until idev->vdev is NULL before
 	 * freeing the idev, which only happens once the vdev is finished
-	 * destruction. Thus we do not need refcounting on either idev->vdev

or

drivers/iommu/iommufd/viommu.c +1 -2
-	 * vdev->idev.
+	 * destruction.
 	 */
-	vdev->idev = idev;
 	idev->vdev = vdev;
 
 	curr = xa_cmpxchg(&viommu->vdevs, virt_id, NULL, vdev,

GFP_KERNEL);

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct · line 219

Quoted 7 lines
drivers/iommu/iommufd/viommu.c +2 -1
@@ -207,7 +214,8 @@ int iommufd_vdevice_alloc_ioctl(struct
 iommufd_ucmd *ucmd)
 out_unlock_igroup:
 	mutex_unlock(&idev->igroup->lock);
 out_put_idev:
-	iommufd_put_object(ucmd->ictx, &idev->obj);
+	if (rc)
+		iommufd_put_object(ucmd->ictx, &idev->obj);

but this sounds like a misuse of shortterm_users which is not intended to be held long beyond ioctl...

maintainer
#20 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 2 lines · Kevin

but this sounds like a misuse of shortterm_users which is not intended to be held long beyond ioctl...

With the addition of the pre_destroy it purpose gets changed to 'beyond the ioctl or past pre_destroy'

Jason

author
#21 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

In reply to Jason Gunthorpe
Quoted 55 lines · Jason Gunthorpe
Quoted 10 lines · Kevin
Quoted 10 lines
Quoted 5 lines

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.

Quoted 1 line

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:

drivers/iommu/iommufd/device.c +9 -1
index 4e781aa9fc6329..9174fa7c972b80 100644
@@ -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))
drivers/iommu/iommufd/main.c +0 -0
index b2e8e106c16158..387c630fdabfbd 100644

drivers/iommu/iommufd/main.c static int iommufd_object_dec_wait_shortterm(struct iommufd_ctx *ictx, · line 158

Quoted 8 lines
drivers/iommu/iommufd/main.c +3 -0
@@ -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.

drivers/iommu/iommufd/device.c +8 -1
index 9876bf70980a..49b787bc0688 100644
@@ -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)) {
drivers/iommu/iommufd/iommufd_private.h +41 -0

47 lines not on this page · raw message

drivers/iommu/iommufd/main.c +29 -3
index fbccfbd8fb03..148218293f55 100644
@@ -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);
drivers/iommu/iommufd/viommu.c +5 -5
index d49e8f0c8bb3..a7feefd5e472 100644
@@ -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;
maintainer
#22 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 3 lines · Xu Yilun

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.

Hum, yes it seems we could get a race with another vdevice being created during idevice destruction, but I think this could be fixed in a straightforward way with a flag 'being destroyed' in the idev under the lock if it is the only issue

Quoted 2 lines

The worse thing is the new user could hold the shortterm_users until pre_destroy(), but there isn't a second pre_destroy()...

This is OK, the point of pre_destroy is to remove the reference cycle. So it needs to prevent new vdevs from establishing references and remove the reference from any existing vdev.

refcounts outside of this cycle are handled by the existing mechanisms - pre_destroy is only about the refcount loop.

Quoted 2 lines

I think extending the life of shortterm_users at runtime makes things complex.

Well, it keeps the refcounting working the way it is supposed to. Holding a pointer without a reference is nasty and is creating alot of these weird problems and strange flows. Hold the refcount along with the pointer and things follow the normal patterns.

There is nothing inherently wrong with shortterm users for this besides its name. Think of it is as 'wait for users', and we clearly want to wait for the vdev user to put back its idev reference.

I think there are too many gyrations with refcounts in this version below. It is hard to reason about if zero refcounts are re-elevated to non-zero.

drivers/iommu/iommufd/device.c static void iommufd_device_remove_vdev(struct iommufd_device *idev) · line 157

Quoted 10 lines
        vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
drivers/iommu/iommufd/device.c +7 -1
-       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;

This is now really weird, we are mangling the refcounts and the reasoning why it is safe with the concurrent vdev thread is tricky..

Quoted 6 lines
drivers/iommu/iommufd/iommufd_private.h +6 -0
+/*
+ * 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

drivers/iommu/iommufd/iommufd_private.h static inline void iommufd_put_object(struct iommufd_ctx *ictx, · line 222

Quoted 10 lines
drivers/iommu/iommufd/iommufd_private.h +10 -0
+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);
+}

This looks weird too, why is refcount_read safe? That is usually not how this kind of logic is written.

I'm not sure this is OK, it is not an obvious known-good pattern for lifetime management.

Jason

author
#23 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 14 lines · Jason Gunthorpe
Quoted 3 lines · Xu Yilun

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.

Hum, yes it seems we could get a race with another vdevice being created during idevice destruction, but I think this could be fixed in a straightforward way with a flag 'being destroyed' in the idev under the lock if it is the only issue

Quoted 2 lines

The worse thing is the new user could hold the shortterm_users until pre_destroy(), but there isn't a second pre_destroy()...

Quoted 6 lines

This is OK, the point of pre_destroy is to remove the reference cycle. So it needs to prevent new vdevs from establishing references and remove the reference from any existing vdev.

refcounts outside of this cycle are handled by the existing mechanisms - pre_destroy is only about the refcount loop.

That works for me. I didn't prefer a being destroyed flag cause it seems not align with the current refcounting mechanism. But I see it makes sense cause idev does deterministic destruction.

The only concern is idev cannot actually prevent new reference by itself, need vdev to honor the flag. But just keep simple, don't over-engineering.

Thanks,
Yilun

Quoted 13 lines
Quoted 3 lines

I think extending the life of shortterm_users at runtime makes things complex.

Well, it keeps the refcounting working the way it is supposed to. Holding a pointer without a reference is nasty and is creating alot of these weird problems and strange flows. Hold the refcount along with the pointer and things follow the normal patterns.

There is nothing inherently wrong with shortterm users for this besides its name. Think of it is as 'wait for users', and we clearly want to wait for the vdev user to put back its idev reference.

#24 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

In reply to Xu Yilun
Quoted 8 lines · Xu Yilun
drivers/iommu/iommufd/device.c +8 -0
+static void iommufd_device_remove_vdev(struct iommufd_device *idev)
+{
+	struct iommufd_vdevice *vdev;
+
+	mutex_lock(&idev->igroup->lock);
+	/* vdev has been completely destroyed by userspace */
+	if (!idev->vdev)
+		goto out_unlock;

drivers/iommu/iommufd/device.c static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, · line 157

Quoted 10 lines
drivers/iommu/iommufd/device.c +10 -0
+
+	vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
+	if (IS_ERR(vdev)) {
+		/*
+		 * vdev is removed from xarray by userspace, but is not
+		 * destroyed/freed. Since iommufd_vdevice_abort() is reentrant,
+		 * safe to destroy vdev here.
+		 */
+		iommufd_vdevice_abort(&idev->vdev->obj);
+		goto out_unlock;

This is the case #3, i.e. a racing vdev destory, in the commit log?

I think it is worth clarifying that there is a concurrent destroy:
/*
* An ongoing vdev destroy ioctl has removed the vdev from the
* object xarray but has not finished iommufd_vdevice_destroy()
* yet, as it is holding the same mutex. Destroy the vdev here,
* i.e. the iommufd_vdevice_destroy() will be a NOP once it is
* unlocked.
*/

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 191

Quoted 11 lines
drivers/iommu/iommufd/viommu.c +3 -1
@@ -147,10 +183,12 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	if (rc)
 		goto out_abort;
 	iommufd_object_finalize(ucmd->ictx, &vdev->obj);
-	goto out_put_idev;
+	goto out_unlock_igroup;
 
 out_abort:
 	iommufd_object_abort_and_destroy(ucmd->ictx, &vdev->obj);
+out_unlock_igroup:
+	mutex_unlock(&idev->igroup->lock);

Looks like we will have to partially revert the _ucmd allocator, in this function:
https://lore.kernel.org/all/107b24a3b791091bb09c92ffb0081c56c413b26d.1749882255.git.nicolinc@nvidia.com/

Please try fixing the conflicts on top of Jason's for-next tree.

Thanks
Nicolin

author
#25 lore raw

Re: [PATCH v3 2/5] iommufd: Destroy vdevice on idevice destroy

Quoted 22 lines · Nicolin Chen
Quoted 8 lines · Xu Yilun
drivers/iommu/iommufd/device.c +8 -0
+static void iommufd_device_remove_vdev(struct iommufd_device *idev)
+{
+	struct iommufd_vdevice *vdev;
+
+	mutex_lock(&idev->igroup->lock);
+	/* vdev has been completely destroyed by userspace */
+	if (!idev->vdev)
+		goto out_unlock;

drivers/iommu/iommufd/device.c static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, · line 157

Quoted 10 lines
drivers/iommu/iommufd/device.c +10 -0
+
+	vdev = iommufd_get_vdevice(idev->ictx, idev->vdev->obj.id);
+	if (IS_ERR(vdev)) {
+		/*
+		 * vdev is removed from xarray by userspace, but is not
+		 * destroyed/freed. Since iommufd_vdevice_abort() is reentrant,
+		 * safe to destroy vdev here.
+		 */
+		iommufd_vdevice_abort(&idev->vdev->obj);
+		goto out_unlock;

This is the case #3, i.e. a racing vdev destory, in the commit log?

Quoted 5 lines

I think it is worth clarifying that there is a concurrent destroy:
/*
* An ongoing vdev destroy ioctl has removed the vdev from the
* object xarray but has not finished iommufd_vdevice_destroy()
* yet, as it is holding the same mutex.

Applied this part.

Quoted 21 lines

* Destroy the vdev here,
* i.e. the iommufd_vdevice_destroy() will be a NOP once it is
* unlocked.
*/

drivers/iommu/iommufd/viommu.c int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 191

Quoted 11 lines
drivers/iommu/iommufd/viommu.c +3 -1
@@ -147,10 +183,12 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	if (rc)
 		goto out_abort;
 	iommufd_object_finalize(ucmd->ictx, &vdev->obj);
-	goto out_put_idev;
+	goto out_unlock_igroup;
 
 out_abort:
 	iommufd_object_abort_and_destroy(ucmd->ictx, &vdev->obj);
+out_unlock_igroup:
+	mutex_unlock(&idev->igroup->lock);

Looks like we will have to partially revert the _ucmd allocator, in this function:
https://lore.kernel.org/all/107b24a3b791091bb09c92ffb0081c56c413b26d.1749882255.git.nicolinc@nvidia.com/

Quoted 1 line

Please try fixing the conflicts on top of Jason's for-next tree.

Yes, will rebase for next version.

Thanks,
Yilun

Quoted 3 lines

Thanks
Nicolin

author
#26 lore raw

PATCH v3 3/5 iommufd/vdevice: Remove struct device reference from struct vdevice

In reply to Xu Yilun

Remove struct device *dev from struct vdevice.

The dev pointer is the Plan B for vdevice to reference the physical device. As now vdev->idev is added without refcounting concern, just use vdev->idev->dev when needed.

---
 drivers/iommu/iommufd/driver.c          | 4 ++--
 drivers/iommu/iommufd/iommufd_private.h | 1 -
 drivers/iommu/iommufd/viommu.c          | 3 ---
 3 files changed, 2 insertions(+), 6 deletions(-)
drivers/iommu/iommufd/driver.c +2 -2
index 922cd1fe7ec2..942d402bba36 100644
@@ -45,7 +45,7 @@ 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->dev : NULL;
+	return vdev ? vdev->idev->dev : NULL;
 }
 EXPORT_SYMBOL_NS_GPL(iommufd_viommu_find_dev, "IOMMUFD");
 
@@ -62,7 +62,7 @@ int iommufd_viommu_get_vdev_id(struct iommufd_viommu *viommu,
 
 	xa_lock(&viommu->vdevs);
 	xa_for_each(&viommu->vdevs, index, vdev) {
-		if (vdev->dev == dev) {
+		if (vdev->idev->dev == dev) {
 			*vdev_id = vdev->id;
 			rc = 0;
 			break;
drivers/iommu/iommufd/iommufd_private.h +0 -1
index f58aa4439c53..3700193471db 100644
@@ -628,7 +628,6 @@ struct iommufd_vdevice {
 	struct iommufd_object obj;
 	struct iommufd_ctx *ictx;
 	struct iommufd_viommu *viommu;
-	struct device *dev;
 	u64 id; /* per-vIOMMU virtual ID */
 	struct iommufd_device *idev;
 };
drivers/iommu/iommufd/viommu.c +0 -3
index 632d1d7b8fd8..452a7a24d738 100644
@@ -103,7 +103,6 @@ void iommufd_vdevice_abort(struct iommufd_object *obj)
 	/* 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;
 }
@@ -159,8 +158,6 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	}
 
 	vdev->id = virt_id;
-	vdev->dev = idev->dev;
-	get_device(idev->dev);
 	vdev->viommu = viommu;
 	refcount_inc(&viommu->obj.users);
 	/*
Signature · 1 line
2.25.1
Reviewed-by on v3 3/5
#27 lore raw

Re: [PATCH v3 3/5] iommufd/vdevice: Remove struct device reference from struct vdevice

Quoted 7 lines · Xu Yilun

Remove struct device *dev from struct vdevice.

The dev pointer is the Plan B for vdevice to reference the physical device. As now vdev->idev is added without refcounting concern, just use vdev->idev->dev when needed.

maintainer Reviewed-by on v3 3/5
#28 lore raw

Re: [PATCH v3 3/5] iommufd/vdevice: Remove struct device reference from struct vdevice

In reply to Xu Yilun
Quoted 6 lines · Xu Yilun

Remove struct device *dev from struct vdevice.

The dev pointer is the Plan B for vdevice to reference the physical device. As now vdev->idev is added without refcounting concern, just use vdev->idev->dev when needed.

Quoted 6 lines
---
 drivers/iommu/iommufd/driver.c          | 4 ++--
 drivers/iommu/iommufd/iommufd_private.h | 1 -
 drivers/iommu/iommufd/viommu.c          | 3 ---
 3 files changed, 2 insertions(+), 6 deletions(-)

Jason

author
#29 lore raw

PATCH v3 4/5 iommufd/selftest: Explicitly skip tests for inapplicable variant

In reply to Xu Yilun

no_viommu is not applicable for some viommu/vdevice tests. Explicitly report the skipping, don't do it silently.

Only add the prints. No functional change intended.

---
 tools/testing/selftests/iommu/iommufd.c | 378 ++++++++++++------------
 1 file changed, 190 insertions(+), 188 deletions(-)
tools/testing/selftests/iommu/iommufd.c +190 -188
index 1a8e85afe9aa..4a9b6e3b37fa 100644
@@ -2760,35 +2760,36 @@ TEST_F(iommufd_viommu, viommu_alloc_nested_iopf)
 	uint32_t fault_fd;
 	uint32_t vdev_id;
 
-	if (self->device_id) {
-		test_ioctl_fault_alloc(&fault_id, &fault_fd);
-		test_err_hwpt_alloc_iopf(
-			ENOENT, dev_id, viommu_id, UINT32_MAX,
-			IOMMU_HWPT_FAULT_ID_VALID, &iopf_hwpt_id,
-			IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data));
-		test_err_hwpt_alloc_iopf(
-			EOPNOTSUPP, dev_id, viommu_id, fault_id,
-			IOMMU_HWPT_FAULT_ID_VALID | (1 << 31), &iopf_hwpt_id,
-			IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data));
-		test_cmd_hwpt_alloc_iopf(
-			dev_id, viommu_id, fault_id, IOMMU_HWPT_FAULT_ID_VALID,
-			&iopf_hwpt_id, IOMMU_HWPT_DATA_SELFTEST, &data,
-			sizeof(data));
+	if (!dev_id)
+		SKIP(return, "Skipping test for variant no_viommu");
 
-		/* Must allocate vdevice before attaching to a nested hwpt */
-		test_err_mock_domain_replace(ENOENT, self->stdev_id,
-					     iopf_hwpt_id);
-		test_cmd_vdevice_alloc(viommu_id, dev_id, 0x99, &vdev_id);
-		test_cmd_mock_domain_replace(self->stdev_id, iopf_hwpt_id);
-		EXPECT_ERRNO(EBUSY,
-			     _test_ioctl_destroy(self->fd, iopf_hwpt_id));
-		test_cmd_trigger_iopf(dev_id, fault_fd);
+	test_ioctl_fault_alloc(&fault_id, &fault_fd);
+	test_err_hwpt_alloc_iopf(
+		ENOENT, dev_id, viommu_id, UINT32_MAX,
+		IOMMU_HWPT_FAULT_ID_VALID, &iopf_hwpt_id,
+		IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data));
+	test_err_hwpt_alloc_iopf(
+		EOPNOTSUPP, dev_id, viommu_id, fault_id,
+		IOMMU_HWPT_FAULT_ID_VALID | (1 << 31), &iopf_hwpt_id,
+		IOMMU_HWPT_DATA_SELFTEST, &data, sizeof(data));
+	test_cmd_hwpt_alloc_iopf(
+		dev_id, viommu_id, fault_id, IOMMU_HWPT_FAULT_ID_VALID,
+		&iopf_hwpt_id, IOMMU_HWPT_DATA_SELFTEST, &data,
+		sizeof(data));
+
+	/* Must allocate vdevice before attaching to a nested hwpt */
+	test_err_mock_domain_replace(ENOENT, self->stdev_id,
+				     iopf_hwpt_id);
+	test_cmd_vdevice_alloc(viommu_id, dev_id, 0x99, &vdev_id);
+	test_cmd_mock_domain_replace(self->stdev_id, iopf_hwpt_id);
+	EXPECT_ERRNO(EBUSY,
+		     _test_ioctl_destroy(self->fd, iopf_hwpt_id));
+	test_cmd_trigger_iopf(dev_id, fault_fd);
 
-		test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
-		test_ioctl_destroy(iopf_hwpt_id);
-		close(fault_fd);
-		test_ioctl_destroy(fault_id);
-	}
+	test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
+	test_ioctl_destroy(iopf_hwpt_id);
+	close(fault_fd);
+	test_ioctl_destroy(fault_id);
 }
 
 TEST_F(iommufd_viommu, vdevice_alloc)
@@ -2849,169 +2850,170 @@ TEST_F(iommufd_viommu, vdevice_cache)
 	uint32_t vdev_id = 0;
 	uint32_t num_inv;
 
-	if (dev_id) {
-		test_cmd_vdevice_alloc(viommu_id, dev_id, 0x99, &vdev_id);
-
-		test_cmd_dev_check_cache_all(dev_id,
-					     IOMMU_TEST_DEV_CACHE_DEFAULT);
-
-		/* Check data_type by passing zero-length array */
-		num_inv = 0;
-		test_cmd_viommu_invalidate(viommu_id, inv_reqs,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(!num_inv);
-
-		/* Negative test: Invalid data_type */
-		num_inv = 1;
-		test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(!num_inv);
-
-		/* Negative test: structure size sanity */
-		num_inv = 1;
-		test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   sizeof(*inv_reqs) + 1, &num_inv);
-		assert(!num_inv);
-
-		num_inv = 1;
-		test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   1, &num_inv);
-		assert(!num_inv);
-
-		/* Negative test: invalid flag is passed */
-		num_inv = 1;
-		inv_reqs[0].flags = 0xffffffff;
-		inv_reqs[0].vdev_id = 0x99;
-		test_err_viommu_invalidate(EOPNOTSUPP, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(!num_inv);
-
-		/* Negative test: invalid data_uptr when array is not empty */
-		num_inv = 1;
-		inv_reqs[0].flags = 0;
-		inv_reqs[0].vdev_id = 0x99;
-		test_err_viommu_invalidate(EINVAL, viommu_id, NULL,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(!num_inv);
-
-		/* Negative test: invalid entry_len when array is not empty */
-		num_inv = 1;
-		inv_reqs[0].flags = 0;
-		inv_reqs[0].vdev_id = 0x99;
-		test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   0, &num_inv);
-		assert(!num_inv);
-
-		/* Negative test: invalid cache_id */
-		num_inv = 1;
-		inv_reqs[0].flags = 0;
-		inv_reqs[0].vdev_id = 0x99;
-		inv_reqs[0].cache_id = MOCK_DEV_CACHE_ID_MAX + 1;
-		test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(!num_inv);
-
-		/* Negative test: invalid vdev_id */
-		num_inv = 1;
-		inv_reqs[0].flags = 0;
-		inv_reqs[0].vdev_id = 0x9;
-		inv_reqs[0].cache_id = 0;
-		test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(!num_inv);
-
-		/*
-		 * Invalidate the 1st cache entry but fail the 2nd request
-		 * due to invalid flags configuration in the 2nd request.
-		 */
-		num_inv = 2;
-		inv_reqs[0].flags = 0;
-		inv_reqs[0].vdev_id = 0x99;
-		inv_reqs[0].cache_id = 0;
-		inv_reqs[1].flags = 0xffffffff;
-		inv_reqs[1].vdev_id = 0x99;
-		inv_reqs[1].cache_id = 1;
-		test_err_viommu_invalidate(EOPNOTSUPP, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(num_inv == 1);
-		test_cmd_dev_check_cache(dev_id, 0, 0);
-		test_cmd_dev_check_cache(dev_id, 1,
-					 IOMMU_TEST_DEV_CACHE_DEFAULT);
-		test_cmd_dev_check_cache(dev_id, 2,
-					 IOMMU_TEST_DEV_CACHE_DEFAULT);
-		test_cmd_dev_check_cache(dev_id, 3,
-					 IOMMU_TEST_DEV_CACHE_DEFAULT);
+	if (!dev_id)
+		SKIP(return, "Skipping test for variant no_viommu");
+
+	test_cmd_vdevice_alloc(viommu_id, dev_id, 0x99, &vdev_id);
+
+	test_cmd_dev_check_cache_all(dev_id,
+				     IOMMU_TEST_DEV_CACHE_DEFAULT);
+
+	/* Check data_type by passing zero-length array */
+	num_inv = 0;
+	test_cmd_viommu_invalidate(viommu_id, inv_reqs,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(!num_inv);
+
+	/* Negative test: Invalid data_type */
+	num_inv = 1;
+	test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(!num_inv);
+
+	/* Negative test: structure size sanity */
+	num_inv = 1;
+	test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   sizeof(*inv_reqs) + 1, &num_inv);
+	assert(!num_inv);
+
+	num_inv = 1;
+	test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   1, &num_inv);
+	assert(!num_inv);
+
+	/* Negative test: invalid flag is passed */
+	num_inv = 1;
+	inv_reqs[0].flags = 0xffffffff;
+	inv_reqs[0].vdev_id = 0x99;
+	test_err_viommu_invalidate(EOPNOTSUPP, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(!num_inv);
+
+	/* Negative test: invalid data_uptr when array is not empty */
+	num_inv = 1;
+	inv_reqs[0].flags = 0;
+	inv_reqs[0].vdev_id = 0x99;
+	test_err_viommu_invalidate(EINVAL, viommu_id, NULL,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(!num_inv);
+
+	/* Negative test: invalid entry_len when array is not empty */
+	num_inv = 1;
+	inv_reqs[0].flags = 0;
+	inv_reqs[0].vdev_id = 0x99;
+	test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   0, &num_inv);
+	assert(!num_inv);
+
+	/* Negative test: invalid cache_id */
+	num_inv = 1;
+	inv_reqs[0].flags = 0;
+	inv_reqs[0].vdev_id = 0x99;
+	inv_reqs[0].cache_id = MOCK_DEV_CACHE_ID_MAX + 1;
+	test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(!num_inv);
+
+	/* Negative test: invalid vdev_id */
+	num_inv = 1;
+	inv_reqs[0].flags = 0;
+	inv_reqs[0].vdev_id = 0x9;
+	inv_reqs[0].cache_id = 0;
+	test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(!num_inv);
 
-		/*
-		 * Invalidate the 1st cache entry but fail the 2nd request
-		 * due to invalid cache_id configuration in the 2nd request.
-		 */
-		num_inv = 2;
-		inv_reqs[0].flags = 0;
-		inv_reqs[0].vdev_id = 0x99;
-		inv_reqs[0].cache_id = 0;
-		inv_reqs[1].flags = 0;
-		inv_reqs[1].vdev_id = 0x99;
-		inv_reqs[1].cache_id = MOCK_DEV_CACHE_ID_MAX + 1;
-		test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
-					   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(num_inv == 1);
-		test_cmd_dev_check_cache(dev_id, 0, 0);
-		test_cmd_dev_check_cache(dev_id, 1,
-					 IOMMU_TEST_DEV_CACHE_DEFAULT);
-		test_cmd_dev_check_cache(dev_id, 2,
-					 IOMMU_TEST_DEV_CACHE_DEFAULT);
-		test_cmd_dev_check_cache(dev_id, 3,
-					 IOMMU_TEST_DEV_CACHE_DEFAULT);
-
-		/* Invalidate the 2nd cache entry and verify */
-		num_inv = 1;
-		inv_reqs[0].flags = 0;
-		inv_reqs[0].vdev_id = 0x99;
-		inv_reqs[0].cache_id = 1;
-		test_cmd_viommu_invalidate(viommu_id, inv_reqs,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(num_inv == 1);
-		test_cmd_dev_check_cache(dev_id, 0, 0);
-		test_cmd_dev_check_cache(dev_id, 1, 0);
-		test_cmd_dev_check_cache(dev_id, 2,
-					 IOMMU_TEST_DEV_CACHE_DEFAULT);
-		test_cmd_dev_check_cache(dev_id, 3,
-					 IOMMU_TEST_DEV_CACHE_DEFAULT);
-
-		/* Invalidate the 3rd and 4th cache entries and verify */
-		num_inv = 2;
-		inv_reqs[0].flags = 0;
-		inv_reqs[0].vdev_id = 0x99;
-		inv_reqs[0].cache_id = 2;
-		inv_reqs[1].flags = 0;
-		inv_reqs[1].vdev_id = 0x99;
-		inv_reqs[1].cache_id = 3;
-		test_cmd_viommu_invalidate(viommu_id, inv_reqs,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(num_inv == 2);
-		test_cmd_dev_check_cache_all(dev_id, 0);
+	/*
+	 * Invalidate the 1st cache entry but fail the 2nd request
+	 * due to invalid flags configuration in the 2nd request.
+	 */
+	num_inv = 2;
+	inv_reqs[0].flags = 0;
+	inv_reqs[0].vdev_id = 0x99;
+	inv_reqs[0].cache_id = 0;
+	inv_reqs[1].flags = 0xffffffff;
+	inv_reqs[1].vdev_id = 0x99;
+	inv_reqs[1].cache_id = 1;
+	test_err_viommu_invalidate(EOPNOTSUPP, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(num_inv == 1);
+	test_cmd_dev_check_cache(dev_id, 0, 0);
+	test_cmd_dev_check_cache(dev_id, 1,
+				 IOMMU_TEST_DEV_CACHE_DEFAULT);
+	test_cmd_dev_check_cache(dev_id, 2,
+				 IOMMU_TEST_DEV_CACHE_DEFAULT);
+	test_cmd_dev_check_cache(dev_id, 3,
+				 IOMMU_TEST_DEV_CACHE_DEFAULT);
 
-		/* Invalidate all cache entries for nested_dev_id[1] and verify */
-		num_inv = 1;
-		inv_reqs[0].vdev_id = 0x99;
-		inv_reqs[0].flags = IOMMU_TEST_INVALIDATE_FLAG_ALL;
-		test_cmd_viommu_invalidate(viommu_id, inv_reqs,
-					   sizeof(*inv_reqs), &num_inv);
-		assert(num_inv == 1);
-		test_cmd_dev_check_cache_all(dev_id, 0);
-		test_ioctl_destroy(vdev_id);
-	}
+	/*
+	 * Invalidate the 1st cache entry but fail the 2nd request
+	 * due to invalid cache_id configuration in the 2nd request.
+	 */
+	num_inv = 2;
+	inv_reqs[0].flags = 0;
+	inv_reqs[0].vdev_id = 0x99;
+	inv_reqs[0].cache_id = 0;
+	inv_reqs[1].flags = 0;
+	inv_reqs[1].vdev_id = 0x99;
+	inv_reqs[1].cache_id = MOCK_DEV_CACHE_ID_MAX + 1;
+	test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs,
+				   IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(num_inv == 1);
+	test_cmd_dev_check_cache(dev_id, 0, 0);
+	test_cmd_dev_check_cache(dev_id, 1,
+				 IOMMU_TEST_DEV_CACHE_DEFAULT);
+	test_cmd_dev_check_cache(dev_id, 2,
+				 IOMMU_TEST_DEV_CACHE_DEFAULT);
+	test_cmd_dev_check_cache(dev_id, 3,
+				 IOMMU_TEST_DEV_CACHE_DEFAULT);
+
+	/* Invalidate the 2nd cache entry and verify */
+	num_inv = 1;
+	inv_reqs[0].flags = 0;
+	inv_reqs[0].vdev_id = 0x99;
+	inv_reqs[0].cache_id = 1;
+	test_cmd_viommu_invalidate(viommu_id, inv_reqs,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(num_inv == 1);
+	test_cmd_dev_check_cache(dev_id, 0, 0);
+	test_cmd_dev_check_cache(dev_id, 1, 0);
+	test_cmd_dev_check_cache(dev_id, 2,
+				 IOMMU_TEST_DEV_CACHE_DEFAULT);
+	test_cmd_dev_check_cache(dev_id, 3,
+				 IOMMU_TEST_DEV_CACHE_DEFAULT);
+
+	/* Invalidate the 3rd and 4th cache entries and verify */
+	num_inv = 2;
+	inv_reqs[0].flags = 0;
+	inv_reqs[0].vdev_id = 0x99;
+	inv_reqs[0].cache_id = 2;
+	inv_reqs[1].flags = 0;
+	inv_reqs[1].vdev_id = 0x99;
+	inv_reqs[1].cache_id = 3;
+	test_cmd_viommu_invalidate(viommu_id, inv_reqs,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(num_inv == 2);
+	test_cmd_dev_check_cache_all(dev_id, 0);
+
+	/* Invalidate all cache entries for nested_dev_id[1] and verify */
+	num_inv = 1;
+	inv_reqs[0].vdev_id = 0x99;
+	inv_reqs[0].flags = IOMMU_TEST_INVALIDATE_FLAG_ALL;
+	test_cmd_viommu_invalidate(viommu_id, inv_reqs,
+				   sizeof(*inv_reqs), &num_inv);
+	assert(num_inv == 1);
+	test_cmd_dev_check_cache_all(dev_id, 0);
+	test_ioctl_destroy(vdev_id);
 }
 
 FIXTURE(iommufd_device_pasid)
Signature · 1 line
2.25.1
maintainer Reviewed-by on v3 4/5
#30 lore raw

Re: [PATCH v3 4/5] iommufd/selftest: Explicitly skip tests for inapplicable variant

Quoted 9 lines · Xu Yilun

no_viommu is not applicable for some viommu/vdevice tests. Explicitly report the skipping, don't do it silently.

Only add the prints. No functional change intended.

---
 tools/testing/selftests/iommu/iommufd.c | 378 ++++++++++++------------
 1 file changed, 190 insertions(+), 188 deletions(-)

Jason

author
#31 lore raw

PATCH v3 5/5 iommufd/selftest: Add coverage for vdevice tombstone

In reply to Xu Yilun

This tests the flow to tombstone vdevice when idevice is to be unbound before vdevice destruction. The expected result is:

- idevice unbinding tombstones vdevice ID, the ID can't be repurposed anymore.
- Even ioctl(IOMMU_DESTROY) can't free the tombstoned ID.
- iommufd_fops_release() can still free everything.

---
 tools/testing/selftests/iommu/iommufd.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/testing/selftests/iommu/iommufd.c +14 -0
index 4a9b6e3b37fa..e1470a7a42cd 100644
@@ -3016,6 +3016,20 @@ TEST_F(iommufd_viommu, vdevice_cache)
 	test_ioctl_destroy(vdev_id);
 }
 
+TEST_F(iommufd_viommu, vdevice_tombstone)
+{
+	uint32_t viommu_id = self->viommu_id;
+	uint32_t dev_id = self->device_id;
+	uint32_t vdev_id = 0;
+
+	if (!dev_id)
+		SKIP(return, "Skipping test for variant no_viommu");
+
+	test_cmd_vdevice_alloc(viommu_id, dev_id, 0x99, &vdev_id);
+	test_ioctl_destroy(self->stdev_id);
+	EXPECT_ERRNO(ENOENT, _test_ioctl_destroy(self->fd, vdev_id));
+}
+
 FIXTURE(iommufd_device_pasid)
 {
 	int fd;
Signature · 1 line
2.25.1