Re: [PATCH v2 07/11] media: intel/ipu6: fix IOVA leak on first entry failure in ipu6_dma_map_sg()

From: Valerii Sokolov

Date: Tue Nov 17 2026 - 19:44:55 EST


Hi Sakari,

On Wed, Nov 04, 2026 at 03:02:56PM +0200, Sakari Ailus wrote:
> On Tue, Nov 03, 2026 at 09:11:03PM +0000, Valerii Sokolov wrote:
> > diff --git a/drivers/media/pci/intel/ipu6/ipu6-dma.c b/drivers/media/pci/intel/ipu6/ipu6-dma.c
> > index 90f530a937..a923a1e4bd 100644
> > --- a/drivers/media/pci/intel/ipu6/ipu6-dma.c
> > +++ b/drivers/media/pci/intel/ipu6/ipu6-dma.c
> > @@ -444,7 +444,10 @@ int ipu6_dma_map_sg(struct ipu6_bus_device *sys, struct scatterlist *sglist,
> > return nents;
> >
> > out_fail:
> > - ipu6_dma_unmap_sg(sys, sglist, i, dir, attrs);
> > + if (i)
> > + ipu6_dma_unmap_sg(sys, sglist, i, dir, attrs);
> > + else
> > + __free_iova(&mmu->dmap->iovad, iova);
>
> This should be handled in ipu6_dma_unmap_sg(). I'd release the iova in the
> beginning and reverse the order of the nents / iova checks.
>
> I'll merge the rest now.

Sorry for the delayed reply, and thank you for merging the other
patches!

Regarding handling nents == 0 inside ipu6_dma_unmap_sg():
ipu6_dma_unmap_sg() looks up the IOVA via:

struct iova *iova = find_iova(&mmu->dmap->iovad,
PHYS_PFN(sg_dma_address(sglist)));

However, in ipu6_dma_map_sg(), sg_dma_address(sg) is only overwritten
with PFN_PHYS(iova_addr) after ipu6_mmu_map() succeeds:

ret = ipu6_mmu_map(mmu->dmap->mmu_info, PFN_PHYS(iova_addr),
sg_dma_address(sg),
PAGE_ALIGN(sg_dma_len(sg)));
if (ret)
goto out_fail;

sg_dma_address(sg) = PFN_PHYS(iova_addr);

So when ipu6_mmu_map() fails on the first entry (i == 0),
sg_dma_address(sglist) still holds the host/PCI physical DMA address
rather than PFN_PHYS(iova->pfn_lo). Calling find_iova() on that address
in ipu6_dma_unmap_sg() will either return NULL (hitting WARN_ON(!iova)
and still leaking the allocated IOVA) or match an unrelated IOVA if the
physical PFN happens to fall into another active IOVA range.

Because ipu6_dma_unmap_sg() does not take struct iova * directly, only
ipu6_dma_map_sg() knows the iova pointer when 0 entries were mapped.

Best regards,
Valerii