Skip to contentSkip to conversation

Coresight TMC-ETR some bugfixes and cleanups

PATCH In review Junhao He 5 participants 9 messages Aug 18, 2025 – Sep 19, 2025

Subsystems (2)
lore
Share & tools
lore

Junhao He

author
#0 lore raw

v3 cover Coresight TMC-ETR some bugfixes and cleanups

This patchset builds upon Yicong's previous patches [1].

Introducing fix two race issues found by using TMC-ETR and CATU, Two cleanups found when debugging the issues.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/

---
Changes in v3:
- Patches 1: Additional comment for tmc_drvdata::etr_mode. Update comment for tmc_drvdata::reading with Jonathan's Tag.
- Patches 2: Replace scoped_guard with guard with Jonathan's Tag.
- Patches 2: Fix spinlock to raw_spinlock, and refactor this code based on Leo's suggested solution.
- Patches 3: change the size's type to ssize_t and use max_t to simplify the code with Leo's Tag.

Changes in v2:
- Updated the commit of patch2.
- Rebase to v6.16-rc1

Junhao He (1):
coresight: tmc: refactor the tmc-etr mode setting to avoid race
conditions

Yicong Yang (2):
coresight: tmc: Add missing doc including reading and etr_mode of
struct tmc_drvdata
coresight: tmc: Decouple the perf buffer allocation from sysfs mode

 .../hwtracing/coresight/coresight-tmc-etr.c   | 110 ++++++++----------
 drivers/hwtracing/coresight/coresight-tmc.h   |   2 +
 2 files changed, 53 insertions(+), 59 deletions(-)
Signature · 1 line
2.33.0
author
#1 lore raw

PATCH v3 1/3 coresight: tmc: Add missing doc including reading and etr_mode of struct tmc_drvdata

From: Yicong Yang <[email protected]>

tmc_drvdata::reading is used to indicate whether a reading process is performed through /dev/xyz.tmc.
tmc_drvdata::etr_mode is used to store the Coresight TMC-ETR buffer mode selected by the user.
Document them.

---
 drivers/hwtracing/coresight/coresight-tmc.h | 2 ++
 1 file changed, 2 insertions(+)
drivers/hwtracing/coresight/coresight-tmc.h +2 -0
index 6541a27a018e..9daa2680cfb6 100644
@@ -220,6 +220,7 @@ struct tmc_resrv_buf {
  * @pid:	Process ID of the process that owns the session that is using
  *		this component. For example this would be the pid of the Perf
  *		process.
+ * @reading:	buffer's in the reading through "/dev/xyz.tmc" entry
  * @stop_on_flush: Stop on flush trigger user configuration.
  * @buf:	Snapshot of the trace data for ETF/ETB.
  * @etr_buf:	details of buffer used in TMC-ETR
@@ -232,6 +233,7 @@ struct tmc_resrv_buf {
  * @trigger_cntr: amount of words to store after a trigger.
  * @etr_caps:	Bitmask of capabilities of the TMC ETR, inferred from the
  *		device configuration register (DEVID)
+ * @etr_mode:	User preferred mode of the ETR device, default auto mode.
  * @idr:	Holds etr_bufs allocated for this ETR.
  * @idr_mutex:	Access serialisation for idr.
  * @sysfs_buf:	SYSFS buffer for ETR.
Signature · 1 line
2.33.0
reviewer Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] coresight: tmc: Add missing doc including reading and etr_mode of struct tmc_drvdata

Quoted 11 lines · Junhao He

From: Yicong Yang <[email protected]>

tmc_drvdata::reading is used to indicate whether a reading process is performed through /dev/xyz.tmc.
tmc_drvdata::etr_mode is used to store the Coresight TMC-ETR buffer mode selected by the user.
Document them.

Quoted 27 lines
---
 drivers/hwtracing/coresight/coresight-tmc.h | 2 ++
 1 file changed, 2 insertions(+)
drivers/hwtracing/coresight/coresight-tmc.h +2 -0
index 6541a27a018e..9daa2680cfb6 100644
@@ -220,6 +220,7 @@ struct tmc_resrv_buf {
  * @pid:	Process ID of the process that owns the session that is using
  *		this component. For example this would be the pid of the Perf
  *		process.
+ * @reading:	buffer's in the reading through "/dev/xyz.tmc" entry
  * @stop_on_flush: Stop on flush trigger user configuration.
  * @buf:	Snapshot of the trace data for ETF/ETB.
  * @etr_buf:	details of buffer used in TMC-ETR
@@ -232,6 +233,7 @@ struct tmc_resrv_buf {
  * @trigger_cntr: amount of words to store after a trigger.
  * @etr_caps:	Bitmask of capabilities of the TMC ETR, inferred from the
  *		device configuration register (DEVID)
+ * @etr_mode:	User preferred mode of the ETR device, default auto mode.
  * @idr:	Holds etr_bufs allocated for this ETR.
  * @idr_mutex:	Access serialisation for idr.
  * @sysfs_buf:	SYSFS buffer for ETR.
Signature · 1 line
2.33.0
author
#3 lore raw

PATCH v3 2/3 coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions

In reply to Junhao He

When trying to run perf and sysfs mode simultaneously, the WARN_ON() in tmc_etr_enable_hw() is triggered sometimes:

 WARNING: CPU: 42 PID: 3911571 at drivers/hwtracing/coresight/coresight-tmc-etr.c:1060 tmc_etr_enable_hw+0xc0/0xd8 [coresight_tmc]
 [..snip..]
 Call trace:
  tmc_etr_enable_hw+0xc0/0xd8 [coresight_tmc] (P)
  tmc_enable_etr_sink+0x11c/0x250 [coresight_tmc] (L)
  tmc_enable_etr_sink+0x11c/0x250 [coresight_tmc]
  coresight_enable_path+0x1c8/0x218 [coresight]
  coresight_enable_sysfs+0xa4/0x228 [coresight]
  enable_source_store+0x58/0xa8 [coresight]
  dev_attr_store+0x20/0x40
  sysfs_kf_write+0x4c/0x68
  kernfs_fop_write_iter+0x120/0x1b8
  vfs_write+0x2c8/0x388
  ksys_write+0x74/0x108
  __arm64_sys_write+0x24/0x38
  el0_svc_common.constprop.0+0x64/0x148
  do_el0_svc+0x24/0x38
  el0_svc+0x3c/0x130
  el0t_64_sync_handler+0xc8/0xd0
  el0t_64_sync+0x1ac/0x1b0
 ---[ end trace 0000000000000000 ]---

Since the sysfs buffer allocation and the hardware enablement is not in the same critical region, it's possible to race with the perf

mode:
[sysfs mode] [perf mode]
tmc_etr_get_sysfs_buffer()

    spin_lock(&drvdata->spinlock)
    [sysfs buffer allocation]
    spin_unlock(&drvdata->spinlock)
                                 spin_lock(&drvdata->spinlock)
                                 tmc_etr_enable_hw()
                                   drvdata->etr_buf = etr_perf->etr_buf
                                 spin_unlock(&drvdata->spinlock)

spin_lock(&drvdata->spinlock)
tmc_etr_enable_hw()
WARN_ON(drvdata->etr_buf) // WARN sicne etr_buf initialized at
the perf side
spin_unlock(&drvdata->spinlock)

A race condition is introduced here, perf always prioritizes execution, and warnings can lead to concerns about potential hidden bugs, such as getting out of sync.

To fix this, configure the tmc-etr mode before invoking enable_etr_perf() or enable_etr_sysfs(), explicitly check if the tmc-etr sink is already enabled in a different mode, and simplily the setup and checks for "mode". To prevent race conditions between mode transitions.

Fixes 296b01fd106e ("coresight: Refactor out buffer allocation function for ETR")
---
 .../hwtracing/coresight/coresight-tmc-etr.c   | 80 ++++++++++---------
 1 file changed, 42 insertions(+), 38 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +42 -38
index b07fcdb3fe1a..06c74717be19 100644
@@ -1263,7 +1263,7 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
 		raw_spin_lock_irqsave(&drvdata->spinlock, flags);
 	}
 
-	if (drvdata->reading || coresight_get_mode(csdev) == CS_MODE_PERF) {
+	if (drvdata->reading) {
 		ret = -EBUSY;
 		goto out;
 	}
@@ -1300,20 +1300,18 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
 	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
 
 	/*
-	 * In sysFS mode we can have multiple writers per sink.  Since this
-	 * sink is already enabled no memory is needed and the HW need not be
-	 * touched, even if the buffer size has changed.
+	 * When two sysfs sessions race to acquire an idle sink, both may enter
+	 * this function. We need to recheck if the sink is already in use to
+	 * prevent duplicate hardware configuration.
 	 */
-	if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
+	if (csdev->refcnt) {
 		csdev->refcnt++;
 		goto out;
 	}
 
 	ret = tmc_etr_enable_hw(drvdata, sysfs_buf);
-	if (!ret) {
-		coresight_set_mode(csdev, CS_MODE_SYSFS);
+	if (!ret)
 		csdev->refcnt++;
-	}
 
 out:
 	raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
@@ -1729,39 +1727,24 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
 {
 	int rc = 0;
 	pid_t pid;
-	unsigned long flags;
 	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
 	struct perf_output_handle *handle = data;
 	struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
 
-	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
-	 /* Don't use this sink if it is already claimed by sysFS */
-	if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
-		rc = -EBUSY;
-		goto unlock_out;
-	}
-
-	if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
-		rc = -EINVAL;
-		goto unlock_out;
-	}
+	if (WARN_ON(!etr_perf || !etr_perf->etr_buf))
+		return -EINVAL;
 
 	/* Get a handle on the pid of the session owner */
 	pid = etr_perf->pid;
 
 	/* Do not proceed if this device is associated with another session */
-	if (drvdata->pid != -1 && drvdata->pid != pid) {
-		rc = -EBUSY;
-		goto unlock_out;
-	}
+	if (drvdata->pid != -1 && drvdata->pid != pid)
+		return -EBUSY;
 
-	/*
-	 * No HW configuration is needed if the sink is already in
-	 * use for this session.
-	 */
+	/* The sink is already in use for this session */
 	if (drvdata->pid == pid) {
 		csdev->refcnt++;
-		goto unlock_out;
+		return rc;
 	}
 
 	rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
@@ -1773,22 +1756,43 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
 		csdev->refcnt++;
 	}
 
-unlock_out:
-	raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
 	return rc;
 }
 
 static int tmc_enable_etr_sink(struct coresight_device *csdev,
 			       enum cs_mode mode, void *data)
 {
-	switch (mode) {
-	case CS_MODE_SYSFS:
-		return tmc_enable_etr_sink_sysfs(csdev);
-	case CS_MODE_PERF:
-		return tmc_enable_etr_sink_perf(csdev, data);
-	default:
-		return -EINVAL;
+	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+	int rc;
+
+	scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock) {
+		if (coresight_get_mode(csdev) != CS_MODE_DISABLED &&
+		    coresight_get_mode(csdev) != mode)
+			return -EBUSY;
+
+		switch (mode) {
+		case CS_MODE_SYSFS:
+			if (csdev->refcnt) {
+				/* The sink is already enabled */
+				csdev->refcnt++;
+				return 0;
+			}
+			coresight_set_mode(csdev, mode);
+			break;
+		case CS_MODE_PERF:
+			return tmc_enable_etr_sink_perf(csdev, data);
+		default:
+			return -EINVAL;
+		}
+	}
+
+	rc = tmc_enable_etr_sink_sysfs(csdev);
+	if (rc) {
+		scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock)
+		coresight_set_mode(csdev, CS_MODE_DISABLED);
 	}
