Skip to content

Commit b67483b

Browse files
rmurphy-armjoergroedel
authored andcommitted
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>
1 parent ad4750b commit b67483b

File tree

9 files changed

+19
-73
lines changed

9 files changed

+19
-73
lines changed

arch/arm64/mm/dma-mapping.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
5858
ARCH_DMA_MINALIGN, cls);
5959

6060
dev->dma_coherent = coherent;
61-
if (device_iommu_mapped(dev))
62-
iommu_setup_dma_ops(dev, dma_base, dma_base + size - 1);
6361

6462
xen_setup_dma_ops(dev);
6563
}

drivers/iommu/amd/iommu.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,13 +2175,6 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
21752175
return iommu_dev;
21762176
}
21772177

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-
21852178
static void amd_iommu_release_device(struct device *dev)
21862179
{
21872180
struct amd_iommu *iommu;
@@ -2784,7 +2777,6 @@ const struct iommu_ops amd_iommu_ops = {
27842777
.domain_alloc_user = amd_iommu_domain_alloc_user,
27852778
.probe_device = amd_iommu_probe_device,
27862779
.release_device = amd_iommu_release_device,
2787-
.probe_finalize = amd_iommu_probe_finalize,
27882780
.device_group = amd_iommu_device_group,
27892781
.get_resv_regions = amd_iommu_get_resv_regions,
27902782
.is_attach_deferred = amd_iommu_is_attach_deferred,

drivers/iommu/dma-iommu.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,33 +1741,27 @@ static const struct dma_map_ops iommu_dma_ops = {
17411741
.max_mapping_size = iommu_dma_max_mapping_size,
17421742
};
17431743

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)
17491745
{
17501746
struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
17511747

1752-
if (!domain)
1753-
goto out_err;
1748+
if (dev_is_pci(dev))
1749+
dev->iommu->pci_32bit_workaround = !iommu_dma_forcedac;
17541750

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-
*/
17591751
if (iommu_is_dma_domain(domain)) {
17601752
if (iommu_dma_init_domain(domain, dev))
17611753
goto out_err;
17621754
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;
17631758
}
17641759

17651760
return;
17661761
out_err:
17671762
pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
17681763
dev_name(dev));
17691764
}
1770-
EXPORT_SYMBOL_GPL(iommu_setup_dma_ops);
17711765

17721766
static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
17731767
phys_addr_t msi_addr, struct iommu_domain *domain)

drivers/iommu/dma-iommu.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#ifdef CONFIG_IOMMU_DMA
1111

12+
void iommu_setup_dma_ops(struct device *dev);
13+
1214
int iommu_get_dma_cookie(struct iommu_domain *domain);
1315
void iommu_put_dma_cookie(struct iommu_domain *domain);
1416

@@ -17,13 +19,13 @@ int iommu_dma_init_fq(struct iommu_domain *domain);
1719
void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list);
1820

1921
extern bool iommu_dma_forcedac;
20-
static inline void iommu_dma_set_pci_32bit_workaround(struct device *dev)
21-
{
22-
dev->iommu->pci_32bit_workaround = !iommu_dma_forcedac;
23-
}
2422

2523
#else /* CONFIG_IOMMU_DMA */
2624

25+
static inline void iommu_setup_dma_ops(struct device *dev)
26+
{
27+
}
28+
2729
static inline int iommu_dma_init_fq(struct iommu_domain *domain)
2830
{
2931
return -EINVAL;
@@ -42,9 +44,5 @@ static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_he
4244
{
4345
}
4446

45-
static inline void iommu_dma_set_pci_32bit_workaround(struct device *dev)
46-
{
47-
}
48-
4947
#endif /* CONFIG_IOMMU_DMA */
5048
#endif /* __DMA_IOMMU_H */

drivers/iommu/intel/iommu.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4349,12 +4349,6 @@ static void intel_iommu_release_device(struct device *dev)
43494349
set_dma_ops(dev, NULL);
43504350
}
43514351

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-
43584352
static void intel_iommu_get_resv_regions(struct device *device,
43594353
struct list_head *head)
43604354
{
@@ -4834,7 +4828,6 @@ const struct iommu_ops intel_iommu_ops = {
48344828
.domain_alloc = intel_iommu_domain_alloc,
48354829
.domain_alloc_user = intel_iommu_domain_alloc_user,
48364830
.probe_device = intel_iommu_probe_device,
4837-
.probe_finalize = intel_iommu_probe_finalize,
48384831
.release_device = intel_iommu_release_device,
48394832
.get_resv_regions = intel_iommu_get_resv_regions,
48404833
.device_group = intel_iommu_device_group,

drivers/iommu/iommu.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,11 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
581581
if (list_empty(&group->entry))
582582
list_add_tail(&group->entry, group_list);
583583
}
584-
mutex_unlock(&group->mutex);
585584

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);
588589

