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 v4] perf fdarray: Fix destructor invocation and event counting in fdarray__filter
Date: Mon, 17 Aug 2026 23:28:10 +0000	[thread overview]
Message-ID: <20260817232811.58DB11F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260817230431.424646-1-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The refinement to fdarray__filter causes an immediate 'pollfd index mismatch' error and premature termination on control descriptor POLLHUP by prematurely clearing fd and revents.
--

--- Patch [1]: [PATCH v4] perf fdarray: Fix destructor invocation and event counting in fdarray__filter ---
commit 495f184eb7be01a8af2c7157e4d7f142c89228b0
Author: Ian Rogers <irogers@google.com>

perf fdarray: Fix destructor invocation and event counting in fdarray__filter

This commit refines the early continue filter in fdarray__filter to only skip
system-wide perf events. Control descriptors now fall through to have their fd
values unset to -1 on POLLHUP while avoiding their destructors. It also ensures
the active event counter increments strictly for completely filterable events.

> diff --git a/tools/lib/api/fd/array.c b/tools/lib/api/fd/array.c
> index 67b73481df27e..3681ad7c65278 100644
> --- a/tools/lib/api/fd/array.c
> +++ b/tools/lib/api/fd/array.c
> @@ -116,14 +116,23 @@ int fdarray__filter(struct fdarray *fda, short revents,
>  		return 0;
>  
>  	for (fd = 0; fd < fda->nr; ++fd) {
> -		if (fda->priv[fd].flags & fdarray_flag__nonfilterable)
> +		/*
> +		 * System-wide perf events are nonfilterable but not non_perf_event.
> +		 * We want to skip them entirely and never process revents on them.
> +		 */
> +		if ((fda->priv[fd].flags & fdarray_flag__nonfilterable) &&
> +		    !(fda->priv[fd].flags & fdarray_flag__non_perf_event))
>  			continue;
>  
>  		if (!fda->entries[fd].events)
>  			continue;
>  
>  		if (fda->entries[fd].revents & revents) {
> -			if (entry_destructor)
> +			/*
> +			 * Control descriptors are non_perf_event and don't need
> +			 * their perf-specific destructors triggered.
> +			 */
> +			if (entry_destructor && !(fda->priv[fd].flags & fdarray_flag__non_perf_event))
>  				entry_destructor(fda, fd, arg);
>  
>  			/*
>  			 * Set fd to -1 so poll() ignores this entry; otherwise
>  			 * POLLHUP/POLLERR are still reported for events=0 fds
>  			 * (POSIX: always checked), causing a poll storm.
>  			 */
>  			fda->entries[fd].fd = -1;
>  			fda->entries[fd].revents = fda->entries[fd].events = 0;

[Severity: High]
Does unsetting the file descriptor to -1 and clearing revents for control
descriptors cause a pollfd index mismatch error during teardown?

When __cmd_record() handles a POLLHUP on the control pipe, it calls
fdarray__filter() which now mutates the thread's control descriptor:

tools/perf/builtin-record.c:__cmd_record() {
    ...
    if (fdarray__filter(&thread->pollfd, POLLERR | POLLHUP,
                        record__thread_munmap_filtered, NULL) == 0)
        draining = true;

    err = record__update_evlist_pollfd_from_thread(rec, rec->evlist, thread);
    ...
}

Immediately after, record__update_evlist_pollfd_from_thread() compares the
mutated thread array against evlist->pollfd, which retains the original
valid file descriptor. This strict check fails and returns -EINVAL, causing
an early ungraceful abort:

tools/perf/builtin-record.c:record__update_evlist_pollfd_from_thread() {
    ...
    if (e_entries[e_pos].fd != t_entries[t_pos].fd ||
        e_entries[e_pos].events != t_entries[t_pos].events) {
        pr_err("Thread and evlist pollfd index mismatch\n");
        err = -EINVAL;
        continue;
    }
}

Furthermore, does clearing revents to 0 here ensure evlist__ctlfd_process()
will never see the POLLHUP to perform a clean teardown via
evlist__finalize_ctlfd()?

>  			continue;
>  		}
>  
> -		++nr;
> +		if (!(fda->priv[fd].flags & fdarray_flag__nonfilterable))
> +			++nr;
>  	}
>  
>  	return nr;

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

  parent reply	other threads:[~2026-08-17 23:28 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-10  5:39 [PATCH v1 1/2] perf record: Fix teardown hang on system-wide multi-threaded sessions Ian Rogers
2026-07-10  5:39 ` [PATCH v1 2/2] perf cap: If capability is missing still perform root test Ian Rogers
2026-07-10  5:47   ` sashiko-bot
2026-07-10 22:24     ` Namhyung Kim
2026-07-16  7:40       ` [PATCH v2] perf cap: Remove used_root parameter and simplify capability checks Ian Rogers
2026-07-23  5:06         ` [PATCH v3] " Ian Rogers
2026-07-24  5:47           ` Namhyung Kim
2026-07-10  5:56 ` [PATCH v1 1/2] perf record: Fix teardown hang on system-wide multi-threaded sessions sashiko-bot
2026-07-10 22:02 ` Namhyung Kim
2026-07-12  6:56 ` (subset) " Namhyung Kim
2026-07-16  7:37   ` [PATCH v2] perf record: Fix destructor invocation and event counting in fdarray__filter Ian Rogers
2026-07-16  8:00     ` sashiko-bot
2026-07-23  5:04     ` [PATCH v3] " Ian Rogers
2026-07-23  5:27       ` sashiko-bot
2026-08-17 23:04       ` [PATCH v4] perf fdarray: " Ian Rogers
2026-08-17 23:17         ` Namhyung Kim
2026-08-17 23:30           ` Ian Rogers
2026-08-17 23:28         ` sashiko-bot [this message]
2026-08-18  0:31         ` [PATCH v5] perf record: Fix unhandled POLLHUP on non_perf_event descriptors Ian Rogers
2026-08-18  0:43           ` 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=20260817232811.58DB11F000E9@smtp.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