Skip to contentSkip to conversation

iommu/amd: Improve updates, GCR3 teardown, and completions

PATCH New Vasant Hegde 1 participant 8 messages Nov 27

Subsystems (2)
lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

v2 cover iommu/amd: Improve updates, GCR3 teardown, and completions

Various enhancement to command completion handling, fix page accounting issue, remove redundant dte flush, etc.

- attach_device() publishes a device on dev_list under the domain lock, then writes the DTE after dropping it. change_top() and dirty tracking walk that list under the same lock, so the new DTE can store a stale page-table root or drop the HDAT bit. detach_device() clears the DTE before taking the lock, and a concurrent walk can make the entry valid again. Hold the domain lock across both the list update and the DTE write. defer_attach and use_vapic are updated only after attach succeeds. change_top() no longer repeats the alias clone and DTE flush that set_dte_entry() already performs.

- GCR3 intermediate pages were allocated with get_zeroed_page() and freed with iommu_free_pages(), so the IOMMU page counters drift on teardown. Allocate them with iommu_alloc_pages_node_sz() on the IOMMU node. A clear does not allocate a missing intermediate table, and a two-level table frees its level-1 pages as well as the leaves.

- iommu_flush_irt_and_complete() reserved a completion-wait sequence before the invalidate commands were queued, and dropped it if queueing failed. Reserve the sequence only after those commands are queued.

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

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

Changes since v1:
- Rebased on top of v7.4-rc3
- Addressed AI review comments

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

Vasant Hegde (7):
iommu/amd: Drop redundant flushes when the page table top changes iommu/amd: Fix page-counter drift in multi-level GCR3 table teardown iommu/amd: Do not allocate GCR3 tables when clearing a PASID iommu/amd: Allocate the completion sequence after queueing commands iommu/amd: Free the level-1 tables of a two-level GCR3 table iommu/amd: Update device attach state only after a successful attach iommu/amd: Write the DTE under the domain lock on attach and detach

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

PATCH v2 1/7 iommu/amd: Drop redundant flushes when the page table top changes

amd_iommu_change_top() updates each device's DTE via set_dte_entry(), which already clones the aliases, flushes the DTE and device IOTLB, and waits for completion. Hence remove redundant clone_aliases and dte flush from amd_iommu_change_top().

Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/iommu.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/iommu/amd/iommu.c +5 -8
index 0b87d4252906..22aef8ce98e2 100644
@@ -2623,19 +2623,16 @@ static void amd_iommu_change_top(struct pt_iommu *iommu_table,
 
 	lockdep_assert_held(&pdom->lock);
 
-	/* Update the DTE for all devices attached to this domain */
+	/*
+	 * Update the DTE for all devices attached to this domain. This also
+	 * updates the aliases and flushes the DTEs.
+	 */
 	list_for_each_entry(dev_data, &pdom->dev_list, list) {
-		struct amd_iommu *iommu = rlookup_amd_iommu(dev_data->dev);
+		struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
 
 		/* Update the HW references with the new level and top ptr */
 		set_dte_entry(iommu, dev_data, top_paddr, top_level);
-		clone_aliases(iommu, dev_data->dev);
 	}
-
-	list_for_each_entry(dev_data, &pdom->dev_list, list)
-		device_flush_dte(dev_data);
-
-	domain_flush_complete(pdom);
 }
 
 /*
Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH v2 2/7 iommu/amd: Fix page-counter drift in multi-level GCR3 table teardown

In reply to Vasant Hegde

__get_gcr3_pte() allocates intermediate GCR3 tables with get_zeroed_page(), but free_gcr3_tbl_level1() releases them with iommu_free_pages(). iommu_free_pages() decrements NR_IOMMU_PAGES and NR_SECONDARY_PAGETABLE, but get_zeroed_page() never incremented those counters. So every teardown of a multi-level PASID (GCR3) table makes both counters drift negative.

Allocate the sub tables with iommu_alloc_pages_node_sz() on the IOMMU node. That matches the GCR3 root allocation, so the alloc and free paths are symmetric and the page counters stay balanced.

Fixes 75114cbaa136 ("iommu/amd: use page allocation function provided by iommu-pages.h")
---
 drivers/iommu/amd/iommu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iommu/amd/iommu.c +5 -3
index 22aef8ce98e2..ba6492e9bf0f 100644
@@ -2002,7 +2002,7 @@ static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info,
 }
 
 static u64 *__get_gcr3_pte(struct gcr3_tbl_info *gcr3_info,
-			   ioasid_t pasid, bool alloc)
+			   ioasid_t pasid, bool alloc, int nid)
 {
 	int index;
 	u64 *pte;
@@ -2021,7 +2021,7 @@ static u64 *__get_gcr3_pte(struct gcr3_tbl_info *gcr3_info,
 			if (!alloc)
 				return NULL;
 
-			root = (void *)get_zeroed_page(GFP_ATOMIC);
+			root = iommu_alloc_pages_node_sz(nid, GFP_ATOMIC, SZ_4K);
 			if (root == NULL)
 				return NULL;
 
@@ -2040,9 +2040,11 @@ static int update_gcr3(struct iommu_dev_data *dev_data,
 		       ioasid_t pasid, unsigned long gcr3, bool set)
 {
 	struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
+	struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
+	int nid = iommu ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE;
 	u64 *pte;
 
-	pte = __get_gcr3_pte(gcr3_info, pasid, true);
+	pte = __get_gcr3_pte(gcr3_info, pasid, true, nid);
 	if (pte == NULL)
 		return -ENOMEM;
 
Signature · 1 line
2.31.1
author maintainer
#3 lore raw

PATCH v2 3/7 iommu/amd: Do not allocate GCR3 tables when clearing a PASID

In reply to Vasant Hegde

Allocate gcr3 intermediate tables only when installing an entry. A missing intermediate table means nothing was installed under it, so a clear simply succeeds.

Fixes e8e1aac33458 ("iommu/amd: Refactor helper function for setting / clearing GCR3")
Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/iommu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/amd/iommu.c +2 -2
index ba6492e9bf0f..cd34ca735da7 100644
@@ -2044,9 +2044,9 @@ static int update_gcr3(struct iommu_dev_data *dev_data,
 	int nid = iommu ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE;
 	u64 *pte;
 
-	pte = __get_gcr3_pte(gcr3_info, pasid, true, nid);
+	pte = __get_gcr3_pte(gcr3_info, pasid, set, nid);
 	if (pte == NULL)
-		return -ENOMEM;
+		return set ? -ENOMEM : 0;
 
 	if (set)
 		*pte = (gcr3 & PAGE_MASK) | GCR3_VALID;
Signature · 1 line
2.31.1
author maintainer
#4 lore raw

PATCH v2 4/7 iommu/amd: Allocate the completion sequence after queueing commands

In reply to Vasant Hegde

iommu_flush_irt_and_complete() reserves a completion-wait sequence number (via get_cmdsem_val()) before queueing the invalidate_irt commands. If one of those commands fails to queue, it returns early without ever queueing the completion wait that would store that sequence number.

Currently this is harmless, since iommu_completion_wait() is only called after some command has been queued. But allocating a sequence number and not using it may cause problems in the future.

Reserve and queue the completion wait only after the invalidate_irt commands are queued. Move the locked sequence allocation and queueing into a new __iommu_completion_wait() helper, called from both iommu_completion_wait() and iommu_flush_irt_and_complete(). The caller holds iommu->lock; the semaphore wait stays outside the lock.

---
 drivers/iommu/amd/iommu.c | 50 ++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 27 deletions(-)
drivers/iommu/amd/iommu.c +23 -27
index cd34ca735da7..a9bb8d50146f 100644
@@ -1436,24 +1436,11 @@ static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
 	return iommu_queue_command_sync(iommu, cmd, true);
 }
 
-static u64 get_cmdsem_val(struct amd_iommu *iommu)
-{
-	lockdep_assert_held(&iommu->lock);
-	return ++iommu->cmd_sem_val;
-}
-
-/*
- * This function queues a completion wait command into the command
- * buffer of an IOMMU
- */
-static int iommu_completion_wait(struct amd_iommu *iommu)
+static int __iommu_completion_wait(struct amd_iommu *iommu, u64 *data)
 {
 	struct iommu_cmd cmd;
-	unsigned long flags;
-	int ret;
-	u64 data;
 
-	raw_spin_lock_irqsave(&iommu->lock, flags);
+	lockdep_assert_held(&iommu->lock);
 
 	if (!iommu->need_sync) {
 		/*
@@ -1465,15 +1452,28 @@ static int iommu_completion_wait(struct amd_iommu *iommu)
 		 * command queued before this call (possibly on another CPU)
 		 * is guaranteed to have completed before returning.
 		 */
-		data = iommu->cmd_sem_val;
-		raw_spin_unlock_irqrestore(&iommu->lock, flags);
-		return wait_on_sem(iommu, data);
+		*data = iommu->cmd_sem_val;
+		return 0;
 	}
 
-	data = get_cmdsem_val(iommu);
-	build_completion_wait(&cmd, iommu, data);
+	*data = ++iommu->cmd_sem_val;
+	build_completion_wait(&cmd, iommu, *data);
+
+	return __iommu_queue_command_sync(iommu, &cmd, false);
+}
+
+/*
+ * This function queues a completion wait command into the command
+ * buffer of an IOMMU
+ */
+static int iommu_completion_wait(struct amd_iommu *iommu)
+{
+	unsigned long flags;
+	int ret;
+	u64 data;
 
-	ret = __iommu_queue_command_sync(iommu, &cmd, false);
+	raw_spin_lock_irqsave(&iommu->lock, flags);
+	ret = __iommu_completion_wait(iommu, &data);
 	raw_spin_unlock_irqrestore(&iommu->lock, flags);
 
 	if (ret)
@@ -3287,7 +3287,6 @@ static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
 	int ret;
 	u64 data;
 	unsigned long flags;
-	struct iommu_cmd cmd;
 	struct pci_dev *pdev = NULL;
 	struct iommu_dev_data *dev_data = search_dev_data(iommu, devid);
 
@@ -3298,9 +3297,6 @@ static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
 		pdev = to_pci_dev(dev_data->dev);
 
 	raw_spin_lock_irqsave(&iommu->lock, flags);
-	data = get_cmdsem_val(iommu);
-	build_completion_wait(&cmd, iommu, data);
-
 	if (pdev)
 		ret = pci_for_each_dma_alias(pdev, iommu_flush_dev_irt, iommu);
 	else
@@ -3308,11 +3304,11 @@ static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
 	if (ret)
 		goto out_err;
 
-	ret = __iommu_queue_command_sync(iommu, &cmd, false);
+	ret = __iommu_completion_wait(iommu, &data);
 	if (ret)
 		goto out_err;
-	raw_spin_unlock_irqrestore(&iommu->lock, flags);
 
+	raw_spin_unlock_irqrestore(&iommu->lock, flags);
 	wait_on_sem(iommu, data);
 	return;
 
Signature · 1 line
2.31.1
author maintainer
#5 lore raw

PATCH v2 5/7 iommu/amd: Free the level-1 tables of a two-level GCR3 table

In reply to Vasant Hegde

free_gcr3_tbl_level2() frees the leaf tables under each level-1 table via free_gcr3_tbl_level1(), but never frees the level-1 tables themselves. So tearing down a two-level GCR3 table leaks every level-1 table.

This is not reachable today: PASIDs are limited to 16 bits (PASID_MASK), so setup_gcr3_table() only ever needs a single level above the leaves. Free the level-1 tables too, so the table can later be grown for 20-bit PASIDs without leaking.

---
 drivers/iommu/amd/iommu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/amd/iommu.c +1 -0
index a9bb8d50146f..49cccc969639 100644
@@ -1934,6 +1934,7 @@ static void free_gcr3_tbl_level2(u64 *tbl)
 		ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
 
 		free_gcr3_tbl_level1(ptr);
+		iommu_free_pages(ptr);
 	}
 }
 
