iommu/dma: Fix domain init
authorRobin Murphy <robin.murphy@arm.com>
Mon, 20 May 2024 19:14:44 +0000 (20:14 +0100)
committerJoerg Roedel <jroedel@suse.de>
Tue, 4 Jun 2024 11:54:09 +0000 (13:54 +0200)
Despite carefully rewording the kerneldoc to describe the new direct
interaction with dma_range_map, it seems I managed to confuse myself in
removing the redundant force_aperture check and ended up making the code
not do that at all. This led to dma_range_maps inadvertently being able
to set iovad->start_pfn = 0, and all the nonsensical chaos which ensues
from there. Restore the correct behaviour of constraining base_pfn to
the domain aperture regardless of dma_range_map, and not trying to apply
dma_range_map constraints to the basic IOVA domain since they will be
properly handled with reserved regions later.

Reported-by: Jon Hunter <jonathanh@nvidia.com>
Reported-by: Jerry Snitselaar <jsnitsel@redhat.com>
Fixes: ad4750b07d34 ("iommu/dma: Make limit checks self-contained")
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Tested-by: Jerry Snitselaar <jsnitsel@redhat.com>
Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com>
Link: https://lore.kernel.org/r/721fa6baebb0924aa40db0b8fb86bcb4538434af.1716232484.git.robin.murphy@arm.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/dma-iommu.c

index f731e4b2a41724e76459c767efe78ab7d1a41e7f..43520e7275cc12818c2a056cc00d0a539792f1da 100644 (file)
@@ -686,15 +686,15 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, struct device *dev
 
        /* Check the domain allows at least some access to the device... */
        if (map) {
-               dma_addr_t base = dma_range_map_min(map);
-               if (base > domain->geometry.aperture_end ||
+               if (dma_range_map_min(map) > domain->geometry.aperture_end ||
                    dma_range_map_max(map) < domain->geometry.aperture_start) {
                        pr_warn("specified DMA range outside IOMMU capability\n");
                        return -EFAULT;
                }
-               /* ...then finally give it a kicking to make sure it fits */
-               base_pfn = max(base, domain->geometry.aperture_start) >> order;
        }
+       /* ...then finally give it a kicking to make sure it fits */
+       base_pfn = max_t(unsigned long, base_pfn,
+                        domain->geometry.aperture_start >> order);
 
        /* start_pfn is always nonzero for an already-initialised domain */
        mutex_lock(&cookie->mutex);