Re: [PATCH v10 04/12] iommu: Add attach/detach_dev_pasid iommu interface

From: Baolu Lu
Date: Sun Jul 24 2022 - 03:23:30 EST


On 2022/7/23 22:11, Jason Gunthorpe wrote:
+
+ /*
+ * Otherwise, the device came from DT/ACPI, assume it is static and
+ * then singleton can know from the device count in the group.
+ */
+ return true;
+}
I would be happer if probe was changed to refuse to add a device to a
group if the group's pasid xarray is not empty, as a protective
measure.

Agreed. I will add below code.

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 047898666b9f..e43cb6776087 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -895,6 +895,14 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
int ret, i = 0;
struct group_device *device;

+ /*
+ * The iommu_attach_device_pasid() requires a singleton group.
+ * Refuse to add a device into it if this assumption has been
+ * made.
+ */
+ if (!xa_empty(group->pasid_array))
+ return -EBUSY;
+
device = kzalloc(sizeof(*device), GFP_KERNEL);
if (!device)
return -ENOMEM;


+int iommu_attach_device_pasid(struct iommu_domain *domain, struct device *dev,
+ ioasid_t pasid)
+{
+ struct iommu_group *group;
+ void *curr;
+ int ret;
+
+ if (!domain->ops->set_dev_pasid)
+ return -EOPNOTSUPP;
+
+ group = iommu_group_get(dev);
+ if (!group || !iommu_group_immutable_singleton(group, dev)) {
+ iommu_group_put(group);
+ return -EINVAL;
goto error below

+ }
+
+ mutex_lock(&group->mutex);
Just hold the group->mutex a few lines above and don't put locking in
iommu_group_immutable_singleton(), it is clearer

Above two comments agreed. iommu_attach_device_pasid() looks like below
after update.

int iommu_attach_device_pasid(struct iommu_domain *domain, struct device *dev,
ioasid_t pasid)
{
struct iommu_group *group;
int ret = -EINVAL;
void *curr;

if (!domain->ops->set_dev_pasid)
return -EOPNOTSUPP;

group = iommu_group_get(dev);
if (!group)
return -ENODEV;

mutex_lock(&group->mutex);
if (!iommu_group_immutable_singleton(group, dev))
goto out_unlock;

curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
if (curr) {
ret = xa_err(curr) ? : -EBUSY;
goto out_unlock;
}
ret = domain->ops->set_dev_pasid(domain, dev, pasid);
if (ret)
xa_erase(&group->pasid_array, pasid);
out_unlock:
mutex_unlock(&group->mutex);
iommu_group_put(group);

return ret;
}

Best regards,
baolu