Re: [PATCH v8 1/4] iommu/dma: Keep reserved regions below the IOVA rbtree anchor

From: Will Deacon

Date: Fri Dec 04 2026 - 09:14:13 EST


On Fri, Nov 20, 2026 at 04:05:13PM +0530, Vikash Garodia wrote:
> The IOVA allocator tracks address ranges by page frame number (PFN).
> The highest node in its rbtree is a fixed anchor at PFN ~0UL, which is
> embedded in the DMA cookie. dma-iommu converts reserved regions to PFNs
> with iova_pfn(), which returns unsigned long. On x86-32 with PAE,
> addresses are 64-bit but unsigned long is 32-bit, so the upper bits of
> the PFN are lost.
> A reserved region that ends at the top of the address space then gets
> the anchor's PFN. reserve_iova() merges it into the anchor and lowers
> the anchor's pfn_lo. At teardown, free_iova_mem() no longer recognises
> the anchor and frees it to iova_cache, which corrupts the slab. PCI host
> bridge windows above 16TiB and the reservation after the last PCI DMA
> window hit the same problem.
> Compute the PFNs in u64. Skip ranges that start above the PFN space and
> clamp the end one PFN below the anchor. The allocator never hands out
> those IOVAs, so nothing is lost. On 64-bit, PFNs never reach ~0UL, so
> nothing changes there.

Do you have a crash log you could include here?

There is some driver code doing similar things, does that need fixing
too? (see e.g. ipu7_get_fw_code_region()).

> Fixes: fade1ec055dc ("iommu/dma: Avoid PCI host bridge windows")
> Fixes: 7c1b058c8b5a ("iommu/dma: Handle IOMMU API reserved regions")
> Fixes: aadad097cd46 ("iommu/dma: Reserve IOVA for PCIe inaccessible DMA address")
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Closes: https://lore.kernel.org/all/sashiko-outbox-195658@xxxxxxxxxx/
> Cc: stable@xxxxxxxxxxxxxxx
> Reviewed-by: Krzysztof Kozlowski <krzk@xxxxxxxxxx>
> Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@xxxxxxxxxxxxxxxx>
> Co-developed-by: Vishnu Reddy <busanna.reddy@xxxxxxxxxxxxxxxx>
> Signed-off-by: Vishnu Reddy <busanna.reddy@xxxxxxxxxxxxxxxx>
> Signed-off-by: Vikash Garodia <vikash.garodia@xxxxxxxxxxxxxxxx>
> ---
> drivers/iommu/dma-iommu.c | 32 ++++++++++++++++++++------------
> 1 file changed, 20 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
> index b66e83cfbc5f195a46d286d376068fcc13148611..2ee30df470730fd03b5dc46b46e3eaba2df130d6 100644
> --- a/drivers/iommu/dma-iommu.c
> +++ b/drivers/iommu/dma-iommu.c
> @@ -505,21 +505,34 @@ static int iommu_dma_ranges_sort(void *priv, const struct list_head *a,
> return res_a->res->start > res_b->res->start;
> }
>
> +/*
> + * IOVA PFNs are unsigned long, and ~0UL is the rbtree anchor. On 32-bit
> + * with a 64-bit dma_addr_t, iova_pfn() would truncate the range. Compute
> + * the PFNs in u64 instead, skip a range that starts beyond the PFN space
> + * and clamp its end below the anchor. Such IOVAs cannot be allocated.
> + */
> +static void iova_reserve_range(struct iova_domain *iovad, u64 start, u64 end)
> +{
> + u64 lo = start >> iova_shift(iovad);
> + u64 hi = end >> iova_shift(iovad);
> +
> + if (lo < ULONG_MAX)
> + reserve_iova(iovad, lo, min(hi, ULONG_MAX - 1));
> +}

I appreciate that you're trying to fix an existing issue here, but the
terminology used in the IOVA code is really confusing to me. iova_pfn()
shifts a virtual address (the "va" in "iova" to get a pfn, which is
completely different to how we think about this on the CPU side where
the pfn is the shifted _physical_ address (e.g. compare with virt_to_pfn()).
It would be good to remove iova_pfn() altogether, if we can.

Is there a good reason why IOVAs aren't dma_addr_t instead of unsigned
long? I suppose it doesn't help that iommu_map() takes an unsigned long,
so poor old dma-iommu.c gets caught in the middle a bit here.

Anyway, I think what you have here is fine, I just think that we could
probably do more.

Will