+
+	return rc;
 }
 
 static int tmc_disable_etr_sink(struct coresight_device *csdev)
Signature · 1 line
2.33.0
Tested-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions

Quoted 53 lines · Junhao He

When trying to run perf and sysfs mode simultaneously, the WARN_ON() in tmc_etr_enable_hw() is triggered sometimes:

 WARNING: CPU: 42 PID: 3911571 at drivers/hwtracing/coresight/coresight-tmc-etr.c:1060 tmc_etr_enable_hw+0xc0/0xd8 [coresight_tmc]
 [..snip..]
 Call trace:
  tmc_etr_enable_hw+0xc0/0xd8 [coresight_tmc] (P)
  tmc_enable_etr_sink+0x11c/0x250 [coresight_tmc] (L)
  tmc_enable_etr_sink+0x11c/0x250 [coresight_tmc]
  coresight_enable_path+0x1c8/0x218 [coresight]
  coresight_enable_sysfs+0xa4/0x228 [coresight]
  enable_source_store+0x58/0xa8 [coresight]
  dev_attr_store+0x20/0x40
  sysfs_kf_write+0x4c/0x68
  kernfs_fop_write_iter+0x120/0x1b8
  vfs_write+0x2c8/0x388
  ksys_write+0x74/0x108
  __arm64_sys_write+0x24/0x38
  el0_svc_common.constprop.0+0x64/0x148
  do_el0_svc+0x24/0x38
  el0_svc+0x3c/0x130
  el0t_64_sync_handler+0xc8/0xd0
  el0t_64_sync+0x1ac/0x1b0
 ---[ end trace 0000000000000000 ]---

Since the sysfs buffer allocation and the hardware enablement is not in the same critical region, it's possible to race with the perf

mode:
[sysfs mode] [perf mode]
tmc_etr_get_sysfs_buffer()

    spin_lock(&drvdata->spinlock)
    [sysfs buffer allocation]
    spin_unlock(&drvdata->spinlock)
                                 spin_lock(&drvdata->spinlock)
                                 tmc_etr_enable_hw()
                                   drvdata->etr_buf = etr_perf->etr_buf
                                 spin_unlock(&drvdata->spinlock)

spin_lock(&drvdata->spinlock)
tmc_etr_enable_hw()
WARN_ON(drvdata->etr_buf) // WARN sicne etr_buf initialized at
the perf side
spin_unlock(&drvdata->spinlock)

A race condition is introduced here, perf always prioritizes execution, and warnings can lead to concerns about potential hidden bugs, such as getting out of sync.

To fix this, configure the tmc-etr mode before invoking enable_etr_perf() or enable_etr_sysfs(), explicitly check if the tmc-etr sink is already enabled in a different mode, and simplily the setup and checks for "mode". To prevent race conditions between mode transitions.

Quoted 4 lines
Fixes 296b01fd106e ("coresight: Refactor out buffer allocation function for ETR")

Tested by running perf and sysfs mode simultaneously, no warning reproduced.

Quoted 143 lines
---
 .../hwtracing/coresight/coresight-tmc-etr.c   | 80 ++++++++++---------
 1 file changed, 42 insertions(+), 38 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +42 -38
