Add a check to enforce that I/O virtual addresses picked by iommu API
users stay within the domains geometry aperture.

Signed-off-by: Sebastian Ott <seb...@amazon.de>
Cc: Benjamin Serebrin <sereb...@amazon.com>
Cc: Filippo Sironi <sir...@amazon.de>

CR: https://code.amazon.com/reviews/CR-26408388
---
 drivers/iommu/amd/iommu.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index b3f79820fd6d..bfa9c4a1fcf8 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2159,11 +2159,13 @@ static struct iommu_device 
*amd_iommu_probe_device(struct device *dev)
 static void amd_iommu_probe_finalize(struct device *dev)
 {
        struct iommu_domain *domain;
+       u64 base = IOVA_START_PFN << PAGE_SHIFT;
+       u64 size = amd_iommu_max_va - base;
 
        /* Domains are initialized for this device - have a look what we ended 
up with */
        domain = iommu_get_domain_for_dev(dev);
        if (domain->type == IOMMU_DOMAIN_DMA)
-               iommu_setup_dma_ops(dev, IOVA_START_PFN << PAGE_SHIFT, 0);
+               iommu_setup_dma_ops(dev, base, size);
 }
 
 static void amd_iommu_release_device(struct device *dev)
@@ -2500,6 +2502,11 @@ static int amd_iommu_map(struct iommu_domain *dom, 
unsigned long iova,
        if (pgtable.mode == PAGE_MODE_NONE)
                return -EINVAL;
 
+       if (dom->geometry.force_aperture &&
+           (iova < dom->geometry.aperture_start ||
+            iova + page_size - 1 > dom->geometry.aperture_end))
+               return -EINVAL;
+
        if (iommu_prot & IOMMU_READ)
                prot |= IOMMU_PROT_IR;
        if (iommu_prot & IOMMU_WRITE)
-- 
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879



_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

Reply via email to