From: sashiko-bot@kernel.org
To: "Jie Gan" <jie.gan@oss.qualcomm.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v24 7/8] coresight: ctcu: enable byte-cntr for TMC ETR devices
Date: Fri, 20 Nov 2026 03:59:46 +0000 [thread overview]
Message-ID: <sashiko-outbox-224427@kernel.org> (raw)
In-Reply-To: <20261120-enable-byte-cntr-for-ctcu-v24-7-2d792c6b4819@oss.qualcomm.com>
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] coresight: ctcu: conflicting read sessions in
tmc_read_prepare_byte_cntr()
- [Medium] coresight: ctcu: UAF race between byte_cntr_sysfs_ops and
CTCU removal
- [Medium] coresight: ctcu: missing reading check in
ctcu_byte_cntr_start()
- [Medium] coresight: tmc: tmc_read() converts negative byte-cntr errors
into EOF
- [Medium] coresight: ctcu: premature release of read_lock in
tmc_byte_cntr_get_data()
--
commit 4905b42ec0de51a04446a6ae0f775c313734d367
Author: Jie Gan <jie.gan@oss.qualcomm.com>
coresight: ctcu: enable byte-cntr for TMC ETR devices
This commit enables the CTCU byte-cntr interrupt mechanism to stream trace
data from switched TMC ETR buffers to userspace while tracing is active.
> diff --git a/drivers/hwtracing/coresight/coresight-ctcu-byte-cntr.c b/drivers/hwtracing/coresight/coresight-ctcu-byte-cntr.c
> new file mode 100644
> index 0000000000000..f3dca66467817
> --- /dev/null
> +++ b/drivers/hwtracing/coresight/coresight-ctcu-byte-cntr.c
> @@ -0,0 +1,484 @@
[ ... ]
> +/*
> + * Drop the ops user of the CTCU device, only once for each device. It's called
> + * before the CTCU device is unregistered, so that the ops can't be used with a
> + * device which is being freed, and again by the devres action.
> + */
> +void ctcu_byte_cntr_put_ops(struct ctcu_drvdata *drvdata)
> +{
> + guard(mutex)(&byte_cntr_ops_lock);
> + if (!drvdata->byte_cntr_ops_held)
> + return;
> +
> + drvdata->byte_cntr_ops_held = false;
> + if (!--byte_cntr_ops_users)
> + tmc_etr_reset_byte_cntr_sysfs_ops();
> +}
[ ... ]
> +static struct ctcu_byte_cntr *ctcu_get_byte_cntr(struct coresight_device *ctcu,
> + struct coresight_device *etr)
> +{
> + struct ctcu_byte_cntr *byte_cntr_data;
> + struct ctcu_drvdata *drvdata;
> + int port;
> +
> + /* The ETR may not be connected to a CTCU device */
> + if (!ctcu)
> + return NULL;
> +
> + drvdata = dev_get_drvdata(ctcu->dev.parent);
> + port = coresight_get_in_port(etr, ctcu);
> + if (port < 0 || port >= ETR_MAX_NUM)
> + return NULL;
> +
> + byte_cntr_data = &drvdata->byte_cntr_data[port];
> + /* The port is not initialized by ctcu_byte_cntr_init() */
> + if (!byte_cntr_data->ctcu_drvdata)
> + return NULL;
> +
> + return byte_cntr_data;
> +}
[Severity: Medium]
Can byte_cntr_sysfs_ops callbacks race with ctcu_platform_remove() and
cause a use-after-free on ctcu and drvdata?
tmc_read_prepare_etr(), tmc_etr_get_sysfs_trace(), and
tmc_read_unprepare_etr() read byte_cntr_sysfs_ops with READ_ONCE() and
call into the callbacks without holding a lock or device reference.
Because session_done is initialized as completed and is only reinitialized
midway through tmc_read_prepare_byte_cntr(), wait_for_completion() in
ctcu_platform_remove() returns immediately when no byte-cntr session has
armed it yet, and ctcu_byte_cntr_put_ops() does not wait for in-flight
callers (or clear byte_cntr_sysfs_ops if another CTCU instance exists):
CPU0 CPU1
tmc_read_prepare_etr()
byte_cntr_ops = READ_ONCE(...)
tmc_read_prepare_byte_cntr()
ctcu = tmc_etr_get_ctcu_device()
ctcu_platform_remove()
wait_for_completion(&session_done)
ctcu_byte_cntr_put_ops()
ctcu_remove()
coresight_unregister(csdev)
ctcu_get_byte_cntr(ctcu, ...)
dev_get_drvdata(ctcu->dev.parent)
coresight_get_in_port(etr, ctcu) <-- use-after-free
The same race occurs when a generic ETR sysfs read or close invokes
get_trace_data() or read_unprepare() while the CTCU device is being
unbound.
> +/*
> + * ctcu_byte_cntr_get_data() - reads data from the deactivated and filled buffer.
> + * The byte-cntr reading work reads data from the deactivated and filled buffer.
> + * The read operation waits for a buffer to become available, either filled or
> + * upon timeout, and then reads trace data from the synced buffer.
> + */
> +static ssize_t tmc_byte_cntr_get_data(struct tmc_drvdata *etr_drvdata, loff_t pos,
> + size_t len, char **bufpp)
> +{
[ ... ]
> + /*
> + * Multiple readers of one file descriptor can't get a meaningful trace
> + * sequence, serialize them to protect the buf_node.
> + */
> + ACQUIRE(mutex_intr, read_guard)(&byte_cntr_data->read_lock);
> + ret = ACQUIRE_ERR(mutex_intr, &read_guard);
> + if (ret)
> + return ret;
[ ... ]
> + pos = byte_cntr_data->buf_node->pos;
> + sysfs_buf = byte_cntr_data->buf_node->sysfs_buf;
> + actual = tmc_etr_read_sysfs_buf(sysfs_buf, pos, len, bufpp);
> + if (actual <= 0) {
> + /* Reset buf_node upon reading is finished or failed */
> + byte_cntr_data->buf_node->reading = false;
> + byte_cntr_data->buf_node = NULL;
> +
> + /*
> + * Nothing in the buffer, waiting for the next buffer
> + * to be filled.
> + */
> + if (actual == 0)
> + goto wait_buffer;
> + }
> +
> + return actual;
> +}
[Severity: Medium]
Does releasing byte_cntr_data->read_lock on return from
tmc_byte_cntr_get_data() leave copy_to_user() and
tmc_etr_update_buf_node_pos() in tmc_read() unserialized against
concurrent readers sharing the file descriptor?
Because /dev/tmc_etrX is a character device opened with nonseekable_open(),
VFS does not hold file->f_pos_lock across tmc_read(). Once
tmc_byte_cntr_get_data() returns and drops read_lock, a second reader can
enter before the first reader updates buf_node->pos, drain the buffer, and
switch the node back to ETR DMA while the first reader is still in
copy_to_user():
CPU0 (Reader 1) CPU1 (Reader 2)
tmc_read()
tmc_byte_cntr_get_data()
lock(&byte_cntr_data->read_lock)
pos = buf_node->pos
unlock(&byte_cntr_data->read_lock)
tmc_read()
tmc_byte_cntr_get_data()
lock(&byte_cntr_data->read_lock)
/* reads same pos, drains buf */
buf_node->reading = false
buf_node = NULL
tmc_etr_switch_buf_node()
copy_to_user(data, bufp, actual) /* ETR DMA reuses sysfs_buf */
tmc_etr_update_buf_node_pos() <-- advances new buf_node->pos or *ppos
> +static int tmc_read_prepare_byte_cntr(struct tmc_drvdata *etr_drvdata)
> +{
> + struct coresight_device *ctcu = tmc_etr_get_ctcu_device(etr_drvdata);
> + struct ctcu_byte_cntr *byte_cntr_data;
> + unsigned long flags;
> + int ret = 0;
> +
> + /* byte-cntr is operating with SYSFS mode being enabled only */
> + if (coresight_get_mode(etr_drvdata->csdev) != CS_MODE_SYSFS)
> + return -EINVAL;
> +
> + byte_cntr_data = ctcu_get_byte_cntr(ctcu, etr_drvdata->csdev);
> + if (!byte_cntr_data)
> + return -EINVAL;
> +
> + /*
> + * A generic read session is in progress, don't mark byte-cntr as
> + * reading even temporarily as it misleads the reader of that session.
> + */
> + if (READ_ONCE(etr_drvdata->reading))
> + return -EBUSY;
> +
> + raw_spin_lock_irqsave(&byte_cntr_data->spin_lock, flags);
> + /*
> + * irq_enabled can only be changed with the lock held and reading
> + * cleared, so check it here to keep it stable once reading is set.
> + */
> + if (!byte_cntr_data->irq_enabled) {
> + raw_spin_unlock_irqrestore(&byte_cntr_data->spin_lock, flags);
> + return -EINVAL;
> + }
> +
> + if (byte_cntr_data->reading) {
> + raw_spin_unlock_irqrestore(&byte_cntr_data->spin_lock, flags);
> + return -EBUSY;
> + }
[ ... ]
> + reinit_completion(&byte_cntr_data->session_done);
> + byte_cntr_data->reading = true;
> + raw_spin_unlock_irqrestore(&byte_cntr_data->spin_lock, flags);
> +
> + /* Setup an available etr_buf_list for byte-cntr */
> + ret = tmc_create_etr_buf_list(etr_drvdata, 2);
> + if (ret)
> + goto err_reading;
[ ... ]
> +static int tmc_read_unprepare_byte_cntr(struct tmc_drvdata *etr_drvdata)
> +{
[ ... ]
> + tmc_clean_etr_buf_list(etr_drvdata);
> +
> + /* disable_irq_wake() may sleep on slow-bus irqchips, call it unlocked */
> + disable_irq_wake(byte_cntr_data->irq);
> +
> + /*
> + * The byte-cntr register is not accessed here: the trace path, which
> + * keeps the CTCU powered, may have been released already. It is reset
> + * when the path is disabled.
> + */
> + scoped_guard(raw_spinlock_irqsave, &byte_cntr_data->spin_lock) {
> + byte_cntr_data->reading = false;
[Severity: Medium]
Can tmc_read_prepare_byte_cntr() allow a generic read session to start
while byte_cntr_data->reading is still true?
tmc_read_prepare_byte_cntr() checks coresight_get_mode(etr_drvdata->csdev)
!= CS_MODE_SYSFS and returns -EINVAL before checking
byte_cntr_data->reading. When tmc_read_prepare_etr() receives -EINVAL, it
falls back to generic read and only checks drvdata->reading and
!list_empty(&drvdata->etr_buf_list). However, etr_buf_list is empty while
byte_cntr_data->reading is true in two sleeping windows: before
tmc_create_etr_buf_list() splices the list in
tmc_read_prepare_byte_cntr(), and after tmc_clean_etr_buf_list() empties
it in tmc_read_unprepare_byte_cntr():
CPU0 CPU1
tmc_read_prepare_byte_cntr()
byte_cntr_data->reading = true
tmc_create_etr_buf_list()
tmc_alloc_etr_buf() /* sleeps */
/* trace stops: mode != CS_MODE_SYSFS */
tmc_read_prepare_etr()
tmc_read_prepare_byte_cntr()
/* mode != CS_MODE_SYSFS -> -EINVAL */
/* etr_buf_list is empty */
etr_drvdata->reading = true
tmc_read()
tmc_etr_get_sysfs_trace()
tmc_byte_cntr_get_data()
/* sees byte_cntr_data->reading */
Checking byte_cntr_data->reading before returning -EINVAL when the sink is
not in CS_MODE_SYSFS would reject the concurrent open() with -EBUSY.
> +/* Start the byte-cntr function when the path is enabled. */
> +void ctcu_byte_cntr_start(struct coresight_device *csdev, struct coresight_path *path)
> +{
> + struct coresight_device *sink = coresight_get_sink(path);
> + struct tmc_drvdata *etr_drvdata;
> + struct ctcu_byte_cntr *byte_cntr_data;
> +
> + byte_cntr_data = ctcu_get_byte_cntr(csdev, sink);
> + if (!byte_cntr_data)
> + return;
> +
> + etr_drvdata = dev_get_drvdata(sink->dev.parent);
> + guard(raw_spinlock_irqsave)(&byte_cntr_data->spin_lock);
> + /*
> + * Don't start byte-cntr function when irq_enabled is not set, or when
> + * the CTCU device is being removed. A byte-cntr read of the previous
> + * trace still in progress doesn't matter: the sink can't be enabled while
> + * its etr_buf_list exists, and ctcu_byte_cntr_stop() stops the byte-cntr
> + * when the enabling of the path fails.
> + */
> + if (!byte_cntr_data->irq_enabled || byte_cntr_data->removing)
> + return;
> +
> + /*
> + * The etr_bufs of the reserved mode share one memory region, they
> + * can't be switched by byte-cntr.
> + */
> + if (etr_drvdata->etr_mode == ETR_MODE_RESRV)
> + return;
> +
> + /*
> + * Don't start byte-cntr if the sink is already enabled by another path,
> + * the sink is enabled after the CTCU helper. The byte-cntr is started or
> + * not with the first path, and ctcu_byte_cntr_stop() does the same check.
> + * The buffer_size may also have been changed since then while the etr_buf
> + * in use is not.
> + */
> + if (coresight_get_mode(sink) == CS_MODE_SYSFS)
> + return;
> +
> + /* A new trace starts with an empty etr_buf */
> + atomic_set(&byte_cntr_data->irq_cnt, 0);
> +
> + /*
> + * Configure the byte-cntr register to enable IRQ while the path is
> + * enabled, the CTCU is powered then. The configured size is 5% of the
> + * buffer_size.
> + */
> + ctcu_cfg_byte_cntr_reg(byte_cntr_data->ctcu_drvdata,
> + etr_drvdata->size / MAX_IRQ_CNT,
> + byte_cntr_data->irq_ctrl_offset);
> + byte_cntr_data->enable = true;
> +}
[Severity: Medium]
Should ctcu_byte_cntr_start() also bail out when byte_cntr_data->reading
is set or when coresight_get_mode(sink) != CS_MODE_DISABLED?
coresight_enable_path_from_to() runs ctcu_enable() ->
ctcu_byte_cntr_start() before enabling the ETR sink. If a byte-cntr
reader from a previous stopped trace is still open (or closing in
tmc_read_unprepare_byte_cntr() after tmc_clean_etr_buf_list() has emptied
etr_buf_list), ctcu_byte_cntr_start() resets irq_cnt to 0 and sets
byte_cntr_data->enable = true while byte_cntr_data->reading is still true:
CPU0 CPU1
/* previous trace stopped */
tmc_read()
tmc_byte_cntr_get_data()
coresight_enable_path_from_to()
ctcu_byte_cntr_start()
atomic_set(&irq_cnt, 0)
byte_cntr_data->enable = true
wait_event_interruptible_timeout()
/* sees enable == true, blocks instead of returning 0 for final buf */
If the new path is enabled right after tmc_clean_etr_buf_list() in
tmc_read_unprepare_byte_cntr(), enabling the sink also succeeds while
byte_cntr_data->reading is still true, causing a subsequent open() for the
new trace to fail with -EBUSY. In addition, checking only
coresight_get_mode(sink) == CS_MODE_SYSFS allows a sysfs enable attempt
while the sink is in CS_MODE_PERF to transiently program IRQCTRL and enable
CTCU interrupts before the sink enable fails.
> diff --git a/drivers/hwtracing/coresight/coresight-tmc-core.c b/drivers/hwtracing/coresight/coresight-tmc-core.c
> index 4b40b692be4d8..6ad09995ba87c 100644
> --- a/drivers/hwtracing/coresight/coresight-tmc-core.c
> +++ b/drivers/hwtracing/coresight/coresight-tmc-core.c
> @@ -293,7 +293,8 @@ static ssize_t tmc_read(struct file *file, char __user *data, size_t len,
> return -EFAULT;
> }
>
> - *ppos += actual;
> + if (!tmc_etr_update_buf_node_pos(drvdata, actual))
> + *ppos += actual;
> dev_dbg(&drvdata->csdev->dev, "%zu bytes copied\n", actual);
>
> return actual;
[Severity: Medium]
Does tmc_read() turn negative error codes from tmc_byte_cntr_get_data()
into EOF?
tmc_etr_get_sysfs_trace() forwards negative error codes other than -EINVAL
from byte_cntr_ops->get_trace_data() (such as -ERESTARTSYS or -EINTR when
a waiting reader is interrupted by a signal, -ENOMEM, or -ENODEV), but
tmc_read() discards them:
drivers/hwtracing/coresight/coresight-tmc-core.c:tmc_read() {
actual = tmc_get_sysfs_trace(drvdata, *ppos, len, &bufp);
if (actual <= 0)
return 0;
Any signal delivered to a process blocked in
wait_event_interruptible_timeout() or mutex_lock_interruptible() inside
tmc_byte_cntr_get_data() will cause read() to return 0 (EOF) and terminate
trace collection prematurely instead of returning the error.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-enable-byte-cntr-for-ctcu-v24-0-2d792c6b4819@oss.qualcomm.com?part=7
next prev parent reply other threads:[~2026-11-20 3:59 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 3:27 [PATCH v24 0/8] coresight: ctcu: Enable byte-cntr function for TMC ETR Jie Gan
2026-11-20 3:27 ` [PATCH v24 1/8] coresight: core: refactor ctcu_get_active_port and make it generic Jie Gan
2026-11-20 3:27 ` [PATCH v24 2/8] coresight: tmc: add create/clean functions for etr_buf_list Jie Gan
2026-11-20 3:27 ` [PATCH v24 3/8] coresight: tmc: add a function to switch the ETR to a free etr_buf Jie Gan
2026-11-20 3:27 ` [PATCH v24 4/8] coresight: tmc: introduce tmc_sysfs_ops to wrap sysfs read operations Jie Gan
2026-11-20 3:27 ` [PATCH v24 5/8] coresight: etr: add a new function to retrieve the CTCU device Jie Gan
2026-11-20 3:27 ` [PATCH v24 6/8] dt-bindings: arm: add an interrupt property for Coresight CTCU Jie Gan
2026-11-20 3:27 ` [PATCH v24 7/8] coresight: ctcu: enable byte-cntr for TMC ETR devices Jie Gan
2026-11-20 3:59 ` sashiko-bot [this message]
2026-11-20 3:27 ` [PATCH v24 8/8] arm64: dts: qcom: lemans: add interrupts to CTCU device Jie Gan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-224427@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jie.gan@oss.qualcomm.com \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.