]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iommu: Get DT/ACPI parsing into the proper probe path
authorRobin Murphy <robin.murphy@arm.com>
Fri, 28 Feb 2025 15:46:33 +0000 (15:46 +0000)
committerJoerg Roedel <jroedel@suse.de>
Tue, 11 Mar 2025 13:05:43 +0000 (14:05 +0100)
In hindsight, there were some crucial subtleties overlooked when moving
{of,acpi}_dma_configure() to driver probe time to allow waiting for
IOMMU drivers with -EPROBE_DEFER, and these have become an
ever-increasing source of problems. The IOMMU API has some fundamental
assumptions that iommu_probe_device() is called for every device added
to the system, in the order in which they are added. Calling it in a
random order or not at all dependent on driver binding leads to
malformed groups, a potential lack of isolation for devices with no
driver, and all manner of unexpected concurrency and race conditions.
We've attempted to mitigate the latter with point-fix bodges like
iommu_probe_device_lock, but it's a losing battle and the time has come
to bite the bullet and address the true source of the problem instead.

The crux of the matter is that the firmware parsing actually serves two
distinct purposes; one is identifying the IOMMU instance associated with
a device so we can check its availability, the second is actually
telling that instance about the relevant firmware-provided data for the
device. However the latter also depends on the former, and at the time
there was no good place to defer and retry that separately from the
availability check we also wanted for client driver probe.

Nowadays, though, we have a proper notion of multiple IOMMU instances in
the core API itself, and each one gets a chance to probe its own devices
upon registration, so we can finally make that work as intended for
DT/IORT/VIOT platforms too. All we need is for iommu_probe_device() to
be able to run the iommu_fwspec machinery currently buried deep in the
wrong end of {of,acpi}_dma_configure(). Luckily it turns out to be
surprisingly straightforward to bootstrap this transformation by pretty
much just calling the same path twice. At client driver probe time,
dev->driver is obviously set; conversely at device_add(), or a
subsequent bus_iommu_probe(), any device waiting for an IOMMU really
should *not* have a driver already, so we can use that as a condition to
disambiguate the two cases, and avoid recursing back into the IOMMU core
at the wrong times.

Obviously this isn't the nicest thing, but for now it gives us a
functional baseline to then unpick the layers in between without many
more awkward cross-subsystem patches. There are some minor side-effects
like dma_range_map potentially being created earlier, and some debug
prints being repeated, but these aren't significantly detrimental. Let's
make things work first, then deal with making them nice.

With the basic flow finally in the right order again, the next step is
probably turning the bus->dma_configure paths inside-out, since all we
really need from bus code is its notion of which device and input ID(s)
to parse the common firmware properties with...

Acked-by: Bjorn Helgaas <bhelgaas@google.com> # pci-driver.c
Acked-by: Rob Herring (Arm) <robh@kernel.org> # of/device.c
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Reviewed-by: Lorenzo Pieralisi <lpieralisi@kernel.org>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/e3b191e6fd6ca9a1e84c5e5e40044faf97abb874.1740753261.git.robin.murphy@arm.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/acpi/arm64/dma.c
drivers/acpi/scan.c
drivers/amba/bus.c
drivers/base/platform.c
drivers/bus/fsl-mc/fsl-mc-bus.c
drivers/cdx/cdx.c
drivers/iommu/iommu.c
drivers/iommu/of_iommu.c
drivers/of/device.c
drivers/pci/pci-driver.c

index 52b2abf88689824bad7d08ae83515cb5b2ee1502..f30f138352b7bb586b35d0c279fe24ec034d6075 100644 (file)
@@ -26,6 +26,11 @@ void acpi_arch_dma_setup(struct device *dev)
        else
                end = (1ULL << 32) - 1;
 
+       if (dev->dma_range_map) {
+               dev_dbg(dev, "dma_range_map already set\n");
+               return;
+       }
+
        ret = acpi_dma_get_range(dev, &map);
        if (!ret && map) {
                end = dma_range_map_max(map);
index 9f4efa8f75a61e363593ce90cbe1fff7bb6a9616..fb1fe9f3b1a366196b814a35900921637ebc4656 100644 (file)
@@ -1632,13 +1632,6 @@ static int acpi_iommu_configure_id(struct device *dev, const u32 *id_in)
                err = viot_iommu_configure(dev);
        mutex_unlock(&iommu_probe_device_lock);
 
-       /*
-        * If we have reason to believe the IOMMU driver missed the initial
-        * iommu_probe_device() call for dev, replay it to get things in order.
-        */
-       if (!err && dev->bus)
-               err = iommu_probe_device(dev);
-
        return err;
 }
 
index 8ef259b4d0378a27cccc2183e7e0cab62dae2342..71482d639a6dae1d62b31c8a210b83d88c24e305 100644 (file)
@@ -364,7 +364,8 @@ static int amba_dma_configure(struct device *dev)
                ret = acpi_dma_configure(dev, attr);
        }
 
-       if (!ret && !drv->driver_managed_dma) {
+       /* @drv may not be valid when we're called from the IOMMU layer */
+       if (!ret && dev->driver && !drv->driver_managed_dma) {
                ret = iommu_device_use_default_domain(dev);
                if (ret)
                        arch_teardown_dma_ops(dev);
index 6f2a33722c5203ac196a6e36e153648d0fe6c6d4..1813cfd0c4bdf4153662e650d8aaa32de69fcf76 100644 (file)
@@ -1451,7 +1451,8 @@ static int platform_dma_configure(struct device *dev)
                attr = acpi_get_dma_attr(to_acpi_device_node(fwnode));
                ret = acpi_dma_configure(dev, attr);
        }
-       if (ret || drv->driver_managed_dma)
+       /* @drv may not be valid when we're called from the IOMMU layer */
+       if (ret || !dev->driver || drv->driver_managed_dma)
                return ret;
 
        ret = iommu_device_use_default_domain(dev);
index d1f3d327ddd15d3f2a08fad3e79d63a888c7bac5..a8be8cf246fb6f044aa4a24c555394330ab4e3f7 100644 (file)
@@ -153,7 +153,8 @@ static int fsl_mc_dma_configure(struct device *dev)
        else
                ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
 
-       if (!ret && !mc_drv->driver_managed_dma) {
+       /* @mc_drv may not be valid when we're called from the IOMMU layer */
+       if (!ret && dev->driver && !mc_drv->driver_managed_dma) {
                ret = iommu_device_use_default_domain(dev);
                if (ret)
                        arch_teardown_dma_ops(dev);
index c573ed2ee71a80a8a94197bad58a4af4cb9d882e..780fb0c4adbae913fd98e2bcbfcee8c057af2a60 100644 (file)
@@ -360,7 +360,8 @@ static int cdx_dma_configure(struct device *dev)
                return ret;
        }
 
-       if (!ret && !cdx_drv->driver_managed_dma) {
+       /* @cdx_drv may not be valid when we're called from the IOMMU layer */
+       if (!ret && dev->driver && !cdx_drv->driver_managed_dma) {
                ret = iommu_device_use_default_domain(dev);
                if (ret)
                        arch_teardown_dma_ops(dev);
index b0be5c168a435a612747a8db4f3e5fdf0724db50..c283721579b33a7beb3f6477df15d6791e018ba8 100644 (file)
@@ -417,9 +417,21 @@ static int iommu_init_device(struct device *dev)
        if (!dev_iommu_get(dev))
                return -ENOMEM;
        /*
-        * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU
-        * instances with non-NULL fwnodes, and client devices should have been
-        * identified with a fwspec by this point. Otherwise, we can currently
+        * For FDT-based systems and ACPI IORT/VIOT, the common firmware parsing
+        * is buried in the bus dma_configure path. Properly unpicking that is
+        * still a big job, so for now just invoke the whole thing. The device
+        * already having a driver bound means dma_configure has already run and
+        * either found no IOMMU to wait for, or we're in its replay call right
+        * now, so either way there's no point calling it again.
+        */
+       if (!dev->driver && dev->bus->dma_configure) {
+               mutex_unlock(&iommu_probe_device_lock);
+               dev->bus->dma_configure(dev);
+               mutex_lock(&iommu_probe_device_lock);
+       }
+       /*
+        * At this point, relevant devices either now have a fwspec which will
+        * match ops registered with a non-NULL fwnode, or we can reasonably
         * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can
         * be present, and that any of their registered instances has suitable
         * ops for probing, and thus cheekily co-opt the same mechanism.
@@ -429,6 +441,12 @@ static int iommu_init_device(struct device *dev)
                ret = -ENODEV;
                goto err_free;
        }
+       /*
+        * And if we do now see any replay calls, they would indicate someone
+        * misusing the dma_configure path outside bus code.
+        */
+       if (dev->driver)
+               dev_WARN(dev, "late IOMMU probe at driver bind, something fishy here!\n");
 
        if (!try_module_get(ops->owner)) {
                ret = -EINVAL;
index e10a68b5ffde1359f948f755c79ad6d90b78d740..6b989a62def20ecafd833f00a3a92ce8dca192e0 100644 (file)
@@ -155,7 +155,12 @@ int of_iommu_configure(struct device *dev, struct device_node *master_np,
                dev_iommu_free(dev);
        mutex_unlock(&iommu_probe_device_lock);
 
-       if (!err && dev->bus)
+       /*
+        * If we're not on the iommu_probe_device() path (as indicated by the
+        * initial dev->iommu) then try to simulate it. This should no longer
+        * happen unless of_dma_configure() is being misused outside bus code.
+        */
+       if (!err && dev->bus && !dev_iommu_present)
                err = iommu_probe_device(dev);
 
        if (err && err != -EPROBE_DEFER)
index edf3be1972658f6dc165f577da53b10c7eebc116..5053e5d532cc385e91c4230da17166191422bd52 100644 (file)
@@ -99,6 +99,11 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
        bool coherent, set_map = false;
        int ret;
 
+       if (dev->dma_range_map) {
+               dev_dbg(dev, "dma_range_map already set\n");
+               goto skip_map;
+       }
+
        if (np == dev->of_node)
                bus_np = __of_get_dma_parent(np);
        else
@@ -119,7 +124,7 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
                end = dma_range_map_max(map);
                set_map = true;
        }
-
+skip_map:
        /*
         * If @dev is expected to be DMA-capable then the bus code that created
         * it should have initialised its dma_mask pointer by this point. For
index f57ea36d125d6eecfb60a6dc84d189cf9ed9b423..4468b7327cabe4c2e93dbd6ca188784534b78605 100644 (file)
@@ -1653,7 +1653,8 @@ static int pci_dma_configure(struct device *dev)
 
        pci_put_host_bridge_device(bridge);
 
-       if (!ret && !driver->driver_managed_dma) {
+       /* @driver may not be valid when we're called from the IOMMU layer */
+       if (!ret && dev->driver && !driver->driver_managed_dma) {
                ret = iommu_device_use_default_domain(dev);
                if (ret)
                        arch_teardown_dma_ops(dev);