Skip to contentSkip to conversation

iommu/amd: Invalidate IRT cache for DMA aliases

PATCH Applied Magnus Kalland 3 participants 5 messages Apr 1 – Apr 2

Subsystems (2)

v4 of 5 Applied Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 iommu/amd: Invalidate IRT cache for DMA aliases

DMA aliasing causes interrupt remapping table entries (IRTEs) to be shared between multiple device IDs. See commit 3c124435e8dd ("iommu/amd: Support multiple PCI DMA aliases in IRQ Remapping") for more information on this. However, the AMD IOMMU driver currently invalidates IRTE cache entries on a per-device basis whenever an IRTE is updated, not for each alias.

This approach leaves stale IRTE cache entries when an IRTE is cached under one DMA alias but later updated and invalidated through a different alias. In such cases, the original device ID is never invalidated, since it is programmed via aliasing.

This incoherency bug has been observed when IRTEs are cached for one Non-Transparent Bridge (NTB) DMA alias, later updated via another.

Fix this by invalidating the interrupt remapping table cache for all DMA aliases when updating an IRTE.

Changes since v3:
- Revert back to using pci_for_each_dma_alias (v2), but look up pdev with search_dev_data instead of pci_get_domain_bus_and_slot (lockdep). This avoids changing any locks
- Rebased on iommu next
- Tested atop
https://lore.kernel.org/linux-iommu/[email protected]/

Link to v2:
https://lore.kernel.org/linux-iommu/[email protected]/

---
 drivers/iommu/amd/iommu.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/iommu/amd/iommu.c +23 -7
index f22a7e9ecfdb..217da630d847 100644
@@ -3186,26 +3186,42 @@ const struct iommu_ops amd_iommu_ops = {
 static struct irq_chip amd_ir_chip;
 static DEFINE_SPINLOCK(iommu_table_lock);
 
+static int iommu_flush_dev_irt(struct pci_dev *unused, u16 devid, void *data)
+{
+	int ret;
+	struct iommu_cmd cmd;
+	struct amd_iommu *iommu = data;
+
+	build_inv_irt(&cmd, devid);
+	ret = __iommu_queue_command_sync(iommu, &cmd, true);
+	return ret;
+}
+
 static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
 {
 	int ret;
 	u64 data;
 	unsigned long flags;
-	struct iommu_cmd cmd, cmd2;
+	struct iommu_cmd cmd;
+	struct pci_dev *pdev = NULL;
+	struct iommu_dev_data *dev_data = search_dev_data(iommu, devid);
 
 	if (iommu->irtcachedis_enabled)
 		return;
 
-	build_inv_irt(&cmd, devid);
+	if (dev_data && dev_data->dev && dev_is_pci(dev_data->dev))
+		pdev = to_pci_dev(dev_data->dev);
 
 	raw_spin_lock_irqsave(&iommu->lock, flags);
 	data = get_cmdsem_val(iommu);
-	build_completion_wait(&cmd2, iommu, data);
+	build_completion_wait(&cmd, iommu, data);
 
-	ret = __iommu_queue_command_sync(iommu, &cmd, true);
-	if (ret)
-		goto out_err;
-	ret = __iommu_queue_command_sync(iommu, &cmd2, false);
+	if (pdev)
+		ret = pci_for_each_dma_alias(pdev, iommu_flush_dev_irt, iommu);
+	else
+		ret = iommu_flush_dev_irt(NULL, devid, iommu);
+
+	ret = __iommu_queue_command_sync(iommu, &cmd, false);
 	if (ret)
 		goto out_err;
 	raw_spin_unlock_irqrestore(&iommu->lock, flags);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v4 1/1
#1 lore raw

Magnus,

Quoted 26 lines · Magnus Kalland

DMA aliasing causes interrupt remapping table entries (IRTEs) to be shared between multiple device IDs. See commit 3c124435e8dd ("iommu/amd: Support multiple PCI DMA aliases in IRQ Remapping") for more information on this. However, the AMD IOMMU driver currently invalidates IRTE cache entries on a per-device basis whenever an IRTE is updated, not for each alias.

This approach leaves stale IRTE cache entries when an IRTE is cached under one DMA alias but later updated and invalidated through a different alias. In such cases, the original device ID is never invalidated, since it is programmed via aliasing.

This incoherency bug has been observed when IRTEs are cached for one Non-Transparent Bridge (NTB) DMA alias, later updated via another.

Fix this by invalidating the interrupt remapping table cache for all DMA aliases when updating an IRTE.

Changes since v3:
- Revert back to using pci_for_each_dma_alias (v2), but look up pdev with search_dev_data instead of pci_get_domain_bus_and_slot (lockdep). This avoids changing any locks
- Rebased on iommu next
- Tested atop
https://lore.kernel.org/linux-iommu/[email protected]/

Quoted 2 lines

Link to v2:
https://lore.kernel.org/linux-iommu/[email protected]/

Next time please move the changelog after "---" so that it gets discarded automatically when applying patches.

Quoted 9 lines

Overall it looks good. One minor comment (see below).

Quoted 45 lines
---
 drivers/iommu/amd/iommu.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/iommu/amd/iommu.c +17 -3
index f22a7e9ecfdb..217da630d847 100644
@@ -3186,26 +3186,42 @@ const struct iommu_ops amd_iommu_ops = {
 static struct irq_chip amd_ir_chip;
 static DEFINE_SPINLOCK(iommu_table_lock);
 
+static int iommu_flush_dev_irt(struct pci_dev *unused, u16 devid, void *data)
+{
+	int ret;
+	struct iommu_cmd cmd;
+	struct amd_iommu *iommu = data;
+
+	build_inv_irt(&cmd, devid);
+	ret = __iommu_queue_command_sync(iommu, &cmd, true);
+	return ret;
+}
+
 static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
 {
 	int ret;
 	u64 data;
 	unsigned long flags;
-	struct iommu_cmd cmd, cmd2;
+	struct iommu_cmd cmd;
+	struct pci_dev *pdev = NULL;
+	struct iommu_dev_data *dev_data = search_dev_data(iommu, devid);
 
 	if (iommu->irtcachedis_enabled)
 		return;
 
-	build_inv_irt(&cmd, devid);
+	if (dev_data && dev_data->dev && dev_is_pci(dev_data->dev))
+		pdev = to_pci_dev(dev_data->dev);
 
 	raw_spin_lock_irqsave(&iommu->lock, flags);
 	data = get_cmdsem_val(iommu);
-	build_completion_wait(&cmd2, iommu, data);
+	build_completion_wait(&cmd, iommu, data);

drivers/iommu/amd/iommu.c const struct iommu_ops amd_iommu_ops = { · line 3223

Quoted 10 lines
drivers/iommu/amd/iommu.c +5 -4
 
-	ret = __iommu_queue_command_sync(iommu, &cmd, true);
-	if (ret)
-		goto out_err;
-	ret = __iommu_queue_command_sync(iommu, &cmd2, false);
+	if (pdev)
+		ret = pci_for_each_dma_alias(pdev, iommu_flush_dev_irt, iommu);
+	else
+		ret = iommu_flush_dev_irt(NULL, devid, iommu);
+

if (ret)

	goto out_err;

-Vasant

maintainer Applied
#2 lore raw
In reply to Magnus Kalland
Quoted 2 lines · Magnus Kalland
 drivers/iommu/amd/iommu.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

Applied, thanks.

maintainer
#4 lore raw
Quoted 10 lines · Magnus Kalland

Hi Joerg,

Quoted 4 lines
Quoted 2 lines

drivers/iommu/amd/iommu.c | 30 +++++++++++++++++++++++-------

1 file changed, 23 insertions(+), 7 deletions(-)

Applied, thanks.

I just sent a v5 addressing the requested changes from v4 from Vasant.

Please apply v5 instead.

Replaced v4 with v5, thanks for the heads-up.