[PATCH v3 10/11] iommu: Add debugging on domain lifetime for iopf

From: Lu Baolu
Date: Thu Aug 17 2023 - 19:44:59 EST


The iopf handling framework in the core requires the domain's lifetime
should cover all possible iopfs. This has been documented in the comments
for iommu_queue_iopf() which is the entry of the framework.

Add some debugging to enforce this.

Suggested-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Signed-off-by: Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx>
---
drivers/iommu/io-pgfault.c | 3 +++
drivers/iommu/iommu.c | 24 ++++++++++++++++++++++++
2 files changed, 27 insertions(+)

diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c
index a61c2aabd1b8..bf667ed39b01 100644
--- a/drivers/iommu/io-pgfault.c
+++ b/drivers/iommu/io-pgfault.c
@@ -129,6 +129,9 @@ int iommu_queue_iopf(struct iommu_fault *fault, struct device *dev)
domain = iommu_get_domain_for_dev(dev);

if (!domain || !domain->iopf_handler) {
+ dev_warn_ratelimited(dev,
+ "iopf from pasid %d received without handler installed\n",
+ fault->prm.pasid);
ret = -ENODEV;
goto cleanup_partial;
}
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 9b622088c741..c170bcd3f05e 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2032,6 +2032,28 @@ int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
return 0;
}

+static void assert_no_pending_iopf(struct device *dev, ioasid_t pasid)
+{
+ struct iommu_fault_param *iopf_param = dev->iommu->fault_param;
+ struct iommu_fault_event *evt;
+ struct iopf_fault *iopf;
+
+ if (!iopf_param)
+ return;
+
+ mutex_lock(&iopf_param->lock);
+ list_for_each_entry(iopf, &iopf_param->partial, list) {
+ if (WARN_ON(iopf->fault.prm.pasid == pasid))
+ break;
+ }
+
+ list_for_each_entry(evt, &iopf_param->faults, list) {
+ if (WARN_ON(evt->fault.prm.pasid == pasid))
+ break;
+ }
+ mutex_unlock(&iopf_param->lock);
+}
+
void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
{
/* Caller must be a probed driver on dev */
@@ -2040,6 +2062,7 @@ void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
if (!group)
return;

+ assert_no_pending_iopf(dev, IOMMU_NO_PASID);
mutex_lock(&group->mutex);
if (WARN_ON(domain != group->domain) ||
WARN_ON(list_count_nodes(&group->devices) != 1))
@@ -3340,6 +3363,7 @@ void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
/* Caller must be a probed driver on dev */
struct iommu_group *group = dev->iommu_group;

+ assert_no_pending_iopf(dev, pasid);
mutex_lock(&group->mutex);
__iommu_remove_group_pasid(group, pasid);
WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
--
2.34.1