[PATCH v2 4/7] perf intel-tpebs: Avoid race when evlist is being deleted

From: Ian Rogers
Date: Tue May 27 2025 - 23:27:39 EST


Reading through the evsel->evlist may seg fault if a sample arrives
when the evlist is being deleted. Detect this case and ignore samples
arriving when the evlist is being deleted.

Fixes: bcfab08db7fb ("perf intel-tpebs: Filter non-workload samples")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/intel-tpebs.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c
index 4ad4bc118ea5..3b92ebf5c112 100644
--- a/tools/perf/util/intel-tpebs.c
+++ b/tools/perf/util/intel-tpebs.c
@@ -162,9 +162,17 @@ static bool is_child_pid(pid_t parent, pid_t child)

static bool should_ignore_sample(const struct perf_sample *sample, const struct tpebs_retire_lat *t)
{
- pid_t workload_pid = t->evsel->evlist->workload.pid;
- pid_t sample_pid = sample->pid;
+ pid_t workload_pid, sample_pid = sample->pid;

+ /*
+ * During evlist__purge the evlist will be removed prior to the
+ * evsel__exit calling evsel__tpebs_close and taking the
+ * tpebs_mtx. Avoid a segfault by ignoring samples in this case.
+ */
+ if (t->evsel->evlist == NULL)
+ return true;
+
+ workload_pid = t->evsel->evlist->workload.pid;
if (workload_pid < 0 || workload_pid == sample_pid)
return false;

--
2.49.0.1238.gf8c92423fb-goog