Skip to contentSkip to conversation

iommufd: Allocate attach_handle for any HWPT

PATCH In review Nicolin Chen 3 participants 7 messages Feb 4, 2025 – Feb 11, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover iommufd: Allocate attach_handle for any HWPT

An attach_handle is a hwpt->fault specific thing, so the fault.c has been holding a set of attach/detach/replace helpers for attach_handle and iopf enabling/disabling.

Now, both the iommufd_msi series [1] and the attach_pasid series [2] want to reuse the attach_handle for non-fault cases. Rework the attach_handle, to prepare for either of the series. It should be merged with either that gets merged first.

[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://lore.kernel.org/linux-iommu/[email protected]/

Patches in this series are from [1] with some fixes addressing the review comments.

Changelog
v1 (since [1])
* Rebase on v6.14-rc1
* Unwrap the fault attach/detach/replace helpers
* Split the attach_handle patch to make it clearer

Thanks
Nicolin

Nicolin Chen (2):
iommufd: Make attach_handle generic than fault specific iommufd/fault: Remove iommufd_fault_domain_attach/detach/replace_dev()

 drivers/iommu/iommufd/iommufd_private.h |  41 +-------
 drivers/iommu/iommufd/device.c          | 101 +++++++++++++++++++
 drivers/iommu/iommufd/fault.c           | 128 +-----------------------
 3 files changed, 109 insertions(+), 161 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 iommufd: Make attach_handle generic than fault specific

"attach_handle" was added exclusively for the iommufd_fault_iopf_handler() used by IOPF/PRI use cases. Now, both the MSI and PASID series require to reuse the attach_handle for non-fault cases.

Add a set of new attach/detach/replace helpers that does the attach_handle allocation/releasing/replacement in the common path and also handles those fault specific routines such as iopf enabling/disabling and auto response.

This covers both non-fault and fault cases in a clean way, replacing those inline helpers in the header. The following patch will clean up those old helpers in the fault.c file.

---
 drivers/iommu/iommufd/iommufd_private.h |  33 +-------
 drivers/iommu/iommufd/device.c          | 101 ++++++++++++++++++++++++
 drivers/iommu/iommufd/fault.c           |   8 +-
 3 files changed, 109 insertions(+), 33 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +4 -29
index 0b1bafc7fd99..02fe1ada97cc 100644
@@ -504,35 +504,10 @@ int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
 				     struct iommufd_hw_pagetable *hwpt,
 				     struct iommufd_hw_pagetable *old);
 
-static inline int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
-					     struct iommufd_device *idev)
-{
-	if (hwpt->fault)
-		return iommufd_fault_domain_attach_dev(hwpt, idev);
-
-	return iommu_attach_group(hwpt->domain, idev->igroup->group);
-}
-
-static inline void iommufd_hwpt_detach_device(struct iommufd_hw_pagetable *hwpt,
-					      struct iommufd_device *idev)
-{
-	if (hwpt->fault) {
-		iommufd_fault_domain_detach_dev(hwpt, idev);
-		return;
-	}
-
-	iommu_detach_group(hwpt->domain, idev->igroup->group);
-}
-
-static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
-					      struct iommufd_hw_pagetable *hwpt,
-					      struct iommufd_hw_pagetable *old)
-{
-	if (old->fault || hwpt->fault)
-		return iommufd_fault_domain_replace_dev(idev, hwpt, old);
-
-	return iommu_group_replace_domain(idev->igroup->group, hwpt->domain);
-}
+int iommufd_fault_iopf_enable(struct iommufd_device *idev);
+void iommufd_fault_iopf_disable(struct iommufd_device *idev);
+void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
+				  struct iommufd_attach_handle *handle);
 
 static inline struct iommufd_viommu *
 iommufd_get_viommu(struct iommufd_ucmd *ucmd, u32 id)
drivers/iommu/iommufd/device.c +101 -0

107 lines not on this page · raw message

drivers/iommu/iommufd/fault.c +4 -4
index d9a937450e55..cb844e6799d4 100644
@@ -17,7 +17,7 @@
 #include "../iommu-priv.h"
 #include "iommufd_private.h"
 
-static int iommufd_fault_iopf_enable(struct iommufd_device *idev)
+int iommufd_fault_iopf_enable(struct iommufd_device *idev)
 {
 	struct device *dev = idev->dev;
 	int ret;
@@ -50,7 +50,7 @@ static int iommufd_fault_iopf_enable(struct iommufd_device *idev)
 	return ret;
 }
 
-static void iommufd_fault_iopf_disable(struct iommufd_device *idev)
+void iommufd_fault_iopf_disable(struct iommufd_device *idev)
 {
 	mutex_lock(&idev->iopf_lock);
 	if (!WARN_ON(idev->iopf_enabled == 0)) {
@@ -98,8 +98,8 @@ int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt,
 	return ret;
 }
 
-static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
-					 struct iommufd_attach_handle *handle)
+void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
+				  struct iommufd_attach_handle *handle)
 {
 	struct iommufd_fault *fault = hwpt->fault;
 	struct iopf_group *group, *next;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] iommufd: Make attach_handle generic than fault specific

Quoted 12 lines · Nicolin Chen

"attach_handle" was added exclusively for the iommufd_fault_iopf_handler() used by IOPF/PRI use cases. Now, both the MSI and PASID series require to reuse the attach_handle for non-fault cases.

