[RFC PATCH v3 2/5] iommufd/device: Associate a kvm pointer to iommufd_device
From: Shameer Kolothum <hidden>
Date: 2025-03-19 17:38:03
Also in:
kvmarm, linux-iommu
Subsystem:
iommu subsystem, iommufd, the rest, vfio driver · Maintainers:
Joerg Roedel, Will Deacon, Jason Gunthorpe, Kevin Tian, Linus Torvalds, Alex Williamson
Add a struct kvm * to iommufd_device_bind() fn and associate it with idev if bind is successful. Signed-off-by: Shameer Kolothum <redacted> --- drivers/iommu/iommufd/device.c | 5 ++++- drivers/iommu/iommufd/iommufd_private.h | 2 ++ drivers/vfio/iommufd.c | 2 +- include/linux/iommufd.h | 4 +++- 4 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index dfd0898fb6c1..0f3983b16d56 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c@@ -146,6 +146,7 @@ void iommufd_device_destroy(struct iommufd_object *obj) * iommufd_device_bind - Bind a physical device to an iommu fd * @ictx: iommufd file descriptor * @dev: Pointer to a physical device struct + * @kvm: Pointer to struct kvm if device belongs to a KVM VM * @id: Output ID number to return to userspace for this device * * A successful bind establishes an ownership over the device and returns
@@ -159,7 +160,8 @@ void iommufd_device_destroy(struct iommufd_object *obj) * The caller must undo this with iommufd_device_unbind() */ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, - struct device *dev, u32 *id) + struct device *dev, struct kvm *kvm, + u32 *id) { struct iommufd_device *idev; struct iommufd_group *igroup;
@@ -209,6 +211,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!iommufd_selftest_is_mock_dev(dev)) iommufd_ctx_get(ictx); idev->dev = dev; + idev->kvm = kvm; idev->enforce_cache_coherency = device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); /* The calling driver is a user until iommufd_device_unbind() */
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 0b1bafc7fd99..73201ff2c40e 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h@@ -398,6 +398,8 @@ struct iommufd_device { struct list_head group_item; /* always the physical device */ struct device *dev; + /* ..and kvm if available */ + struct kvm *kvm; bool enforce_cache_coherency; /* protect iopf_enabled counter */ struct mutex iopf_lock;
diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c
index 516294fd901b..664e3579ce0e 100644
--- a/drivers/vfio/iommufd.c
+++ b/drivers/vfio/iommufd.c@@ -115,7 +115,7 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, { struct iommufd_device *idev; - idev = iommufd_device_bind(ictx, vdev->dev, out_device_id); + idev = iommufd_device_bind(ictx, vdev->dev, vdev->kvm, out_device_id); if (IS_ERR(idev)) return PTR_ERR(idev); vdev->iommufd_device = idev;
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 11110c749200..ac8cca0190f4 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h@@ -22,6 +22,7 @@ struct iommufd_ctx; struct iommufd_device; struct iommufd_viommu_ops; struct page; +struct kvm; enum iommufd_object_type { IOMMUFD_OBJ_NONE,
@@ -49,7 +50,8 @@ struct iommufd_object { }; struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, - struct device *dev, u32 *id); + struct device *dev, struct kvm *kvm, + u32 *id); void iommufd_device_unbind(struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id);
--
2.47.0