Re: [PATCH V2] iommu: sprd: release dma buffer to avoid memory leak
From: Chunyan Zhang
Date: Thu Mar 09 2023 - 21:11:53 EST
On Thu, 9 Mar 2023 at 19:23, Robin Murphy <robin.murphy@xxxxxxx> wrote:
>
> On 2023-03-09 02:50, Baolu Lu wrote:
> > On 3/8/23 8:37 PM, Robin Murphy wrote:
> >> On 2023-03-08 03:41, Chunyan Zhang wrote:
> >>> Release page table DMA buffer when the IOMMU domain is not used:
> >>>
> >>> - Domain freed.
> >>>
> >>> - IOMMU is attaching to a new domain.
> >>> Since one sprd IOMMU servers only one client device, if the IOMMU has
> >>> been attached to other domain, it has to be detached first, that's
> >>> saying the DMA buffer should be released, otherwise that would
> >>> cause memory leak issue.
> >>
> >> This is clearly wrong; domain resources should only be freed when the
> >> domain is freed.
> >
> > Agreed. Perhaps, in the attach path:
> >
> > if (!dom->pgt_va)
> > dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size,
> > &dom->pgt_pa, GFP_KERNEL);
> > ?
>
> Why? dom->pgt_va will always be NULL if dom->sdev is NULL, and once
Yes, I also found this. I'm going to remove the check of dom->sdev
that will make reattching being allowed, like you mentioned below, in
the next version.
Thanks for your comments,
Chunyan
> dom->sdev is set then that allocation is not reachable (of course this
> means the driver also has a separate bug where reattaching to a
> previously-used domain will erroneously fail, but that's not a memory
> leak as such). The only thing to recognise here is that the allocation
> is logically not part of the .attach_dev operation, but really a one-off
> deferred part of .domain_alloc, and thus it should be balanced in .free,
> not anywhere else.
>
> Thanks,
> Robin.