Skip to contentSkip to conversation

iommu/arm-smmu: adreno-smmu page fault handling

PATCH Superseded Jordan Crouse 4 participants 10 messages Nov 24, 2020 – Jan 26, 2021

Subsystems (7)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover iommu/arm-smmu: adreno-smmu page fault handling

This is a stack to add an Adreno GPU specific handler for pagefaults. The first patch starts by wiring up report_iommu_fault for arm-smmu. The next patch adds a adreno-smmu-priv function hook to capture a handful of important debugging registers such as TTBR0, CONTEXTIDR, FSYNR0 and others. This is used by the third patch to print more detailed information on page fault such as the TTBR0 for the pagetable that caused the fault and the source of the fault as determined by a combination of the FSYNR1 register and an internal GPU register.

This code provides a solid base that we can expand on later for even more extensive GPU side page fault debugging capabilities.

v2: Fix comment wording and function pointer check per Rob Clark

Jordan Crouse (3):
iommu/arm-smmu: Add support for driver IOMMU fault handlers drm/msm: Add an adreno-smmu-priv callback to get pagefault info drm/msm: Improve the a6xx page fault handler

 drivers/gpu/drm/msm/adreno/a5xx_gpu.c      |  4 +-
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c      | 76 +++++++++++++++++++++-
 drivers/gpu/drm/msm/msm_iommu.c            | 11 +++-
 drivers/gpu/drm/msm/msm_mmu.h              |  4 +-
 drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c | 19 ++++++
 drivers/iommu/arm/arm-smmu/arm-smmu.c      | 16 ++++-
 drivers/iommu/arm/arm-smmu/arm-smmu.h      |  2 +
 include/linux/adreno-smmu-priv.h           | 31 ++++++++-
 8 files changed, 151 insertions(+), 12 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/3 iommu/arm-smmu: Add support for driver IOMMU fault handlers

Call report_iommu_fault() to allow upper-level drivers to register their own fault handlers.

---

 drivers/iommu/arm/arm-smmu/arm-smmu.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu/arm-smmu.c +13 -3
index 0f28a8614da3..7fd18bbda8f5 100644
@@ -427,6 +427,7 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
 	struct arm_smmu_device *smmu = smmu_domain->smmu;
 	int idx = smmu_domain->cfg.cbndx;
+	int ret;
 
 	fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
 	if (!(fsr & ARM_SMMU_FSR_FAULT))
@@ -436,11 +437,20 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
 	iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
 	cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
 
-	dev_err_ratelimited(smmu->dev,
-	"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
+	ret = report_iommu_fault(domain, dev, iova,
+		fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ);
+
+	if (ret == -ENOSYS)
+		dev_err_ratelimited(smmu->dev,
+		"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
 			    fsr, iova, fsynr, cbfrsynra, idx);
 
-	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
+	/*
+	 * If the iommu fault returns an error (except -ENOSYS) then assume that
+	 * they will handle resuming on their own
+	 */
+	if (!ret || ret == -ENOSYS)
+		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
 	return IRQ_HANDLED;
 }
 
Signature · 6 lines
2.25.1

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

Re: [PATCH v2 1/3] iommu/arm-smmu: Add support for driver IOMMU fault handlers

Quoted 32 lines · Jordan Crouse

Call report_iommu_fault() to allow upper-level drivers to register their own fault handlers.

---

 drivers/iommu/arm/arm-smmu/arm-smmu.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu/arm-smmu.c +6 -2
index 0f28a8614da3..7fd18bbda8f5 100644
@@ -427,6 +427,7 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
 	struct arm_smmu_device *smmu = smmu_domain->smmu;
 	int idx = smmu_domain->cfg.cbndx;
+	int ret;
 
 	fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
 	if (!(fsr & ARM_SMMU_FSR_FAULT))
@@ -436,11 +437,20 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
 	iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
 	cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
 
-	dev_err_ratelimited(smmu->dev,
-	"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
+	ret = report_iommu_fault(domain, dev, iova,
+		fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ);
+
+	if (ret == -ENOSYS)
+		dev_err_ratelimited(smmu->dev,

drivers/iommu/arm/arm-smmu/arm-smmu.c static irqreturn_t arm_smmu_context_fault(int irq, void *dev) · line 453

Quoted 10 lines
drivers/iommu/arm/arm-smmu/arm-smmu.c +7 -1
+		"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
 			    fsr, iova, fsynr, cbfrsynra, idx);
 
-	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
+	/*
+	 * If the iommu fault returns an error (except -ENOSYS) then assume that
+	 * they will handle resuming on their own
+	 */
+	if (!ret || ret == -ENOSYS)
+		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);

Hmm, I don't grok this part. If the fault handler returned an error and we don't clear the FSR, won't we just re-take the irq immediately? I think it would be better to do this unconditionally, and print the "Unhandled context fault" message for any non-zero value of ret.

Will
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

reviewer
#3 lore raw

Re: [PATCH v2 1/3] iommu/arm-smmu: Add support for driver IOMMU fault handlers

Quoted 36 lines · Will Deacon
Quoted 35 lines · Jordan Crouse

Call report_iommu_fault() to allow upper-level drivers to register their own fault handlers.

---

  drivers/iommu/arm/arm-smmu/arm-smmu.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu/arm-smmu.c +7 -2
index 0f28a8614da3..7fd18bbda8f5 100644
@@ -427,6 +427,7 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  	struct arm_smmu_device *smmu = smmu_domain->smmu;
  	int idx = smmu_domain->cfg.cbndx;
+	int ret;
 
  	fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
  	if (!(fsr & ARM_SMMU_FSR_FAULT))
@@ -436,11 +437,20 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  	iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
  	cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
 
-	dev_err_ratelimited(smmu->dev,
-	"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
+	ret = report_iommu_fault(domain, dev, iova,
+		fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ);
+
+	if (ret == -ENOSYS)
+		dev_err_ratelimited(smmu->dev,
+		"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
  			    fsr, iova, fsynr, cbfrsynra, idx);
 
Quoted 10 lines

drivers/iommu/arm/arm-smmu/arm-smmu.c static irqreturn_t arm_smmu_context_fault(int irq, void *dev) · line 453

Quoted 7 lines
drivers/iommu/arm/arm-smmu/arm-smmu.c +6 -1
-	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
+	/*
+	 * If the iommu fault returns an error (except -ENOSYS) then assume that
+	 * they will handle resuming on their own
+	 */
+	if (!ret || ret == -ENOSYS)
+		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);

Hmm, I don't grok this part. If the fault handler returned an error and we don't clear the FSR, won't we just re-take the irq immediately?

If we don't touch the FSR at all, yes. Even if we clear the fault indicator bits, the interrupt *might* remain asserted until a stalled transaction is actually resolved - that's that lovely IMP-DEF corner.

Robin.

Quoted 6 lines

I think
it would be better to do this unconditionally, and print the "Unhandled context fault" message for any non-zero value of ret.

Will

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

#4 lore raw

Re: [PATCH v2 1/3] iommu/arm-smmu: Add support for driver IOMMU fault handlers

Quoted 45 lines · Robin Murphy
Quoted 35 lines · Will Deacon
Quoted 34 lines · Jordan Crouse

Call report_iommu_fault() to allow upper-level drivers to register their own fault handlers.

---

 drivers/iommu/arm/arm-smmu/arm-smmu.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu/arm-smmu.c +8 -3
index 0f28a8614da3..7fd18bbda8f5 100644
@@ -427,6 +427,7 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
 	struct arm_smmu_device *smmu = smmu_domain->smmu;
 	int idx = smmu_domain->cfg.cbndx;
+	int ret;
 	fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
 	if (!(fsr & ARM_SMMU_FSR_FAULT))
@@ -436,11 +437,20 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
 	iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
 	cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
-	dev_err_ratelimited(smmu->dev,
-	"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
+	ret = report_iommu_fault(domain, dev, iova,
+		fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ);
+
+	if (ret == -ENOSYS)
+		dev_err_ratelimited(smmu->dev,
+		"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
 			    fsr, iova, fsynr, cbfrsynra, idx);
-	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
+	/*
Quoted 8 lines

drivers/iommu/arm/arm-smmu/arm-smmu.c static irqreturn_t arm_smmu_context_fault(int irq, void *dev) · line 453

Quoted 5 lines
drivers/iommu/arm/arm-smmu/arm-smmu.c +5 -0
+	 * If the iommu fault returns an error (except -ENOSYS) then assume that
+	 * they will handle resuming on their own
+	 */
+	if (!ret || ret == -ENOSYS)
+		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);

Hmm, I don't grok this part. If the fault handler returned an error and we don't clear the FSR, won't we just re-take the irq immediately?

Quoted 6 lines

If we don't touch the FSR at all, yes. Even if we clear the fault indicator bits, the interrupt *might* remain asserted until a stalled transaction is actually resolved - that's that lovely IMP-DEF corner.

Robin.

This is for stall-on-fault. The idea is that if the developer chooses to do so we would stall the GPU after a fault long enough to take a picture of it with devcoredump and then release the FSR. Since we can't take the devcoredump from the interrupt handler we schedule it in a worker and then return an error to let the main handler know that we'll come back around clear the FSR later when we are done.

It is assumed that we'll have to turn off interrupts in our handler to allow this to work. Its all very implementation specific, but then again we're assuming that if you want to do this then you know what you are doing.

In that spirit the error that skips the FSR should probably be something specific instead of "all errors" - that way a well meaning handler that returns a -EINVAL doesn't accidentally break itself.

Jordan

Quoted 7 lines
Quoted 7 lines

I think
it would be better to do this unconditionally, and print the "Unhandled context fault" message for any non-zero value of ret.

Will

Signature · 6 lines
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
reviewer
#5 lore raw

Re: [PATCH v2 1/3] iommu/arm-smmu: Add support for driver IOMMU fault handlers

Quoted 53 lines · Jordan Crouse
Quoted 45 lines · Robin Murphy
Quoted 35 lines · Will Deacon
Quoted 34 lines · Jordan Crouse

Call report_iommu_fault() to allow upper-level drivers to register their own fault handlers.

---

  drivers/iommu/arm/arm-smmu/arm-smmu.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu/arm-smmu.c +8 -3
index 0f28a8614da3..7fd18bbda8f5 100644
@@ -427,6 +427,7 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  	struct arm_smmu_device *smmu = smmu_domain->smmu;
  	int idx = smmu_domain->cfg.cbndx;
+	int ret;
  	fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
  	if (!(fsr & ARM_SMMU_FSR_FAULT))
@@ -436,11 +437,20 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  	iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
  	cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
-	dev_err_ratelimited(smmu->dev,
-	"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
+	ret = report_iommu_fault(domain, dev, iova,
+		fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ);
+
+	if (ret == -ENOSYS)
+		dev_err_ratelimited(smmu->dev,
+		"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
  			    fsr, iova, fsynr, cbfrsynra, idx);
-	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
+	/*
Quoted 8 lines

drivers/iommu/arm/arm-smmu/arm-smmu.c static irqreturn_t arm_smmu_context_fault(int irq, void *dev) · line 453

Quoted 5 lines
drivers/iommu/arm/arm-smmu/arm-smmu.c +5 -0
+	 * If the iommu fault returns an error (except -ENOSYS) then assume that
+	 * they will handle resuming on their own
+	 */
+	if (!ret || ret == -ENOSYS)
+		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);

Hmm, I don't grok this part. If the fault handler returned an error and we don't clear the FSR, won't we just re-take the irq immediately?

Quoted 6 lines

If we don't touch the FSR at all, yes. Even if we clear the fault indicator bits, the interrupt *might* remain asserted until a stalled transaction is actually resolved - that's that lovely IMP-DEF corner.

Robin.

Quoted 6 lines

This is for stall-on-fault. The idea is that if the developer chooses to do so we would stall the GPU after a fault long enough to take a picture of it with devcoredump and then release the FSR. Since we can't take the devcoredump from the interrupt handler we schedule it in a worker and then return an error to let the main handler know that we'll come back around clear the FSR later when we are done.

Sure, but clearing FSR is not writing to RESUME to resolve the stalled transaction(s). You can already snarf the FSR contents from your report_iommu_fault() handler if you want to, so either way I don't see what's gained by not clearing it as expected at the point where we've handled the *interrupt*, even if it will take longer to decide what to do with the underlying *fault* that it signalled. I'm particularly not keen on having unusual behaviour in the core interrupt handling which callers may unwittingly trigger, for the sake of one very-very-driver-specific flow having a slightly richer debugging experience.

For actually *handling* faults, I thought we were going to need to hook up the new IOPF fault queue stuff anyway?

Robin.

Quoted 4 lines

It is assumed that we'll have to turn off interrupts in our handler to allow this to work. Its all very implementation specific, but then again we're assuming that if you want to do this then you know what you are doing.

Quoted 14 lines

In that spirit the error that skips the FSR should probably be something specific instead of "all errors" - that way a well meaning handler that returns a -EINVAL doesn't accidentally break itself.

Jordan

Quoted 7 lines
Quoted 7 lines

I think
it would be better to do this unconditionally, and print the "Unhandled context fault" message for any non-zero value of ret.

Will

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

#6 lore raw

Re: [PATCH v2 1/3] iommu/arm-smmu: Add support for driver IOMMU fault handlers

Quoted 62 lines · Robin Murphy
Quoted 53 lines · Jordan Crouse
Quoted 45 lines · Robin Murphy
Quoted 35 lines · Will Deacon
Quoted 34 lines · Jordan Crouse

Call report_iommu_fault() to allow upper-level drivers to register their own fault handlers.

---

  drivers/iommu/arm/arm-smmu/arm-smmu.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu/arm-smmu.c +8 -3
index 0f28a8614da3..7fd18bbda8f5 100644
@@ -427,6 +427,7 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
   struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
   struct arm_smmu_device *smmu = smmu_domain->smmu;
   int idx = smmu_domain->cfg.cbndx;
+  int ret;
   fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
   if (!(fsr & ARM_SMMU_FSR_FAULT))
@@ -436,11 +437,20 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
   iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
   cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
-  dev_err_ratelimited(smmu->dev,
-  "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
+  ret = report_iommu_fault(domain, dev, iova,
+          fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ);
+
+  if (ret == -ENOSYS)
+          dev_err_ratelimited(smmu->dev,
+          "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
                       fsr, iova, fsynr, cbfrsynra, idx);
-  arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
+  /*
Quoted 8 lines

drivers/iommu/arm/arm-smmu/arm-smmu.c static irqreturn_t arm_smmu_context_fault(int irq, void *dev) · line 453

Quoted 5 lines
drivers/iommu/arm/arm-smmu/arm-smmu.c +5 -0
+   * If the iommu fault returns an error (except -ENOSYS) then assume that
+   * they will handle resuming on their own
+   */
+  if (!ret || ret == -ENOSYS)
+          arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);

Hmm, I don't grok this part. If the fault handler returned an error and we don't clear the FSR, won't we just re-take the irq immediately?

Quoted 6 lines

If we don't touch the FSR at all, yes. Even if we clear the fault indicator bits, the interrupt *might* remain asserted until a stalled transaction is actually resolved - that's that lovely IMP-DEF corner.

Robin.

Quoted 6 lines

This is for stall-on-fault. The idea is that if the developer chooses to do so we would stall the GPU after a fault long enough to take a picture of it with devcoredump and then release the FSR. Since we can't take the devcoredump from the interrupt handler we schedule it in a worker and then return an error to let the main handler know that we'll come back around clear the FSR later when we are done.

Quoted 10 lines

Sure, but clearing FSR is not writing to RESUME to resolve the stalled transaction(s). You can already snarf the FSR contents from your report_iommu_fault() handler if you want to, so either way I don't see what's gained by not clearing it as expected at the point where we've handled the *interrupt*, even if it will take longer to decide what to do with the underlying *fault* that it signalled. I'm particularly not keen on having unusual behaviour in the core interrupt handling which callers may unwittingly trigger, for the sake of one very-very-driver-specific flow having a slightly richer debugging experience.

Tbf, "slightly" is an understatement.. it is a big enough improvement that I've hacked up deferred resume several times to debug various issues. ;-)

(Which is always a bit of a PITA because of things moving around in arm-smmu as well as the drm side of things.)

But from my recollection, we can clear FSR immediately, all we need to do is defer writing ARM_SMMU_CB_RESUME

BR,
-R

Quoted 21 lines

For actually *handling* faults, I thought we were going to need to hook up the new IOPF fault queue stuff anyway?

Robin.

Quoted 15 lines

It is assumed that we'll have to turn off interrupts in our handler to allow this to work. Its all very implementation specific, but then again we're assuming that if you want to do this then you know what you are doing.

In that spirit the error that skips the FSR should probably be something specific instead of "all errors" - that way a well meaning handler that returns a -EINVAL doesn't accidentally break itself.

Jordan

Quoted 5 lines
Quoted 5 lines

I think
it would be better to do this unconditionally, and print the "Unhandled context fault" message for any non-zero value of ret.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines

Will

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

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

reviewer
#7 lore raw

Re: [PATCH v2 1/3] iommu/arm-smmu: Add support for driver IOMMU fault handlers

Quoted 74 lines · Rob Clark
Quoted 62 lines · Robin Murphy
Quoted 53 lines · Jordan Crouse
Quoted 45 lines · Robin Murphy
Quoted 35 lines · Will Deacon
Quoted 34 lines · Jordan Crouse

Call report_iommu_fault() to allow upper-level drivers to register their own fault handlers.

---

   drivers/iommu/arm/arm-smmu/arm-smmu.c | 16 +++++++++++++---
   1 file changed, 13 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu/arm-smmu.c +8 -3
index 0f28a8614da3..7fd18bbda8f5 100644
@@ -427,6 +427,7 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
    struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
    struct arm_smmu_device *smmu = smmu_domain->smmu;
    int idx = smmu_domain->cfg.cbndx;
+  int ret;
    fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
    if (!(fsr & ARM_SMMU_FSR_FAULT))
@@ -436,11 +437,20 @@ static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
    iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
    cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
-  dev_err_ratelimited(smmu->dev,
-  "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
+  ret = report_iommu_fault(domain, dev, iova,
+          fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ);
+
+  if (ret == -ENOSYS)
+          dev_err_ratelimited(smmu->dev,
+          "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
                        fsr, iova, fsynr, cbfrsynra, idx);
-  arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
+  /*
Quoted 8 lines

drivers/iommu/arm/arm-smmu/arm-smmu.c static irqreturn_t arm_smmu_context_fault(int irq, void *dev) · line 453

Quoted 5 lines
drivers/iommu/arm/arm-smmu/arm-smmu.c +5 -0
+   * If the iommu fault returns an error (except -ENOSYS) then assume that
+   * they will handle resuming on their own
+   */
+  if (!ret || ret == -ENOSYS)
+          arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);

Hmm, I don't grok this part. If the fault handler returned an error and we don't clear the FSR, won't we just re-take the irq immediately?

Quoted 6 lines

If we don't touch the FSR at all, yes. Even if we clear the fault indicator bits, the interrupt *might* remain asserted until a stalled transaction is actually resolved - that's that lovely IMP-DEF corner.

Robin.

Quoted 6 lines

This is for stall-on-fault. The idea is that if the developer chooses to do so we would stall the GPU after a fault long enough to take a picture of it with devcoredump and then release the FSR. Since we can't take the devcoredump from the interrupt handler we schedule it in a worker and then return an error to let the main handler know that we'll come back around clear the FSR later when we are done.

Quoted 10 lines

Sure, but clearing FSR is not writing to RESUME to resolve the stalled transaction(s). You can already snarf the FSR contents from your report_iommu_fault() handler if you want to, so either way I don't see what's gained by not clearing it as expected at the point where we've handled the *interrupt*, even if it will take longer to decide what to do with the underlying *fault* that it signalled. I'm particularly not keen on having unusual behaviour in the core interrupt handling which callers may unwittingly trigger, for the sake of one very-very-driver-specific flow having a slightly richer debugging experience.

Quoted 3 lines

Tbf, "slightly" is an understatement.. it is a big enough improvement that I've hacked up deferred resume several times to debug various issues. ;-)

Oh, fear not, I fully appreciate that keeping the GPU stalled on a faulting transaction is a game-changer in itself ("almost like a real MMU!"). That comment was only aimed at whatever the perceived benefits are of deliberately not trying to clear the SMMU interrupt (even if it *would* stay clear). I have no issue with calling report_iommu_fault(), I'm just wary of doing anything weird with the result.

Quoted 5 lines

(Which is always a bit of a PITA because of things moving around in arm-smmu as well as the drm side of things.)

But from my recollection, we can clear FSR immediately, all we need to do is defer writing ARM_SMMU_CB_RESUME

Phew! Thanks for the reassurance :)

Robin.

Quoted 32 lines

BR,
-R

Quoted 28 lines

For actually *handling* faults, I thought we were going to need to hook up the new IOPF fault queue stuff anyway?

Robin.

Quoted 4 lines

It is assumed that we'll have to turn off interrupts in our handler to allow this to work. Its all very implementation specific, but then again we're assuming that if you want to do this then you know what you are doing.

Quoted 14 lines

In that spirit the error that skips the FSR should probably be something specific instead of "all errors" - that way a well meaning handler that returns a -EINVAL doesn't accidentally break itself.

Jordan

Quoted 7 lines
Quoted 7 lines

I think
it would be better to do this unconditionally, and print the "Unhandled context fault" message for any non-zero value of ret.

Will

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

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

#8 lore raw

PATCH v2 2/3 drm/msm: Add an adreno-smmu-priv callback to get pagefault info

In reply to Jordan Crouse

Add a callback in adreno-smmu-priv to read interesting SMMU registers to provide an opportunity for a richer debug experience in the GPU driver.

---

 drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c | 19 +++++++++++++
 drivers/iommu/arm/arm-smmu/arm-smmu.h      |  2 ++
 include/linux/adreno-smmu-priv.h           | 31 +++++++++++++++++++++-
 3 files changed, 51 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c +19 -0
index d0636c803a36..367a267324a2 100644
@@ -32,6 +32,24 @@ static void qcom_adreno_smmu_write_sctlr(struct arm_smmu_device *smmu, int idx,
 	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
 }
 
+static void qcom_adreno_smmu_get_fault_info(const void *cookie,
+		struct adreno_smmu_fault_info *info)
+{
+	struct arm_smmu_domain *smmu_domain = (void *)cookie;
+	struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
+	struct arm_smmu_device *smmu = smmu_domain->smmu;
+
+	info->fsr = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSR);
+	/* FIXME: return error here if we aren't really in a fault? */
+
+	info->fsynr0 = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSYNR0);
+	info->fsynr1 = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSYNR1);
+	info->far = arm_smmu_cb_readq(smmu, cfg->cbndx, ARM_SMMU_CB_FAR);
+	info->cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(cfg->cbndx));
+	info->ttbr0 = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_TTBR0);
+	info->contextidr = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_CONTEXTIDR);
+}
+
 #define QCOM_ADRENO_SMMU_GPU_SID 0
 
 static bool qcom_adreno_smmu_is_gpu_device(struct device *dev)
@@ -156,6 +174,7 @@ static int qcom_adreno_smmu_init_context(struct arm_smmu_domain *smmu_domain,
 	priv->cookie = smmu_domain;
 	priv->get_ttbr1_cfg = qcom_adreno_smmu_get_ttbr1_cfg;
 	priv->set_ttbr0_cfg = qcom_adreno_smmu_set_ttbr0_cfg;
+	priv->get_fault_info = qcom_adreno_smmu_get_fault_info;
 
 	return 0;
 }
drivers/iommu/arm/arm-smmu/arm-smmu.h +2 -0
index 04288b6fc619..fe511540a6bf 100644
@@ -224,6 +224,8 @@ enum arm_smmu_cbar_type {
 #define ARM_SMMU_CB_FSYNR0		0x68
 #define ARM_SMMU_FSYNR0_WNR		BIT(4)
 
+#define ARM_SMMU_CB_FSYNR1		0x6c
+
 #define ARM_SMMU_CB_S1_TLBIVA		0x600
 #define ARM_SMMU_CB_S1_TLBIASID		0x610
 #define ARM_SMMU_CB_S1_TLBIVAL		0x620
include/linux/adreno-smmu-priv.h +30 -1
index a889f28afb42..53fe32fb9214 100644
@@ -8,6 +8,32 @@
 
 #include <linux/io-pgtable.h>
 
+/**
+ * struct adreno_smmu_fault_info - container for key fault information
+ *
+ * @far: The faulting IOVA from ARM_SMMU_CB_FAR
+ * @ttbr0: The current TTBR0 pagetable from ARM_SMMU_CB_TTBR0
+ * @contextidr: The value of ARM_SMMU_CB_CONTEXTIDR
+ * @fsr: The fault status from ARM_SMMU_CB_FSR
+ * @fsynr0: The value of FSYNR0 from ARM_SMMU_CB_FSYNR0
+ * @fsynr1: The value of FSYNR1 from ARM_SMMU_CB_FSYNR0
+ * @cbfrsynra: The value of CBFRSYNRA from ARM_SMMU_GR1_CBFRSYNRA(idx)
+ *
+ * This struct passes back key page fault information to the GPU driver
+ * through the get_fault_info function pointer.
+ * The GPU driver can use this information to print informative
+ * log messages and provide deeper GPU specific insight into the fault.
+ */
+struct adreno_smmu_fault_info {
+	u64 far;
+	u64 ttbr0;
+	u32 contextidr;
+	u32 fsr;
+	u32 fsynr0;
+	u32 fsynr1;
+	u32 cbfrsynra;
+};
+
 /**
  * struct adreno_smmu_priv - private interface between adreno-smmu and GPU
  *
@@ -17,6 +43,8 @@
  * @set_ttbr0_cfg: Set the TTBR0 config for the GPUs context bank.  A
  *                 NULL config disables TTBR0 translation, otherwise
  *                 TTBR0 translation is enabled with the specified cfg
+ * @get_fault_info: Called by the GPU fault handler to get information about
+ *                  the fault
  *
  * The GPU driver (drm/msm) and adreno-smmu work together for controlling
  * the GPU's SMMU instance.  This is by necessity, as the GPU is directly
@@ -31,6 +59,7 @@ struct adreno_smmu_priv {
     const void *cookie;
     const struct io_pgtable_cfg *(*get_ttbr1_cfg)(const void *cookie);
     int (*set_ttbr0_cfg)(const void *cookie, const struct io_pgtable_cfg *cfg);
+    void (*get_fault_info)(const void *cookie, struct adreno_smmu_fault_info *info);
 };
 
-#endif /* __ADRENO_SMMU_PRIV_H */
\ No newline at end of file
+#endif /* __ADRENO_SMMU_PRIV_H */
Signature · 6 lines
2.25.1

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

PATCH v2 3/3 drm/msm: Improve the a6xx page fault handler

In reply to Jordan Crouse

Use the new adreno-smmu-priv fault info function to get more SMMU debug registers and print the current TTBR0 to debug per-instance pagetables and figure out which GPU block generated the request.

---

 drivers/gpu/drm/msm/adreno/a5xx_gpu.c |  4 +-
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 76 +++++++++++++++++++++++++--
 drivers/gpu/drm/msm/msm_iommu.c       | 11 +++-
 drivers/gpu/drm/msm/msm_mmu.h         |  4 +-
 4 files changed, 87 insertions(+), 8 deletions(-)
drivers/gpu/drm/msm/adreno/a5xx_gpu.c +2 -2
index d6804a802355..ed4cb81af874 100644
@@ -933,7 +933,7 @@ bool a5xx_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
 	return true;
 }
 
