[PATCH v2 2/3] iommufd/device: Make hwpt_list list_add/del symmetric

From: Nicolin Chen
Date: Sat Jan 28 2023 - 16:18:59 EST


Since the list_del() of hwpt_item is done in iommufd_device_detach(), move
its list_add_tail() to a similar place in iommufd_device_do_attach().

Also move and place the mutex outside the iommufd_device_auto_get_domain()
and iommufd_device_do_attach() calls, to serialize attach/detach routines.
This adds an additional locking protection so that the following patch can
safely remove devices_lock.

Co-developed-by: Yi Liu <yi.l.liu@xxxxxxxxx>
Signed-off-by: Yi Liu <yi.l.liu@xxxxxxxxx>
Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx>
---
drivers/iommu/iommufd/device.c | 26 ++++++++++++--------------
1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 208757c39c90..9375fcac884c 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -198,6 +198,8 @@ static int iommufd_device_do_attach(struct iommufd_device *idev,
phys_addr_t sw_msi_start = PHYS_ADDR_MAX;
int rc;

+ lockdep_assert_held(&hwpt->ioas->mutex);
+
mutex_lock(&hwpt->devices_lock);

/*
@@ -241,6 +243,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev,
hwpt->domain);
if (rc)
goto out_detach;
+ list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
}
}

@@ -271,12 +274,13 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev,
struct iommufd_hw_pagetable *hwpt;
int rc;

+ lockdep_assert_held(&ioas->mutex);
+
/*
* There is no differentiation when domains are allocated, so any domain
* that is willing to attach to the device is interchangeable with any
* other.
*/
- mutex_lock(&ioas->mutex);
list_for_each_entry(hwpt, &ioas->hwpt_list, hwpt_item) {
if (!hwpt->auto_domain)
continue;
@@ -290,29 +294,23 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev,
*/
if (rc == -EINVAL)
continue;
- goto out_unlock;
+ return rc;
}

hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev->dev);
- if (IS_ERR(hwpt)) {
- rc = PTR_ERR(hwpt);
- goto out_unlock;
- }
+ if (IS_ERR(hwpt))
+ return PTR_ERR(hwpt);
hwpt->auto_domain = true;

rc = iommufd_device_do_attach(idev, hwpt);
if (rc)
goto out_abort;
- list_add_tail(&hwpt->hwpt_item, &ioas->hwpt_list);

- mutex_unlock(&ioas->mutex);
iommufd_object_finalize(idev->ictx, &hwpt->obj);
return 0;

out_abort:
iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj);
-out_unlock:
- mutex_unlock(&ioas->mutex);
return rc;
}

@@ -342,20 +340,20 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id)
struct iommufd_hw_pagetable *hwpt =
container_of(pt_obj, struct iommufd_hw_pagetable, obj);

+ mutex_lock(&hwpt->ioas->mutex);
rc = iommufd_device_do_attach(idev, hwpt);
+ mutex_unlock(&hwpt->ioas->mutex);
if (rc)
goto out_put_pt_obj;
-
- mutex_lock(&hwpt->ioas->mutex);
- list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
- mutex_unlock(&hwpt->ioas->mutex);
break;
}
case IOMMUFD_OBJ_IOAS: {
struct iommufd_ioas *ioas =
container_of(pt_obj, struct iommufd_ioas, obj);

+ mutex_lock(&ioas->mutex);
rc = iommufd_device_auto_get_domain(idev, ioas);
+ mutex_unlock(&ioas->mutex);
if (rc)
goto out_put_pt_obj;
break;
--
2.39.1