Skip to contentSkip to conversation

PCI: Fix driver_managed_dma check

PATCH Applied Robin Murphy 3 participants 3 messages Apr 25, 2025 – Jul 15, 2025 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Fix driver_managed_dma check

Since it's not currently safe to take device_lock() in the IOMMU probe path, that can race against really_probe() setting dev->driver before attempting to bind. The race itself isn't so bad, since we're only concerned with dereferencing dev->driver itself anyway, but sadly my attempt to implement the check with minimal churn leads to a kind of TOCTOU issue, where dev->driver becomes valid after to_pci_driver(NULL) is already computed, and thus the check fails to work as intended.

Will and I both hit this with the platform bus, but the pattern here is the same, so fix it for correctness too.

Fixes bcb81ac6ae3c ("iommu: Get DT/ACPI parsing into the proper probe path")
---
 drivers/pci/pci-driver.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pci/pci-driver.c +3 -3
index c8bd71a739f7..66e3bea7dc1a 100644
@@ -1634,7 +1634,7 @@ static int pci_bus_num_vf(struct device *dev)
  */
 static int pci_dma_configure(struct device *dev)
 {
-	struct pci_driver *driver = to_pci_driver(dev->driver);
+	const struct device_driver *drv = READ_ONCE(dev->driver);
 	struct device *bridge;
 	int ret = 0;
 
@@ -1651,8 +1651,8 @@ static int pci_dma_configure(struct device *dev)
 
 	pci_put_host_bridge_device(bridge);
 
-	/* @driver may not be valid when we're called from the IOMMU layer */
-	if (!ret && dev->driver && !driver->driver_managed_dma) {
+	/* @drv may not be valid when we're called from the IOMMU layer */
+	if (!ret && drv && !to_pci_driver(drv)->driver_managed_dma) {
 		ret = iommu_device_use_default_domain(dev);
 		if (ret)
 			arch_teardown_dma_ops(dev);
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Robin Murphy

Since it's not currently safe to take device_lock() in the IOMMU probe path, that can race against really_probe() setting dev->driver before attempting to bind. The race itself isn't so bad, since we're only concerned with dereferencing dev->driver itself anyway, but sadly my attempt to implement the check with minimal churn leads to a kind of TOCTOU issue, where dev->driver becomes valid after to_pci_driver(NULL) is already computed, and thus the check fails to work as intended.

Will and I both hit this with the platform bus, but the pattern here is the same, so fix it for correctness too.

Thanks!

Quoted 35 lines
Fixes bcb81ac6ae3c ("iommu: Get DT/ACPI parsing into the proper probe path")
---
 drivers/pci/pci-driver.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pci/pci-driver.c +3 -3
index c8bd71a739f7..66e3bea7dc1a 100644
@@ -1634,7 +1634,7 @@ static int pci_bus_num_vf(struct device *dev)
  */
 static int pci_dma_configure(struct device *dev)
 {
-	struct pci_driver *driver = to_pci_driver(dev->driver);
+	const struct device_driver *drv = READ_ONCE(dev->driver);
 	struct device *bridge;
 	int ret = 0;
 
@@ -1651,8 +1651,8 @@ static int pci_dma_configure(struct device *dev)
 
 	pci_put_host_bridge_device(bridge);
 
-	/* @driver may not be valid when we're called from the IOMMU layer */
-	if (!ret && dev->driver && !driver->driver_managed_dma) {
+	/* @drv may not be valid when we're called from the IOMMU layer */
+	if (!ret && drv && !to_pci_driver(drv)->driver_managed_dma) {
 		ret = iommu_device_use_default_domain(dev);
 		if (ret)
 			arch_teardown_dma_ops(dev);
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
#2 lore raw
In reply to Robin Murphy
Quoted 8 lines · Robin Murphy

Since it's not currently safe to take device_lock() in the IOMMU probe path, that can race against really_probe() setting dev->driver before attempting to bind. The race itself isn't so bad, since we're only concerned with dereferencing dev->driver itself anyway, but sadly my attempt to implement the check with minimal churn leads to a kind of TOCTOU issue, where dev->driver becomes valid after to_pci_driver(NULL) is already computed, and thus the check fails to work as intended.

Quoted 6 lines

Will and I both hit this with the platform bus, but the pattern here is the same, so fix it for correctness too.

Fixes bcb81ac6ae3c ("iommu: Get DT/ACPI parsing into the proper probe path")

Applied to pci/iommu for v6.17, thanks!

Quoted 31 lines
---
 drivers/pci/pci-driver.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pci/pci-driver.c +3 -3
index c8bd71a739f7..66e3bea7dc1a 100644
@@ -1634,7 +1634,7 @@ static int pci_bus_num_vf(struct device *dev)
  */
 static int pci_dma_configure(struct device *dev)
 {
-	struct pci_driver *driver = to_pci_driver(dev->driver);
+	const struct device_driver *drv = READ_ONCE(dev->driver);
 	struct device *bridge;
 	int ret = 0;
 
@@ -1651,8 +1651,8 @@ static int pci_dma_configure(struct device *dev)
 
 	pci_put_host_bridge_device(bridge);
 
-	/* @driver may not be valid when we're called from the IOMMU layer */
-	if (!ret && dev->driver && !driver->driver_managed_dma) {
+	/* @drv may not be valid when we're called from the IOMMU layer */
+	if (!ret && drv && !to_pci_driver(drv)->driver_managed_dma) {
 		ret = iommu_device_use_default_domain(dev);
 		if (ret)
 			arch_teardown_dma_ops(dev);
Signature · 1 line
2.39.2.101.g768bb238c484.dirty