Re: [PATCH RFC 09/11] iommu: Add iommu_get_domain_for_dev_pasid()

Lu Baolu
2022-03-22T04:51:05Z

On 2022/3/21 20:40, Jason Gunthorpe wrote:
> On Sun, Mar 20, 2022 at 02:40:28PM +0800, Lu Baolu wrote:
>> @@ -3098,7 +3101,16 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
>> if (iommu_group_device_count(group) != 1)
>> goto out_unlock;
>>
>> + xa_lock(&group->pasid_array);
>> + curr = __xa_cmpxchg(&group->pasid_array, pasid, NULL,
>> + domain, GFP_KERNEL);
>> + xa_unlock(&group->pasid_array);
>> + if (curr)
>
> curr can be an xa_err that should be propogated.

Yes, should check xa_err().

>
>> + goto out_unlock;
>> +
>> ret = domain->ops->attach_dev_pasid(domain, dev, pasid);
>> + if (ret)
>> + xa_erase(&group->pasid_array, pasid);
>>
>> out_unlock:
>> mutex_unlock(&group->mutex);
>> @@ -3118,6 +3130,25 @@ void iommu_detach_device_pasid(struct iommu_domain *domain,
>>
>> mutex_lock(&group->mutex);
>> domain->ops->detach_dev_pasid(domain, dev, pasid);
>> + xa_erase(&group->pasid_array, pasid);
>> + mutex_unlock(&group->mutex);
>> + iommu_group_put(group);
>> +}
>> +
>> +struct iommu_domain *
>> +iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid)
>> +{
>> + struct iommu_domain *domain;
>> + struct iommu_group *group;
>> +
>> + group = iommu_group_get(dev);
>> + if (!group)
>> + return NULL;
>> +
>> + mutex_lock(&group->mutex);
>> + domain = xa_load(&group->pasid_array, pasid);
>> mutex_unlock(&group->mutex);
>> iommu_group_put(group);
>
> This whole API seems sketchy - what is the lifecycle of the returned
> iommu_domain and what prevents it from being concurrently freed after
> unlocking?

Agreed. The domain could be used in page fault handling thread, hence
need a mechanism to guarantee the concurrence.

Best regards,
baolu