mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: James Clark <james.clark@linaro.org>
To: Suzuki K Poulose <suzuki.poulose@arm.com>,
	 Mike Leach <mike.leach@arm.com>, Leo Yan <leo.yan@arm.com>,
	 Suyash Mahar <smahar@meta.com>,
	Yeoreum Yun <yeoreum.yun@arm.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Qi Liu <liuqi115@huawei.com>, Junhao He <hejunhao3@huawei.com>,
	 coresight@lists.linaro.org,
	linux-arm-kernel@lists.infradead.org,
	 linux-kernel@vger.kernel.org,
	James Clark <james.clark@linaro.org>,
	 Jonathan Cameron <jic23@kernel.org>,
	sashiko-bot <sashiko-bot@kernel.org>
Subject: [PATCH v3 3/8] coresight: etm-perf: Flush workqueue before unloading module
Date: Tue, 28 Jul 2026 16:00:15 +0100	[thread overview]
Message-ID: <20260728-james-cs-multiple-per-threads-v3-3-6aee7579f1dc@linaro.org> (raw)
In-Reply-To: <20260728-james-cs-multiple-per-threads-v3-0-6aee7579f1dc@linaro.org>

The work queue is never flushed on module unload, so pending work items
can run after coresight-config is cleaned up or the .text section is
unloaded. The global workqueue also can't be flushed manually, so change
it to a local workqueue and flush it on exit.

The init function needs to be rearranged to fix a similar problem. The
Perf PMU can be used as soon as registration succeeds, so move it as
late as possible so that the only failure that can follow is coresight-
config. Failing to register coresight-config means no flush dependency
between coresight-config and the Perf PMU will exist, so it doesn't need
to be done.

Assisted-by: Codex:GPT-5.6-Sol
Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Fixes: 0bcbf2e30ff2 ("coresight: etm-perf: new PMU driver for ETM tracers")
Signed-off-by: James Clark <james.clark@linaro.org>
---
 drivers/hwtracing/coresight/coresight-core.c     | 28 ++++++++++++++----------
 drivers/hwtracing/coresight/coresight-etm-perf.c | 22 +++++++++++++++----
 drivers/hwtracing/coresight/coresight-etm-perf.h |  1 +
 3 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtracing/coresight/coresight-core.c
index f7b1308a759c..5e653b08a83d 100644
--- a/drivers/hwtracing/coresight/coresight-core.c
+++ b/drivers/hwtracing/coresight/coresight-core.c
@@ -2094,31 +2094,31 @@ static int __init coresight_init(void)
 	if (ret)
 		return ret;
 
-	ret = etm_perf_init();
-	if (ret)
-		goto exit_bus_unregister;
-
 	/* Register function to be called for panic */
 	ret = atomic_notifier_chain_register(&panic_notifier_list,
 					     &coresight_notifier);
 	if (ret)
-		goto exit_perf;
+		goto exit_bus_unregister;
 
-	/* initialise the coresight syscfg API */
-	ret = cscfg_init();
+	ret = coresight_pm_setup();
 	if (ret)
 		goto exit_notifier;
 
-	ret = coresight_pm_setup();
+	ret = etm_perf_init();
+	if (ret)
+		goto exit_pm;
+
+	/* initialise the coresight syscfg API */
+	ret = cscfg_init();
 	if (!ret)
 		return 0;
 
-	cscfg_exit();
+	etm_perf_exit();
+exit_pm:
+	coresight_pm_cleanup();
 exit_notifier:
 	atomic_notifier_chain_unregister(&panic_notifier_list,
 					     &coresight_notifier);
-exit_perf:
-	etm_perf_exit();
 exit_bus_unregister:
 	bus_unregister(&coresight_bustype);
 	return ret;
