perf: Reject group leaders detached by CPU hot-unplug
v3 cover perf: Reject group leaders detached by CPU hot-unplug
Patch 1 is the v2, unchanged.
Patch 2 is the wakeup suggested by Aditya.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Daniel Wade (2):
perf: Reject group leaders detached by CPU hot-unplug
perf: Wake pollers when CPU hot-unplug detaches an event
kernel/events/core.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
PATCH v3 1/2 perf: Reject group leaders detached by CPU hot-unplug
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.
---
Unchanged from v2.
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(-)
kernel/events/core.c +4 -2
@@ -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); }
PATCH v3 2/2 perf: Wake pollers when CPU hot-unplug detaches an event
In reply to Daniel WadeCPU hot-unplug now detaches events with DETACH_EXIT, so is_event_hup() is true for them and a fresh poll() returns EPOLLHUP. A task already sleeping in poll() is not woken, because __perf_remove_from_context() does not call perf_event_wakeup() the way perf_event_exit_event() does on the task-exit path. It sleeps on until its own timeout.
Queue the event's irq_work, as merge_sched_in() does when it puts an event into ERROR.
---
New in v3.
kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/events/core.c +3 -0
@@ -15345,6 +15345,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); }
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]