perf session: Add byte-swap handler for PERF_RECORD_COMPRESSED2

PERF_RECORD_COMPRESSED2 events carry a data_size field that must be
byte-swapped when reading cross-endian perf.data files.  Without a
swap handler, reading COMPRESSED2 events on a different-endian machine
would misinterpret data_size as a garbage value, causing the
decompression path to read the wrong number of bytes.

The compressed payload itself is a raw byte stream and needs no
swapping.

Fixes: 208c0e1683 ("perf record: Add 8-byte aligned event type PERF_RECORD_COMPRESSED2")
Reported-by: sashiko-bot@kernel.org # Running on a local machine
Reviewed-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Assisted-by: Claude:claude-opus-4.6-1m
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Arnaldo Carvalho de Melo 2026-05-02 13:22:33 -03:00
parent 3669697bda
commit 1c9d2ac2ea

View File

@ -1056,6 +1056,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)
{
@ -1197,6 +1205,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,