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 v23 7/8] coresight: ctcu: enable byte-cntr for TMC ETR devices
Date: Wed, 18 Nov 2026 08:40:38 +0000 [thread overview]
Message-ID: <sashiko-outbox-220466@kernel.org> (raw)
In-Reply-To: <20261118-enable-byte-cntr-for-ctcu-v23-7-67fd9db6bc0e@oss.qualcomm.com>
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] coresight: ctcu: premature release of read_lock in
tmc_byte_cntr_get_data()
- [Medium] coresight: ctcu: use-after-free on buf_node in
tmc_byte_cntr_get_data()
- [Medium] coresight: ctcu: byte_cntr_data->enable leaked on generic
read fallback
- [Medium] coresight: ctcu: unordered enable and removing stores in
ctcu_platform_remove()
- [Medium] coresight: ctcu: UAF race between byte_cntr_sysfs_ops and
CTCU teardown
- [Medium] coresight: tmc: tmc_read() converts byte-cntr negative errors
into EOF
- [Low] coresight: ctcu: race in tmc_read_unprepare_byte_cntr() skips
byte-cntr start
--
commit e2d99652345f219b25f03926606cfb55a2d0509a
Author: Jie Gan <jie.gan@oss.qualcomm.com>
coresight: ctcu: enable byte-cntr for TMC ETR devices
This commit enables the CTCU byte-counter interrupt mechanism to transfer
trace data from switched ETR buffers to userspace while the ETR continues
tracing.
> 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..688fe6342c042
> --- /dev/null
> +++ b/drivers/hwtracing/coresight/coresight-ctcu-byte-cntr.c
> @@ -0,0 +1,465 @@
[ ... ]
> +static ssize_t tmc_byte_cntr_get_data(struct tmc_drvdata *etr_drvdata, loff_t pos,
> + size_t len, char **bufpp)
> +{
> + struct coresight_device *ctcu = tmc_etr_get_ctcu_device(etr_drvdata);
> + struct device *dev = &etr_drvdata->csdev->dev;
> + struct ctcu_byte_cntr *byte_cntr_data;
> + struct etr_buf *sysfs_buf;
> + atomic_t *irq_cnt;
> + ssize_t actual;
> + int ret;
> +
> + /* No byte-cntr read session, let the caller use the generic read */
> + byte_cntr_data = ctcu_get_byte_cntr(ctcu, etr_drvdata->csdev);
> + if (!byte_cntr_data || !byte_cntr_data->irq_enabled ||
> + !byte_cntr_data->reading)
> + return -EINVAL;
> +
> + /*
> + * A zero-length read must not be treated as the end of the buffer,
> + * otherwise the unread data of the current buffer would be dropped.
> + */
> + if (!len)
> + return 0;
> +
> + /*
> + * 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() unserialized in tmc_read()?
Because /dev/tmc_etrX is a character device opened with
nonseekable_open(), the VFS does not hold file->f_pos_lock across
vfs_read(). Once tmc_byte_cntr_get_data() returns, read_guard drops
byte_cntr_data->read_lock before tmc_read() copies from bufp or advances
buf_node->pos:
CPU0 CPU1
tmc_read()
tmc_byte_cntr_get_data()
lock(&byte_cntr_data->read_lock)
pos = buf_node->pos
tmc_etr_read_sysfs_buf(..., &bufp)
unlock(&byte_cntr_data->read_lock)
copy_to_user(data, bufp, actual)
tmc_read()
tmc_byte_cntr_get_data()
...
tmc_etr_update_buf_node_pos()
nd->pos += actual
tmc_read()
tmc_byte_cntr_get_data()
actual == 0:
buf_node->reading = false
buf_node = NULL
tmc_etr_switch_buf_node()
<-- ETR DMAs into bufp while
CPU0 is in copy_to_user()
tmc_etr_update_buf_node_pos()
nd->pos += actual <-- advances the newly switched buf_node->pos
[ ... ]
> +static int tmc_read_unprepare_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;
> +
> + /*
> + * Do the unprepare operation only when the byte_cntr_data->reading
> + * is truly set
> + */
> + byte_cntr_data = ctcu_get_byte_cntr(ctcu, etr_drvdata->csdev);
> + if (!byte_cntr_data || !byte_cntr_data->irq_enabled ||
> + !byte_cntr_data->reading)
> + return -EINVAL;
> +
> + 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->buf_node = NULL;
> + byte_cntr_data->reading = false;
[Severity: Medium]
Can freeing etr_buf_list before clearing byte_cntr_data->buf_node and
byte_cntr_data->reading cause a use-after-free in
tmc_byte_cntr_get_data()?
If a byte-cntr read session closes while byte_cntr_data->buf_node still
points to a partially read buffer and the trace stops before
tmc_read_unprepare_byte_cntr() acquires byte_cntr_data->spin_lock, a
concurrent open() and read() on /dev/tmc_etrX can enter
tmc_byte_cntr_get_data() while byte_cntr_data->reading is still true:
CPU0 CPU1
tmc_read_unprepare_byte_cntr()
tmc_clean_etr_buf_list()
__tmc_clean_etr_buf_list()
tmc_etr_free_sysfs_buf(buf_node->sysfs_buf)
kfree(buf_node)
disable_irq_wake()
/* trace stops */
tmc_open()
tmc_read_prepare_etr()
tmc_read_prepare_byte_cntr()
mode != CS_MODE_SYSFS -> -EINVAL
/* falls back to generic read */
drvdata->reading = true
tmc_read()
tmc_etr_get_sysfs_trace()
tmc_byte_cntr_get_data()
pos = buf_node->pos
<-- use-after-free
> + /*
> + * Complete the session with reading cleared in one critical
> + * section, so that a new session can't start in between.
> + * ctcu_platform_remove() may free byte_cntr_data after this,
> + * don't use it anymore.
> + */
> + complete_all(&byte_cntr_data->session_done);
> + }
> +
> + module_put(THIS_MODULE);
> +
> + return 0;
> +}
> +
> +static const struct tmc_sysfs_ops byte_cntr_sysfs_ops = {
> + .read_prepare = tmc_read_prepare_byte_cntr,
> + .read_unprepare = tmc_read_unprepare_byte_cntr,
> + .get_trace_data = tmc_byte_cntr_get_data,
> +};
> +
> +/* 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, when the
> + * CTCU device is being removed, or when the read of the previous trace
> + * is still in progress: the sink can't be enabled until it is finished.
> + */
> + if (!byte_cntr_data->irq_enabled || byte_cntr_data->removing ||
> + byte_cntr_data->reading)
> + return;
[Severity: Low]
Can a trace path enabled while tmc_read_unprepare_byte_cntr() is running
start the ETR sink without starting byte-cntr?
The check on byte_cntr_data->reading assumes that enabling the ETR sink
will fail while the previous byte-cntr read session is still active.
However, tmc_etr_get_sysfs_buffer() only checks
!list_empty(&drvdata->etr_buf_list), and tmc_read_unprepare_byte_cntr()
empties etr_buf_list in tmc_clean_etr_buf_list() (and calls
disable_irq_wake()) before clearing byte_cntr_data->reading:
CPU0 CPU1
tmc_read_unprepare_byte_cntr()
tmc_clean_etr_buf_list()
/* etr_buf_list is now empty */
disable_irq_wake()
coresight_enable_path_from_to()
ctcu_enable()
ctcu_byte_cntr_start()
byte_cntr_data->reading == true
return; <-- skips byte-cntr
tmc_enable_etr_sink()
tmc_etr_get_sysfs_buffer()
list_empty(&etr_buf_list) == true
/* sink enables */
byte_cntr_data->reading = false
> +
> + /*
> + * 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;
[ ... ]
> +/* Stop the byte-cntr function when the path is disabled. */
> +void ctcu_byte_cntr_stop(struct coresight_device *csdev, struct coresight_path *path)
> +{
> + struct coresight_device *sink = coresight_get_sink(path);
> + struct ctcu_byte_cntr *byte_cntr_data;
> +
> + if (coresight_get_mode(sink) == CS_MODE_SYSFS)
> + return;
[Severity: Medium]
Can byte_cntr_data->enable remain stuck at true after the trace path is
released if a session falls back to generic read?
ctcu_byte_cntr_start() runs before the ETR sink is enabled and only checks
etr_drvdata->etr_mode == ETR_MODE_RESRV. However,
tmc_etr_get_sysfs_buffer() can reuse an existing drvdata->sysfs_buf whose
mode is still ETR_MODE_RESRV (after buf_mode_preferred was changed without
changing buffer_size), or buf_mode_preferred can be set to resrv after the
trace starts. When /dev/tmc_etrX is then opened,
tmc_create_etr_buf_list() returns -EINVAL and tmc_read_prepare_etr() falls
back to generic read, setting drvdata->reading = true while
byte_cntr_data->enable remains true.
If the trace source is disabled while that generic read file descriptor is
open, tmc_disable_etr_sink() sees drvdata->reading == true and returns
-EBUSY without clearing CS_MODE_SYSFS on the sink. ctcu_byte_cntr_stop()
then sees coresight_get_mode(sink) == CS_MODE_SYSFS and returns early
without zeroing irq_ctrl_offset or clearing byte_cntr_data->enable, right
before coresight_release_path() drops the CTCU runtime PM reference. From
that point on, irq_enabled_store() permanently fails with -EBUSY and
ctcu_platform_remove() will write to the CTCU register while the CTCU is no
longer kept powered by an active path.
> diff --git a/drivers/hwtracing/coresight/coresight-ctcu-core.c b/drivers/hwtracing/coresight/coresight-ctcu-core.c
> index 739105acb0a13..94283661f10d4 100644
> --- a/drivers/hwtracing/coresight/coresight-ctcu-core.c
> +++ b/drivers/hwtracing/coresight/coresight-ctcu-core.c
[ ... ]
> @@ -244,10 +356,65 @@ static int ctcu_platform_probe(struct platform_device *pdev)
> static void ctcu_platform_remove(struct platform_device *pdev)
> {
> struct ctcu_drvdata *drvdata = platform_get_drvdata(pdev);
> + struct ctcu_byte_cntr *byte_cntr_data;
> + unsigned long flags;
> + int i;
>
> if (WARN_ON(!drvdata))
> return;
>
> + /*
> + * Signal all active byte-cntr readers to exit, then wait for them to
> + * finish before resetting the ops pointer and freeing driver data.
> + * Without this, a reader blocked in wait_event_interruptible_timeout()
> + * would access the freed ctcu_drvdata wait-queue head (use-after-free).
> + *
> + * The wait ends only when the userspace closes the tmc_etr file, so an
> + * unbind with the file left open blocks until then. Unloading the module
> + * is rejected with -EBUSY while a read session is open, so only a manual
> + * unbind can block here.
> + */
> + for (i = 0; i < ETR_MAX_NUM; i++) {
> + byte_cntr_data = &drvdata->byte_cntr_data[i];
> + /* The port was not initialized by ctcu_byte_cntr_init() */
> + if (!byte_cntr_data->irq)
> + continue;
> +
> + raw_spin_lock_irqsave(&byte_cntr_data->spin_lock, flags);
> + /*
> + * Set enable=false to signal teardown to racing readers, and
> + * removing=true to keep a new path from enabling byte-cntr again.
> + * ctcu_byte_cntr_stop() won't touch the register once enable is
> + * cleared, so disable the IRQ here. The CTCU is powered as long as
> + * enable is set, which is cleared when the path is disabled.
> + */
> + if (byte_cntr_data->enable)
> + ctcu_program_register(drvdata, 0,
> + byte_cntr_data->irq_ctrl_offset);
> + byte_cntr_data->enable = false;
> + byte_cntr_data->removing = true;
> + raw_spin_unlock_irqrestore(&byte_cntr_data->spin_lock, flags);
[Severity: Medium]
Can tmc_byte_cntr_get_data() observe byte_cntr_data->enable == false before
byte_cntr_data->removing == true and fall back to reading the unsynced live
ETR buffer?
tmc_byte_cntr_get_data() checks byte_cntr_data->enable and
READ_ONCE(byte_cntr_data->removing) without holding
byte_cntr_data->spin_lock:
CPU0 CPU1
ctcu_platform_remove()
byte_cntr_data->enable = false
tmc_etr_get_sysfs_trace()
tmc_byte_cntr_get_data()
wait_event_interruptible_timeout()
/* sees !enable */
if (byte_cntr_data->enable) ...
else {
if (READ_ONCE(removing) && ...)
/* sees !removing */
return 0;
}
/* ret == 0 falls through */
tmc_etr_read_sysfs_buf(sysfs_buf, ...)
<-- reads live unsynced ETR buffer
byte_cntr_data->removing = true
Setting byte_cntr_data->removing = true before clearing
byte_cntr_data->enable (with paired release/acquire or barrier ordering)
would ensure the reader sees removing set once it observes enable cleared.
> +
> + /* Wake up the blocked reader, then wait for the session to end */
> + wake_up_all(&byte_cntr_data->wq);
> + wait_for_completion(&byte_cntr_data->session_done);
> +
> + /*
> + * The ending session completes it with the lock held. Take the
> + * lock once more, to be sure that it has released the lock
> + * before byte_cntr_data is freed.
> + */
> + raw_spin_lock_irqsave(&byte_cntr_data->spin_lock, flags);
> + raw_spin_unlock_irqrestore(&byte_cntr_data->spin_lock, flags);
> + }
> +
> + /*
> + * No byte-cntr session is left and a new one can't start now. Stop the
> + * tmc driver from calling the byte-cntr ops before the CTCU device is
> + * unregistered and freed.
> + */
> + ctcu_byte_cntr_put_ops(drvdata);
> +
> + ctcu_remove(pdev);
[Severity: Medium]
Can an in-flight byte_cntr_sysfs_ops call race with ctcu_platform_remove()
and cause a use-after-free on ctcu and drvdata?
byte_cntr_data->session_done is initialized as completed in
ctcu_byte_cntr_init() and is only reinitialized inside
tmc_read_prepare_byte_cntr() after tmc_etr_get_ctcu_device() and
ctcu_get_byte_cntr() have already run. In addition,
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 it on every ETR open, read, and close (even during generic reads when
session_done is completed and THIS_MODULE is not pinned), while
ctcu_byte_cntr_put_ops() simply clears byte_cntr_sysfs_ops with
WRITE_ONCE() without waiting for in-flight callers:
CPU0 CPU1
tmc_read_prepare_etr()
byte_cntr_ops = READ_ONCE(byte_cntr_sysfs_ops)
tmc_read_prepare_byte_cntr()
ctcu = tmc_etr_get_ctcu_device()
ctcu_platform_remove()
wait_for_completion(&session_done)
/* already complete */
ctcu_byte_cntr_put_ops()
ctcu_remove()
coresight_unregister(csdev)
/* devres frees drvdata */
ctcu_get_byte_cntr(ctcu, ...)
dev_get_drvdata(ctcu->dev.parent)
coresight_get_in_port(etr, ctcu) <-- use-after-free
> 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;
[ ... ]
> diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c
> index cc341cdf8d8e5..d856f0a190f72 100644
> --- a/drivers/hwtracing/coresight/coresight-tmc-etr.c
> +++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c
> @@ -1178,23 +1181,44 @@ static int tmc_etr_enable_hw(struct tmc_drvdata *drvdata,
[ ... ]
> +ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
> + loff_t pos, size_t len, char **bufpp)
> +{
> + ssize_t ret;
> + const struct tmc_sysfs_ops *byte_cntr_ops = READ_ONCE(byte_cntr_sysfs_ops);
> +
> + if (byte_cntr_ops) {
> + ret = byte_cntr_ops->get_trace_data(drvdata, pos, len, bufpp);
> + /*
> + * -EINVAL means byte-cntr has no active read session. 0 means
> + * byte-cntr has no more data because the sink has stopped, the
> + * last etr_buf is read by the generic read. Anything else is
> + * the result of the byte-cntr session.
> + */
> + if (ret != -EINVAL && ret != 0)
> + return ret;
> + }
> +
> + return tmc_etr_read_sysfs_buf(drvdata->sysfs_buf, pos, len, bufpp);
> }
[Severity: Medium]
Does tmc_read() swallow the negative error codes returned by
tmc_byte_cntr_get_data() and turn them into EOF?
tmc_etr_get_sysfs_trace() propagates negative errors from
tmc_byte_cntr_get_data() such as -ERESTARTSYS (when
wait_event_interruptible_timeout() is interrupted by a signal), -EINTR,
-ENOMEM, and -ENODEV. However, the caller in tmc_read() converts all
non-positive values to 0:
drivers/hwtracing/coresight/coresight-tmc-core.c:tmc_read() {
actual = tmc_get_sysfs_trace(drvdata, *ppos, len, &bufp);
if (actual <= 0)
return 0;
When a signal arrives while userspace is blocked waiting for trace data,
read() returns 0 (EOF) instead of restarting or returning -EINTR, causing
the reader to terminate trace collection prematurely while the ETR sink is
still running.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-enable-byte-cntr-for-ctcu-v23-0-67fd9db6bc0e@oss.qualcomm.com?part=7
next prev parent reply other threads:[~2026-11-18 8:40 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 8:02 [PATCH v23 0/8] coresight: ctcu: Enable byte-cntr function for TMC ETR Jie Gan
2026-11-18 8:02 ` [PATCH v23 1/8] coresight: core: refactor ctcu_get_active_port and make it generic Jie Gan
2026-11-18 8:02 ` [PATCH v23 2/8] coresight: tmc: add create/clean functions for etr_buf_list Jie Gan
2026-11-18 8:02 ` [PATCH v23 3/8] coresight: tmc: add a function to switch the ETR to a free etr_buf Jie Gan
2026-11-18 8:20 ` sashiko-bot
2026-11-18 8:02 ` [PATCH v23 4/8] coresight: tmc: introduce tmc_sysfs_ops to wrap sysfs read operations Jie Gan
2026-11-18 8:02 ` [PATCH v23 5/8] coresight: etr: add a new function to retrieve the CTCU device Jie Gan
2026-11-18 8:02 ` [PATCH v23 6/8] dt-bindings: arm: add an interrupt property for Coresight CTCU Jie Gan
2026-11-18 8:02 ` [PATCH v23 7/8] coresight: ctcu: enable byte-cntr for TMC ETR devices Jie Gan
2026-11-18 8:40 ` sashiko-bot [this message]
2026-11-18 8:02 ` [PATCH v23 8/8] arm64: dts: qcom: lemans: add interrupts to CTCU device Jie Gan
2026-11-18 8:25 ` sashiko-bot
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-220466@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.