Skip to content

Commit

Permalink
iommu: Avoid races around device probe
Browse files Browse the repository at this point in the history
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 <briannorris@chromium.org>
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Tested-by: Brian Norris <briannorris@chromium.org>
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 <jroedel@suse.de>
  • Loading branch information
Robin Murphy authored and Joerg Roedel committed Nov 19, 2022
1 parent 69e61ed commit 01657bc
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions drivers/iommu/iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,23 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
const struct iommu_ops *ops = dev->bus->iommu_ops;
struct iommu_device *iommu_dev;
struct iommu_group *group;
static DEFINE_MUTEX(iommu_probe_device_lock);
int ret;

if (!ops)
return -ENODEV;

if (!dev_iommu_get(dev))
return -ENOMEM;
/*
* Serialise to avoid races between IOMMU drivers registering in
* parallel and/or the "replay" calls from ACPI/OF code via client
* driver probe. Once the latter have been cleaned up we should
* probably be able to use device_lock() here to minimise the scope,
* but for now enforcing a simple global ordering is fine.
*/
mutex_lock(&iommu_probe_device_lock);
if (!dev_iommu_get(dev)) {
ret = -ENOMEM;
goto err_unlock;
}

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

mutex_lock(&group->mutex);
if (group_list && !group->default_domain && list_empty(&group->entry))
list_add_tail(&group->entry, group_list);
mutex_unlock(&group->mutex);
iommu_group_put(group);

mutex_unlock(&iommu_probe_device_lock);
iommu_device_link(iommu_dev, dev);

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

err_unlock:
mutex_unlock(&iommu_probe_device_lock);

return ret;
}

Expand Down Expand Up @@ -1824,11 +1840,11 @@ int bus_iommu_probe(struct bus_type *bus)
return ret;

list_for_each_entry_safe(group, next, &group_list, entry) {
mutex_lock(&group->mutex);

/* Remove item from the list */
list_del_init(&group->entry);

mutex_lock(&group->mutex);

/* Try to allocate default domain */
probe_alloc_default_domain(bus, group);

Expand Down

0 comments on commit 01657bc

Please sign in to comment.