Skip to content

Commit df15d76

Browse files
jgunthorpejoergroedel
authored andcommitted
iommu: Simplify the __iommu_group_remove_device() flow
Instead of returning the struct group_device and then later freeing it, do the entire free under the group->mutex and defer only putting the iommu_group. It is safe to remove the sysfs_links and free memory while holding that mutex. Move the sanity assert of the group status into __iommu_group_free_device(). The next patch will improve upon this and consolidate the group put and the mutex into __iommu_group_remove_device(). __iommu_group_free_device() is close to being the paired undo of iommu_group_add_device(), following patches will improve on that. Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Lu Baolu <[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 7bdb996 commit df15d76

File tree

1 file changed

+39
-44
lines changed

1 file changed

+39
-44
lines changed

drivers/iommu/iommu.c

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -470,32 +470,8 @@ int iommu_probe_device(struct device *dev)
470470

471471
}
472472

473-
/*
474-
* Remove a device from a group's device list and return the group device
475-
* if successful.
476-
*/
477-
static struct group_device *
478-
__iommu_group_remove_device(struct iommu_group *group, struct device *dev)
479-
{
480-
struct group_device *device;
481-
482-
lockdep_assert_held(&group->mutex);
483-
for_each_group_device(group, device) {
484-
if (device->dev == dev) {
485-
list_del(&device->list);
486-
return device;
487-
}
488-
}
489-
490-
return NULL;
491-
}
492-
493-
/*
494-
* Release a device from its group and decrements the iommu group reference
495-
* count.
496-
*/
497-
static void __iommu_group_release_device(struct iommu_group *group,
498-
struct group_device *grp_dev)
473+
static void __iommu_group_free_device(struct iommu_group *group,
474+
struct group_device *grp_dev)
499475
{
500476
struct device *dev = grp_dev->dev;
501477

@@ -504,16 +480,45 @@ static void __iommu_group_release_device(struct iommu_group *group,
504480

505481
trace_remove_device_from_group(group->id, dev);
506482

483+
/*
484+
* If the group has become empty then ownership must have been
485+
* released, and the current domain must be set back to NULL or
486+
* the default domain.
487+
*/
488+
if (list_empty(&group->devices))
489+
WARN_ON(group->owner_cnt ||
490+
group->domain != group->default_domain);
491+
507492
kfree(grp_dev->name);
508493
kfree(grp_dev);
509494
dev->iommu_group = NULL;
510-
iommu_group_put(group);
511495
}
512496

513-
static void iommu_release_device(struct device *dev)
497+
/*
498+
* Remove the iommu_group from the struct device. The attached group must be put
499+
* by the caller after releaseing the group->mutex.
500+
*/
501+
static void __iommu_group_remove_device(struct device *dev)
514502
{
515503
struct iommu_group *group = dev->iommu_group;
516504
struct group_device *device;
505+
506+
lockdep_assert_held(&group->mutex);
507+
for_each_group_device(group, device) {
508+
if (device->dev != dev)
509+
continue;
510+
511+
list_del(&device->list);
512+
__iommu_group_free_device(group, device);
513+
/* Caller must put iommu_group */
514+
return;
515+
}
516+
WARN(true, "Corrupted iommu_group device_list");
517+
}
518+
519+
static void iommu_release_device(struct device *dev)
520+
{
521+
struct iommu_group *group = dev->iommu_group;
517522
const struct iommu_ops *ops;
518523

519524
if (!dev->iommu || !group)
@@ -522,16 +527,7 @@ static void iommu_release_device(struct device *dev)
522527
iommu_device_unlink(dev->iommu->iommu_dev, dev);
523528

524529
mutex_lock(&group->mutex);
525-
device = __iommu_group_remove_device(group, dev);
526-
527-
/*
528-
* If the group has become empty then ownership must have been released,
529-
* and the current domain must be set back to NULL or the default
530-
* domain.
531-
*/
532-
if (list_empty(&group->devices))
533-
WARN_ON(group->owner_cnt ||
534-
group->domain != group->default_domain);
530+
__iommu_group_remove_device(dev);
535531

536532
/*
537533
* release_device() must stop using any attached domain on the device.
@@ -547,8 +543,8 @@ static void iommu_release_device(struct device *dev)
547543
ops->release_device(dev);
548544
mutex_unlock(&group->mutex);
549545

550-
if (device)
551-
__iommu_group_release_device(group, device);
546+
/* Pairs with the get in iommu_group_add_device() */
547+
iommu_group_put(group);
552548

553549
module_put(ops->owner);
554550
dev_iommu_free(dev);
@@ -1107,19 +1103,18 @@ EXPORT_SYMBOL_GPL(iommu_group_add_device);
11071103
void iommu_group_remove_device(struct device *dev)
11081104
{
11091105
struct iommu_group *group = dev->iommu_group;
1110-
struct group_device *device;
11111106

11121107
if (!group)
11131108
return;
11141109

11151110
dev_info(dev, "Removing from iommu group %d\n", group->id);
11161111

11171112
mutex_lock(&group->mutex);
1118-
device = __iommu_group_remove_device(group, dev);
1113+
__iommu_group_remove_device(dev);
11191114
mutex_unlock(&group->mutex);
11201115

1121-
if (device)
1122-
__iommu_group_release_device(group, device);
1116+
/* Pairs with the get in iommu_group_add_device() */
1117+
iommu_group_put(group);
11231118
}
11241119
EXPORT_SYMBOL_GPL(iommu_group_remove_device);
11251120

0 commit comments

Comments
 (0)