Skip to content

Commit fcbb0a4

Browse files
jgunthorpejoergroedel
authored andcommitted
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: Lu Baolu <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Tested-by: Heiko Stuebner <[email protected]> Tested-by: Niklas Schnelle <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent 8b4eb75 commit fcbb0a4

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

drivers/iommu/iommu.c

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ static const char * const iommu_group_resv_type_string[] = {
9393
static int iommu_bus_notifier(struct notifier_block *nb,
9494
unsigned long action, void *data);
9595
static void iommu_release_device(struct device *dev);
96-
static int iommu_alloc_default_domain(struct iommu_group *group,
97-
struct device *dev);
96+
static struct iommu_domain *
97+
iommu_group_alloc_default_domain(struct iommu_group *group, int req_type);
98+
static int iommu_get_def_domain_type(struct device *dev);
9899
static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus,
99100
unsigned type);
100101
static int __iommu_attach_device(struct iommu_domain *domain,
@@ -434,7 +435,8 @@ int iommu_probe_device(struct device *dev)
434435
* support default domains, so the return value is not yet
435436
* checked.
436437
*/
437-
iommu_alloc_default_domain(group, dev);
438+
group->default_domain = iommu_group_alloc_default_domain(
439+
group, iommu_get_def_domain_type(dev));
438440
if (group->default_domain) {
439441
iommu_create_device_direct_mappings(group, dev);
440442
ret = __iommu_group_set_domain(group,
@@ -1645,35 +1647,38 @@ static int iommu_get_def_domain_type(struct device *dev)
16451647
return 0;
16461648
}
16471649

1648-
static int iommu_group_alloc_default_domain(const struct bus_type *bus,
1649-
struct iommu_group *group,
1650-
unsigned int type)
1650+
/*
1651+
* req_type of 0 means "auto" which means to select a domain based on
1652+
* iommu_def_domain_type or what the driver actually supports.
1653+
*/
1654+
static struct iommu_domain *
1655+
iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
16511656
{
1657+
const struct bus_type *bus =
1658+
list_first_entry(&group->devices, struct group_device, list)
1659+
->dev->bus;
16521660
struct iommu_domain *dom;
16531661

1654-
dom = __iommu_domain_alloc(bus, type);
1655-
if (!dom && type != IOMMU_DOMAIN_DMA) {
1656-
dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1657-
if (dom)
1658-
pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1659-
type, group->name);
1660-
}
1661-
1662-
if (!dom)
1663-
return -ENOMEM;
1662+
lockdep_assert_held(&group->mutex);
16641663

1665-
group->default_domain = dom;
1666-
return 0;
1667-
}
1664+
if (req_type)
1665+
return __iommu_domain_alloc(bus, req_type);
16681666

1669-
static int iommu_alloc_default_domain(struct iommu_group *group,
1670-
struct device *dev)
1671-
{
1672-
unsigned int type;
1667+
/* The driver gave no guidance on what type to use, try the default */
1668+
dom = __iommu_domain_alloc(bus, iommu_def_domain_type);
1669+
if (dom)
1670+
return dom;
16731671

1674-
type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
1672+
/* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
1673+
if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
1674+
return NULL;
1675+
dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1676+
if (!dom)
1677+
return NULL;
16751678

1676-
return iommu_group_alloc_default_domain(dev->bus, group, type);
1679+
pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1680+
iommu_def_domain_type, group->name);
1681+
return dom;
16771682
}
16781683

16791684
/**
@@ -1785,15 +1790,12 @@ static int iommu_get_default_domain_type(struct iommu_group *group,
17851790
"Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
17861791
iommu_domain_type_str(type), dev_name(last_dev),
17871792
iommu_domain_type_str(best_type));
1788-
return iommu_def_domain_type;
1793+
return 0;
17891794
}
17901795
if (!best_type)
17911796
best_type = type;
17921797
last_dev = gdev->dev;
17931798
}
1794-
1795-
if (!best_type)
1796-
return iommu_def_domain_type;
17971799
return best_type;
17981800
}
17991801

@@ -1850,9 +1852,8 @@ int bus_iommu_probe(const struct bus_type *bus)
18501852
list_del_init(&group->entry);
18511853

18521854
/* Try to allocate default domain */
1853-
iommu_group_alloc_default_domain(
1854-
bus, group, iommu_get_default_domain_type(group, 0));
1855-
1855+
group->default_domain = iommu_group_alloc_default_domain(
1856+
group, iommu_get_default_domain_type(group, 0));
18561857
if (!group->default_domain) {
18571858
mutex_unlock(&group->mutex);
18581859
continue;
@@ -2897,9 +2898,11 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
28972898
group->domain = NULL;
28982899

28992900
/* Sets group->default_domain to the newly allocated domain */
2900-
ret = iommu_group_alloc_default_domain(dev->bus, group, type);
2901-
if (ret)
2901+
group->default_domain = iommu_group_alloc_default_domain(group, type);
2902+
if (!group->default_domain) {
2903+
ret = -EINVAL;
29022904
goto restore_old_domain;
2905+
}
29032906

29042907
group->domain = prev_dom;
29052908
ret = iommu_create_device_direct_mappings(group, dev);

0 commit comments

Comments
 (0)