Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v13 15/21] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling
Date: Fri, 13 Nov 2026 17:13:58 +0000	[thread overview]
Message-ID: <sashiko-outbox-213286@kernel.org> (raw)
In-Reply-To: <20261113164152.2095566-16-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf intel-tpebs: race and broken error cleanup in
  evsel__tpebs_open()
--

commit a6a4e6c8987e63cad1bc76ddde1b72c0404e3b8e
Author: Ian Rogers <irogers@google.com>

perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling

This commit fixes lifecycle races around the background perf record
process, sample reader thread, and control/ack file descriptors in
intel-tpebs.

> diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c
> index 7c0b8b15bd10e..5cb090b27fbd7 100644
> --- a/tools/perf/util/intel-tpebs.c
> +++ b/tools/perf/util/intel-tpebs.c

[ ... ]

> @@ -567,13 +635,22 @@ 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;
> +		started_thread = true;
>  		ret = tpebs_send_record_cmd(EVLIST_CTL_CMD_ENABLE_TAG);
> +		if (!ret) {
> +			if (tpebs_gen != open_gen)
> +				ret = -EBUSY;
> +			else
> +				tpebs_enabled = true;
> +		}
>  	}
>  out:
>  	if (ret) {
> @@ -584,11 +661,25 @@ int evsel__tpebs_open(struct evsel *evsel)
>  			tpebs_retire_lat__delete(t);
>  		}
>  
> -		if (started_process) {
> -			if (tpebs_cmd.pid > 0) {
> -				kill(tpebs_cmd.pid, SIGTERM);
> -				finish_command(&tpebs_cmd);
> +		if (started_process && tpebs_gen == open_gen) {
> +			pid_t actual_pid = tpebs_cmd.pid;

[Severity: Medium]
Can concurrent or failing calls to evsel__tpebs_open() corrupt
tpebs_results or return before events are enabled?

First, evsel__tpebs_start_perf_record() sets tpebs_cmd.pid and marks all
entries in tpebs_results with t->started = true before
tpebs_send_record_cmd(EVLIST_CTL_CMD_ENABLE_TAG) drops tpebs_mtx:

tools/perf/util/intel-tpebs.c:evsel__tpebs_open() {
    ...
	if (tpebs_cmd.pid != 0) {
		struct tpebs_retire_lat *t;
		bool valid;

		t = tpebs_retire_lat__find(evsel);
		valid = t && t->started;
		mutex_unlock(tpebs_mtx_get());
		/* May fail as the event wasn't started. */
		return valid ? 0 : -EBUSY;
	}
    ...
}

A second caller entering evsel__tpebs_open() while ENABLE is in flight
sees tpebs_cmd.pid != 0 and t->started == true without checking
tpebs_enabled, returning 0 before events are enabled (or even if ENABLE
then fails).

Second, in the out: error path, tpebs_retire_lat__find(evsel) removes and
deletes t outside the (started_process && tpebs_gen == open_gen) guard:

tools/perf/util/intel-tpebs.c:evsel__tpebs_open() {
    ...
	mutex_lock(tpebs_mtx_get());
	if (tpebs_stopping || tpebs_cmd.pid != 0) {
		ret = -EBUSY;
		goto out;
	}
    ...
out:
	if (ret) {
		struct tpebs_retire_lat *t = tpebs_retire_lat__find(evsel);

		if (t) {
			list_del_init(&t->nd);
			tpebs_retire_lat__delete(t);
		}

		if (started_process && tpebs_gen == open_gen) {
    ...
}

If a second thread races through evsel__tpebs_prepare() and hits
tpebs_cmd.pid != 0 (or if tpebs_gen != open_gen after a concurrent stop
and reopen), jumping to out: deletes the active tpebs_retire_lat entry
owned by the running session.  When that entry is the only one in
tpebs_results, a later evsel__tpebs_close() will not find it and will
never call tpebs_stop(), leaking the background perf record process,
reader thread, and pipes.

Conversely, when (started_process && tpebs_gen == open_gen) is true on
failure, evsel__tpebs_prepare() added all retire_lat evsels on
evsel->evlist to tpebs_results, but out: only deletes the single evsel
passed to evsel__tpebs_open(), leaving the sibling entries behind with
stale started state.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261113164152.2095566-1-irogers@google.com?part=15

  reply	other threads:[~2026-11-13 17:13 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 16:41 [PATCH v13 00/21] tools/perf: Adopt kernel compiler-context-analysis.h annotations Ian Rogers
2026-11-13 16:41 ` [PATCH v13 01/21] compiler-context-analysis: Avoid unused parameter warning in _context_unsafe_alias() Ian Rogers
2026-11-13 16:50   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 02/21] tools headers compiler: Synchronize compiler_types.h with the kernel sources Ian Rogers
2026-11-13 16:56   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 03/21] tools headers: Synchronize linux/compiler-context-analysis.h " Ian Rogers
2026-11-13 16:49   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 04/21] perf mutex: Use kernel style compiler-context-analysis.h annotations Ian Rogers
2026-11-13 16:57   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 05/21] perf build: Enable additional Clang thread-safety flags Ian Rogers
2026-11-13 16:56   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 06/21] perf comm: Add __guarded_by annotations to struct comm_strs Ian Rogers
2026-11-13 17:02   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 07/21] perf vdso: Fix unlocked __dsos__add() and race in __machine__addnew_vdso() Ian Rogers
2026-11-13 16:50   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 08/21] perf dsos: Skip BPF and OOL DSOs in dsos__find_kernel_dso() Ian Rogers
2026-11-13 16:58   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 09/21] perf dsos: Ensure DSOs in auxtrace, machine, and symbol are in dsos Ian Rogers
2026-11-13 16:59   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 10/21] perf dso: Pass dso_data to close_data_fd() and dso__list_del() Ian Rogers
2026-11-13 16:55   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 11/21] perf dso: Use DEFINE_MUTEX for dso__data_open_lock Ian Rogers
2026-11-13 16:52   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 12/21] perf dsos: Use read lock fast path in dsos__findnew_id() Ian Rogers
2026-11-13 17:09   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 13/21] perf dso: Expand context analysis annotations Ian Rogers
2026-11-13 16:58   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 14/21] perf thread: Expand context analysis annotations to threads and namespaces_lock Ian Rogers
2026-11-13 16:59   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 15/21] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling Ian Rogers
2026-11-13 17:13   ` sashiko-bot [this message]
2026-11-13 16:41 ` [PATCH v13 16/21] perf intel-tpebs: Use DEFINE_MUTEX for tpebs_mtx and add lock annotations Ian Rogers
2026-11-13 17:09   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 17/21] perf config: Add __guarded_by and __must_hold annotations Ian Rogers
2026-11-13 17:01   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 18/21] perf env: " Ian Rogers
2026-11-13 17:11   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 19/21] perf maps: Fix lock inversion in maps__load_first() Ian Rogers
2026-11-13 17:10   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 20/21] perf maps: Fix map index lookup fallback and removal Ian Rogers
2026-11-13 17:07   ` sashiko-bot
2026-11-13 16:41 ` [PATCH v13 21/21] perf maps: Add context analysis annotations Ian Rogers
2026-11-13 17:04   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-213286@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=irogers@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox