Skip to contentSkip to conversation

coresight: catu: Introduce refcount and spinlock for enabling/disabling

PATCH Applied Yabin Cui 3 participants 7 messages Apr 29, 2025 – Apr 30, 2025

Subsystems (2)
lore
Share & tools
lore

Yabin Cui

author
#0 lore raw

v5 cover coresight: catu: Introduce refcount and spinlock for enabling/disabling

Hi Coresight maintainers,

When tracing ETM data on multiple CPUs concurrently via the perf interface, the CATU device is shared across different CPU paths. This can lead to race conditions when multiple CPUs attempt to enable or disable the CATU device simultaneously. This patchset is to fix race conditions when enabling/disabling a CATU device.

Changes since v4:
- Collect Review-by tags.
- return -EINVAL for unknown types in coresight_enable_path.

Changes since v3:
- Add newlines between variable definition and guard().
- Add path parameter when calling coresight_disable_helpers.
- Use "goto err_disable_helpers" in coresight_enable_path().

Changes since v2:
- In catu_disable(), return 0 when refcnt > 0.
- Remove the patch checking enabled mode.
- Disable helpers at the places where a coresight device fails to enable.

Changes since v1:
- Use raw_spinlock_t and guard().
- Add a patch to check enabled mode.
- Add a patch to disable helpers when fails to enable a device.

Yabin Cui (2):
coresight: catu: Introduce refcount and spinlock for
enabling/disabling
coresight: core: Disable helpers for devices that fail to enable

 drivers/hwtracing/coresight/coresight-catu.c | 25 +++++++++++++-------
 drivers/hwtracing/coresight/coresight-catu.h |  1 +
 drivers/hwtracing/coresight/coresight-core.c | 11 ++++++---
 3 files changed, 26 insertions(+), 11 deletions(-)
Signature · 1 line
2.49.0.967.g6a0df3ecc3-goog
author
#1 lore raw

PATCH v5 1/2 coresight: catu: Introduce refcount and spinlock for enabling/disabling

When tracing ETM data on multiple CPUs concurrently via the perf interface, the CATU device is shared across different CPU paths. This can lead to race conditions when multiple CPUs attempt to enable or disable the CATU device simultaneously.

To address these race conditions, this patch introduces the following changes:

1. The enable and disable operations for the CATU device are not reentrant. Therefore, a spinlock is added to ensure that only one CPU can enable or disable a given CATU device at any point in time.

2. A reference counter is used to manage the enable/disable state of the CATU device. The device is enabled when the first CPU requires it and is only disabled when the last CPU finishes using it. This ensures the device remains active as long as at least one CPU needs it.

---
 drivers/hwtracing/coresight/coresight-catu.c | 25 +++++++++++++-------
 drivers/hwtracing/coresight/coresight-catu.h |  1 +
 2 files changed, 18 insertions(+), 8 deletions(-)
drivers/hwtracing/coresight/coresight-catu.c +17 -8
index 96cb48b140af..d4e2e175e077 100644
@@ -458,12 +458,17 @@ static int catu_enable_hw(struct catu_drvdata *drvdata, enum cs_mode cs_mode,
 static int catu_enable(struct coresight_device *csdev, enum cs_mode mode,
 		       void *data)
 {
-	int rc;
+	int rc = 0;
 	struct catu_drvdata *catu_drvdata = csdev_to_catu_drvdata(csdev);
 
-	CS_UNLOCK(catu_drvdata->base);
-	rc = catu_enable_hw(catu_drvdata, mode, data);
-	CS_LOCK(catu_drvdata->base);
+	guard(raw_spinlock_irqsave)(&catu_drvdata->spinlock);
+	if (csdev->refcnt == 0) {
+		CS_UNLOCK(catu_drvdata->base);
+		rc = catu_enable_hw(catu_drvdata, mode, data);
+		CS_LOCK(catu_drvdata->base);
+	}
+	if (!rc)
+		csdev->refcnt++;
 	return rc;
 }
 
@@ -486,12 +491,15 @@ static int catu_disable_hw(struct catu_drvdata *drvdata)
 
 static int catu_disable(struct coresight_device *csdev, void *__unused)
 {
-	int rc;
+	int rc = 0;
 	struct catu_drvdata *catu_drvdata = csdev_to_catu_drvdata(csdev);
 
-	CS_UNLOCK(catu_drvdata->base);
-	rc = catu_disable_hw(catu_drvdata);
-	CS_LOCK(catu_drvdata->base);
+	guard(raw_spinlock_irqsave)(&catu_drvdata->spinlock);
+	if (--csdev->refcnt == 0) {
+		CS_UNLOCK(catu_drvdata->base);
+		rc = catu_disable_hw(catu_drvdata);
+		CS_LOCK(catu_drvdata->base);
+	}
 	return rc;
 }
 
@@ -550,6 +558,7 @@ static int __catu_probe(struct device *dev, struct resource *res)
 	dev->platform_data = pdata;
 
 	drvdata->base = base;
+	raw_spin_lock_init(&drvdata->spinlock);
 	catu_desc.access = CSDEV_ACCESS_IOMEM(base);
 	catu_desc.pdata = pdata;
 	catu_desc.dev = dev;
drivers/hwtracing/coresight/coresight-catu.h +1 -0
index 141feac1c14b..755776cd19c5 100644
@@ -65,6 +65,7 @@ struct catu_drvdata {
 	void __iomem *base;
 	struct coresight_device *csdev;
 	int irq;
+	raw_spinlock_t spinlock;
 };
 
 #define CATU_REG32(name, offset)					\
Signature · 1 line
2.49.0.967.g6a0df3ecc3-goog
maintainer
#2 lore raw
Quoted 19 lines · Yabin Cui

When tracing ETM data on multiple CPUs concurrently via the perf interface, the CATU device is shared across different CPU paths. This can lead to race conditions when multiple CPUs attempt to enable or disable the CATU device simultaneously.

To address these race conditions, this patch introduces the following changes:

1. The enable and disable operations for the CATU device are not reentrant. Therefore, a spinlock is added to ensure that only one CPU can enable or disable a given CATU device at any point in time.

2. A reference counter is used to manage the enable/disable state of the CATU device. The device is enabled when the first CPU requires it and is only disabled when the last CPU finishes using it. This ensures the device remains active as long as at least one CPU needs it.

Quoted 3 lines

Looks good to me, I will add :

Fixes fcacb5c154ba ("coresight: Introduce support for Coresight Address Translation Unit")

Suzuki

Quoted 71 lines
---
  drivers/hwtracing/coresight/coresight-catu.c | 25 +++++++++++++-------
  drivers/hwtracing/coresight/coresight-catu.h |  1 +
  2 files changed, 18 insertions(+), 8 deletions(-)
drivers/hwtracing/coresight/coresight-catu.c +17 -8
index 96cb48b140af..d4e2e175e077 100644
@@ -458,12 +458,17 @@ static int catu_enable_hw(struct catu_drvdata *drvdata, enum cs_mode cs_mode,
  static int catu_enable(struct coresight_device *csdev, enum cs_mode mode,
  		       void *data)
  {
-	int rc;
+	int rc = 0;
  	struct catu_drvdata *catu_drvdata = csdev_to_catu_drvdata(csdev);
 
-	CS_UNLOCK(catu_drvdata->base);
-	rc = catu_enable_hw(catu_drvdata, mode, data);
-	CS_LOCK(catu_drvdata->base);
+	guard(raw_spinlock_irqsave)(&catu_drvdata->spinlock);
+	if (csdev->refcnt == 0) {
+		CS_UNLOCK(catu_drvdata->base);
+		rc = catu_enable_hw(catu_drvdata, mode, data);
+		CS_LOCK(catu_drvdata->base);
+	}
+	if (!rc)
+		csdev->refcnt++;
  	return rc;
  }
 
@@ -486,12 +491,15 @@ static int catu_disable_hw(struct catu_drvdata *drvdata)
 
  static int catu_disable(struct coresight_device *csdev, void *__unused)
  {
-	int rc;
+	int rc = 0;
  	struct catu_drvdata *catu_drvdata = csdev_to_catu_drvdata(csdev);
 
-	CS_UNLOCK(catu_drvdata->base);
-	rc = catu_disable_hw(catu_drvdata);
-	CS_LOCK(catu_drvdata->base);
+	guard(raw_spinlock_irqsave)(&catu_drvdata->spinlock);
+	if (--csdev->refcnt == 0) {
+		CS_UNLOCK(catu_drvdata->base);
+		rc = catu_disable_hw(catu_drvdata);
+		CS_LOCK(catu_drvdata->base);
+	}
  	return rc;
  }
 
@@ -550,6 +558,7 @@ static int __catu_probe(struct device *dev, struct resource *res)
  	dev->platform_data = pdata;
 
  	drvdata->base = base;
+	raw_spin_lock_init(&drvdata->spinlock);
  	catu_desc.access = CSDEV_ACCESS_IOMEM(base);
  	catu_desc.pdata = pdata;
  	catu_desc.dev = dev;
drivers/hwtracing/coresight/coresight-catu.h +1 -0
index 141feac1c14b..755776cd19c5 100644
@@ -65,6 +65,7 @@ struct catu_drvdata {
  	void __iomem *base;
  	struct coresight_device *csdev;
  	int irq;
+	raw_spinlock_t spinlock;
  };
 
  #define CATU_REG32(name, offset)					\
author
#3 lore raw

PATCH v5 2/2 coresight: core: Disable helpers for devices that fail to enable

In reply to Yabin Cui

When enabling a SINK or LINK type coresight device fails, the associated helpers should be disabled.

---
 drivers/hwtracing/coresight/coresight-core.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +10 -5
index dabec7073aed..d3523f0262af 100644
@@ -465,7 +465,7 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
 		/* Enable all helpers adjacent to the path first */
 		ret = coresight_enable_helpers(csdev, mode, path);
 		if (ret)
-			goto err;
+			goto err_disable_path;
 		/*
 		 * ETF devices are tricky... They can be a link or a sink,
 		 * depending on how they are configured.  If an ETF has been
@@ -486,8 +486,10 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
 			 * that need disabling. Disabling the path here
 			 * would mean we could disrupt an existing session.
 			 */
-			if (ret)
+			if (ret) {
+				coresight_disable_helpers(csdev, path);
 				goto out;
+			}
 			break;
 		case CORESIGHT_DEV_TYPE_SOURCE:
 			/* sources are enabled from either sysFS or Perf */
@@ -497,16 +499,19 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
 			child = list_next_entry(nd, link)->csdev;
 			ret = coresight_enable_link(csdev, parent, child, source);
 			if (ret)
-				goto err;
+				goto err_disable_helpers;
 			break;
 		default:
-			goto err;
+			ret = -EINVAL;
+			goto err_disable_helpers;
 		}
 	}
 
 out:
 	return ret;
-err:
+err_disable_helpers:
+	coresight_disable_helpers(csdev, path);
+err_disable_path:
 	coresight_disable_path_from(path, nd);
 	goto out;
 }
Signature · 1 line
2.49.0.967.g6a0df3ecc3-goog
reviewer Reviewed-by on v5 2/2
#4 lore raw

Re: [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable

Quoted 7 lines · Yabin Cui

When enabling a SINK or LINK type coresight device fails, the associated helpers should be disabled.

Quoted 55 lines
---
 drivers/hwtracing/coresight/coresight-core.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +10 -5
index dabec7073aed..d3523f0262af 100644
@@ -465,7 +465,7 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
 		/* Enable all helpers adjacent to the path first */
 		ret = coresight_enable_helpers(csdev, mode, path);
 		if (ret)
-			goto err;
+			goto err_disable_path;
 		/*
 		 * ETF devices are tricky... They can be a link or a sink,
 		 * depending on how they are configured.  If an ETF has been
@@ -486,8 +486,10 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
 			 * that need disabling. Disabling the path here
 			 * would mean we could disrupt an existing session.
 			 */
-			if (ret)
+			if (ret) {
+				coresight_disable_helpers(csdev, path);
 				goto out;
+			}
 			break;
 		case CORESIGHT_DEV_TYPE_SOURCE:
 			/* sources are enabled from either sysFS or Perf */
@@ -497,16 +499,19 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
 			child = list_next_entry(nd, link)->csdev;
 			ret = coresight_enable_link(csdev, parent, child, source);
 			if (ret)
-				goto err;
+				goto err_disable_helpers;
 			break;
 		default:
-			goto err;
+			ret = -EINVAL;
+			goto err_disable_helpers;
 		}
 	}
 
 out:
 	return ret;
-err:
+err_disable_helpers:
+	coresight_disable_helpers(csdev, path);
+err_disable_path:
 	coresight_disable_path_from(path, nd);
 	goto out;
 }
Signature · 1 line
2.49.0.967.g6a0df3ecc3-goog
maintainer
#5 lore raw

Re: [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable

In reply to Yabin Cui
Quoted 7 lines · Yabin Cui

When enabling a SINK or LINK type coresight device fails, the associated helpers should be disabled.

Again, this should have :

Fixes 6148652807ba ("coresight: Enable and disable helper devices adjacent to the path")

I have added it locally

Rest looks good to me

Suzuki

Quoted 52 lines
---
  drivers/hwtracing/coresight/coresight-core.c | 15 ++++++++++-----
  1 file changed, 10 insertions(+), 5 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +10 -5
index dabec7073aed..d3523f0262af 100644
@@ -465,7 +465,7 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
  		/* Enable all helpers adjacent to the path first */
  		ret = coresight_enable_helpers(csdev, mode, path);
  		if (ret)
-			goto err;
+			goto err_disable_path;
  		/*
  		 * ETF devices are tricky... They can be a link or a sink,
  		 * depending on how they are configured.  If an ETF has been
@@ -486,8 +486,10 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
  			 * that need disabling. Disabling the path here
  			 * would mean we could disrupt an existing session.
  			 */
-			if (ret)
+			if (ret) {
+				coresight_disable_helpers(csdev, path);
  				goto out;
+			}
  			break;
  		case CORESIGHT_DEV_TYPE_SOURCE:
  			/* sources are enabled from either sysFS or Perf */
@@ -497,16 +499,19 @@ int coresight_enable_path(struct coresight_path *path, enum cs_mode mode,
  			child = list_next_entry(nd, link)->csdev;
  			ret = coresight_enable_link(csdev, parent, child, source);
  			if (ret)
-				goto err;
+				goto err_disable_helpers;
  			break;
  		default:
-			goto err;
+			ret = -EINVAL;
+			goto err_disable_helpers;
  		}
  	}
 
  out:
  	return ret;
-err:
+err_disable_helpers:
+	coresight_disable_helpers(csdev, path);
+err_disable_path:
  	coresight_disable_path_from(path, nd);
  	goto out;
  }
maintainer Applied
#6 lore raw
In reply to Yabin Cui
Quoted 11 lines · Yabin Cui

When tracing ETM data on multiple CPUs concurrently via the perf interface, the CATU device is shared across different CPU paths. This can lead to race conditions when multiple CPUs attempt to enable or disable the CATU device simultaneously. This patchset is to fix race conditions when enabling/disabling a CATU device.

Changes since v4:
- Collect Review-by tags.
- return -EINVAL for unknown types in coresight_enable_path.

[...]

Applied, thanks!

[1/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling

      https://git.kernel.org/coresight/c/a03a0a08
[2/2] coresight: core: Disable helpers for devices that fail to enable
      https://git.kernel.org/coresight/c/f6028eee

Best regards,

Signature · 1 line
Suzuki K Poulose <[email protected]>