* [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling
@ 2025-04-29 23:12 Yabin Cui
2025-04-29 23:12 ` [PATCH v5 1/2] " Yabin Cui
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Yabin Cui @ 2025-04-29 23:12 UTC (permalink / raw)
To: Suzuki K Poulose, Mike Leach, James Clark, Leo Yan, Jie Gan,
Alexander Shishkin
Cc: coresight, linux-arm-kernel, linux-kernel, Yabin Cui
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(-)
--
2.49.0.967.g6a0df3ecc3-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v5 1/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling
2025-04-29 23:12 [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling Yabin Cui
@ 2025-04-29 23:12 ` Yabin Cui
2025-04-30 13:45 ` Suzuki K Poulose
2025-04-29 23:13 ` [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable Yabin Cui
2025-04-30 13:55 ` [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling Suzuki K Poulose
2 siblings, 1 reply; 7+ messages in thread
From: Yabin Cui @ 2025-04-29 23:12 UTC (permalink / raw)
To: Suzuki K Poulose, Mike Leach, James Clark, Leo Yan, Jie Gan,
Alexander Shishkin
Cc: coresight, linux-arm-kernel, linux-kernel, 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.
Signed-off-by: Yabin Cui <yabinc@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Reviewed-by: Leo Yan <leo.yan@arm.com>
---
drivers/hwtracing/coresight/coresight-catu.c | 25 +++++++++++++-------
drivers/hwtracing/coresight/coresight-catu.h | 1 +
2 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-catu.c b/drivers/hwtracing/coresight/coresight-catu.c
index 96cb48b140af..d4e2e175e077 100644
--- a/drivers/hwtracing/coresight/coresight-catu.c
+++ b/drivers/hwtracing/coresight/coresight-catu.c
@@ -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;
diff --git a/drivers/hwtracing/coresight/coresight-catu.h b/drivers/hwtracing/coresight/coresight-catu.h
index 141feac1c14b..755776cd19c5 100644
--- a/drivers/hwtracing/coresight/coresight-catu.h
+++ b/drivers/hwtracing/coresight/coresight-catu.h
@@ -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) \
--
2.49.0.967.g6a0df3ecc3-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable
2025-04-29 23:12 [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling Yabin Cui
2025-04-29 23:12 ` [PATCH v5 1/2] " Yabin Cui
@ 2025-04-29 23:13 ` Yabin Cui
2025-04-30 12:57 ` Leo Yan
2025-04-30 13:45 ` Suzuki K Poulose
2025-04-30 13:55 ` [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling Suzuki K Poulose
2 siblings, 2 replies; 7+ messages in thread
From: Yabin Cui @ 2025-04-29 23:13 UTC (permalink / raw)
To: Suzuki K Poulose, Mike Leach, James Clark, Leo Yan, Jie Gan,
Alexander Shishkin
Cc: coresight, linux-arm-kernel, linux-kernel, Yabin Cui
When enabling a SINK or LINK type coresight device fails, the
associated helpers should be disabled.
Signed-off-by: Yabin Cui <yabinc@google.com>
Suggested-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Reviewed-by: Mike Leach <mike.leach@linaro.org>
---
drivers/hwtracing/coresight/coresight-core.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtracing/coresight/coresight-core.c
index dabec7073aed..d3523f0262af 100644
--- a/drivers/hwtracing/coresight/coresight-core.c
+++ b/drivers/hwtracing/coresight/coresight-core.c
@@ -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;
}
--
2.49.0.967.g6a0df3ecc3-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable
2025-04-29 23:13 ` [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable Yabin Cui
@ 2025-04-30 12:57 ` Leo Yan
2025-04-30 13:45 ` Suzuki K Poulose
1 sibling, 0 replies; 7+ messages in thread
From: Leo Yan @ 2025-04-30 12:57 UTC (permalink / raw)
To: Yabin Cui
Cc: Suzuki K Poulose, Mike Leach, James Clark, Jie Gan,
Alexander Shishkin, coresight, linux-arm-kernel, linux-kernel
On Tue, Apr 29, 2025 at 04:13:00PM -0700, Yabin Cui wrote:
> When enabling a SINK or LINK type coresight device fails, the
> associated helpers should be disabled.
>
> Signed-off-by: Yabin Cui <yabinc@google.com>
> Suggested-by: Suzuki K Poulose <suzuki.poulose@arm.com>
> Reviewed-by: James Clark <james.clark@linaro.org>
> Reviewed-by: Mike Leach <mike.leach@linaro.org>
Reviewed-by: Leo Yan <leo.yan@arm.com>
> ---
> drivers/hwtracing/coresight/coresight-core.c | 15 ++++++++++-----
> 1 file changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtracing/coresight/coresight-core.c
> index dabec7073aed..d3523f0262af 100644
> --- a/drivers/hwtracing/coresight/coresight-core.c
> +++ b/drivers/hwtracing/coresight/coresight-core.c
> @@ -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;
> }
> --
> 2.49.0.967.g6a0df3ecc3-goog
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5 1/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling
2025-04-29 23:12 ` [PATCH v5 1/2] " Yabin Cui
@ 2025-04-30 13:45 ` Suzuki K Poulose
0 siblings, 0 replies; 7+ messages in thread
From: Suzuki K Poulose @ 2025-04-30 13:45 UTC (permalink / raw)
To: Yabin Cui, Mike Leach, James Clark, Leo Yan, Jie Gan,
Alexander Shishkin
Cc: coresight, linux-arm-kernel, linux-kernel
On 30/04/2025 00:12, Yabin Cui wrote:
> 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.
>
> Signed-off-by: Yabin Cui <yabinc@google.com>
> Reviewed-by: James Clark <james.clark@linaro.org>
> Reviewed-by: Leo Yan <leo.yan@arm.com>
Looks good to me, I will add :
Fixes: fcacb5c154ba ("coresight: Introduce support for Coresight Address
Translation Unit")
Suzuki
> ---
> drivers/hwtracing/coresight/coresight-catu.c | 25 +++++++++++++-------
> drivers/hwtracing/coresight/coresight-catu.h | 1 +
> 2 files changed, 18 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/hwtracing/coresight/coresight-catu.c b/drivers/hwtracing/coresight/coresight-catu.c
> index 96cb48b140af..d4e2e175e077 100644
> --- a/drivers/hwtracing/coresight/coresight-catu.c
> +++ b/drivers/hwtracing/coresight/coresight-catu.c
> @@ -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;
> diff --git a/drivers/hwtracing/coresight/coresight-catu.h b/drivers/hwtracing/coresight/coresight-catu.h
> index 141feac1c14b..755776cd19c5 100644
> --- a/drivers/hwtracing/coresight/coresight-catu.h
> +++ b/drivers/hwtracing/coresight/coresight-catu.h
> @@ -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) \
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable
2025-04-29 23:13 ` [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable Yabin Cui
2025-04-30 12:57 ` Leo Yan
@ 2025-04-30 13:45 ` Suzuki K Poulose
1 sibling, 0 replies; 7+ messages in thread
From: Suzuki K Poulose @ 2025-04-30 13:45 UTC (permalink / raw)
To: Yabin Cui, Mike Leach, James Clark, Leo Yan, Jie Gan,
Alexander Shishkin
Cc: coresight, linux-arm-kernel, linux-kernel
On 30/04/2025 00:13, Yabin Cui wrote:
> When enabling a SINK or LINK type coresight device fails, the
> associated helpers should be disabled.
>
> Signed-off-by: Yabin Cui <yabinc@google.com>
> Suggested-by: Suzuki K Poulose <suzuki.poulose@arm.com>
> Reviewed-by: James Clark <james.clark@linaro.org>
> Reviewed-by: Mike Leach <mike.leach@linaro.org>
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
> ---
> drivers/hwtracing/coresight/coresight-core.c | 15 ++++++++++-----
> 1 file changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtracing/coresight/coresight-core.c
> index dabec7073aed..d3523f0262af 100644
> --- a/drivers/hwtracing/coresight/coresight-core.c
> +++ b/drivers/hwtracing/coresight/coresight-core.c
> @@ -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;
> }
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling
2025-04-29 23:12 [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling Yabin Cui
2025-04-29 23:12 ` [PATCH v5 1/2] " Yabin Cui
2025-04-29 23:13 ` [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable Yabin Cui
@ 2025-04-30 13:55 ` Suzuki K Poulose
2 siblings, 0 replies; 7+ messages in thread
From: Suzuki K Poulose @ 2025-04-30 13:55 UTC (permalink / raw)
To: Mike Leach, James Clark, Leo Yan, Jie Gan, Alexander Shishkin,
Yabin Cui
Cc: Suzuki K Poulose, coresight, linux-arm-kernel, linux-kernel
On Tue, 29 Apr 2025 16:12:58 -0700, Yabin Cui wrote:
> 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,
--
Suzuki K Poulose <suzuki.poulose@arm.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-04-30 15:34 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-29 23:12 [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling Yabin Cui
2025-04-29 23:12 ` [PATCH v5 1/2] " Yabin Cui
2025-04-30 13:45 ` Suzuki K Poulose
2025-04-29 23:13 ` [PATCH v5 2/2] coresight: core: Disable helpers for devices that fail to enable Yabin Cui
2025-04-30 12:57 ` Leo Yan
2025-04-30 13:45 ` Suzuki K Poulose
2025-04-30 13:55 ` [PATCH v5 0/2] coresight: catu: Introduce refcount and spinlock for enabling/disabling Suzuki K Poulose
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).