Signature · 1 line
2.31.1
author maintainer
#6 lore raw

PATCH v2 6/7 iommu/amd: Update device attach state only after a successful attach

In reply to Vasant Hegde

amd_iommu_attach_device() clears dev_data->defer_attach before it has validated or attempted anything, and recomputes dev_data->use_vapic from the new domain after attach_device() returns, whether or not the attach succeeded.

When the attach fails, the new domain is not installed. Clearing defer_attach then drops the deferred attach for a device that was never attached to the new domain, and use_vapic ends up describing a domain the device is not using.

Clear defer_attach and update use_vapic only once attach_device() succeeds. An early failure, such as the dirty tracking check or set_device_perfopt(), now leaves both fields untouched.

---
 drivers/iommu/amd/iommu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iommu/amd/iommu.c +5 -3
index 49cccc969639..1c307310cc2c 100644
@@ -3017,8 +3017,6 @@ static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,
 	if (dev_data->domain == domain)
 		return 0;
 
-	dev_data->defer_attach = false;
-
 	/*
 	 * Restrict to devices with compatible IOMMU hardware support
 	 * when enforcement of dirty tracking is enabled.
@@ -3036,6 +3034,10 @@ static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,
 		detach_device(dev);
 
 	ret = attach_device(dev, domain);
+	if (ret)
+		return ret;
+
+	dev_data->defer_attach = false;
 
 #ifdef CONFIG_IRQ_REMAP
 	if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
@@ -3046,7 +3048,7 @@ static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,
 	}
 #endif
 
-	return ret;
+	return 0;
 }
 
 static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap)
Signature · 1 line
2.31.1
author maintainer
#7 lore raw

PATCH v2 7/7 iommu/amd: Write the DTE under the domain lock on attach and detach

In reply to Vasant Hegde

amd_iommu_change_top() and amd_iommu_set_dirty_tracking() rewrite the DTE of every device on the domain's dev_list while holding the domain lock. attach_device() adds the device to dev_list under that lock, but builds and writes its DTE after dropping it.

On concurrent update it's possible that it may end up writing old top or losing HAD bits.

detach_device() has the mirror problem: it clears the DTE before taking the domain lock, so a concurrent change_top() can make the DTE valid again before the device is removed from dev_list.

Hold the domain lock across both the dev_list update and the DTE write, in both the attach and detach paths. This follows the existing pdom->lock -> dte_lock -> iommu->lock ordering already used by change_top().

---
 drivers/iommu/amd/iommu.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/iommu/amd/iommu.c +9 -6
index 1c307310cc2c..45298d31b183 100644
@@ -2396,14 +2396,16 @@ static int attach_device(struct device *dev,
 	}
 
 skip_caps:
-	/* Update data structures */
-	dev_data->domain = domain;
+	/*
+	 * Build and write the DTE under the domain lock so a concurrent
+	 * change_top() or dirty tracking update cannot be overwritten with
+	 * stale values.
+	 */
 	spin_lock_irqsave(&domain->lock, flags);
+	dev_data->domain = domain;
 	list_add(&dev_data->list, &domain->dev_list);
-	spin_unlock_irqrestore(&domain->lock, flags);
-
-	/* Update device table */
 	dev_update_dte(dev_data, true);
+	spin_unlock_irqrestore(&domain->lock, flags);
 
 out:
 	mutex_unlock(&dev_data->mutex);
@@ -2441,11 +2443,12 @@ static void detach_device(struct device *dev)
 	if (dev_is_pci(dev))
 		pdev_disable_caps(to_pci_dev(dev));
 
+	spin_lock_irqsave(&domain->lock, flags);
+
 	/* Clear DTE and flush the entry */
 	dev_update_dte(dev_data, false);
 
 	/* Flush IOTLB and wait for the flushes to finish */
-	spin_lock_irqsave(&domain->lock, flags);
 	amd_iommu_domain_flush_all(domain);
 	list_del(&dev_data->list);
 	spin_unlock_irqrestore(&domain->lock, flags);
Signature · 1 line
2.31.1