Coresight TMC-ETR some bugfixes and cleanups
v2 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 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 of tmc_drvdata::reading
coresight: tmc: Decouple the perf buffer allocation from sysfs mode
.../hwtracing/coresight/coresight-tmc-etr.c | 102 +++++++++--------- drivers/hwtracing/coresight/coresight-tmc.h | 1 + 2 files changed, 53 insertions(+), 50 deletions(-)
Signature · 1 line
2.33.0
PATCH v2 1/3 coresight: tmc: Add missing doc of tmc_drvdata::reading
From: Yicong Yang <[email protected]>
tmc_drvdata::reading is used to indicate whether a reading process is performed through /dev/xyz.tmc. Document it.
--- drivers/hwtracing/coresight/coresight-tmc.h | 1 + 1 file changed, 1 insertion(+)
drivers/hwtracing/coresight/coresight-tmc.h +1 -0
@@ -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
Signature · 1 line
2.33.0
Re: [PATCH v2 1/3] coresight: tmc: Add missing doc of tmc_drvdata::reading
Quoted 17 lines · On Fri, 20 Jun 2025 15:54:10 +0800 Junhao He <[email protected]> wrote:
From: Yicong Yang <[email protected]>
tmc_drvdata::readingis used to indicate whether a reading process is performed through/dev/xyz.tmc. Document it.Reviewed-by James Clark <[email protected]>Signed-off-by Yicong Yang <[email protected]>Signed-off-by Junhao He <[email protected]>--- drivers/hwtracing/coresight/coresight-tmc.h | 1 + 1 file changed, 1 insertion(+)drivers/hwtracing/coresight/coresight-tmc.h +0
-0
drivers/hwtracing/coresight/coresight-tmc.h struct tmc_resrv_buf { · line 223
Quoted 4 lines
drivers/hwtracing/coresight/coresight-tmc.h +1
-0@@ -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
Hi,
Perhaps reword:
"buffer is being read through "/dev/xyz.tmc" entry" or "buffer read in progress through "/dev/xyz.tmc" entry"
I've not checked what this actually means - just looking at what you have here.
Quoted 3 lines
drivers/hwtracing/coresight/coresight-tmc.h +0
-0* @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
Re: [PATCH v2 1/3] coresight: tmc: Add missing doc of tmc_drvdata::reading
Quoted 26 lines · Jonathan Cameron
Quoted 17 lines · On Fri, 20 Jun 2025 15:54:10 +0800 Junhao He <[email protected]> wrote:
From: Yicong Yang <[email protected]>
tmc_drvdata::readingis used to indicate whether a reading process is performed through/dev/xyz.tmc. Document it.Reviewed-by James Clark <[email protected]>Signed-off-by Yicong Yang <[email protected]>Signed-off-by Junhao He <[email protected]>--- drivers/hwtracing/coresight/coresight-tmc.h | 1 + 1 file changed, 1 insertion(+)drivers/hwtracing/coresight/coresight-tmc.h +0
-0drivers/hwtracing/coresight/coresight-tmc.h struct tmc_resrv_buf { · line 223
Quoted 4 lines
drivers/hwtracing/coresight/coresight-tmc.h +1
-0@@ -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 Hi,
Quoted 6 lines
Perhaps reword:
"buffer is being read through "
/dev/xyz.tmc" entry" or "buffer read in progress through "/dev/xyz.tmc" entry"I've not checked what this actually means - just looking at what you have here.
Will update this. Thanks
Best regards,
Junhao.
Quoted 5 lines
drivers/hwtracing/coresight/coresight-tmc.h struct tmc_resrv_buf { · line 226
Quoted 3 lines
drivers/hwtracing/coresight/coresight-tmc.h +0
-0* @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 .
Leo Yan
reviewerRe: [PATCH v2 1/3] coresight: tmc: Add missing doc of tmc_drvdata::reading
In reply to Junhao HeQuoted 17 lines · Junhao He
From: Yicong Yang <[email protected]>
tmc_drvdata::readingis used to indicate whether a reading process is performed through/dev/xyz.tmc. Document it.Reviewed-by James Clark <[email protected]>Signed-off-by Yicong Yang <[email protected]>Signed-off-by Junhao He <[email protected]>--- drivers/hwtracing/coresight/coresight-tmc.h | 1 + 1 file changed, 1 insertion(+)drivers/hwtracing/coresight/coresight-tmc.h +0
-0
drivers/hwtracing/coresight/coresight-tmc.h struct tmc_resrv_buf { · line 223
Quoted 4 lines
drivers/hwtracing/coresight/coresight-tmc.h +1
-0@@ -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
Are you working on the latest code base? For example, the mainline kernel or coresight next branch.
The latest code already has comment for reading, and I saw a duplicated "reading" field in tmc_drvdata.
The tmc_resrv_buf structure does not have the fields "pid", "stop_on_flush", "buf", etc. They have been moved into the tmc_drvdata structure.
Thanks,
Leo
Quoted 6 lines
drivers/hwtracing/coresight/coresight-tmc.h +0
-0* @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 Signature · 1 line
2.33.0
Re: [PATCH v2 1/3] coresight: tmc: Add missing doc of tmc_drvdata::reading
Quoted 13 lines · Leo Yan
Quoted 12 lines · Junhao He
From: Yicong Yang <[email protected]>
tmc_drvdata::readingis used to indicate whether a reading process is performed through/dev/xyz.tmc. Document it.Reviewed-by James Clark <[email protected]>Signed-off-by Yicong Yang <[email protected]>Signed-off-by Junhao He <[email protected]>--- drivers/hwtracing/coresight/coresight-tmc.h | 1 + 1 file changed, 1 insertion(+)
Quoted 13 lines
drivers/hwtracing/coresight/coresight-tmc.h struct tmc_resrv_buf { · line 223
Quoted 9 lines
drivers/hwtracing/coresight/coresight-tmc.h +1
-0@@ -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 Are you working on the latest code base? For example, the mainline kernel or coresight next branch.
The latest code already has comment for reading, and I saw a duplicated "reading" field intmc_drvdata.
This patch is based on mainline kernel v6.16-rc1, and adds a comment to struct tmc_drvdata::reading. Perhaps, this git log could easily be misunderstood as adding comments to tmc_resrv_buf::reading.
Quoted 18 lines
The
tmc_resrv_bufstructure does not have the fields "pid", "stop_on_flush", "buf", etc. They have been moved into thetmc_drvdatastructure.Thanks,
LeoQuoted 6 lines
drivers/hwtracing/coresight/coresight-tmc.h +0
-0* @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 Signature · 1 line
2.33.0_______________________________________________
CoreSight mailing list -- [email protected] To unsubscribe send an email to [email protected] .
PATCH v2 2/3 coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions
In reply to Junhao HeWhen 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.
--- .../hwtracing/coresight/coresight-tmc-etr.c | 73 +++++++++++-------- 1 file changed, 43 insertions(+), 30 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +43 -30
@@ -1263,11 +1263,6 @@ 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 ) { - ret = -EBUSY ; - goto out; - } -/* * If we don't have a buffer or it doesn't match the requested size, * use the buffer allocated above. Otherwise reuse the existing buffer.
@@ -1278,7 +1273,6 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev) drvdata->sysfs_buf = new_buf; } -out:raw_spin_unlock_irqrestore (&drvdata->spinlock, flags);/* Free memory outside the spinlock if need be */
@@ -1289,7 +1283,7 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)static int tmc_enable_etr_sink_sysfs (struct coresight_device *csdev) { -int ret =0 ; +int ret;unsigned long flags;struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent);struct etr_buf *sysfs_buf =tmc_etr_get_sysfs_buffer (csdev);
@@ -1299,23 +1293,10 @@ 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. -*/ -if (coresight_get_mode (csdev) ==CS_MODE_SYSFS ) { - 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);if (!ret)
@@ -1735,11 +1716,6 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *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 ;
@@ -1768,7 +1744,6 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)if (!rc) {/* Associate with monitored process. */ drvdata->pid = pid; -coresight_set_mode (csdev,CS_MODE_PERF ); drvdata->perf_buf = etr_perf->etr_buf; csdev->refcnt++; }
@@ -1781,14 +1756,52 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)static int tmc_enable_etr_sink (struct coresight_device *csdev,enum cs_mode mode,void *data) { +struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent); +enum cs_mode old_mode; +int rc = -EINVAL ; + + scoped_guard(spinlock_irqsave, &drvdata->spinlock) { + old_mode =coresight_get_mode (csdev); +if (old_mode !=CS_MODE_DISABLED && old_mode != mode) +return -EBUSY ; + +if (drvdata->reading) +return -EBUSY ; + +/* In sysFS mode we can have multiple writers per sink. */ +if (old_mode ==CS_MODE_SYSFS ) { + csdev->refcnt++; +return 0 ; + } + +/* +* minor note: In sysFS mode, the task1 get locked first, it setup +* etr mode to SYSFS. Then task2 get locked,it will directly return +* success even when the tmc-etr is not enabled at this moment. +* Ultimately, task1 will still successfully enable tmc-etr. +* This is a transient state and does not cause an anomaly. +*/ +coresight_set_mode (csdev, mode); + } +switch (mode) {case CS_MODE_SYSFS : -return tmc_enable_etr_sink_sysfs (csdev); + rc =tmc_enable_etr_sink_sysfs (csdev); +break ;case CS_MODE_PERF : -return tmc_enable_etr_sink_perf (csdev, data); + rc =tmc_enable_etr_sink_perf (csdev, data); +break ;default : -return -EINVAL ; + rc = -EINVAL ; } + +if (rc && old_mode != mode) { + scoped_guard(spinlock_irqsave, &drvdata->spinlock) { +coresight_set_mode (csdev, old_mode); + } + } + +return rc; }static int tmc_disable_etr_sink (struct coresight_device *csdev)
Signature · 1 line
2.33.0
Re: [PATCH v2 2/3] coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions
Quoted 147 lines · On Fri, 20 Jun 2025 15:54:11 +0800 Junhao He <[email protected]> wrote:
When trying to run perf and sysfs mode simultaneously, the
WARN_ON()intmc_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 sicneetr_bufinitialized 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()orenable_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")Reported-by Yicong Yang <[email protected]>Signed-off-by Junhao He <[email protected]>--- .../hwtracing/coresight/coresight-tmc-etr.c | 73 +++++++++++-------- 1 file changed, 43 insertions(+), 30 deletions(-)drivers/hwtracing/coresight/coresight-tmc-etr.c +6
-27@@ -1263,11 +1263,6 @@ 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 ) { - ret = -EBUSY ; - goto out; - } -/* * If we don't have a buffer or it doesn't match the requested size, * use the buffer allocated above. Otherwise reuse the existing buffer. @@ -1278,7 +1273,6 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev) drvdata->sysfs_buf = new_buf; } -out:raw_spin_unlock_irqrestore (&drvdata->spinlock, flags);/* Free memory outside the spinlock if need be */ @@ -1289,7 +1283,7 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)static int tmc_enable_etr_sink_sysfs (struct coresight_device *csdev) { -int ret =0 ; +int ret;unsigned long flags;struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent);struct etr_buf *sysfs_buf =tmc_etr_get_sysfs_buffer (csdev);@@ -1299,23 +1293,10 @@ 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. -*/ -if (coresight_get_mode (csdev) ==CS_MODE_SYSFS ) { - 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);if (!ret)@@ -1735,11 +1716,6 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *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 ;@@ -1768,7 +1744,6 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)if (!rc) {/* Associate with monitored process. */ drvdata->pid = pid; -coresight_set_mode (csdev,CS_MODE_PERF ); drvdata->perf_buf = etr_perf->etr_buf; csdev->refcnt++; }@@ -1781,14 +1756,52 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)static int tmc_enable_etr_sink (struct coresight_device *csdev,enum cs_mode mode,void *data) { +struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent); +enum cs_mode old_mode; +int rc = -EINVAL ; +
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1772
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +10
-0+ scoped_guard(spinlock_irqsave, &drvdata->spinlock) { + old_mode =coresight_get_mode (csdev); +if (old_mode !=CS_MODE_DISABLED && old_mode != mode) +return -EBUSY ; + +if (drvdata->reading) +return -EBUSY ; + +/* In sysFS mode we can have multiple writers per sink. */ +if (old_mode ==CS_MODE_SYSFS ) {
This seems odd. You are incrementing the reference count when potentially changing away from CS_MODE_SYSFS?
Is this meant to only occur if old_mode == mode && old_mode == CS_MODE_SYSFS?
In the code prior to this patch this bit only ran in tmc_enable_etr_sink_sysfs() which was only called based on the mode being configured (mode here I think) being sysfs. That no longer looks to be the case.
Maybe I'm missing something as the flows around this are complex.
Quoted 20 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +16
-1+ csdev->refcnt++; +return 0 ; + } + +/* +* minor note: In sysFS mode, the task1 get locked first, it setup +* etr mode to SYSFS. Then task2 get locked,it will directly return +* success even when the tmc-etr is not enabled at this moment. +* Ultimately, task1 will still successfully enable tmc-etr. +* This is a transient state and does not cause an anomaly. +*/ +coresight_set_mode (csdev, mode); + } +switch (mode) {case CS_MODE_SYSFS : -return tmc_enable_etr_sink_sysfs (csdev); + rc =tmc_enable_etr_sink_sysfs (csdev); +break ;case CS_MODE_PERF :
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1799
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +6
-2-return tmc_enable_etr_sink_perf (csdev, data); + rc =tmc_enable_etr_sink_perf (csdev, data); +break ;default : -return -EINVAL ; + rc = -EINVAL ; } + +if (rc && old_mode != mode) { + scoped_guard(spinlock_irqsave, &drvdata->spinlock) {
Might be a local style matching thing but if not the scope is tight anyway so could use the unscoped version.
guard(spinlock_irqsave)(&drvdata->spinlock); coresight_set_mode(csdev, old_mode);
Quoted 8 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +5
-0+coresight_set_mode (csdev, old_mode); + } + } + +return rc; }static int tmc_disable_etr_sink (struct coresight_device *csdev)
Re: [PATCH v2 2/3] coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions
Quoted 155 lines · Jonathan Cameron
Quoted 152 lines · On Fri, 20 Jun 2025 15:54:11 +0800 Junhao He <[email protected]> wrote:
When trying to run perf and sysfs mode simultaneously, the
WARN_ON()intmc_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 sicneetr_bufinitialized 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()orenable_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")Reported-by Yicong Yang <[email protected]>Signed-off-by Junhao He <[email protected]>--- .../hwtracing/coresight/coresight-tmc-etr.c | 73 +++++++++++-------- 1 file changed, 43 insertions(+), 30 deletions(-)drivers/hwtracing/coresight/coresight-tmc-etr.c +11
-27@@ -1263,11 +1263,6 @@ 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 ) { - ret = -EBUSY ; - goto out; - } -/* * If we don't have a buffer or it doesn't match the requested size, * use the buffer allocated above. Otherwise reuse the existing buffer. @@ -1278,7 +1273,6 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev) drvdata->sysfs_buf = new_buf; } -out:raw_spin_unlock_irqrestore (&drvdata->spinlock, flags);/* Free memory outside the spinlock if need be */ @@ -1289,7 +1283,7 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)static int tmc_enable_etr_sink_sysfs (struct coresight_device *csdev) { -int ret =0 ; +int ret;unsigned long flags;struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent);struct etr_buf *sysfs_buf =tmc_etr_get_sysfs_buffer (csdev);@@ -1299,23 +1293,10 @@ 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. -*/ -if (coresight_get_mode (csdev) ==CS_MODE_SYSFS ) { - 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);if (!ret)@@ -1735,11 +1716,6 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *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 ;@@ -1768,7 +1744,6 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)if (!rc) {/* Associate with monitored process. */ drvdata->pid = pid; -coresight_set_mode (csdev,CS_MODE_PERF ); drvdata->perf_buf = etr_perf->etr_buf; csdev->refcnt++; }@@ -1781,14 +1756,52 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)static int tmc_enable_etr_sink (struct coresight_device *csdev,enum cs_mode mode,void *data) { +struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent); +enum cs_mode old_mode; +int rc = -EINVAL ; + + scoped_guard(spinlock_irqsave, &drvdata->spinlock) { + old_mode =coresight_get_mode (csdev); +if (old_mode !=CS_MODE_DISABLED && old_mode != 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 1772
Quoted 5 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +5
-0+if (drvdata->reading) +return -EBUSY ; + +/* In sysFS mode we can have multiple writers per sink. */ +if (old_mode ==CS_MODE_SYSFS ) {This seems odd. You are incrementing the reference count when potentially changing away from
CS_MODE_SYSFS?
Is this meant to only occur ifold_mode== mode &&old_mode==CS_MODE_SYSFS?
Yes, old_mode == CS_MODE_SYSFS means tmc_etr is occupied by sysfs, and old_mode == mode, so can add reference counting directly. When tmc_etr is idle its mode is CS_MODE_DISABLED.
Quoted 3 lines
In the code prior to this patch this bit only ran in
tmc_enable_etr_sink_sysfs()which was only called based on the mode being configured (mode here I think) being sysfs. That no longer looks to be the case.
before the patch, first check (mode == CS_MODE_SYSFS), then check (coresight_get_mode(csdev) == CS_MODE_SYSFS), and finally csdev->refcnt++. This is the same as the current functionality.
Quoted 36 lines
Maybe I'm missing something as the flows around this are complex.
Quoted 20 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +16
-1+ csdev->refcnt++; +return 0 ; + } + +/* +* minor note: In sysFS mode, the task1 get locked first, it setup +* etr mode to SYSFS. Then task2 get locked,it will directly return +* success even when the tmc-etr is not enabled at this moment. +* Ultimately, task1 will still successfully enable tmc-etr. +* This is a transient state and does not cause an anomaly. +*/ +coresight_set_mode (csdev, mode); + } +switch (mode) {case CS_MODE_SYSFS : -return tmc_enable_etr_sink_sysfs (csdev); + rc =tmc_enable_etr_sink_sysfs (csdev); +break ;case CS_MODE_PERF :drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1799
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +6
-2-return tmc_enable_etr_sink_perf (csdev, data); + rc =tmc_enable_etr_sink_perf (csdev, data); +break ;default : -return -EINVAL ; + rc = -EINVAL ; } + +if (rc && old_mode != mode) { + scoped_guard(spinlock_irqsave, &drvdata->spinlock) {Might be a local style matching thing but if not the scope is tight anyway so could use the unscoped version.
Quoted 2 lines
guard(spinlock_irqsave)(&drvdata->spinlock); coresight_set_mode(csdev, old_mode);
Sure, Will fix in next version.
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1807
Quoted 8 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +5
-0+coresight_set_mode (csdev, old_mode); + } + } + +return rc; }static int tmc_disable_etr_sink (struct coresight_device *csdev).
Leo Yan
reviewerRe: [PATCH v2 2/3] coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions
In reply to Junhao HeOn Fri, Jun 20, 2025 at 03:54:11PM +0800, Junhao He wrote:
[...]
Quoted 4 lines
To fix this, configure the tmc-etr mode before invoking
enable_etr_perf()orenable_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.
I have a similiar fixing for CTI driver, see:
https://lore.kernel.org/linux-arm-kernel/[email protected]/T/#maccd68b460fb8d74ccdd3504163d8f486f04178b
[...]
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1763
Quoted 9 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +5
-0@@ -1781,14 +1756,52 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)static int tmc_enable_etr_sink (struct coresight_device *csdev,enum cs_mode mode,void *data) { +struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent); +enum cs_mode old_mode; +int rc = -EINVAL ; + + scoped_guard(spinlock_irqsave, &drvdata->spinlock) {
scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock) {
I am concerned that the spinlock is acquired and released for several times in a single flow. It is possible to hit some corner case, e.g.,
- CPU0 acquires the lock, set sink as SYSFS mode, and releases the lock;
- CPU1 acquires the lock, detects the SYSFS mode has been set, directly bail out, then enable ETM.
The problem is the sink has not been enabled yet. This leads to CPU1 to enable the tracer but prior to sink's enabling.
The key piont is we should ensure the mode is consistent to the hardware state. I will take a bit time for if we can have a better idea for this.
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1773
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +10
-0+ old_mode =coresight_get_mode (csdev); +if (old_mode !=CS_MODE_DISABLED && old_mode != mode) +return -EBUSY ; + +if (drvdata->reading) +return -EBUSY ; + +/* In sysFS mode we can have multiple writers per sink. */ +if (old_mode ==CS_MODE_SYSFS ) { + csdev->refcnt++;
I am just wandering if we can unify the "refcnt" code for both SYSFS mode and Perf mode, and as a result, we can consolidate the code cross different drivers.
Something like:
if (!csdev->refcnt) {
coresight_set_mode(csdev, mode);
} else {
/* The device has been configured with an incompatible mode */
if (coresight_get_mode(csdev) != mode)
return -EBUSY;
csdev->refcnt++;
}Then when disable the device:
csdev->refcnt--;
if (!csdev->refcnt)
coresight_set_mode(csdev, CS_MODE_DISABLED);We should not check "drvdata->reading" when enable or disable sink, as it is a flag to track if reading the trace buffer, it is irrelevant to the sink device's enabling or disabling.
Please verify perf mode (especially for system wide session) to avoid anything missed.
Thanks,
Leo
Quoted 40 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +26
-3+return 0 ; + } + +/* +* minor note: In sysFS mode, the task1 get locked first, it setup +* etr mode to SYSFS. Then task2 get locked,it will directly return +* success even when the tmc-etr is not enabled at this moment. +* Ultimately, task1 will still successfully enable tmc-etr. +* This is a transient state and does not cause an anomaly. +*/ +coresight_set_mode (csdev, mode); + } +switch (mode) {case CS_MODE_SYSFS : -return tmc_enable_etr_sink_sysfs (csdev); + rc =tmc_enable_etr_sink_sysfs (csdev); +break ;case CS_MODE_PERF : -return tmc_enable_etr_sink_perf (csdev, data); + rc =tmc_enable_etr_sink_perf (csdev, data); +break ;default : -return -EINVAL ; + rc = -EINVAL ; } + +if (rc && old_mode != mode) { + scoped_guard(spinlock_irqsave, &drvdata->spinlock) { +coresight_set_mode (csdev, old_mode); + } + } + +return rc; }static int tmc_disable_etr_sink (struct coresight_device *csdev)Signature · 1 line
2.33.0
Re: [PATCH v2 2/3] coresight: tmc: refactor the tmc-etr mode setting to avoid race conditions
Quoted 13 lines · Leo Yan
On Fri, Jun 20, 2025 at 03:54:11PM +0800, Junhao He wrote:
[...]
Quoted 4 lines
To fix this, configure the tmc-etr mode before invoking
enable_etr_perf()orenable_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.I have a similiar fixing for CTI driver, see:
https://lore.kernel.org/linux-arm-kernel/[email protected]/T/#maccd68b460fb8d74ccdd3504163d8f486f04178b[...]
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1763
Quoted 9 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +5
-0@@ -1781,14 +1756,52 @@ static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data)static int tmc_enable_etr_sink (struct coresight_device *csdev,enum cs_mode mode,void *data) { +struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent); +enum cs_mode old_mode; +int rc = -EINVAL ; + + scoped_guard(spinlock_irqsave, &drvdata->spinlock) {
scoped_guard(raw_spinlock_irqsave, &drvdata->spinlock){
Ok, will fix it.
Quoted 9 lines
I am concerned that the spinlock is acquired and released for several times in a single flow. It is possible to hit some corner case, e.g.,
- CPU0 acquires the lock, set sink as SYSFS mode, and releases the lock;
- CPU1 acquires the lock, detects the SYSFS mode has been set, directly bail out, then enable ETM.The problem is the sink has not been enabled yet. This leads to CPU1 to enable the tracer but prior to sink's enabling.
Yes, you are right. So I added a minor note for that. The impact of this corner case may not have been fully considered.
Quoted 20 lines
The key piont is we should ensure the mode is consistent to the hardware state. I will take a bit time for if we can have a better idea for this.
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_perf(struct coresight_device *csdev, void *data) · line 1773
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +10
-0+ old_mode =coresight_get_mode (csdev); +if (old_mode !=CS_MODE_DISABLED && old_mode != mode) +return -EBUSY ; + +if (drvdata->reading) +return -EBUSY ; + +/* In sysFS mode we can have multiple writers per sink. */ +if (old_mode ==CS_MODE_SYSFS ) { + csdev->refcnt++;I am just wandering if we can unify the "refcnt" code for both SYSFS mode and Perf mode, and as a result, we can consolidate the code cross different drivers.
Something like:
Quoted 8 lines
if (!csdev->refcnt) {
coresight_set_mode(csdev, mode);
} else {
/* The device has been configured with an incompatible mode */
if (coresight_get_mode(csdev) != mode)
return -EBUSY;
csdev->refcnt++;we need to check the mode, cannot directly increase the reference count In performance mode.
Quoted 11 lines
}Then when disable the device:
csdev->refcnt--; if (!csdev->refcnt) coresight_set_mode(csdev, CS_MODE_DISABLED);We should not check "
drvdata->reading" when enable or disable sink, as it is a flag to track if reading the trace buffer, it is irrelevant to the sink device's enabling or disabling.
In sysfs mode, it is necessary to check drvdata->reading.
Quoted 3 lines
Please verify perf mode (especially for system wide session) to avoid anything missed.
I will refactor this code and upload a new version that includes all of your suggested solutions.
Thanks,
Junhao
Quoted 45 lines
Thanks,
LeoQuoted 40 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +26
-3+return 0 ; + } + +/* +* minor note: In sysFS mode, the task1 get locked first, it setup +* etr mode to SYSFS. Then task2 get locked,it will directly return +* success even when the tmc-etr is not enabled at this moment. +* Ultimately, task1 will still successfully enable tmc-etr. +* This is a transient state and does not cause an anomaly. +*/ +coresight_set_mode (csdev, mode); + } +switch (mode) {case CS_MODE_SYSFS : -return tmc_enable_etr_sink_sysfs (csdev); + rc =tmc_enable_etr_sink_sysfs (csdev); +break ;case CS_MODE_PERF : -return tmc_enable_etr_sink_perf (csdev, data); + rc =tmc_enable_etr_sink_perf (csdev, data); +break ;default : -return -EINVAL ; + rc = -EINVAL ; } + +if (rc && old_mode != mode) { + scoped_guard(spinlock_irqsave, &drvdata->spinlock) { +coresight_set_mode (csdev, old_mode); + } + } + +return rc; }static int tmc_disable_etr_sink (struct coresight_device *csdev)Signature · 1 line
2.33.0.
PATCH v2 3/3 coresight: tmc: Decouple the perf buffer allocation from sysfs mode
In reply to Junhao HeFrom: 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 | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +9 -20
@@ -1327,9 +1327,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 *
@@ -1341,33 +1339,24 @@ alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,unsigned long 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 = (unsigned long )nr_pages <<PAGE_SHIFT ; +if (size <TMC_ETR_PERF_MIN_BUF_SIZE ) + 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
Leo Yan
reviewerRe: [PATCH v2 3/3] coresight: tmc: Decouple the perf buffer allocation from sysfs mode
On Fri, Jun 20, 2025 at 03:54:12PM +0800, Junhao He wrote:
[..]
Quoted 7 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +0
-3@@ -1341,33 +1339,24 @@ alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,unsigned long 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.
drivers/hwtracing/coresight/coresight-tmc-etr.c alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, · line 1344
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +3
-7-*/ -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 = (unsigned long )nr_pages <<PAGE_SHIFT ;
Please change the size's type to ssize_t, then:
size = nr_pages << PAGE_SHIFT;
drivers/hwtracing/coresight/coresight-tmc-etr.c alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, · line 1346
Quoted 2 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +2
-0+if (size <TMC_ETR_PERF_MIN_BUF_SIZE ) + size =TMC_ETR_PERF_MIN_BUF_SIZE ;
size = min_t(ssize_t, size, TMC_ETR_PERF_MIN_BUF_SIZE);
Quoted 5 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +1
-2/* -* 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
drivers/hwtracing/coresight/coresight-tmc-etr.c alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, · line 1357
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +2
-2+* 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 );
Do we really need to scale down buffer size for failure cases?
I would like a straightforward code:
etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL);
if (IS_ERR_OR_NULL(etr_buf))
return etr_buf;Just a side topic, we know tmc_alloc_etr_buf() should not return NULL pointer. For a sanity check, the callers (alloc_etr_buf(), tmc_etr_get_sysfs_buffer(), etc) should valid a buffer pointer with IS_ERR_OR_NULL() rather than IS_ERR(). This can be a separate patch.
Thanks,
Leo
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +0
-3return ERR_PTR (-ENOMEM ); - -done: -return etr_buf; } static struct etr_buf *Signature · 1 line
2.33.0
Re: [PATCH v2 3/3] coresight: tmc: Decouple the perf buffer allocation from sysfs mode
Quoted 23 lines · Leo Yan
On Fri, Jun 20, 2025 at 03:54:12PM +0800, Junhao He wrote:
[..]
Quoted 7 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +0
-3@@ -1341,33 +1339,24 @@ alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,unsigned long 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. drivers/hwtracing/coresight/coresight-tmc-etr.c alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, · line 1344
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +3
-7-*/ -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 = (unsigned long )nr_pages <<PAGE_SHIFT ;Please change the size's type to
ssize_t, then:
Quoted 5 lines
size = nr_pages << PAGE_SHIFT;drivers/hwtracing/coresight/coresight-tmc-etr.c alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, · line 1346
Quoted 2 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +2
-0+if (size <TMC_ETR_PERF_MIN_BUF_SIZE ) + size =TMC_ETR_PERF_MIN_BUF_SIZE ;size =
min_t(ssize_t, size, TMC_ETR_PERF_MIN_BUF_SIZE);
Sure, I will do that.
Quoted 18 lines
Quoted 5 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +1
-2/* -* 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 drivers/hwtracing/coresight/coresight-tmc-etr.c alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event, · line 1357
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +2
-2+* 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 );Do we really need to scale down buffer size for failure cases? I would like a straightforward code:
Quoted 8 lines
etr_buf = tmc_alloc_etr_buf(drvdata, size, 0, node, NULL); if (IS_ERR_OR_NULL(etr_buf)) return etr_buf;Just a side topic, we know
tmc_alloc_etr_buf()should not return NULL pointer. For a sanity check, the callers (alloc_etr_buf(),tmc_etr_get_sysfs_buffer(), etc) should valid a buffer pointer withIS_ERR_OR_NULL()rather thanIS_ERR(). This can be a separate patch.
A new patch will be added to achieve this.
Thank you for your comments.
Junhao.
Quoted 15 lines
Thanks,
LeoQuoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +0
-3return ERR_PTR (-ENOMEM ); - -done: -return etr_buf; } static struct etr_buf *Signature · 1 line
2.33.0.