Skip to contentSkip to conversation

iommu/amd: Fix init teardown and IOMMU interrupt setup

PATCH New Vasant Hegde 1 participant 6 messages Nov 5

Subsystems (2)
lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

cover iommu/amd: Fix init teardown and IOMMU interrupt setup

Init failure tears the IOMMUs down in an order the hardware and the protection-domain IDA cannot survive, and interrupt setup can select XT mode or drop the shared INTCAPXT domain while IRQs are still mapped.

- free_dma_resources() destroyed the protection-domain IDA before the group default domains were freed, so ida_free() warned for every domain released during init failure. Destroy the IDA after the domains have released their IDs.

- The failure path disabled the IOMMUs and then unregistered them. Clearing a DTE queues a completion wait, and the command processor was already off, so every wait timed out. Unregister first, while the IOMMUs still accept commands, then disable the hardware.

- IVHD XTSup was treated as the mode to run in, so CONTROL_XT_EN and INTCAPXT were enabled even when the kernel cannot use x2APIC. Stay in xAPIC mode and use MSI unless x2apic_supported() is true. iommu_init_irq() logs the missing MSI capability and the errno when interrupt allocation fails.

- The INTCAPXT irq domain is shared by every IOMMU and by the event, PPR, and GA interrupts. Removing it when one request failed tripped the revmap warning and left iommu_irqdomain pointing at a freed domain. Free only the failed IRQ, and remove the domain once its

Full series is available at :
https://github.com/AMDESE/linux-iommu/tree/iommu_code_audit_7.4_v1

Vasant Hegde (5):
iommu/amd: Destroy protection domain IDs after the domains are freed iommu/amd: Unregister the IOMMUs before disabling them on init failure iommu/amd: Select XT mode only when the kernel supports x2APIC iommu/amd: Log error message when IOMMU interrupt setup failed iommu/amd: Remove the shared INTCAPXT domain only after its IRQs are freed

 drivers/iommu/amd/amd_iommu_types.h |   3 +
 drivers/iommu/amd/init.c            | 108 ++++++++++++++++++++++------
 2 files changed, 91 insertions(+), 20 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/5 iommu/amd: Destroy protection domain IDs after the domains are freed

free_dma_resources() destroyed the protection-domain IDA before free_iommu_resources() unregistered the IOMMU. Unregister frees the group default domains, and amd_iommu_domain_free() then calls ida_free() on an IDA that has already been destroyed. That warns for every domain ID released during init failure.

Move the destroy into free_iommu_resources(), after free_iommu_all() has unregistered the devices and the domains have released their IDs.

---
 drivers/iommu/amd/init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/amd/init.c +1 -1
index 28a4067a7a05..425c61c3956f 100644
@@ -3174,6 +3174,7 @@ static struct syscore amd_iommu_syscore = {
 static void __init free_iommu_resources(void)
 {
 	free_iommu_all();
+	amd_iommu_pdom_id_destroy();
 	free_pci_segments();
 }
 
@@ -3268,7 +3269,6 @@ static bool __init check_ioapic_information(void)
 
 static void __init free_dma_resources(void)
 {
-	amd_iommu_pdom_id_destroy();
 	free_unity_maps();
 }
 
Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH 2/5 iommu/amd: Unregister the IOMMUs before disabling them on init failure

In reply to Vasant Hegde

The init failure path disabled the IOMMUs and then called free_iommu_resources(), which unregisters each IOMMU. Unregistering releases the devices behind it and clearing a DTE queues an INVALIDATE_DEVTAB_ENTRY followed by a COMPLETION_WAIT. The command processor was already off, so every one of those waits spins for LOOP_TIMEOUT and reports:

Sample log:
AMD-Vi: IOMMU 0000:70:00.2: Completion-Wait loop timed out
AMD-Vi: CMD Buffer head=56 tail=58

Unregister the IOMMUs first, while they still process commands, then disable the hardware and free the resources.

---
 drivers/iommu/amd/init.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/iommu/amd/init.c +13 -3
index 425c61c3956f..877122898e05 100644
@@ -1792,9 +1792,19 @@ static void __init free_pci_segments(void)
 	}
 }
 
-static void __init free_sysfs(struct amd_iommu *iommu)
+/*
+ * Unregistering an IOMMU releases the devices behind it and clearing a
+ * DTE queues an invalidation and waits for it to complete. The IOMMUs
+ * must still accept commands.
+ */
+static void __init unregister_iommus(void)
 {
-	if (iommu->iommu.dev) {
+	struct amd_iommu *iommu;
+
+	for_each_iommu(iommu) {
+		if (!iommu->iommu.dev)
+			continue;
+
 		iommu_device_unregister(&iommu->iommu);
 		iommu_device_sysfs_remove(&iommu->iommu);
 	}
@@ -1802,7 +1812,6 @@ static void __init free_sysfs(struct amd_iommu *iommu)
 
 static void __init free_iommu_one(struct amd_iommu *iommu)
 {
-	free_sysfs(iommu);
 	free_iommu_buffers(iommu);
 	amd_iommu_free_ppr_log(iommu);
 	free_ga_log(iommu);
@@ -3631,6 +3640,7 @@ static int __init state_next(void)
 	if (ret) {
 		free_dma_resources();
 		if (!irq_remapping_enabled) {
+			unregister_iommus();
 			disable_iommus();
 			free_iommu_resources();
 		} else {
Signature · 1 line
2.31.1
author maintainer
#3 lore raw

PATCH 3/5 iommu/amd: Select XT mode only when the kernel supports x2APIC

In reply to Vasant Hegde

IVHD XTSup means the IOMMU can program a 32-bit APIC destination ID. The driver treated that bit as the mode it should run in, so it enabled CONTROL_XT_EN and allocated INTCAPXT interrupts even when x2APIC support was compiled out or disabled on the command line.

Stay in the default xAPIC mode unless x2apic_supported() is true. MSI remains the interrupt path for that mode.

Assisted-by Cursor
---
 drivers/iommu/amd/init.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +8 -4
index 877122898e05..9cb61c2665cf 100644
@@ -1948,13 +1948,17 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
 			amd_iommu_hatdis = true;
 		}
 
-		/* XT and GAM require GA mode. */
+		/*
+		 * XT and GAM require GA mode. XT mode programs 32-bit
+		 * APIC IDs, so select it only when the kernel can enable
+		 * x2APIC. Otherwise stay in xAPIC mode and use MSI.
+		 */
 		if ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0) {
 			amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
 			break;
-		} else {
-			if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
-				amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
+		} else if ((h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT)) &&
+			   x2apic_supported()) {
+			amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
 		}
 
 		early_iommu_features_init(iommu, h);
Signature · 1 line
2.31.1
author maintainer
#4 lore raw

PATCH 4/5 iommu/amd: Log error message when IOMMU interrupt setup failed

In reply to Vasant Hegde

iommu_init_irq() returns -ENODEV when XT mode was not selected and the IOMMU has no MSI capability. MSI and INTCAPXT failures return their own errno. The caller records the error and tears the IOMMU down without saying which step failed.

Log the missing capability, and log the errno when interrupt allocation fails.

Assisted-by Cursor
---
 drivers/iommu/amd/init.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iommu/amd/init.c +11 -5
index 9cb61c2665cf..d967544baa6c 100644
@@ -2641,15 +2641,21 @@ static int iommu_init_irq(struct amd_iommu *iommu)
 	if (iommu->int_enabled)
 		goto enable_faults;
 
-	if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
+	if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE) {
 		ret = iommu_setup_intcapxt(iommu);
-	else if (iommu->dev->msi_cap)
+	} else if (iommu->dev->msi_cap) {
 		ret = iommu_setup_msi(iommu);
-	else
-		ret = -ENODEV;
+	} else {
+		pr_err("IOMMU%d: x2APIC is unavailable and the IOMMU has no MSI capability\n",
+		       iommu->index);
+		return -ENODEV;
+	}
 
-	if (ret)
+	if (ret) {
+		pr_err("IOMMU%d: failed to enable interrupts (%d)\n",
+		       iommu->index, ret);
 		return ret;
+	}
 
 	iommu->int_enabled = true;
 enable_faults:
Signature · 1 line
2.31.1
author maintainer
#5 lore raw

PATCH 5/5 iommu/amd: Remove the shared INTCAPXT domain only after its IRQs are freed

In reply to Vasant Hegde

__iommu_setup_intcapxt() called irq_domain_remove() when allocation or request_threaded_irq() failed. That domain is shared by every IOMMU and by the event, PPR, and GA interrupts. Removing it while another IRQ is still mapped trips the revmap warning, and iommu_irqdomain keeps pointing at the freed domain.

Free only the IRQ whose request failed. Remember each requested IRQ and release it if a later interrupt fails or when the IOMMU is destroyed. Remove the domain, and clear iommu_irqdomain, once its last IRQ is gone.

Assisted-by Cursor
---
 drivers/iommu/amd/amd_iommu_types.h |  3 ++
 drivers/iommu/amd/init.c            | 62 +++++++++++++++++++++++++----
 2 files changed, 58 insertions(+), 7 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +3 -0
index 0e79c43be001..47a5ec3c8ef0 100644
@@ -686,18 +686,21 @@ struct amd_iommu {
 
 	/* Name for event log interrupt */
 	unsigned char evt_irq_name[16];
+	int evt_irq;
 
 	/* Base of the PPR log, if present */
 	u8 *ppr_log;
 
 	/* Name for PPR log interrupt */
 	unsigned char ppr_irq_name[16];
+	int ppr_irq;
 
 	/* Base of the GA log, if present */
 	u8 *ga_log;
 
 	/* Name for GA log interrupt */
 	unsigned char ga_irq_name[16];
+	int ga_irq;
 
 	/* Tail of the GA log, if present */
 	u8 *ga_log_tail;
drivers/iommu/amd/init.c +55 -7

111 lines not on this page · raw message

Signature · 1 line
2.31.1