Commit

e76b36b4be7160eaa14b416681e548b125702395

perf: Fix perf_pmu_migrate_context() leaving events behind

__perf_pmu_remove() walks the group leaders of a CPU and PMU with
perf_event_groups_for_cpu_pmu(), removing each one as it goes.
perf_remove_from_context() takes the leader out of the groups rbtree,
after which rb_next() on that node returns NULL, prematurely ending the
walk. Other groups stay behind in the source context.

On CPU hotplug, they are then torn down with the dying CPU. They keep
running until teardown, while the PMU's interrupt and migrated events
are on a new CPU, which drivers rely on never happening.

Walk ctx->event_list instead and pick out the PMU's events by their
pmu_ctx.

Fixes: bd2756811766 ("perf: Rewrite core context handling")
Signed-off-by: Haris Okanovic <harisokn@amazon.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20261013185603.1347265-2-harisokn@amazon.com

Haris Okanovic <harisokn@amazon.com> · 2026-10-13 18:56 UTC · committed by Peter Zijlstra

Browse files
1 file changed, +7 −13
modified kernel/events/core.c
@@ -14394,23 +14394,18 @@ err:
14394 14394 }
14395 14395 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
14396 14396
14397 - static void __perf_pmu_remove(struct perf_event_context *ctx,
14398 - int cpu, struct pmu *pmu,
14399 - struct perf_event_groups *groups,
14397 + static void __perf_pmu_remove(struct perf_event_context *ctx, struct pmu *pmu,
14400 14398 struct list_head *events)
14401 14399 {
14402 - struct perf_event *event, *sibling;
14400 + struct perf_event *event, *tmp;
14401 +
14402 + list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
14403 + if (event->pmu_ctx->pmu != pmu)
14404 + continue;
14403 14405
14404 - perf_event_groups_for_cpu_pmu(event, groups, cpu, pmu) {
14405 14406 perf_remove_from_context(event, 0);
14406 14407 put_pmu_ctx(event->pmu_ctx);
14407 14408 list_add(&event->migrate_entry, events);
14408 -
14409 - for_each_sibling_event(sibling, event) {
14410 - perf_remove_from_context(sibling, 0);
14411 - put_pmu_ctx(sibling->pmu_ctx);
14412 - list_add(&sibling->migrate_entry, events);
14413 - }
14414 14409 }
14415 14410 }
14416 14411
@@ -14486,8 +14481,7 @@ void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
14486 14481 */
14487 14482 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
14488 14483
14489 - __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->pinned_groups, &events);
14490 - __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->flexible_groups, &events);
14484 + __perf_pmu_remove(src_ctx, pmu, &events);
14491 14485
14492 14486 if (!list_empty(&events)) {
14493 14487 /*