Skip to contentSkip to conversation

iommu/amd: Convert the AMD iommu driver to the dma-iommu api

PATCH Superseded Tom Murphy 3 participants 13 messages Apr 30, 2019 – May 7, 2019

Subsystems (13)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tom Murphy

author
#0 lore raw

v2 cover iommu/amd: Convert the AMD iommu driver to the dma-iommu api

Convert the AMD iommu driver to the dma-iommu api. Remove the iova handling and reserve region code from the AMD iommu driver.

Change-log:
v2:
-Rebase on top of this series:
http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/dma-iommu-ops.3
-Add a gfp_t parameter to the iommu_ops::map function. -Made use of the reserve region code inside the dma-iommu api

Tom Murphy (4):
iommu: Add gfp parameter to iommu_ops::map iommu/dma-iommu: Handle deferred devices iommu/dma-iommu: Use the dev->coherent_dma_mask iommu/amd: Convert the AMD iommu driver to the dma-iommu api

 drivers/iommu/Kconfig          |   1 +
 drivers/iommu/amd_iommu.c      | 694 ++++-----------------------------
 drivers/iommu/arm-smmu-v3.c    |   2 +-
 drivers/iommu/arm-smmu.c       |   2 +-
 drivers/iommu/dma-iommu.c      |  52 ++-
 drivers/iommu/exynos-iommu.c   |   2 +-
 drivers/iommu/intel-iommu.c    |   2 +-
 drivers/iommu/iommu.c          |  43 +-
 drivers/iommu/ipmmu-vmsa.c     |   2 +-
 drivers/iommu/msm_iommu.c      |   2 +-
 drivers/iommu/mtk_iommu.c      |   2 +-
 drivers/iommu/mtk_iommu_v1.c   |   2 +-
 drivers/iommu/omap-iommu.c     |   2 +-
 drivers/iommu/qcom_iommu.c     |   2 +-
 drivers/iommu/rockchip-iommu.c |   2 +-
 drivers/iommu/s390-iommu.c     |   2 +-
 drivers/iommu/tegra-gart.c     |   2 +-
 drivers/iommu/tegra-smmu.c     |   2 +-
 include/linux/iommu.h          |  21 +-
 19 files changed, 187 insertions(+), 652 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/4 iommu: Add gfp parameter to iommu_ops::map

Add a gfp_t parameter to the iommu_ops::map function. Remove the needless locking in the AMD iommu driver.

The iommu_ops::map function (or the iommu_map function which calls it) was always supposed to be sleepable (according to Joerg's comment in this thread: https://lore.kernel.org/patchwork/patch/977520/ ) and so should probably have had a "might_sleep()" since it was written. However currently the dma-iommu api can call iommu_map in an atomic context, which it shouldn't do. This doesn't cause any problems because any iommu driver which uses the dma-iommu api uses gfp_atomic in it's iommu_ops::map function. But doing this wastes the memory allocators atomic pools.

We can remove the mutex lock from amd_iommu_map and amd_iommu_unmap. iommu_map doesn’t lock while mapping and so no two calls should touch the same iova range. The AMD driver already handles the page table page allocations without locks so we can safely remove the locks.

---
 drivers/iommu/amd_iommu.c      | 14 ++++-------
 drivers/iommu/arm-smmu-v3.c    |  2 +-
 drivers/iommu/arm-smmu.c       |  2 +-
 drivers/iommu/dma-iommu.c      |  6 ++---
 drivers/iommu/exynos-iommu.c   |  2 +-
 drivers/iommu/intel-iommu.c    |  2 +-
 drivers/iommu/iommu.c          | 43 +++++++++++++++++++++++++++++-----
 drivers/iommu/ipmmu-vmsa.c     |  2 +-
 drivers/iommu/msm_iommu.c      |  2 +-
 drivers/iommu/mtk_iommu.c      |  2 +-
 drivers/iommu/mtk_iommu_v1.c   |  2 +-
 drivers/iommu/omap-iommu.c     |  2 +-
 drivers/iommu/qcom_iommu.c     |  2 +-
 drivers/iommu/rockchip-iommu.c |  2 +-
 drivers/iommu/s390-iommu.c     |  2 +-
 drivers/iommu/tegra-gart.c     |  2 +-
 drivers/iommu/tegra-smmu.c     |  2 +-
 include/linux/iommu.h          | 21 ++++++++++++++++-
 18 files changed, 78 insertions(+), 34 deletions(-)
drivers/iommu/amd_iommu.c +4 -10
index ebd062522cf5..ea3a5dc61bb0 100644
@@ -3092,7 +3092,8 @@ static int amd_iommu_attach_device(struct iommu_domain *dom,
 }
 
 static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
-			 phys_addr_t paddr, size_t page_size, int iommu_prot)
+			 phys_addr_t paddr, size_t page_size, int iommu_prot,
+			 gfp_t gfp)
 {
 	struct protection_domain *domain = to_pdomain(dom);
 	int prot = 0;
@@ -3106,9 +3107,7 @@ static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
 	if (iommu_prot & IOMMU_WRITE)
 		prot |= IOMMU_PROT_IW;
 
-	mutex_lock(&domain->api_lock);
-	ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
-	mutex_unlock(&domain->api_lock);
+	ret = iommu_map_page(domain, iova, paddr, page_size, prot, gfp);
 
 	domain_flush_np_cache(domain, iova, page_size);
 
@@ -3119,16 +3118,11 @@ static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
 			   size_t page_size)
 {
 	struct protection_domain *domain = to_pdomain(dom);
-	size_t unmap_size;
 
 	if (domain->mode == PAGE_MODE_NONE)
 		return 0;
 
-	mutex_lock(&domain->api_lock);
-	unmap_size = iommu_unmap_page(domain, iova, page_size);
-	mutex_unlock(&domain->api_lock);
-
-	return unmap_size;
+	return iommu_unmap_page(domain, iova, page_size);
 }
 
 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
