Coresight TMC-ETR some bugfixes and cleanups
cover Coresight TMC-ETR some bugfixes and cleanups
From: Yicong Yang <[email protected]>
Fix two race issues found by using TMC-ETR. Two cleanups found when debugging the issues. Patch 1/4 is suggested by Suzuki in [1] and tested to solve the original issue.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Suzuki K Poulose (1):
coresight: tmc: Don't reallocate the sysfs_buffer if it's in use
Yicong Yang (3):
coresight: tmc: Add missing doc of tmc_drvdata::reading
coresight: tmc-etr: Fix race condition between sysfs and perf mode
coresight: tmc-etr: Decouple the perf buffer allocation from sysfs
mode
.../hwtracing/coresight/coresight-tmc-etr.c | 65 +++++++++++++------ drivers/hwtracing/coresight/coresight-tmc.h | 1 + 2 files changed, 45 insertions(+), 21 deletions(-)
Signature · 1 line
2.24.0
PATCH 1/4 coresight: tmc: Don't reallocate the sysfs_buffer if it's in use
From: Suzuki K Poulose <[email protected]>
Enable the trace in below steps will crash the kernel by NULL pointer dereferencing:
echo 1 > /sys/bus/coresight/devices/tmc_etr0/enable_sink echo 1 > /sys/bus/coresight/devices/etm0/enable_source echo 0x400000 > /sys/bus/coresight/devices/tmc_etr0/buffer_size echo 1 > /sys/bus/coresight/devices/etm2/enable_source
dd if=/dev/tmc_etr0 of=test_etm_sysfs_etr_030.data
The call trace will be like:
WARNING: CPU: 39 PID: 8586 at drivers/hwtracing/coresight/coresight-tmc-etr.c:1123 __tmc_etr_disable_hw+0x108/0x140 [coresight_tmc] [...] Call trace: __tmc_etr_disable_hw+0x108/0x140 [coresight_tmc] tmc_read_prepare_etr+0xc0/0xd0 [coresight_tmc] tmc_open+0x60/0xa0 [coresight_tmc] misc_open+0x11c/0x170 chrdev_open+0xcc/0x2b0 do_dentry_open+0x140/0x4e0 vfs_open+0x34/0xf8 path_openat+0x2b0/0xf58 do_filp_open+0x8c/0x148 do_sys_openat2+0xb8/0xe8 __arm64_sys_openat+0x70/0xc0 el0_svc_common.constprop.0+0x64/0x148 do_el0_svc+0x24/0x38 el0_svc+0x40/0x140 el0t_64_sync_handler+0xc0/0xc8 el0t_64_sync+0x1a4/0x1a8 ---[ end trace 0000000000000000 ]--- Unable to handle kernel NULL pointer dereference at virtual address 0000000000000028 [...] Call trace: tmc_etr_get_sysfs_trace+0x10/0x80 [coresight_tmc] vfs_read+0xcc/0x310 ksys_read+0x74/0x108 __arm64_sys_read+0x24/0x38 el0_svc_common.constprop.0+0x64/0x148 do_el0_svc+0x24/0x38 el0_svc+0x40/0x140
Due to the buffer size changed, the buffer will be reallocated in tmc_etr_get_sysfs_buffer() when the second source enabled. At trace end tmc_etr_sync_sysfs_buf() will reset the drvdata->sysfs_buf and trigger the later NULL pointer dereference when reading out the data. Fix this by checking whether the buffer's in use before reallocate a now one in tmc_etr_get_sysfs_buffer().
--- drivers/hwtracing/coresight/coresight-tmc-etr.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +5 -1
@@ -1178,7 +1178,11 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)*/ spin_lock_irqsave (&drvdata->spinlock, flags); sysfs_buf =READ_ONCE (drvdata->sysfs_buf); -if (!sysfs_buf || (sysfs_buf->size != drvdata->size)) { + +/* Don't reallocate the buffer if it's already in use */ +if (!sysfs_buf || + ((sysfs_buf->size != drvdata->size) && +coresight_get_mode (csdev) !=CS_MODE_SYSFS )) {spin_unlock_irqrestore (&drvdata->spinlock, flags);/* Allocate memory with the locks released */
Signature · 1 line
2.24.0
PATCH 2/4 coresight: tmc: Add missing doc of tmc_drvdata::reading
In reply to Yicong YangFrom: 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
@@ -174,6 +174,7 @@ struct etr_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 * @buf: Snapshot of the trace data for ETF/ETB. * @etr_buf: details of buffer used in TMC-ETR * @len: size of the available trace for ETF/ETB.
Signature · 1 line
2.24.0
PATCH 3/4 coresight: tmc-etr: Fix race condition between sysfs and perf mode
In reply to Yicong YangFrom: 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.
--- .../hwtracing/coresight/coresight-tmc-etr.c | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+)
drivers/hwtracing/coresight/coresight-tmc-etr.c +30 -0
@@ -1230,6 +1230,36 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)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 +* 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) +* +* So check here before continue. +*/ +if (coresight_get_mode (csdev) ==CS_MODE_PERF ) { + drvdata->sysfs_buf =NULL ; +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.24.0
Suzuki K Poulose
maintainerRe: [PATCH 3/4] coresight: tmc-etr: Fix race condition between sysfs and perf mode
Hi Yicong
Quoted 27 lines · Yicong Yang
From: Yicong Yang <[email protected]>
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 ]---
Quoted 4 lines
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.
Thanks for the fix. Some minor comments below.
It needs a Fixes tag.
Quoted 33 lines
Signed-off-by Yicong Yang <[email protected]>--- .../hwtracing/coresight/coresight-tmc-etr.c | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+)drivers/hwtracing/coresight/coresight-tmc-etr.c +19
-0@@ -1230,6 +1230,36 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)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 +* 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) +*
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev) · line 1261
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +10
-0+* So check here before continue. +*/ +if (coresight_get_mode (csdev) ==CS_MODE_PERF ) { + drvdata->sysfs_buf =NULL ; +spin_unlock_irqrestore (&drvdata->spinlock, flags); + +/* Free allocated memory out side of the spinlock */ +tmc_etr_free_sysfs_buf (sysfs_buf); +return -EBUSY ; + }
With this in place, I think we should remove the check for CS_MODE_PERF in get_etr_sysfs_buf() to avoid confusion (which I believe opened up this race)
Suzuki
Quoted 4 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +1
-0+/* * 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
Re: [PATCH 3/4] coresight: tmc-etr: Fix race condition between sysfs and perf mode
Quoted 74 lines · Suzuki K Poulose
Hi Yicong
Quoted 27 lines · Yicong Yang
From: Yicong Yang <[email protected]>
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 ]---Quoted 4 lines
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.
Thanks for the fix. Some minor comments below.
It needs a Fixes tag.
Quoted 35 lines
Signed-off-by Yicong Yang <[email protected]>---
.../hwtracing/coresight/coresight-tmc-etr.c | 30 +++++++++++++++++++1 file changed, 30 insertions(+)drivers/hwtracing/coresight/coresight-tmc-etr.c +0
-0@@ -1230,6 +1230,36 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev) 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 + * 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)
+ *
+ * So check here before continue.
+ */
+ if (coresight_get_mode(csdev)==CS_MODE_PERF) {
+drvdata->sysfs_buf= NULL;
Quoted 9 lines
Quoted 6 lines
+
spin_unlock_irqrestore(&drvdata->spinlock, flags); +
+ /* Free allocated memory out side of the spinlock */ +tmc_etr_free_sysfs_buf(sysfs_buf);
+ return-EBUSY;
+ }With this in place, I think we should remove the check for
CS_MODE_PERFinget_etr_sysfs_buf()to avoid confusion (which I believe opened up this race)
ok, considering that maybe we can optimize it a bit more in the tmc_etr_get_sysfs_buffer(). Check 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.
Thanks.
James Clark
reviewerRe: [PATCH 3/4] coresight: tmc-etr: Fix race condition between sysfs and perf mode
In reply to Suzuki K PouloseQuoted 87 lines · Suzuki K Poulose
Hi Yicong
Quoted 27 lines · Yicong Yang
From: Yicong Yang <[email protected]>
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 ]---Quoted 4 lines
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.
Thanks for the fix. Some minor comments below.
It needs a Fixes tag.
Quoted 37 lines
Signed-off-by Yicong Yang <[email protected]>---
.../hwtracing/coresight/coresight-tmc-etr.c | 30 +++++++++++++++++++1 file changed, 30 insertions(+)drivers/hwtracing/coresight/coresight-tmc-etr.c +2
-0@@ -1230,6 +1230,36 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev) 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 + * 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)
+ *
+ * So check here before continue.
+ */Quoted 8 lines
+ if (
coresight_get_mode(csdev)==CS_MODE_PERF) { +drvdata->sysfs_buf= NULL;
+spin_unlock_irqrestore(&drvdata->spinlock, flags); +
+ /* Free allocated memory out side of the spinlock */ +tmc_etr_free_sysfs_buf(sysfs_buf);
+ return-EBUSY;
+ }With this in place, I think we should remove the check for
CS_MODE_PERFinget_etr_sysfs_buf()to avoid confusion (which I believe opened up this race)
Quoted 2 lines
Suzuki
Is it not simpler to set the mode to SYSFS before allocating the buffer in the first place? Then we don't need to free if it races and can't get into the intermediate state where it's a half initialized sysfs mode. The lock doesn't need to be held the whole time, just when setting the mode.
Or maybe to make it more consistent with etm4_enable() use coresight_take_mode() outside of the lock.
And then also clean up the perf mode check in get_etr_sysfs_buf().
drivers/hwtracing/coresight/coresight-tmc-etr.c +7 -3
@@ -1219,13 +1219,17 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)int ret =0 ;unsigned long flags;struct tmc_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent); -struct etr_buf *sysfs_buf =tmc_etr_get_sysfs_buffer (csdev); +struct etr_buf *sysfs_buf; +spin_lock_irqsave (&drvdata->spinlock, flags); + +if (coresight_get_mode (csdev) ==CS_MODE_PERF ) +return -EBUSY ; + + sysfs_buf =tmc_etr_get_sysfs_buffer (csdev);if (IS_ERR (sysfs_buf))return PTR_ERR (sysfs_buf); -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
Suzuki K Poulose
maintainerRe: [PATCH 3/4] coresight: tmc-etr: Fix race condition between sysfs and perf mode
Quoted 107 lines · James Clark
Quoted 87 lines · Suzuki K Poulose
Hi Yicong
Quoted 27 lines · Yicong Yang
From: Yicong Yang <[email protected]>
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 ]---Quoted 4 lines
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.
Thanks for the fix. Some minor comments below.
It needs a Fixes tag.
Quoted 37 lines
Signed-off-by Yicong Yang <[email protected]>---
.../hwtracing/coresight/coresight-tmc-etr.c | 30 +++++++++++++++++++1 file changed, 30 insertions(+)drivers/hwtracing/coresight/coresight-tmc-etr.c +2
-0@@ -1230,6 +1230,36 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev) 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 + * 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)
+ *
+ * So check here before continue.
+ */Quoted 8 lines
+ if (
coresight_get_mode(csdev)==CS_MODE_PERF) { +drvdata->sysfs_buf= NULL;
+spin_unlock_irqrestore(&drvdata->spinlock, flags); +
+ /* Free allocated memory out side of the spinlock */ +tmc_etr_free_sysfs_buf(sysfs_buf);
+ return-EBUSY;
+ }With this in place, I think we should remove the check for
CS_MODE_PERFinget_etr_sysfs_buf()to avoid confusion (which I believe opened up this race)Quoted 2 lines
Suzuki
Is it not simpler to set the mode to SYSFS before allocating the buffer in the first place? Then we don't need to free if it races and can't get into the intermediate state where it's a half initialized sysfs mode. The lock doesn't need to be held the whole time, just when setting the mode.
Or maybe to make it more consistent with
etm4_enable()usecoresight_take_mode()outside of the lock.And then also clean up the perf mode check in
get_etr_sysfs_buf().drivers/hwtracing/coresight/coresight-tmc-etr.c +0
-0@@ -1219,13 +1219,17 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev) int ret = 0; unsigned long flags;
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_sysfs(struct · line 1230
Quoted 10 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +7
-1struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); - struct etr_buf *sysfs_buf = tmc_etr_get_sysfs_buffer(csdev); + struct etr_buf *sysfs_buf; + spin_lock_irqsave(&drvdata->spinlock, flags); + + if (coresight_get_mode(csdev) == CS_MODE_PERF) + return -EBUSY; + + sysfs_buf = tmc_etr_get_sysfs_buffer(csdev);
^^ This would try to loack the spinlock again. Or we should explicitly assert that the spinlock is held in the tmc_etr_get_sysfs_buffer() and release it and then allocate the buffer. Which again opens up a race for a PERF session to take it over ?
Suzuki
Quoted 12 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c +0
-2if (IS_ERR(sysfs_buf)) return PTR_ERR(sysfs_buf); - 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
Suzuki K Poulose
maintainerRe: [PATCH 3/4] coresight: tmc-etr: Fix race condition between sysfs and perf mode
Quoted 116 lines · Suzuki K Poulose
Quoted 115 lines · James Clark
Quoted 88 lines · Suzuki K Poulose
Hi Yicong
Quoted 27 lines · Yicong Yang
From: Yicong Yang <[email protected]>
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 ]---Quoted 4 lines
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.
Thanks for the fix. Some minor comments below.
It needs a Fixes tag.
Quoted 40 lines
Signed-off-by Yicong Yang <[email protected]>---
.../hwtracing/coresight/coresight-tmc-etr.c | 30 +++++++++++++++
++++1 file changed, 30 insertions(+)drivers/hwtracing/coresight/coresight-tmc-etr.c +2
-0@@ -1230,6 +1230,36 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev) 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
+ * 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-Quoted 1 line
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)
+ *
+ * So check here before continue.
+ */Quoted 8 lines
+ if (
coresight_get_mode(csdev)==CS_MODE_PERF) { +drvdata->sysfs_buf= NULL;
+spin_unlock_irqrestore(&drvdata->spinlock, flags); +
+ /* Free allocated memory out side of the spinlock */ +tmc_etr_free_sysfs_buf(sysfs_buf);
+ return-EBUSY;
+ }Quoted 4 lines
With this in place, I think we should remove the check for
CS_MODE_PERFinget_etr_sysfs_buf()to avoid confusion (which I believe opened up this race)Suzuki
Is it not simpler to set the mode to SYSFS before allocating the buffer in the first place? Then we don't need to free if it races and can't get into the intermediate state where it's a half initialized sysfs mode. The lock doesn't need to be held the whole time, just when setting the mode.
Or maybe to make it more consistent with
etm4_enable()usecoresight_take_mode()outside of the lock.And then also clean up the perf mode check in
get_etr_sysfs_buf().drivers/hwtracing/coresight/coresight-tmc-etr.c +0
-0@@ -1219,13 +1219,17 @@ static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev) int ret = 0; unsigned long flags;struct
tmc_drvdata*drvdata = dev_get_drvdata(csdev-Quoted 1 line
dev.parent);
- struct
etr_buf*sysfs_buf=tmc_etr_get_sysfs_buffer(csdev); + structetr_buf*sysfs_buf;
Quoted 9 lines
drivers/hwtracing/coresight/coresight-tmc-etr.c static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev) · line 1229
Quoted 6 lines
+
spin_lock_irqsave(&drvdata->spinlock, flags);drivers/hwtracing/coresight/coresight-tmc-etr.c +5
-0+ + if (coresight_get_mode(csdev) == CS_MODE_PERF) + return -EBUSY; + + sysfs_buf = tmc_etr_get_sysfs_buffer(csdev);^^ This would try to loack the spinlock again. Or we should explicitly assert that the spinlock is held in the
tmc_etr_get_sysfs_buffer()and release it and then allocate the buffer. Which again opens up a race for a PERF session to take it over ?
Ah, no, the mode will prevent PERF mode taking over.
Suzuki
Quoted 15 lines
Suzuki
Quoted 10 lines
if (
IS_ERR(sysfs_buf))
returnPTR_ERR(sysfs_buf);-
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
PATCH 4/4 coresight: tmc-etr: Decouple the perf buffer allocation from sysfs mode
In reply to Yicong YangFrom: 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
@@ -1307,9 +1307,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 *
@@ -1321,33 +1319,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.24.0