[PATCH v4 08/14] iommufd/viommu: Add vdev_to_dev helper

Nicolin Chen posted 14 patches 1 month ago
There is a newer version of this series
[PATCH v4 08/14] iommufd/viommu: Add vdev_to_dev helper
Posted by Nicolin Chen 1 month ago
This avoids a bigger trouble of moving the struct iommufd_device to the
public header.

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
 include/linux/iommufd.h        | 6 ++++++
 drivers/iommu/iommufd/driver.c | 7 +++++++
 2 files changed, 13 insertions(+)

diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 559f274a26ea..2f4ec6e6df21 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -204,6 +204,7 @@ static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx)
 struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
 					     size_t size,
 					     enum iommufd_object_type type);
+struct device *vdev_to_dev(struct iommufd_vdevice *vdev);
 #else /* !CONFIG_IOMMUFD_DRIVER */
 static inline struct iommufd_object *
 _iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size,
@@ -211,6 +212,11 @@ _iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size,
 {
 	return ERR_PTR(-EOPNOTSUPP);
 }
+
+static inline struct device *vdev_to_dev(struct iommufd_vdevice *vdev)
+{
+	return NULL;
+}
 #endif /* CONFIG_IOMMUFD_DRIVER */
 
 /*
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index c0876d3f91c7..a5d750d2cfaa 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -36,3 +36,10 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
 	return ERR_PTR(rc);
 }
 EXPORT_SYMBOL_NS_GPL(_iommufd_object_alloc, IOMMUFD);
+
+/* Caller should xa_lock(&viommu->vdevs) to protect the return value */
+struct device *vdev_to_dev(struct iommufd_vdevice *vdev)
+{
+	return vdev ? vdev->idev->dev : NULL;
+}
+EXPORT_SYMBOL_NS_GPL(vdev_to_dev, IOMMUFD);
-- 
2.43.0
Re: [PATCH v4 08/14] iommufd/viommu: Add vdev_to_dev helper
Posted by Nicolin Chen 1 month ago
On Mon, Oct 21, 2024 at 05:20:17PM -0700, Nicolin Chen wrote:
> +/* Caller should xa_lock(&viommu->vdevs) to protect the return value */
> +struct device *vdev_to_dev(struct iommufd_vdevice *vdev)
> +{
> +       return vdev ? vdev->idev->dev : NULL;
> +}
> +EXPORT_SYMBOL_NS_GPL(vdev_to_dev, IOMMUFD);

Changing to not exposing the iommufd_vdevice structure, I reworked
this helper to a vIOMMU-based one:

+/* Caller should xa_lock(&viommu->vdevs) to protect the return value */
+struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
+                                      unsigned long vdev_id)
+{
+       struct iommufd_vdevice *vdev;
+
+       lockdep_is_held(&viommu->vdevs.xa_lock);
+
+       vdev = xa_load(&viommu->vdevs, vdev_id);
+       return vdev ? vdev->idev->dev : NULL;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_viommu_find_dev, IOMMUFD);

Nicolin