Skip to content

Commit 8b4eb75

Browse files
jgunthorpejoergroedel
authored andcommitted
iommu: Consolidate the code to calculate the target default domain type
Put all the code to calculate the default domain type into one function. Make the function able to handle the iommu_change_dev_def_domain() by taking in the target domain type and erroring out if the target type isn't reachable. This makes it really clear that specifying a 0 type during iommu_change_dev_def_domain() will have the same outcome as the normal probe path. Remove the obfuscating use of __iommu_group_for_each_dev() and related struct __group_domain_type. 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 dfddd54 commit 8b4eb75

File tree

1 file changed

+35
-53
lines changed

1 file changed

+35
-53
lines changed

drivers/iommu/iommu.c

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,50 +1758,43 @@ static int iommu_bus_notifier(struct notifier_block *nb,
17581758
return 0;
17591759
}
17601760

1761-
struct __group_domain_type {
1762-
struct device *dev;
1763-
unsigned int type;
1764-
};
1765-
1766-
static int probe_get_default_domain_type(struct device *dev, void *data)
1761+
/* A target_type of 0 will select the best domain type and cannot fail */
1762+
static int iommu_get_default_domain_type(struct iommu_group *group,
1763+
int target_type)
17671764
{
1768-
struct __group_domain_type *gtype = data;
1769-
unsigned int type = iommu_get_def_domain_type(dev);
1765+
int best_type = target_type;
1766+
struct group_device *gdev;
1767+
struct device *last_dev;
17701768

1771-
if (type) {
1772-
if (gtype->type && gtype->type != type) {
1773-
dev_warn(dev, "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1774-
iommu_domain_type_str(type),
1775-
dev_name(gtype->dev),
1776-
iommu_domain_type_str(gtype->type));
1777-
gtype->type = 0;
1778-
}
1769+
lockdep_assert_held(&group->mutex);
17791770

1780-
if (!gtype->dev) {
1781-
gtype->dev = dev;
1782-
gtype->type = type;
1771+
for_each_group_device(group, gdev) {
1772+
unsigned int type = iommu_get_def_domain_type(gdev->dev);
1773+
1774+
if (best_type && type && best_type != type) {
1775+
if (target_type) {
1776+
dev_err_ratelimited(
1777+
gdev->dev,
1778+
"Device cannot be in %s domain\n",
1779+
iommu_domain_type_str(target_type));
1780+
return -1;
1781+
}
1782+
1783+
dev_warn(
1784+
gdev->dev,
1785+
"Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1786+
iommu_domain_type_str(type), dev_name(last_dev),
1787+
iommu_domain_type_str(best_type));
1788+
return iommu_def_domain_type;
17831789
}
1790+
if (!best_type)
1791+
best_type = type;
1792+
last_dev = gdev->dev;
17841793
}
17851794

1786-
return 0;
1787-
}
1788-
1789-
static void probe_alloc_default_domain(const struct bus_type *bus,
1790-
struct iommu_group *group)
1791-
{
1792-
struct __group_domain_type gtype;
1793-
1794-
memset(&gtype, 0, sizeof(gtype));
1795-
1796-
/* Ask for default domain requirements of all devices in the group */
1797-
__iommu_group_for_each_dev(group, &gtype,
1798-
probe_get_default_domain_type);
1799-
1800-
if (!gtype.type)
1801-
gtype.type = iommu_def_domain_type;
1802-
1803-
iommu_group_alloc_default_domain(bus, group, gtype.type);
1804-
1795+
if (!best_type)
1796+
return iommu_def_domain_type;
1797+
return best_type;
18051798
}
18061799

18071800
static int iommu_group_do_probe_finalize(struct device *dev, void *data)
@@ -1857,7 +1850,8 @@ int bus_iommu_probe(const struct bus_type *bus)
18571850
list_del_init(&group->entry);
18581851

18591852
/* Try to allocate default domain */
1860-
probe_alloc_default_domain(bus, group);
1853+
iommu_group_alloc_default_domain(
1854+
bus, group, iommu_get_default_domain_type(group, 0));
18611855

18621856
if (!group->default_domain) {
18631857
mutex_unlock(&group->mutex);
@@ -2882,27 +2876,15 @@ EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
28822876
static int iommu_change_dev_def_domain(struct iommu_group *group,
28832877
struct device *dev, int type)
28842878
{
2885-
struct __group_domain_type gtype = {NULL, 0};
28862879
struct iommu_domain *prev_dom;
28872880
int ret;
28882881

28892882
lockdep_assert_held(&group->mutex);
28902883

28912884
prev_dom = group->default_domain;
2892-
__iommu_group_for_each_dev(group, &gtype,
2893-
probe_get_default_domain_type);
2894-
if (!type) {
2895-
/*
2896-
* If the user hasn't requested any specific type of domain and
2897-
* if the device supports both the domains, then default to the
2898-
* domain the device was booted with
2899-
*/
2900-
type = gtype.type ? : iommu_def_domain_type;
2901-
} else if (gtype.type && type != gtype.type) {
2902-
dev_err_ratelimited(dev, "Device cannot be in %s domain\n",
2903-
iommu_domain_type_str(type));
2885+
type = iommu_get_default_domain_type(group, type);
2886+
if (type < 0)
29042887
return -EINVAL;
2905-
}
29062888

29072889
/*
29082890
* Switch to a new domain only if the requested domain type is different

0 commit comments

Comments
 (0)