mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Daniel Wade <danjwade95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Kyle Zeng <kylebot@openai.com>,
	Aditya Chillara <aditya.chillara@oss.qualcomm.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] perf: Reject group leaders detached by CPU hot-unplug
Date: Wed, 18 Nov 2026 11:43:53 +0100	[thread overview]
Message-ID: <20261118104353.GN1813469@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20261116081248.21059-1-danjwade95@gmail.com>

On Mon, Nov 16, 2026 at 06:42:48PM +1030, Daniel Wade wrote:
> When a CPU goes offline, __perf_event_exit_context() removes every event
> of that CPU's context with DETACH_GROUP. That clears PERF_ATTACH_GROUP and
> PERF_ATTACH_CONTEXT and leaves the event in PERF_EVENT_STATE_OFF, or in
> ERROR, while the file descriptor stays open and event->ctx keeps pointing
> at the per-CPU context; nothing re-attaches it when the CPU comes back.
> Commit fa091f46c383 ("perf: Reject exited events as group leaders")
> fixed the same defect for remove-on-exec, by rejecting a leader in a
> state <= PERF_EVENT_STATE_EXIT. OFF and ERROR are both above EXIT, so
> once the CPU is online again a leader detached by hot-unplug is still
> accepted and perf_group_attach() links the new event to it.
> 
> perf_group_detach() returns early when PERF_ATTACH_GROUP is already
> clear, which is right for the double detach it was written for, where no
> group is left to break up. With a sibling added after the hot-unplug
> detach it skips a sibling_list which is no longer empty: the leader is
> freed with that sibling still on it and still pointing at it through
> ->group_leader. Closing the sibling, or a group read or ioctl on it, then
> uses the freed event.
> 
> Commit 42c5ca1f0a28 ("perf/core: Fix group leader use-after-free after
> sibling detach") fixed the mirror image, a sibling detached by hot-unplug
> and the leader closed afterwards. It promotes the sibling from the paths
> below that early return, so a leader detached first and given a sibling
> afterwards still reaches the same free.

Nice

>  kernel/events/core.c |   12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index f5f2261f1fe..21c529cd7d1 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -14274,6 +14274,18 @@ SYSCALL_DEFINE5(perf_event_open,
>  			goto err_locked;
>  		}
>  
> +		/*
> +		 * Likewise against CPU hot-unplug, which detaches the events
> +		 * of a CPU context and breaks up its groups without putting
> +		 * them in a terminal state. perf_group_detach() does nothing
> +		 * for a leader that is already detached, so a sibling added
> +		 * to one would stay linked to it after the leader is freed.
> +		 */
> +		if (!(group_leader->attach_state & PERF_ATTACH_CONTEXT)) {
> +			err = -ENODEV;
> +			goto err_locked;
> +		}
> +
>  		/*
>  		 * Only a group leader can be exclusive or pinned
>  		 */

It occurs to me that changeing __perf_event_exit_contxt() to use
DETACH_EXIT | DETACH_GROUP, will also fix things, right?

In that case the STATE_EXIT check from fa091f46c383 will catch it.

Doing this makes the various cases (task-exit, CPU-offline) behave
similarly.

Does that work for you?


      parent reply	other threads:[~2026-11-18 10:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  8:12 Daniel Wade
2026-11-17  6:48 ` Aditya Chillara
2026-11-18 10:43 ` Peter Zijlstra [this message]

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=20261118104353.GN1813469@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=aditya.chillara@oss.qualcomm.com \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=danjwade95@gmail.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kylebot@openai.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    /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

all inboxes | Powered by JetHome®