Re: [PATCH v12 15/20] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling

From: Ian Rogers

Date: Fri Nov 13 2026 - 16:17:33 EST


On Wed, Nov 11, 2026 at 11:19 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
>
> 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.

Agreed, the commit message grew unwieldy across revisions. In v13, I've
rewritten it into two short paragraphs summarizing the two bugs (the
tpebs_cmd.out double-close/leak between __sample_reader() and
tpebs_stop(), and the unlocked tpebs_reader_thread read / concurrent
open-close teardown race across tpebs_send_record_cmd()'s unlocked
window) and how they are fixed.

Thanks,
Ian

> Thanks,
> Namhyung