[PATCH 30/34] perf cs-etm: Invalidate OpenCSD's cache on MMAPs

From: James Clark

Date: Wed Nov 18 2026 - 11:31:30 EST


OpenCSD caches instruction bytes, but with a JIT runtime a new jitdump
mmap can replace code at a cached address without changing the context,
so subsequent decoding can use stale instructions.

Invalidate the packet decoder's caches on MMAPs when virtual timestamps
are enabled. Without virtual timestamps there's no proper ordering so
this wouldn't increase accuracy. This is unlikely to cause any
significant increase in overhead because MMAPs should be relatively rare
and OpenCSD invalidates the cache on context switches anyway.

The API has no explicit cache invalidation operation, so disable and
re-enable caching to discard its contents.

Signed-off-by: James Clark <james.clark@xxxxxxxxxx>
---
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 42 +++++++++++++++++++++++++
tools/perf/util/cs-etm-decoder/cs-etm-decoder.h | 2 ++
tools/perf/util/cs-etm.c | 36 +++++++++++++++++++--
3 files changed, 78 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
index b7ed7847f209..d6c2ae0b6a04 100644
--- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
+++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
@@ -9,6 +9,7 @@
#include <asm/bug.h>
#include <linux/coresight-pmu.h>
#include <linux/err.h>
+#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/overflow.h>
#include <linux/zalloc.h>
@@ -112,6 +113,47 @@ int cs_etm_decoder__add_mem_access_cb(struct cs_etm_packet_decoder *decoder,
return 0;
}

+#if OCSD_VER_NUM >= 0x010503
+static int cs_etm_decoder__cache_param(const char *name, int default_value,
+ long min_value, long max_value)
+{
+ const char *env = getenv(name);
+ long value = env ? strtol(env, NULL, 0) : 0;
+
+ if (value <= 0)
+ return default_value;
+
+ return clamp(value, min_value, max_value);
+}
+
+int cs_etm_decoder__invalidate_cache(struct cs_etm_packet_decoder *decoder)
+{
+ int page_size, nr_pages;
+
+ if (getenv("OPENCSD_MEMACC_CACHE_OFF"))
+ return 0;
+
+ /* Match OpenCSD's defaults and environment overrides. */
+ page_size = cs_etm_decoder__cache_param("OPENCSD_MEMACC_CACHE_PAGE_SIZE",
+ 2048, 64, 16384);
+ nr_pages = cs_etm_decoder__cache_param("OPENCSD_MEMACC_CACHE_PAGE_NUM",
+ 16, 4, 256);
+
+ /* The C API has no invalidate operation; disabling destroys the cache. */
+ if (ocsd_dt_set_mem_acc_cacheing(decoder->base.dcd_tree, 0, 0, 0) ||
+ ocsd_dt_set_mem_acc_cacheing(decoder->base.dcd_tree, 1, page_size, nr_pages))
+ return -1;
+
+ return 0;
+}
+
+#else
+int cs_etm_decoder__invalidate_cache(struct cs_etm_packet_decoder *decoder __maybe_unused)
+{
+ return 0;
+}
+#endif
+
static int cs_etm_decoder__reset_core(struct cs_etm_decoder *core)
{
ocsd_datapath_resp_t dp_ret;
diff --git a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.h b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.h
index 67592b93778e..6e56ac57b311 100644
--- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.h
+++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.h
@@ -108,6 +108,8 @@ int cs_etm_decoder__add_mem_access_cb(struct cs_etm_packet_decoder *decoder,
u64 start, u64 end,
cs_etm_mem_cb_type cb_func);

+int cs_etm_decoder__invalidate_cache(struct cs_etm_packet_decoder *decoder);
+
/* Reset all decoders to the start index of a new AUX buffer. */
int cs_etm_decoder__reset(struct cs_etm_packet_decoder *decoder, u64 index);
const char *cs_etm_decoder__get_name(struct cs_etm_packet_decoder *decoder);
diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index 5a704dfeebd3..a747c5873bc8 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -2682,6 +2682,25 @@ static int cs_etm__process_switch(struct cs_etm_auxtrace *etm,
return cs_etm__associate_tid(etm, sample->pid, sample->tid);
}

+static int cs_etm__invalidate_cache(struct cs_etm_auxtrace *etm)
+{
+ unsigned int i;
+ int ret;
+
+ for (i = 0; i < etm->queues.nr_queues; i++) {
+ struct cs_etm_queue *etmq = etm->queues.queue_array[i].priv;
+
+ if (!etmq || !etmq->decoder)
+ continue;
+
+ ret = cs_etm_decoder__invalidate_cache(etmq->decoder);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
static int cs_etm__process_event(struct perf_session *session,
union perf_event *event,
struct perf_sample *sample,
@@ -2764,8 +2783,21 @@ static int cs_etm__process_event(struct perf_session *session,
break;
}

- if (has_sample_time)
- return cs_etm__pop_heap(etm, sample->time);
+ if (has_sample_time) {
+ ret = cs_etm__pop_heap(etm, sample->time);
+ if (ret)
+ return ret;
+ }
+
+ /*
+ * Invalidate after popping and decoding because Perf only updates the
+ * map after returning from this function. That way decoding doesn't
+ * pull the old map back into the cache again.
+ */
+ if (etm->has_virtual_ts &&
+ (event->header.type == PERF_RECORD_MMAP ||
+ event->header.type == PERF_RECORD_MMAP2))
+ return cs_etm__invalidate_cache(etm);

return 0;
}

--
2.43.0