[PATCH v3 13/17] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling
From: Ian Rogers
Date: Fri Oct 16 2026 - 13:40:54 EST
In tpebs_stop(), read tpebs_reader_thread into a local variable before
releasing tpebs_mtx to call pthread_join().
Transfer ownership of tpebs_cmd.out to __sample_reader() by passing it as
the thread argument and setting tpebs_cmd.out = -1 in
evsel__tpebs_open() so tpebs_stop() does not double-close the fd closed
by perf_session__delete(). Also reset tpebs_cmd.out = -1 if
start_command() fails in evsel__tpebs_start_perf_record(), as
start_command() closes the pipe internally on error without resetting
cmd->out.
Include <stdint.h>, <stdlib.h>, <string.h>, and <unistd.h> explicitly for
intptr_t, close(), read(), write(), malloc(), and strlen().
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/intel-tpebs.c | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c
index 7c0b8b15bd10..b81831f5e5a4 100644
--- a/tools/perf/util/intel-tpebs.c
+++ b/tools/perf/util/intel-tpebs.c
@@ -27,6 +27,10 @@
#include <errno.h>
#include <poll.h>
#include <math.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
#define PERF_DATA "-"
@@ -119,6 +123,8 @@ static int evsel__tpebs_start_perf_record(struct evsel *evsel)
tpebs_cmd.argv = record_argv;
tpebs_cmd.out = -1;
ret = start_command(&tpebs_cmd);
+ if (ret)
+ tpebs_cmd.out = -1;
zfree(&tpebs_cmd.argv);
list_for_each_entry(t, &tpebs_results, nd)
t->started = true;
@@ -220,13 +226,13 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
return 0;
}
-static void *__sample_reader(void *arg __maybe_unused)
+static void *__sample_reader(void *arg)
{
struct perf_session *session;
struct perf_data data = {
.mode = PERF_DATA_MODE_READ,
.path = PERF_DATA,
- .file.fd = tpebs_cmd.out,
+ .file.fd = (int)(intptr_t)arg,
};
struct perf_tool tool;
@@ -236,8 +242,10 @@ static void *__sample_reader(void *arg __maybe_unused)
tool.attr = perf_event__process_attr;
session = perf_session__new(&data, &tool);
- if (IS_ERR(session))
+ if (IS_ERR(session)) {
+ close(data.file.fd);
return NULL;
+ }
perf_session__process_events(session);
perf_session__delete(session);
@@ -331,12 +339,14 @@ static int tpebs_stop(void) __must_hold(tpebs_mtx_get())
/* Like tpebs_start, we should only run tpebs_end once. */
if (tpebs_cmd.pid != 0) {
pid_t actual_pid = tpebs_cmd.pid;
+ pthread_t reader_thread;
tpebs_stopping = true;
tpebs_send_record_cmd(EVLIST_CTL_CMD_STOP_TAG);
tpebs_cmd.pid = 0;
+ reader_thread = tpebs_reader_thread;
mutex_unlock(tpebs_mtx_get());
- pthread_join(tpebs_reader_thread, NULL);
+ pthread_join(reader_thread, NULL);
mutex_lock(tpebs_mtx_get());
if (control_fd[0] >= 0) {
close(control_fd[0]);
@@ -567,12 +577,14 @@ int evsel__tpebs_open(struct evsel *evsel)
goto out;
if (pthread_create(&tpebs_reader_thread, /*attr=*/NULL, __sample_reader,
- /*arg=*/NULL)) {
+ (void *)(intptr_t)tpebs_cmd.out)) {
kill(tpebs_cmd.pid, SIGTERM);
pr_err("Could not create thread to process sample data.\n");
ret = -1;
goto out;
}
+ /* __sample_reader now owns and closes the output fd. */
+ tpebs_cmd.out = -1;
ret = tpebs_send_record_cmd(EVLIST_CTL_CMD_ENABLE_TAG);
}
out:
--
2.56.0.212.g129ede20d9-goog