Skip to contentSkip to conversation

iommu: Allow attaching static domains in iommu_attach_device_pasid()

PATCH Superseded Lu Baolu 4 participants 5 messages Apr 23, 2025 – Apr 24, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lu Baolu

author
#0 lore raw

PATCH v2 1/1 iommu: Allow attaching static domains in iommu_attach_device_pasid()

The idxd driver attaches the default domain to a PASID of the device to perform kernel DMA using that PASID. The domain is attached to the device's PASID through iommu_attach_device_pasid(), which checks if the domain->owner matches the iommu_ops retrieved from the device. If they do not match, it returns a failure.

        if (ops != domain->owner || pasid == IOMMU_NO_PASID)
                return -EINVAL;

The static identity domain implemented by the intel iommu driver doesn't specify the domain owner. Therefore, kernel DMA with PASID doesn't work for the idxd driver if the device translation mode is set to passthrough.

Generally the owner field of static domains are not set because they are already part of iommu ops. Add a helper domain_iommu_ops_compatible() that checks if a domain is compatible with the device's iommu ops. This helper explicitly allows the static blocked and identity domains associated with the device's iommu_ops to be considered compatible.

---
 drivers/iommu/iommu.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Change log:
-v2:
- Make the solution generic for all static domains as suggested by
Jason.
-v1: https://lore.kernel.org/linux-iommu/[email protected]/

drivers/iommu/iommu.c +15 -1
index 4f91a740c15f..abda40ec377a 100644
@@ -3402,6 +3402,19 @@ static void __iommu_remove_group_pasid(struct iommu_group *group,
 		iommu_remove_dev_pasid(device->dev, pasid, domain);
 }
 
+static bool domain_iommu_ops_compatible(const struct iommu_ops *ops,
+					struct iommu_domain *domain)
+{
+	if (domain->owner == ops)
+		return true;
+
+	/* For static domains, owner isn't set. */
+	if (domain == ops->blocked_domain || domain == ops->identity_domain)
+		return true;
+
+	return false;
+}
+
 /*
  * iommu_attach_device_pasid() - Attach a domain to pasid of device
  * @domain: the iommu domain.
@@ -3435,7 +3448,8 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 	    !ops->blocked_domain->ops->set_dev_pasid)
 		return -EOPNOTSUPP;
 
-	if (ops != domain->owner || pasid == IOMMU_NO_PASID)
+	if (!domain_iommu_ops_compatible(ops, domain) ||
+	    pasid == IOMMU_NO_PASID)
 		return -EINVAL;
 
 	mutex_lock(&group->mutex);
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 19 lines · Lu Baolu

The idxd driver attaches the default domain to a PASID of the device to perform kernel DMA using that PASID. The domain is attached to the device's PASID through iommu_attach_device_pasid(), which checks if the domain->owner matches the iommu_ops retrieved from the device. If they do not match, it returns a failure.

        if (ops != domain->owner || pasid == IOMMU_NO_PASID)
                return -EINVAL;

The static identity domain implemented by the intel iommu driver doesn't specify the domain owner. Therefore, kernel DMA with PASID doesn't work for the idxd driver if the device translation mode is set to passthrough.

Generally the owner field of static domains are not set because they are already part of iommu ops. Add a helper domain_iommu_ops_compatible() that checks if a domain is compatible with the device's iommu ops. This helper explicitly allows the static blocked and identity domains associated with the device's iommu_ops to be considered compatible.

Quoted 6 lines
Quoted 44 lines
---
 drivers/iommu/iommu.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Change log:

(quoted diff) +0 -2
-v2:
 - Make the solution generic for all static domains as suggested by
   Jason.
-v1: https://lore.kernel.org/linux-iommu/[email protected]/
drivers/iommu/iommu.c +15 -1
index 4f91a740c15f..abda40ec377a 100644
@@ -3402,6 +3402,19 @@ static void __iommu_remove_group_pasid(struct iommu_group *group,
 		iommu_remove_dev_pasid(device->dev, pasid, domain);
 }
 
+static bool domain_iommu_ops_compatible(const struct iommu_ops *ops,
+					struct iommu_domain *domain)
+{
+	if (domain->owner == ops)
+		return true;
+
+	/* For static domains, owner isn't set. */
+	if (domain == ops->blocked_domain || domain == ops->identity_domain)
+		return true;
+
+	return false;
+}
+
 /*
  * iommu_attach_device_pasid() - Attach a domain to pasid of device
  * @domain: the iommu domain.
@@ -3435,7 +3448,8 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 	    !ops->blocked_domain->ops->set_dev_pasid)
 		return -EOPNOTSUPP;
 
-	if (ops != domain->owner || pasid == IOMMU_NO_PASID)
+	if (!domain_iommu_ops_compatible(ops, domain) ||
+	    pasid == IOMMU_NO_PASID)
 		return -EINVAL;
 
 	mutex_lock(&group->mutex);
#2 lore raw
In reply to Lu Baolu

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

Quoted 8 lines · Lu Baolu
drivers/iommu/iommu.c +2 -1
@@ -3435,7 +3448,8 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
 	    !ops->blocked_domain->ops->set_dev_pasid)
 		return -EOPNOTSUPP;
 
-	if (ops != domain->owner || pasid == IOMMU_NO_PASID)
+	if (!domain_iommu_ops_compatible(ops, domain) ||
+	    pasid == IOMMU_NO_PASID)
 		return -EINVAL;

Convert all the places checking domain->owner to the new function..

static int __iommu_attach_group(struct iommu_domain *domain,
struct iommu_group *group)

int iommu_replace_device_pasid(struct iommu_domain *domain,
struct device *dev, ioasid_t pasid, struct iommu_attach_handle *handle)

Jason

author
#3 lore raw
Quoted 11 lines · Jason Gunthorpe

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

Quoted 8 lines · Lu Baolu
drivers/iommu/iommu.c +2 -1
@@ -3435,7 +3448,8 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
  	    !ops->blocked_domain->ops->set_dev_pasid)
  		return -EOPNOTSUPP;
 
-	if (ops != domain->owner || pasid == IOMMU_NO_PASID)
+	if (!domain_iommu_ops_compatible(ops, domain) ||
+	    pasid == IOMMU_NO_PASID)
  		return -EINVAL;

Convert all the places checking domain->owner to the new function..

Quoted 6 lines

static int __iommu_attach_group(struct iommu_domain *domain,
struct iommu_group *group)

int iommu_replace_device_pasid(struct iommu_domain *domain,
struct device *dev, ioasid_t pasid, struct iommu_attach_handle *handle)

Sure. Will make it in a new version.

Thanks,
baolu

reviewer Reviewed-by on v2 1/1
#4 lore raw
In reply to Lu Baolu
Quoted 13 lines · Lu Baolu

The idxd driver attaches the default domain to a PASID of the device to perform kernel DMA using that PASID. The domain is attached to the device's PASID through iommu_attach_device_pasid(), which checks if the domain->owner matches the iommu_ops retrieved from the device. If they do not match, it returns a failure.

         if (ops != domain->owner || pasid == IOMMU_NO_PASID)
                 return -EINVAL;

The static identity domain implemented by the intel iommu driver doesn't specify the domain owner. Therefore, kernel DMA with PASID doesn't work for the idxd driver if the device translation mode is set to passthrough.

Quoted 5 lines

Generally the owner field of static domains are not set because they are already part of iommu ops. Add a helper domain_iommu_ops_compatible() that checks if a domain is compatible with the device's iommu ops. This helper explicitly allows the static blocked and identity domains associated with the device's iommu_ops to be considered compatible.

With the other domain->owner checks also wrapped as Jason pointed out (since it would be weird but not impossible for static domains to get into those paths as well),

Quoted 50 lines
---
  drivers/iommu/iommu.c | 16 +++++++++++++++-
  1 file changed, 15 insertions(+), 1 deletion(-)

Change log:

(quoted diff) +0 -2
-v2:
  - Make the solution generic for all static domains as suggested by
    Jason.
-v1: https://lore.kernel.org/linux-iommu/[email protected]/
drivers/iommu/iommu.c +15 -1
index 4f91a740c15f..abda40ec377a 100644
@@ -3402,6 +3402,19 @@ static void __iommu_remove_group_pasid(struct iommu_group *group,
  		iommu_remove_dev_pasid(device->dev, pasid, domain);
  }
 
+static bool domain_iommu_ops_compatible(const struct iommu_ops *ops,
+					struct iommu_domain *domain)
+{
+	if (domain->owner == ops)
+		return true;
+
+	/* For static domains, owner isn't set. */
+	if (domain == ops->blocked_domain || domain == ops->identity_domain)
+		return true;
+
+	return false;
+}
+
  /*
   * iommu_attach_device_pasid() - Attach a domain to pasid of device
   * @domain: the iommu domain.
@@ -3435,7 +3448,8 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
  	    !ops->blocked_domain->ops->set_dev_pasid)
  		return -EOPNOTSUPP;
 
-	if (ops != domain->owner || pasid == IOMMU_NO_PASID)
+	if (!domain_iommu_ops_compatible(ops, domain) ||
+	    pasid == IOMMU_NO_PASID)
  		return -EINVAL;
 
  	mutex_lock(&group->mutex);