* [PATCH] perf: Don't read the original event's pmu_ctx in inherit_event()
@ 2026-11-16 13:07 Daniel Wade
2026-11-16 13:19 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Daniel Wade @ 2026-11-16 13:07 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, Aditya Chillara, Jun Yang,
linux-perf-users, linux-kernel
inherit_event() walks @parent_event up to the original, top-level event
and takes the PMU from its pmu_ctx. perf_event_init_context() holds the
mutex of the forking task's own context, but that original is attached to
the context it was opened in, whose mutex and lock are both unheld, and the
fork is not in an RCU read-side section. The move_group path of
perf_event_open() drops that pmu_ctx with put_pmu_ctx(), which can free it
through call_rcu(), and reinstalls the pointer only two loops later:
G: perf_event_open(group_fd = P) M, a child of G: fork()
-------------------------------- ----------------------------
put_pmu_ctx(P->pmu_ctx)
call_rcu(free_epc_rcu)
[ grace period -> kfree() ] inherit_event(C)
parent_event = P
P->pmu_ctx->pmu <-- freed
P->pmu_ctx = pmu_ctx
The value is used as a struct pmu. No privilege is needed: a software
leader, an inheriting hardware event on the caller's own task, and a fork.
Take the PMU from the event inherit_event() is given, before the
replacement; it is attached to @parent_ctx, whose mutex is held. The result
differs only when the group was moved after the context was cloned from it.
Fixes: 4b9ce6719606 ("perf: Make sure to use pmu_ctx->pmu for groups")
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
Cc: stable@vger.kernel.org
---
Tested on mainline b3dfbde62cf1 (kernel/events identical to v7.4-rc2),
x86-64 with KASAN and lockdep, as uid 65534 at perf_event_paranoid 2.
With synchronize_rcu(), rcu_barrier() and msleep(3000) after that
put_pmu_ctx() to hold the window open, and no patch: "BUG: KASAN:
slab-use-after-free in inherit_event", freed from an RCU callback queued by
put_pmu_ctx(), then an oops in perf_ctx_disable(). With the delay and this
patch, and with this patch and no delay: clean.
Not tested without the delay: only a task whose own context is not the
leader's can race it, since perf_event_init_context() takes the forking
task's ctx->mutex and perf_event_open() takes the leader's.
4b9ce6719606 is in 6.6, 6.12, 6.18 and 6.19 stable. Jun Yang's unmerged
"perf/core: Remove group siblings before the leader in move_group" reorders
that block; it shortens this window but does not close it.
kernel/events/core.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index f5f2261f1fe..544a2c10409 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -14974,6 +14974,15 @@ inherit_event(struct perf_event *parent_event,
struct perf_event_pmu_context *pmu_ctx;
struct perf_event *child_event;
unsigned long flags;
+ struct pmu *pmu;
+
+ /*
+ * @parent_event is attached to @parent_ctx, whose mutex is held, so
+ * its pmu_ctx is stable. The original it is replaced with below can
+ * be in another context, where perf_event_open()'s move_group path
+ * may have dropped the pmu_ctx and not yet installed the new one.
+ */
+ pmu = parent_event->pmu_ctx->pmu;
/*
* Instead of creating recursive hierarchies of events,
@@ -15003,7 +15012,7 @@ inherit_event(struct perf_event *parent_event,
get_ctx(child_ctx);
child_event->ctx = child_ctx;
- pmu_ctx = find_get_pmu_context(parent_event->pmu_ctx->pmu, child_ctx, child_event);
+ pmu_ctx = find_get_pmu_context(pmu, child_ctx, child_event);
if (IS_ERR(pmu_ctx)) {
free_event(child_event);
return ERR_CAST(pmu_ctx);
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] perf: Don't read the original event's pmu_ctx in inherit_event()
2026-11-16 13:07 [PATCH] perf: Don't read the original event's pmu_ctx in inherit_event() Daniel Wade
@ 2026-11-16 13:19 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-16 13:19 UTC (permalink / raw)
To: Daniel Wade; +Cc: linux-perf-users
> inherit_event() walks @parent_event up to the original, top-level event
> and takes the PMU from its pmu_ctx. perf_event_init_context() holds the
> mutex of the forking task's own context, but that original is attached to
> the context it was opened in, whose mutex and lock are both unheld, and the
> fork is not in an RCU read-side section. The move_group path of
> perf_event_open() drops that pmu_ctx with put_pmu_ctx(), which can free it
> through call_rcu(), and reinstalls the pointer only two loops later:
>
> G: perf_event_open(group_fd = P) M, a child of G: fork()
> -------------------------------- ----------------------------
> put_pmu_ctx(P->pmu_ctx)
> call_rcu(free_epc_rcu)
> [ grace period -> kfree() ] inherit_event(C)
> parent_event = P
> P->pmu_ctx->pmu <-- freed
> P->pmu_ctx = pmu_ctx
>
> The value is used as a struct pmu. No privilege is needed: a software
> leader, an inheriting hardware event on the caller's own task, and a fork.
>
> Take the PMU from the event inherit_event() is given, before the
> replacement; it is attached to @parent_ctx, whose mutex is held. The result
> differs only when the group was moved after the context was cloned from it.
>
> Fixes: 4b9ce6719606 ("perf: Make sure to use pmu_ctx->pmu for groups")
> Signed-off-by: Daniel Wade <danjwade95@gmail.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116130736.32585-1-danjwade95@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-16 13:19 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 13:07 [PATCH] perf: Don't read the original event's pmu_ctx in inherit_event() Daniel Wade
2026-11-16 13:19 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox