Skip to content

Commit 92d579e

Browse files
captain5050acmel
authored andcommitted
perf stat: Fix and validate CPU map inputs in synthetic PERF_RECORD_STAT events
Stat events can come from disk and so need a degree of validation. They contain a CPU which needs looking up via CPU map to access a counter. Add the CPU to index translation, alongside validity checking. Discussion thread: https://lore.kernel.org/linux-perf-users/CAP-5=fWQR=sCuiSMktvUtcbOLidEpUJLCybVF6=BRvORcDOq+g@mail.gmail.com/ Fixes: 7ac0089 ("perf evsel: Pass cpu not cpu map index to synthesize") Reported-by: Michael Petlan <[email protected]> Suggested-by: Michael Petlan <[email protected]> Signed-off-by: Ian Rogers <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Dave Marchevsky <[email protected]> Cc: Ian Rogers <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Fastabend <[email protected]> Cc: Kan Liang <[email protected]> Cc: KP Singh <[email protected]> Cc: Lv Ruyi <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Martin KaFai Lau <[email protected]> Cc: Michael Petlan <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: [email protected] Cc: Peter Zijlstra <[email protected]> Cc: Quentin Monnet <[email protected]> Cc: Song Liu <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Xing Zhengjun <[email protected]> Cc: Yonghong Song <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 0ae065a commit 92d579e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

tools/perf/util/stat.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,10 @@ int perf_stat_process_counter(struct perf_stat_config *config,
472472
int perf_event__process_stat_event(struct perf_session *session,
473473
union perf_event *event)
474474
{
475-
struct perf_counts_values count;
475+
struct perf_counts_values count, *ptr;
476476
struct perf_record_stat *st = &event->stat;
477477
struct evsel *counter;
478+
int cpu_map_idx;
478479

479480
count.val = st->val;
480481
count.ena = st->ena;
@@ -485,8 +486,18 @@ int perf_event__process_stat_event(struct perf_session *session,
485486
pr_err("Failed to resolve counter for stat event.\n");
486487
return -EINVAL;
487488
}
488-
489-
*perf_counts(counter->counts, st->cpu, st->thread) = count;
489+
cpu_map_idx = perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu){.cpu = st->cpu});
490+
if (cpu_map_idx == -1) {
491+
pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter));
492+
return -EINVAL;
493+
}
494+
ptr = perf_counts(counter->counts, cpu_map_idx, st->thread);
495+
if (ptr == NULL) {
496+
pr_err("Failed to find perf count for CPU %d thread %d on event %s.\n",
497+
st->cpu, st->thread, evsel__name(counter));
498+
return -EINVAL;
499+
}
500+
*ptr = count;
490501
counter->supported = true;
491502
return 0;
492503
}

0 commit comments

Comments
 (0)