Re: [PATCH v12 15/20] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling
From: Namhyung Kim
Date: Thu Nov 12 2026 - 02:26:53 EST
On Wed, Nov 11, 2026 at 09:42:07AM -0800, Ian Rogers wrote:
> 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.pid = 0 and
> tpebs_cmd.out = -1 and return early if start_command() fails in
> evsel__tpebs_start_perf_record(), as start_command() closes the pipe
> internally and may leave cmd->pid = -1 on error.
>
> Serialize tpebs_send_record_cmd() with teardown via tpebs_cmd_in_flight
> and tpebs_cmd_cond, newline-terminate control commands so consecutive
> writes cannot coalesce in evlist__ctlfd_recv(), check
> check_if_command_finished(&tpebs_cmd) under tpebs_mtx only when not
> stopping, ignore late samples in process_sample_event() once
> tpebs_stopping is set, and guard evsel__tpebs_open() against a
> concurrent tpebs_stop() with tpebs_gen, tpebs_enabled, and
> tpebs_stopping across closing the fds and finish_command().
>
> Include <pthread.h>, <signal.h>, <stdint.h>, <stdlib.h>, <string.h>, and
> <unistd.h> explicitly.
I feel it's too verbose and not clear what's problem. Please explain
the race and how it fixes the issue concisely.
Thanks,
Namhyung