@@ -2127,6 +2127,12 @@ static int __init coresight_init(void)
 static void __exit coresight_exit(void)
 {
 	coresight_pm_cleanup();
+	/*
+	 * Flush Perf workqueue before the rest of Coresight is torn down
+	 * because work items touch coresight-config and also require the .text
+	 * to remain loaded.
+	 */
+	etm_perf_flush_workqueue();
 	cscfg_exit();
 	atomic_notifier_chain_unregister(&panic_notifier_list,
 					     &coresight_notifier);
diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c
index 09b21a711a87..7fb5c3d18bd5 100644
--- a/drivers/hwtracing/coresight/coresight-etm-perf.c
+++ b/drivers/hwtracing/coresight/coresight-etm-perf.c
@@ -27,6 +27,7 @@
 #include "coresight-trace-id.h"
 
 static struct pmu etm_pmu;
+static struct workqueue_struct *etm_free_wq;
 static bool etm_perf_up;
 
 /*
@@ -292,7 +293,7 @@ static void etm_free_aux(void *data)
 {
 	struct etm_event_data *event_data = data;
 
-	schedule_work(&event_data->work);
+	queue_work(etm_free_wq, &event_data->work);
 }
 
 /*
@@ -1034,6 +1035,10 @@ int __init etm_perf_init(void)
 {
 	int ret;
 
+	etm_free_wq = alloc_workqueue("coresight_etm_free", WQ_UNBOUND, 0);
+	if (!etm_free_wq)
+		return -ENOMEM;
+
 	etm_pmu.capabilities		= (PERF_PMU_CAP_EXCLUSIVE |
 					   PERF_PMU_CAP_ITRACE |
 					   PERF_PMU_CAP_AUX_PAUSE);
@@ -1054,13 +1059,22 @@ int __init etm_perf_init(void)
 	etm_pmu.module			= THIS_MODULE;
 
 	ret = perf_pmu_register(&etm_pmu, CORESIGHT_ETM_PMU_NAME, -1);
-	if (ret == 0)
-		etm_perf_up = true;
+	if (ret) {
+		destroy_workqueue(etm_free_wq);
+		return ret;
+	}
 
-	return ret;
+	etm_perf_up = true;
+	return 0;
 }
 
 void etm_perf_exit(void)
 {
 	perf_pmu_unregister(&etm_pmu);
+	destroy_workqueue(etm_free_wq);
+}
+
+void etm_perf_flush_workqueue(void)
+{
+	flush_workqueue(etm_free_wq);
 }
diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.h b/drivers/hwtracing/coresight/coresight-etm-perf.h
index 24d929428633..86e259cc1adf 100644
--- a/drivers/hwtracing/coresight/coresight-etm-perf.h
+++ b/drivers/hwtracing/coresight/coresight-etm-perf.h
@@ -116,5 +116,6 @@ int etm_perf_add_symlink_cscfg(struct device *dev,
 void etm_perf_del_symlink_cscfg(struct cscfg_config_desc *config_desc);
 int __init etm_perf_init(void);
 void etm_perf_exit(void);
+void etm_perf_flush_workqueue(void);
 
 #endif

-- 
2.34.1


  parent reply	other threads:[~2026-07-28 15:01 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28 15:00 [PATCH v3 0/8] coresight: Prevent per-thread events from sharing a sink James Clark
2026-07-28 15:00 ` [PATCH v3 1/8] coresight: tmc-etr: Don't stop Perf cleanup for active sysfs reads James Clark
2026-08-12 16:45   ` Leo Yan
2026-08-13  9:02     ` James Clark
2026-07-28 15:00 ` [PATCH v3 2/8] coresight: configfs: Don't assume active until cscfg_mgr is set James Clark
2026-08-13  9:09   ` Leo Yan
2026-08-13  9:24     ` James Clark
2026-07-28 15:00 ` James Clark [this message]
2026-08-13 14:19   ` [PATCH v3 3/8] coresight: etm-perf: Flush workqueue before unloading module Leo Yan
2026-08-14  8:55     ` James Clark
2026-07-28 15:00 ` [PATCH v3 4/8] coresight: tmc-etr: Prevent per-thread events from sharing a sink James Clark
2026-08-13 16:05   ` Leo Yan
2026-08-14  9:09     ` James Clark
2026-08-19  8:45       ` Leo Yan
2026-08-20 11:09         ` James Clark
2026-08-20 11:27           ` James Clark
2026-08-25 17:32           ` Leo Yan
2026-08-26  8:40             ` James Clark
2026-10-09 14:27         ` James Clark
2026-07-28 15:00 ` [PATCH v3 5/8] coresight: tmc-etr: Use session ID for buffer ownership James Clark
2026-07-28 15:00 ` [PATCH v3 6/8] coresight: tmc-etf: Prevent per-thread events from sharing a sink James Clark
2026-07-28 15:00 ` [PATCH v3 7/8] coresight: etb10: " James Clark
2026-07-28 15:00 ` [PATCH v3 8/8] coresight: ultrasoc-smb: " 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=20260728-james-cs-multiple-per-threads-v3-3-6aee7579f1dc@linaro.org \
    --to=james.clark@linaro.org \
    --cc=coresight@lists.linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hejunhao3@huawei.com \
    --cc=jic23@kernel.org \
    --cc=leo.yan@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liuqi115@huawei.com \
    --cc=mike.leach@arm.com \
    --cc=sashiko-bot@kernel.org \
    --cc=smahar@meta.com \
    --cc=suzuki.poulose@arm.com \
    --cc=yeoreum.yun@arm.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®