iommufd/viommu: Add iommufd_viommu_get_vdev_id helper
authorNicolin Chen <nicolinc@nvidia.com>
Tue, 11 Mar 2025 19:44:24 +0000 (12:44 -0700)
committerJason Gunthorpe <jgg@nvidia.com>
Tue, 18 Mar 2025 17:17:47 +0000 (14:17 -0300)
This is a reverse search v.s. iommufd_viommu_find_dev, as drivers may want
to convert a struct device pointer (physical) to its virtual device ID for
an event injection to the user space VM.

Again, this avoids exposing more core structures to the drivers, than the
iommufd_viommu alone.

Link: https://patch.msgid.link/r/18b8e8bc1b8104d43b205d21602c036fd0804e56.1741719725.git.nicolinc@nvidia.com
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/iommu/iommufd/driver.c
include/linux/iommufd.h

index 2d98b04ff1cb7edd382064f1bcedd10afe0a5b77..f132b98fb89981af3b4edcf26a3f0c22c8c10e70 100644 (file)
@@ -49,5 +49,29 @@ struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
 }
 EXPORT_SYMBOL_NS_GPL(iommufd_viommu_find_dev, "IOMMUFD");
 
+/* Return -ENOENT if device is not associated to the vIOMMU */
+int iommufd_viommu_get_vdev_id(struct iommufd_viommu *viommu,
+                              struct device *dev, unsigned long *vdev_id)
+{
+       struct iommufd_vdevice *vdev;
+       unsigned long index;
+       int rc = -ENOENT;
+
+       if (WARN_ON_ONCE(!vdev_id))
+               return -EINVAL;
+
+       xa_lock(&viommu->vdevs);
+       xa_for_each(&viommu->vdevs, index, vdev) {
+               if (vdev->dev == dev) {
+                       *vdev_id = vdev->id;
+                       rc = 0;
+                       break;
+               }
+       }
+       xa_unlock(&viommu->vdevs);
+       return rc;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_viommu_get_vdev_id, "IOMMUFD");
+
 MODULE_DESCRIPTION("iommufd code shared with builtin modules");
 MODULE_LICENSE("GPL");
index 8948b1836940c89380ee42afba094567286a13e2..05cb393aff0afb80287a5648aa6e7fb218979891 100644 (file)
@@ -190,6 +190,8 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
                                             enum iommufd_object_type type);
 struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
                                       unsigned long vdev_id);
+int iommufd_viommu_get_vdev_id(struct iommufd_viommu *viommu,
+                              struct device *dev, unsigned long *vdev_id);
 #else /* !CONFIG_IOMMUFD_DRIVER_CORE */
 static inline struct iommufd_object *
 _iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size,
@@ -203,6 +205,13 @@ iommufd_viommu_find_dev(struct iommufd_viommu *viommu, unsigned long vdev_id)
 {
        return NULL;
 }
+
+static inline int iommufd_viommu_get_vdev_id(struct iommufd_viommu *viommu,
+                                            struct device *dev,
+                                            unsigned long *vdev_id)
+{
+       return -ENOENT;
+}
 #endif /* CONFIG_IOMMUFD_DRIVER_CORE */
 
 /*