Skip to content

Commit da33e87

Browse files
rmurphy-armjoergroedel
authored andcommitted
iommu: Handle yet another race around registration
Next up on our list of race windows to close is another one during iommu_device_register() - it's now OK again for multiple instances to run their bus_iommu_probe() in parallel, but an iommu_probe_device() can still also race against a running bus_iommu_probe(). As Johan has managed to prove, this has now become a lot more visible on DT platforms wth driver_async_probe where a client driver is attempting to probe in parallel with its IOMMU driver - although commit b46064a ("iommu: Handle race with default domain setup") resolves this from the client driver's point of view, this isn't before of_iommu_configure() has had the chance to attempt to "replay" a probe that the bus walk hasn't even tried yet, and so still cause the out-of-order group allocation behaviour that we're trying to clean up (and now warning about). The most reliable thing to do here is to explicitly keep track of the "iommu_device_register() is still running" state, so we can then special-case the ops lookup for the replay path (based on dev->iommu again) to let that think it's still waiting for the IOMMU driver to appear at all. This still leaves the longstanding theoretical case of iommu_bus_notifier() being triggered during bus_iommu_probe(), but it's not so simple to defer a notifier, and nobody's ever reported that being a visible issue, so let's quietly kick that can down the road for now... Reported-by: Johan Hovold <johan@kernel.org> Fixes: bcb81ac ("iommu: Get DT/ACPI parsing into the proper probe path") Signed-off-by: Robin Murphy <robin.murphy@arm.com> Link: https://lore.kernel.org/r/88d54c1b48fed8279aa47d30f3d75173685bb26a.1745516488.git.robin.murphy@arm.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 6f73401 commit da33e87

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

drivers/iommu/iommu.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ int iommu_device_register(struct iommu_device *iommu,
277277
err = bus_iommu_probe(iommu_buses[i]);
278278
if (err)
279279
iommu_device_unregister(iommu);
280+
else
281+
WRITE_ONCE(iommu->ready, true);
280282
return err;
281283
}
282284
EXPORT_SYMBOL_GPL(iommu_device_register);
@@ -2846,31 +2848,39 @@ bool iommu_default_passthrough(void)
28462848
}
28472849
EXPORT_SYMBOL_GPL(iommu_default_passthrough);
28482850

2849-
const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode)
2851+
static const struct iommu_device *iommu_from_fwnode(const struct fwnode_handle *fwnode)
28502852
{
2851-
const struct iommu_ops *ops = NULL;
2852-
struct iommu_device *iommu;
2853+
const struct iommu_device *iommu, *ret = NULL;
28532854

28542855
spin_lock(&iommu_device_lock);
28552856
list_for_each_entry(iommu, &iommu_device_list, list)
28562857
if (iommu->fwnode == fwnode) {
2857-
ops = iommu->ops;
2858+
ret = iommu;
28582859
break;
28592860
}
28602861
spin_unlock(&iommu_device_lock);
2861-
return ops;
2862+
return ret;
2863+
}
2864+
2865+
const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode)
2866+
{
2867+
const struct iommu_device *iommu = iommu_from_fwnode(fwnode);
2868+
2869+
return iommu ? iommu->ops : NULL;
28622870
}
28632871

28642872
int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode)
28652873
{
2866-
const struct iommu_ops *ops = iommu_ops_from_fwnode(iommu_fwnode);
2874+
const struct iommu_device *iommu = iommu_from_fwnode(iommu_fwnode);
28672875
struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
28682876

2869-
if (!ops)
2877+
if (!iommu)
28702878
return driver_deferred_probe_check_state(dev);
2879+
if (!dev->iommu && !READ_ONCE(iommu->ready))
2880+
return -EPROBE_DEFER;
28712881

28722882
if (fwspec)
2873-
return ops == iommu_fwspec_ops(fwspec) ? 0 : -EINVAL;
2883+
return iommu->ops == iommu_fwspec_ops(fwspec) ? 0 : -EINVAL;
28742884

28752885
if (!dev_iommu_get(dev))
28762886
return -ENOMEM;

include/linux/iommu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ struct iommu_domain_ops {
746746
* @dev: struct device for sysfs handling
747747
* @singleton_group: Used internally for drivers that have only one group
748748
* @max_pasids: number of supported PASIDs
749+
* @ready: set once iommu_device_register() has completed successfully
749750
*/
750751
struct iommu_device {
751752
struct list_head list;
@@ -754,6 +755,7 @@ struct iommu_device {
754755
struct device *dev;
755756
struct iommu_group *singleton_group;
756757
u32 max_pasids;
758+
bool ready;
757759
};
758760

759761
/**

0 commit comments

Comments
 (0)