Add a set of new attach/detach/replace helpers that does the attach_handle allocation/releasing/replacement in the common path and also handles those fault specific routines such as iopf enabling/disabling and auto response.

This covers both non-fault and fault cases in a clean way, replacing those inline helpers in the header. The following patch will clean up those old helpers in the fault.c file.

Quoted 6 lines
---
  drivers/iommu/iommufd/iommufd_private.h |  33 +-------
  drivers/iommu/iommufd/device.c          | 101 ++++++++++++++++++++++++
  drivers/iommu/iommufd/fault.c           |   8 +-
  3 files changed, 109 insertions(+), 33 deletions(-)
Signature · 2 lines
Regards,
Yi Liu
maintainer
#3 lore raw

Re: [PATCH v1 1/2] iommufd: Make attach_handle generic than fault specific

In reply to Nicolin Chen
Quoted 12 lines · Nicolin Chen

"attach_handle" was added exclusively for the iommufd_fault_iopf_handler() used by IOPF/PRI use cases. Now, both the MSI and PASID series require to reuse the attach_handle for non-fault cases.

Add a set of new attach/detach/replace helpers that does the attach_handle allocation/releasing/replacement in the common path and also handles those fault specific routines such as iopf enabling/disabling and auto response.

This covers both non-fault and fault cases in a clean way, replacing those inline helpers in the header. The following patch will clean up those old helpers in the fault.c file.

Quoted 6 lines
---
 drivers/iommu/iommufd/iommufd_private.h |  33 +-------
 drivers/iommu/iommufd/device.c          | 101 ++++++++++++++++++++++++
 drivers/iommu/iommufd/fault.c           |   8 +-
 3 files changed, 109 insertions(+), 33 deletions(-)

I am going to add some lockdeps to this:

drivers/iommu/iommufd/device.c +4 -0
index 360ba3ed85455e..0786290b4056df 100644
@@ -360,6 +360,8 @@ static int iommufd_hwpt_attach_device(struct iommufd_hw_pagetable *hwpt,
 	struct iommufd_attach_handle *handle;
 	int rc;
 
+	lockdep_assert_held(&idev->igroup->lock);
+
 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
 	if (!handle)
 		return -ENOMEM;
@@ -391,6 +393,8 @@ iommufd_device_get_attach_handle(struct iommufd_device *idev)
 {
 	struct iommu_attach_handle *handle;
 
+	lockdep_assert_held(&idev->igroup->lock);
+
 	handle =
 		iommu_attach_handle_get(idev->igroup->group, IOMMU_NO_PASID, 0);
 	if (IS_ERR(handle))

I think the one in get_attach_handle is important to discourage misuse of that ..

Thanks,
Jason

#4 lore raw

PATCH 2/2 iommufd/fault: Remove iommufd_fault_domain_attach/detach/replace_dev()

In reply to Nicolin Chen

There are new attach/detach/replace helpers in device.c taking care of both the attach_handle and the fault specific routines for iopf_enable/disable() and auto response.

Clean up these redundant functions in the fault.c file.

---
 drivers/iommu/iommufd/iommufd_private.h |   8 --
 drivers/iommu/iommufd/fault.c           | 120 ------------------------
 2 files changed, 128 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +0 -8
index 02fe1ada97cc..8e0e3ab64747 100644
@@ -496,14 +496,6 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd);
 void iommufd_fault_destroy(struct iommufd_object *obj);
 int iommufd_fault_iopf_handler(struct iopf_group *group);
 
-int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt,
-				    struct iommufd_device *idev);
-void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
-				     struct iommufd_device *idev);
-int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
-				     struct iommufd_hw_pagetable *hwpt,
-				     struct iommufd_hw_pagetable *old);
-
 int iommufd_fault_iopf_enable(struct iommufd_device *idev);
 void iommufd_fault_iopf_disable(struct iommufd_device *idev);
 void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
drivers/iommu/iommufd/fault.c +0 -120

132 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH v1 2/2] iommufd/fault: Remove iommufd_fault_domain_attach/detach/replace_dev()

Quoted 11 lines · Nicolin Chen

There are new attach/detach/replace helpers in device.c taking care of both the attach_handle and the fault specific routines for iopf_enable/disable() and auto response.

Clean up these redundant functions in the fault.c file.

---
  drivers/iommu/iommufd/iommufd_private.h |   8 --
  drivers/iommu/iommufd/fault.c           | 120 ------------------------
  2 files changed, 128 deletions(-)
Signature · 2 lines
Regards,
Yi Liu
maintainer
#6 lore raw
In reply to Nicolin Chen
Quoted 9 lines · Nicolin Chen

An attach_handle is a hwpt->fault specific thing, so the fault.c has been holding a set of attach/detach/replace helpers for attach_handle and iopf enabling/disabling.

Now, both the iommufd_msi series [1] and the attach_pasid series [2] want to reuse the attach_handle for non-fault cases. Rework the attach_handle, to prepare for either of the series. It should be merged with either that gets merged first.

Quoted 5 lines

[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://lore.kernel.org/linux-iommu/[email protected]/

Patches in this series are from [1] with some fixes addressing the review comments.

Applied, the other patches can rebase on top of this

Thanks,
Jason