[RFC,2/7] iommufd: Introduce iommufd_alloc_device()

Message ID 20231009085123.463179-3-yi.l.liu@intel.com
State New
Headers
Series Add SIOV virtual device support |

Commit Message

Yi Liu Oct. 9, 2023, 8:51 a.m. UTC
  From: Kevin Tian <kevin.tian@intel.com>

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 <kevin.tian@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/iommu/iommufd/device.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)
  

Patch

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.