[PATCH v8 23/29] perf evsel: Don't pass evsel with sample

Ian Rogers
2026-04-11T06:58:17Z

Arrange for the sample to contain the evsel and so it is unnecessary
to pass the evsel as well. This is done for uniformity, although
parsing of the sample is arguably a special case. Add missing bound
check in perf_evsel__parse_id_sample.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/evsel.c | 53 ++++++++++++++++++++++++++++++++---------
1 file changed, 42 insertions(+), 11 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index ee30e15af054..3c10b840778e 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -3003,24 +3003,39 @@ int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads)
return ret;
}

-static int perf_evsel__parse_id_sample(const struct evsel *evsel,
- const union perf_event *event,
+static int perf_evsel__parse_id_sample(const union perf_event *event,
struct perf_sample *sample)
{
+ const struct evsel *evsel = sample->evsel;
u64 type = evsel->core.attr.sample_type;
- const __u64 *array = event->sample.array;
+ const __u64 *array, *array_begin = event->sample.array;
bool swapped = evsel->needs_swap;
union u64_swap u;

- array += ((event->header.size -
- sizeof(event->header)) / sizeof(u64)) - 1;
+ if ((type & (PERF_SAMPLE_IDENTIFIER |
+ PERF_SAMPLE_CPU |
+ PERF_SAMPLE_STREAM_ID |
+ PERF_SAMPLE_ID |
+ PERF_SAMPLE_TIME |
+ PERF_SAMPLE_TID)) == 0)
+ return 0;
+
+ if (event->header.size < sizeof(event->header) + sizeof(u64))
+ return -EFAULT;

+ array = array_begin + ((event->header.size - sizeof(event->header)) / sizeof(u64)) - 1;
if (type & PERF_SAMPLE_IDENTIFIER) {
+ if (array < array_begin)
+ return -EFAULT;
+
sample->id = *array;
array--;
}

if (type & PERF_SAMPLE_CPU) {
+ if (array < array_begin)
+ return -EFAULT;
+
u.val64 = *array;
if (swapped) {
/* undo swap of u64, then swap on individual u32s */
@@ -3033,21 +3048,33 @@ static int perf_evsel__parse_id_sample(const struct evsel *evsel,
}

if (type & PERF_SAMPLE_STREAM_ID) {
+ if (array < array_begin)
+ return -EFAULT;
+
sample->stream_id = *array;
array--;
}

if (type & PERF_SAMPLE_ID) {
+ if (array < array_begin)
+ return -EFAULT;
+
sample->id = *array;
array--;
}

if (type & PERF_SAMPLE_TIME) {
+ if (array < array_begin)
+ return -EFAULT;
+
sample->time = *array;
array--;
}

if (type & PERF_SAMPLE_TID) {
+ if (array < array_begin)
+ return -EFAULT;
+
u.val64 = *array;
if (swapped) {
/* undo swap of u64, then swap on individual u32s */
@@ -3244,15 +3271,18 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event,

data->deferred_cookie = event->callchain_deferred.cookie;

- if (evsel->core.attr.sample_id_all)
- perf_evsel__parse_id_sample(evsel, event, data);
-
+ if (evsel->core.attr.sample_id_all) {
+ if (perf_evsel__parse_id_sample(event, data))
+ goto out_efault;
+ }
return 0;
}

if (event->header.type != PERF_RECORD_SAMPLE) {
- if (evsel->core.attr.sample_id_all)
- perf_evsel__parse_id_sample(evsel, event, data);
+ if (evsel->core.attr.sample_id_all) {
+ if (perf_evsel__parse_id_sample(event, data))
+ goto out_efault;
+ }
return 0;
}

@@ -3614,12 +3644,13 @@ int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,

if (event->header.type != PERF_RECORD_SAMPLE) {
struct perf_sample data = {
+ .evsel = evsel,
.time = -1ULL,
};

if (!evsel->core.attr.sample_id_all)
return -1;
- if (perf_evsel__parse_id_sample(evsel, event, &data))
+ if (perf_evsel__parse_id_sample(event, &data))
return -1;

*timestamp = data.time;
--
2.53.0.1213.gd9a14994de-goog