summaryrefslogtreecommitdiff
path: root/drivers/iommu/iommu.c
diff options
context:
space:
mode:
authorJason Gunthorpe <jgg@nvidia.com>2023-06-06 03:59:48 +0300
committerJoerg Roedel <jroedel@suse.de>2023-07-14 17:14:16 +0300
commitf188056352bcdcd4bec31c22baa00ba6568f1416 (patch)
treec8f0c5dbfe89dfad99da666d67cb201dec0c0d16 /drivers/iommu/iommu.c
parentfa08280364882c42993dc5d8394c467d76803fdb (diff)
downloadlinux-f188056352bcdcd4bec31c22baa00ba6568f1416.tar.xz
iommu: Avoid locking/unlocking for iommu_probe_device()
Remove the race where a hotplug of a device into an existing group will have the device installed in the group->devices, but not yet attached to the group's current domain. Move the group attachment logic from iommu_probe_device() and put it under the same mutex that updates the group->devices list so everything is atomic under the lock. We retain the two step setup of the default domain for the bus_iommu_probe() case solely so that we have a more complete view of the group when creating the default domain for boot time devices. This is not generally necessary with the current code structure but seems to be supporting some odd corner cases like alias RID's and IOMMU_RESV_DIRECT or driver bugs returning different default_domain types for the same group. During bus_iommu_probe() the group will have a device list but both group->default_domain and group->domain will be NULL. Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/10-v3-328044aa278c+45e49-iommu_probe_jgg@nvidia.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
Diffstat (limited to 'drivers/iommu/iommu.c')
-rw-r--r--drivers/iommu/iommu.c78
1 files changed, 35 insertions, 43 deletions
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 3129e5c50c87..42377f49ab87 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -131,6 +131,8 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
const char *buf, size_t count);
static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
struct device *dev);
+static void __iommu_group_free_device(struct iommu_group *group,
+ struct group_device *grp_dev);
#define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
struct iommu_group_attribute iommu_group_attr_##_name = \
@@ -469,14 +471,39 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
goto err_put_group;
}
+ /*
+ * The gdev must be in the list before calling
+ * iommu_setup_default_domain()
+ */
list_add_tail(&gdev->list, &group->devices);
- if (group_list && !group->default_domain && list_empty(&group->entry))
- list_add_tail(&group->entry, group_list);
+ WARN_ON(group->default_domain && !group->domain);
+ if (group->default_domain)
+ iommu_create_device_direct_mappings(group->default_domain, dev);
+ if (group->domain) {
+ ret = __iommu_device_set_domain(group, dev, group->domain, 0);
+ if (ret)
+ goto err_remove_gdev;
+ } else if (!group->default_domain && !group_list) {
+ ret = iommu_setup_default_domain(group, 0);
+ if (ret)
+ goto err_remove_gdev;
+ } else if (!group->default_domain) {
+ /*
+ * With a group_list argument we defer the default_domain setup
+ * to the caller by providing a de-duplicated list of groups
+ * that need further setup.
+ */
+ if (list_empty(&group->entry))
+ list_add_tail(&group->entry, group_list);
+ }
mutex_unlock(&group->mutex);
mutex_unlock(&iommu_probe_device_lock);
return 0;
+err_remove_gdev:
+ list_del(&gdev->list);
+ __iommu_group_free_device(group, gdev);
err_put_group:
iommu_deinit_device(dev);
mutex_unlock(&group->mutex);
@@ -490,52 +517,17 @@ out_unlock:
int iommu_probe_device(struct device *dev)
{
const struct iommu_ops *ops;
- struct iommu_group *group;
int ret;
ret = __iommu_probe_device(dev, NULL);
if (ret)
- goto err_out;
-
- group = iommu_group_get(dev);
- if (!group) {
- ret = -ENODEV;
- goto err_release;
- }
-
- mutex_lock(&group->mutex);
-
- if (group->default_domain)
- iommu_create_device_direct_mappings(group->default_domain, dev);
-
- if (group->domain) {
- ret = __iommu_device_set_domain(group, dev, group->domain, 0);
- if (ret)
- goto err_unlock;
- } else if (!group->default_domain) {
- ret = iommu_setup_default_domain(group, 0);
- if (ret)
- goto err_unlock;
- }
-
- mutex_unlock(&group->mutex);
- iommu_group_put(group);
+ return ret;
ops = dev_iommu_ops(dev);
if (ops->probe_finalize)
ops->probe_finalize(dev);
return 0;
-
-err_unlock:
- mutex_unlock(&group->mutex);
- iommu_group_put(group);
-err_release:
- iommu_release_device(dev);
-
-err_out:
- return ret;
-
}
static void __iommu_group_free_device(struct iommu_group *group,
@@ -1815,11 +1807,6 @@ int bus_iommu_probe(const struct bus_type *bus)
LIST_HEAD(group_list);
int ret;
- /*
- * This code-path does not allocate the default domain when
- * creating the iommu group, so do it after the groups are
- * created.
- */
ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
if (ret)
return ret;
@@ -1832,6 +1819,11 @@ int bus_iommu_probe(const struct bus_type *bus)
/* Remove item from the list */
list_del_init(&group->entry);
+ /*
+ * We go to the trouble of deferred default domain creation so
+ * that the cross-group default domain type and the setup of the
+ * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios.
+ */
ret = iommu_setup_default_domain(group, 0);
if (ret) {
mutex_unlock(&group->mutex);