Skip to content

Commit e7f85df

Browse files
jgunthorpejoergroedel
authored andcommitted
iommu: Fix iommu_probe_device() to attach the right domain
The general invariant is that all devices in an iommu_group are attached to group->domain. We missed some cases here where an owned group would not get the device attached. Rework this logic so it follows the default domain flow of the bus_iommu_probe() - call iommu_alloc_default_domain(), then use __iommu_group_set_domain_internal() to set up all the devices. Finally always attach the device to the current domain if it is already set. This is an unlikely functional issue as iommufd uses iommu_attach_group(). It is possible to hot plug in a new group member, add a vfio driver to it and then hot add it to an existing iommufd. In this case it is required that the core code set the iommu_domain properly since iommufd won't call iommu_attach_group() again. 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 2f74198 commit e7f85df

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

drivers/iommu/iommu.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -421,27 +421,31 @@ int iommu_probe_device(struct device *dev)
421421
goto err_release;
422422
}
423423

424-
/*
425-
* Try to allocate a default domain - needs support from the
426-
* IOMMU driver. There are still some drivers which don't
427-
* support default domains, so the return value is not yet
428-
* checked.
429-
*/
430424
mutex_lock(&group->mutex);
431-
iommu_alloc_default_domain(group, dev);
432425

433-
/*
434-
* If device joined an existing group which has been claimed, don't
435-
* attach the default domain.
436-
*/
437-
if (group->default_domain && !group->owner) {
426+
if (group->domain) {
438427
ret = __iommu_device_set_domain(group, dev, group->domain, 0);
439-
if (ret) {
440-
mutex_unlock(&group->mutex);
441-
iommu_group_put(group);
442-
goto err_release;
443-
}
428+
} else if (!group->default_domain) {
429+
/*
430+
* Try to allocate a default domain - needs support from the
431+
* IOMMU driver. There are still some drivers which don't
432+
* support default domains, so the return value is not yet
433+
* checked.
434+
*/
435+
iommu_alloc_default_domain(group, dev);
436+
group->domain = NULL;
437+
if (group->default_domain)
438+
ret = __iommu_group_set_domain(group,
439+
group->default_domain);
440+
441+
/*
442+
* We assume that the iommu driver starts up the device in
443+
* 'set_platform_dma_ops' mode if it does not support default
444+
* domains.
445+
*/
444446
}
447+
if (ret)
448+
goto err_unlock;
445449

446450
iommu_create_device_direct_mappings(group, dev);
447451

@@ -454,6 +458,9 @@ int iommu_probe_device(struct device *dev)
454458

455459
return 0;
456460

461+
err_unlock:
462+
mutex_unlock(&group->mutex);
463+
iommu_group_put(group);
457464
err_release:
458465
iommu_release_device(dev);
459466

@@ -1665,9 +1672,6 @@ static int iommu_alloc_default_domain(struct iommu_group *group,
16651672
{
16661673
unsigned int type;
16671674

1668-
if (group->default_domain)
1669-
return 0;
1670-
16711675
type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type;
16721676

16731677
return iommu_group_alloc_default_domain(dev->bus, group, type);

0 commit comments

Comments
 (0)