589590
return 0;
590591

@@ -1828,6 +1829,8 @@ int bus_iommu_probe(const struct bus_type *bus)
18281829
mutex_unlock(&group->mutex);
18291830
return ret;
18301831
}
1832+
for_each_group_device(group, gdev)
1833+
iommu_setup_dma_ops(gdev->dev);
18311834
mutex_unlock(&group->mutex);
18321835

18331836
/*
@@ -3066,18 +3069,9 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
30663069
if (ret)
30673070
goto out_unlock;
30683071

3069-
/*
3070-
* Release the mutex here because ops->probe_finalize() call-back of
3071-
* some vendor IOMMU drivers calls arm_iommu_attach_device() which
3072-
* in-turn might call back into IOMMU core code, where it tries to take
3073-
* group->mutex, resulting in a deadlock.
3074-
*/
3075-
mutex_unlock(&group->mutex);
3076-
30773072
/* Make sure dma_ops is appropriatley set */
30783073
for_each_group_device(group, gdev)
3079-
iommu_group_do_probe_finalize(gdev->dev);
3080-
return count;
3074+
iommu_setup_dma_ops(gdev->dev);
30813075

30823076
out_unlock:
30833077
mutex_unlock(&group->mutex);

drivers/iommu/s390-iommu.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,6 @@ static size_t s390_iommu_unmap_pages(struct iommu_domain *domain,
695695
return size;
696696
}
697697

698-
static void s390_iommu_probe_finalize(struct device *dev)
699-
{
700-
iommu_setup_dma_ops(dev, 0, U64_MAX);
701-
}
702-
703698
struct zpci_iommu_ctrs *zpci_get_iommu_ctrs(struct zpci_dev *zdev)
704699
{
705700
if (!zdev || !zdev->s390_domain)
@@ -785,7 +780,6 @@ static const struct iommu_ops s390_iommu_ops = {
785780
.capable = s390_iommu_capable,
786781
.domain_alloc_paging = s390_domain_alloc_paging,
787782
.probe_device = s390_iommu_probe_device,
788-
.probe_finalize = s390_iommu_probe_finalize,
789783
.release_device = s390_iommu_release_device,
790784
.device_group = generic_device_group,
791785
.pgsize_bitmap = SZ_4K,

drivers/iommu/virtio-iommu.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,15 +1025,6 @@ static struct iommu_device *viommu_probe_device(struct device *dev)
10251025
return ERR_PTR(ret);
10261026
}
10271027

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-
10371028
static void viommu_release_device(struct device *dev)
10381029
{
10391030
struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
@@ -1073,7 +1064,6 @@ static struct iommu_ops viommu_ops = {
10731064
.capable = viommu_capable,
10741065
.domain_alloc = viommu_domain_alloc,
10751066
.probe_device = viommu_probe_device,
1076-
.probe_finalize = viommu_probe_finalize,
10771067
.release_device = viommu_release_device,
10781068
.device_group = viommu_device_group,
10791069
.get_resv_regions = viommu_get_resv_regions,

include/linux/iommu.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,9 +1446,6 @@ static inline void iommu_debugfs_setup(void) {}
14461446
#ifdef CONFIG_IOMMU_DMA
14471447
#include <linux/msi.h>
14481448

1449-
/* Setup call for arch DMA mapping code */
1450-
void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit);
1451-
14521449
int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base);
14531450

14541451
int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr);
@@ -1459,10 +1456,6 @@ void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg);
14591456
struct msi_desc;
14601457
struct msi_msg;
14611458

1462-
static inline void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit)
1463-
{
1464-
}
1465-
14661459
static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
14671460
{
14681461
return -ENODEV;

0 commit comments

Comments
 (0)