Skip to content

Commit 6449c42

Browse files
Peter Zijlstragregkh
authored andcommitted
perf: Fix cgroup state vs ERROR
[ Upstream commit 61988e3 ] While chasing down a missing perf_cgroup_event_disable() elsewhere, Leo Yan found that both perf_put_aux_event() and perf_remove_sibling_event() were also missing one. Specifically, the rule is that events that switch to OFF,ERROR need to call perf_cgroup_event_disable(). Unify the disable paths to ensure this. Fixes: ab43762 ("perf: Allow normal events to output AUX data") Fixes: 9f0c4fa ("perf/core: Add a new PERF_EV_CAP_SIBLING event capability") Reported-by: Leo Yan <leo.yan@arm.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20250605123343.GD35970@noisy.programming.kicks-ass.net Stable-dep-of: 42c5ca1 ("perf/core: Fix group leader use-after-free after sibling detach") Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4fe9b3b commit 6449c42

1 file changed

Lines changed: 30 additions & 26 deletions

File tree

kernel/events/core.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,14 +2059,13 @@ perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event)
20592059
}
20602060

20612061
static void put_event(struct perf_event *event);
2062-
static void event_sched_out(struct perf_event *event,
2063-
struct perf_cpu_context *cpuctx,
2064-
struct perf_event_context *ctx);
2062+
static void __event_disable(struct perf_event *event,
2063+
struct perf_event_context *ctx,
2064+
enum perf_event_state state);
20652065

20662066
static void perf_put_aux_event(struct perf_event *event)
20672067
{
20682068
struct perf_event_context *ctx = event->ctx;
2069-
struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
20702069
struct perf_event *iter;
20712070

20722071
/*
@@ -2095,8 +2094,7 @@ static void perf_put_aux_event(struct perf_event *event)
20952094
* state so that we don't try to schedule it again. Note
20962095
* that perf_event_enable() will clear the ERROR status.
20972096
*/
2098-
event_sched_out(iter, cpuctx, ctx);
2099-
perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
2097+
__event_disable(iter, ctx, PERF_EVENT_STATE_ERROR);
21002098
}
21012099
}
21022100

@@ -2150,21 +2148,6 @@ static inline struct list_head *get_event_list(struct perf_event *event)
21502148
return event->attr.pinned ? &ctx->pinned_active : &ctx->flexible_active;
21512149
}
21522150

2153-
/*
2154-
* Events that have PERF_EV_CAP_SIBLING require being part of a group and
2155-
* cannot exist on their own, schedule them out and move them into the ERROR
2156-
* state. Also see _perf_event_enable(), it will not be able to recover
2157-
* this ERROR state.
2158-
*/
2159-
static inline void perf_remove_sibling_event(struct perf_event *event)
2160-
{
2161-
struct perf_event_context *ctx = event->ctx;
2162-
struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2163-
2164-
event_sched_out(event, cpuctx, ctx);
2165-
perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
2166-
}
2167-
21682151
static void perf_group_detach(struct perf_event *event)
21692152
{
21702153
struct perf_event *leader = event->group_leader;
@@ -2200,8 +2183,15 @@ static void perf_group_detach(struct perf_event *event)
22002183
*/
22012184
list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) {
22022185

2186+
/*
2187+
* Events that have PERF_EV_CAP_SIBLING require being part of
2188+
* a group and cannot exist on their own, schedule them out
2189+
* and move them into the ERROR state. Also see
2190+
* _perf_event_enable(), it will not be able to recover this
2191+
* ERROR state.
2192+
*/
22032193
if (sibling->event_caps & PERF_EV_CAP_SIBLING)
2204-
perf_remove_sibling_event(sibling);
2194+
__event_disable(sibling, ctx, PERF_EVENT_STATE_ERROR);
22052195

22062196
sibling->group_leader = sibling;
22072197
list_del_init(&sibling->sibling_list);
@@ -2463,6 +2453,15 @@ static void perf_remove_from_context(struct perf_event *event, unsigned long fla
24632453
event_function_call(event, __perf_remove_from_context, (void *)flags);
24642454
}
24652455

2456+
static void __event_disable(struct perf_event *event,
2457+
struct perf_event_context *ctx,
2458+
enum perf_event_state state)
2459+
{
2460+
event_sched_out(event, __get_cpu_context(ctx), ctx);
2461+
perf_cgroup_event_disable(event, ctx);
2462+
perf_event_set_state(event, state);
2463+
}
2464+
24662465
/*
24672466
* Cross CPU call to disable a performance event
24682467
*/
@@ -2479,13 +2478,18 @@ static void __perf_event_disable(struct perf_event *event,
24792478
update_cgrp_time_from_event(event);
24802479
}
24812480

2481+
/*
2482+
* When disabling a group leader, the whole group becomes ineligible
2483+
* to run, so schedule out the full group.
2484+
*/
24822485
if (event == event->group_leader)
24832486
group_sched_out(event, cpuctx, ctx);
2484-
else
2485-
event_sched_out(event, cpuctx, ctx);
24862487

2487-
perf_event_set_state(event, PERF_EVENT_STATE_OFF);
2488-
perf_cgroup_event_disable(event, ctx);
2488+
/*
2489+
* But only mark the leader OFF; the siblings will remain
2490+
* INACTIVE.
2491+
*/
2492+
__event_disable(event, ctx, PERF_EVENT_STATE_OFF);
24892493
}
24902494

24912495
/*

0 commit comments

Comments
 (0)