Re: [PATCH v2 4/5] iommu: Support mm PASID 1:n with sva domains

From: Vasant Hegde
Date: Thu Aug 31 2023 - 02:33:04 EST


Hi Tina,

On 8/28/2023 2:40 PM, Zhang, Tina wrote:
> Hi Vasant,
>
>> -----Original Message-----
>> From: Vasant Hegde <vasant.hegde@xxxxxxx>
>> Sent: Monday, August 28, 2023 4:33 PM
>> To: Zhang, Tina <tina.zhang@xxxxxxxxx>; Jason Gunthorpe <jgg@xxxxxxxx>;
>> Tian, Kevin <kevin.tian@xxxxxxxxx>; Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx>;
>> Michael Shavit <mshavit@xxxxxxxxxx>
>> Cc: iommu@xxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx
>> Subject: Re: [PATCH v2 4/5] iommu: Support mm PASID 1:n with sva domains
>>
>> Hi Tina,
>>
>> On 8/27/2023 2:14 PM, Tina Zhang wrote:
>>> Each mm bound to devices gets a PASID and corresponding sva domains
>>> allocated in iommu_sva_bind_device(), which are referenced by
>> iommu_mm
>>> field of the mm. The PASID is released in __mmdrop(), while a sva
>>> domain is released when no one is using it (the reference count is
>>> decremented in iommu_sva_unbind_device()).
>>>
>>> Since the required info of PASID and sva domains is kept in struct
>>> iommu_mm_data of a mm, use mm->iommu_mm field instead of the old
>> pasid
>>> field in mm struct. The sva domain list is protected by iommu_sva_lock.
>>>
>>> Besides, this patch removes mm_pasid_init(), as with the introduced
>>> iommu_mm structure, initializing mm pasid in mm_init() is unnecessary.
>>>
>>> Signed-off-by: Tina Zhang <tina.zhang@xxxxxxxxx>
>>> ---
>>> drivers/iommu/iommu-sva.c | 38 +++++++++++++++++++++++++-------------
>>> include/linux/iommu.h | 10 +++-------
>>> kernel/fork.c | 1 -
>>> 3 files changed, 28 insertions(+), 21 deletions(-)
>>>
>>
>>
>> .../...
>>
>>>
>>> /* Allocate a new domain and set it on device pasid. */ @@ -105,6
>>> +113,8 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev,
>> struct mm_struct *mm
>>> if (ret)
>>> goto out_free_domain;
>>> domain->users = 1;
>>> + list_add(&domain->next, &mm->iommu_mm->sva_domains);
>>> +
>>> out:
>>> mutex_unlock(&iommu_sva_lock);
>>> handle->dev = dev;
>>> @@ -137,8 +147,9 @@ void iommu_sva_unbind_device(struct iommu_sva
>> *handle)
>>> struct device *dev = handle->dev;
>>>
>>> mutex_lock(&iommu_sva_lock);
>>> + iommu_detach_device_pasid(domain, dev, pasid);
>>> if (--domain->users == 0) {
>>> - iommu_detach_device_pasid(domain, dev, pasid);
>>> + list_del(&domain->next);
>>> iommu_domain_free(domain);
>>> }
>>> mutex_unlock(&iommu_sva_lock);
>>> @@ -218,4 +229,5 @@ void mm_pasid_drop(struct mm_struct *mm)
>>> return;
>>>
>>> ida_free(&iommu_global_pasid_ida, mm_get_pasid(mm));
>>> + kfree(mm->iommu_mm);
>>
>>
>> I am not sure whether I understood the flow completely. Just wondering why
>> you are not freeing pasid in iommu_sva_unbind_device().
>> I mean once iommu_mm->sva_domains becomes free shouldn't we free the
>> iommu_mm->pasid?
> No, the sva domain and the PASID are separate objects with their own lifecycles.
> The iommu_mm->pasid is released when the mm is being released, meanwhile the sva_domain is released when no one is using it.

Thanks for the explanation.

-Vasant