* [PATCH v2] perf: Reject group leaders detached by CPU hot-unplug
@ 2026-11-19 1:47 Daniel Wade
2026-11-19 1:55 ` sashiko-bot
2026-11-19 8:54 ` Aditya Chillara
0 siblings, 2 replies; 4+ messages in thread
From: Daniel Wade @ 2026-11-19 1:47 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
Adrian Hunter, James Clark, Kyle Zeng, Aditya Chillara,
linux-perf-users, linux-kernel
CPU hot-unplug detaches the events of a CPU context but leaves them in
OFF or ERROR, so once the CPU is back a detached leader is still accepted
as group_fd. perf_group_detach() does nothing for a leader that is
already detached, so closing it frees it with the new sibling still
linked, and closing the sibling is a use-after-free.
Detach them with DETACH_EXIT as well, so that they end up in
PERF_EVENT_STATE_EXIT and are rejected by the check from
commit fa091f46c383 ("perf: Reject exited events as group leaders").
Side effect: poll() on a hot-unplugged event now reports EPOLLHUP, and it
is refused as group_fd even with PERF_FLAG_FD_NO_GROUP.
Fixes: 226424eee809 ("perf: Fix corruption of sibling list with hotplug")
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
Cc: stable@vger.kernel.org
---
v2: use DETACH_EXIT at hot-unplug instead of a new check, as Peter
suggested.
v1: https://lore.kernel.org/all/20261116081248.21059-1-danjwade95@gmail.com/
Tested on mainline b3dfbde62cf1, x86-64 with KASAN and PROVE_LOCKING,
2 vCPUs under KVM. Without the patch a sibling is accepted on a
hot-unplugged leader and the close gives 15 KASAN reports. With it the
open fails with ENODEV and no report appears.
kernel/events/core.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index f5f2261f1fe6..2de4c3cc37ac 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -15342,8 +15342,10 @@ static void __perf_event_exit_context(void *__info)
raw_spin_lock(&ctx->lock);
ctx_sched_out(ctx, NULL, EVENT_TIME);
- list_for_each_entry(event, &ctx->event_list, event_entry)
- __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
+ list_for_each_entry(event, &ctx->event_list, event_entry) {
+ __perf_remove_from_context(event, cpuctx, ctx,
+ (void *)(DETACH_EXIT | DETACH_GROUP));
+ }
raw_spin_unlock(&ctx->lock);
}
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v2] perf: Reject group leaders detached by CPU hot-unplug 2026-11-19 1:47 [PATCH v2] perf: Reject group leaders detached by CPU hot-unplug Daniel Wade @ 2026-11-19 1:55 ` sashiko-bot 2026-11-19 8:54 ` Aditya Chillara 1 sibling, 0 replies; 4+ messages in thread From: sashiko-bot @ 2026-11-19 1:55 UTC (permalink / raw) To: Daniel Wade; +Cc: linux-perf-users > CPU hot-unplug detaches the events of a CPU context but leaves them in > OFF or ERROR, so once the CPU is back a detached leader is still accepted > as group_fd. perf_group_detach() does nothing for a leader that is > already detached, so closing it frees it with the new sibling still > linked, and closing the sibling is a use-after-free. > > Detach them with DETACH_EXIT as well, so that they end up in > PERF_EVENT_STATE_EXIT and are rejected by the check from > commit fa091f46c383 ("perf: Reject exited events as group leaders"). > > Side effect: poll() on a hot-unplugged event now reports EPOLLHUP, and it > is refused as group_fd even with PERF_FLAG_FD_NO_GROUP. > > Fixes: 226424eee809 ("perf: Fix corruption of sibling list with hotplug") > Suggested-by: Peter Zijlstra <peterz@infradead.org> > Signed-off-by: Daniel Wade <danjwade95@gmail.com> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261119014730.2074-1-danjwade95@gmail.com?part=1 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] perf: Reject group leaders detached by CPU hot-unplug 2026-11-19 1:47 [PATCH v2] perf: Reject group leaders detached by CPU hot-unplug Daniel Wade 2026-11-19 1:55 ` sashiko-bot @ 2026-11-19 8:54 ` Aditya Chillara 2026-11-19 9:19 ` Peter Zijlstra 1 sibling, 1 reply; 4+ messages in thread From: Aditya Chillara @ 2026-11-19 8:54 UTC (permalink / raw) To: Daniel Wade, Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Kyle Zeng, linux-perf-users, linux-kernel On 11/19/2026 7:17 AM, Daniel Wade wrote: > CPU hot-unplug detaches the events of a CPU context but leaves them in > OFF or ERROR, so once the CPU is back a detached leader is still accepted > as group_fd. perf_group_detach() does nothing for a leader that is > already detached, so closing it frees it with the new sibling still > linked, and closing the sibling is a use-after-free. > > Detach them with DETACH_EXIT as well, so that they end up in > PERF_EVENT_STATE_EXIT and are rejected by the check from > commit fa091f46c383 ("perf: Reject exited events as group leaders"). > > Side effect: poll() on a hot-unplugged event now reports EPOLLHUP, and it > is refused as group_fd even with PERF_FLAG_FD_NO_GROUP. > > Fixes: 226424eee809 ("perf: Fix corruption of sibling list with hotplug") > Suggested-by: Peter Zijlstra <peterz@infradead.org> > Signed-off-by: Daniel Wade <danjwade95@gmail.com> > Cc: stable@vger.kernel.org > --- > v2: use DETACH_EXIT at hot-unplug instead of a new check, as Peter > suggested. > v1: https://lore.kernel.org/all/20261116081248.21059-1-danjwade95@gmail.com/ > > Tested on mainline b3dfbde62cf1, x86-64 with KASAN and PROVE_LOCKING, > 2 vCPUs under KVM. Without the patch a sibling is accepted on a > hot-unplugged leader and the close gives 15 KASAN reports. With it the > open fails with ENODEV and no report appears. > > kernel/events/core.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/kernel/events/core.c b/kernel/events/core.c > index f5f2261f1fe6..2de4c3cc37ac 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -15342,8 +15342,10 @@ static void __perf_event_exit_context(void *__info) > > raw_spin_lock(&ctx->lock); > ctx_sched_out(ctx, NULL, EVENT_TIME); > - list_for_each_entry(event, &ctx->event_list, event_entry) > - __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); > + list_for_each_entry(event, &ctx->event_list, event_entry) { > + __perf_remove_from_context(event, cpuctx, ctx, > + (void *)(DETACH_EXIT | DETACH_GROUP)); > + } > raw_spin_unlock(&ctx->lock); > } The patch itself LGTM. Reviewed-by: Aditya Chillara <aditya.chillara@oss.qualcomm.com> PatchWise AI code review (gpt-6-astra) flagged the following though: [medium] Adding DETACH_EXIT makes is_event_hup() true, but this path never wakes event->waitq. __perf_remove_from_context() changes the state and detaches the event without calling perf_event_wakeup(), unlike perf_event_exit_event(). A thread blocked in poll/epoll on a disabled, mmap-backed CPU event can remain asleep indefinitely after CPU unplug, even though a fresh poll reports EPOLLHUP. Arrange a wakeup in an appropriate context after publishing the terminal state so existing waiters receive the hangup indication. -- To be clear, this is not a regression: before your patch these events went to OFF and poll() never reported a hangup at all, so a waiter slept just the same. But the EPOLLHUP the patch advertises does not reach tasks that are already waiting: task already asleep in poll() a fresh poll() before your patch stays asleep no data your patch stays asleep EPOLLHUP your patch + wakeup woken, sees EPOLLHUP EPOLLHUP perf_event_exit_event() kicks perf_poll() with perf_event_wakeup() after moving events to EXIT, but this path doesn't, so a task already sleeping in poll() is not woken and only sees EPOLLHUP when its timeout runs out. Deferring the wakeup to the event's irq_work, as merge_sched_in() does, seems enough. Something like this, on top of your patch: diff --git a/kernel/events/core.c b/kernel/events/core.c index c1f3e1f013ec..885b6594139a 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -15350,6 +15350,9 @@ static void __perf_event_exit_context(void *__info) list_for_each_entry(event, &ctx->event_list, event_entry) { __perf_remove_from_context(event, cpuctx, ctx, (void *)(DETACH_EXIT | DETACH_GROUP)); + /* Kick perf_poll() for is_event_hup(). */ + event->pending_wakeup = 1; + irq_work_queue(&event->pending_irq); } raw_spin_unlock(&ctx->lock); } Tested in a 4-CPU x86_64 QEMU/KVM guest (virtme-ng), 7.4.0-rc3+ with lockdep enabled, using perf record -C 1 and then taking CPU 1 offline: perf record -C 1 -e cpu-clock -o /tmp/hp.data & echo 0 > /sys/devices/system/cpu/cpu1/online jobs perf report -i /tmp/hp.data --stdio | head A) Your patch only. perf keeps running after the unplug and needs Ctrl-C: # perf record -C 1 -e cpu-clock -o /tmp/hp.data & [1] 149 # echo 0 > /sys/devices/system/cpu/cpu1/online # jobs [1]+ Running perf record -C 1 -e cpu-clock -o /tmp/hp.data & # fg perf record -C 1 -e cpu-clock -o /tmp/hp.data ^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.040 MB /tmp/hp.data (283 samples) ] # perf report -i /tmp/hp.data --stdio | head ... # Samples: 283 of event 'cpu-clock' # Event count (approx.): 70750000 B) Your patch plus the wakeup above. perf gets EPOLLHUP at once, flushes and exits by itself: # perf record -C 1 -e cpu-clock -o /tmp/hp.data & [1] 151 # echo 0 > /sys/devices/system/cpu/cpu1/online [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.065 MB /tmp/hp.data (737 samples) ] [1]+ Done perf record -C 1 -e cpu-clock -o /tmp/hp.data # perf report -i /tmp/hp.data --stdio | head ... # Samples: 737 of event 'cpu-clock' # Event count (approx.): 184250000 Thank you, Aditya ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] perf: Reject group leaders detached by CPU hot-unplug 2026-11-19 8:54 ` Aditya Chillara @ 2026-11-19 9:19 ` Peter Zijlstra 0 siblings, 0 replies; 4+ messages in thread From: Peter Zijlstra @ 2026-11-19 9:19 UTC (permalink / raw) To: Aditya Chillara Cc: Daniel Wade, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Kyle Zeng, linux-perf-users, linux-kernel On Thu, Nov 19, 2026 at 02:24:19PM +0530, Aditya Chillara wrote: > task already asleep in poll() a fresh poll() > before your patch stays asleep no data > your patch stays asleep EPOLLHUP > your patch + wakeup woken, sees EPOLLHUP EPOLLHUP > > perf_event_exit_event() kicks perf_poll() with perf_event_wakeup() after > moving events to EXIT, but this path doesn't, so a task already sleeping > in poll() is not woken and only sees EPOLLHUP when its timeout runs out. > > Deferring the wakeup to the event's irq_work, as merge_sched_in() does, > seems enough. Something like this, on top of your patch: > > diff --git a/kernel/events/core.c b/kernel/events/core.c > index c1f3e1f013ec..885b6594139a 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -15350,6 +15350,9 @@ static void __perf_event_exit_context(void *__info) > list_for_each_entry(event, &ctx->event_list, event_entry) { > __perf_remove_from_context(event, cpuctx, ctx, > (void *)(DETACH_EXIT | DETACH_GROUP)); > + /* Kick perf_poll() for is_event_hup(). */ > + event->pending_wakeup = 1; > + irq_work_queue(&event->pending_irq); > } > raw_spin_unlock(&ctx->lock); > } Yeah, I suppose that makes sense. ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-19 9:20 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-19 1:47 [PATCH v2] perf: Reject group leaders detached by CPU hot-unplug Daniel Wade 2026-11-19 1:55 ` sashiko-bot 2026-11-19 8:54 ` Aditya Chillara 2026-11-19 9:19 ` Peter Zijlstra
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox