From: Leo Yan <leo.yan@arm.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
James Clark <james.clark@linaro.org>,
Mike Leach <mike.leach@arm.com>,
Anshuman Khandual <anshuman.khandual@arm.com>,
Yeoreum Yun <yeoreum.yun@arm.com>,
Jonathan Corbet <corbet@lwn.net>, Suyash Mahar <smahar@meta.com>,
Amir Ayupov <aaupov@fb.com>
Cc: Ingo Molnar <mingo@kernel.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
coresight@lists.linaro.org,
linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org,
Leo Yan <leo.yan@arm.com>
Subject: [PATCH 09/25] coresight: perf: Improve hardware state transitions
Date: Tue, 15 Sep 2026 16:48:55 +0100 [thread overview]
Message-ID: <20260915-arm_cs_support_aux_sample-v1-9-35cf6f297d20@arm.com> (raw)
In-Reply-To: <20260915-arm_cs_support_aux_sample-v1-0-35cf6f297d20@arm.com>
Keep hw.state consistent with event activation and AUX buffer updates.
Initialize events as stopped and up to date, clear both flags after a
successful start, and set UPTODATE after committing an AUX buffer
update. On resume, clear UPTODATE only on success. Preserve state on
resume failure and ignore duplicate stops.
Add helpers for setting and clearing state bits. Use READ_ONCE() and
WRITE_ONCE() to prevent compiler merging or refetching of state
accesses.
Assisted-by: Codex:gpt-6
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
drivers/hwtracing/coresight/coresight-etm-perf.c | 58 ++++++++++++++++--------
1 file changed, 40 insertions(+), 18 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c
index e3c9d3cf84c502f357a430bc3bfa854a776618f1..f66c99b26e1862ab04105704276c901b21728207 100644
--- a/drivers/hwtracing/coresight/coresight-etm-perf.c
+++ b/drivers/hwtracing/coresight/coresight-etm-perf.c
@@ -150,6 +150,20 @@ etm_event_cpu_path(struct etm_event_data *data, int cpu)
return *etm_event_cpu_path_ptr(data, cpu);
}
+static inline void etm_event_set_hw_state(struct perf_event *event, int mask)
+{
+ struct hw_perf_event *hwc = &event->hw;
+
+ WRITE_ONCE(hwc->state, READ_ONCE(hwc->state) | mask);
+}
+
+static inline void etm_event_clear_hw_state(struct perf_event *event, int mask)
+{
+ struct hw_perf_event *hwc = &event->hw;
+
+ WRITE_ONCE(hwc->state, READ_ONCE(hwc->state) & ~mask);
+}
+
static void etm_event_read(struct perf_event *event) {}
static int etm_addr_filters_alloc(struct perf_event *event)
@@ -520,6 +534,7 @@ static void *etm_setup_aux(struct perf_event *event, void **pages,
static int etm_event_resume(struct etm_ctxt *ctxt)
{
struct perf_output_handle *handle = &ctxt->handle;
+ struct perf_event *event = handle->event;
struct coresight_device *source;
struct coresight_path *path;
int ret;
@@ -536,10 +551,13 @@ static int etm_event_resume(struct etm_ctxt *ctxt)
return 0;
ret = coresight_resume_source(source);
- if (ret < 0)
+ if (ret < 0) {
dev_err(&source->dev, "Failed to resume ETM event.\n");
+ return ret;
+ }
- return ret;
+ etm_event_clear_hw_state(event, PERF_HES_UPTODATE);
+ return 0;
}
static void etm_event_start(struct perf_event *event, int flags)
@@ -553,8 +571,7 @@ static void etm_event_start(struct perf_event *event, int flags)
u64 hw_id;
if (flags & PERF_EF_RESUME) {
- if (etm_event_resume(ctxt) < 0)
- goto fail;
+ WARN_ON_ONCE(etm_event_resume(ctxt));
return;
}
@@ -583,7 +600,7 @@ static void etm_event_start(struct perf_event *event, int flags)
*/
if (!cpumask_test_cpu(cpu, &event_data->mask)) {
perf_aux_output_end(handle, 0);
- event->hw.state = 0;
+ etm_event_clear_hw_state(event, PERF_HES_STOPPED);
return;
}
@@ -621,7 +638,7 @@ static void etm_event_start(struct perf_event *event, int flags)
}
/* Tell the perf core the event is alive */
- event->hw.state = 0;
+ etm_event_clear_hw_state(event, PERF_HES_STOPPED | PERF_HES_UPTODATE);
/* Save the event_data for this ETM */
WRITE_ONCE(ctxt->event_data, event_data);
return;
@@ -639,11 +656,12 @@ static void etm_event_start(struct perf_event *event, int flags)
perf_aux_output_end(handle, 0);
}
fail:
- event->hw.state = PERF_HES_STOPPED;
+ etm_event_set_hw_state(event, PERF_HES_STOPPED);
return;
}
-static void etm_event_update_buffer(struct perf_output_handle *handle,
+static void etm_event_update_buffer(struct perf_event *event,
+ struct perf_output_handle *handle,
struct etm_event_data *event_data,
struct coresight_device *sink,
int mode)
@@ -668,6 +686,7 @@ static void etm_event_update_buffer(struct perf_output_handle *handle,
size = sink_ops(sink)->update_buffer(sink, handle,
event_data->snk_config);
perf_aux_output_end(handle, size);
+ etm_event_set_hw_state(event, PERF_HES_UPTODATE);
return;
}
@@ -704,7 +723,8 @@ static void etm_event_pause(struct coresight_path *path,
return;
event_data = READ_ONCE(ctxt->event_data);
- etm_event_update_buffer(handle, event_data, sink, PERF_EF_UPDATE);
+ etm_event_update_buffer(event, handle, event_data, sink,
+ PERF_EF_UPDATE);
/* Prepare the handle for resuming trace */
perf_aux_output_begin(handle, event);
@@ -716,8 +736,13 @@ static void etm_event_stop(struct perf_event *event, int mode)
struct etm_ctxt *ctxt = this_cpu_ptr(&etm_ctxt);
struct perf_output_handle *handle = &ctxt->handle;
struct coresight_path *path = etm_event_get_ctxt_path(ctxt);
+ struct hw_perf_event *hwc = &event->hw;
struct etm_event_data *event_data;
+ /* If we're already stopped, then nothing to do */
+ if (READ_ONCE(hwc->state) & PERF_HES_STOPPED)
+ return;
+
if (mode & PERF_EF_PAUSE)
return etm_event_pause(path, event, ctxt);
@@ -726,7 +751,7 @@ static void etm_event_stop(struct perf_event *event, int mode)
* to synchronize. Mark the event stopped and up to date.
*/
if (!path) {
- event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
+ etm_event_set_hw_state(event, PERF_HES_STOPPED | PERF_HES_UPTODATE);
return;
}
@@ -734,9 +759,6 @@ static void etm_event_stop(struct perf_event *event, int mode)
/* Clear the event_data as this ETM is stopping the trace. */
WRITE_ONCE(ctxt->event_data, NULL);
- if (event->hw.state == PERF_HES_STOPPED)
- return;
-
source = coresight_get_source(path);
sink = coresight_get_sink(path);
if (!source || !sink)
@@ -746,9 +768,9 @@ static void etm_event_stop(struct perf_event *event, int mode)
coresight_disable_source(source, event);
/* tell the core */
- event->hw.state = PERF_HES_STOPPED;
+ etm_event_set_hw_state(event, PERF_HES_STOPPED);
- etm_event_update_buffer(handle, event_data, sink, mode);
+ etm_event_update_buffer(event, handle, event_data, sink, mode);
/* Disabling the path make its elements available to other sessions */
coresight_disable_path(path);
@@ -759,12 +781,12 @@ static int etm_event_add(struct perf_event *event, int mode)
int ret = 0;
struct hw_perf_event *hwc = &event->hw;
+ etm_event_set_hw_state(event, PERF_HES_STOPPED | PERF_HES_UPTODATE);
+
if (mode & PERF_EF_START) {
etm_event_start(event, 0);
- if (hwc->state & PERF_HES_STOPPED)
+ if (READ_ONCE(hwc->state) & PERF_HES_STOPPED)
ret = -EINVAL;
- } else {
- hwc->state = PERF_HES_STOPPED;
}
return ret;
--
2.34.1
next prev parent reply other threads:[~2026-09-15 15:49 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 15:48 [PATCH 00/25] CoreSight / perf: Support AUX sampling Leo Yan
2026-09-15 15:48 ` [PATCH 01/25] perf/core: Reject non-positive AUX snapshot sizes Leo Yan
2026-09-15 15:48 ` [PATCH 02/25] perf/core: Prevent AUX sampling from racing with pause/resume Leo Yan
2026-09-15 15:48 ` [PATCH 03/25] perf/core: Export output copy helpers for CoreSight Leo Yan
2026-09-15 15:48 ` [PATCH 04/25] coresight: perf: End AUX output when an event is throttled Leo Yan
2026-09-15 15:48 ` [PATCH 05/25] coresight: perf: Extract AUX buffer update helper Leo Yan
2026-09-15 15:48 ` [PATCH 06/25] coresight: perf: Simplify flow for CPUs without a path Leo Yan
2026-09-15 15:48 ` [PATCH 07/25] coresight: perf: Validate the live context through its path Leo Yan
2026-09-15 15:48 ` [PATCH 08/25] coresight: perf: Validate context before resuming trace Leo Yan
2026-09-15 15:48 ` Leo Yan [this message]
2026-09-15 15:48 ` [PATCH 10/25] coresight: trbe: Pad snapshot buffers Leo Yan
2026-09-15 15:48 ` [PATCH 11/25] coresight: trbe: Use circular buffer mode for snapshots Leo Yan
2026-09-15 15:48 ` [PATCH 12/25] coresight: trbe: Track per-CPU sink interrupt handling Leo Yan
2026-09-15 15:48 ` [PATCH 13/25] coresight: trbe: Handle an already disabled sink Leo Yan
2026-09-15 15:49 ` [PATCH 14/25] coresight: perf: Look up the trace path in etm_event_pause() Leo Yan
2026-09-15 15:49 ` [PATCH 15/25] coresight: perf: Serialize AUX pause and resume with event stops Leo Yan
2026-09-15 15:49 ` [PATCH 16/25] coresight: perf: Support AUX sampling with per-CPU sinks Leo Yan
2026-09-15 15:49 ` [PATCH 17/25] perf cs-etm: Allow history collection for samples with zero IP Leo Yan
2026-09-15 15:49 ` [PATCH 18/25] perf cs-etm: Unify timeless buffer decoding Leo Yan
2026-09-15 15:49 ` [PATCH 19/25] perf cs-etm: Propagate errors from trace queue flushing Leo Yan
2026-09-15 15:49 ` [PATCH 20/25] perf cs-etm: Drain pending packets before finishing trace blocks Leo Yan
2026-09-15 15:49 ` [PATCH 21/25] perf cs-etm: Complete packet draining with end of trace Leo Yan
2026-09-15 15:49 ` [PATCH 22/25] perf cs-etm: Centralize sample history collection Leo Yan
2026-09-15 15:49 ` [PATCH 23/25] perf cs-etm: Decode AUX samples into callchains and branch stacks Leo Yan
2026-09-15 15:49 ` [PATCH 24/25] perf test: Add CoreSight AUX sample decoding test Leo Yan
2026-09-15 15:49 ` [PATCH 25/25] Documentation: coresight: Document AUX sample decoding Leo Yan
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=20260915-arm_cs_support_aux_sample-v1-9-35cf6f297d20@arm.com \
--to=leo.yan@arm.com \
--cc=aaupov@fb.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=anshuman.khandual@arm.com \
--cc=corbet@lwn.net \
--cc=coresight@lists.linaro.org \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mike.leach@arm.com \
--cc=mingo@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.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®