drivers/iommu/arm-smmu-v3.c +1 -1
index d3880010c6cf..e5c48089b49f 100644
@@ -1777,7 +1777,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 }
 
 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
-			phys_addr_t paddr, size_t size, int prot)
+			phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
 
drivers/iommu/arm-smmu.c +1 -1
index 045d93884164..2d50db55b788 100644
@@ -1286,7 +1286,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 }
 
 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
-			phys_addr_t paddr, size_t size, int prot)
+			phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
 	struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
drivers/iommu/dma-iommu.c +3 -3
index fa5713a4f6f8..7a96c2c8f56b 100644
@@ -440,7 +440,7 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
 	if (!iova)
 		return DMA_MAPPING_ERROR;
 
-	if (iommu_map(domain, iova, phys - iova_off, size, prot)) {
+	if (iommu_map_atomic(domain, iova, phys - iova_off, size, prot)) {
 		iommu_dma_free_iova(cookie, iova, size);
 		return DMA_MAPPING_ERROR;
 	}
@@ -641,7 +641,7 @@ static void *iommu_dma_alloc_remap(struct device *dev, size_t size,
 			arch_dma_prep_coherent(sg_page(sg), sg->length);
 	}
 
-	if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, ioprot)
+	if (iommu_map_sg_atomic(domain, iova, sgt.sgl, sgt.orig_nents, ioprot)
 			< size)
 		goto out_free_sg;
 
@@ -1003,7 +1003,7 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
 	 * We'll leave any physical concatenation to the IOMMU driver's
 	 * implementation - it knows better than we do.
 	 */
-	if (iommu_map_sg(domain, iova, sg, nents, prot) < iova_len)
+	if (iommu_map_sg_atomic(domain, iova, sg, nents, prot) < iova_len)
 		goto out_free_iova;
 
 	return __finalise_sg(dev, sg, nents, iova);
drivers/iommu/exynos-iommu.c +1 -1
index 05c6bc099d62..46414234c179 100644
@@ -1078,7 +1078,7 @@ static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
  */
 static int exynos_iommu_map(struct iommu_domain *iommu_domain,
 			    unsigned long l_iova, phys_addr_t paddr, size_t size,
-			    int prot)
+			    int prot, gfp_t gfp)
 {
 	struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
 	sysmmu_pte_t *entry;
drivers/iommu/intel-iommu.c +1 -1
index 28cb713d728c..4f0ff28f7cb9 100644
@@ -5137,7 +5137,7 @@ static void intel_iommu_detach_device(struct iommu_domain *domain,
 
 static int intel_iommu_map(struct iommu_domain *domain,
 			   unsigned long iova, phys_addr_t hpa,
-			   size_t size, int iommu_prot)
+			   size_t size, int iommu_prot, gfp_t gfp)
 {
 	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
 	u64 max_addr;
drivers/iommu/iommu.c +37 -6

84 lines not on this page · raw message

drivers/iommu/ipmmu-vmsa.c +1 -1
index 9a380c10655e..e005c83d49d8 100644
@@ -707,7 +707,7 @@ static void ipmmu_detach_device(struct iommu_domain *io_domain,
 }
 
 static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
-		     phys_addr_t paddr, size_t size, int prot)
+		     phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
 
drivers/iommu/msm_iommu.c +1 -1
index 9fb0eb7a4d02..3f6bf3653aa2 100644
@@ -508,7 +508,7 @@ static void msm_iommu_detach_dev(struct iommu_domain *domain,
 }
 
 static int msm_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			 phys_addr_t pa, size_t len, int prot)
+			 phys_addr_t pa, size_t len, int prot, gfp_t gfp)
 {
 	struct msm_priv *priv = to_msm_priv(domain);
 	unsigned long flags;
drivers/iommu/mtk_iommu.c +1 -1
index de3e02277b70..3176b9b54d4d 100644
@@ -364,7 +364,7 @@ static void mtk_iommu_detach_device(struct iommu_domain *domain,
 }
 
 static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			 phys_addr_t paddr, size_t size, int prot)
+			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
 	unsigned long flags;
drivers/iommu/mtk_iommu_v1.c +1 -1
index 52b01e3a49df..e7b1907faec1 100644
@@ -303,7 +303,7 @@ static void mtk_iommu_detach_device(struct iommu_domain *domain,
 }
 
 static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			 phys_addr_t paddr, size_t size, int prot)
+			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
 	unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
drivers/iommu/omap-iommu.c +1 -1
index d2fb347aa4ff..c1d5a71285dc 100644
@@ -1109,7 +1109,7 @@ static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
 }
 
 static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
-			  phys_addr_t pa, size_t bytes, int prot)
+			  phys_addr_t pa, size_t bytes, int prot, gfp_t gfp)
 {
 	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
 	struct device *dev = omap_domain->dev;
drivers/iommu/qcom_iommu.c +1 -1
index 8cdd3f059513..a01e07a4e76f 100644
@@ -411,7 +411,7 @@ static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *de
 }
 
 static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			  phys_addr_t paddr, size_t size, int prot)
+			  phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	int ret;
 	unsigned long flags;
drivers/iommu/rockchip-iommu.c +1 -1
index 77d4bd93fe4b..aa3507f35107 100644
@@ -760,7 +760,7 @@ static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr,
 }
 
 static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova,
-			phys_addr_t paddr, size_t size, int prot)
+			phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
 	unsigned long flags;
drivers/iommu/s390-iommu.c +1 -1
index 22d4db302c1c..efa6aa68521d 100644
@@ -265,7 +265,7 @@ static int s390_iommu_update_trans(struct s390_domain *s390_domain,
 }
 
 static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			  phys_addr_t paddr, size_t size, int prot)
+			  phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct s390_domain *s390_domain = to_s390_domain(domain);
 	int flags = ZPCI_PTE_VALID, rc = 0;
drivers/iommu/tegra-gart.c +1 -1
index 4d8057916552..f300099852b1 100644
@@ -190,7 +190,7 @@ static inline int __gart_iommu_map(struct gart_device *gart, unsigned long iova,
 }
 
 static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			  phys_addr_t pa, size_t bytes, int prot)
+			  phys_addr_t pa, size_t bytes, int prot, gfp_t gfp)
 {
 	struct gart_device *gart = gart_handle;
 	int ret;
drivers/iommu/tegra-smmu.c +1 -1
index 5182c7d6171e..e1bf867e0607 100644
@@ -641,7 +641,7 @@ static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
 }
 
 static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
-			  phys_addr_t paddr, size_t size, int prot)
+			  phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
 {
 	struct tegra_smmu_as *as = to_smmu_as(domain);
 	dma_addr_t pte_dma;
include/linux/iommu.h +20 -1
index ffbbc7e39cee..76b8e7fe3ed0 100644
@@ -198,7 +198,7 @@ struct iommu_ops {
 	int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
 	void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
 	int (*map)(struct iommu_domain *domain, unsigned long iova,
-		   phys_addr_t paddr, size_t size, int prot);
+		   phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
 	size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
 		     size_t size);
 	void (*flush_iotlb_all)(struct iommu_domain *domain);
@@ -295,12 +295,17 @@ extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
 extern struct iommu_domain *iommu_get_dma_domain(struct device *dev);
 extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
 		     phys_addr_t paddr, size_t size, int prot);
+extern int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
+		     phys_addr_t paddr, size_t size, int prot);
 extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
 			  size_t size);
 extern size_t iommu_unmap_fast(struct iommu_domain *domain,
 			       unsigned long iova, size_t size);
 extern size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
 			   struct scatterlist *sg,unsigned int nents, int prot);
+extern size_t iommu_map_sg_atomic(struct iommu_domain *domain,
+				  unsigned long iova, struct scatterlist *sg,
+				  unsigned int nents, int prot);
 extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
 extern void iommu_set_fault_handler(struct iommu_domain *domain,
 			iommu_fault_handler_t handler, void *token);
@@ -469,6 +474,13 @@ static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
 	return -ENODEV;
 }
 
+static inline int iommu_map_atomic(struct iommu_domain *domain,
+				   unsigned long iova, phys_addr_t paddr,
+				   size_t size, int prot)
+{
+	return -ENODEV;
+}
+
 static inline size_t iommu_unmap(struct iommu_domain *domain,
 				 unsigned long iova, size_t size)
 {
@@ -488,6 +500,13 @@ static inline size_t iommu_map_sg(struct iommu_domain *domain,
 	return 0;
 }
 
+static inline size_t iommu_map_sg_atomic(struct iommu_domain *domain,
+				  unsigned long iova, struct scatterlist *sg,
+				  unsigned int nents, int prot)
+{
+	return 0;
+}
+
 static inline void iommu_flush_tlb_all(struct iommu_domain *domain)
 {
 }
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v2 2/4 iommu/dma-iommu: Handle deferred devices

In reply to Tom Murphy

Handle devices which defer their attach to the iommu in the dma-iommu api

---
 drivers/iommu/dma-iommu.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
drivers/iommu/dma-iommu.c +30 -0
index 7a96c2c8f56b..c18f74ad1e8b 100644
@@ -322,6 +322,17 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
 	return iova_reserve_iommu_regions(dev, domain);
 }
 
+static int handle_deferred_device(struct device *dev)
+{
+	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
+	const struct iommu_ops *ops = domain->ops;
+
+	if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))
+		return iommu_attach_device(domain, dev);
+
+	return 0;
+}
+
 /**
  * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
  *                    page flags.
@@ -835,6 +846,8 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
 	bool coherent = dev_is_dma_coherent(dev);
 	dma_addr_t dma_handle;
 
+	handle_deferred_device(dev);
+
 	dma_handle =__iommu_dma_map(dev, phys, size,
 			dma_info_to_prot(dir, coherent, attrs),
 			iommu_get_dma_domain(dev));
@@ -849,6 +862,8 @@ static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
 {
 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
 
+	handle_deferred_device(dev);
+
 	if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
 		phys_addr_t phys = iommu_iova_to_phys(domain, dma_handle);
 
@@ -873,6 +888,8 @@ static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
 	unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
 	int i, count = 0;
 
+	handle_deferred_device(dev);
+
 	for_each_sg(sg, s, nents, i) {
 		/* Restore this segment's original unaligned fields first */
 		unsigned int s_iova_off = sg_dma_address(s);
@@ -1022,6 +1039,8 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
 	struct scatterlist *tmp;
 	int i;
 
+	handle_deferred_device(dev);
+
 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
 		iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
 
@@ -1042,6 +1061,8 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
 static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
 		size_t size, enum dma_data_direction dir, unsigned long attrs)
 {
+	handle_deferred_device(dev);
+
 	return __iommu_dma_map(dev, phys, size,
 			dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO,
 			iommu_get_dma_domain(dev));
@@ -1050,12 +1071,15 @@ static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
 static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
 		size_t size, enum dma_data_direction dir, unsigned long attrs)
 {
+	handle_deferred_device(dev);
+
 	__iommu_dma_unmap(iommu_get_dma_domain(dev), handle, size);
 }
 
 static void *iommu_dma_alloc(struct device *dev, size_t size,
 		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
 {
+	handle_deferred_device(dev);
 	gfp |= __GFP_ZERO;
 
 #ifdef CONFIG_DMA_DIRECT_REMAP
@@ -1076,6 +1100,8 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
 {
 	struct page *page;
 
+	handle_deferred_device(dev);
+
 	/*
 	 * cpu_addr can be one of 4 things depending on how it was allocated:
 	 *
@@ -1115,6 +1141,8 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
 	unsigned long pfn;
 	int ret;
 
+	handle_deferred_device(dev);
+
 	vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs);
 
 	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
@@ -1143,6 +1171,8 @@ static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
 	struct page *page;
 	int ret;
 
+	handle_deferred_device(dev);
+
 #ifdef CONFIG_DMA_DIRECT_REMAP
 	if (is_vmalloc_addr(cpu_addr)) {
 		if (!(attrs & DMA_ATTR_FORCE_CONTIGUOUS))
Signature · 1 line
2.17.1
maintainer
#3 lore raw

Re: [PATCH v2 2/4] iommu/dma-iommu: Handle deferred devices

Quoted 1 line · Tom Murphy

Handle devices which defer their attach to the iommu in the dma-iommu api

I've just spent a while trying to understand what this is about...

AFAICS it's a kdump thing where the regular default domain attachment may lead to ongoing DMA traffic from the crashed kernel raising a fault storm, so we put off the "real" attach of a given device until we know it's been reset and brought into a sane state, but the only way to reliably detect that is to wait until the kdump kernel driver starts making new DMA mappings. Is that about right?

(I note that for SMMUv3 we now handle that situation with the slightly more heavy-handed approach of just turning off reporting and letting the 'rogue' devices fault silently, but I appreciate that not all IOMMUs may have that option)

Quoted 10 lines
---
  drivers/iommu/dma-iommu.c | 30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)
drivers/iommu/dma-iommu.c +0 -0
index 7a96c2c8f56b..c18f74ad1e8b 100644

drivers/iommu/dma-iommu.c static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, · line 327

Quoted 6 lines
drivers/iommu/dma-iommu.c +3 -0
@@ -322,6 +322,17 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
  	return iova_reserve_iommu_regions(dev, domain);
  }
 
+static int handle_deferred_device(struct device *dev)
+{
+	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);

We don't want iommu_get_domain_for_dev() in fast-paths, as the contention on the group refcount has proven to have a surprisingly high overhead on some large systems. That's what iommu_get_dma_domain() solves, but ideally, can this be wrapped in is_kdump_kernel() such as to have no impact at all on the general case?

Quoted 21 lines
drivers/iommu/dma-iommu.c +10 -0
+	const struct iommu_ops *ops = domain->ops;
+
+	if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))
+		return iommu_attach_device(domain, dev);
+
+	return 0;
+}
+
  /**
   * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
   *                    page flags.
@@ -835,6 +846,8 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
  	bool coherent = dev_is_dma_coherent(dev);
  	dma_addr_t dma_handle;
 
+	handle_deferred_device(dev);
+
  	dma_handle =__iommu_dma_map(dev, phys, size,
  			dma_info_to_prot(dir, coherent, attrs),
  			iommu_get_dma_domain(dev));

drivers/iommu/dma-iommu.c static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle, · line 865

Quoted 4 lines
drivers/iommu/dma-iommu.c +1 -0
@@ -849,6 +862,8 @@ static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
  {
  	struct iommu_domain *domain = iommu_get_dma_domain(dev);
 
+	handle_deferred_device(dev);

You don't need this - it's completely bogus to make an unmap call without having already called the corresponding map function, so we can assume it's already been dealt with.

drivers/iommu/dma-iommu.c static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents, · line 891

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
  	if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
  		phys_addr_t phys = iommu_iova_to_phys(domain, dma_handle);
 
@@ -873,6 +888,8 @@ static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
  	unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
  	int i, count = 0;
 
+	handle_deferred_device(dev);

Hmm, this should be in iommu_dma_map_sg() - that's the guy that needs a valid domain, and it's impossible to get to __finalise_sg() without having been through there anyway.

drivers/iommu/dma-iommu.c static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, · line 1042

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
  	for_each_sg(sg, s, nents, i) {
  		/* Restore this segment's original unaligned fields first */
  		unsigned int s_iova_off = sg_dma_address(s);
@@ -1022,6 +1039,8 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  	struct scatterlist *tmp;
  	int i;
 
+	handle_deferred_device(dev);

Again, not necessary.

drivers/iommu/dma-iommu.c static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, · line 1064

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
  	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  		iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
 
@@ -1042,6 +1061,8 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
  		size_t size, enum dma_data_direction dir, unsigned long attrs)
  {
+	handle_deferred_device(dev);

Ditto.

drivers/iommu/dma-iommu.c static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys, · line 1074

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
  	return __iommu_dma_map(dev, phys, size,
  			dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO,
  			iommu_get_dma_domain(dev));
@@ -1050,12 +1071,15 @@ static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
  static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
  		size_t size, enum dma_data_direction dir, unsigned long attrs)
  {
+	handle_deferred_device(dev);

Ditto.

Quoted 12 lines
drivers/iommu/dma-iommu.c +2 -0
+
  	__iommu_dma_unmap(iommu_get_dma_domain(dev), handle, size);
  }
 
  static void *iommu_dma_alloc(struct device *dev, size_t size,
  		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  {
+	handle_deferred_device(dev);
  	gfp |= __GFP_ZERO;
 
  #ifdef CONFIG_DMA_DIRECT_REMAP

drivers/iommu/dma-iommu.c static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, · line 1103

Quoted 4 lines
drivers/iommu/dma-iommu.c +1 -0
@@ -1076,6 +1100,8 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
  {
  	struct page *page;
 
+	handle_deferred_device(dev);

Similarly, you can't free anything that hasn't already come from a successful call to iommu_dma_alloc()...

drivers/iommu/dma-iommu.c static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, · line 1144

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
  	/*
  	 * cpu_addr can be one of 4 things depending on how it was allocated:
  	 *
@@ -1115,6 +1141,8 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  	unsigned long pfn;
  	int ret;
 
+	handle_deferred_device(dev);

...nor can you mmap() it...

drivers/iommu/dma-iommu.c static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt, · line 1174

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
  	vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs);
 
  	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
@@ -1143,6 +1171,8 @@ static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
  	struct page *page;
  	int ret;
 
+	handle_deferred_device(dev);

...nor attempt to export it.

Robin.

Quoted 5 lines
drivers/iommu/dma-iommu.c +1 -0
+
  #ifdef CONFIG_DMA_DIRECT_REMAP
  	if (is_vmalloc_addr(cpu_addr)) {
  		if (!(attrs & DMA_ATTR_FORCE_CONTIGUOUS))
author
#4 lore raw

Re: [PATCH v2 2/4] iommu/dma-iommu: Handle deferred devices

Quoted 4 lines · Robin Murphy
Quoted 1 line · Tom Murphy

Handle devices which defer their attach to the iommu in the dma-iommu api

Quoted 8 lines

I've just spent a while trying to understand what this is about...

AFAICS it's a kdump thing where the regular default domain attachment may lead to ongoing DMA traffic from the crashed kernel raising a fault storm, so we put off the "real" attach of a given device until we know it's been reset and brought into a sane state, but the only way to reliably detect that is to wait until the kdump kernel driver starts making new DMA mappings. Is that about right?

That's the impression I got too. The many iterations of the patch series which contributed this code makes it hard to figure out exactly why it's doing what it's doing but AFAIK it works how you described it.

Quoted 19 lines

(I note that for SMMUv3 we now handle that situation with the slightly more heavy-handed approach of just turning off reporting and letting the 'rogue' devices fault silently, but I appreciate that not all IOMMUs may have that option)

Quoted 13 lines
---
  drivers/iommu/dma-iommu.c | 30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)
drivers/iommu/dma-iommu.c +0 -0
index 7a96c2c8f56b..c18f74ad1e8b 100644
@@ -322,6 +322,17 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
      return iova_reserve_iommu_regions(dev, domain);
  }
 
Quoted 9 lines

drivers/iommu/dma-iommu.c static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, · line 327

Quoted 3 lines
drivers/iommu/dma-iommu.c +3 -0
+static int handle_deferred_device(struct device *dev)
+{
+     struct iommu_domain *domain = iommu_get_domain_for_dev(dev);

We don't want iommu_get_domain_for_dev() in fast-paths, as the contention on the group refcount has proven to have a surprisingly high overhead on some large systems. That's what iommu_get_dma_domain() solves, but ideally, can this be wrapped in is_kdump_kernel() such as to have no impact at all on the general case?

will do.

Quoted 116 lines
Quoted 22 lines
drivers/iommu/dma-iommu.c +10 -0
+     const struct iommu_ops *ops = domain->ops;
+
+     if (ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))
+             return iommu_attach_device(domain, dev);
+
+     return 0;
+}
+
  /**
   * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
   *                    page flags.
@@ -835,6 +846,8 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
      bool coherent = dev_is_dma_coherent(dev);
      dma_addr_t dma_handle;
 
+     handle_deferred_device(dev);
+
      dma_handle =__iommu_dma_map(dev, phys, size,
                      dma_info_to_prot(dir, coherent, attrs),
                      iommu_get_dma_domain(dev));

drivers/iommu/dma-iommu.c static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle, · line 865

Quoted 4 lines
drivers/iommu/dma-iommu.c +1 -0
@@ -849,6 +862,8 @@ static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
  {
      struct iommu_domain *domain = iommu_get_dma_domain(dev);
 
+     handle_deferred_device(dev);

You don't need this - it's completely bogus to make an unmap call without having already called the corresponding map function, so we can assume it's already been dealt with.

drivers/iommu/dma-iommu.c static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents, · line 891

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
      if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
              phys_addr_t phys = iommu_iova_to_phys(domain, dma_handle);
 
@@ -873,6 +888,8 @@ static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
      unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
      int i, count = 0;
 
+     handle_deferred_device(dev);

Hmm, this should be in iommu_dma_map_sg() - that's the guy that needs a valid domain, and it's impossible to get to __finalise_sg() without having been through there anyway.

drivers/iommu/dma-iommu.c static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, · line 1042

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
      for_each_sg(sg, s, nents, i) {
              /* Restore this segment's original unaligned fields first */
              unsigned int s_iova_off = sg_dma_address(s);
@@ -1022,6 +1039,8 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
      struct scatterlist *tmp;
      int i;
 
+     handle_deferred_device(dev);

Again, not necessary.

drivers/iommu/dma-iommu.c static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, · line 1064

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
      if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
              iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
 
@@ -1042,6 +1061,8 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
              size_t size, enum dma_data_direction dir, unsigned long attrs)
  {
+     handle_deferred_device(dev);

Ditto.

drivers/iommu/dma-iommu.c static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys, · line 1074

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
      return __iommu_dma_map(dev, phys, size,
                      dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO,
                      iommu_get_dma_domain(dev));
@@ -1050,12 +1071,15 @@ static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
  static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
              size_t size, enum dma_data_direction dir, unsigned long attrs)
  {
+     handle_deferred_device(dev);

Ditto.

Quoted 12 lines
drivers/iommu/dma-iommu.c +2 -0
+
      __iommu_dma_unmap(iommu_get_dma_domain(dev), handle, size);
  }
 
  static void *iommu_dma_alloc(struct device *dev, size_t size,
              dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  {
+     handle_deferred_device(dev);
      gfp |= __GFP_ZERO;
 
  #ifdef CONFIG_DMA_DIRECT_REMAP

drivers/iommu/dma-iommu.c static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, · line 1103

Quoted 4 lines
drivers/iommu/dma-iommu.c +1 -0
@@ -1076,6 +1100,8 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
  {
      struct page *page;
 
+     handle_deferred_device(dev);

Similarly, you can't free anything that hasn't already come from a successful call to iommu_dma_alloc()...

drivers/iommu/dma-iommu.c static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, · line 1144

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -0
+
      /*
       * cpu_addr can be one of 4 things depending on how it was allocated:
       *
@@ -1115,6 +1141,8 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
      unsigned long pfn;
      int ret;
 
+     handle_deferred_device(dev);

...nor can you mmap() it...

Quoted 3 lines
drivers/iommu/dma-iommu.c +1 -0
+
      vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs);
 
Quoted 8 lines

drivers/iommu/dma-iommu.c static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt, · line 1174

Quoted 6 lines
drivers/iommu/dma-iommu.c +1 -0
      if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
@@ -1143,6 +1171,8 @@ static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
      struct page *page;
      int ret;
 
+     handle_deferred_device(dev);

...nor attempt to export it.

That all makes sense. Will fix.

Quoted 8 lines

Robin.

Quoted 5 lines
drivers/iommu/dma-iommu.c +1 -0
+
  #ifdef CONFIG_DMA_DIRECT_REMAP
      if (is_vmalloc_addr(cpu_addr)) {
              if (!(attrs & DMA_ATTR_FORCE_CONTIGUOUS))
author
#5 lore raw

PATCH v2 3/4 iommu/dma-iommu: Use the dev->coherent_dma_mask

In reply to Tom Murphy

Use the dev->coherent_dma_mask when allocating in the dma-iommu ops api.

---
 drivers/iommu/dma-iommu.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/iommu/dma-iommu.c +9 -7
index c18f74ad1e8b..df03104978d7 100644
@@ -436,7 +436,8 @@ static void __iommu_dma_unmap(struct iommu_domain *domain, dma_addr_t dma_addr,
 }
 
 static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
-		size_t size, int prot, struct iommu_domain *domain)
+		size_t size, int prot, struct iommu_domain *domain,
+		dma_addr_t dma_limit)
 {
 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
 	size_t iova_off = 0;
@@ -447,7 +448,7 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
 		size = iova_align(&cookie->iovad, size + iova_off);
 	}
 
-	iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
+	iova = iommu_dma_alloc_iova(domain, size, dma_limit, dev);
 	if (!iova)
 		return DMA_MAPPING_ERROR;
 
@@ -490,7 +491,7 @@ static void *iommu_dma_alloc_contiguous(struct device *dev, size_t size,
 		return NULL;
 
 	*dma_handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot,
-			iommu_get_dma_domain(dev));
+			iommu_get_dma_domain(dev), dev->coherent_dma_mask);
 	if (*dma_handle == DMA_MAPPING_ERROR) {
 		if (!dma_release_from_contiguous(dev, page, count))
 			__free_pages(page, page_order);
@@ -760,7 +761,7 @@ static void *iommu_dma_alloc_pool(struct device *dev, size_t size,
 
 	*dma_handle = __iommu_dma_map(dev, page_to_phys(page), size,
 			dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs),
-			iommu_get_domain_for_dev(dev));
+			iommu_get_domain_for_dev(dev), dev->coherent_dma_mask);
 	if (*dma_handle == DMA_MAPPING_ERROR) {
 		dma_free_from_pool(vaddr, PAGE_ALIGN(size));
 		return NULL;
@@ -850,7 +851,7 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
 
 	dma_handle =__iommu_dma_map(dev, phys, size,
 			dma_info_to_prot(dir, coherent, attrs),
-			iommu_get_dma_domain(dev));
+			iommu_get_dma_domain(dev), dma_get_mask(dev));
 	if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
 	    dma_handle != DMA_MAPPING_ERROR)
 		arch_sync_dma_for_device(dev, phys, size, dir);
@@ -1065,7 +1066,7 @@ static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
 
 	return __iommu_dma_map(dev, phys, size,
 			dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO,
-			iommu_get_dma_domain(dev));
+			iommu_get_dma_domain(dev), dma_get_mask(dev));
 }
 
 static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
@@ -1250,7 +1251,8 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
 	if (!msi_page)
 		return NULL;
 
-	iova = __iommu_dma_map(dev, msi_addr, size, prot, domain);
+	iova = __iommu_dma_map(dev, msi_addr, size, prot, domain,
+			dma_get_mask(dev));
 	if (iova == DMA_MAPPING_ERROR)
 		goto out_free_page;
 
Signature · 1 line
2.17.1
#6 lore raw

Re: [PATCH v2 3/4] iommu/dma-iommu: Use the dev->coherent_dma_mask

Review

Quoted 4 lines

static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,

(quoted diff) +2 -1
-		size_t size, int prot, struct iommu_domain *domain)
+		size_t size, int prot, struct iommu_domain *domain,
+		dma_addr_t dma_limit)

Can we just call this dma_mask?

Review static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev, · line 1255

Quoted 8 lines

static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,

(quoted diff) +2 -1
@@ -1250,7 +1251,8 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
 	if (!msi_page)
 		return NULL;
 
-	iova = __iommu_dma_map(dev, msi_addr, size, prot, domain);
+	iova = __iommu_dma_map(dev, msi_addr, size, prot, domain,
+			dma_get_mask(dev));

Hmm, I don't think we need the DMA mask for the MSI mapping, this should probably always use a 64-bit mask. Or we could just untangle it from the DMA mapping fast path entire, something like:

---

Quoted 1 line

From 0debafc85174ca830f2e371ff8e8f7465bde3ad8 Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Tue, 30 Apr 2019 07:06:23 -0400
Subject: iommu/dma: opencode __iommu_dma_map in iommu_dma_get_msi_page

The MSI page mapping really is a little different from the normal DMA mappings and doesn't need to look at the DMA mask. Just open code it instead of trying to squeeze the behavior into the DMA path helpers.

---
 drivers/iommu/dma-iommu.c | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)
drivers/iommu/dma-iommu.c +7 -20
index 58c35bab7626..2ac0df0879d7 100644
@@ -358,11 +358,6 @@ static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
 	struct iova_domain *iovad = &cookie->iovad;
 	unsigned long shift, iova_len, iova = 0;
 
-	if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
-		cookie->msi_iova += size;
-		return cookie->msi_iova - size;
-	}
-
 	shift = iova_shift(iovad);
 	iova_len = size >> shift;
 	/*
@@ -397,10 +392,7 @@ static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
 {
 	struct iova_domain *iovad = &cookie->iovad;
 
-	/* The MSI case is only ever cleaning up its most recent allocation */
-	if (cookie->type == IOMMU_DMA_MSI_COOKIE)
-		cookie->msi_iova -= size;
-	else if (cookie->fq_domain)	/* non-strict mode */
+	if (cookie->fq_domain)	/* non-strict mode */
 		queue_iova(iovad, iova_pfn(iovad, iova),
 				size >> iova_shift(iovad), 0);
 	else
@@ -430,14 +422,10 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
 {
 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
-	size_t iova_off = 0;
+	size_t iova_off = iova_offset(&cookie->iovad, phys);
 	dma_addr_t iova;
 
-	if (cookie->type == IOMMU_DMA_IOVA_COOKIE) {
-		iova_off = iova_offset(&cookie->iovad, phys);
-		size = iova_align(&cookie->iovad, size + iova_off);
-	}
-
+	size = iova_align(&cookie->iovad, size + iova_off);
 	iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
 	if (!iova)
 		return DMA_MAPPING_ERROR;
@@ -1121,7 +1109,6 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
 {
 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
 	struct iommu_dma_msi_page *msi_page;
-	dma_addr_t iova;
 	int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
 	size_t size = cookie_msi_granule(cookie);
 
@@ -1134,16 +1121,16 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
 	if (!msi_page)
 		return NULL;
 
-	iova = __iommu_dma_map(dev, msi_addr, size, prot);
-	if (iova == DMA_MAPPING_ERROR)
+	if (iommu_map(domain, cookie->msi_iova, msi_addr, size, prot))
 		goto out_free_page;
 
 	INIT_LIST_HEAD(&msi_page->list);
 	msi_page->phys = msi_addr;
-	msi_page->iova = iova;
+	msi_page->iova = cookie->msi_iova;
 	list_add(&msi_page->list, &cookie->msi_page_list);
-	return msi_page;
 
+	cookie->msi_iova += size;
+	return msi_page;
 out_free_page:
 	kfree(msi_page);
 	return NULL;
Signature · 1 line
2.20.1
maintainer
#7 lore raw

Re: [PATCH v2 3/4] iommu/dma-iommu: Use the dev->coherent_dma_mask

Quoted 12 lines · Christoph Hellwig

Review

Quoted 4 lines

static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,

(quoted diff) +2 -1
-		size_t size, int prot, struct iommu_domain *domain)
+		size_t size, int prot, struct iommu_domain *domain,
+		dma_addr_t dma_limit)

Can we just call this dma_mask?

Quoted 5 lines

static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,

drivers/iommu/dma-iommu.c +0 -0
@@ -1250,7 +1251,8 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
  	if (!msi_page)
  		return NULL;
 
Quoted 6 lines

Review static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev, · line 1255

Quoted 3 lines
(quoted diff) +2 -1
-	iova = __iommu_dma_map(dev, msi_addr, size, prot, domain);
+	iova = __iommu_dma_map(dev, msi_addr, size, prot, domain,
+			dma_get_mask(dev));

Hmm, I don't think we need the DMA mask for the MSI mapping, this should probably always use a 64-bit mask.

If that were true then we wouldn't need DMA masks for regular mappings either. If we have to map the MSI doorbell at all, then we certainly have to place it at an IOVA that the relevant device is actually capable of addressing.

Robin.

Quoted 93 lines

Or we could just untangle
it from the DMA mapping fast path entire, something like:

---
From 0debafc85174ca830f2e371ff8e8f7465bde3ad8 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Tue, 30 Apr 2019 07:06:23 -0400
Subject: iommu/dma: opencode __iommu_dma_map in iommu_dma_get_msi_page

The MSI page mapping really is a little different from the normal DMA mappings and doesn't need to look at the DMA mask. Just open code it instead of trying to squeeze the behavior into the DMA path helpers.

---
  drivers/iommu/dma-iommu.c | 27 +++++++--------------------
  1 file changed, 7 insertions(+), 20 deletions(-)
drivers/iommu/dma-iommu.c +7 -20
index 58c35bab7626..2ac0df0879d7 100644
@@ -358,11 +358,6 @@ static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
  	struct iova_domain *iovad = &cookie->iovad;
  	unsigned long shift, iova_len, iova = 0;
 
-	if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
-		cookie->msi_iova += size;
-		return cookie->msi_iova - size;
-	}
-
  	shift = iova_shift(iovad);
  	iova_len = size >> shift;
  	/*
@@ -397,10 +392,7 @@ static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
  {
  	struct iova_domain *iovad = &cookie->iovad;
 
-	/* The MSI case is only ever cleaning up its most recent allocation */
-	if (cookie->type == IOMMU_DMA_MSI_COOKIE)
-		cookie->msi_iova -= size;
-	else if (cookie->fq_domain)	/* non-strict mode */
+	if (cookie->fq_domain)	/* non-strict mode */
  		queue_iova(iovad, iova_pfn(iovad, iova),
  				size >> iova_shift(iovad), 0);
  	else
@@ -430,14 +422,10 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
  {
  	struct iommu_domain *domain = iommu_get_dma_domain(dev);
  	struct iommu_dma_cookie *cookie = domain->iova_cookie;
-	size_t iova_off = 0;
+	size_t iova_off = iova_offset(&cookie->iovad, phys);
  	dma_addr_t iova;
 
-	if (cookie->type == IOMMU_DMA_IOVA_COOKIE) {
-		iova_off = iova_offset(&cookie->iovad, phys);
-		size = iova_align(&cookie->iovad, size + iova_off);
-	}
-
+	size = iova_align(&cookie->iovad, size + iova_off);
  	iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
  	if (!iova)
  		return DMA_MAPPING_ERROR;
@@ -1121,7 +1109,6 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
  {
  	struct iommu_dma_cookie *cookie = domain->iova_cookie;
  	struct iommu_dma_msi_page *msi_page;
-	dma_addr_t iova;
  	int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
  	size_t size = cookie_msi_granule(cookie);
 
@@ -1134,16 +1121,16 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
  	if (!msi_page)
  		return NULL;
 
-	iova = __iommu_dma_map(dev, msi_addr, size, prot);
-	if (iova == DMA_MAPPING_ERROR)
+	if (iommu_map(domain, cookie->msi_iova, msi_addr, size, prot))
  		goto out_free_page;
 
  	INIT_LIST_HEAD(&msi_page->list);
  	msi_page->phys = msi_addr;
-	msi_page->iova = iova;
+	msi_page->iova = cookie->msi_iova;
  	list_add(&msi_page->list, &cookie->msi_page_list);
-	return msi_page;
 
+	cookie->msi_iova += size;
+	return msi_page;
  out_free_page:
  	kfree(msi_page);
  	return NULL;
#8 lore raw

Re: [PATCH v2 3/4] iommu/dma-iommu: Use the dev->coherent_dma_mask

Quoted 7 lines · Robin Murphy
Quoted 2 lines

Hmm, I don't think we need the DMA mask for the MSI mapping, this should probably always use a 64-bit mask.

If that were true then we wouldn't need DMA masks for regular mappings either. If we have to map the MSI doorbell at all, then we certainly have to place it at an IOVA that the relevant device is actually capable of addressing.

Well, as shown by the patch below we don't even look at the DMA mask for the MSI page - we just allocate from bottom to top.

maintainer
#9 lore raw

Re: [PATCH v2 3/4] iommu/dma-iommu: Use the dev->coherent_dma_mask

Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · Robin Murphy
Quoted 2 lines

Hmm, I don't think we need the DMA mask for the MSI mapping, this should probably always use a 64-bit mask.

If that were true then we wouldn't need DMA masks for regular mappings either. If we have to map the MSI doorbell at all, then we certainly have to place it at an IOVA that the relevant device is actually capable of addressing.

Well, as shown by the patch below we don't even look at the DMA mask for the MSI page - we just allocate from bottom to top.

In the trivial cookie for unmanaged domains, yes, but in that case the responsibility is on VFIO to provide a suitable (i.e. sub-32-bit) address range for that cookie in the first place. In the managed case, allocation uses the streaming mask via iommu_dma_get_msi_page() calling __iommu_dma_map(). Admittedly the mask can then get overlooked when reusing an existing mapping, which strictly could pose a problem if you have multiple devices with incompatible masks in the same group (and such that the PCI stuff doesn't already mitigate it), but that's such an obscure corner case that I'm reticent to introduce the complication to handle it until it's actually proven necessary.

Robin.

author
#10 lore raw

Re: [PATCH v2 3/4] iommu/dma-iommu: Use the dev->coherent_dma_mask

Just to make this clear, I won't apply Christoph's patch (the one in this email thread) and instead the only change I will make is to rename dma_limit to dma_mask.

Quoted 26 lines · Robin Murphy
Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · Robin Murphy
Quoted 2 lines

Hmm, I don't think we need the DMA mask for the MSI mapping, this should probably always use a 64-bit mask.

If that were true then we wouldn't need DMA masks for regular mappings either. If we have to map the MSI doorbell at all, then we certainly have to place it at an IOVA that the relevant device is actually capable of addressing.

Well, as shown by the patch below we don't even look at the DMA mask for the MSI page - we just allocate from bottom to top.

In the trivial cookie for unmanaged domains, yes, but in that case the responsibility is on VFIO to provide a suitable (i.e. sub-32-bit) address range for that cookie in the first place. In the managed case, allocation uses the streaming mask via iommu_dma_get_msi_page() calling __iommu_dma_map(). Admittedly the mask can then get overlooked when reusing an existing mapping, which strictly could pose a problem if you have multiple devices with incompatible masks in the same group (and such that the PCI stuff doesn't already mitigate it), but that's such an obscure corner case that I'm reticent to introduce the complication to handle it until it's actually proven necessary.

Robin.

#11 lore raw

Re: [PATCH v2 3/4] iommu/dma-iommu: Use the dev->coherent_dma_mask

Quoted 3 lines · Tom Murphy

Just to make this clear, I won't apply Christoph's patch (the one in this email thread) and instead the only change I will make is to rename dma_limit to dma_mask.

Sounds good for now.

author
#12 lore raw

PATCH v2 4/4 iommu/amd: Convert the AMD iommu driver to the dma-iommu api

In reply to Tom Murphy

Convert the AMD iommu driver to the dma-iommu api. Remove the iova handling and reserve region code from the AMD iommu driver.

---
 drivers/iommu/Kconfig     |   1 +
 drivers/iommu/amd_iommu.c | 680 ++++----------------------------------
 2 files changed, 70 insertions(+), 611 deletions(-)
drivers/iommu/Kconfig +1 -0
index 6f07f3b21816..5914ba85180b 100644
@@ -136,6 +136,7 @@ config AMD_IOMMU
 	select PCI_PASID
 	select IOMMU_API
 	select IOMMU_IOVA
+	select IOMMU_DMA
 	depends on X86_64 && PCI && ACPI
 	---help---
 	  With this option you can enable support for AMD IOMMU hardware in
drivers/iommu/amd_iommu.c +69 -611

832 lines not on this page · raw message

Signature · 1 line
2.17.1