mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jie Gan <jie.gan@oss.qualcomm.com>
To: Suzuki K Poulose <suzuki.poulose@arm.com>,
	Mike Leach <mike.leach@arm.com>,
	James Clark <james.clark@linaro.org>, Leo Yan <leo.yan@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Tingwei Zhang <tingwei.zhang@oss.qualcomm.com>,
	Tao Zhang <tao.zhang@oss.qualcomm.com>
Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, Jie Gan <jie.gan@oss.qualcomm.com>
Subject: [PATCH] coresight: tpda: fix race between refcnt check and register access
Date: Wed, 08 Apr 2026 21:23:44 +0800	[thread overview]
Message-ID: <20260408-fix-race-condition-issue-v1-1-9a148d07e08f@oss.qualcomm.com> (raw)

Several sysfs show/store handlers check csdev->refcnt before acquiring
the spinlock, then access hardware registers inside the lock.  This is
a race: between the check and the lock acquisition, the following can
occur on another CPU:

  CPU 0 (sysfs reader)              CPU 1 (disable path)
  ─────────────────────────────     ──────────────────────────────────
  refcnt == 1, check passes
  [waiting for spinlock]            tpda_disable(): refcnt → 0
                                    debug clock disabled
  spinlock acquired
  readl_relaxed()  ← FAULT/hang

tpda_disable() decrements csdev->refcnt under the same spinlock, so
moving the refcnt check inside the lock closes the race.

Fix all six affected sysfs handlers: global_flush_req_show/store,
syncr_mode_show, syncr_count_show, and port_flush_req_show/store.

Fixes: 8e1c358a3b0e ("coresight: tpda: add global_flush_req sysfs node")
Fixes: 33f04ead7c49 ("coresight: tpda: add logic to configure TPDA_SYNCR register")
Fixes: a089d585a7f4 ("coresight: tpda: add sysfs node to flush specific port")
Signed-off-by: Jie Gan <jie.gan@oss.qualcomm.com>
---
 drivers/hwtracing/coresight/coresight-tpda.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-tpda.c b/drivers/hwtracing/coresight/coresight-tpda.c
index 89c8f71f0aff..63a979312463 100644
--- a/drivers/hwtracing/coresight/coresight-tpda.c
+++ b/drivers/hwtracing/coresight/coresight-tpda.c
@@ -360,10 +360,10 @@ static ssize_t global_flush_req_show(struct device *dev,
 	struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
 	unsigned long val;
 
+	guard(spinlock)(&drvdata->spinlock);
 	if (!drvdata->csdev->refcnt)
 		return -EINVAL;
 
-	guard(spinlock)(&drvdata->spinlock);
 	val = readl_relaxed(drvdata->base + TPDA_CR);
 	/* read global_flush_req bit */
 	val &= TPDA_CR_FLREQ;
@@ -382,10 +382,13 @@ static ssize_t global_flush_req_store(struct device *dev,
 	if (kstrtoul(buf, 0, &val))
 		return -EINVAL;
 
-	if (!drvdata->csdev->refcnt || !val)
+	if (!val)
 		return -EINVAL;
 
 	guard(spinlock)(&drvdata->spinlock);
+	if (!drvdata->csdev->refcnt)
+		return -EINVAL;
+
 	val = readl_relaxed(drvdata->base + TPDA_CR);
 	/* set global_flush_req bit */
 	val |= TPDA_CR_FLREQ;
@@ -404,10 +407,10 @@ static ssize_t syncr_mode_show(struct device *dev,
 	struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
 	unsigned long val, syncr_val;
 
+	guard(spinlock)(&drvdata->spinlock);
 	if (!drvdata->csdev->refcnt)
 		return -EINVAL;
 
-	guard(spinlock)(&drvdata->spinlock);
 	syncr_val = readl_relaxed(drvdata->base + TPDA_SYNCR);
 	val = FIELD_GET(TPDA_SYNCR_MODE_CTRL_MASK, syncr_val);
 
@@ -440,10 +443,10 @@ static ssize_t syncr_count_show(struct device *dev,
 	struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
 	unsigned long val;
 
+	guard(spinlock)(&drvdata->spinlock);
 	if (!drvdata->csdev->refcnt)
 		return -EINVAL;
 
-	guard(spinlock)(&drvdata->spinlock);
 	val = readl_relaxed(drvdata->base + TPDA_SYNCR);
 	val &= TPDA_SYNCR_COUNT_MASK;
 
@@ -478,10 +481,10 @@ static ssize_t port_flush_req_show(struct device *dev,
 	struct tpda_drvdata *drvdata = dev_get_drvdata(dev->parent);
 	unsigned long val;
 
+	guard(spinlock)(&drvdata->spinlock);
 	if (!drvdata->csdev->refcnt)
 		return -EINVAL;
 
-	guard(spinlock)(&drvdata->spinlock);
 	val = readl_relaxed(drvdata->base + TPDA_FLUSH_CR);
 
 	return sysfs_emit(buf, "0x%lx\n", val);
@@ -498,10 +501,13 @@ static ssize_t port_flush_req_store(struct device *dev,
 	if (kstrtou32(buf, 0, &val))
 		return -EINVAL;
 
-	if (!drvdata->csdev->refcnt || !val)
+	if (!val)
 		return -EINVAL;
 
 	guard(spinlock)(&drvdata->spinlock);
+	if (!drvdata->csdev->refcnt)
+		return -EINVAL;
+
 	CS_UNLOCK(drvdata->base);
 	writel_relaxed(val, drvdata->base + TPDA_FLUSH_CR);
 	CS_LOCK(drvdata->base);

---
base-commit: f3e6330d7fe42b204af05a2dbc68b379e0ad179e
change-id: 20260408-fix-race-condition-issue-d44203254b87

Best regards,
-- 
Jie Gan <jie.gan@oss.qualcomm.com>


             reply	other threads:[~2026-04-08 13:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-08 13:23 Jie Gan [this message]
2026-04-08 13:27 ` James Clark

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=20260408-fix-race-condition-issue-v1-1-9a148d07e08f@oss.qualcomm.com \
    --to=jie.gan@oss.qualcomm.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=coresight@lists.linaro.org \
    --cc=james.clark@linaro.org \
    --cc=leo.yan@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mike.leach@arm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=tao.zhang@oss.qualcomm.com \
    --cc=tingwei.zhang@oss.qualcomm.com \
    /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

all inboxes | Powered by JetHome®