Skip to contentSkip to conversation

iommu/amd: Cleanup and fixes

PATCH In review Vasant Hegde via iommu 2 participants 7 messages Mar 1, 2022 – Mar 4, 2022

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

v2 cover iommu/amd: Cleanup and fixes

This series contains various cleanup and trivial fixes.

Changes in v2:
- Fixed error log message in patch 1 as suggested by Joerg.

Suravee Suthikulpanit (2):
iommu/amd: Improve error handling for amd_iommu_init_pci iommu/amd: Improve amd_iommu_v2_exit()

Vasant Hegde (3):
iommu/amd: Call memunmap in error path
iommu/amd: Clean up function declarations
iommu/amd: Remove unused struct fault.devid

 drivers/iommu/amd/amd_iommu.h |  4 ----
 drivers/iommu/amd/init.c      | 18 ++++++++++++++----
 drivers/iommu/amd/iommu_v2.c  | 35 +++++++++++++++++------------------
 3 files changed, 31 insertions(+), 26 deletions(-)
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#1 lore raw

PATCH v2 1/5 iommu/amd: Improve error handling for amd_iommu_init_pci

From: Suravee Suthikulpanit <[email protected]>

Add error messages to prevent silent failure.

---
 drivers/iommu/amd/init.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/iommu/amd/init.c +11 -3
index b10fb52ea442..6b5af568f3d5 100644
@@ -1943,9 +1943,11 @@ static int __init amd_iommu_init_pci(void)
 
 	for_each_iommu(iommu) {
 		ret = iommu_init_pci(iommu);
-		if (ret)
-			break;
-
+		if (ret) {
+			pr_err("IOMMU%d: Failed to initialize IOMMU Hardware (error=%d)!\n",
+			       iommu->index, ret);
+			goto out;
+		}
 		/* Need to setup range after PCI init */
 		iommu_set_cwwb_range(iommu);
 	}
@@ -1961,6 +1963,11 @@ static int __init amd_iommu_init_pci(void)
 	 * active.
 	 */
 	ret = amd_iommu_init_api();
+	if (ret) {
+		pr_err("IOMMU: Failed to initialize IOMMU-API interface (error=%d)!\n",
+		       ret);
+		goto out;
+	}
 
 	init_device_table_dma();
 
@@ -1970,6 +1977,7 @@ static int __init amd_iommu_init_pci(void)
 	if (!ret)
 		print_iommu_info();
 
+out:
 	return ret;
 }
 
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#2 lore raw

PATCH v2 2/5 iommu/amd: Call memunmap in error path

In reply to Vasant Hegde via iommu

Unmap old_devtb in error path.

---
 drivers/iommu/amd/init.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/amd/init.c +2 -0
index 6b5af568f3d5..f7e7d208063c 100644
@@ -980,6 +980,7 @@ static bool copy_device_table(void)
 				get_order(dev_table_size));
 	if (old_dev_tbl_cpy == NULL) {
 		pr_err("Failed to allocate memory for copying old device table!\n");
+		memunmap(old_devtb);
 		return false;
 	}
 
@@ -1010,6 +1011,7 @@ static bool copy_device_table(void)
 			if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
 			    (int_tab_len != DTE_INTTABLEN)) {
 				pr_err("Wrong old irq remapping flag: %#x\n", devid);
+				memunmap(old_devtb);
 				return false;
 			}
 
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#3 lore raw

PATCH v2 3/5 iommu/amd: Clean up function declarations

In reply to Vasant Hegde via iommu

Remove unused declarations and add static keyword as needed.

---
 drivers/iommu/amd/amd_iommu.h | 4 ----
 drivers/iommu/amd/init.c      | 2 +-
 2 files changed, 1 insertion(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -4
index 416815a525d6..94d33626d692 100644
@@ -14,10 +14,6 @@
 extern irqreturn_t amd_iommu_int_thread(int irq, void *data);
 extern irqreturn_t amd_iommu_int_handler(int irq, void *data);
 extern void amd_iommu_apply_erratum_63(u16 devid);
-extern void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu);
-extern int amd_iommu_init_devices(void);
-extern void amd_iommu_uninit_devices(void);
-extern void amd_iommu_init_notifier(void);
 extern int amd_iommu_init_api(void);
 
 #ifdef CONFIG_AMD_IOMMU_DEBUGFS
drivers/iommu/amd/init.c +1 -1
index f7e7d208063c..07d6dd302989 100644
@@ -661,7 +661,7 @@ static int __init alloc_command_buffer(struct amd_iommu *iommu)
  * This function resets the command buffer if the IOMMU stopped fetching
  * commands from it.
  */
-void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
+static void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
 {
 	iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
 
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#4 lore raw

PATCH v2 4/5 iommu/amd: Remove unused struct fault.devid

In reply to Vasant Hegde via iommu

This variable has not been used since it was introduced.

---
 drivers/iommu/amd/iommu_v2.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iommu/amd/iommu_v2.c +0 -1
index 58da08cc3d01..2daf37c21b85 100644
@@ -71,7 +71,6 @@ struct fault {
 	struct pasid_state *state;
 	struct mm_struct *mm;
 	u64 address;
-	u16 devid;
 	u32 pasid;
 	u16 tag;
 	u16 finish;
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#5 lore raw

PATCH v2 5/5 iommu/amd: Improve amd_iommu_v2_exit()

In reply to Vasant Hegde via iommu

From: Suravee Suthikulpanit <[email protected]>

During module exit, the current logic loops through all possible 16-bit device ID space to search for existing devices and clean up device state structures. This can be simplified by looping through the device state list.

Also, refactor various clean up logic into free_device_state() for better reusability.

---
 drivers/iommu/amd/iommu_v2.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/iommu/amd/iommu_v2.c +17 -17
index 2daf37c21b85..c72969ac4956 100644
@@ -24,7 +24,6 @@
 MODULE_LICENSE("GPL v2");
 MODULE_AUTHOR("Joerg Roedel <[email protected]>");
 
-#define MAX_DEVICES		0x10000
 #define PRI_QUEUE_SIZE		512
 
 struct pri_queue {
@@ -124,6 +123,15 @@ static void free_device_state(struct device_state *dev_state)
 {
 	struct iommu_group *group;
 
+	/* Get rid of any remaining pasid states */
+	free_pasid_states(dev_state);
+
+	/*
+	 * Wait until the last reference is dropped before freeing
+	 * the device state.
+	 */
+	wait_event(dev_state->wq, !atomic_read(&dev_state->count));
+
 	/*
 	 * First detach device from domain - No more PRI requests will arrive
 	 * from that device after it is unbound from the IOMMUv2 domain.
@@ -849,15 +857,7 @@ void amd_iommu_free_device(struct pci_dev *pdev)
 
 	spin_unlock_irqrestore(&state_lock, flags);
 
-	/* Get rid of any remaining pasid states */
-	free_pasid_states(dev_state);
-
 	put_device_state(dev_state);
-	/*
-	 * Wait until the last reference is dropped before freeing
-	 * the device state.
-	 */
-	wait_event(dev_state->wq, !atomic_read(&dev_state->count));
 	free_device_state(dev_state);
 }
 EXPORT_SYMBOL(amd_iommu_free_device);
@@ -954,8 +954,8 @@ static int __init amd_iommu_v2_init(void)
 
 static void __exit amd_iommu_v2_exit(void)
 {
-	struct device_state *dev_state;
-	int i;
+	struct device_state *dev_state, *next;
+	unsigned long flags;
 
 	if (!amd_iommu_v2_supported())
 		return;
@@ -968,18 +968,18 @@ static void __exit amd_iommu_v2_exit(void)
 	 * The loop below might call flush_workqueue(), so call
 	 * destroy_workqueue() after it
 	 */
-	for (i = 0; i < MAX_DEVICES; ++i) {
-		dev_state = get_device_state(i);
-
-		if (dev_state == NULL)
-			continue;
+	spin_lock_irqsave(&state_lock, flags);
 
+	list_for_each_entry_safe(dev_state, next, &state_list, list) {
 		WARN_ON_ONCE(1);
 
 		put_device_state(dev_state);
-		amd_iommu_free_device(dev_state->pdev);
+		list_del(&dev_state->list);
+		free_device_state(dev_state);
 	}
 
+	spin_unlock_irqrestore(&state_lock, flags);
+
 	destroy_workqueue(iommu_wq);
 }
 
Signature · 6 lines
2.27.0

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
maintainer
#6 lore raw
In reply to Vasant Hegde via iommu
Quoted 6 lines · Vasant Hegde

This series contains various cleanup and trivial fixes.

Changes in v2:
- Fixed error log message in patch 1 as suggested by Joerg.

Quoted 8 lines

Suravee Suthikulpanit (2):
iommu/amd: Improve error handling for amd_iommu_init_pci iommu/amd: Improve amd_iommu_v2_exit()

Vasant Hegde (3):
iommu/amd: Call memunmap in error path
iommu/amd: Clean up function declarations
iommu/amd: Remove unused struct fault.devid

Applied, thanks Vasant and Suravee.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu