Skip to contentSkip to conversation

coresight: tmc-etr: Don't enable ETR when it's not ready

PATCH Superseded Yabin Cui 4 participants 18 messages Jan 9, 2023 – Feb 22, 2023

Subsystems (2)
lore
Share & tools
lore

Yabin Cui

author
#0 lore raw

PATCH 1/1 coresight: tmc-etr: Don't enable ETR when it's not ready

Otherwise, it may cause error in AXI bus and result in a kernel panic.

---
 .../hwtracing/coresight/coresight-tmc-core.c   |  4 +++-
 .../hwtracing/coresight/coresight-tmc-etr.c    | 18 +++++++++++++++---
 drivers/hwtracing/coresight/coresight-tmc.h    |  2 +-
 3 files changed, 19 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 {
 	struct coresight_device *csdev = drvdata->csdev;
 	struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 	if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
 		dev_err(&csdev->dev,
 			"timeout while waiting for TMC to be Ready\n");
+		return -EBUSY;
 	}
+	return 0;
 }
 
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..2da99dd41ed6 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
 	etr_buf->ops->sync(etr_buf, rrp, rwp);
 }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 {
 	u32 axictl, sts;
 	struct etr_buf *etr_buf = drvdata->etr_buf;
+	int rc = 0;
 
 	CS_UNLOCK(drvdata->base);
 
 	/* Wait for TMCSReady bit to be set */
-	tmc_wait_for_tmcready(drvdata);
+	rc = tmc_wait_for_tmcready(drvdata);
+	if (rc) {
+		dev_err(&drvdata->csdev->dev, "not ready ETR isn't enabled\n");
+		CS_LOCK(drvdata->base);
+		return rc;
+	}
 
 	writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
 	writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 	tmc_enable_hw(drvdata);
 
 	CS_LOCK(drvdata->base);
+	return rc;
 }
 
 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
 	rc = coresight_claim_device(drvdata->csdev);
 	if (!rc) {
 		drvdata->etr_buf = etr_buf;
-		__tmc_etr_enable_hw(drvdata);
+		rc = __tmc_etr_enable_hw(drvdata);
+		if (rc) {
+			drvdata->etr_buf = NULL;
+			coresight_disclaim_device(drvdata->csdev);
+			tmc_etr_disable_catu(drvdata);
+		}
 	}
 
 	return rc;
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
 };
 
 /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
 void tmc_enable_hw(struct tmc_drvdata *drvdata);
 void tmc_disable_hw(struct tmc_drvdata *drvdata);
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
#1 lore raw
Quoted 1 line · Yabin Cui

Otherwise, it may cause error in AXI bus and result in a kernel panic.

Hi Yabin,

Thanks for the fix. Do you have a reproducer for this or some more info? For example is it a regression or has it always been there? And on which platform.

Thanks
James

Quoted 94 lines
---
 .../hwtracing/coresight/coresight-tmc-core.c   |  4 +++-
 .../hwtracing/coresight/coresight-tmc-etr.c    | 18 +++++++++++++++---
 drivers/hwtracing/coresight/coresight-tmc.h    |  2 +-
 3 files changed, 19 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 {
 	struct coresight_device *csdev = drvdata->csdev;
 	struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 	if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
 		dev_err(&csdev->dev,
 			"timeout while waiting for TMC to be Ready\n");
+		return -EBUSY;
 	}
+	return 0;
 }
 
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..2da99dd41ed6 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
 	etr_buf->ops->sync(etr_buf, rrp, rwp);
 }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 {
 	u32 axictl, sts;
 	struct etr_buf *etr_buf = drvdata->etr_buf;
+	int rc = 0;
 
 	CS_UNLOCK(drvdata->base);
 
 	/* Wait for TMCSReady bit to be set */
-	tmc_wait_for_tmcready(drvdata);
+	rc = tmc_wait_for_tmcready(drvdata);
+	if (rc) {
+		dev_err(&drvdata->csdev->dev, "not ready ETR isn't enabled\n");
+		CS_LOCK(drvdata->base);
+		return rc;
+	}
 
 	writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
 	writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 	tmc_enable_hw(drvdata);
 
 	CS_LOCK(drvdata->base);
+	return rc;
 }
 
 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
 	rc = coresight_claim_device(drvdata->csdev);
 	if (!rc) {
 		drvdata->etr_buf = etr_buf;
-		__tmc_etr_enable_hw(drvdata);
+		rc = __tmc_etr_enable_hw(drvdata);
+		if (rc) {
+			drvdata->etr_buf = NULL;
+			coresight_disclaim_device(drvdata->csdev);
+			tmc_etr_disable_catu(drvdata);
+		}
 	}
 
 	return rc;
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
 };
 
 /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
 void tmc_enable_hw(struct tmc_drvdata *drvdata);
 void tmc_disable_hw(struct tmc_drvdata *drvdata);
#2 lore raw

Hi,

Quoted 103 lines · James Clark
Quoted 1 line · Yabin Cui

Otherwise, it may cause error in AXI bus and result in a kernel panic.

Hi Yabin,

Thanks for the fix. Do you have a reproducer for this or some more info? For example is it a regression or has it always been there? And on which platform.

Thanks
James

Quoted 88 lines
---
 .../hwtracing/coresight/coresight-tmc-core.c   |  4 +++-
 .../hwtracing/coresight/coresight-tmc-etr.c    | 18 +++++++++++++++---
 drivers/hwtracing/coresight/coresight-tmc.h    |  2 +-
 3 files changed, 19 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 {
      struct coresight_device *csdev = drvdata->csdev;
      struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
      if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
              dev_err(&csdev->dev,
                      "timeout while waiting for TMC to be Ready\n");
+             return -EBUSY;
      }
+     return 0;
 }
 
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..2da99dd41ed6 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
      etr_buf->ops->sync(etr_buf, rrp, rwp);
 }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 {
      u32 axictl, sts;
      struct etr_buf *etr_buf = drvdata->etr_buf;
+     int rc = 0;
 
      CS_UNLOCK(drvdata->base);
 
      /* Wait for TMCSReady bit to be set */
-     tmc_wait_for_tmcready(drvdata);
+     rc = tmc_wait_for_tmcready(drvdata);
+     if (rc) {
+             dev_err(&drvdata->csdev->dev, "not ready ETR isn't enabled\n");
+             CS_LOCK(drvdata->base);
+             return rc;
+     }
 
      writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
      writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
      tmc_enable_hw(drvdata);
 
      CS_LOCK(drvdata->base);
+     return rc;
 }
 
 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
      rc = coresight_claim_device(drvdata->csdev);
      if (!rc) {
              drvdata->etr_buf = etr_buf;
-             __tmc_etr_enable_hw(drvdata);
+             rc = __tmc_etr_enable_hw(drvdata);
+             if (rc) {
+                     drvdata->etr_buf = NULL;
+                     coresight_disclaim_device(drvdata->csdev);
+                     tmc_etr_disable_catu(drvdata);
+             }
      }
 
      return rc;
drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
 };
 
Quoted 6 lines

drivers/hwtracing/coresight/coresight-tmc.h struct tmc_sg_table { · line 261

Quoted 6 lines
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
 /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
 void tmc_enable_hw(struct tmc_drvdata *drvdata);
 void tmc_disable_hw(struct tmc_drvdata *drvdata);

There is no point in waiting for a timeout, and then carrying on even when it is exceeded. As such this patch seems reasonable. We should also apply the same principle to the ETF and ETB devices which use the same tmc_wait_for_tmcready() function.

However - the concern is that this appears to be happening on starting the ETR - there should be no outstanding AXI operations that cause the system to not be ready - as we will either be using this the first time after reset, or we should have successfully stopped and flushed the ETR from the previous operation. This warrants further investigation - should we be extending the timeout - which is already at a rather generous 100uS, and do we also need to check the MemErr bit in the status register?

As James says, we need details of when and how the problem occurs - as far as I know it has not been seen on any systems we currently use (though could have been missed given the current code)

Regards

Mike

--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK

maintainer
#3 lore raw
Quoted 106 lines · Mike Leach

Hi,

Quoted 103 lines · James Clark
Quoted 1 line · Yabin Cui

Otherwise, it may cause error in AXI bus and result in a kernel panic.

Hi Yabin,

Thanks for the fix. Do you have a reproducer for this or some more info? For example is it a regression or has it always been there? And on which platform.

Thanks
James

Quoted 88 lines
---
  .../hwtracing/coresight/coresight-tmc-core.c   |  4 +++-
  .../hwtracing/coresight/coresight-tmc-etr.c    | 18 +++++++++++++++---
  drivers/hwtracing/coresight/coresight-tmc.h    |  2 +-
  3 files changed, 19 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
  DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
  DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
  {
       struct coresight_device *csdev = drvdata->csdev;
       struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
       if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
               dev_err(&csdev->dev,
                       "timeout while waiting for TMC to be Ready\n");
+             return -EBUSY;
       }
+     return 0;
  }
 
  void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..2da99dd41ed6 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
       etr_buf->ops->sync(etr_buf, rrp, rwp);
  }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
  {
       u32 axictl, sts;
       struct etr_buf *etr_buf = drvdata->etr_buf;
+     int rc = 0;
 
       CS_UNLOCK(drvdata->base);
 
       /* Wait for TMCSReady bit to be set */
-     tmc_wait_for_tmcready(drvdata);
+     rc = tmc_wait_for_tmcready(drvdata);
+     if (rc) {
+             dev_err(&drvdata->csdev->dev, "not ready ETR isn't enabled\n");
+             CS_LOCK(drvdata->base);
+             return rc;
+     }
 
       writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
       writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
       tmc_enable_hw(drvdata);
 
       CS_LOCK(drvdata->base);
+     return rc;
  }
 
  static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
       rc = coresight_claim_device(drvdata->csdev);
       if (!rc) {
               drvdata->etr_buf = etr_buf;
-             __tmc_etr_enable_hw(drvdata);
+             rc = __tmc_etr_enable_hw(drvdata);
+             if (rc) {
+                     drvdata->etr_buf = NULL;
+                     coresight_disclaim_device(drvdata->csdev);
+                     tmc_etr_disable_catu(drvdata);
+             }
       }
 
       return rc;
drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
  };
 
Quoted 11 lines
Quoted 6 lines

drivers/hwtracing/coresight/coresight-tmc.h struct tmc_sg_table { · line 261

Quoted 6 lines
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
  /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
  void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
  void tmc_enable_hw(struct tmc_drvdata *drvdata);
  void tmc_disable_hw(struct tmc_drvdata *drvdata);

There is no point in waiting for a timeout, and then carrying on even when it is exceeded. As such this patch seems reasonable. We should also apply the same principle to the ETF and ETB devices which use the same tmc_wait_for_tmcready() function.

+1

I am fine with pushing this change, as it is doing the right thing.

Quoted 9 lines

However - the concern is that this appears to be happening on starting the ETR - there should be no outstanding AXI operations that cause the system to not be ready - as we will either be using this the first time after reset, or we should have successfully stopped and flushed the ETR from the previous operation. This warrants further investigation - should we be extending the timeout - which is already at a rather generous 100uS, and do we also need to check the MemErr bit in the status register?

It would be good to dump the value of TMC_STATUS to see what is going on.

Quoted 4 lines

As James says, we need details of when and how the problem occurs - as far as I know it has not been seen on any systems we currently use (though could have been missed given the current code)

+1

Kind regards
Suzuki

Quoted 10 lines

Regards

Mike

--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author
#4 lore raw
Quoted 3 lines

Do you have a reproducer for this or some more info?
For example is it a regression or has it always been there? And on which platform.

It happens on Pixel 6 and 7. We collect ETM data periodically from some internal dogfood devices. The problem has happened several times on dogfood devices. But I am still trying to reproduce it locally.

We use the scatter-gather mode of ETR, and allocate a 4M buffer. In userspace, we use simpleperf in Android to collect system wide ETM data. What is special is, simpleperf disables and reenables perf events every 100ms to flush ETM data to perf aux buffer.

Pixel 6 and 7 have hardware monitoring AXI traffic. The hardware finds ETR is trying to read from or write to a low invalid address (like 0x2E0000). The problem always happens right after the "tmc_etr: timeout while waiting for TMC to be Ready" message. And in almost all cases, I can find a "timeout while waiting for completion of Manual Flush" message from the previous session.

One log history is below:

[11484.610008][    C0] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[11484.610177][    C0] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[11484.615367][    C0] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[11484.615534][    C0] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[12089.486044][    C0] coresight tmc_etr0: timeout while waiting for

TMC to be Ready
AXI error report reading from invalid address

Another log history is below:

[76709.382650][    C5] coresight tmc_etf1: timeout while waiting for
TMC to be Ready
[76709.382852][    C7] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[76709.382995][    C7] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[76709.384510][    C7] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[76709.384649][    C7] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[76709.384838][    C0] coresight tmc_etr0: timeout while waiting for

TMC to be Ready
AIX error report writing to invalid address

It seems if the previous manual flush doesn't finish gracefully, ETR may not be ready for the next enable (even after 10min as in the first log). And if we continue to enable ETR, an invalid AXI IO may happen.

Thanks,
Yabin

Quoted 159 lines · Suzuki K Poulose
Quoted 106 lines · Mike Leach

Hi,

Quoted 103 lines · James Clark
Quoted 1 line · Yabin Cui

Otherwise, it may cause error in AXI bus and result in a kernel panic.

Hi Yabin,

Thanks for the fix. Do you have a reproducer for this or some more info? For example is it a regression or has it always been there? And on which platform.

Thanks
James

Quoted 88 lines
---
  .../hwtracing/coresight/coresight-tmc-core.c   |  4 +++-
  .../hwtracing/coresight/coresight-tmc-etr.c    | 18 +++++++++++++++---
  drivers/hwtracing/coresight/coresight-tmc.h    |  2 +-
  3 files changed, 19 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
  DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
  DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
  {
       struct coresight_device *csdev = drvdata->csdev;
       struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
       if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
               dev_err(&csdev->dev,
                       "timeout while waiting for TMC to be Ready\n");
+             return -EBUSY;
       }
+     return 0;
  }
 
  void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..2da99dd41ed6 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
       etr_buf->ops->sync(etr_buf, rrp, rwp);
  }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
  {
       u32 axictl, sts;
       struct etr_buf *etr_buf = drvdata->etr_buf;
+     int rc = 0;
 
       CS_UNLOCK(drvdata->base);
 
       /* Wait for TMCSReady bit to be set */
-     tmc_wait_for_tmcready(drvdata);
+     rc = tmc_wait_for_tmcready(drvdata);
+     if (rc) {
+             dev_err(&drvdata->csdev->dev, "not ready ETR isn't enabled\n");
+             CS_LOCK(drvdata->base);
+             return rc;
+     }
 
       writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
       writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
       tmc_enable_hw(drvdata);
 
       CS_LOCK(drvdata->base);
+     return rc;
  }
 
  static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
       rc = coresight_claim_device(drvdata->csdev);
       if (!rc) {
               drvdata->etr_buf = etr_buf;
-             __tmc_etr_enable_hw(drvdata);
+             rc = __tmc_etr_enable_hw(drvdata);
+             if (rc) {
+                     drvdata->etr_buf = NULL;
+                     coresight_disclaim_device(drvdata->csdev);
+                     tmc_etr_disable_catu(drvdata);
+             }
       }
 
       return rc;
drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
  };
 
Quoted 11 lines
Quoted 6 lines

drivers/hwtracing/coresight/coresight-tmc.h struct tmc_sg_table { · line 261

Quoted 6 lines
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
  /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
  void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
  void tmc_enable_hw(struct tmc_drvdata *drvdata);
  void tmc_disable_hw(struct tmc_drvdata *drvdata);

There is no point in waiting for a timeout, and then carrying on even when it is exceeded. As such this patch seems reasonable. We should also apply the same principle to the ETF and ETB devices which use the same tmc_wait_for_tmcready() function.

+1

I am fine with pushing this change, as it is doing the right thing.

Quoted 9 lines

However - the concern is that this appears to be happening on starting the ETR - there should be no outstanding AXI operations that cause the system to not be ready - as we will either be using this the first time after reset, or we should have successfully stopped and flushed the ETR from the previous operation. This warrants further investigation - should we be extending the timeout - which is already at a rather generous 100uS, and do we also need to check the MemErr bit in the status register?

It would be good to dump the value of TMC_STATUS to see what is going on.

Quoted 4 lines

As James says, we need details of when and how the problem occurs - as far as I know it has not been seen on any systems we currently use (though could have been missed given the current code)

+1

Kind regards
Suzuki

Quoted 10 lines

Regards

Mike

--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK

author
#5 lore raw

Ping for review. And I still can't reproduce it, even if I reduced the timeout to 2us and tried different workloads. Any suggestions for how to reproduce it?

Thanks,
Yabin

Quoted 216 lines · Yabin Cui
Quoted 4 lines

Do you have a reproducer for this or some more info?
For example is it a regression or has it always been there? And on which platform.

It happens on Pixel 6 and 7. We collect ETM data periodically from some internal dogfood devices. The problem has happened several times on dogfood devices. But I am still trying to reproduce it locally.

We use the scatter-gather mode of ETR, and allocate a 4M buffer. In userspace, we use simpleperf in Android to collect system wide ETM data. What is special is, simpleperf disables and reenables perf events every 100ms to flush ETM data to perf aux buffer.

Pixel 6 and 7 have hardware monitoring AXI traffic. The hardware finds ETR is trying to read from or write to a low invalid address (like 0x2E0000). The problem always happens right after the "tmc_etr: timeout while waiting for TMC to be Ready" message. And in almost all cases, I can find a "timeout while waiting for completion of Manual Flush" message from the previous session.

One log history is below:

[11484.610008][    C0] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[11484.610177][    C0] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[11484.615367][    C0] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[11484.615534][    C0] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[12089.486044][    C0] coresight tmc_etr0: timeout while waiting for

TMC to be Ready
AXI error report reading from invalid address

Another log history is below:

[76709.382650][    C5] coresight tmc_etf1: timeout while waiting for
TMC to be Ready
[76709.382852][    C7] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[76709.382995][    C7] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[76709.384510][    C7] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[76709.384649][    C7] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[76709.384838][    C0] coresight tmc_etr0: timeout while waiting for

TMC to be Ready
AIX error report writing to invalid address

It seems if the previous manual flush doesn't finish gracefully, ETR may not be ready for the next enable (even after 10min as in the first log). And if we continue to enable ETR, an invalid AXI IO may happen.

Thanks,
Yabin

Quoted 159 lines · Suzuki K Poulose
Quoted 106 lines · Mike Leach

Hi,

Quoted 103 lines · James Clark
Quoted 1 line · Yabin Cui

Otherwise, it may cause error in AXI bus and result in a kernel panic.

Hi Yabin,

Thanks for the fix. Do you have a reproducer for this or some more info? For example is it a regression or has it always been there? And on which platform.

Thanks
James

Quoted 88 lines
---
  .../hwtracing/coresight/coresight-tmc-core.c   |  4 +++-
  .../hwtracing/coresight/coresight-tmc-etr.c    | 18 +++++++++++++++---
  drivers/hwtracing/coresight/coresight-tmc.h    |  2 +-
  3 files changed, 19 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
  DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
  DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
  {
       struct coresight_device *csdev = drvdata->csdev;
       struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
       if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
               dev_err(&csdev->dev,
                       "timeout while waiting for TMC to be Ready\n");
+             return -EBUSY;
       }
+     return 0;
  }
 
  void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..2da99dd41ed6 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
       etr_buf->ops->sync(etr_buf, rrp, rwp);
  }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
  {
       u32 axictl, sts;
       struct etr_buf *etr_buf = drvdata->etr_buf;
+     int rc = 0;
 
       CS_UNLOCK(drvdata->base);
 
       /* Wait for TMCSReady bit to be set */
-     tmc_wait_for_tmcready(drvdata);
+     rc = tmc_wait_for_tmcready(drvdata);
+     if (rc) {
+             dev_err(&drvdata->csdev->dev, "not ready ETR isn't enabled\n");
+             CS_LOCK(drvdata->base);
+             return rc;
+     }
 
       writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
       writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
       tmc_enable_hw(drvdata);
 
       CS_LOCK(drvdata->base);
+     return rc;
  }
 
  static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
       rc = coresight_claim_device(drvdata->csdev);
       if (!rc) {
               drvdata->etr_buf = etr_buf;
-             __tmc_etr_enable_hw(drvdata);
+             rc = __tmc_etr_enable_hw(drvdata);
+             if (rc) {
+                     drvdata->etr_buf = NULL;
+                     coresight_disclaim_device(drvdata->csdev);
+                     tmc_etr_disable_catu(drvdata);
+             }
       }
 
       return rc;
drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
  };
 
Quoted 11 lines
Quoted 6 lines

drivers/hwtracing/coresight/coresight-tmc.h struct tmc_sg_table { · line 261

Quoted 6 lines
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
  /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
  void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
  void tmc_enable_hw(struct tmc_drvdata *drvdata);
  void tmc_disable_hw(struct tmc_drvdata *drvdata);

There is no point in waiting for a timeout, and then carrying on even when it is exceeded. As such this patch seems reasonable. We should also apply the same principle to the ETF and ETB devices which use the same tmc_wait_for_tmcready() function.

+1

I am fine with pushing this change, as it is doing the right thing.

Quoted 9 lines

However - the concern is that this appears to be happening on starting the ETR - there should be no outstanding AXI operations that cause the system to not be ready - as we will either be using this the first time after reset, or we should have successfully stopped and flushed the ETR from the previous operation. This warrants further investigation - should we be extending the timeout - which is already at a rather generous 100uS, and do we also need to check the MemErr bit in the status register?

It would be good to dump the value of TMC_STATUS to see what is going on.

Quoted 4 lines

As James says, we need details of when and how the problem occurs - as far as I know it has not been seen on any systems we currently use (though could have been missed given the current code)

+1

Kind regards
Suzuki

Quoted 10 lines

Regards

Mike

--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK

#6 lore raw
Quoted 4 lines · Yabin Cui

Ping for review. And I still can't reproduce it, even if I reduced the timeout to 2us and tried different workloads. Any suggestions for how to reproduce it?

Hi Yabin,

I just found this in my kernel log files from a few days ago.

Jan 19 15:03:35 n1-sdp kernel: [865337.521413] coresight tmc_etr0:
timeout while waiting for completion of Manual Flush
Jan 19 15:03:35 n1-sdp kernel: [865337.529351] coresight tmc_etr0:

timeout while waiting for TMC to be Ready

This is on N1SDP, and I've only been running "normal" perf commands like:

perf record -e cs_etm//k -- taskset -c 2 sleep 1 perf record -e cs_etm// -- stress -c 2 -t 1

I also haven't been able to reproduce it since

James

maintainer
#7 lore raw
In reply to Yabin Cui

Folks,

Quoted 4 lines · Yabin Cui

Ping for review. And I still can't reproduce it, even if I reduced the timeout to 2us and tried different workloads. Any suggestions for how to reproduce it?

I think we should go ahead and fix this in the driver to handle flaky hardware cases. But I would like this to be addressed for all the TMC types, not just ETRs, as Mike pointed out.

Thanks
Suzuki

Quoted 220 lines

Thanks,
Yabin

Quoted 216 lines · Yabin Cui
Quoted 4 lines

Do you have a reproducer for this or some more info?
For example is it a regression or has it always been there? And on which platform.

It happens on Pixel 6 and 7. We collect ETM data periodically from some internal dogfood devices. The problem has happened several times on dogfood devices. But I am still trying to reproduce it locally.

We use the scatter-gather mode of ETR, and allocate a 4M buffer. In userspace, we use simpleperf in Android to collect system wide ETM data. What is special is, simpleperf disables and reenables perf events every 100ms to flush ETM data to perf aux buffer.

Pixel 6 and 7 have hardware monitoring AXI traffic. The hardware finds ETR is trying to read from or write to a low invalid address (like 0x2E0000). The problem always happens right after the "tmc_etr: timeout while waiting for TMC to be Ready" message. And in almost all cases, I can find a "timeout while waiting for completion of Manual Flush" message from the previous session.

One log history is below:

[11484.610008][    C0] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[11484.610177][    C0] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[11484.615367][    C0] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[11484.615534][    C0] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[12089.486044][    C0] coresight tmc_etr0: timeout while waiting for

TMC to be Ready
AXI error report reading from invalid address

Another log history is below:

[76709.382650][    C5] coresight tmc_etf1: timeout while waiting for
TMC to be Ready
[76709.382852][    C7] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[76709.382995][    C7] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[76709.384510][    C7] coresight tmc_etr0: timeout while waiting for
completion of Manual Flush
[76709.384649][    C7] coresight tmc_etr0: timeout while waiting for
TMC to be Ready
[76709.384838][    C0] coresight tmc_etr0: timeout while waiting for

TMC to be Ready
AIX error report writing to invalid address

It seems if the previous manual flush doesn't finish gracefully, ETR may not be ready for the next enable (even after 10min as in the first log). And if we continue to enable ETR, an invalid AXI IO may happen.

Thanks,
Yabin

Quoted 159 lines · Suzuki K Poulose
Quoted 106 lines · Mike Leach

Hi,

Quoted 103 lines · James Clark
Quoted 1 line · Yabin Cui

Otherwise, it may cause error in AXI bus and result in a kernel panic.

Hi Yabin,

Thanks for the fix. Do you have a reproducer for this or some more info? For example is it a regression or has it always been there? And on which platform.

Thanks
James

Quoted 88 lines
---
   .../hwtracing/coresight/coresight-tmc-core.c   |  4 +++-
   .../hwtracing/coresight/coresight-tmc-etr.c    | 18 +++++++++++++++---
   drivers/hwtracing/coresight/coresight-tmc.h    |  2 +-
   3 files changed, 19 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
   DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
   DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
   {
        struct coresight_device *csdev = drvdata->csdev;
        struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
        if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
                dev_err(&csdev->dev,
                        "timeout while waiting for TMC to be Ready\n");
+             return -EBUSY;
        }
+     return 0;
   }
 
   void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..2da99dd41ed6 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
        etr_buf->ops->sync(etr_buf, rrp, rwp);
   }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
   {
        u32 axictl, sts;
        struct etr_buf *etr_buf = drvdata->etr_buf;
+     int rc = 0;
 
        CS_UNLOCK(drvdata->base);
 
        /* Wait for TMCSReady bit to be set */
-     tmc_wait_for_tmcready(drvdata);
+     rc = tmc_wait_for_tmcready(drvdata);
+     if (rc) {
+             dev_err(&drvdata->csdev->dev, "not ready ETR isn't enabled\n");
+             CS_LOCK(drvdata->base);
+             return rc;
+     }
 
        writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
        writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
        tmc_enable_hw(drvdata);
 
        CS_LOCK(drvdata->base);
+     return rc;
   }
 
   static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
        rc = coresight_claim_device(drvdata->csdev);
        if (!rc) {
                drvdata->etr_buf = etr_buf;
-             __tmc_etr_enable_hw(drvdata);
+             rc = __tmc_etr_enable_hw(drvdata);
+             if (rc) {
+                     drvdata->etr_buf = NULL;
+                     coresight_disclaim_device(drvdata->csdev);
+                     tmc_etr_disable_catu(drvdata);
+             }
        }
 
        return rc;
drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
   };
 
Quoted 11 lines
Quoted 6 lines

drivers/hwtracing/coresight/coresight-tmc.h struct tmc_sg_table { · line 261

Quoted 6 lines
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
   /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
   void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
   void tmc_enable_hw(struct tmc_drvdata *drvdata);
   void tmc_disable_hw(struct tmc_drvdata *drvdata);

There is no point in waiting for a timeout, and then carrying on even when it is exceeded. As such this patch seems reasonable. We should also apply the same principle to the ETF and ETB devices which use the same tmc_wait_for_tmcready() function.

+1

I am fine with pushing this change, as it is doing the right thing.

Quoted 9 lines

However - the concern is that this appears to be happening on starting the ETR - there should be no outstanding AXI operations that cause the system to not be ready - as we will either be using this the first time after reset, or we should have successfully stopped and flushed the ETR from the previous operation. This warrants further investigation - should we be extending the timeout - which is already at a rather generous 100uS, and do we also need to check the MemErr bit in the status register?

It would be good to dump the value of TMC_STATUS to see what is going on.

Quoted 4 lines

As James says, we need details of when and how the problem occurs - as far as I know it has not been seen on any systems we currently use (though could have been missed given the current code)

+1

Kind regards
Suzuki

Quoted 10 lines

Regards

Mike

--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK

author
#8 lore raw

PATCH v2 1/1 coresight: tmc: Don't enable TMC when it's not ready.

If TMC ETR is enabled without being ready, in later use we may see AXI bus errors caused by accessing invalid addresses.

---
V1 -> V2: Make change to all TMCs instead of just ETR

 .../hwtracing/coresight/coresight-tmc-core.c  |  4 +-
 .../hwtracing/coresight/coresight-tmc-etf.c   | 43 +++++++++++++++----
 .../hwtracing/coresight/coresight-tmc-etr.c   | 18 ++++++--
 drivers/hwtracing/coresight/coresight-tmc.h   |  2 +-
 4 files changed, 53 insertions(+), 14 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 {
 	struct coresight_device *csdev = drvdata->csdev;
 	struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 	if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
 		dev_err(&csdev->dev,
 			"timeout while waiting for TMC to be Ready\n");
+		return -EBUSY;
 	}
+	return 0;
 }
 
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etf.c +34 -9

99 lines not on this page · raw message

drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..0811cb44588b 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
 	etr_buf->ops->sync(etr_buf, rrp, rwp);
 }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 {
 	u32 axictl, sts;
 	struct etr_buf *etr_buf = drvdata->etr_buf;
+	int rc = 0;
 
 	CS_UNLOCK(drvdata->base);
 
 	/* Wait for TMCSReady bit to be set */
-	tmc_wait_for_tmcready(drvdata);
+	rc = tmc_wait_for_tmcready(drvdata);
+	if (rc) {
+		dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+		CS_LOCK(drvdata->base);
+		return rc;
+	}
 
 	writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
 	writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 	tmc_enable_hw(drvdata);
 
 	CS_LOCK(drvdata->base);
+	return rc;
 }
 
 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
 	rc = coresight_claim_device(drvdata->csdev);
 	if (!rc) {
 		drvdata->etr_buf = etr_buf;
-		__tmc_etr_enable_hw(drvdata);
+		rc = __tmc_etr_enable_hw(drvdata);
+		if (rc) {
+			drvdata->etr_buf = NULL;
+			coresight_disclaim_device(drvdata->csdev);
+			tmc_etr_disable_catu(drvdata);
+		}
 	}
 
 	return rc;
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
 };
 
 /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
 void tmc_enable_hw(struct tmc_drvdata *drvdata);
 void tmc_disable_hw(struct tmc_drvdata *drvdata);
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
#9 lore raw

Re: [PATCH v2] coresight: tmc: Don't enable TMC when it's not ready.

Hi,

Quoted 136 lines · Yabin Cui

If TMC ETR is enabled without being ready, in later use we may see AXI bus errors caused by accessing invalid addresses.

---
V1 -> V2: Make change to all TMCs instead of just ETR

 .../hwtracing/coresight/coresight-tmc-core.c  |  4 +-
 .../hwtracing/coresight/coresight-tmc-etf.c   | 43 +++++++++++++++----
 .../hwtracing/coresight/coresight-tmc-etr.c   | 18 ++++++--
 drivers/hwtracing/coresight/coresight-tmc.h   |  2 +-
 4 files changed, 53 insertions(+), 14 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 {
        struct coresight_device *csdev = drvdata->csdev;
        struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
        if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
                dev_err(&csdev->dev,
                        "timeout while waiting for TMC to be Ready\n");
+               return -EBUSY;
        }
+       return 0;
 }
 
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etf.c +29 -8
index 4c4cbd1f7258..2840227e9135 100644
@@ -16,12 +16,19 @@
 static int tmc_set_etf_buffer(struct coresight_device *csdev,
                              struct perf_output_handle *handle);
 
-static void __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
 {
+       int rc = 0;
+
        CS_UNLOCK(drvdata->base);
 
        /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
        writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
        writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
@@ -33,6 +40,7 @@ static void __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
        tmc_enable_hw(drvdata);
 
        CS_LOCK(drvdata->base);
+       return rc;
 }
 
 static int tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
@@ -42,8 +50,10 @@ static int tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
        if (rc)
                return rc;
 
-       __tmc_etb_enable_hw(drvdata);
-       return 0;
+       rc = __tmc_etb_enable_hw(drvdata);
+       if (rc)
+               coresight_disclaim_device(drvdata->csdev);
+       return rc;
 }
 
 static void tmc_etb_dump_hw(struct tmc_drvdata *drvdata)
@@ -91,12 +101,19 @@ static void tmc_etb_disable_hw(struct tmc_drvdata *drvdata)
        coresight_disclaim_device(drvdata->csdev);
 }
 
-static void __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
 {
+       int rc = 0;
+
        CS_UNLOCK(drvdata->base);
 
        /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
        writel_relaxed(TMC_MODE_HARDWARE_FIFO, drvdata->base + TMC_MODE);
        writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI,
@@ -105,6 +122,7 @@ static void __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
        tmc_enable_hw(drvdata);
 
        CS_LOCK(drvdata->base);
+       return rc;
 }
 
 static int tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
@@ -114,8 +132,10 @@ static int tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
        if (rc)
                return rc;
 
-       __tmc_etf_enable_hw(drvdata);
-       return 0;
+       rc = __tmc_etf_enable_hw(drvdata);
+       if (rc)
+               coresight_disclaim_device(drvdata->csdev);
+       return rc;
 }
 
 static void tmc_etf_disable_hw(struct tmc_drvdata *drvdata)
@@ -639,6 +659,7 @@ int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata)
        char *buf = NULL;
        enum tmc_mode mode;
        unsigned long flags;
+       int rc = 0;
 
        /* config types are set a boot time and never change */
        if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETB &&

drivers/hwtracing/coresight/coresight-tmc-etf.c int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata) · line 692

Quoted 9 lines
drivers/hwtracing/coresight/coresight-tmc-etf.c +5 -1
@@ -664,7 +685,11 @@ int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata)
                 * can't be NULL.
                 */
                memset(drvdata->buf, 0, drvdata->size);
-               __tmc_etb_enable_hw(drvdata);
+               rc = __tmc_etb_enable_hw(drvdata);
+               if (rc) {
+                       spin_unlock_irqrestore(&drvdata->spinlock, flags);
+                       return rc;
+               }

There is a similar unprepare function in ETR - this should have similar updates.

Quoted 67 lines
drivers/hwtracing/coresight/coresight-tmc-etf.c +0 -0
        } else {
                /*
                 * The ETB/ETF is not tracing and the buffer was just read.
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..0811cb44588b 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
        etr_buf->ops->sync(etr_buf, rrp, rwp);
 }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 {
        u32 axictl, sts;
        struct etr_buf *etr_buf = drvdata->etr_buf;
+       int rc = 0;
 
        CS_UNLOCK(drvdata->base);
 
        /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
        writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
        writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
        tmc_enable_hw(drvdata);
 
        CS_LOCK(drvdata->base);
+       return rc;
 }
 
 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
        rc = coresight_claim_device(drvdata->csdev);
        if (!rc) {
                drvdata->etr_buf = etr_buf;
-               __tmc_etr_enable_hw(drvdata);
+               rc = __tmc_etr_enable_hw(drvdata);
+               if (rc) {
+                       drvdata->etr_buf = NULL;
+                       coresight_disclaim_device(drvdata->csdev);
+                       tmc_etr_disable_catu(drvdata);
+               }
        }
 
        return rc;
drivers/hwtracing/coresight/coresight-tmc.h +1 -2
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
 };
 
 /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
 void tmc_enable_hw(struct tmc_drvdata *drvdata);
 void tmc_disable_hw(struct tmc_drvdata *drvdata);
--
Quoted 2 lines

2.39.1.456.gfc5497dd1b-goog

The tmc_flush_and_stop() function also calls tmc_wait_for_tmcready().

The etb/etf_prepare_read operations disable the etb /etf before a read commences. For consistency if this fails due to TMC not being ready do we also need to fail the read prepare operations?

Regards

Mike

Signature · 3 lines
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK
maintainer
#10 lore raw

Re: [PATCH v2] coresight: tmc: Don't enable TMC when it's not ready.

Hi Mike,

Thanks for the comments, please note that I have queued this patch for next.

Quoted 212 lines · Mike Leach

Hi,

Quoted 136 lines · Yabin Cui

If TMC ETR is enabled without being ready, in later use we may see AXI bus errors caused by accessing invalid addresses.

---
V1 -> V2: Make change to all TMCs instead of just ETR

  .../hwtracing/coresight/coresight-tmc-core.c  |  4 +-
  .../hwtracing/coresight/coresight-tmc-etf.c   | 43 +++++++++++++++----
  .../hwtracing/coresight/coresight-tmc-etr.c   | 18 ++++++--
  drivers/hwtracing/coresight/coresight-tmc.h   |  2 +-
  4 files changed, 53 insertions(+), 14 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
  DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
  DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
  {
         struct coresight_device *csdev = drvdata->csdev;
         struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
         if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
                 dev_err(&csdev->dev,
                         "timeout while waiting for TMC to be Ready\n");
+               return -EBUSY;
         }
+       return 0;
  }
 
  void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etf.c +29 -8
index 4c4cbd1f7258..2840227e9135 100644
@@ -16,12 +16,19 @@
  static int tmc_set_etf_buffer(struct coresight_device *csdev,
                               struct perf_output_handle *handle);
 
-static void __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
  {
+       int rc = 0;
+
         CS_UNLOCK(drvdata->base);
 
         /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
         writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
         writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
@@ -33,6 +40,7 @@ static void __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
         tmc_enable_hw(drvdata);
 
         CS_LOCK(drvdata->base);
+       return rc;
  }
 
  static int tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
@@ -42,8 +50,10 @@ static int tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
         if (rc)
                 return rc;
 
-       __tmc_etb_enable_hw(drvdata);
-       return 0;
+       rc = __tmc_etb_enable_hw(drvdata);
+       if (rc)
+               coresight_disclaim_device(drvdata->csdev);
+       return rc;
  }
 
  static void tmc_etb_dump_hw(struct tmc_drvdata *drvdata)
@@ -91,12 +101,19 @@ static void tmc_etb_disable_hw(struct tmc_drvdata *drvdata)
         coresight_disclaim_device(drvdata->csdev);
  }
 
-static void __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
  {
+       int rc = 0;
+
         CS_UNLOCK(drvdata->base);
 
         /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
         writel_relaxed(TMC_MODE_HARDWARE_FIFO, drvdata->base + TMC_MODE);
         writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI,
@@ -105,6 +122,7 @@ static void __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
         tmc_enable_hw(drvdata);
 
         CS_LOCK(drvdata->base);
+       return rc;
  }
 
  static int tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
@@ -114,8 +132,10 @@ static int tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
         if (rc)
                 return rc;
 
-       __tmc_etf_enable_hw(drvdata);
-       return 0;
+       rc = __tmc_etf_enable_hw(drvdata);
+       if (rc)
+               coresight_disclaim_device(drvdata->csdev);
+       return rc;
  }
 
  static void tmc_etf_disable_hw(struct tmc_drvdata *drvdata)
@@ -639,6 +659,7 @@ int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata)
         char *buf = NULL;
         enum tmc_mode mode;
         unsigned long flags;
+       int rc = 0;
 
         /* config types are set a boot time and never change */
         if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETB &&

drivers/hwtracing/coresight/coresight-tmc-etf.c int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata) · line 692

Quoted 9 lines
drivers/hwtracing/coresight/coresight-tmc-etf.c +5 -1
@@ -664,7 +685,11 @@ int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata)
                  * can't be NULL.
                  */
                 memset(drvdata->buf, 0, drvdata->size);
-               __tmc_etb_enable_hw(drvdata);
+               rc = __tmc_etb_enable_hw(drvdata);
+               if (rc) {
+                       spin_unlock_irqrestore(&drvdata->spinlock, flags);
+                       return rc;
+               }

There is a similar unprepare function in ETR - this should have similar updates.

Quoted 60 lines
drivers/hwtracing/coresight/coresight-tmc-etf.c +0 -0
         } else {
                 /*
                  * The ETB/ETF is not tracing and the buffer was just read.
drivers/hwtracing/coresight/coresight-tmc-etr.c +15 -3
index 867ad8bb9b0c..0811cb44588b 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
         etr_buf->ops->sync(etr_buf, rrp, rwp);
  }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
  {
         u32 axictl, sts;
         struct etr_buf *etr_buf = drvdata->etr_buf;
+       int rc = 0;
 
         CS_UNLOCK(drvdata->base);
 
         /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
         writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
         writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
         tmc_enable_hw(drvdata);
 
         CS_LOCK(drvdata->base);
+       return rc;
  }
 
  static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
         rc = coresight_claim_device(drvdata->csdev);
         if (!rc) {
                 drvdata->etr_buf = etr_buf;
-               __tmc_etr_enable_hw(drvdata);
+               rc = __tmc_etr_enable_hw(drvdata);
+               if (rc) {
+                       drvdata->etr_buf = NULL;
+                       coresight_disclaim_device(drvdata->csdev);
+                       tmc_etr_disable_catu(drvdata);
+               }
         }
 
         return rc;
drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
  };
 
Quoted 11 lines
Quoted 9 lines
drivers/hwtracing/coresight/coresight-tmc.h +1 -2
  /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
  void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
  void tmc_enable_hw(struct tmc_drvdata *drvdata);
  void tmc_disable_hw(struct tmc_drvdata *drvdata);
--

2.39.1.456.gfc5497dd1b-goog

The tmc_flush_and_stop() function also calls tmc_wait_for_tmcready().

I think this already an error message there, when it fails to complete the flush.I thought of adding a WARN_ON, but thought it is not worth much.

Quoted 4 lines

The etb/etf_prepare_read operations disable the etb /etf before a read commences. For consistency if this fails due to TMC not being ready do we also need to fail the read prepare operations?

I think that should be OK, as we can read what the TMC has already flushed/written to the memory. A following session may not be able to use the ETR, which is fine.

Cheers
Suzuki

Quoted 6 lines

Regards

Mike

author
#11 lore raw

Re: [PATCH v2] coresight: tmc: Don't enable TMC when it's not ready.

Hi,

Quoted 1 line
Quoted 1 line

There is a similar unprepare function in ETR - this should have similar updates.

Thanks for reminding me! I will update the patch to fix it.

Quoted 4 lines
Quoted 1 line

The tmc_flush_and_stop() function also calls tmc_wait_for_tmcready().

I think this already an error message there, when it fails to complete the flush.I thought of adding a WARN_ON, but thought it is not worth much.

I can see error messages when tmc flush timeouts or tmcready timeouts. I don't know how the callers (read_buffer and disable) can handle this error. I think the perf event call chain doesn't accept failures when disabling coresight
devices. So I feel reporting the error is the best effort we can do.

Thanks,
Yabin

author
#12 lore raw

PATCH v3 1/1 coresight: tmc: Don't enable TMC when it's not ready.

If TMC ETR is enabled without being ready, in later use we may see AXI bus errors caused by accessing invalid addresses.

---
V1 -> V2: Make change to all TMCs instead of just ETR V2 -> V3: Handle etr enable failure in tmc_read_unprepare_etr

 .../hwtracing/coresight/coresight-tmc-core.c  |  4 +-
 .../hwtracing/coresight/coresight-tmc-etf.c   | 43 +++++++++++++++----
 .../hwtracing/coresight/coresight-tmc-etr.c   | 25 +++++++++--
 drivers/hwtracing/coresight/coresight-tmc.h   |  2 +-
 4 files changed, 59 insertions(+), 15 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 {
 	struct coresight_device *csdev = drvdata->csdev;
 	struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 	if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
 		dev_err(&csdev->dev,
 			"timeout while waiting for TMC to be Ready\n");
+		return -EBUSY;
 	}
+	return 0;
 }
 
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etf.c +34 -9

99 lines not on this page · raw message

drivers/hwtracing/coresight/coresight-tmc-etr.c +21 -4
index 867ad8bb9b0c..4952425dd6e4 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
 	etr_buf->ops->sync(etr_buf, rrp, rwp);
 }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 {
 	u32 axictl, sts;
 	struct etr_buf *etr_buf = drvdata->etr_buf;
+	int rc = 0;
 
 	CS_UNLOCK(drvdata->base);
 
 	/* Wait for TMCSReady bit to be set */
-	tmc_wait_for_tmcready(drvdata);
+	rc = tmc_wait_for_tmcready(drvdata);
+	if (rc) {
+		dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+		CS_LOCK(drvdata->base);
+		return rc;
+	}
 
 	writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
 	writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 	tmc_enable_hw(drvdata);
 
 	CS_LOCK(drvdata->base);
+	return rc;
 }
 
 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
 	rc = coresight_claim_device(drvdata->csdev);
 	if (!rc) {
 		drvdata->etr_buf = etr_buf;
-		__tmc_etr_enable_hw(drvdata);
+		rc = __tmc_etr_enable_hw(drvdata);
+		if (rc) {
+			drvdata->etr_buf = NULL;
+			coresight_disclaim_device(drvdata->csdev);
+			tmc_etr_disable_catu(drvdata);
+		}
 	}
 
 	return rc;
@@ -1750,6 +1762,7 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
 {
 	unsigned long flags;
 	struct etr_buf *sysfs_buf = NULL;
+	int rc = 0;
 
 	/* config types are set a boot time and never change */
 	if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
@@ -1764,7 +1777,11 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
 		 * buffer. Since the tracer is still enabled drvdata::buf can't
 		 * be NULL.
 		 */
-		__tmc_etr_enable_hw(drvdata);
+		rc = __tmc_etr_enable_hw(drvdata);
+		if (rc) {
+			spin_unlock_irqrestore(&drvdata->spinlock, flags);
+			return rc;
+		}
 	} else {
 		/*
 		 * The ETR is not tracing and the buffer was just read.
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
 };
 
 /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
 void tmc_enable_hw(struct tmc_drvdata *drvdata);
 void tmc_disable_hw(struct tmc_drvdata *drvdata);
Signature · 1 line
2.39.1.519.gcb327c4b5f-goog
author
#13 lore raw

Re: [PATCH v3] coresight: tmc: Don't enable TMC when it's not ready.

Friendly ping?

Quoted 236 lines · Yabin Cui

If TMC ETR is enabled without being ready, in later use we may see AXI bus errors caused by accessing invalid addresses.

---
V1 -> V2: Make change to all TMCs instead of just ETR V2 -> V3: Handle etr enable failure in tmc_read_unprepare_etr

 .../hwtracing/coresight/coresight-tmc-core.c  |  4 +-
 .../hwtracing/coresight/coresight-tmc-etf.c   | 43 +++++++++++++++----
 .../hwtracing/coresight/coresight-tmc-etr.c   | 25 +++++++++--
 drivers/hwtracing/coresight/coresight-tmc.h   |  2 +-
 4 files changed, 59 insertions(+), 15 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-core.c +3 -1
index 07abf28ad725..c106d142e632 100644
@@ -31,7 +31,7 @@ DEFINE_CORESIGHT_DEVLIST(etb_devs, "tmc_etb");
 DEFINE_CORESIGHT_DEVLIST(etf_devs, "tmc_etf");
 DEFINE_CORESIGHT_DEVLIST(etr_devs, "tmc_etr");
 
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
 {
        struct coresight_device *csdev = drvdata->csdev;
        struct csdev_access *csa = &csdev->access;
@@ -40,7 +40,9 @@ void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
        if (coresight_timeout(csa, TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
                dev_err(&csdev->dev,
                        "timeout while waiting for TMC to be Ready\n");
+               return -EBUSY;
        }
+       return 0;
 }
 
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
drivers/hwtracing/coresight/coresight-tmc-etf.c +34 -9
index 4c4cbd1f7258..2840227e9135 100644
@@ -16,12 +16,19 @@
 static int tmc_set_etf_buffer(struct coresight_device *csdev,
                              struct perf_output_handle *handle);
 
-static void __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
 {
+       int rc = 0;
+
        CS_UNLOCK(drvdata->base);
 
        /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
        writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
        writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
@@ -33,6 +40,7 @@ static void __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
        tmc_enable_hw(drvdata);
 
        CS_LOCK(drvdata->base);
+       return rc;
 }
 
 static int tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
@@ -42,8 +50,10 @@ static int tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
        if (rc)
                return rc;
 
-       __tmc_etb_enable_hw(drvdata);
-       return 0;
+       rc = __tmc_etb_enable_hw(drvdata);
+       if (rc)
+               coresight_disclaim_device(drvdata->csdev);
+       return rc;
 }
 
 static void tmc_etb_dump_hw(struct tmc_drvdata *drvdata)
@@ -91,12 +101,19 @@ static void tmc_etb_disable_hw(struct tmc_drvdata *drvdata)
        coresight_disclaim_device(drvdata->csdev);
 }
 
-static void __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
 {
+       int rc = 0;
+
        CS_UNLOCK(drvdata->base);
 
        /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
        writel_relaxed(TMC_MODE_HARDWARE_FIFO, drvdata->base + TMC_MODE);
        writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI,
@@ -105,6 +122,7 @@ static void __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
        tmc_enable_hw(drvdata);
 
        CS_LOCK(drvdata->base);
+       return rc;
 }
 
 static int tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
@@ -114,8 +132,10 @@ static int tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
        if (rc)
                return rc;
 
-       __tmc_etf_enable_hw(drvdata);
-       return 0;
+       rc = __tmc_etf_enable_hw(drvdata);
+       if (rc)
+               coresight_disclaim_device(drvdata->csdev);
+       return rc;
 }
 
 static void tmc_etf_disable_hw(struct tmc_drvdata *drvdata)
@@ -639,6 +659,7 @@ int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata)
        char *buf = NULL;
        enum tmc_mode mode;
        unsigned long flags;
+       int rc = 0;
 
        /* config types are set a boot time and never change */
        if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETB &&
@@ -664,7 +685,11 @@ int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata)
                 * can't be NULL.
                 */
                memset(drvdata->buf, 0, drvdata->size);
-               __tmc_etb_enable_hw(drvdata);
+               rc = __tmc_etb_enable_hw(drvdata);
+               if (rc) {
+                       spin_unlock_irqrestore(&drvdata->spinlock, flags);
+                       return rc;
+               }
        } else {
                /*
                 * The ETB/ETF is not tracing and the buffer was just read.
drivers/hwtracing/coresight/coresight-tmc-etr.c +21 -4
index 867ad8bb9b0c..4952425dd6e4 100644
@@ -983,15 +983,21 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
        etr_buf->ops->sync(etr_buf, rrp, rwp);
 }
 
-static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
+static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
 {
        u32 axictl, sts;
        struct etr_buf *etr_buf = drvdata->etr_buf;
+       int rc = 0;
 
        CS_UNLOCK(drvdata->base);
 
        /* Wait for TMCSReady bit to be set */
-       tmc_wait_for_tmcready(drvdata);
+       rc = tmc_wait_for_tmcready(drvdata);
+       if (rc) {
+               dev_err(&drvdata->csdev->dev, "fails to enable not ready TMC\n");
+               CS_LOCK(drvdata->base);
+               return rc;
+       }
 
        writel_relaxed(etr_buf->size / 4, drvdata->base + TMC_RSZ);
        writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
@@ -1032,6 +1038,7 @@ static void __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
        tmc_enable_hw(drvdata);
 
        CS_LOCK(drvdata->base);
+       return rc;
 }
 
 static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
@@ -1060,7 +1067,12 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
        rc = coresight_claim_device(drvdata->csdev);
        if (!rc) {
                drvdata->etr_buf = etr_buf;
-               __tmc_etr_enable_hw(drvdata);
+               rc = __tmc_etr_enable_hw(drvdata);
+               if (rc) {
+                       drvdata->etr_buf = NULL;
+                       coresight_disclaim_device(drvdata->csdev);
+                       tmc_etr_disable_catu(drvdata);
+               }
        }
 
        return rc;
@@ -1750,6 +1762,7 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
 {
        unsigned long flags;
        struct etr_buf *sysfs_buf = NULL;
+       int rc = 0;
 
        /* config types are set a boot time and never change */
        if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
@@ -1764,7 +1777,11 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
                 * buffer. Since the tracer is still enabled drvdata::buf can't
                 * be NULL.
                 */
-               __tmc_etr_enable_hw(drvdata);
+               rc = __tmc_etr_enable_hw(drvdata);
+               if (rc) {
+                       spin_unlock_irqrestore(&drvdata->spinlock, flags);
+                       return rc;
+               }
        } else {
                /*
                 * The ETR is not tracing and the buffer was just read.
drivers/hwtracing/coresight/coresight-tmc.h +1 -2
index 66959557cf39..01c0382a29c0 100644
@@ -255,7 +255,7 @@ struct tmc_sg_table {
 };
 
 /* Generic functions */
-void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
+int tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
 void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
 void tmc_enable_hw(struct tmc_drvdata *drvdata);
 void tmc_disable_hw(struct tmc_drvdata *drvdata);
--

2.39.1.519.gcb327c4b5f-goog

maintainer
#14 lore raw

Re: [PATCH v3] coresight: tmc: Don't enable TMC when it's not ready.

Quoted 11 lines · Yabin Cui

Friendly ping?

Quoted 8 lines · Yabin Cui

If TMC ETR is enabled without being ready, in later use we may see AXI bus errors caused by accessing invalid addresses.

---
V1 -> V2: Make change to all TMCs instead of just ETR V2 -> V3: Handle etr enable failure in tmc_read_unprepare_etr

As I mentioned, v2 was queued. Please could you update your changes on top of the coresight next branch and resend the patch ?

Suzuki

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author
#15 lore raw

PATCH 1/1 coresight: tmc-etr: Handle enable failure in tmc_read_unprepare_etr

It's similar to what we did in tmc_read_unprepare_etb.

---
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +6 -1
index 918d461fcf4a..b04f12079efd 100644
@@ -1763,6 +1763,7 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
 {
 	unsigned long flags;
 	struct etr_buf *sysfs_buf = NULL;
+	int rc = 0;
 
 	/* config types are set a boot time and never change */
 	if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
@@ -1777,7 +1778,11 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
 		 * buffer. Since the tracer is still enabled drvdata::buf can't
 		 * be NULL.
 		 */
-		__tmc_etr_enable_hw(drvdata);
+		rc = __tmc_etr_enable_hw(drvdata);
+		if (rc) {
+			spin_unlock_irqrestore(&drvdata->spinlock, flags);
+			return rc;
+		}
 	} else {
 		/*
 		 * The ETR is not tracing and the buffer was just read.
Signature · 1 line
2.39.1.581.gbfd45094c4-goog
author
#16 lore raw

Re: [PATCH] coresight: tmc-etr: Handle enable failure in tmc_read_unprepare_etr

Ping for review?

Quoted 36 lines · Yabin Cui

It's similar to what we did in tmc_read_unprepare_etb.

---
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +6 -2
index 918d461fcf4a..b04f12079efd 100644
@@ -1763,6 +1763,7 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
 {
        unsigned long flags;
        struct etr_buf *sysfs_buf = NULL;
+       int rc = 0;
 
        /* config types are set a boot time and never change */
        if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
@@ -1777,7 +1778,11 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
                 * buffer. Since the tracer is still enabled drvdata::buf can't
                 * be NULL.
                 */
-               __tmc_etr_enable_hw(drvdata);
+               rc = __tmc_etr_enable_hw(drvdata);
+               if (rc) {
+                       spin_unlock_irqrestore(&drvdata->spinlock, flags);
+                       return rc;
+               }
        } else {
                /*
                 * The ETR is not tracing and the buffer was just read.
--

2.39.1.581.gbfd45094c4-goog

maintainer
#17 lore raw

Re: [PATCH] coresight: tmc-etr: Handle enable failure in tmc_read_unprepare_etr

Quoted 8 lines · Yabin Cui

Ping for review?

Quoted 5 lines · Yabin Cui

It's similar to what we did in tmc_read_unprepare_etb.

---

Thanks Yabin for the patch, will queue this at rc1

Suzuki

Quoted 31 lines
Quoted 31 lines
  drivers/hwtracing/coresight/coresight-tmc-etr.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +6 -2
index 918d461fcf4a..b04f12079efd 100644
@@ -1763,6 +1763,7 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
  {
         unsigned long flags;
         struct etr_buf *sysfs_buf = NULL;
+       int rc = 0;
 
         /* config types are set a boot time and never change */
         if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETR))
@@ -1777,7 +1778,11 @@ int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata)
                  * buffer. Since the tracer is still enabled drvdata::buf can't
                  * be NULL.
                  */
-               __tmc_etr_enable_hw(drvdata);
+               rc = __tmc_etr_enable_hw(drvdata);
+               if (rc) {
+                       spin_unlock_irqrestore(&drvdata->spinlock, flags);
+                       return rc;
+               }
         } else {
                 /*
                  * The ETR is not tracing and the buffer was just read.
--

2.39.1.581.gbfd45094c4-goog