index b07fcdb3fe1a..06c74717be19 100644
@@ -1263,7 +1263,7 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
 		raw_spin_lock_irqsave(&drvdata->spinlock, flags);
 	}
 
-	if (drvdata->reading || coresight_get_mode(csdev) == CS_MODE_PERF) {
+	if (drvdata->reading) {
 		ret = -EBUSY;
 		goto out;
 	}
@@ -1300,20 +1300,18 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
 	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
 
 	/*
-	 * In sysFS mode we can have multiple writers per sink.  Since this
-	 * sink is already enabled no memory is needed and the HW need not be
-	 * touched, even if the buffer size has changed.
+	 * When two sysfs sessions race to acquire an idle sink, both may enter
+	 * this function. We need to recheck if the sink is already in use to
+	 * prevent duplicate hardware configuration.
 	 */
-	if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
+	if (csdev->refcnt) {
 		csdev->refcnt++;
 		goto out;
 	}
 
 	ret = tmc_etr_enable_hw(drvdata, sysfs_buf);
-	if (!ret) {
-		coresight_set_mode(csdev, CS_MODE_SYSFS);
+	if (!ret)
 		csdev->refcnt++;
-	}
 
 out:
 	raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
@@ -1729,39 +1727,24 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
 {
 	int rc = 0;
 	pid_t pid;
-	unsigned long flags;
 	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
 	struct perf_output_handle *handle = data;
 	struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
 
-	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
-	 /* Don't use this sink if it is already claimed by sysFS */
-	if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
-		rc = -EBUSY;
-		goto unlock_out;
-	}
-
-	if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
-		rc = -EINVAL;
-		goto unlock_out;
-	}
+	if (WARN_ON(!etr_perf || !etr_perf->etr_buf))
+		return -EINVAL;
 
 	/* Get a handle on the pid of the session owner */
 	pid = etr_perf->pid;
 
 	/* Do not proceed if this device is associated with another session */
-	if (drvdata->pid != -1 && drvdata->pid != pid) {
-		rc = -EBUSY;
-		goto unlock_out;
-	}
+	if (drvdata->pid != -1 && drvdata->pid != pid)
+		return -EBUSY;
 
-	/*
-	 * No HW configuration is needed if the sink is already in
-	 * use for this session.
-	 */
+	/* The sink is already in use for this session */
 	if (drvdata->pid == pid) {
 		csdev->refcnt++;
-		goto unlock_out;
+		return rc;
 	}
 
 	rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
@@ -1773,22 +1756,43 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
 		csdev->refcnt++;
 	}
 
-unlock_out:
-	raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
 	return rc;
 }
 
 static int tmc_enable_etr_sink(struct coresight_device *csdev,
 			       enum cs_mode mode, void *data)
 {
-	switch (mode) {
-	case CS_MODE_SYSFS:
-		return tmc_enable_etr_sink_sysfs(csdev);
-	case CS_MODE_PERF:
-		return tmc_enable_etr_sink_perf(csdev, data);
-	default:
-		return -EINVAL;
+	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+	int rc;
+
+	scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock) {
+		if (coresight_get_mode(csdev) != CS_MODE_DISABLED &&
+		    coresight_get_mode(csdev) != mode)
+			return -EBUSY;
+
+		switch (mode) {
+		case CS_MODE_SYSFS:
+			if (csdev->refcnt) {
+				/* The sink is already enabled */
+				csdev->refcnt++;
+				return 0;
+			}
+			coresight_set_mode(csdev, mode);
+			break;
+		case CS_MODE_PERF:
+			return tmc_enable_etr_sink_perf(csdev, data);
+		default:
+			return -EINVAL;
+		}
+	}
+
+	rc = tmc_enable_etr_sink_sysfs(csdev);
+	if (rc) {
+		scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock)
+		coresight_set_mode(csdev, CS_MODE_DISABLED);
 	}
+
+	return rc;
 }
 
 static int tmc_disable_etr_sink(struct coresight_device *csdev)
maintainer
#5 lore raw

Re: [PATCH v3 2/3] coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions

Quoted 179 lines · Yicong Yang
Quoted 53 lines · Junhao He

When trying to run perf and sysfs mode simultaneously, the WARN_ON() in tmc_etr_enable_hw() is triggered sometimes:

  WARNING: CPU: 42 PID: 3911571 at drivers/hwtracing/coresight/coresight-tmc-etr.c:1060 tmc_etr_enable_hw+0xc0/0xd8 [coresight_tmc]
  [..snip..]
  Call trace:
   tmc_etr_enable_hw+0xc0/0xd8 [coresight_tmc] (P)
   tmc_enable_etr_sink+0x11c/0x250 [coresight_tmc] (L)
   tmc_enable_etr_sink+0x11c/0x250 [coresight_tmc]
   coresight_enable_path+0x1c8/0x218 [coresight]
   coresight_enable_sysfs+0xa4/0x228 [coresight]
   enable_source_store+0x58/0xa8 [coresight]
   dev_attr_store+0x20/0x40
   sysfs_kf_write+0x4c/0x68
   kernfs_fop_write_iter+0x120/0x1b8
   vfs_write+0x2c8/0x388
   ksys_write+0x74/0x108
   __arm64_sys_write+0x24/0x38
   el0_svc_common.constprop.0+0x64/0x148
   do_el0_svc+0x24/0x38
   el0_svc+0x3c/0x130
   el0t_64_sync_handler+0xc8/0xd0
   el0t_64_sync+0x1ac/0x1b0
  ---[ end trace 0000000000000000 ]---

Since the sysfs buffer allocation and the hardware enablement is not in the same critical region, it's possible to race with the perf

mode:

   [sysfs mode]                   [perf mode]
   tmc_etr_get_sysfs_buffer()
     spin_lock(&drvdata->spinlock)
     [sysfs buffer allocation]
     spin_unlock(&drvdata->spinlock)
                                  spin_lock(&drvdata->spinlock)
                                  tmc_etr_enable_hw()
                                    drvdata->etr_buf = etr_perf->etr_buf
                                  spin_unlock(&drvdata->spinlock)

spin_lock(&drvdata->spinlock)
tmc_etr_enable_hw()
WARN_ON(drvdata->etr_buf) // WARN sicne etr_buf initialized at
the perf side
spin_unlock(&drvdata->spinlock)

A race condition is introduced here, perf always prioritizes execution, and warnings can lead to concerns about potential hidden bugs, such as getting out of sync.

To fix this, configure the tmc-etr mode before invoking enable_etr_perf() or enable_etr_sysfs(), explicitly check if the tmc-etr sink is already enabled in a different mode, and simplily the setup and checks for "mode". To prevent race conditions between mode transitions.

Quoted 4 lines
Fixes 296b01fd106e ("coresight: Refactor out buffer allocation function for ETR")

Tested by running perf and sysfs mode simultaneously, no warning reproduced.

Quoted 116 lines
---
  .../hwtracing/coresight/coresight-tmc-etr.c   | 80 ++++++++++---------
  1 file changed, 42 insertions(+), 38 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +20 -38
index b07fcdb3fe1a..06c74717be19 100644
@@ -1263,7 +1263,7 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
  		raw_spin_lock_irqsave(&drvdata->spinlock, flags);
  	}
 
-	if (drvdata->reading || coresight_get_mode(csdev) == CS_MODE_PERF) {
+	if (drvdata->reading) {
  		ret = -EBUSY;
  		goto out;
  	}
@@ -1300,20 +1300,18 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
  	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
 
  	/*
-	 * In sysFS mode we can have multiple writers per sink.  Since this
-	 * sink is already enabled no memory is needed and the HW need not be
-	 * touched, even if the buffer size has changed.
+	 * When two sysfs sessions race to acquire an idle sink, both may enter
+	 * this function. We need to recheck if the sink is already in use to
+	 * prevent duplicate hardware configuration.
  	 */
-	if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
+	if (csdev->refcnt) {
  		csdev->refcnt++;
  		goto out;
  	}
 
  	ret = tmc_etr_enable_hw(drvdata, sysfs_buf);
-	if (!ret) {
-		coresight_set_mode(csdev, CS_MODE_SYSFS);
+	if (!ret)
  		csdev->refcnt++;
-	}
 
  out:
  	raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
@@ -1729,39 +1727,24 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
  {
  	int rc = 0;
  	pid_t pid;
-	unsigned long flags;
  	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  	struct perf_output_handle *handle = data;
  	struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
 
-	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
-	 /* Don't use this sink if it is already claimed by sysFS */
-	if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
-		rc = -EBUSY;
-		goto unlock_out;
-	}
-
-	if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
-		rc = -EINVAL;
-		goto unlock_out;
-	}
+	if (WARN_ON(!etr_perf || !etr_perf->etr_buf))
+		return -EINVAL;
 
  	/* Get a handle on the pid of the session owner */
  	pid = etr_perf->pid;
 
  	/* Do not proceed if this device is associated with another session */
-	if (drvdata->pid != -1 && drvdata->pid != pid) {
-		rc = -EBUSY;
-		goto unlock_out;
-	}
+	if (drvdata->pid != -1 && drvdata->pid != pid)
+		return -EBUSY;
 
-	/*
-	 * No HW configuration is needed if the sink is already in
-	 * use for this session.
-	 */
+	/* The sink is already in use for this session */
  	if (drvdata->pid == pid) {
  		csdev->refcnt++;
-		goto unlock_out;
+		return rc;
  	}
 
  	rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
@@ -1773,22 +1756,43 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)
  		csdev->refcnt++;
  	}
 
-unlock_out:
-	raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
  	return rc;
  }
 
  static int tmc_enable_etr_sink(struct coresight_device *csdev,
  			       enum cs_mode mode, void *data)
  {
-	switch (mode) {
-	case CS_MODE_SYSFS:
-		return tmc_enable_etr_sink_sysfs(csdev);
-	case CS_MODE_PERF:
-		return tmc_enable_etr_sink_perf(csdev, data);
-	default:
-		return -EINVAL;
+	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+	int rc;
+
+	scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock) {
+		if (coresight_get_mode(csdev) != CS_MODE_DISABLED &&
+		    coresight_get_mode(csdev) != mode)
+			return -EBUSY;
+
Quoted 8 lines

drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1780

Quoted 8 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +8 -0
+		switch (mode) {
+		case CS_MODE_SYSFS:
+			if (csdev->refcnt) {
+				/* The sink is already enabled */
+				csdev->refcnt++;
+				return 0;
+			}
+			coresight_set_mode(csdev, mode);

Why are we spilling bits here in the common code for sysfs ? More on this, see below.

Quoted 9 lines

drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1789

Quoted 9 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +9 -0
+			break;
+		case CS_MODE_PERF:
+			return tmc_enable_etr_sink_perf(csdev, data);
+		default:
+			return -EINVAL;
+		}
+	}
+
+	rc = tmc_enable_etr_sink_sysfs(csdev);

We now call the above function without the spinlock and the refcnt is managed with and without the spinlock by the users. This is problematic, with refcnt being a non-atomic type.

Please fix. I don't see why we can't set the mode in tmc_enable_etr_sink_sysfs() with the locks held and reset the mode if we failed enable it properly.

Suzuki

Quoted 10 lines
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +5 -0
+	if (rc) {
+		scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock)
+		coresight_set_mode(csdev, CS_MODE_DISABLED);
  	}
+
+	return rc;
  }
 
  static int tmc_disable_etr_sink(struct coresight_device *csdev)
#6 lore raw

Re: [PATCH v3 2/3] coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions

Hi Suzuki

thanks for the review

Quoted 183 lines · Suzuki K Poulose
Quoted 182 lines · Yicong Yang
Quoted 55 lines · Junhao He

When trying to run perf and sysfs mode simultaneously, the WARN_ON() in tmc_etr_enable_hw() is triggered sometimes:

  WARNING: CPU: 42 PID: 3911571 at 
drivers/hwtracing/coresight/coresight-tmc-etr.c:1060 tmc_etr_enable_hw+0xc0/0xd8 [coresight_tmc]
  [..snip..]
  Call trace:
   tmc_etr_enable_hw+0xc0/0xd8 [coresight_tmc] (P)
   tmc_enable_etr_sink+0x11c/0x250 [coresight_tmc] (L)
   tmc_enable_etr_sink+0x11c/0x250 [coresight_tmc]
   coresight_enable_path+0x1c8/0x218 [coresight]
   coresight_enable_sysfs+0xa4/0x228 [coresight]
   enable_source_store+0x58/0xa8 [coresight]
   dev_attr_store+0x20/0x40
   sysfs_kf_write+0x4c/0x68
   kernfs_fop_write_iter+0x120/0x1b8
   vfs_write+0x2c8/0x388
   ksys_write+0x74/0x108
   __arm64_sys_write+0x24/0x38
   el0_svc_common.constprop.0+0x64/0x148
   do_el0_svc+0x24/0x38
   el0_svc+0x3c/0x130
   el0t_64_sync_handler+0xc8/0xd0
   el0t_64_sync+0x1ac/0x1b0
  ---[ end trace 0000000000000000 ]---

Since the sysfs buffer allocation and the hardware enablement is not in the same critical region, it's possible to race with the perf

mode:
[sysfs mode] [perf mode]
tmc_etr_get_sysfs_buffer()

     spin_lock(&drvdata->spinlock)
     [sysfs buffer allocation]
     spin_unlock(&drvdata->spinlock)
spin_lock(&drvdata->spinlock)
                                  tmc_etr_enable_hw()
                                    drvdata->etr_buf = 

etr_perf->etr_buf
spin_unlock(&drvdata->spinlock)
spin_lock(&drvdata->spinlock)
tmc_etr_enable_hw()
WARN_ON(drvdata->etr_buf) // WARN sicne etr_buf initialized at
the perf side
spin_unlock(&drvdata->spinlock)

A race condition is introduced here, perf always prioritizes execution, and warnings can lead to concerns about potential hidden bugs, such as getting out of sync.

To fix this, configure the tmc-etr mode before invoking enable_etr_perf() or enable_etr_sysfs(), explicitly check if the tmc-etr sink is already enabled in a different mode, and simplily the setup and checks for "mode". To prevent race conditions between mode transitions.

Quoted 4 lines
Fixes 296b01fd106e ("coresight: Refactor out buffer allocation function for ETR")

Tested by running perf and sysfs mode simultaneously, no warning reproduced.

Quoted 117 lines
---
  .../hwtracing/coresight/coresight-tmc-etr.c   | 80 
++++++++++---------
  1 file changed, 42 insertions(+), 38 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -2
index b07fcdb3fe1a..06c74717be19 100644
@@ -1263,7 +1263,7 @@ static struct etr_buf 
 *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
          raw_spin_lock_irqsave(&drvdata->spinlock, flags);
      }
  -    if (drvdata->reading || coresight_get_mode(csdev) == 
 CS_MODE_PERF) {
+    if (drvdata->reading) {
          ret = -EBUSY;
          goto out;
      }
@@ -1300,20 +1300,18 @@ static int tmc_enable_etr_sink_sysfs(struct 
 coresight_device *csdev)
      raw_spin_lock_irqsave(&drvdata->spinlock, flags);
        /*
-     * In sysFS mode we can have multiple writers per sink. Since this
-     * sink is already enabled no memory is needed and the HW need 

not be

drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -1
-     * touched, even if the buffer size has changed.
+     * When two sysfs sessions race to acquire an idle sink, both 

may enter
+ * this function. We need to recheck if the sink is already in
use to

drivers/hwtracing/coresight/coresight-tmc-etr.c +5 -15
+     * prevent duplicate hardware configuration.
       */
-    if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
+    if (csdev->refcnt) {
          csdev->refcnt++;
          goto out;
      }
        ret = tmc_etr_enable_hw(drvdata, sysfs_buf);
-    if (!ret) {
-        coresight_set_mode(csdev, CS_MODE_SYSFS);
+    if (!ret)
          csdev->refcnt++;
-    }
    out:
      raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
@@ -1729,39 +1727,24 @@ static int tmc_enable_etr_sink_perf(struct 
 coresight_device *csdev, void *data)
  {
      int rc = 0;
      pid_t pid;
-    unsigned long flags;
      struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
      struct perf_output_handle *handle = data;
      struct etr_perf_buffer *etr_perf = etm_perf_sink_config(handle);
  -    raw_spin_lock_irqsave(&drvdata->spinlock, flags);
-     /* Don't use this sink if it is already claimed by sysFS */
-    if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
-        rc = -EBUSY;
-        goto unlock_out;
-    }
-
-    if (WARN_ON(!etr_perf || !etr_perf->etr_buf)) {
-        rc = -EINVAL;
-        goto unlock_out;
-    }
+    if (WARN_ON(!etr_perf || !etr_perf->etr_buf))
+        return -EINVAL;
        /* Get a handle on the pid of the session owner */
      pid = etr_perf->pid;
        /* Do not proceed if this device is associated with another 

session */

drivers/hwtracing/coresight/coresight-tmc-etr.c +14 -16
-    if (drvdata->pid != -1 && drvdata->pid != pid) {
-        rc = -EBUSY;
-        goto unlock_out;
-    }
+    if (drvdata->pid != -1 && drvdata->pid != pid)
+        return -EBUSY;
  -    /*
-     * No HW configuration is needed if the sink is already in
-     * use for this session.
-     */
+    /* The sink is already in use for this session */
      if (drvdata->pid == pid) {
          csdev->refcnt++;
-        goto unlock_out;
+        return rc;
      }
        rc = tmc_etr_enable_hw(drvdata, etr_perf->etr_buf);
@@ -1773,22 +1756,43 @@ static int tmc_enable_etr_sink_perf(struct 
 coresight_device *csdev, void *data)
          csdev->refcnt++;
      }
  -unlock_out:
-    raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
      return rc;
  }
    static int tmc_enable_etr_sink(struct coresight_device *csdev,
                     enum cs_mode mode, void *data)
  {
-    switch (mode) {
-    case CS_MODE_SYSFS:
-        return tmc_enable_etr_sink_sysfs(csdev);
-    case CS_MODE_PERF:
-        return tmc_enable_etr_sink_perf(csdev, data);
-    default:
-        return -EINVAL;
+    struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+    int rc;
+
+    scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock) {
+        if (coresight_get_mode(csdev) != CS_MODE_DISABLED &&
+            coresight_get_mode(csdev) != mode)
+            return -EBUSY;
+
+        switch (mode) {
+        case CS_MODE_SYSFS:
Quoted 8 lines
Quoted 6 lines

drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1780

Quoted 6 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +6 -0
+            if (csdev->refcnt) {
+                /* The sink is already enabled */
+                csdev->refcnt++;
+                return 0;
+            }
+            coresight_set_mode(csdev, mode);

Why are we spilling bits here in the common code for sysfs ? More on this, see below.

This is to ensure that all operations on the mode are placed within a spinlock critical section. If the above mode setting operation is removed, when the sysfs session and the perf session enable the sink simultaneously, the sysfs session will release the spinlock multiple times during the process of enabling the sink. The perf session can acquire the lock at multiple points in time when the spinlock is released by sysfs and complete the entire sink enabling process. Therefore, the sysfs session needs to check whether the sink is occupied by perf each time before reacquiring the lock.

Quoted 14 lines
Quoted 10 lines

drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1789

Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +9 -0
+            break;
+        case CS_MODE_PERF:
+            return tmc_enable_etr_sink_perf(csdev, data);
+        default:
+            return -EINVAL;
+        }
+    }
+
+    rc = tmc_enable_etr_sink_sysfs(csdev);

We now call the above function without the spinlock and the refcnt is managed with and without the spinlock by the users. This is problematic, with refcnt being a non-atomic type.

Quoted 5 lines

Please fix. I don't see why we can't set the mode in tmc_enable_etr_sink_sysfs() with the locks held and reset the mode if we failed enable it properly.

Suzuki

The internal of the tmc_enable_etr_sink_sysfs() function will acquire a spinlock. It manages the refcnt within the spinlock context, ensuring that the operation is safe.

And tmc_enable_etr_sink_sysfs() will call tmc_etr_get_sysfs_buffer(), the function tmc_etr_get_sysfs_buffer() will release the spinlock when allocating the buffer, so it is not possible to move tmc_enable_etr_sink_sysfs() into the context of the spinlock.

Or is there another way to do it?

Best regards,
Junhao.

Quoted 16 lines
Quoted 11 lines

drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)

