Skip to content

Commit d99be00

Browse files
jgunthorpejoergroedel
authored andcommitted
iommu: Consolidate the default_domain setup to one function
Make iommu_change_dev_def_domain() general enough to setup the initial default_domain or replace it with a new default_domain. Call the new function iommu_setup_default_domain() and make it the only place in the code that stores to group->default_domain. Consolidate the three copies of the default_domain setup sequence. The flow flow requires: - Determining the domain type to use - Checking if the current default domain is the same type - Allocating a domain - Doing iommu_create_device_direct_mappings() - Attaching it to devices - Store group->default_domain This adjusts the domain allocation from the prior patch to be able to detect if each of the allocation steps is already the domain we already have, which is a more robust version of what change default domain was already doing. 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 fcbb0a4 commit d99be00

File tree

1 file changed

+89
-113
lines changed

1 file changed

+89
-113
lines changed

drivers/iommu/iommu.c

Lines changed: 89 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ 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 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);
9996
static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus,
10097
unsigned type);
10198
static int __iommu_attach_device(struct iommu_domain *domain,
@@ -126,7 +123,9 @@ static void __iommu_group_set_domain_nofail(struct iommu_group *group,
126123
group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
127124
}
128125

129-
static int iommu_create_device_direct_mappings(struct iommu_group *group,
126+
static int iommu_setup_default_domain(struct iommu_group *group,
127+
int target_type);
128+
static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
130129
struct device *dev);
131130
static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
132131
static ssize_t iommu_group_store_type(struct iommu_group *group,
@@ -424,33 +423,18 @@ int iommu_probe_device(struct device *dev)
424423

425424
mutex_lock(&group->mutex);
426425

427-
iommu_create_device_direct_mappings(group, dev);
426+
if (group->default_domain)
427+
iommu_create_device_direct_mappings(group->default_domain, dev);
428428

429429
if (group->domain) {
430430
ret = __iommu_device_set_domain(group, dev, group->domain, 0);
431+
if (ret)
432+
goto err_unlock;
431433
} else if (!group->default_domain) {
432-
/*
433-
* Try to allocate a default domain - needs support from the
434-
* IOMMU driver. There are still some drivers which don't
435-
* support default domains, so the return value is not yet
436-
* checked.
437-
*/
438-
group->default_domain = iommu_group_alloc_default_domain(
439-
group, iommu_get_def_domain_type(dev));
440-
if (group->default_domain) {
441-
iommu_create_device_direct_mappings(group, dev);
442-
ret = __iommu_group_set_domain(group,
443-
group->default_domain);
444-
}
445-
446-
/*
447-
* We assume that the iommu driver starts up the device in
448-
* 'set_platform_dma_ops' mode if it does not support default
449-
* domains.
450-
*/
434+
ret = iommu_setup_default_domain(group, 0);
435+
if (ret)
436+
goto err_unlock;
451437
}
452-
if (ret)
453-
goto err_unlock;
454438

455439
mutex_unlock(&group->mutex);
456440
iommu_group_put(group);
@@ -967,16 +951,15 @@ int iommu_group_set_name(struct iommu_group *group, const char *name)
967951
}
968952
EXPORT_SYMBOL_GPL(iommu_group_set_name);
969953

970-
static int iommu_create_device_direct_mappings(struct iommu_group *group,
954+
static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
971955
struct device *dev)
972956
{
973-
struct iommu_domain *domain = group->default_domain;
974957
struct iommu_resv_region *entry;
975958
struct list_head mappings;
976959
unsigned long pg_size;
977960
int ret = 0;
978961

979-
if (!domain || !iommu_is_dma_domain(domain))
962+
if (!iommu_is_dma_domain(domain))
980963
return 0;
981964

982965
BUG_ON(!domain->pgsize_bitmap);
@@ -1647,6 +1630,15 @@ static int iommu_get_def_domain_type(struct device *dev)
16471630
return 0;
16481631
}
16491632

1633+
static struct iommu_domain *
1634+
__iommu_group_alloc_default_domain(const struct bus_type *bus,
1635+
struct iommu_group *group, int req_type)
1636+
{
1637+
if (group->default_domain && group->default_domain->type == req_type)
1638+
return group->default_domain;
1639+
return __iommu_domain_alloc(bus, req_type);
1640+
}
1641+
16501642
/*
16511643
* req_type of 0 means "auto" which means to select a domain based on
16521644
* iommu_def_domain_type or what the driver actually supports.
@@ -1662,17 +1654,17 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
16621654
lockdep_assert_held(&group->mutex);
16631655

16641656
if (req_type)
1665-
return __iommu_domain_alloc(bus, req_type);
1657+
return __iommu_group_alloc_default_domain(bus, group, req_type);
16661658

16671659
/* The driver gave no guidance on what type to use, try the default */
1668-
dom = __iommu_domain_alloc(bus, iommu_def_domain_type);
1660+
dom = __iommu_group_alloc_default_domain(bus, group, iommu_def_domain_type);
16691661
if (dom)
16701662
return dom;
16711663

16721664
/* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
16731665
if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
16741666
return NULL;
1675-
dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA);
1667+
dom = __iommu_group_alloc_default_domain(bus, group, IOMMU_DOMAIN_DMA);
16761668
if (!dom)
16771669
return NULL;
16781670

@@ -1815,21 +1807,6 @@ static void __iommu_group_dma_finalize(struct iommu_group *group)
18151807
iommu_group_do_probe_finalize);
18161808
}
18171809

1818-
static int iommu_do_create_direct_mappings(struct device *dev, void *data)
1819-
{
1820-
struct iommu_group *group = data;
1821-
1822-
iommu_create_device_direct_mappings(group, dev);
1823-
1824-
return 0;
1825-
}
1826-
1827-
static int iommu_group_create_direct_mappings(struct iommu_group *group)
1828-
{
1829-
return __iommu_group_for_each_dev(group, group,
1830-
iommu_do_create_direct_mappings);
1831-
}
1832-
18331810
int bus_iommu_probe(const struct bus_type *bus)
18341811
{
18351812
struct iommu_group *group, *next;
@@ -1851,27 +1828,16 @@ int bus_iommu_probe(const struct bus_type *bus)
18511828
/* Remove item from the list */
18521829
list_del_init(&group->entry);
18531830

1854-
/* Try to allocate default domain */
1855-
group->default_domain = iommu_group_alloc_default_domain(
1856-
group, iommu_get_default_domain_type(group, 0));
1857-
if (!group->default_domain) {
1831+
ret = iommu_setup_default_domain(group, 0);
1832+
if (ret) {
18581833
mutex_unlock(&group->mutex);
1859-
continue;
1834+
return ret;
18601835
}
1861-
1862-
iommu_group_create_direct_mappings(group);
1863-
1864-
ret = __iommu_group_set_domain(group, group->default_domain);
1865-
18661836
mutex_unlock(&group->mutex);
1867-
1868-
if (ret)
1869-
break;
1870-
18711837
__iommu_group_dma_finalize(group);
18721838
}
18731839

1874-
return ret;
1840+
return 0;
18751841
}
18761842

