Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups
v5 cover Coresight TMC-ETR: refactor the tmc-etr mode and some cleanups
From: Junhao He <[email protected]>
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/[email protected]/
---
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
---
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.
---
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
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(-)
Signature · 1 line
2.33.0
PATCH v5 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
@@ -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.
Signature · 1 line
2.33.0
PATCH v5 2/3 coresight: tmc-etr: Fix race condition between sysfs and perf mode
In reply to Yushan WangFrom: Yicong Yang <[email protected]>
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.
--- drivers/hwtracing/coresight/coresight-tmc-etr.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/hwtracing/coresight/coresight-tmc-etr.c +13 -0
@@ -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
Signature · 1 line
2.33.0
PATCH v5 3/3 coresight: tmc: Decouple the perf buffer allocation from sysfs mode
In reply to Yushan WangFrom: 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
@@ -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 *
Signature · 1 line
2.33.0
Quoted 9 lines · Yushan Wang
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/[email protected]/
[...]
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,
Signature · 1 line
Suzuki K Poulose <[email protected]>