Quoted 11 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +5 -0
+    if (rc) {
+        scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock)
+        coresight_set_mode(csdev, CS_MODE_DISABLED);
      }
+
+    return rc;
  }
    static int tmc_disable_etr_sink(struct coresight_device *csdev)
 

.

author
#7 lore raw

PATCH v3 3/3 coresight: tmc: Decouple the perf buffer allocation from sysfs mode

In reply to Junhao He

From: Yicong Yang <[email protected]>

Currently the perf buffer allocation follows the below logic:
- if the required AUX buffer size if larger, allocate the buffer with the required size
- otherwise allocate the size reference to the sysfs buffer size

This is not useful as we only collect to one AUX data, so just try to allocate the buffer match the AUX buffer size.

---
 .../hwtracing/coresight/coresight-tmc-etr.c   | 30 ++++++-------------
 1 file changed, 9 insertions(+), 21 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +9 -21
index 06c74717be19..af12d6b98030 100644
@@ -1344,9 +1344,7 @@ EXPORT_SYMBOL_GPL(tmc_etr_get_buffer);
 
 /*
  * alloc_etr_buf: Allocate ETR buffer for use by perf.
- * The size of the hardware buffer is dependent on the size configured
- * via sysfs and the perf ring buffer size. We prefer to allocate the
- * largest possible size, scaling down the size by half until it
+ * Allocate the largest possible size, scaling down the size by half until it
  * reaches a minimum limit (1M), beyond which we give up.
  */
 static struct etr_buf *
@@ -1355,36 +1353,26 @@ alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
 {
 	int node;
 	struct etr_buf *etr_buf;
-	unsigned long size;
+	ssize_t size;
 
 	node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
-	/*
-	 * Try to match the perf ring buffer size if it is larger
-	 * than the size requested via sysfs.
-	 */
-	if ((nr_pages << PAGE_SHIFT) > drvdata->size) {
-		etr_buf = tmc_alloc_etr_buf(drvdata, ((ssize_t)nr_pages << PAGE_SHIFT),
-					    0, node, NULL);
-		if (!IS_ERR(etr_buf))
-			goto done;
-	}
+
+	/* Use the minimum limit if the required size is smaller */
+	size = nr_pages << PAGE_SHIFT;
+	size = max_t(ssize_t, size, TMC_ETR_PERF_MIN_BUF_SIZE);
 
 	/*
-	 * Else switch to configured size for this ETR
-	 * and scale down until we hit the minimum limit.
+	 * Try to allocate the required size for this ETR, if failed scale
+	 * down until we hit the minimum limit.
 	 */
-	size = drvdata->size;
 	do {
 		etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL);
 		if (!IS_ERR(etr_buf))
-			goto done;
+			return etr_buf;
 		size /= 2;
 	} while (size >= TMC_ETR_PERF_MIN_BUF_SIZE);
 
 	return ERR_PTR(-ENOMEM);
-
-done:
-	return etr_buf;
 }
 
 static struct etr_buf *
Signature · 1 line
2.33.0
#8 lore raw
In reply to Junhao He

a gentle ping...

Quoted 35 lines · Junhao He

This patchset builds upon Yicong's previous patches [1].

Introducing fix two race issues found by using TMC-ETR and CATU, Two cleanups found when debugging the issues.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/

---
Changes in v3:
- Patches 1: Additional comment for tmc_drvdata::etr_mode. Update comment for tmc_drvdata::reading with Jonathan's Tag.
- Patches 2: Replace scoped_guard with guard with Jonathan's Tag.
- Patches 2: Fix spinlock to raw_spinlock, and refactor this code based on Leo's suggested solution.
- Patches 3: change the size's type to ssize_t and use max_t to simplify the code with Leo's Tag.

Changes in v2:
- Updated the commit of patch2.
- Rebase to v6.16-rc1

Junhao He (1):
coresight: tmc: refactor the tmc-etr mode setting to avoid race
conditions

Yicong Yang (2):
coresight: tmc: Add missing doc including reading and etr_mode of
struct tmc_drvdata
coresight: tmc: Decouple the perf buffer allocation from sysfs mode

  .../hwtracing/coresight/coresight-tmc-etr.c   | 110 ++++++++----------
  drivers/hwtracing/coresight/coresight-tmc.h   |   2 +
  2 files changed, 53 insertions(+), 59 deletions(-)