* [PATCH Fix perf cgroup problem 0/2]
@ 2025-05-14 6:47 Luo Gengkun
2025-05-14 6:47 ` [PATCH Fix perf cgroup problem 1/2] perf/core: Fix nr_cgroups/cpuctx->cgrp is not updated correctly Luo Gengkun
2025-05-14 6:47 ` [PATCH Fix perf cgroup problem 2/2] perf/core: Fix WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0) in perf_cgroup_switch Luo Gengkun
0 siblings, 2 replies; 3+ messages in thread
From: Luo Gengkun @ 2025-05-14 6:47 UTC (permalink / raw)
To: peterz
Cc: mingo, acme, namhyung, mark.rutland, alexander.shishkin, jolsa,
irogers, adrian.hunter, kan.liang, yeoreum.yun, linux-perf-users,
linux-kernel
Fixed the issue that nr_cgroup and cpuctx->cgrp of the perf cgroup event
are not correctly updated. And Fixed the possible concurrency of
perf_cgroup_switch and perf_cgroup_event_disable.
Luo Gengkun (2):
perf/core: Fix nr_cgroups/cpuctx->cgrp is not updated correctly
perf/core: Fix WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0) in
perf_cgroup_switch
kernel/events/core.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH Fix perf cgroup problem 1/2] perf/core: Fix nr_cgroups/cpuctx->cgrp is not updated correctly
2025-05-14 6:47 [PATCH Fix perf cgroup problem 0/2] Luo Gengkun
@ 2025-05-14 6:47 ` Luo Gengkun
2025-05-14 6:47 ` [PATCH Fix perf cgroup problem 2/2] perf/core: Fix WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0) in perf_cgroup_switch Luo Gengkun
1 sibling, 0 replies; 3+ messages in thread
From: Luo Gengkun @ 2025-05-14 6:47 UTC (permalink / raw)
To: peterz
Cc: mingo, acme, namhyung, mark.rutland, alexander.shishkin, jolsa,
irogers, adrian.hunter, kan.liang, yeoreum.yun, linux-perf-users,
linux-kernel
Since __perf_remove_from_context updates event->state before list_del_event,
this prevents list_del_event from calling perf_cgroup_event_disable,
resulting in will not update nr_cgroups and cpuctx->cgrp.
To resolve this issue:
1. First update the event time to ensure perf_child_detach uses
accurate event time.
2. Then update event->state after list_del_event completes.
Fixes: a3c3c66670ce ("perf/core: Fix child_total_time_enabled accounting bug at task exit")
Signed-off-by: Luo Gengkun <luogengkun@huaweicloud.com>
---
kernel/events/core.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 95e703891b24..ecb4d852a006 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2482,12 +2482,17 @@ __perf_remove_from_context(struct perf_event *event,
state = PERF_EVENT_STATE_DEAD;
}
event_sched_out(event, ctx);
- perf_event_set_state(event, min(event->state, state));
+ perf_event_update_time(event);
if (flags & DETACH_GROUP)
perf_group_detach(event);
if (flags & DETACH_CHILD)
perf_child_detach(event);
list_del_event(event, ctx);
+ /*
+ * Update the event->state after list_del_event.
+ */
+ if (flags & (DETACH_DEAD | DETACH_EXIT))
+ event->state = state;
if (!pmu_ctx->nr_events) {
pmu_ctx->rotate_necessary = 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH Fix perf cgroup problem 2/2] perf/core: Fix WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0) in perf_cgroup_switch
2025-05-14 6:47 [PATCH Fix perf cgroup problem 0/2] Luo Gengkun
2025-05-14 6:47 ` [PATCH Fix perf cgroup problem 1/2] perf/core: Fix nr_cgroups/cpuctx->cgrp is not updated correctly Luo Gengkun
@ 2025-05-14 6:47 ` Luo Gengkun
1 sibling, 0 replies; 3+ messages in thread
From: Luo Gengkun @ 2025-05-14 6:47 UTC (permalink / raw)
To: peterz
Cc: mingo, acme, namhyung, mark.rutland, alexander.shishkin, jolsa,
irogers, adrian.hunter, kan.liang, yeoreum.yun, linux-perf-users,
linux-kernel
There may be concurrency between perf_cgroup_switch and
perf_cgroup_event_disable. Consider the following scenario: after a new
perf cgroup event is created on CPU0, the new event might not trigger
reprogramm, leaving ctx->is_active as 0. In this case, when CPU1 disables
the perf event, it executes __perf_remove_from_context->list_del_event->
perf_cgroup_event_disable on CPU1, resulting in a race with
perf_cgroup_switch running on CPU0.
To fix this problem, expand the lock-holding critical section in
perf_cgroup_switch.
Fixes: db4a835601b7 ("perf/core: Set cgroup in CPU contexts for new cgroup events")
Signed-off-by: Luo Gengkun <luogengkun@huaweicloud.com>
---
kernel/events/core.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index ecb4d852a006..fae1f68cbca5 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -931,20 +931,20 @@ static void perf_cgroup_switch(struct task_struct *task)
struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context);
struct perf_cgroup *cgrp;
+ cgrp = perf_cgroup_from_task(task, NULL);
+ perf_ctx_lock(cpuctx, cpuctx->task_ctx);
/*
* cpuctx->cgrp is set when the first cgroup event enabled,
* and is cleared when the last cgroup event disabled.
*/
if (READ_ONCE(cpuctx->cgrp) == NULL)
- return;
+ goto unlock;
WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
- cgrp = perf_cgroup_from_task(task, NULL);
if (READ_ONCE(cpuctx->cgrp) == cgrp)
- return;
+ goto unlock;
- perf_ctx_lock(cpuctx, cpuctx->task_ctx);
perf_ctx_disable(&cpuctx->ctx, true);
ctx_sched_out(&cpuctx->ctx, NULL, EVENT_ALL|EVENT_CGROUP);
@@ -962,6 +962,7 @@ static void perf_cgroup_switch(struct task_struct *task)
ctx_sched_in(&cpuctx->ctx, NULL, EVENT_ALL|EVENT_CGROUP);
perf_ctx_enable(&cpuctx->ctx, true);
+unlock:
perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-05-14 6:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-14 6:47 [PATCH Fix perf cgroup problem 0/2] Luo Gengkun
2025-05-14 6:47 ` [PATCH Fix perf cgroup problem 1/2] perf/core: Fix nr_cgroups/cpuctx->cgrp is not updated correctly Luo Gengkun
2025-05-14 6:47 ` [PATCH Fix perf cgroup problem 2/2] perf/core: Fix WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0) in perf_cgroup_switch Luo Gengkun
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).