Skip to contentSkip to conversation

iommu/amd: Various fixes to interrupt handler code path

PATCH New Vasant Hegde 1 participant 4 messages Nov 27

Subsystems (2)
lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

v2 cover iommu/amd: Various fixes to interrupt handler code path

Two of the handlers race their teardown. amd_iommu_report_page_fault() reads dev_data->domain with no lock, while detach_device() clears and frees that domain under dev_data->mutex. The GA log notifier is loaded twice outside an RCU read-side section, so synchronize_rcu() on unregister does not cover the call.

The I/O page-fault case also returns before the event slot is zeroed, so the entry is decoded again. Print the raw event before decoding it, so fields the decoder does not know about stay visible.

Full series including other cleanup is available at :
https://github.com/AMDESE/linux-iommu/tree/iommu_code_audit_7.4_v2

This series is made with the help of AI (Cursor).

Changes since v1:
- Droped "iommu/amd: Call the GA log notifier inside an RCU read section" as we have similar patch from Li.
- Rebased on top of v7.4-rc3.

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

Vasant Hegde (3):
iommu/amd: Clear the event log entry after reporting an I/O page fault iommu/amd: Hold the device mutex across a page-fault report iommu/amd: Log raw event log

 drivers/iommu/amd/iommu.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH v2 1/3 iommu/amd: Clear the event log entry after reporting an I/O page fault

The I/O page fault path returned early after reporting the fault, skipping the code that zeroes the slot. Make sure slot is cleared before returning.

---
 drivers/iommu/amd/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +2 -1
index 29a3374216b6..9913b0108f5f 100644
@@ -932,7 +932,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 
 	if (type == EVENT_TYPE_IO_FAULT) {
 		amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
-		return;
+		goto out;
 	}
 
 	switch (type) {
@@ -986,6 +986,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 			event[0], event[1], event[2], event[3]);
 	}
 
+out:
 	/*
 	 * To detect the hardware errata 732 we need to clear the
 	 * entry back to zero. This issue does not exist on SNP
Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH v2 2/3 iommu/amd: Hold the device mutex across a page-fault report

In reply to Vasant Hegde

amd_iommu_report_page_fault() read dev_data->domain outside any lock and then dereferenced it in report_iommu_fault(). detach_device() clears that pointer under dev_data->mutex and the domain is freed only after detach returns. Nothing prevented a concurrent detach from freeing the domain between the read and the report, leading to a potential use-after-free.

The fault runs in the threaded IRQ handler, which can sleep, so the same mutex can cover both the lookup and the report. Hold dev_data->mutex across reading dev_data->domain and calling report_iommu_fault(), so the domain cannot be detached and freed while it is in use.

---
 drivers/iommu/amd/iommu.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/iommu/amd/iommu.c +15 -6
index 9913b0108f5f..7259f80a36db 100644
@@ -836,8 +836,21 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
 		 * prevent logging it.
 		 */
 		if (IS_IOMMU_MEM_TRANSACTION(flags)) {
+			struct protection_domain *domain;
+			int handled = -ENODEV;
+
+			mutex_lock(&dev_data->mutex);
+			domain = dev_data->domain;
+			if (domain)
+				handled = report_iommu_fault(&domain->domain,
+							     &pdev->dev, address,
+							     IS_WRITE_REQUEST(flags) ?
+							     IOMMU_FAULT_WRITE :
+							     IOMMU_FAULT_READ);
+			mutex_unlock(&dev_data->mutex);
+
 			/* Device not attached to domain properly */
-			if (dev_data->domain == NULL) {
+			if (!domain) {
 				pr_err_ratelimited("Event logged [Device not attached to domain properly]\n");
 				pr_err_ratelimited("  device=%04x:%02x:%02x.%x domain=0x%04x\n",
 						   iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid),
@@ -845,11 +858,7 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
 				goto out;
 			}
 
-			if (!report_iommu_fault(&dev_data->domain->domain,
-						&pdev->dev, address,
-						IS_WRITE_REQUEST(flags) ?
-							IOMMU_FAULT_WRITE :
-							IOMMU_FAULT_READ))
+			if (!handled)
 				goto out;
 		}
 
Signature · 1 line
2.31.1
author maintainer
#3 lore raw

PATCH v2 3/3 iommu/amd: Log raw event log

In reply to Vasant Hegde

Log the raw event entry unconditionally before processing/decoding the event. This is useful for debugging especially when decoded log doesn't decode all the fields like newly introduced fields.

---
 drivers/iommu/amd/iommu.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iommu/amd/iommu.c +3 -0
index 7259f80a36db..0b87d4252906 100644
@@ -939,6 +939,9 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
 		goto retry;
 	}
 
+	dev_err_ratelimited(dev, "Raw event: evt[0]=0x%08x, evt[1]=0x%08x, evt[2]=0x%08x, evt[3]=0x%08x\n",
+			    event[0], event[1], event[2], event[3]);
+
 	if (type == EVENT_TYPE_IO_FAULT) {
 		amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
 		goto out;
Signature · 1 line
2.31.1