From: Kevin Tian <[email protected]>

This abstracts the common logic used in the iommufd_device_bind() and the
later iommufd_device_bind_pasid() to be helper.

Signed-off-by: Kevin Tian <[email protected]>
Signed-off-by: Yi Liu <[email protected]>
---
 drivers/iommu/iommufd/device.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index ca3919fecc89..9dd76d92b7f6 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -164,6 +164,27 @@ static int iommufd_allow_unsafe_interrupts(struct device 
*dev)
        return 0;
 }
 
+static struct iommufd_device *iommufd_alloc_device(struct iommufd_ctx *ictx,
+                                                  struct device *dev)
+{
+       struct iommufd_device *idev;
+
+       idev = iommufd_object_alloc(ictx, idev, IOMMUFD_OBJ_DEVICE);
+       if (IS_ERR(idev))
+               return idev;
+       idev->ictx = ictx;
+       if (!iommufd_selftest_is_mock_dev(dev))
+               iommufd_ctx_get(ictx);
+       idev->dev = dev;
+       idev->enforce_cache_coherency =
+               device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
+       xa_init(&idev->pasid_hwpts);
+
+       /* The calling driver is a user until iommufd_device_unbind() */
+       refcount_inc(&idev->obj.users);
+       return idev;
+}
+
 /**
  * iommufd_device_bind - Bind a physical device to an iommu fd
  * @ictx: iommufd file descriptor
@@ -209,24 +230,15 @@ struct iommufd_device *iommufd_device_bind(struct 
iommufd_ctx *ictx,
        if (rc)
                goto out_group_put;
 
-       idev = iommufd_object_alloc(ictx, idev, IOMMUFD_OBJ_DEVICE);
+       idev = iommufd_alloc_device(ictx, dev);
        if (IS_ERR(idev)) {
                rc = PTR_ERR(idev);
                goto out_release_owner;
        }
-       idev->ictx = ictx;
-       if (!iommufd_selftest_is_mock_dev(dev))
-               iommufd_ctx_get(ictx);
-       idev->dev = dev;
-       idev->enforce_cache_coherency =
-               device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
-       /* The calling driver is a user until iommufd_device_unbind() */
-       refcount_inc(&idev->obj.users);
+
        /* igroup refcount moves into iommufd_device */
        idev->igroup = igroup;
 
-       xa_init(&idev->pasid_hwpts);
-
        /*
         * If the caller fails after this success it must call
         * iommufd_unbind_device() which is safe since we hold this refcount.
-- 
2.34.1

Reply via email to