-static int a5xx_fault_handler(void *arg, unsigned long iova, int flags)
+static int a5xx_fault_handler(void *arg, unsigned long iova, int flags, void *data)
 {
 	struct msm_gpu *gpu = arg;
 	pr_warn_ratelimited("*** gpu fault: iova=%08lx, flags=%d (%u,%u,%u,%u)\n",
@@ -943,7 +943,7 @@ static int a5xx_fault_handler(void *arg, unsigned long iova, int flags)
 			gpu_read(gpu, REG_A5XX_CP_SCRATCH_REG(6)),
 			gpu_read(gpu, REG_A5XX_CP_SCRATCH_REG(7)));
 
-	return -EFAULT;
+	return 0;
 }
 
 static void a5xx_cp_err_irq(struct msm_gpu *gpu)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +73 -3

91 lines not on this page · raw message

drivers/gpu/drm/msm/msm_iommu.c +10 -1
index 22ac7c692a81..73db30dfda0a 100644
@@ -212,8 +212,17 @@ static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
 		unsigned long iova, int flags, void *arg)
 {
 	struct msm_iommu *iommu = arg;
+	struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(iommu->base.dev);
+	struct adreno_smmu_fault_info info, *ptr = NULL;
+
+	if (adreno_smmu->get_fault_info) {
+		adreno_smmu->get_fault_info(adreno_smmu->cookie, &info);
+		ptr = &info;
+	}
+
 	if (iommu->base.handler)
-		return iommu->base.handler(iommu->base.arg, iova, flags);
+		return iommu->base.handler(iommu->base.arg, iova, flags, ptr);
+
 	pr_warn_ratelimited("*** fault: iova=%16lx, flags=%d\n", iova, flags);
 	return 0;
 }
drivers/gpu/drm/msm/msm_mmu.h +2 -2
index 61ade89d9e48..a88f44c3268d 100644
@@ -26,7 +26,7 @@ enum msm_mmu_type {
 struct msm_mmu {
 	const struct msm_mmu_funcs *funcs;
 	struct device *dev;
-	int (*handler)(void *arg, unsigned long iova, int flags);
+	int (*handler)(void *arg, unsigned long iova, int flags, void *data);
 	void *arg;
 	enum msm_mmu_type type;
 };
@@ -43,7 +43,7 @@ struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain);
 struct msm_mmu *msm_gpummu_new(struct device *dev, struct msm_gpu *gpu);
 
 static inline void msm_mmu_set_fault_handler(struct msm_mmu *mmu, void *arg,
-		int (*handler)(void *arg, unsigned long iova, int flags))
+		int (*handler)(void *arg, unsigned long iova, int flags, void *data))
 {
 	mmu->arg = arg;
 	mmu->handler = handler;
Signature · 6 lines
2.25.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel