RE: [PATCH] iommu: Fix iommu_sva_bind_device to the same domain

From: Zhang, Tina
Date: Wed Feb 21 2024 - 02:59:17 EST


Hi,

> -----Original Message-----
> From: Zhangfei Gao <zhangfei.gao@xxxxxxxxxx>
> Sent: Wednesday, February 21, 2024 2:19 PM
> To: Joerg Roedel <joro@xxxxxxxxxx>; Will Deacon <will@xxxxxxxxxx>; jean-
> philippe <jean-philippe@xxxxxxxxxx>; Jason Gunthorpe <jgg@xxxxxxxxxx>;
> baolu.lu@xxxxxxxxxxxxxxx; Zhang, Tina <tina.zhang@xxxxxxxxx>
> Cc: iommu@xxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx; Zhangfei Gao
> <zhangfei.gao@xxxxxxxxxx>
> Subject: [PATCH] iommu: Fix iommu_sva_bind_device to the same domain
>
> The accelerator dev can provide multi-queue and bind to the same domain in
> multi-thread for better performance, and domain refcount takes care of it.
>
> 'commit 092edaddb660 ("iommu: Support mm PASID 1:n with sva domains")'
> removes the possibility, so fix it
>
> Fixs: '092edaddb660 ("iommu: Support mm PASID 1:n with sva domains")'
> Signed-off-by: Zhangfei Gao <zhangfei.gao@xxxxxxxxxx>
> ---
> drivers/iommu/iommu-sva.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index
> c3fc9201d0be..a95c8f3a5407 100644
> --- a/drivers/iommu/iommu-sva.c
> +++ b/drivers/iommu/iommu-sva.c
> @@ -91,7 +91,7 @@ struct iommu_sva *iommu_sva_bind_device(struct
> device *dev, struct mm_struct *mm
> /* Search for an existing domain. */
> list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next)
> {
> ret = iommu_attach_device_pasid(domain, dev, iommu_mm-
> >pasid);
> - if (!ret) {
> + if (!ret || ret == -EBUSY) {
If rebinding is allowed, how could IOMMU core know if this invocation is intended (like the multi-thread case mentioned in the commit message) or is mistakenly invoked?

Thanks,
-Tina

> domain->users++;
> goto out;
> }
> @@ -141,8 +141,8 @@ 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, iommu_mm->pasid);
> if (--domain->users == 0) {
> + iommu_detach_device_pasid(domain, dev, iommu_mm-
> >pasid);
> list_del(&domain->next);
> iommu_domain_free(domain);
> }
> --
> 2.34.1