mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups
@ 2026-01-21 10:15 Yushan Wang
  2026-01-21 10:15 ` [PATCH v5 1/3] coresight: tmc: Add missing doc including reading and etr_mode of struct tmc_drvdata Yushan Wang
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Yushan Wang @ 2026-01-21 10:15 UTC (permalink / raw)
  To: suzuki.poulose, james.clark, anshuman.khandual, yeoreum.yun,
	mike.leach, leo.yan
  Cc: coresight, linux-arm-kernel, linux-kernel, linuxarm,
	jonathan.cameron, hejunhao3, wangyushan12

From: Junhao He <hejunhao3@h-partners.com>

This patchset builds upon Yicong's previous patches [1].
I rebase the patchset onto coresight for-next branch.

Patch 2 introducing fix race issues found by using TMC-ETR.
Patch 1 & 3 introducing two cleanups found when debugging the issues.

[1] https://lore.kernel.org/linux-arm-kernel/20241202092419.11777-1-yangyicong@huawei.com/

---
Changes in v5:
 - a) Rebase the patchset onto coresight for-next branch.
 - b) Drop the refactoring of the mode handling, retain only the essential fix
Link: https://lore.kernel.org/linux-arm-kernel/20251111122149.1981162-1-hejunhao3@h-partners.com/

---
Changes in v4:
 - a) Add comment at the context of set etr to sysfs mode.
 - b) Move the check on drvdata->read to the start of enable etr.
 - c) Add checks to prevent multiple sysfs processes from simultaneously
 competing to enable ETR.
 - d) Fix the issue with the guard used.
Link: https://lore.kernel.org/linux-arm-kernel/20250818080600.418425-1-hejunhao3@huawei.com/

---
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.
Link: https://lore.kernel.org/linux-arm-kernel/20250620075412.952934-1-hejunhao3@huawei.com/

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

Yicong Yang (3):
  coresight: tmc: Add missing doc including reading and etr_mode of
    struct tmc_drvdata
  coresight: tmc-etr: Fix race condition between sysfs and perf mode
  coresight: tmc: Decouple the perf buffer allocation from sysfs mode

 .../hwtracing/coresight/coresight-tmc-etr.c   | 43 ++++++++++---------
 drivers/hwtracing/coresight/coresight-tmc.h   |  2 +
 2 files changed, 24 insertions(+), 21 deletions(-)

-- 
2.33.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v5 1/3] coresight: tmc: Add missing doc including reading and etr_mode of struct tmc_drvdata
  2026-01-21 10:15 [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups Yushan Wang
@ 2026-01-21 10:15 ` Yushan Wang
  2026-01-21 10:15 ` [PATCH v5 2/3] coresight: tmc-etr: Fix race condition between sysfs and perf mode Yushan Wang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Yushan Wang @ 2026-01-21 10:15 UTC (permalink / raw)
  To: suzuki.poulose, james.clark, anshuman.khandual, yeoreum.yun,
	mike.leach, leo.yan
  Cc: coresight, linux-arm-kernel, linux-kernel, linuxarm,
	jonathan.cameron, hejunhao3, wangyushan12

From: Yicong Yang <yangyicong@hisilicon.com>

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.

Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
Reviewed-by: Leo Yan <leo.yan@arm.com>
Signed-off-by: Junhao He <hejunhao3@h-partners.com>
---
 drivers/hwtracing/coresight/coresight-tmc.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/hwtracing/coresight/coresight-tmc.h b/drivers/hwtracing/coresight/coresight-tmc.h
index 95473d131032..319a354ede9f 100644
--- a/drivers/hwtracing/coresight/coresight-tmc.h
+++ b/drivers/hwtracing/coresight/coresight-tmc.h
@@ -221,6 +221,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
@@ -233,6 +234,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.
-- 
2.33.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v5 2/3] coresight: tmc-etr: Fix race condition between sysfs and perf mode
  2026-01-21 10:15 [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups Yushan Wang
  2026-01-21 10:15 ` [PATCH v5 1/3] coresight: tmc: Add missing doc including reading and etr_mode of struct tmc_drvdata Yushan Wang
@ 2026-01-21 10:15 ` Yushan Wang
  2026-01-21 10:15 ` [PATCH v5 3/3] coresight: tmc: Decouple the perf buffer allocation from sysfs mode Yushan Wang
  2026-01-21 16:43 ` [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups Suzuki K Poulose
  3 siblings, 0 replies; 5+ messages in thread
From: Yushan Wang @ 2026-01-21 10:15 UTC (permalink / raw)
  To: suzuki.poulose, james.clark, anshuman.khandual, yeoreum.yun,
	mike.leach, leo.yan
  Cc: coresight, linux-arm-kernel, linux-kernel, linuxarm,
	jonathan.cameron, hejunhao3, wangyushan12

From: Yicong Yang <yangyicong@hisilicon.com>

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 enablement of sysfs mode is separeted into two critical regions,
one for sysfs buffer allocation and another for hardware enablement, it's
possible to race with the perf mode. Fix this by double check whether
the perf mode's been used before enabling the hardware in sysfs mode.

 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)

With this fix, we retain the check for CS_MODE_PERF in get_etr_sysfs_buf.
This ensures we verify whether the perf mode's already running before we
actually allocate the buffer. Then we can save the time of
allocating/freeing the sysfs buffer if race with the perf mode.

Fixes: 296b01fd106e ("coresight: Refactor out buffer allocation function for ETR")
Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
Signed-off-by: Junhao He <hejunhao3@h-partners.com>
---
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c
index e0d83ee01b77..fc0a946053dd 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-etr.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c
@@ -1306,6 +1306,19 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
 
 	raw_spin_lock_irqsave(&drvdata->spinlock, flags);
 
+	/*
+	 * Since the sysfs buffer allocation and the hardware enablement is not
+	 * in the same critical region, it's possible to race with the perf.
+	 */
+	if (coresight_get_mode(csdev) == CS_MODE_PERF) {
+		drvdata->sysfs_buf = NULL;
+		raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
+
+		/* Free allocated memory out side of the spinlock */
+		tmc_etr_free_sysfs_buf(sysfs_buf);
+		return -EBUSY;
+	}
+
 	/*
 	 * 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
-- 
2.33.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v5 3/3] coresight: tmc: Decouple the perf buffer allocation from sysfs mode
  2026-01-21 10:15 [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups Yushan Wang
  2026-01-21 10:15 ` [PATCH v5 1/3] coresight: tmc: Add missing doc including reading and etr_mode of struct tmc_drvdata Yushan Wang
  2026-01-21 10:15 ` [PATCH v5 2/3] coresight: tmc-etr: Fix race condition between sysfs and perf mode Yushan Wang
@ 2026-01-21 10:15 ` Yushan Wang
  2026-01-21 16:43 ` [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups Suzuki K Poulose
  3 siblings, 0 replies; 5+ messages in thread
From: Yushan Wang @ 2026-01-21 10:15 UTC (permalink / raw)
  To: suzuki.poulose, james.clark, anshuman.khandual, yeoreum.yun,
	mike.leach, leo.yan
  Cc: coresight, linux-arm-kernel, linux-kernel, linuxarm,
	jonathan.cameron, hejunhao3, wangyushan12

From: Yicong Yang <yangyicong@hisilicon.com>

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.

Suggested-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Link: https://lore.kernel.org/linux-arm-kernel/df8967cd-2157-46a2-97d9-a1aea883cf63@arm.com/
Signed-off-by: Yicong Yang <yangyicong@hisilicon.com>
Signed-off-by: Junhao He <hejunhao3@h-partners.com>
---
 .../hwtracing/coresight/coresight-tmc-etr.c   | 30 ++++++-------------
 1 file changed, 9 insertions(+), 21 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c
index fc0a946053dd..cee82e52c4ea 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-etr.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c
@@ -1367,9 +1367,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 *
@@ -1378,36 +1376,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 *
-- 
2.33.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups
  2026-01-21 10:15 [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups Yushan Wang
                   ` (2 preceding siblings ...)
  2026-01-21 10:15 ` [PATCH v5 3/3] coresight: tmc: Decouple the perf buffer allocation from sysfs mode Yushan Wang
@ 2026-01-21 16:43 ` Suzuki K Poulose
  3 siblings, 0 replies; 5+ messages in thread
From: Suzuki K Poulose @ 2026-01-21 16:43 UTC (permalink / raw)
  To: james.clark, anshuman.khandual, yeoreum.yun, mike.leach, leo.yan,
	Yushan Wang
  Cc: Suzuki K Poulose, coresight, linux-arm-kernel, linux-kernel,
	linuxarm, jonathan.cameron, hejunhao3


On Wed, 21 Jan 2026 18:15:40 +0800, Yushan Wang wrote:
> This patchset builds upon Yicong's previous patches [1].
> I rebase the patchset onto coresight for-next branch.
> 
> Patch 2 introducing fix race issues found by using TMC-ETR.
> Patch 1 & 3 introducing two cleanups found when debugging the issues.
> 
> [1] https://lore.kernel.org/linux-arm-kernel/20241202092419.11777-1-yangyicong@huawei.com/
> 
> [...]

Applied, thanks!

[1/3] coresight: tmc: Add missing doc including reading and etr_mode of struct tmc_drvdata
      https://git.kernel.org/coresight/c/5da8c55dd879
[2/3] coresight: tmc-etr: Fix race condition between sysfs and perf mode
      https://git.kernel.org/coresight/c/e6e43e82c79c
[3/3] coresight: tmc: Decouple the perf buffer allocation from sysfs mode
      https://git.kernel.org/coresight/c/eebe8dbd8630

Best regards,
-- 
Suzuki K Poulose <suzuki.poulose@arm.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-01-21 16:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-21 10:15 [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups Yushan Wang
2026-01-21 10:15 ` [PATCH v5 1/3] coresight: tmc: Add missing doc including reading and etr_mode of struct tmc_drvdata Yushan Wang
2026-01-21 10:15 ` [PATCH v5 2/3] coresight: tmc-etr: Fix race condition between sysfs and perf mode Yushan Wang
2026-01-21 10:15 ` [PATCH v5 3/3] coresight: tmc: Decouple the perf buffer allocation from sysfs mode Yushan Wang
2026-01-21 16:43 ` [PATCH v5 0/3] Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups 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

all inboxes | Powered by JetHome®