Skip to contentSkip to conversation

iommu/vsi: Fix the runtime PM of the IOMMU

PATCH Applied Arun Iyengar 3 participants 6 messages Nov 13 – Nov 23

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

cover iommu/vsi: Fix the runtime PM of the IOMMU

Patch 1 lets the IOMMU suspend again: a failed pm_runtime_get() in the TLB flush leaked a usage count. Patch 2 stops the interrupt handler from resuming the IOMMU in hard IRQ context.

Tested on RK3588S (Orange Pi 5), v7.3-based kernel that also has hantro-robust v3 [1] and the vsi-iommu suspend patch I will send next:
without patch 1 the AV1 IOMMU stayed active after decoding. With both it is runtime suspended with usage count 0 after AV1 fluster (203/242, unchanged), 20 AV1 decoder unbind/bind cycles and 3 s2idle cycles. No IOMMU fault occurred, so patch 2's handler path did not run.

No conflict with patch 10 of [1], which touches the probe.

Developed with Claude (Anthropic), which wrote the code and the commit messages. I reviewed every change.

[1] https://lore.kernel.org/r/[email protected]

---
Arun Iyengar (2):
iommu/vsi: Don't leak a runtime PM reference in the TLB flush iommu/vsi: Don't resume the IOMMU in its interrupt handler

 drivers/iommu/vsi-iommu.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

---
base-commit: 2cf4be57b301ed2d7af82e70cff5dfd96894d8de
change-id: 20261113-vsi-iommu-pm-ref-22c83a5473be

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>
#1 lore raw

PATCH 1/2 iommu/vsi: Don't leak a runtime PM reference in the TLB flush

pm_runtime_get() takes a usage reference even when it fails, e.g. with -EINPROGRESS while a resume runs, and vsi_iommu_flush_tlb() then skips the put. The IOMMU then never suspends again.

Flush only an active IOMMU and flush the TLB when the IOMMU is enabled, which covers resume.

Fixes 917ace84b770 ("iommu: Add verisilicon IOMMU driver")
---
 drivers/iommu/vsi-iommu.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iommu/vsi-iommu.c +4 -3
index 5dac20b143a5..5d581be95333 100644
@@ -369,7 +369,8 @@ static void vsi_iommu_flush_tlb(struct iommu_domain *domain)
 	struct vsi_iommu *iommu;
 
 	list_for_each_entry(iommu, &vsi_domain->iommus, node) {
-		if (pm_runtime_get(iommu->dev) < 0)
+		/* A suspended IOMMU is flushed when it resumes */
+		if (pm_runtime_get_if_active(iommu->dev) <= 0)
 			continue;
 
 		spin_lock(&iommu->lock);
@@ -545,6 +546,8 @@ static void vsi_iommu_enable(struct vsi_iommu *iommu, struct iommu_domain *domai
 	writel(VSI_MMU_OUT_OF_BOUND, iommu->regs + VSI_MMU_CONFIG1_BASE);
 	writel(VSI_MMU_BIT_ENABLE, iommu->regs + VSI_MMU_AHB_EXCEPTION_BASE);
 	writel(VSI_MMU_BIT_ENABLE, iommu->regs + VSI_MMU_AHB_CONTROL_BASE);
+	writel(VSI_MMU_BIT_FLUSH, iommu->regs + VSI_MMU_FLUSH_BASE);
+	writel(0, iommu->regs + VSI_MMU_FLUSH_BASE);
 	iommu->enable = true;
 }
 
@@ -564,8 +567,6 @@ static int vsi_iommu_attach_device(struct iommu_domain *domain,
 	spin_lock(&iommu->lock);
 
 	vsi_iommu_enable(iommu, domain);
-	writel(VSI_MMU_BIT_FLUSH, iommu->regs + VSI_MMU_FLUSH_BASE);
-	writel(0, iommu->regs + VSI_MMU_FLUSH_BASE);
 
 	list_del_init(&iommu->node);
 	list_add_tail(&iommu->node, &vsi_domain->iommus);
Signature · 1 line
2.55.0
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] iommu/vsi: Don't leak a runtime PM reference in the TLB flush

Le 13/11/2026 à 21:04, Arun Iyengar via B4 Relay a écrit :

Quoted 11 lines

From: Arun Iyengar <[email protected]>

pm_runtime_get() takes a usage reference even when it fails, e.g. with -EINPROGRESS while a resume runs, and vsi_iommu_flush_tlb() then skips the put. The IOMMU then never suspends again.

Flush only an active IOMMU and flush the TLB when the IOMMU is enabled, which covers resume.

Fixes 917ace84b770 ("iommu: Add verisilicon IOMMU driver")
Quoted 37 lines
---
  drivers/iommu/vsi-iommu.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iommu/vsi-iommu.c +4 -3
index 5dac20b143a5..5d581be95333 100644
@@ -369,7 +369,8 @@ static void vsi_iommu_flush_tlb(struct iommu_domain *domain)
  	struct vsi_iommu *iommu;
 
  	list_for_each_entry(iommu, &vsi_domain->iommus, node) {
-		if (pm_runtime_get(iommu->dev) < 0)
+		/* A suspended IOMMU is flushed when it resumes */
+		if (pm_runtime_get_if_active(iommu->dev) <= 0)
  			continue;
 
  		spin_lock(&iommu->lock);
@@ -545,6 +546,8 @@ static void vsi_iommu_enable(struct vsi_iommu *iommu, struct iommu_domain *domai
  	writel(VSI_MMU_OUT_OF_BOUND, iommu->regs + VSI_MMU_CONFIG1_BASE);
  	writel(VSI_MMU_BIT_ENABLE, iommu->regs + VSI_MMU_AHB_EXCEPTION_BASE);
  	writel(VSI_MMU_BIT_ENABLE, iommu->regs + VSI_MMU_AHB_CONTROL_BASE);
+	writel(VSI_MMU_BIT_FLUSH, iommu->regs + VSI_MMU_FLUSH_BASE);
+	writel(0, iommu->regs + VSI_MMU_FLUSH_BASE);
  	iommu->enable = true;
  }
 
@@ -564,8 +567,6 @@ static int vsi_iommu_attach_device(struct iommu_domain *domain,
  	spin_lock(&iommu->lock);
 
  	vsi_iommu_enable(iommu, domain);
-	writel(VSI_MMU_BIT_FLUSH, iommu->regs + VSI_MMU_FLUSH_BASE);
-	writel(0, iommu->regs + VSI_MMU_FLUSH_BASE);
 
  	list_del_init(&iommu->node);
  	list_add_tail(&iommu->node, &vsi_domain->iommus);
#3 lore raw

PATCH 2/2 iommu/vsi: Don't resume the IOMMU in its interrupt handler

In reply to Arun Iyengar

The interrupt handler calls pm_runtime_resume_and_get(), which can sleep, in hard IRQ context. Handle the interrupt only if the IOMMU is active.

Fixes 917ace84b770 ("iommu: Add verisilicon IOMMU driver")
---
 drivers/iommu/vsi-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/vsi-iommu.c +1 -1
index 5d581be95333..3dec58224953 100644
@@ -196,7 +196,7 @@ static irqreturn_t vsi_iommu_irq(int irq, void *dev_id)
 	dma_addr_t iova;
 	u32 status;
 
-	if (pm_runtime_resume_and_get(iommu->dev) < 0)
+	if (pm_runtime_get_if_active(iommu->dev) <= 0)
 		return IRQ_NONE;
 
 	spin_lock_irqsave(&iommu->lock, flags);
Signature · 1 line
2.55.0
maintainer Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] iommu/vsi: Don't resume the IOMMU in its interrupt handler

Le 13/11/2026 à 21:04, Arun Iyengar via B4 Relay a écrit :

Quoted 8 lines

From: Arun Iyengar <[email protected]>

The interrupt handler calls pm_runtime_resume_and_get(), which can sleep, in hard IRQ context. Handle the interrupt only if the IOMMU is active.

Fixes 917ace84b770 ("iommu: Add verisilicon IOMMU driver")
Quoted 18 lines
---
  drivers/iommu/vsi-iommu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/vsi-iommu.c +1 -1
index 5d581be95333..3dec58224953 100644
@@ -196,7 +196,7 @@ static irqreturn_t vsi_iommu_irq(int irq, void *dev_id)
  	dma_addr_t iova;
  	u32 status;
 
-	if (pm_runtime_resume_and_get(iommu->dev) < 0)
+	if (pm_runtime_get_if_active(iommu->dev) <= 0)
  		return IRQ_NONE;
 
  	spin_lock_irqsave(&iommu->lock, flags);
maintainer Applied
#5 lore raw
In reply to Arun Iyengar
Quoted 3 lines · On Fri, Nov 13, 2026 at 08:04:35PM +0000, Arun Iyengar via B4 Relay wrote:

Arun Iyengar (2):
iommu/vsi: Don't leak a runtime PM reference in the TLB flush iommu/vsi: Don't resume the IOMMU in its interrupt handler

Applied, thanks.