18771843
bool iommu_present(const struct bus_type *bus)
@@ -2860,68 +2826,83 @@ int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
28602826
}
28612827
EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
28622828

2863-
/*
2864-
* Changes the default domain of an iommu group
2865-
*
2866-
* @group: The group for which the default domain should be changed
2867-
* @dev: The first device in the group
2868-
* @type: The type of the new default domain that gets associated with the group
2869-
*
2870-
* Returns 0 on success and error code on failure
2829+
/**
2830+
* iommu_setup_default_domain - Set the default_domain for the group
2831+
* @group: Group to change
2832+
* @target_type: Domain type to set as the default_domain
28712833
*
2872-
* Note:
2873-
* 1. Presently, this function is called only when user requests to change the
2874-
* group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type
2875-
* Please take a closer look if intended to use for other purposes.
2834+
* Allocate a default domain and set it as the current domain on the group. If
2835+
* the group already has a default domain it will be changed to the target_type.
2836+
* When target_type is 0 the default domain is selected based on driver and
2837+
* system preferences.
28762838
*/
2877-
static int iommu_change_dev_def_domain(struct iommu_group *group,
2878-
struct device *dev, int type)
2839+
static int iommu_setup_default_domain(struct iommu_group *group,
2840+
int target_type)
28792841
{
2880-
struct iommu_domain *prev_dom;
2842+
struct iommu_domain *old_dom = group->default_domain;
2843+
struct group_device *gdev;
2844+
struct iommu_domain *dom;
2845+
int req_type;
28812846
int ret;
28822847

28832848
lockdep_assert_held(&group->mutex);
28842849

2885-
prev_dom = group->default_domain;
2886-
type = iommu_get_default_domain_type(group, type);
2887-
if (type < 0)
2850+
req_type = iommu_get_default_domain_type(group, target_type);
2851+
if (req_type < 0)
28882852
return -EINVAL;
28892853

28902854
/*
2891-
* Switch to a new domain only if the requested domain type is different
2892-
* from the existing default domain type
2855+
* There are still some drivers which don't support default domains, so
2856+
* we ignore the failure and leave group->default_domain NULL.
2857+
*
2858+
* We assume that the iommu driver starts up the device in
2859+
* 'set_platform_dma_ops' mode if it does not support default domains.
28932860
*/
2894-
if (prev_dom->type == type)
2861+
dom = iommu_group_alloc_default_domain(group, req_type);
2862+
if (!dom) {
2863+
/* Once in default_domain mode we never leave */
2864+
if (group->default_domain)
2865+
return -ENODEV;
2866+
group->default_domain = NULL;
28952867
return 0;
2896-
2897-
group->default_domain = NULL;
2898-
group->domain = NULL;
2899-
2900-
/* Sets group->default_domain to the newly allocated domain */
2901-
group->default_domain = iommu_group_alloc_default_domain(group, type);
2902-
if (!group->default_domain) {
2903-
ret = -EINVAL;
2904-
goto restore_old_domain;
29052868
}
29062869

2907-
group->domain = prev_dom;
2908-
ret = iommu_create_device_direct_mappings(group, dev);
2909-
if (ret)
2910-
goto free_new_domain;
2911-
2912-
ret = __iommu_group_set_domain(group, group->default_domain);
2913-
if (ret)
2914-
goto free_new_domain;
2915-
2916-
iommu_domain_free(prev_dom);
2870+
if (group->default_domain == dom)
2871+
return 0;
29172872

2918-
return 0;
2873+
/*
2874+
* IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
2875+
* mapped before their device is attached, in order to guarantee
2876+
* continuity with any FW activity
2877+
*/
2878+
for_each_group_device(group, gdev)
2879+
iommu_create_device_direct_mappings(dom, gdev->dev);
29192880

2920-
free_new_domain:
2921-
iommu_domain_free(group->default_domain);
2922-
restore_old_domain:
2923-
group->default_domain = prev_dom;
2881+
/* We must set default_domain early for __iommu_device_set_domain */
2882+
group->default_domain = dom;
2883+
if (!group->domain) {
2884+
/*
2885+
* Drivers are not allowed to fail the first domain attach.
2886+
* The only way to recover from this is to fail attaching the
2887+
* iommu driver and call ops->release_device. Put the domain
2888+
* in group->default_domain so it is freed after.
2889+
*/
2890+
ret = __iommu_group_set_domain_internal(
2891+
group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
2892+
if (WARN_ON(ret))
2893+
goto out_free;
2894+
} else {
2895+
ret = __iommu_group_set_domain(group, dom);
2896+
if (ret) {
2897+
iommu_domain_free(dom);
2898+
group->default_domain = old_dom;
2899+
return ret;
2900+
}
2901+
}
29242902

2903+
out_free:
2904+
if (old_dom)
2905+
iommu_domain_free(old_dom);
29252906
return ret;
29262907
}
29272908

@@ -2937,8 +2918,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
29372918
static ssize_t iommu_group_store_type(struct iommu_group *group,
29382919
const char *buf, size_t count)
29392920
{
2940-
struct group_device *grp_dev;
2941-
struct device *dev;
29422921
int ret, req_type;
29432922

29442923
if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
@@ -2976,10 +2955,7 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
29762955
return -EPERM;
29772956
}
29782957

2979-
grp_dev = list_first_entry(&group->devices, struct group_device, list);
2980-
dev = grp_dev->dev;
2981-
2982-
ret = iommu_change_dev_def_domain(group, dev, req_type);
2958+
ret = iommu_setup_default_domain(group, req_type);
29832959

29842960
/*
29852961
* Release the mutex here because ops->probe_finalize() call-back of

0 commit comments

Comments
 (0)