[PATCH 22/28] perf session: Add byte-swap for PERF_RECORD_COMPRESSED2 events

From: Arnaldo Carvalho de Melo

Date: Sat May 09 2026 - 23:41:18 EST


From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>

PERF_RECORD_COMPRESSED2 has a data_size field (__u64) at offset 8
that is not covered by the header byte-swap in prefetch_event().
When reading a cross-endian perf.data file, data_size was used
without swapping, causing either garbage decompression sizes or
silent data corruption.

PERF_RECORD_COMPRESSED (the original format) has no fields beyond
the header, so it doesn't need a swap op.

Add perf_event__compressed2_swap() and register it in
perf_event__swap_ops[].

Fixes: 208c0e168344 ("perf record: Add 8-byte aligned event type PERF_RECORD_COMPRESSED2")
Reported-by: sashiko-bot@xxxxxxxxxx # Running on a local machine
Cc: Chun-Tse Shao <ctshao@xxxxxxxxxx>
Cc: Ian Rogers <irogers@xxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Assisted-by: Claude Opus 4.6 (1M context) <noreply@xxxxxxxxxxxxx>
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/util/session.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 85591ccdc2e8ada3..80cb03d150cecc0b 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1037,6 +1037,14 @@ static int perf_event__time_conv_swap(union perf_event *event,
return 0;
}

+static int perf_event__compressed2_swap(union perf_event *event,
+ bool sample_id_all __maybe_unused)
+{
+ /* Only data_size needs swapping — compressed payload is a raw byte stream */
+ event->pack2.data_size = bswap_64(event->pack2.data_size);
+ return 0;
+}
+
static int perf_event__bpf_metadata_swap(union perf_event *event,
bool sample_id_all __maybe_unused)
{
@@ -1175,6 +1183,7 @@ static perf_event__swap_op perf_event__swap_ops[] = {
[PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap,
[PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap,
[PERF_RECORD_TIME_CONV] = perf_event__time_conv_swap,
+ [PERF_RECORD_COMPRESSED2] = perf_event__compressed2_swap,
[PERF_RECORD_BPF_METADATA] = perf_event__bpf_metadata_swap,
[PERF_RECORD_SCHEDSTAT_CPU] = perf_event__schedstat_cpu_swap,
[PERF_RECORD_SCHEDSTAT_DOMAIN] = perf_event__schedstat_domain_swap,
--
2.54.0