From: Peter Zijlstra <peterz@infradead.org>
To: Luo Gengkun <luogengkun@huaweicloud.com>
Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org,
mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com,
kan.liang@linux.intel.com, davidcc@google.com,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] perf/core: Fix WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0) in perf_cgroup_switch
Date: Thu, 5 Jun 2025 09:36:00 +0200 [thread overview]
Message-ID: <20250605073600.GN39944@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <83d9c5e8-9c72-4ab8-a3ac-638e49691694@huaweicloud.com>
On Thu, Jun 05, 2025 at 11:55:03AM +0800, Luo Gengkun wrote:
>
> On 2025/6/4 18:00, Peter Zijlstra wrote:
> > On Wed, Jun 04, 2025 at 03:39:24AM +0000, Luo Gengkun wrote:
> > > 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 may not trigger
> > > a reprogramming, causing ctx->is_active to be 0. In this case, when CPU1
> > > disables this perf event, it executes __perf_remove_from_context->
> > > list _del_event->perf_cgroup_event_disable on CPU1, which causes a race
> > > with perf_cgroup_switch running on CPU0.
> > >
> > > The following describes the details of this concurrency scenario:
> > >
> > > CPU0 CPU1
> > >
> > > perf_cgroup_switch:
> > > ...
> > > # cpuctx->cgrp is not NULL here
> > > if (READ_ONCE(cpuctx->cgrp) == NULL)
> > > return;
> > >
> > > perf_remove_from_context:
> > > ...
> > > raw_spin_lock_irq(&ctx->lock);
> > > ...
> > > # ctx->is_active == 0 because reprogramm is not
> > > # tigger, so CPU1 can do __perf_remove_from_context
> > > # for CPU0
> > > __perf_remove_from_context:
> > > perf_cgroup_event_disable:
> > > ...
> > > if (--ctx->nr_cgroups)
> > > ...
> > >
> > > # this warning will happened because CPU1 changed
> > > # ctx.nr_cgroups to 0.
> > > WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
> > >
> > > 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>
> > > ---
> > Right, so how about we simply re-check the condition once we take the
> > lock?
> >
> > Also, take the opportunity to convert to guard instead of adding goto
> > unlock.
> >
> > --- a/kernel/events/core.c
> > +++ b/kernel/events/core.c
> > @@ -207,6 +207,19 @@ static void perf_ctx_unlock(struct perf_
> > __perf_ctx_unlock(&cpuctx->ctx);
> > }
> > +typedef struct {
> > + struct perf_cpu_context *cpuctx;
> > + struct perf_event_context *ctx;
> > +} class_perf_ctx_lock_t;
> > +
> > +static inline void class_perf_ctx_lock_destructor(class_perf_ctx_lock_t *_T)
> > +{ perf_ctx_unlock(_T->cpuctx, _T->ctx); }
> > +
> > +static inline class_perf_ctx_lock_t
> > +class_perf_ctx_lock_constructor(struct perf_cpu_context *cpuctx,
> > + struct perf_event_context *ctx)
> > +{ perf_ctx_lock(cpuctx, ctx); return (class_perf_ctx_lock_t){ cpuctx, ctx }; }
> > +
> > #define TASK_TOMBSTONE ((void *)-1L)
> > static bool is_kernel_event(struct perf_event *event)
> > @@ -944,7 +957,13 @@ static void perf_cgroup_switch(struct ta
> > if (READ_ONCE(cpuctx->cgrp) == cgrp)
> > return;
> > - perf_ctx_lock(cpuctx, cpuctx->task_ctx);
> > + guard(perf_ctx_lock)(cpuctx, cpuctx->task_ctx);
> > + /*
> > + * Re-check, could've raced vs perf_remove_from_context().
> > + */
> > + if (READ_ONCE(cpuctx->cgrp) == NULL)
> > + return;
> > +
> > perf_ctx_disable(&cpuctx->ctx, true);
> > ctx_sched_out(&cpuctx->ctx, NULL, EVENT_ALL|EVENT_CGROUP);
> > @@ -962,7 +981,6 @@ static void perf_cgroup_switch(struct ta
> > ctx_sched_in(&cpuctx->ctx, NULL, EVENT_ALL|EVENT_CGROUP);
> > perf_ctx_enable(&cpuctx->ctx, true);
> > - perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
> > }
> > static int perf_cgroup_ensure_storage(struct perf_event *event,
>
> Thank for your review, I will make changes based on your suggestions.
>
No need to resend. I've got your patch with modifications. But please
confirm it does work :-)
next prev parent reply other threads:[~2025-06-05 7:36 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-04 3:39 [PATCH v2 0/2] Fix perf cgroup problem Luo Gengkun
2025-06-04 3:39 ` [PATCH v2 1/2] perf/core: Fix nr_cgroups/cpuctx->cgrp is not updated correctly Luo Gengkun
2025-06-04 9:19 ` Peter Zijlstra
2025-06-04 9:42 ` Luo Gengkun
2025-06-04 3:39 ` [PATCH v2 2/2] perf/core: Fix WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0) in perf_cgroup_switch Luo Gengkun
2025-06-04 10:00 ` Peter Zijlstra
2025-06-05 3:55 ` Luo Gengkun
2025-06-05 7:36 ` Peter Zijlstra [this message]
2025-06-11 9:29 ` [tip: perf/urgent] perf/core: Fix WARN in perf_cgroup_switch() tip-bot2 for Luo Gengkun
2025-06-26 13:08 ` Luo Gengkun
2025-06-26 13:13 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250605073600.GN39944@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=davidcc@google.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=luogengkun@huaweicloud.com \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®