[PATCH v9 25/29] perf sched: Bounds check CPU in sched switch events

From: Ian Rogers

Date: Sat Apr 11 2026 - 15:24:59 EST


Ensure the CPU is within expected bounds to avoid out of bound memory
accesses.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-sched.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 45439e2af8ff..6d0655d56e26 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1791,6 +1791,11 @@ static int process_sched_switch_event(const struct perf_tool *tool,
u32 prev_pid = perf_sample__intval(sample, "prev_pid"),
next_pid = perf_sample__intval(sample, "next_pid");

+ if (this_cpu < 0 || this_cpu >= MAX_CPUS) {
+ pr_debug("Out-of-bound sample CPU %d\n", this_cpu);
+ return -1;
+ }
+
if (sched->curr_pid[this_cpu] != (u32)-1) {
/*
* Are we trying to switch away a PID that is
--
2.53.0.1213.gd9a14994de-goog