Skip to content

Commit 01657bc

Browse files
rmurphy-armjoergroedel
authored andcommitted
iommu: Avoid races around device probe
We currently have 3 different ways that __iommu_probe_device() may be called, but no real guarantee that multiple callers can't tread on each other, especially once asynchronous driver probe gets involved. It would likely have taken a fair bit of luck to hit this previously, but commit 57365a0 ("iommu: Move bus setup to IOMMU device registration") ups the odds since now it's not just omap-iommu that may trigger multiple bus_iommu_probe() calls in parallel if probing asynchronously. Add a lock to ensure we can't try to double-probe a device, and also close some possible race windows to make sure we're truly robust against trying to double-initialise a group via two different member devices. Reported-by: Brian Norris <[email protected]> Signed-off-by: Robin Murphy <[email protected]> Tested-by: Brian Norris <[email protected]> Fixes: 57365a0 ("iommu: Move bus setup to IOMMU device registration") Link: https://lore.kernel.org/r/1946ef9f774851732eed78760a78ec40dbc6d178.1667591503.git.robin.murphy@arm.com Signed-off-by: Joerg Roedel <[email protected]>
1 parent 69e61ed commit 01657bc

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

drivers/iommu/iommu.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,23 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
306306
const struct iommu_ops *ops = dev->bus->iommu_ops;
307307
struct iommu_device *iommu_dev;
308308
struct iommu_group *group;
309+
static DEFINE_MUTEX(iommu_probe_device_lock);
309310
int ret;
310311

311312
if (!ops)
312313
return -ENODEV;
313-
314-
if (!dev_iommu_get(dev))
315-
return -ENOMEM;
314+
/*
315+
* Serialise to avoid races between IOMMU drivers registering in
316+
* parallel and/or the "replay" calls from ACPI/OF code via client
317+
* driver probe. Once the latter have been cleaned up we should
318+
* probably be able to use device_lock() here to minimise the scope,
319+
* but for now enforcing a simple global ordering is fine.
320+
*/
321+
mutex_lock(&iommu_probe_device_lock);
322+
if (!dev_iommu_get(dev)) {
323+
ret = -ENOMEM;
324+
goto err_unlock;
325+
}
316326

317327
if (!try_module_get(ops->owner)) {
318328
ret = -EINVAL;
@@ -333,11 +343,14 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
333343
ret = PTR_ERR(group);
334344
goto out_release;
335345
}
336-
iommu_group_put(group);
337346

347+
mutex_lock(&group->mutex);
338348
if (group_list && !group->default_domain && list_empty(&group->entry))
339349
list_add_tail(&group->entry, group_list);
350+
mutex_unlock(&group->mutex);
351+
iommu_group_put(group);
340352

353+
mutex_unlock(&iommu_probe_device_lock);
341354
iommu_device_link(iommu_dev, dev);
342355

343356
return 0;
@@ -352,6 +365,9 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
352365
err_free:
353366
dev_iommu_free(dev);
354367

368+
err_unlock:
369+
mutex_unlock(&iommu_probe_device_lock);
370+
355371
return ret;
356372
}
357373

@@ -1824,11 +1840,11 @@ int bus_iommu_probe(struct bus_type *bus)
18241840
return ret;
18251841

18261842
list_for_each_entry_safe(group, next, &group_list, entry) {
1843+
mutex_lock(&group->mutex);
1844+
18271845
/* Remove item from the list */
18281846
list_del_init(&group->entry);
18291847

1830-
mutex_lock(&group->mutex);
1831-
18321848
/* Try to allocate default domain */
18331849
probe_alloc_default_domain(bus, group);
18341850

0 commit comments

Comments
 (0)