Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iommu/dma: Centralise iommu_setup_dma_ops()

It's somewhat hard to see, but arm64's arch_setup_dma_ops() should only
ever call iommu_setup_dma_ops() after a successful iommu_probe_device(),
which means there should be no harm in achieving the same order of
operations by running it off the back of iommu_probe_device() itself.
This then puts it in line with the x86 and s390 .probe_finalize bodges,
letting us pull it all into the main flow properly. As a bonus this lets
us fold in and de-scope the PCI workaround setup as well.

At this point we can also then pull the call up inside the group mutex,
and avoid having to think about whether iommu_group_store_type() could
theoretically race and free the domain if iommu_setup_dma_ops() ran just
*before* iommu_device_use_default_domain() claims it... Furthermore we
replace one .probe_finalize call completely, since the only remaining
implementations are now one which only needs to run once for the initial
boot-time probe, and two which themselves render that path unreachable.

This leaves us a big step closer to realistically being able to unpick
the variety of different things that iommu_setup_dma_ops() has been
muddling together, and further streamline iommu-dma into core API flows
in future.

Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com> # For Intel IOMMU
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Tested-by: Hanjun Guo <guohanjun@huawei.com>
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Link: https://lore.kernel.org/r/bebea331c1d688b34d9862eefd5ede47503961b8.1713523152.git.robin.murphy@arm.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>

authored by

Robin Murphy and committed by
Joerg Roedel
b67483b3 ad4750b0

+19 -73
-2
arch/arm64/mm/dma-mapping.c
··· 58 58 ARCH_DMA_MINALIGN, cls); 59 59 60 60 dev->dma_coherent = coherent; 61 - if (device_iommu_mapped(dev)) 62 - iommu_setup_dma_ops(dev, dma_base, dma_base + size - 1); 63 61 64 62 xen_setup_dma_ops(dev); 65 63 }
-8
drivers/iommu/amd/iommu.c
··· 2175 2175 return iommu_dev; 2176 2176 } 2177 2177 2178 - static void amd_iommu_probe_finalize(struct device *dev) 2179 - { 2180 - /* Domains are initialized for this device - have a look what we ended up with */ 2181 - set_dma_ops(dev, NULL); 2182 - iommu_setup_dma_ops(dev, 0, U64_MAX); 2183 - } 2184 - 2185 2178 static void amd_iommu_release_device(struct device *dev) 2186 2179 { 2187 2180 struct amd_iommu *iommu; ··· 2777 2784 .domain_alloc_user = amd_iommu_domain_alloc_user, 2778 2785 .probe_device = amd_iommu_probe_device, 2779 2786 .release_device = amd_iommu_release_device, 2780 - .probe_finalize = amd_iommu_probe_finalize, 2781 2787 .device_group = amd_iommu_device_group, 2782 2788 .get_resv_regions = amd_iommu_get_resv_regions, 2783 2789 .is_attach_deferred = amd_iommu_is_attach_deferred,
+6 -12
drivers/iommu/dma-iommu.c
··· 1741 1741 .max_mapping_size = iommu_dma_max_mapping_size, 1742 1742 }; 1743 1743 1744 - /* 1745 - * The IOMMU core code allocates the default DMA domain, which the underlying 1746 - * IOMMU driver needs to support via the dma-iommu layer. 1747 - */ 1748 - void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit) 1744 + void iommu_setup_dma_ops(struct device *dev) 1749 1745 { 1750 1746 struct iommu_domain *domain = iommu_get_domain_for_dev(dev); 1751 1747 1752 - if (!domain) 1753 - goto out_err; 1748 + if (dev_is_pci(dev)) 1749 + dev->iommu->pci_32bit_workaround = !iommu_dma_forcedac; 1754 1750 1755 - /* 1756 - * The IOMMU core code allocates the default DMA domain, which the 1757 - * underlying IOMMU driver needs to support via the dma-iommu layer. 1758 - */ 1759 1751 if (iommu_is_dma_domain(domain)) { 1760 1752 if (iommu_dma_init_domain(domain, dev)) 1761 1753 goto out_err; 1762 1754 dev->dma_ops = &iommu_dma_ops; 1755 + } else if (dev->dma_ops == &iommu_dma_ops) { 1756 + /* Clean up if we've switched *from* a DMA domain */ 1757 + dev->dma_ops = NULL; 1763 1758 } 1764 1759 1765 1760 return; ··· 1762 1767 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n", 1763 1768 dev_name(dev)); 1764 1769 } 1765 - EXPORT_SYMBOL_GPL(iommu_setup_dma_ops); 1766 1770 1767 1771 static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev, 1768 1772 phys_addr_t msi_addr, struct iommu_domain *domain)
+6 -8
drivers/iommu/dma-iommu.h
··· 9 9 10 10 #ifdef CONFIG_IOMMU_DMA 11 11 12 + void iommu_setup_dma_ops(struct device *dev); 13 + 12 14 int iommu_get_dma_cookie(struct iommu_domain *domain); 13 15 void iommu_put_dma_cookie(struct iommu_domain *domain); 14 16 ··· 19 17 void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list); 20 18 21 19 extern bool iommu_dma_forcedac; 22 - static inline void iommu_dma_set_pci_32bit_workaround(struct device *dev) 23 - { 24 - dev->iommu->pci_32bit_workaround = !iommu_dma_forcedac; 25 - } 26 20 27 21 #else /* CONFIG_IOMMU_DMA */ 22 + 23 + static inline void iommu_setup_dma_ops(struct device *dev) 24 + { 25 + } 28 26 29 27 static inline int iommu_dma_init_fq(struct iommu_domain *domain) 30 28 { ··· 41 39 } 42 40 43 41 static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list) 44 - { 45 - } 46 - 47 - static inline void iommu_dma_set_pci_32bit_workaround(struct device *dev) 48 42 { 49 43 } 50 44
-7
drivers/iommu/intel/iommu.c
··· 4349 4349 set_dma_ops(dev, NULL); 4350 4350 } 4351 4351 4352 - static void intel_iommu_probe_finalize(struct device *dev) 4353 - { 4354 - set_dma_ops(dev, NULL); 4355 - iommu_setup_dma_ops(dev, 0, U64_MAX); 4356 - } 4357 - 4358 4352 static void intel_iommu_get_resv_regions(struct device *device, 4359 4353 struct list_head *head) 4360 4354 { ··· 4828 4834 .domain_alloc = intel_iommu_domain_alloc, 4829 4835 .domain_alloc_user = intel_iommu_domain_alloc_user, 4830 4836 .probe_device = intel_iommu_probe_device, 4831 - .probe_finalize = intel_iommu_probe_finalize, 4832 4837 .release_device = intel_iommu_release_device, 4833 4838 .get_resv_regions = intel_iommu_get_resv_regions, 4834 4839 .device_group = intel_iommu_device_group,
+7 -13
drivers/iommu/iommu.c
··· 581 581 if (list_empty(&group->entry)) 582 582 list_add_tail(&group->entry, group_list); 583 583 } 584 - mutex_unlock(&group->mutex); 585 584 586 - if (dev_is_pci(dev)) 587 - iommu_dma_set_pci_32bit_workaround(dev); 585 + if (group->default_domain) 586 + iommu_setup_dma_ops(dev); 587 + 588 + mutex_unlock(&group->mutex); 588 589 589 590 return 0; 590 591 ··· 1829 1828 mutex_unlock(&group->mutex); 1830 1829 return ret; 1831 1830 } 1831 + for_each_group_device(group, gdev) 1832 + iommu_setup_dma_ops(gdev->dev); 1832 1833 mutex_unlock(&group->mutex); 1833 1834 1834 1835 /* ··· 3069 3066 if (ret) 3070 3067 goto out_unlock; 3071 3068 3072 - /* 3073 - * Release the mutex here because ops->probe_finalize() call-back of 3074 - * some vendor IOMMU drivers calls arm_iommu_attach_device() which 3075 - * in-turn might call back into IOMMU core code, where it tries to take 3076 - * group->mutex, resulting in a deadlock. 3077 - */ 3078 - mutex_unlock(&group->mutex); 3079 - 3080 3069 /* Make sure dma_ops is appropriatley set */ 3081 3070 for_each_group_device(group, gdev) 3082 - iommu_group_do_probe_finalize(gdev->dev); 3083 - return count; 3071 + iommu_setup_dma_ops(gdev->dev); 3084 3072 3085 3073 out_unlock: 3086 3074 mutex_unlock(&group->mutex);
-6
drivers/iommu/s390-iommu.c
··· 695 695 return size; 696 696 } 697 697 698 - static void s390_iommu_probe_finalize(struct device *dev) 699 - { 700 - iommu_setup_dma_ops(dev, 0, U64_MAX); 701 - } 702 - 703 698 struct zpci_iommu_ctrs *zpci_get_iommu_ctrs(struct zpci_dev *zdev) 704 699 { 705 700 if (!zdev || !zdev->s390_domain) ··· 780 785 .capable = s390_iommu_capable, 781 786 .domain_alloc_paging = s390_domain_alloc_paging, 782 787 .probe_device = s390_iommu_probe_device, 783 - .probe_finalize = s390_iommu_probe_finalize, 784 788 .release_device = s390_iommu_release_device, 785 789 .device_group = generic_device_group, 786 790 .pgsize_bitmap = SZ_4K,
-10
drivers/iommu/virtio-iommu.c
··· 1025 1025 return ERR_PTR(ret); 1026 1026 } 1027 1027 1028 - static void viommu_probe_finalize(struct device *dev) 1029 - { 1030 - #ifndef CONFIG_ARCH_HAS_SETUP_DMA_OPS 1031 - /* First clear the DMA ops in case we're switching from a DMA domain */ 1032 - set_dma_ops(dev, NULL); 1033 - iommu_setup_dma_ops(dev, 0, U64_MAX); 1034 - #endif 1035 - } 1036 - 1037 1028 static void viommu_release_device(struct device *dev) 1038 1029 { 1039 1030 struct viommu_endpoint *vdev = dev_iommu_priv_get(dev); ··· 1064 1073 .capable = viommu_capable, 1065 1074 .domain_alloc = viommu_domain_alloc, 1066 1075 .probe_device = viommu_probe_device, 1067 - .probe_finalize = viommu_probe_finalize, 1068 1076 .release_device = viommu_release_device, 1069 1077 .device_group = viommu_device_group, 1070 1078 .get_resv_regions = viommu_get_resv_regions,
-7
include/linux/iommu.h
··· 1446 1446 #ifdef CONFIG_IOMMU_DMA 1447 1447 #include <linux/msi.h> 1448 1448 1449 - /* Setup call for arch DMA mapping code */ 1450 - void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit); 1451 - 1452 1449 int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base); 1453 1450 1454 1451 int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr); ··· 1455 1458 1456 1459 struct msi_desc; 1457 1460 struct msi_msg; 1458 - 1459 - static inline void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit) 1460 - { 1461 - } 1462 1461 1463 1462 static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) 1464 1463 {