Re: [tip: perf/urgent] perf/core: Fix WARN in perf_cgroup_switch()

From: Peter Zijlstra
Date: Thu Jun 26 2025 - 09:14:31 EST


On Thu, Jun 26, 2025 at 09:08:38PM +0800, Luo Gengkun wrote:

> Sorry for the late reply, I found that the link is v2 instead of v3.
> This v3 link is:
>
> https://lore.kernel.org/all/20250609035316.250557-1-luogengkun@xxxxxxxxxxxxxxx/
>
> v2 attempts to fix a concurrency problem between perf_cgroup_switch
> and perf_cgroup_event_disable. But it does not to move WARN_ON_ONCE
> into lock-protected region, so the warning is still triggered.
>
> The following patches have been tested and fix this issue.
>
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 1f746469fda5..a35784d42c66 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -951,8 +951,6 @@ static void perf_cgroup_switch(struct task_struct *task)
> if (READ_ONCE(cpuctx->cgrp) == NULL)
> return;
>
> - WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
> -
> cgrp = perf_cgroup_from_task(task, NULL);
> if (READ_ONCE(cpuctx->cgrp) == cgrp)
> return;
> @@ -964,6 +962,8 @@ static void perf_cgroup_switch(struct task_struct *task)
> if (READ_ONCE(cpuctx->cgrp) == NULL)
> return;
>
> + WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
> +
> perf_ctx_disable(&cpuctx->ctx, true);
>
> ctx_sched_out(&cpuctx->ctx, NULL, EVENT_ALL|EVENT_CGROUP);

Can you send as a full new patch, the thing is already in Linus' tree.