Devicetree
 help / color / mirror / Atom feed
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 RFC 6/6] coresight: tmc: add QDCP ETR sysfs support
Date: Thu, 19 Nov 2026 09:25:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-222829@kernel.org> (raw)
In-Reply-To: <20261119-introduce-qcom-qecp-feature-v1-6-b1a730d2c57a@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] coresight: tmc: refcnt leak and stray DMA resume in
  tmc_disable_etr_sink_qdcp()
- [High] coresight: tmc: bogus sync and OOB offset in
  tmc_etr_qdcp_disable_hw()
- [Medium] coresight: tmc: leak of sysfs_buf in tmc_qdcp_etr_shutdown()
  on removal
- [Medium] coresight: tmc: stale buffer metadata left by
  tmc_etr_qdcp_enable_hw()
--

commit 4c2126e70bf6a901c5097e6430bf243717b9f97e
Author: Jie Gan <jie.gan@oss.qualcomm.com>

coresight: tmc: add QDCP ETR sysfs support

This commit adds sysfs sink support for CoreSight TMC-ETR instances whose
control registers are managed by the Qualcomm Embedded Co-Processor
(QECP) over the Qualcomm Debug Control Protocol (QDCP).

> diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr-qdcp.c b/drivers/hwtracing/coresight/coresight-tmc-etr-qdcp.c
> new file mode 100644
> index 0000000000000..db94bce76f15c
> --- /dev/null
> +++ b/drivers/hwtracing/coresight/coresight-tmc-etr-qdcp.c
[ ... ]
> +static int tmc_etr_qdcp_enable_hw(struct tmc_drvdata *drvdata,
> +				  struct etr_buf *etr_buf)
> +{
> +	struct qdcp_tmc_cfg cfg = { 0 };
> +	int ret;
> +
> +	if (WARN_ON(!etr_buf))
> +		return -EINVAL;
> +
> +	if (etr_buf->mode == ETR_MODE_ETR_SG &&
> +	    WARN_ON(!tmc_etr_has_cap(drvdata, TMC_ETR_SG)))
> +		return -EINVAL;
> +
> +	if (WARN_ON(drvdata->etr_buf))
> +		return -EBUSY;
> +
> +	cfg.tmc_id = drvdata->qdcp.id;
> +	tmc_etr_qdcp_get_buffer_addr(drvdata, etr_buf, &cfg.paddr, &cfg.iaddr);
> +	cfg.size = etr_buf->size;
> +	cfg.sg_mode = etr_buf->mode == ETR_MODE_ETR_SG;
> +	cfg.ffcr = tmc_etr_default_ffcr(drvdata);
> +
> +	drvdata->etr_buf = etr_buf;
> +	ret = qdcp_tmc_etr_enable(drvdata->qdcp.provider, &cfg);
> +	/*
> +	 * A failed or timed-out request may still have been executed, so
> +	 * firmware may be writing into the buffer. Only release it once a
> +	 * stop is confirmed, else keep it pinned for a later retry.
> +	 */
> +	if (ret && !qdcp_tmc_etr_disable(drvdata->qdcp.provider, drvdata->qdcp.id))
> +		drvdata->etr_buf = NULL;
> +	return ret;
> +}

[Severity: Medium]
If qdcp_tmc_etr_enable() fails on a reused drvdata->sysfs_buf (for
instance when tmc_qdcp_etr_read_unprepare() resumes an active session)
and the recovery qdcp_tmc_etr_disable() succeeds, does clearing
drvdata->etr_buf leave stale etr_buf->len and etr_buf->full values from
the previous capture?

With drvdata->etr_buf set to NULL, a subsequent tmc_etr_qdcp_disable_hw()
returns 0 immediately without syncing or invalidating the buffer, so the
next read exposes the old len and offset over a failed or partially
overwritten buffer.

> +
> +/*
> + * drvdata->etr_buf is the buffer firmware may be writing into. It is only
> + * cleared once firmware has confirmed the stop, so a non-NULL etr_buf with the
> + * sink disabled means a stop is still pending and the buffer must not be freed
> + * or replaced.
> + */
> +static int tmc_etr_qdcp_disable_hw(struct tmc_drvdata *drvdata)
> +{
> +	int ret;
> +
> +	if (!drvdata->etr_buf)
> +		return 0;
> +
> +	ret = qdcp_tmc_etr_disable(drvdata->qdcp.provider, drvdata->qdcp.id);
> +	if (ret) {
> +		dev_err(&drvdata->csdev->dev,
> +			"failed to stop QDCP ETR: %d\n", ret);
> +		return ret;
> +	}
> +
> +	tmc_etr_sync_sysfs_buf(drvdata);
> +	drvdata->etr_buf = NULL;
> +	return 0;
> +}

[Severity: High]
Can tmc_etr_sync_sysfs_buf() compute an out-of-bounds etr_buf->offset
when tmc_etr_qdcp_disable_hw() retries a stop after a failed enable?

If qdcp_tmc_etr_enable() failed before assigning the buffer to firmware
and the immediate recovery disable also failed, drvdata->etr_buf stays
set while csdev remains in CS_MODE_DISABLED.  When a later
tmc_etr_qdcp_disable_hw() succeeds, it still calls
tmc_etr_sync_sysfs_buf() (unlike __tmc_etr_disable_hw(), which only syncs
when mode == CS_MODE_SYSFS), reading RRP and RWP values from firmware
that are 0 or from an earlier buffer.

Neither tmc_etr_qdcp_get_state() nor tmc_etr_sync_flat_buf() checks that
rrp and rwp lie within [etr_buf->hwaddr, etr_buf->hwaddr + etr_buf->size):

drivers/hwtracing/coresight/coresight-tmc-etr.c:tmc_etr_sync_flat_buf() {
	etr_buf->offset = rrp - etr_buf->hwaddr;
	if (etr_buf->full)
		etr_buf->len = etr_buf->size;
	else
		etr_buf->len = rwp - rrp;
...
}

An out-of-bounds offset underflows etr_buf->size - offset in
tmc_etr_buf_get_data(), causing tmc_etr_buf_insert_barrier_packet() or
tmc_read() to write or read past flat_buf->vaddr.

[ ... ]

> +static int tmc_disable_etr_sink_qdcp(struct coresight_device *csdev)
> +{
> +	struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
> +	int ret;
> +
> +	mutex_lock(&drvdata->qdcp.lock);
> +	if (drvdata->reading) {
> +		mutex_unlock(&drvdata->qdcp.lock);
> +		return -EBUSY;
> +	}
> +
> +	csdev->refcnt--;
> +	if (csdev->refcnt) {
> +		mutex_unlock(&drvdata->qdcp.lock);
> +		return -EBUSY;
> +	}
> +
> +	/*
> +	 * The core drops the path whatever is returned here, so nobody is left
> +	 * to retry. If the stop is not confirmed the buffer stays pinned and
> +	 * the next enable, read or removal retries it.
> +	 */
> +	ret = tmc_etr_qdcp_disable_hw(drvdata);
> +	drvdata->pid = -1;
> +	coresight_set_mode(csdev, CS_MODE_DISABLED);
> +	mutex_unlock(&drvdata->qdcp.lock);
> +
> +	if (!ret)
> +		dev_dbg(&csdev->dev, "TMC-ETR disabled\n");
> +	return ret;
> +}

[Severity: High]
What happens if a sysfs trace source is disabled while /dev/tmc_etrX is
open for reading (drvdata->reading is true)?

coresight_disable_sink() ignores the return value of sink_ops->disable()
and coresight_disable_sysfs() proceeds to tear down the path.  Returning
-EBUSY early here leaves csdev->refcnt elevated and csdev->mode set to
CS_MODE_SYSFS, so when the reader closes /dev/tmc_etrX,
tmc_qdcp_etr_read_unprepare() sees CS_MODE_SYSFS and calls
tmc_etr_qdcp_enable_hw() to restart firmware DMA with no active trace
path instead of freeing drvdata->sysfs_buf.

> +
> +void tmc_qdcp_etr_shutdown(struct tmc_drvdata *drvdata)
> +{
> +	mutex_lock(&drvdata->qdcp.lock);
> +	if (tmc_etr_qdcp_disable_hw(drvdata))
> +		dev_warn(&drvdata->csdev->dev,
> +			 "QDCP ETR did not stop, leaving trace buffer allocated\n");
> +	coresight_set_mode(drvdata->csdev, CS_MODE_DISABLED);
> +	mutex_unlock(&drvdata->qdcp.lock);
> +}

[Severity: Medium]
Does tmc_qdcp_etr_shutdown() leak drvdata->sysfs_buf when
tmc_etr_qdcp_disable_hw() succeeds?

tmc_etr_qdcp_disable_hw() only syncs the buffer and clears
drvdata->etr_buf.  Because __tmc_remove() has already deregistered
drvdata->miscdev, nothing else will free drvdata->sysfs_buf after
tmc_qdcp_etr_shutdown() returns.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119-introduce-qcom-qecp-feature-v1-0-b1a730d2c57a@oss.qualcomm.com?part=6

      reply	other threads:[~2026-11-19  9:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  9:01 [PATCH RFC 0/6] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
2026-11-19  9:01 ` [PATCH RFC 1/6] dt-bindings: soc: qcom: add QDCP provider Jie Gan
2026-11-19  9:12   ` sashiko-bot
2026-11-19  9:01 ` [PATCH RFC 2/6] soc: qcom: add QDCP firmware provider Jie Gan
2026-11-19  9:18   ` sashiko-bot
2026-11-19  9:01 ` [PATCH RFC 3/6] dt-bindings: arm: qcom: add QDCP STM source Jie Gan
2026-11-19  9:01 ` [PATCH RFC 4/6] coresight: " Jie Gan
2026-11-19  9:28   ` sashiko-bot
2026-11-19  9:01 ` [PATCH RFC 5/6] dt-bindings: arm: qcom: add QDCP TMC ETR sink Jie Gan
2026-11-19  9:01 ` [PATCH RFC 6/6] coresight: tmc: add QDCP ETR sysfs support Jie Gan
2026-11-19  9:25   ` sashiko-bot [this message]

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-222829@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox