iommu/amd: Convert the AMD iommu driver to the dma-iommu api
Subsystems (13)
v3 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:
v3:
-rename dma_limit to dma_mask
-exit handle_deferred_device early if (!is_kdump_kernel()) -remove pointless calls to handle_deferred_device 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 | 50 ++- 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, 184 insertions(+), 653 deletions(-)
Signature · 1 line
2.17.1
PATCH v3 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 · 6 lines
2.17.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Robin Murphy
maintainerRe: [PATCH v3 1/4] iommu: Add gfp parameter to iommu_ops::map
Quoted 12 lines · Tom Murphy
Add a
gfp_tparameter to theiommu_ops::mapfunction. Remove the needless locking in the AMD iommu driver.The
iommu_ops::mapfunction (or theiommu_mapfunction 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 calliommu_mapin an atomic context, which it shouldn't do. This doesn't cause any problems because any iommu driver which uses the dma-iommu api usesgfp_atomicin it'siommu_ops::mapfunction. But doing this wastes the memory allocators atomic pools.
Hmm, in some cases iommu_ops::unmap may need to allocate as well, primarily if it needs to split a hugepage mapping. Should we pass flags through there as well, or are we prepared to assume that that case will happen rarely enough that it's fair to just assume GFP_ATOMIC? It won't happen for DMA ops, but it's conceivable that other users such as GPU drivers might make partial unmaps, and I doubt we could totally rule out the wackiest ones doing so from non-sleeping contexts.
Robin.
Quoted 437 lines
We can remove the mutex lock from
amd_iommu_mapandamd_iommu_unmap.iommu_mapdoesn’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.Signed-off-by Tom Murphy <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -1584,8 +1584,8 @@ static size_t iommu_pgsize(struct iommu_domain *domain,return pgsize; } -int iommu_map (struct iommu_domain *domain,unsigned long iova, -phys_addr_t paddr,size_t size,int prot) +int __iommu_map (struct iommu_domain *domain,unsigned long iova, +phys_addr_t paddr,size_t size,int prot,gfp_t gfp) {const struct iommu_ops *ops = domain->ops;unsigned long orig_iova = iova;@@ -1622,8 +1622,8 @@ int iommu_map(struct iommu_domain *domain, unsigned long iova,pr_debug ("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n" , iova, &paddr, pgsize); + ret = ops->map (domain, iova, paddr, pgsize, prot, gfp); - ret = ops->map (domain, iova, paddr, pgsize, prot);if (ret)break ;@@ -1643,8 +1643,22 @@ int iommu_map(struct iommu_domain *domain, unsigned long iova,return ret; } + +int iommu_map (struct iommu_domain *domain,unsigned long iova, +phys_addr_t paddr,size_t size,int prot) +{ +might_sleep (); +return __iommu_map (domain, iova, paddr, size, prot,GFP_KERNEL ); +}EXPORT_SYMBOL_GPL (iommu_map); +int iommu_map_atomic (struct iommu_domain *domain,unsigned long iova, +phys_addr_t paddr,size_t size,int prot) +{ +return __iommu_map (domain, iova, paddr, size, prot,GFP_ATOMIC ); +} +EXPORT_SYMBOL_GPL (iommu_map_atomic); +static size_t __iommu_unmap (struct iommu_domain *domain,unsigned long iova,size_t size,bool sync)@@ -1719,8 +1733,9 @@ size_t iommu_unmap_fast(struct iommu_domain *domain, }EXPORT_SYMBOL_GPL (iommu_unmap_fast); -size_t iommu_map_sg (struct iommu_domain *domain,unsigned long iova, -struct scatterlist *sg,unsigned int nents,int prot) +size_t __iommu_map_sg (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot, +gfp_t gfp) {size_t len =0 , mapped =0 ;phys_addr_t start;@@ -1731,7 +1746,9 @@ size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,phys_addr_t s_phys =sg_phys (sg);if (len && s_phys != start + len) { - ret =iommu_map (domain, iova + mapped, start, len, prot); + ret =__iommu_map (domain, iova + mapped, start, + len, prot, gfp); +if (ret) goto out_err;@@ -1759,8 +1776,22 @@ size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,return 0 ; } + +size_t iommu_map_sg (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot) +{ +might_sleep (); +return __iommu_map_sg (domain, iova, sg, nents, prot,GFP_KERNEL ); +}EXPORT_SYMBOL_GPL (iommu_map_sg); +size_t iommu_map_sg_atomic (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot) +{ +return __iommu_map_sg (domain, iova, sg, nents, prot,GFP_ATOMIC ); +} +EXPORT_SYMBOL_GPL (iommu_map_sg_atomic); +int iommu_domain_window_enable (struct iommu_domain *domain,u32 wnd_nr,phys_addr_t paddr,u64 size,int prot) {drivers/iommu/ipmmu-vmsa.c +1
-1@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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) { }
Re: [PATCH v3 1/4] iommu: Add gfp parameter to iommu_ops::map
Quoted 15 lines · Robin Murphy
Quoted 12 lines · Tom Murphy
Add a
gfp_tparameter to theiommu_ops::mapfunction. Remove the needless locking in the AMD iommu driver.The
iommu_ops::mapfunction (or theiommu_mapfunction 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 calliommu_mapin an atomic context, which it shouldn't do. This doesn't cause any problems because any iommu driver which uses the dma-iommu api usesgfp_atomicin it'siommu_ops::mapfunction. But doing this wastes the memory allocators atomic pools.
Quoted 2 lines
Hmm, in some cases
iommu_ops::unmapmay need to allocate as well, primarily if it needs to split a hugepage mapping. Should we pass flags
Are you sure that's the case?
I don't see that in the amd or intel iommu_ops::unmap code and from looking at the intel code it seems like we actually unmap the whole large page:
/* Cope with horrid API which requires us to unmap more than the size argument if it happens to be a large-page mapping. */ BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
size = VTD_PAGE_SIZE << level_to_offset_bits(level);
Quoted 445 lines
through there as well, or are we prepared to assume that that case will happen rarely enough that it's fair to just assume
GFP_ATOMIC? It won't happen for DMA ops, but it's conceivable that other users such as GPU drivers might make partial unmaps, and I doubt we could totally rule out the wackiest ones doing so from non-sleeping contexts.Robin.
Quoted 437 lines
We can remove the mutex lock from
amd_iommu_mapandamd_iommu_unmap.iommu_mapdoesn’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.Signed-off-by Tom Murphy <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -1584,8 +1584,8 @@ static size_t iommu_pgsize(struct iommu_domain *domain,return pgsize; } -int iommu_map (struct iommu_domain *domain,unsigned long iova, -phys_addr_t paddr,size_t size,int prot) +int __iommu_map (struct iommu_domain *domain,unsigned long iova, +phys_addr_t paddr,size_t size,int prot,gfp_t gfp) {const struct iommu_ops *ops = domain->ops;unsigned long orig_iova = iova;@@ -1622,8 +1622,8 @@ int iommu_map(struct iommu_domain *domain, unsigned long iova,pr_debug ("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n" , iova, &paddr, pgsize); + ret = ops->map (domain, iova, paddr, pgsize, prot, gfp); - ret = ops->map (domain, iova, paddr, pgsize, prot);if (ret)break ;@@ -1643,8 +1643,22 @@ int iommu_map(struct iommu_domain *domain, unsigned long iova,return ret; } + +int iommu_map (struct iommu_domain *domain,unsigned long iova, +phys_addr_t paddr,size_t size,int prot) +{ +might_sleep (); +return __iommu_map (domain, iova, paddr, size, prot,GFP_KERNEL ); +}EXPORT_SYMBOL_GPL (iommu_map); +int iommu_map_atomic (struct iommu_domain *domain,unsigned long iova, +phys_addr_t paddr,size_t size,int prot) +{ +return __iommu_map (domain, iova, paddr, size, prot,GFP_ATOMIC ); +} +EXPORT_SYMBOL_GPL (iommu_map_atomic); +static size_t __iommu_unmap (struct iommu_domain *domain,unsigned long iova,size_t size,bool sync)@@ -1719,8 +1733,9 @@ size_t iommu_unmap_fast(struct iommu_domain *domain, }EXPORT_SYMBOL_GPL (iommu_unmap_fast); -size_t iommu_map_sg (struct iommu_domain *domain,unsigned long iova, -struct scatterlist *sg,unsigned int nents,int prot) +size_t __iommu_map_sg (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot, +gfp_t gfp) {size_t len =0 , mapped =0 ;phys_addr_t start;@@ -1731,7 +1746,9 @@ size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,phys_addr_t s_phys =sg_phys (sg);if (len && s_phys != start + len) { - ret =iommu_map (domain, iova + mapped, start, len, prot); + ret =__iommu_map (domain, iova + mapped, start, + len, prot, gfp); +if (ret) goto out_err;@@ -1759,8 +1776,22 @@ size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,return 0 ; } + +size_t iommu_map_sg (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot) +{ +might_sleep (); +return __iommu_map_sg (domain, iova, sg, nents, prot,GFP_KERNEL ); +}EXPORT_SYMBOL_GPL (iommu_map_sg); +size_t iommu_map_sg_atomic (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot) +{ +return __iommu_map_sg (domain, iova, sg, nents, prot,GFP_ATOMIC ); +} +EXPORT_SYMBOL_GPL (iommu_map_sg_atomic); +int iommu_domain_window_enable (struct iommu_domain *domain,u32 wnd_nr,phys_addr_t paddr,u64 size,int prot) {drivers/iommu/ipmmu-vmsa.c +1
-1@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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) { }
Re: [PATCH v3 1/4] iommu: Add gfp parameter to iommu_ops::map
In reply to Robin MurphyQuoted 15 lines · Robin Murphy
Quoted 12 lines · Tom Murphy
Add a
gfp_tparameter to theiommu_ops::mapfunction. Remove the needless locking in the AMD iommu driver.The
iommu_ops::mapfunction (or theiommu_mapfunction 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 calliommu_mapin an atomic context, which it shouldn't do. This doesn't cause any problems because any iommu driver which uses the dma-iommu api usesgfp_atomicin it'siommu_ops::mapfunction. But doing this wastes the memory allocators atomic pools.
Quoted 8 lines
Hmm, in some cases
iommu_ops::unmapmay need to allocate as well, primarily if it needs to split a hugepage mapping. Should we pass flags through there as well, or are we prepared to assume that that case will happen rarely enough that it's fair to just assumeGFP_ATOMIC? It won't happen for DMA ops, but it's conceivable that other users such as GPU drivers might make partial unmaps, and I doubt we could totally rule out the wackiest ones doing so from non-sleeping contexts.
jfyi, today we (well, drm/msm) only unmap entire buffers, so assuming there isn't any coelescense of adjacent buffers that happen to form a hugepage on ::map(), we are probably ok on ::unmap()..
we do always only call ::map or ::unmap under sleepable conditions.
btw, would it be simpler to just make gfp_t a domain a attribute? That seems like it would be less churn, but maybe I'm overlooking something.
BR,
-R
Re: [PATCH v3 1/4] iommu: Add gfp parameter to iommu_ops::map
In reply to Tom MurphyQuoted 4 lines · Tom Murphy via iommu
We can remove the mutex lock from
amd_iommu_mapandamd_iommu_unmap.iommu_mapdoesn’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.
Btw, this really should be a separate patch.
_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
PATCH v3 2/4 iommu/dma-iommu: Handle deferred devices
In reply to Tom MurphyHandle devices which defer their attach to the iommu in the dma-iommu api
--- drivers/iommu/dma-iommu.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +27 -1
@@ -22,6 +22,7 @@#include <linux/pci.h> #include <linux/scatterlist.h> #include <linux/vmalloc.h> +#include <linux/crash_dump.h> struct iommu_dma_msi_page {struct list_head list;
@@ -322,6 +323,22 @@ 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; +const struct iommu_ops *ops; + +if (!is_kdump_kernel ()) +return 0 ; + + domain =iommu_get_domain_for_dev (dev); + 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,7 +852,10 @@ 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; - dma_handle =__iommu_dma_map (dev, phys, size, +if (unlikely (handle_deferred_device (dev))) +return DMA_MAPPING_ERROR ; + + dma_handle =__iommu_dma_map (dev, phys, size,dma_info_to_prot (dir, coherent, attrs),iommu_get_dma_domain (dev)); if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
@@ -953,6 +973,9 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,unsigned long mask =dma_get_seg_boundary (dev);int i; +if (unlikely (handle_deferred_device (dev))) +return 0 ; +if (!(attrs &DMA_ATTR_SKIP_CPU_SYNC ))iommu_dma_sync_sg_for_device (dev, sg, nents, dir);
@@ -1056,6 +1079,9 @@ static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,static void *iommu_dma_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs) { +if (unlikely (handle_deferred_device (dev))) +return NULL ; + gfp |= __GFP_ZERO;#ifdef CONFIG_DMA_DIRECT_REMAP
Signature · 1 line
2.17.1
Re: [PATCH v3 2/4] iommu/dma-iommu: Handle deferred devices
Quoted 5 lines · Tom Murphy
drivers/iommu/dma-iommu.c +5
-0+static int handle_deferred_device (struct device *dev) +{ +struct iommu_domain *domain; +const struct iommu_ops *ops; +
drivers/iommu/dma-iommu.c static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
Quoted 10 lines
drivers/iommu/dma-iommu.c +8
-1+if (!is_kdump_kernel ()) +return 0 ; + + domain =iommu_get_domain_for_dev (dev); - dma_handle =__iommu_dma_map(dev, phys, size, +if (unlikely (handle_deferred_device (dev))) +return DMA_MAPPING_ERROR ; + + dma_handle = __iommu_dma_map(dev, phys, size,
__iommu_dma_map already looks up the domain, and as far as I can tell all callers need the handle_deferred_device call. Should we just move it to there and pass the domain from the caller?
Also shouldn't the iommu_attach_device call inside handle_deferred_device also get an unlikely marker?
Re: [PATCH v3 2/4] iommu/dma-iommu: Handle deferred devices
like this?
In that case we need to add a call to iommu_dma_alloc_remap.
Quoted 1 line
From
862aeebb601008cf863e3aff4ff8ed7cefebeefaMon Sep 17 00:00:00 2001
From: Tom Murphy <[email protected]>
Date: Wed, 15 May 2019 05:43:25 -0700
Subject: [PATCH] iommu/dma-iommu: Handle deferred devices
Handle devices which defer their attach to the iommu in the dma-iommu api
--- drivers/iommu/dma-iommu.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +1 -0
@@ -22,6 +22,7 @@#include <linux/pci.h> #include <linux/scatterlist.h> #include <linux/vmalloc.h> +#include <linux/crash_dump.h> struct iommu_dma_msi_page {struct list_head list;
@@ -323,6 +324,21 @@ 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)
+{
+ const struct iommu_ops *ops = domain->ops;
+
+ if (!is_kdump_kernel())
+ return 0;
+
+ if (unlikely(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.
@@ -432,6 +448,9 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
size_t iova_off = 0;
dma_addr_t iova;+ if (unlikely(handle_deferred_device(dev, domain)))
+ return DMA_MAPPING_ERROR;
+
if (cookie->type == IOMMU_DMA_IOVA_COOKIE) {
iova_off = iova_offset(&cookie->iovad, phys);
size = iova_align(&cookie->iovad, size + iova_off);
@@ -609,6 +628,9 @@ static void *iommu_dma_alloc_remap(struct device
*dev, size_t size,
dma_addr_t iova;
void *vaddr;+ if (unlikely(handle_deferred_device(dev, domain)))
+ return DMA_MAPPING_ERROR;
+
*dma_handle = DMA_MAPPING_ERROR;
min_size = alloc_sizes & -alloc_sizes;
@@ -836,7 +858,7 @@ 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;- dma_handle =__iommu_dma_map(dev, phys, size,
+ dma_handle = __iommu_dma_map(dev, phys, size,
dma_info_to_prot(dir, coherent, attrs),
iommu_get_dma_domain(dev));
if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
@@ -954,6 +976,9 @@ static int iommu_dma_map_sg(struct device *dev,
struct scatterlist *sg,
unsigned long mask = dma_get_seg_boundary(dev);
int i;+ if (unlikely(handle_deferred_device(dev, domain)))
+ return 0;
+
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
iommu_dma_sync_sg_for_device(dev, sg, nents, dir);Signature · 1 line
2.20.0
Quoted 24 lines · Christoph Hellwig
Quoted 5 lines · Tom Murphy
(quoted diff) +5
-0+static int handle_deferred_device(struct device *dev) +{ + struct iommu_domain *domain; + const struct iommu_ops *ops; +Review
Quoted 10 lines
(quoted diff) +8
-1+ if (!is_kdump_kernel()) + return 0; + + domain = iommu_get_domain_for_dev(dev); - dma_handle =__iommu_dma_map(dev, phys, size, + if (unlikely(handle_deferred_device(dev))) + return DMA_MAPPING_ERROR; + + dma_handle = __iommu_dma_map(dev, phys, size,
__iommu_dma_mapalready looks up the domain, and as far as I can tell all callers need thehandle_deferred_devicecall. Should we just move it to there and pass the domain from the caller?Also shouldn't the
iommu_attach_devicecall insidehandle_deferred_devicealso get an unlikely marker?
PATCH v3 3/4 iommu/dma-iommu: Use the dev->coherent_dma_mask
In reply to Tom MurphyUse 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
@@ -442,7 +442,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_mask) {struct iommu_dma_cookie *cookie = domain->iova_cookie;size_t iova_off =0 ;
@@ -453,7 +454,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_mask, dev);if (!iova)return DMA_MAPPING_ERROR ;
@@ -496,7 +497,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);
@@ -766,7 +767,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 ;
@@ -857,7 +858,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);
@@ -1067,7 +1068,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,
@@ -1246,7 +1247,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
PATCH v3 4/4 iommu/amd: Convert AMD iommu driver to the dma-iommu api
In reply to Tom MurphyConvert 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
@@ -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
Joerg Roedel
maintainerHi Tom,
Quoted 2 lines · 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.
Thank you for your work on this! I appreciate that much, but I am not sure we are ready to make that move for the AMD and Intel IOMMU drivers yet.
My main concern right now is that these changes will add a per-page table lock into the fast-path for dma-mapping operations. There has been much work in the past to remove all locking from these code-paths and make it scalable on x86.
The dma-ops implementations in the x86 IOMMU drivers have the benefit that they can call their page-table manipulation functions directly and without locks, because they can make the necessary assumptions. The IOMMU-API mapping/unmapping path can't make these assumptions because it is also used for non-DMA-API use-cases.
So before we can move the AMD and Intel drivers to the generic DMA-API implementation we need to solve this problem to not introduce new scalability regressions.
Regards,
Joerg
Quoted 7 lines · Joerg Roedel
Hi Tom,
Quoted 2 lines · 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.
Quoted 8 lines
Thank you for your work on this! I appreciate that much, but I am not sure we are ready to make that move for the AMD and Intel IOMMU drivers yet.
My main concern right now is that these changes will add a per-page table lock into the fast-path for dma-mapping operations. There has been much work in the past to remove all locking from these code-paths and make it scalable on x86.
Where is the locking introduced? intel doesn't use a lock in it's iommu_map function:
https://github.com/torvalds/linux/blob/f2c7c76c5d0a443053e94adb9f0918fa2fb85c3a/drivers/iommu/intel-iommu.c#L5302
because it cleverly uses cmpxchg64 to avoid using locks:
https://github.com/torvalds/linux/blob/f2c7c76c5d0a443053e94adb9f0918fa2fb85c3a/drivers/iommu/intel-iommu.c#L900
And the locking in AMD's iommu_map function can be removed (and i have removed it in my patch set) because it does that same thing as intel:
https://github.com/torvalds/linux/blob/f2c7c76c5d0a443053e94adb9f0918fa2fb85c3a/drivers/iommu/amd_iommu.c#L1486
Is there something I'm missing?
Quoted 15 lines
The dma-ops implementations in the x86 IOMMU drivers have the benefit that they can call their page-table manipulation functions directly and without locks, because they can make the necessary assumptions. The IOMMU-API mapping/unmapping path can't make these assumptions because it is also used for non-DMA-API use-cases.
So before we can move the AMD and Intel drivers to the generic DMA-API implementation we need to solve this problem to not introduce new scalability regressions.
Regards,
Joerg