[PATCH v8 26/29] perf timechart: Bounds check CPU

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

Ensure the CPU is within expected bounds to avoid out of bound memory
accesses in arrays tracking CPU state.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-timechart.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)

diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
index 64f69bd67c6b..0db2a9bd5b91 100644
--- a/tools/perf/builtin-timechart.c
+++ b/tools/perf/builtin-timechart.c
@@ -607,6 +607,10 @@ process_sample_cpu_idle(struct timechart *tchart __maybe_unused,
u32 state = perf_sample__intval(sample, "state");
u32 cpu_id = perf_sample__intval(sample, "cpu_id");

+ if (cpu_id >= MAX_CPUS) {
+ pr_debug("Out-of-bounds cpu_id %u\n", cpu_id);
+ return -1;
+ }
if (state == (u32)PWR_EVENT_EXIT)
c_state_end(tchart, cpu_id, sample->time);
else
@@ -622,6 +626,10 @@ process_sample_cpu_frequency(struct timechart *tchart,
u32 state = perf_sample__intval(sample, "state");
u32 cpu_id = perf_sample__intval(sample, "cpu_id");

+ if (cpu_id >= MAX_CPUS) {
+ pr_debug("Out-of-bounds cpu_id %u\n", cpu_id);
+ return -1;
+ }
p_state_change(tchart, cpu_id, sample->time, state);
return 0;
}
@@ -662,6 +670,10 @@ process_sample_power_start(struct timechart *tchart __maybe_unused,
u64 cpu_id = perf_sample__intval(sample, "cpu_id");
u64 value = perf_sample__intval(sample, "value");

+ if (cpu_id >= MAX_CPUS) {
+ pr_debug("Out-of-bounds cpu_id %llu\n", (unsigned long long)cpu_id);
+ return -1;
+ }
c_state_start(cpu_id, sample->time, value);
return 0;
}
@@ -683,6 +695,10 @@ process_sample_power_frequency(struct timechart *tchart,
u64 cpu_id = perf_sample__intval(sample, "cpu_id");
u64 value = perf_sample__intval(sample, "value");

+ if (cpu_id >= MAX_CPUS) {
+ pr_debug("Out-of-bounds cpu_id %llu\n", (unsigned long long)cpu_id);
+ return -1;
+ }
p_state_change(tchart, cpu_id, sample->time, value);
return 0;
}
--
2.53.0.1213.gd9a14994de-goog