Skip to contentSkip to conversation

Misc iommu_attach_handle enhancements in iommu core

PATCH Superseded Yi Liu 4 participants 34 messages Feb 12, 2025 – Feb 20, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yi Liu

author
#0 lore raw

cover Misc iommu_attach_handle enhancements in iommu core

This patch series aims to enhance the iommu_attach_handle-related APIs in the IOMMU core. The changes build upon Nic's previous series[1], which ensured that users of the attach/replace handle APIs always provide a valid handle. With this foundation, the series refactors the IOMMU core to share code between the normal attach/replace APIs and the handle-supporting APIs. Additionally, it seeks to avoid processing Page Request Interrupts (PRIs) before the attach operation succeeds. Ultimately, the group->domain becomes redundant and is retired.

This series also covers the prior series[2] which only takes care of the iommu_attach_device_pasid().

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

Regards,
Yi Liu

Yi Liu (5):
iommu: Make @handle mandatory in iommu_{attach|replace}_group_handle()
iommu: Drop iommu_group_replace_domain()
iommu: Store either domain or handle in group->pasid_array
iommu: Swap the order of setting group->pasid_array and calling attach
op of iommu drivers
iommu: Retire group->domain

 drivers/iommu/dma-iommu.c  |   3 +-
 drivers/iommu/iommu-priv.h |   1 +
 drivers/iommu/iommu.c      | 254 +++++++++++++++++++++----------------
 3 files changed, 151 insertions(+), 107 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/5 iommu: Make @handle mandatory in iommu_{attach|replace}_group_handle()

Caller of the two APIs always provide a valid handle, make @handle as mandatory parameter.

---
 drivers/iommu/iommu.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/iommu/iommu.c +10 -9
index 870c3cdbd0f6..eef7e19c547d 100644
@@ -3511,8 +3511,10 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
 {
 	int ret;
 
-	if (handle)
-		handle->domain = domain;
+	if (!handle)
+		return -EINVAL;
+
+	handle->domain = domain;
 
 	mutex_lock(&group->mutex);
 	ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
@@ -3568,16 +3570,15 @@ int iommu_replace_group_handle(struct iommu_group *group,
 	void *curr;
 	int ret;
 
-	if (!new_domain)
+	if (!new_domain || !handle)
 		return -EINVAL;
 
+	handle->domain = new_domain;
+
 	mutex_lock(&group->mutex);
-	if (handle) {
-		ret = xa_reserve(&group->pasid_array, IOMMU_NO_PASID, GFP_KERNEL);
-		if (ret)
-			goto err_unlock;
-		handle->domain = new_domain;
-	}
+	ret = xa_reserve(&group->pasid_array, IOMMU_NO_PASID, GFP_KERNEL);
+	if (ret)
+		goto err_unlock;
 
 	ret = __iommu_group_set_domain(group, new_domain);
 	if (ret)
Signature · 1 line
2.34.1
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] iommu: Make @handle mandatory in iommu_{attach|replace}_group_handle()

Quoted 7 lines · Yi Liu

Caller of the two APIs always provide a valid handle, make @handle as mandatory parameter.

---
 drivers/iommu/iommu.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

Jason

Reviewed-by on v1 1/5
#3 lore raw

Re: [PATCH 1/5] iommu: Make @handle mandatory in iommu_{attach|replace}_group_handle()

In reply to Yi Liu
Quoted 4 lines · Yi Liu

Caller of the two APIs always provide a valid handle, make @handle as mandatory parameter.

A side note here:

Quoted 14 lines
drivers/iommu/iommu.c +4 -2
@@ -3511,8 +3511,10 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
 {
 	int ret;
 
-	if (handle)
-		handle->domain = domain;
+	if (!handle)
+		return -EINVAL;
+
+	handle->domain = domain;
 
 	mutex_lock(&group->mutex);
 	ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);

drivers/iommu/iommu.c int iommu_replace_group_handle(struct iommu_group *group, · line 3578

Quoted 10 lines
drivers/iommu/iommu.c +3 -1
@@ -3568,16 +3570,15 @@ int iommu_replace_group_handle(struct iommu_group *group,
 	void *curr;
 	int ret;
 
-	if (!new_domain)
+	if (!new_domain || !handle)
 		return -EINVAL;
 
+	handle->domain = new_domain;
+
 	mutex_lock(&group->mutex);

So, we don't have the mutex fencing the handle->domain, since an attach handle is always a newly allocated one in the caller, and there won't be any potential race occurring to it.

Yet, this feels like an implication, which we should have stated explicitly in the kdocs of these two functions?

Thanks
Nicolin

author
#4 lore raw

Re: [PATCH 1/5] iommu: Make @handle mandatory in iommu_{attach|replace}_group_handle()

Quoted 39 lines · Nicolin Chen
Quoted 4 lines · Yi Liu

Caller of the two APIs always provide a valid handle, make @handle as mandatory parameter.

A side note here:

Quoted 14 lines
drivers/iommu/iommu.c +4 -2
@@ -3511,8 +3511,10 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
  {
  	int ret;
 
-	if (handle)
-		handle->domain = domain;
+	if (!handle)
+		return -EINVAL;
+
+	handle->domain = domain;
 
  	mutex_lock(&group->mutex);
  	ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);

drivers/iommu/iommu.c int iommu_replace_group_handle(struct iommu_group *group, · line 3578

Quoted 10 lines
drivers/iommu/iommu.c +3 -1
@@ -3568,16 +3570,15 @@ int iommu_replace_group_handle(struct iommu_group *group,
  	void *curr;
  	int ret;
 
-	if (!new_domain)
+	if (!new_domain || !handle)
  		return -EINVAL;
 
+	handle->domain = new_domain;
+
  	mutex_lock(&group->mutex);

So, we don't have the mutex fencing the handle->domain, since an attach handle is always a newly allocated one in the caller, and there won't be any potential race occurring to it.

Quoted 3 lines

Yet, this feels like an implication, which we should have stated explicitly in the kdocs of these two functions?

hmmm. I can just move it under mutex. :)

Signature · 2 lines
Regards,
Yi Liu
author
#6 lore raw

PATCH 2/5 iommu: Drop iommu_group_replace_domain()

In reply to Yi Liu

iommufd does not use it now, so drop it.

---
 drivers/iommu/iommu.c | 35 ++++++-----------------------------
 1 file changed, 6 insertions(+), 29 deletions(-)
drivers/iommu/iommu.c +6 -29
index eef7e19c547d..4ccb36b5aa30 100644
@@ -2187,32 +2187,6 @@ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
 }
 EXPORT_SYMBOL_GPL(iommu_attach_group);
 
-/**
- * iommu_group_replace_domain - replace the domain that a group is attached to
- * @group: IOMMU group that will be attached to the new domain
- * @new_domain: new IOMMU domain to replace with
- *
- * This API allows the group to switch domains without being forced to go to
- * the blocking domain in-between.
- *
- * If the currently attached domain is a core domain (e.g. a default_domain),
- * it will act just like the iommu_attach_group().
- */
-int iommu_group_replace_domain(struct iommu_group *group,
-			       struct iommu_domain *new_domain)
-{
-	int ret;
-
-	if (!new_domain)
-		return -EINVAL;
-
-	mutex_lock(&group->mutex);
-	ret = __iommu_group_set_domain(group, new_domain);
-	mutex_unlock(&group->mutex);
-	return ret;
-}
-EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, "IOMMUFD_INTERNAL");
-
 static int __iommu_device_set_domain(struct iommu_group *group,
 				     struct device *dev,
 				     struct iommu_domain *new_domain,
@@ -3559,9 +3533,12 @@ EXPORT_SYMBOL_NS_GPL(iommu_detach_group_handle, "IOMMUFD_INTERNAL");
  * @new_domain: new IOMMU domain to replace with
  * @handle: attach handle
  *
- * This is a variant of iommu_group_replace_domain(). It allows the caller to
- * provide an attach handle for the new domain and use it when the domain is
- * attached.
+ * This API allows the group to switch domains without being forced to go to
+ * the blocking domain in-between. It allows the caller to provide an attach
+ * handle for the new domain and use it when the domain is attached.
+ *
+ * If the currently attached domain is a core domain (e.g. a default_domain),
+ * it will act just like the iommu_attach_group_handle().
  */
 int iommu_replace_group_handle(struct iommu_group *group,
 			       struct iommu_domain *new_domain,
Signature · 1 line
2.34.1
author
#9 lore raw

Re: [PATCH 2/5] iommu: Drop iommu_group_replace_domain()

Quoted 10 lines · Nicolin Chen
Quoted 6 lines · Yi Liu

iommufd does not use it now, so drop it.

---
  drivers/iommu/iommu.c | 35 ++++++-----------------------------
  1 file changed, 6 insertions(+), 29 deletions(-)

Should we drop it in the following header too?
drivers/iommu/iommu-priv.h

yes.

Quoted 2 lines

Otherwise,

Signature · 2 lines
Regards,
Yi Liu
author
#11 lore raw

PATCH 3/5 iommu: Store either domain or handle in group->pasid_array

In reply to Yi Liu

iommu_attach_device_pasid() only stores handle to group->pasid_array when there is a valid handle input. However, it makes the iommu_attach_device_pasid() unable to detect if the pasid has been attached or not previously.

To be complete, let the iommu_attach_device_pasid() store the domain to group->pasid_array if no valid handle. The other users of the group->pasid_array should be updated to be consistent. e.g. the iommu_attach_group_handle() and iommu_replace_group_handle().

---
 drivers/iommu/iommu.c | 47 +++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 13 deletions(-)
drivers/iommu/iommu.c +34 -13
index 4ccb36b5aa30..a7ebcbaafb10 100644
@@ -45,6 +45,9 @@ static unsigned int iommu_def_domain_type __read_mostly;
 static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
 static u32 iommu_cmd_line __read_mostly;
 
+/* Tags used with xa_tag_pointer() in group->pasid_array */
+enum { IOMMU_PASID_ARRAY_DOMAIN = 0, IOMMU_PASID_ARRAY_HANDLE = 1 };
+
 struct iommu_group {
 	struct kobject kobj;
 	struct kobject *devices_kobj;
@@ -3331,6 +3334,17 @@ static void __iommu_remove_group_pasid(struct iommu_group *group,
 		iommu_remove_dev_pasid(device->dev, pasid, domain);
 }
 
+static void *iommu_make_pasid_entry(struct iommu_domain *domain,
+				    struct iommu_attach_handle *handle)
+{
+	if (handle) {
+		handle->domain = domain;
+		return xa_tag_pointer(handle, IOMMU_PASID_ARRAY_HANDLE);
+	}
+
+	return xa_tag_pointer(domain, IOMMU_PASID_ARRAY_DOMAIN);
+}
+
 /*
  * iommu_attach_device_pasid() - Attach a domain to pasid of device
  * @domain: the iommu domain.
@@ -3348,6 +3362,7 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 	struct iommu_group *group = dev->iommu_group;
 	struct group_device *device;
 	const struct iommu_ops *ops;
+	void *pasid_entry;
 	int ret;
 
 	if (!group)
@@ -3363,6 +3378,8 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 	if (ops != domain->owner || pasid == IOMMU_NO_PASID)
 		return -EINVAL;
 
+	pasid_entry = iommu_make_pasid_entry(domain, handle);
+
 	mutex_lock(&group->mutex);
 	for_each_group_device(group, device) {
 		if (pasid >= device->dev->iommu->max_pasids) {
@@ -3371,10 +3388,7 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 		}
 	}
 
-	if (handle)
-		handle->domain = domain;
-
-	ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
+	ret = xa_insert(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL);
 	if (ret)
 		goto out_unlock;
 
@@ -3454,13 +3468,17 @@ struct iommu_attach_handle *
 iommu_attach_handle_get(struct iommu_group *group, ioasid_t pasid, unsigned int type)
 {
 	struct iommu_attach_handle *handle;
+	void *pasid_entry;
 
 	xa_lock(&group->pasid_array);
-	handle = xa_load(&group->pasid_array, pasid);
-	if (!handle)
+	pasid_entry = xa_load(&group->pasid_array, pasid);
+	if (xa_pointer_tag(pasid_entry) == IOMMU_PASID_ARRAY_HANDLE) {
+		handle = xa_untag_pointer(pasid_entry);
+		if (type && handle->domain->type != type)
+			handle = ERR_PTR(-EBUSY);
+	} else {
 		handle = ERR_PTR(-ENOENT);
-	else if (type && handle->domain->type != type)
-		handle = ERR_PTR(-EBUSY);
+	}
 	xa_unlock(&group->pasid_array);
 
 	return handle;
@@ -3483,15 +3501,17 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
 			      struct iommu_group *group,
 			      struct iommu_attach_handle *handle)
 {
+	void *pasid_entry;
 	int ret;
 
 	if (!handle)
 		return -EINVAL;
 
-	handle->domain = domain;
+	pasid_entry = iommu_make_pasid_entry(domain, handle);
 
 	mutex_lock(&group->mutex);
-	ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
+	ret = xa_insert(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);
 	if (ret)
 		goto err_unlock;
 
@@ -3544,13 +3564,13 @@ int iommu_replace_group_handle(struct iommu_group *group,
 			       struct iommu_domain *new_domain,
 			       struct iommu_attach_handle *handle)
 {
-	void *curr;
+	void *curr, *pasid_entry;
 	int ret;
 
 	if (!new_domain || !handle)
 		return -EINVAL;
 
-	handle->domain = new_domain;
+	pasid_entry = iommu_make_pasid_entry(new_domain, handle);
 
 	mutex_lock(&group->mutex);
 	ret = xa_reserve(&group->pasid_array, IOMMU_NO_PASID, GFP_KERNEL);
@@ -3561,7 +3581,8 @@ int iommu_replace_group_handle(struct iommu_group *group,
 	if (ret)
 		goto err_release;
 
-	curr = xa_store(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
+	curr = xa_store(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);
 	WARN_ON(xa_is_err(curr));
 
 	mutex_unlock(&group->mutex);
Signature · 1 line
2.34.1
Reviewed-by on v1 3/5
#12 lore raw

Re: [PATCH 3/5] iommu: Store either domain or handle in group->pasid_array

Quoted 10 lines · Yi Liu

iommu_attach_device_pasid() only stores handle to group->pasid_array when there is a valid handle input. However, it makes the iommu_attach_device_pasid() unable to detect if the pasid has been attached or not previously.

To be complete, let the iommu_attach_device_pasid() store the domain to group->pasid_array if no valid handle. The other users of the group->pasid_array should be updated to be consistent. e.g. the iommu_attach_group_handle() and iommu_replace_group_handle().

Quoted 5 lines
---
 drivers/iommu/iommu.c | 47 +++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 13 deletions(-)

Jason

Reviewed-by on v1 3/5
#13 lore raw

Re: [PATCH 3/5] iommu: Store either domain or handle in group->pasid_array

In reply to Yi Liu
Quoted 5 lines · Yi Liu

iommu_attach_device_pasid() only stores handle to group->pasid_array when there is a valid handle input. However, it makes the iommu_attach_device_pasid() unable to detect if the pasid has been attached or not previously.

Quoted 7 lines

To be complete, let the iommu_attach_device_pasid() store the domain to group->pasid_array if no valid handle. The other users of the group->pasid_array should be updated to be consistent. e.g. the iommu_attach_group_handle() and iommu_replace_group_handle().

A nit:

Quoted 17 lines
drivers/iommu/iommu.c +4 -2
@@ -3483,15 +3501,17 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
 			      struct iommu_group *group,
 			      struct iommu_attach_handle *handle)
 {
+	void *pasid_entry;
 	int ret;
 
 	if (!handle)
 		return -EINVAL;
 
-	handle->domain = domain;
+	pasid_entry = iommu_make_pasid_entry(domain, handle);
 
 	mutex_lock(&group->mutex);
-	ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
+	ret = xa_insert(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);

[..]

drivers/iommu/iommu.c int iommu_replace_group_handle(struct iommu_group *group, · line 3585

Quoted 7 lines
drivers/iommu/iommu.c +2 -1
@@ -3561,7 +3581,8 @@ int iommu_replace_group_handle(struct iommu_group *group,
 	if (ret)
 		goto err_release;
 
-	curr = xa_store(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
+	curr = xa_store(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);

It's actually an entry for the group->pasid_array.

So, perhaps:
- s/iommu_make_pasid_entry/iommu_make_pasid_array_entry/
- s/pasid_entry/entry/

This feels a bit clearer and can avoid the two line breaks above.

Thanks
Nic

author
#14 lore raw

Re: [PATCH 3/5] iommu: Store either domain or handle in group->pasid_array

Quoted 46 lines · Nicolin Chen
Quoted 5 lines · Yi Liu

iommu_attach_device_pasid() only stores handle to group->pasid_array when there is a valid handle input. However, it makes the iommu_attach_device_pasid() unable to detect if the pasid has been attached or not previously.

Quoted 7 lines

To be complete, let the iommu_attach_device_pasid() store the domain to group->pasid_array if no valid handle. The other users of the group->pasid_array should be updated to be consistent. e.g. the iommu_attach_group_handle() and iommu_replace_group_handle().

A nit:

Quoted 17 lines
drivers/iommu/iommu.c +4 -2
@@ -3483,15 +3501,17 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
  			      struct iommu_group *group,
  			      struct iommu_attach_handle *handle)
  {
+	void *pasid_entry;
  	int ret;
 
  	if (!handle)
  		return -EINVAL;
 
-	handle->domain = domain;
+	pasid_entry = iommu_make_pasid_entry(domain, handle);
 
  	mutex_lock(&group->mutex);
-	ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
+	ret = xa_insert(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);

[..]

drivers/iommu/iommu.c int iommu_replace_group_handle(struct iommu_group *group, · line 3585

Quoted 7 lines
drivers/iommu/iommu.c +2 -1
@@ -3561,7 +3581,8 @@ int iommu_replace_group_handle(struct iommu_group *group,
  	if (ret)
  		goto err_release;
 
-	curr = xa_store(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
+	curr = xa_store(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);

It's actually an entry for the group->pasid_array.

Quoted 6 lines

So, perhaps:
- s/iommu_make_pasid_entry/iommu_make_pasid_array_entry/
- s/pasid_entry/entry/

This feels a bit clearer and can avoid the two line breaks above.

sure.

Signature · 2 lines
Regards,
Yi Liu
#15 lore raw

RE: [PATCH 3/5] iommu: Store either domain or handle in group->pasid_array

In reply to Nicolin Chen
Quoted 53 lines

From: Nicolin Chen <[email protected]>
Sent: Thursday, February 20, 2025 1:16 AM

Quoted 5 lines · Yi Liu

iommu_attach_device_pasid() only stores handle to group->pasid_array when there is a valid handle input. However, it makes the iommu_attach_device_pasid() unable to detect if the pasid has been attached or not previously.

Quoted 7 lines

To be complete, let the iommu_attach_device_pasid() store the domain to group->pasid_array if no valid handle. The other users of the group->pasid_array should be updated to be consistent. e.g. the iommu_attach_group_handle() and iommu_replace_group_handle().

A nit:

Review int iommu_attach_group_handle(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -3483,15 +3501,17 @@ int iommu_attach_group_handle(struct

iommu_domain *domain,

Quoted 4 lines
drivers/iommu/iommu.c +1 -0
 			      struct iommu_group *group,
 			      struct iommu_attach_handle *handle)
 {
+	void *pasid_entry;

drivers/iommu/iommu.c int iommu_attach_group_handle(struct iommu_domain *domain,

Quoted 10 lines
drivers/iommu/iommu.c +1 -2
 	int ret;
 
 	if (!handle)
 		return -EINVAL;
 
-	handle->domain = domain;
+	pasid_entry = iommu_make_pasid_entry(domain, handle);
 
 	mutex_lock(&group->mutex);
-	ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle,

GFP_KERNEL);

Quoted 2 lines
drivers/iommu/iommu.c +2 -0
+	ret = xa_insert(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);

[..]

Review int iommu_replace_group_handle(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -3561,7 +3581,8 @@ int iommu_replace_group_handle(struct

iommu_group *group,

drivers/iommu/iommu.c int iommu_replace_group_handle(struct iommu_group *group,

Quoted 4 lines
drivers/iommu/iommu.c +0 -1
 	if (ret)
 		goto err_release;
 
-	curr = xa_store(&group->pasid_array, IOMMU_NO_PASID, handle,

GFP_KERNEL);

drivers/iommu/iommu.c int iommu_replace_group_handle(struct iommu_group *group, · line 3585

Quoted 2 lines
drivers/iommu/iommu.c +2 -0
+	curr = xa_store(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);

It's actually an entry for the group->pasid_array.

Quoted 6 lines

So, perhaps:
- s/iommu_make_pasid_entry/iommu_make_pasid_array_entry/
- s/pasid_entry/entry/

This feels a bit clearer and can avoid the two line breaks above.

or iommu_tag_pasid_pointer()

Reviewed-by on v1 3/5
#16 lore raw

RE: [PATCH 3/5] iommu: Store either domain or handle in group->pasid_array

In reply to Yi Liu
Quoted 8 lines

From: Liu, Yi L <[email protected]>
Sent: Wednesday, February 12, 2025 2:06 PM

iommu_attach_device_pasid() only stores handle to group->pasid_array when there is a valid handle input. However, it makes the iommu_attach_device_pasid() unable to detect if the pasid has been attached or not previously.

Quoted 7 lines

To be complete, let the iommu_attach_device_pasid() store the domain to group->pasid_array if no valid handle. The other users of the group->pasid_array should be updated to be consistent. e.g. the iommu_attach_group_handle() and iommu_replace_group_handle().

author
#17 lore raw

PATCH 4/5 iommu: Swap the order of setting group->pasid_array and calling attach op of iommu drivers

In reply to Yi Liu

The current implementation stores group->pasid_array before the underlying iommu driver has successfully set the new domain. This can lead to issues where PRIs are received on the new domain before the attach operation is completed. This patch swaps the order of operations to ensure that the domain is set in the iommu driver before updating group->pasid_array.

---
 drivers/iommu/iommu.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
drivers/iommu/iommu.c +24 -9
index a7ebcbaafb10..d673feab82aa 100644
@@ -3388,13 +3388,21 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 		}
 	}
 
-	ret = xa_insert(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL);
-	if (ret)
+	if (xa_load(&group->pasid_array, pasid)) {
+		ret = -EBUSY;
 		goto out_unlock;
+	}
 
 	ret = __iommu_set_group_pasid(domain, group, pasid);
 	if (ret)
-		xa_erase(&group->pasid_array, pasid);
+		goto out_unlock;
+
+	ret = xa_insert(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL);
+	if (ret) {
+		WARN_ON(ret == -EBUSY);
+		__iommu_remove_group_pasid(group, pasid, domain);
+	}
+
 out_unlock:
 	mutex_unlock(&group->mutex);
 	return ret;
@@ -3510,19 +3518,26 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
 	pasid_entry = iommu_make_pasid_entry(domain, handle);
 
 	mutex_lock(&group->mutex);
-	ret = xa_insert(&group->pasid_array,
-			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);
-	if (ret)
+	if (xa_load(&group->pasid_array, IOMMU_NO_PASID)) {
+		ret = -EBUSY;
 		goto err_unlock;
+	}
 
 	ret = __iommu_attach_group(domain, group);
 	if (ret)
-		goto err_erase;
+		goto err_unlock;
+
+	ret = xa_insert(&group->pasid_array,
+			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);
+	if (ret) {
+		WARN_ON(ret == -EBUSY);
+		goto err_remove;
+	}
 	mutex_unlock(&group->mutex);
 
 	return 0;
-err_erase:
-	xa_erase(&group->pasid_array, IOMMU_NO_PASID);
+err_remove:
+	__iommu_group_set_core_domain(group);
 err_unlock:
 	mutex_unlock(&group->mutex);
 	return ret;
Signature · 1 line
2.34.1
#18 lore raw

Re: [PATCH 4/5] iommu: Swap the order of setting group->pasid_array and calling attach op of iommu drivers

Quoted 5 lines · Yi Liu

The current implementation stores group->pasid_array before the underlying iommu driver has successfully set the new domain. This can lead to issues where PRIs are received on the new domain before the attach operation is completed. This patch swaps the order of operations to ensure that the domain is set in the iommu driver before updating group->pasid_array.

I think I would drop this note in a comment since it is subtle and important..

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3394

Quoted 6 lines
drivers/iommu/iommu.c +3 -2
-	ret = xa_insert(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL);
-	if (ret)
+	if (xa_load(&group->pasid_array, pasid)) {
+		ret = -EBUSY;
 		goto out_unlock;
+	}

Let's write it like this:

	ret = xa_insert(&group->pasid_array, pasid, XA_ZERO_ENTRY, GFP_KERNEL);
	if (ret)
		goto out_unlock;

	ret = __iommu_set_group_pasid(domain, group, pasid);
	if (ret) {
		xa_release(&group->pasid_array, pasid);
		goto out_unlock;
	}

	/*
	 * The xa_insert() above reserved the memory, and the group->mutex is
	 * held, this cannot fail. The new domain cannot be visible until the
	 * operation succeeds as we cannot tolerate PRIs becoming concurrently
	 * queued and then failing attach.
	 */
	ret = xa_err(
		xa_store(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL));
	WARN_ON(ret);
	ret = 0;

drivers/iommu/iommu.c int iommu_attach_group_handle(struct iommu_domain *domain, · line 3524

Quoted 11 lines
drivers/iommu/iommu.c +3 -3
@@ -3510,19 +3518,26 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
 	pasid_entry = iommu_make_pasid_entry(domain, handle);
 
 	mutex_lock(&group->mutex);
-	ret = xa_insert(&group->pasid_array,
-			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);
-	if (ret)
+	if (xa_load(&group->pasid_array, IOMMU_NO_PASID)) {
+		ret = -EBUSY;
 		goto err_unlock;
+	}

Same here

Jason

author
#19 lore raw

Re: [PATCH 4/5] iommu: Swap the order of setting group->pasid_array and calling attach op of iommu drivers

Quoted 31 lines · Jason Gunthorpe
Quoted 5 lines · Yi Liu

The current implementation stores group->pasid_array before the underlying iommu driver has successfully set the new domain. This can lead to issues where PRIs are received on the new domain before the attach operation is completed. This patch swaps the order of operations to ensure that the domain is set in the iommu driver before updating group->pasid_array.

I think I would drop this note in a comment since it is subtle and important..

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3394

Quoted 6 lines
drivers/iommu/iommu.c +3 -2
-	ret = xa_insert(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL);
-	if (ret)
+	if (xa_load(&group->pasid_array, pasid)) {
+		ret = -EBUSY;
  		goto out_unlock;
+	}

Let's write it like this:

	ret = xa_insert(&group->pasid_array, pasid, XA_ZERO_ENTRY, GFP_KERNEL);
	if (ret)
		goto out_unlock;

	ret = __iommu_set_group_pasid(domain, group, pasid);
	if (ret) {
		xa_release(&group->pasid_array, pasid);
		goto out_unlock;
	}

	/*
	 * The xa_insert() above reserved the memory, and the group->mutex is
Quoted 8 lines
	 * held, this cannot fail. The new domain cannot be visible until the
	 * operation succeeds as we cannot tolerate PRIs becoming concurrently
	 * queued and then failing attach.
	 */
	ret = xa_err(
		xa_store(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL));
	WARN_ON(ret);
	ret = 0;

perhaps we can save the above line by WARN_ON(xa_is_err(xa_store())); :)

Quoted 4 lines
Quoted 4 lines
drivers/iommu/iommu.c +0 -0
@@ -3510,19 +3518,26 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
  	pasid_entry = iommu_make_pasid_entry(domain, handle);
 
Quoted 10 lines

drivers/iommu/iommu.c int iommu_attach_group_handle(struct iommu_domain *domain, · line 3524

Quoted 8 lines
drivers/iommu/iommu.c +3 -3
  	mutex_lock(&group->mutex);
-	ret = xa_insert(&group->pasid_array,
-			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);
-	if (ret)
+	if (xa_load(&group->pasid_array, IOMMU_NO_PASID)) {
+		ret = -EBUSY;
  		goto err_unlock;
+	}

Same here

yes.

Signature · 2 lines
Regards,
Yi Liu
Reviewed-by
#20 lore raw

RE: [PATCH 4/5] iommu: Swap the order of setting group->pasid_array and calling attach op of iommu drivers

In reply to Jason Gunthorpe
Quoted 41 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, February 19, 2025 3:27 AM

Quoted 1 line · Yi Liu

The current implementation stores group->pasid_array before the

underlying

Quoted 4 lines

iommu driver has successfully set the new domain. This can lead to issues where PRIs are received on the new domain before the attach operation is completed. This patch swaps the order of operations to ensure that the domain is set in the iommu driver before updating group->pasid_array.

I think I would drop this note in a comment since it is subtle and important..

Review

Quoted 1 line
(quoted diff) +0 -1
-	ret = xa_insert(&group->pasid_array, pasid, pasid_entry,

GFP_KERNEL);

drivers/iommu/iommu.c int iommu_attach_device_pasid(struct iommu_domain *domain, · line 3394

Quoted 5 lines
drivers/iommu/iommu.c +3 -1
-	if (ret)
+	if (xa_load(&group->pasid_array, pasid)) {
+		ret = -EBUSY;
 		goto out_unlock;
+	}

Let's write it like this:

	ret = xa_insert(&group->pasid_array, pasid, XA_ZERO_ENTRY,
GFP_KERNEL);
	if (ret)
		goto out_unlock;

	ret = __iommu_set_group_pasid(domain, group, pasid);
	if (ret) {
		xa_release(&group->pasid_array, pasid);
		goto out_unlock;
	}

	/*
	 * The xa_insert() above reserved the memory, and the group-
Quoted 1 line

mutex is

* held, this cannot fail. The new domain cannot be visible until the
* operation succeeds as we cannot tolerate PRIs becoming
concurrently

Quoted 8 lines
	 * queued and then failing attach.
	 */
	ret = xa_err(
		xa_store(&group->pasid_array, pasid, pasid_entry,
GFP_KERNEL));
	WARN_ON(ret);
	ret = 0;

with above change:

author
#21 lore raw

PATCH 5/5 iommu: Retire group->domain

In reply to Yi Liu

group->domain stores the current attached domain of the group. Now, it is also stored in group->pasid_array[0], hence retire the group->domain.

---
 drivers/iommu/dma-iommu.c  |   3 +-
 drivers/iommu/iommu-priv.h |   1 +
 drivers/iommu/iommu.c      | 178 +++++++++++++++++++++----------------
 3 files changed, 106 insertions(+), 76 deletions(-)
drivers/iommu/dma-iommu.c +2 -1
index 2a9fa0c8cc00..5da9aca0c021 100644
@@ -34,6 +34,7 @@
 
 #include "dma-iommu.h"
 #include "iommu-pages.h"
+#include "iommu-priv.h"
 
 struct iommu_dma_msi_page {
 	struct list_head	list;
@@ -1746,7 +1747,7 @@ size_t iommu_dma_max_mapping_size(struct device *dev)
 
 void iommu_setup_dma_ops(struct device *dev)
 {
-	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
+	struct iommu_domain *domain = iommu_group_domain(dev->iommu_group);
 
 	if (dev_is_pci(dev))
 		dev->iommu->pci_32bit_workaround = !iommu_dma_forcedac;
drivers/iommu/iommu-priv.h +1 -0
index de5b54eaa8bf..fedc57754a48 100644
@@ -46,4 +46,5 @@ void iommu_detach_group_handle(struct iommu_domain *domain,
 int iommu_replace_group_handle(struct iommu_group *group,
 			       struct iommu_domain *new_domain,
 			       struct iommu_attach_handle *handle);
+struct iommu_domain *iommu_group_domain(struct iommu_group *group);
 #endif /* __LINUX_IOMMU_PRIV_H */
drivers/iommu/iommu.c +103 -75

411 lines not on this page · raw message

Signature · 1 line
2.34.1
#22 lore raw

Re: [PATCH 5/5] iommu: Retire group->domain

drivers/iommu/iommu.c struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) · line 2174

Quoted 13 lines · Yi Liu
drivers/iommu/iommu.c +4 -1
@@ -2133,11 +2164,16 @@ struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
 {
 	/* Caller must be a probed driver on dev */
 	struct iommu_group *group = dev->iommu_group;
+	struct iommu_domain *gdomain;
 
 	if (!group)
 		return NULL;
 
-	return group->domain;
+	mutex_lock(&group->mutex);
+	gdomain = iommu_group_domain(group);
+	mutex_unlock(&group->mutex);

That will deadlock

Eg:

static int mtk_iommu_identity_attach(struct iommu_domain *identity_domain,
struct device *dev)

{
	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);

And attach is already called under the group mutex.

IMHO I would have two operations, iommu_group_domain() which lockdep asserts that group->mutex is held by the caller. We should try to call this function when possible to get clear locking rules

And then iommu_get_domain_for_dev() which explains it is an older function that depends on the caller somehow ensuring that the attached domain cannot change. Ideally we would someday try to remove calls to iommu_get_domain_for_dev()..

Since the xarray cannot change for non-lockdepable reasons, it can just call xa_load(). Probably re-organize iommu_group_domain() to have a __iommu_group_domain() which has the body but no lockdep assertion.

Jason

author
#23 lore raw

Re: [PATCH 5/5] iommu: Retire group->domain

Quoted 17 lines · Jason Gunthorpe

drivers/iommu/iommu.c struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) · line 2174

Quoted 13 lines · Yi Liu
drivers/iommu/iommu.c +4 -1
@@ -2133,11 +2164,16 @@ struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
  {
  	/* Caller must be a probed driver on dev */
  	struct iommu_group *group = dev->iommu_group;
+	struct iommu_domain *gdomain;
 
  	if (!group)
  		return NULL;
 
-	return group->domain;
+	mutex_lock(&group->mutex);
+	gdomain = iommu_group_domain(group);
+	mutex_unlock(&group->mutex);

That will deadlock

Quoted 8 lines

Eg:

static int mtk_iommu_identity_attach(struct iommu_domain *identity_domain,
struct device *dev)

{
	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);

And attach is already called under the group mutex.

oops. yes, it is!

Quoted 8 lines

IMHO I would have two operations, iommu_group_domain() which lockdep asserts that group->mutex is held by the caller. We should try to call this function when possible to get clear locking rules

And then iommu_get_domain_for_dev() which explains it is an older function that depends on the caller somehow ensuring that the attached domain cannot change. Ideally we would someday try to remove calls to iommu_get_domain_for_dev()..

The existing iommu_get_domain_for_dev() seems to be abused in some places. There are some device drivers that call it without group->mutex.

Quoted 3 lines

Since the xarray cannot change for non-lockdepable reasons, it can just call xa_load(). Probably re-organize iommu_group_domain() to have a __iommu_group_domain() which has the body but no lockdep assertion.

makes sense.

Signature · 2 lines
Regards,
Yi Liu
author
#24 lore raw

Re: [PATCH 5/5] iommu: Retire group->domain

Quoted 19 lines · Yi Liu
Quoted 18 lines · Jason Gunthorpe

drivers/iommu/iommu.c struct iommu_domain · line 2173

Quoted 12 lines · Yi Liu
drivers/iommu/iommu.c +4 -1
@@ -2133,11 +2164,16 @@ struct iommu_domain 
 *iommu_get_domain_for_dev(struct device *dev)
   {
       /* Caller must be a probed driver on dev */
       struct iommu_group *group = dev->iommu_group;
+    struct iommu_domain *gdomain;
       if (!group)
           return NULL;
-    return group->domain;
+    mutex_lock(&group->mutex);
+    gdomain = iommu_group_domain(group);
+    mutex_unlock(&group->mutex);

That will deadlock

Eg:

Quoted 17 lines
Quoted 6 lines

static int mtk_iommu_identity_attach(struct iommu_domain *identity_domain, struct device *dev)
{
    struct iommu_domain *domain = iommu_get_domain_for_dev(dev);

And attach is already called under the group mutex.

oops. yes, it is!

Quoted 8 lines

IMHO I would have two operations, iommu_group_domain() which lockdep asserts that group->mutex is held by the caller. We should try to call this function when possible to get clear locking rules

And then iommu_get_domain_for_dev() which explains it is an older function that depends on the caller somehow ensuring that the attached domain cannot change. Ideally we would someday try to remove calls to iommu_get_domain_for_dev()..

currently, the paths hold group->mutex are mostly in the iommu drivers. So what we may do is updating the iommu drivers to use a version that has lockdep. While other callers keep using the iommu_get_domain_for_dev().

Quoted 3 lines
Quoted 3 lines

Since the xarray cannot change for non-lockdepable reasons, it can just call xa_load(). Probably re-organize iommu_group_domain() to have a __iommu_group_domain() which has the body but no lockdep assertion.

another approach may be holding xa_lock when retrieving domain from xarray in the iommu_get_domain_for_dev() helper. This approach is consistent with the usage pattern of iommu_attach_handle_get(), where callers also do not acquire the group->mutex. Thoughts?

Signature · 2 lines
Regards,
Yi Liu
#25 lore raw

Re: [PATCH 5/5] iommu: Retire group->domain

Quoted 8 lines · Yi Liu
Quoted 4 lines
Quoted 4 lines

And then iommu_get_domain_for_dev() which explains it is an older function that depends on the caller somehow ensuring that the attached domain cannot change. Ideally we would someday try to remove calls to iommu_get_domain_for_dev()..

currently, the paths hold group->mutex are mostly in the iommu drivers. So what we may do is updating the iommu drivers to use a version that has lockdep. While other callers keep using the iommu_get_domain_for_dev().

Honestly I would prefer to pass the old domain in as a function argument to attach and remove the calls entirely.

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

Since the xarray cannot change for non-lockdepable reasons, it can just call xa_load(). Probably re-organize iommu_group_domain() to have a __iommu_group_domain() which has the body but no lockdep assertion.

another approach may be holding xa_lock when retrieving domain from xarray in the iommu_get_domain_for_dev() helper. This approach is consistent with the usage pattern of iommu_attach_handle_get(), where callers also do not acquire the group->mutex. Thoughts?

It doesn't matter, the pointer is unsafe the instant the xa_lock is released.

The locking works in most places that don't hold the group mutex because they rely on a driver being probed and the dma api keeping the domain constant during that period.

Jason

author
#26 lore raw

Re: [PATCH 5/5] iommu: Retire group->domain

Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Yi Liu
Quoted 4 lines
Quoted 4 lines

And then iommu_get_domain_for_dev() which explains it is an older function that depends on the caller somehow ensuring that the attached domain cannot change. Ideally we would someday try to remove calls to iommu_get_domain_for_dev()..

Quoted 6 lines
Quoted 3 lines

currently, the paths hold group->mutex are mostly in the iommu drivers. So what we may do is updating the iommu drivers to use a version that has lockdep. While other callers keep using the iommu_get_domain_for_dev().

Honestly I would prefer to pass the old domain in as a function argument to attach and remove the calls entirely.

hmmm. I may drop the retire group->domain from this series to avoid blocking the iommufd pasid series.

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

Since the xarray cannot change for non-lockdepable reasons, it can just call xa_load(). Probably re-organize iommu_group_domain() to have a __iommu_group_domain() which has the body but no lockdep assertion.

another approach may be holding xa_lock when retrieving domain from xarray in the iommu_get_domain_for_dev() helper. This approach is consistent with the usage pattern of iommu_attach_handle_get(), where callers also do not acquire the group->mutex. Thoughts?

Quoted 6 lines

It doesn't matter, the pointer is unsafe the instant the xa_lock is released.

The locking works in most places that don't hold the group mutex because they rely on a driver being probed and the dma api keeping the domain constant during that period.

yes. Since group->domain is set/clear under group->mutex as well. So I'm wondering if it is ok to add kdoc to iommu_get_domain_for_dev() to note it, and just add the xa_load && entry_decode stuffs without lockdep?

Signature · 2 lines
Regards,
Yi Liu
#27 lore raw

RE: [PATCH 5/5] iommu: Retire group->domain

Quoted 15 lines

From: Liu, Yi L <[email protected]>
Sent: Thursday, February 20, 2025 12:02 PM

Quoted 3 lines · Jason Gunthorpe
Quoted 2 lines · Yi Liu
Quoted 2 lines
Quoted 2 lines

And then iommu_get_domain_for_dev() which explains it is an older function that depends on the caller somehow ensuring that the

attached

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

domain cannot change. Ideally we would someday try to remove calls

to

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

iommu_get_domain_for_dev()..

currently, the paths hold group->mutex are mostly in the iommu drivers. So what we may do is updating the iommu drivers to use a version that has lockdep. While other callers keep using the

Quoted 9 lines

iommu_get_domain_for_dev().

Quoted 2 lines

Honestly I would prefer to pass the old domain in as a function argument

to

Quoted 1 line

attach and remove the calls entirely.

hmmm. I may drop the retire group->domain from this series to avoid blocking the iommufd pasid series.

This kind of makes sense to me.

#28 lore raw

Re: [PATCH 5/5] iommu: Retire group->domain

In reply to Yi Liu
Quoted 11 lines · Yi Liu
Quoted 10 lines · Jason Gunthorpe
Quoted 8 lines · Yi Liu
Quoted 4 lines
Quoted 4 lines

And then iommu_get_domain_for_dev() which explains it is an older function that depends on the caller somehow ensuring that the attached domain cannot change. Ideally we would someday try to remove calls to iommu_get_domain_for_dev()..

currently, the paths hold group->mutex are mostly in the iommu drivers. So what we may do is updating the iommu drivers to use a version that has lockdep. While other callers keep using the iommu_get_domain_for_dev().

Quoted 5 lines
Quoted 2 lines

Honestly I would prefer to pass the old domain in as a function argument to attach and remove the calls entirely.

hmmm. I may drop the retire group->domain from this series to avoid blocking the iommufd pasid series.

Ok

Quoted 3 lines

yes. Since group->domain is set/clear under group->mutex as well. So I'm wondering if it is ok to add kdoc to iommu_get_domain_for_dev() to note it, and just add the xa_load && entry_decode stuffs without lockdep?

Yes, kdoc is the way to go here

Jason

#29 lore raw

Re: [PATCH 5/5] iommu: Retire group->domain

In reply to Yi Liu

drivers/iommu/iommu.c static void iommu_release_device(struct device *dev); · line 104

Quoted 6 lines · Yi Liu

static int __iommu_attach_device(struct iommu_domain *domain,

drivers/iommu/iommu.c +2 -1
 				 struct device *dev);
 static int __iommu_attach_group(struct iommu_domain *domain,
-				struct iommu_group *group);
+				struct iommu_group *group,
+				struct iommu_attach_handle *handle);

I think I would split this particular transformation and it's related into its own patch, it would shrink this alot. It is principally about sharing the handle and non-handle paths..

Quoted 26 lines
drivers/iommu/iommu.c +6 -3
@@ -563,11 +591,12 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
 	 * iommu_setup_default_domain()
 	 */
 	list_add_tail(&gdev->list, &group->devices);
-	WARN_ON(group->default_domain && !group->domain);
+	gdomain = iommu_group_domain(group);
+	WARN_ON(group->default_domain && !gdomain);
 	if (group->default_domain)
 		iommu_create_device_direct_mappings(group->default_domain, dev);
-	if (group->domain) {
-		ret = __iommu_device_set_domain(group, dev, group->domain, 0);
+	if (gdomain) {
+		ret = __iommu_device_set_domain(group, dev, gdomain, 0);
 		if (ret)
 			goto err_remove_gdev;
 	} else if (!group->default_domain && !group_list) {
@@ -625,6 +654,8 @@ static void __iommu_group_free_device(struct iommu_group *group,
 {
 	struct device *dev = grp_dev->dev;
 
+	lockdep_assert_held(&group->mutex);
+
 	sysfs_remove_link(group->devices_kobj, grp_dev->name);
 	sysfs_remove_link(&dev->kobj, "iommu_group");
 

drivers/iommu/iommu.c static void __iommu_group_free_device(struct iommu_group *group, · line 671

Quoted 5 lines
drivers/iommu/iommu.c +1 -1
@@ -637,7 +668,7 @@ static void __iommu_group_free_device(struct iommu_group *group,
 	 */
 	if (list_empty(&group->devices))
 		WARN_ON(group->owner_cnt ||
-			group->domain != group->default_domain);
+			iommu_group_domain(group) != group->default_domain);

You could also probably put the conversion of locked group->domain into iommu_group_domain() into its own patch (though not using the xarray to start), that conversion looks like 9 hunks.

drivers/iommu/iommu.c static int __iommu_device_set_domain(struct iommu_group *group, · line 2280

Quoted 2 lines
drivers/iommu/iommu.c +2 -0
+static void *iommu_make_pasid_entry(struct iommu_domain *domain,
+				    struct iommu_attach_handle *handle);

Should move the helper to the top of the file in the patch that introduces it

Quoted 8 lines
drivers/iommu/iommu.c +2 -0
 static int __iommu_group_set_domain_internal(struct iommu_group *group,
 					     struct iommu_domain *new_domain,
+					     struct iommu_attach_handle *handle,
 					     unsigned int flags)
 {
 	struct group_device *last_gdev;
+	struct iommu_domain *gdomain;
 	struct group_device *gdev;

drivers/iommu/iommu.c static int __iommu_device_set_domain(struct iommu_group *group, · line 2313

Quoted 10 lines
drivers/iommu/iommu.c +3 -1
+	void *pasid_entry;
 	int result;
 	int ret;
 
 	lockdep_assert_held(&group->mutex);
 
-	if (group->domain == new_domain)
+	gdomain = iommu_group_domain(group);
+	if (gdomain == new_domain)
 		return 0;

This is not quite right anymore, we could replace a domain with a handle or viceversa.

Probably like this:

	if (WARN_ON(!new_domain))
		return -EINVAL;

	pasid_entry = iommu_make_pasid_entry(new_domain, handle);
	curr = xa_cmpxchg(&group->pasid_array, IOMMU_NO_PASID, NULL,
			  XA_ZERO_ENTRY, GFP_KERNEL);
	if (xa_is_err(curr))
		return xa_err(curr);
	if (curr == pasid_entry)
		return 0;
	gdomain = decode_pasid_entry(curr);

drivers/iommu/iommu.c static int __iommu_group_set_domain_internal(struct iommu_group *group, · line 2349

Quoted 9 lines
drivers/iommu/iommu.c +4 -1
@@ -2290,7 +2343,10 @@ static int __iommu_group_set_domain_internal(struct iommu_group *group,
 			goto err_revert;
 		}
 	}
-	group->domain = new_domain;
+
+	pasid_entry = iommu_make_pasid_entry(new_domain, handle);
+	WARN_ON(xa_is_err(xa_store(&group->pasid_array,
+				   IOMMU_NO_PASID, pasid_entry, GFP_KERNEL)));

Ah, this flow uses the same as I suggested a few messages ago already!

Quoted 8 lines
drivers/iommu/iommu.c +1 -1
@@ -2302,16 +2358,17 @@ static int __iommu_group_set_domain_internal(struct iommu_group *group,
 	for_each_group_device(group, gdev) {
 		/*
 		 * A NULL domain can happen only for first probe, in which case
-		 * we leave group->domain as NULL and let release clean
+		 * we leave domain in pasid_array as NULL and let release clean
 		 * everything up.
 		 */

drivers/iommu/iommu.c static int __iommu_group_set_domain_internal(struct iommu_group *group, · line 2371

Quoted 10 lines
drivers/iommu/iommu.c +3 -2
-		if (group->domain)
+		if (gdomain)
 			WARN_ON(__iommu_device_set_domain(
-				group, gdev->dev, group->domain,
+				group, gdev->dev, gdomain,
 				IOMMU_SET_DOMAIN_MUST_SUCCEED));
 		if (gdev == last_gdev)
 			break;
 	}
+	xa_release(&group->pasid_array, IOMMU_NO_PASID);

This does not seem right, the prior flow left group->domain unchanged here. IMHO just drop it, the xarray is always populated and almost never has NULL.

Jason

author
#30 lore raw

Re: [PATCH 5/5] iommu: Retire group->domain

Quoted 11 lines · Jason Gunthorpe

drivers/iommu/iommu.c static void iommu_release_device(struct device *dev); · line 104

Quoted 6 lines · Yi Liu

static int __iommu_attach_device(struct iommu_domain *domain,
struct device *dev);
static int __iommu_attach_group(struct iommu_domain *domain,

drivers/iommu/iommu.c +2 -1
-				struct iommu_group *group);
+				struct iommu_group *group,
+				struct iommu_attach_handle *handle);

I think I would split this particular transformation and it's related into its own patch, it would shrink this alot. It is principally about sharing the handle and non-handle paths..

got it.

Quoted 26 lines
Quoted 26 lines
drivers/iommu/iommu.c +6 -3
@@ -563,11 +591,12 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
  	 * iommu_setup_default_domain()
  	 */
  	list_add_tail(&gdev->list, &group->devices);
-	WARN_ON(group->default_domain && !group->domain);
+	gdomain = iommu_group_domain(group);
+	WARN_ON(group->default_domain && !gdomain);
  	if (group->default_domain)
  		iommu_create_device_direct_mappings(group->default_domain, dev);
-	if (group->domain) {
-		ret = __iommu_device_set_domain(group, dev, group->domain, 0);
+	if (gdomain) {
+		ret = __iommu_device_set_domain(group, dev, gdomain, 0);
  		if (ret)
  			goto err_remove_gdev;
  	} else if (!group->default_domain && !group_list) {
@@ -625,6 +654,8 @@ static void __iommu_group_free_device(struct iommu_group *group,
  {
  	struct device *dev = grp_dev->dev;
 
+	lockdep_assert_held(&group->mutex);
+
  	sysfs_remove_link(group->devices_kobj, grp_dev->name);
  	sysfs_remove_link(&dev->kobj, "iommu_group");
 
Quoted 10 lines

drivers/iommu/iommu.c static void __iommu_group_free_device(struct iommu_group *group, · line 671

Quoted 6 lines
drivers/iommu/iommu.c +1 -1
@@ -637,7 +668,7 @@ static void __iommu_group_free_device(struct iommu_group *group,
  	 */
  	if (list_empty(&group->devices))
  		WARN_ON(group->owner_cnt ||
-			group->domain != group->default_domain);
+			iommu_group_domain(group) != group->default_domain);

You could also probably put the conversion of locked group->domain into iommu_group_domain() into its own patch (though not using the xarray to start), that conversion looks like 9 hunks.

got it.

Quoted 20 lines

drivers/iommu/iommu.c static int __iommu_device_set_domain(struct iommu_group *group, · line 2280

Quoted 2 lines
drivers/iommu/iommu.c +2 -0
+static void *iommu_make_pasid_entry(struct iommu_domain *domain,
+				    struct iommu_attach_handle *handle);

Should move the helper to the top of the file in the patch that introduces it

Quoted 14 lines
drivers/iommu/iommu.c +3 -0
  static int __iommu_group_set_domain_internal(struct iommu_group *group,
  					     struct iommu_domain *new_domain,
+					     struct iommu_attach_handle *handle,
  					     unsigned int flags)
  {
  	struct group_device *last_gdev;
+	struct iommu_domain *gdomain;
  	struct group_device *gdev;
+	void *pasid_entry;
  	int result;
  	int ret;
 
  	lockdep_assert_held(&group->mutex);
Quoted 7 lines

drivers/iommu/iommu.c static int __iommu_device_set_domain(struct iommu_group *group, · line 2313

Quoted 4 lines
drivers/iommu/iommu.c +2 -1
-	if (group->domain == new_domain)
+	gdomain = iommu_group_domain(group);
+	if (gdomain == new_domain)
  		return 0;

This is not quite right anymore, we could replace a domain with a handle or viceversa.

I see. yeah, even domain is the same, it may be on a replace_handle path which needs to update the pasid_entry for sure.

Quoted 15 lines

Probably like this:

	if (WARN_ON(!new_domain))
		return -EINVAL;

	pasid_entry = iommu_make_pasid_entry(new_domain, handle);
	curr = xa_cmpxchg(&group->pasid_array, IOMMU_NO_PASID, NULL,
			  XA_ZERO_ENTRY, GFP_KERNEL);
	if (xa_is_err(curr))
		return xa_err(curr);
	if (curr == pasid_entry)
		return 0;
	gdomain = decode_pasid_entry(curr);
Quoted 11 lines

drivers/iommu/iommu.c static int __iommu_group_set_domain_internal(struct iommu_group *group, · line 2349

Quoted 9 lines
drivers/iommu/iommu.c +4 -1
@@ -2290,7 +2343,10 @@ static int __iommu_group_set_domain_internal(struct iommu_group *group,
  			goto err_revert;
  		}
  	}
-	group->domain = new_domain;
+
+	pasid_entry = iommu_make_pasid_entry(new_domain, handle);
+	WARN_ON(xa_is_err(xa_store(&group->pasid_array,
+				   IOMMU_NO_PASID, pasid_entry, GFP_KERNEL)));

Ah, this flow uses the same as I suggested a few messages ago already!

yeah, I used xa_reserve() though.

Quoted 14 lines
Quoted 14 lines
drivers/iommu/iommu.c +3 -3
@@ -2302,16 +2358,17 @@ static int __iommu_group_set_domain_internal(struct iommu_group *group,
  	for_each_group_device(group, gdev) {
  		/*
  		 * A NULL domain can happen only for first probe, in which case
-		 * we leave group->domain as NULL and let release clean
+		 * we leave domain in pasid_array as NULL and let release clean
  		 * everything up.
  		 */
-		if (group->domain)
+		if (gdomain)
  			WARN_ON(__iommu_device_set_domain(
-				group, gdev->dev, group->domain,
+				group, gdev->dev, gdomain,
  				IOMMU_SET_DOMAIN_MUST_SUCCEED));
Quoted 8 lines

drivers/iommu/iommu.c static int __iommu_group_set_domain_internal(struct iommu_group *group, · line 2371

Quoted 4 lines
drivers/iommu/iommu.c +1 -0
  		if (gdev == last_gdev)
  			break;
  	}
+	xa_release(&group->pasid_array, IOMMU_NO_PASID);

This does not seem right, the prior flow left group->domain unchanged here. IMHO just drop it, the xarray is always populated and almost never has NULL.

yes.

Signature · 2 lines
Regards,
Yi Liu
#31 lore raw
In reply to Yi Liu
Quoted 2 lines · Yi Liu

This patch series aims to enhance the iommu_attach_handle-related APIs in the IOMMU core. The changes build upon Nic's previous series[1],

So it needs some branching help to go into Joerg's tree ?

Quoted 2 lines

This series also covers the prior series[2] which only takes care of the iommu_attach_device_pasid().

Is this a pre-condition for your iommufd PASID series?

Jason

author
#32 lore raw
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Yi Liu

This patch series aims to enhance the iommu_attach_handle-related APIs in the IOMMU core. The changes build upon Nic's previous series[1],

So it needs some branching help to go into Joerg's tree ?

yes since all the changes are in the core. Current series is based on your for-next.

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/log/?h=for-next
dc10ba25d43f433ad5d9e8e6be4f4d2bb3cd9ddb

Quoted 5 lines
Quoted 3 lines

This series also covers the prior series[2] which only takes care of the iommu_attach_device_pasid().

Is this a pre-condition for your iommufd PASID series?

yes. The last patch is nice to have. I included it as it is just one minor step.

Signature · 2 lines
Regards,
Yi Liu
#33 lore raw
Quoted 5 lines · Yi Liu
Quoted 4 lines · Jason Gunthorpe
Quoted 2 lines · Yi Liu

This patch series aims to enhance the iommu_attach_handle-related APIs in the IOMMU core. The changes build upon Nic's previous series[1],

Quoted 7 lines
Quoted 1 line

So it needs some branching help to go into Joerg's tree ?

yes since all the changes are in the core. Current series is based on your for-next.

https://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git/log/?h=for-next
dc10ba25d43f433ad5d9e8e6be4f4d2bb3cd9ddb

OK, Joerg I will make a branch for you to pick up

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

This series also covers the prior series[2] which only takes care of the iommu_attach_device_pasid().

Is this a pre-condition for your iommufd PASID series?

yes. The last patch is nice to have. I included it as it is just one minor step.

Ok

Jason