Commit fcbb0a4d authored by Jason Gunthorpe's avatar Jason Gunthorpe Committed by Joerg Roedel

iommu: Revise iommu_group_alloc_default_domain()

Robin points out that the fallback to guessing what domains the driver
supports should only happen if the driver doesn't return a preference from
its ops->def_domain_type().

Re-organize iommu_group_alloc_default_domain() so it internally uses
iommu_def_domain_type only during the fallback and makes it clearer how
the fallback sequence works.

Make iommu_group_alloc_default_domain() return the domain so the return
based logic is cleaner and to prepare for the next patch.

Remove the iommu_alloc_default_domain() function as it is now trivially
just calling iommu_group_alloc_default_domain().
Reviewed-by: default avatarLu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
Tested-by: default avatarHeiko Stuebner <heiko@sntech.de>
Tested-by: default avatarNiklas Schnelle <schnelle@linux.ibm.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/13-v5-1b99ae392328+44574-iommu_err_unwind_jgg@nvidia.comSigned-off-by: default avatarJoerg Roedel <jroedel@suse.de>
parent 8b4eb75e
...@@ -93,8 +93,9 @@ static const char * const iommu_group_resv_type_string[] = { ...@@ -93,8 +93,9 @@ static const char * const iommu_group_resv_type_string[] = {
static int iommu_bus_notifier(struct notifier_block *nb, static int iommu_bus_notifier(struct notifier_block *nb,
unsigned long action, void *data); unsigned long action, void *data);
static void iommu_release_device(struct device *dev); static void iommu_release_device(struct device *dev);
static int iommu_alloc_default_domain(struct iommu_group *group, static struct iommu_domain *
struct device *dev); iommu_group_alloc_default_domain(struct iommu_group *group, int req_type);
static int iommu_get_def_domain_type(struct device *dev);
static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus,
unsigned type); unsigned type);
static int __iommu_attach_device(struct iommu_domain *domain, static int __iommu_attach_device(struct iommu_domain *domain,
...@@ -434,7 +435,8 @@ int iommu_probe_device(struct device *dev) ...@@ -434,7 +435,8 @@ int iommu_probe_device(struct device *dev)
* support default domains, so the return value is not yet * support default domains, so the return value is not yet
* checked. * checked.
*/ */
iommu_alloc_default_domain(group, dev); group->default_domain = iommu_group_alloc_default_domain(
group, iommu_get_def_domain_type(dev));
if (group->default_domain) { if (group->default_domain) {
iommu_create_device_direct_mappings(group, dev); iommu_create_device_direct_mappings(group, dev);
ret = __iommu_group_set_domain(group, ret = __iommu_group_set_domain(group,
...@@ -1645,35 +1647,38 @@ static int iommu_get_def_domain_type(struct device *dev) ...@@ -1645,35 +1647,38 @@ static int iommu_get_def_domain_type(struct device *dev)
return 0; return 0;
} }
static int iommu_group_alloc_default_domain(const struct bus_type *bus, /*
struct iommu_group *group, * req_type of 0 means "auto" which means to select a domain based on
unsigned int type) * iommu_def_domain_type or what the driver actually supports.
*/
static struct iommu_domain *
iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
{ {
const struct bus_type *bus =
list_first_entry(&group->devices, struct group_device, list)
->dev->bus;
struct iommu_domain *dom; struct iommu_domain *dom;
dom = __iommu_domain_alloc(bus, type); lockdep_assert_held(&group->mutex);
if (!dom && type != IOMMU_DOMAIN_DMA) {
dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
if (dom)
pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
type, group->name);
}
if (!dom)
return -ENOMEM;
group->default_domain = dom; if (req_type)
return 0; return __iommu_domain_alloc(bus, req_type);
}
static int iommu_alloc_default_domain(struct iommu_group *group, /* The driver gave no guidance on what type to use, try the default */
struct device *dev) dom = __iommu_domain_alloc(bus, iommu_def_domain_type);
{ if (dom)
unsigned int type; return dom;
type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type; /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
return NULL;
dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
if (!dom)
return NULL;
return iommu_group_alloc_default_domain(dev->bus, group, type); pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
iommu_def_domain_type, group->name);
return dom;
} }
/** /**
...@@ -1785,15 +1790,12 @@ static int iommu_get_default_domain_type(struct iommu_group *group, ...@@ -1785,15 +1790,12 @@ static int iommu_get_default_domain_type(struct iommu_group *group,
"Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n", "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
iommu_domain_type_str(type), dev_name(last_dev), iommu_domain_type_str(type), dev_name(last_dev),
iommu_domain_type_str(best_type)); iommu_domain_type_str(best_type));
return iommu_def_domain_type; return 0;
} }
if (!best_type) if (!best_type)
best_type = type; best_type = type;
last_dev = gdev->dev; last_dev = gdev->dev;
} }
if (!best_type)
return iommu_def_domain_type;
return best_type; return best_type;
} }
...@@ -1850,9 +1852,8 @@ int bus_iommu_probe(const struct bus_type *bus) ...@@ -1850,9 +1852,8 @@ int bus_iommu_probe(const struct bus_type *bus)
list_del_init(&group->entry); list_del_init(&group->entry);
/* Try to allocate default domain */ /* Try to allocate default domain */
iommu_group_alloc_default_domain( group->default_domain = iommu_group_alloc_default_domain(
bus, group, iommu_get_default_domain_type(group, 0)); group, iommu_get_default_domain_type(group, 0));
if (!group->default_domain) { if (!group->default_domain) {
mutex_unlock(&group->mutex); mutex_unlock(&group->mutex);
continue; continue;
...@@ -2897,9 +2898,11 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, ...@@ -2897,9 +2898,11 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
group->domain = NULL; group->domain = NULL;
/* Sets group->default_domain to the newly allocated domain */ /* Sets group->default_domain to the newly allocated domain */
ret = iommu_group_alloc_default_domain(dev->bus, group, type); group->default_domain = iommu_group_alloc_default_domain(group, type);
if (ret) if (!group->default_domain) {
ret = -EINVAL;
goto restore_old_domain; goto restore_old_domain;
}
group->domain = prev_dom; group->domain = prev_dom;
ret = iommu_create_device_direct_mappings(group, dev); ret = iommu_create_device_direct_mappings(group, dev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment