From: Frederic Weisbecker <frederic@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: linux-kernel@vger.kernel.org, linux-tip-commits@vger.kernel.org,
Ravi Bangoria <ravi.bangoria@amd.com>,
x86@kernel.org
Subject: Re: [tip: perf/core] perf: Simplify perf_event_free_task() wait
Date: Thu, 10 Apr 2025 11:45:49 +0200 [thread overview]
Message-ID: <Z_eTTf6RBeHQ8bmT@localhost.localdomain> (raw)
In-Reply-To: <20250410093456.GA17321@noisy.programming.kicks-ass.net>
Le Thu, Apr 10, 2025 at 11:34:56AM +0200, Peter Zijlstra a écrit :
> On Wed, Apr 09, 2025 at 03:01:12PM +0200, Frederic Weisbecker wrote:
>
> > > diff --git a/kernel/events/core.c b/kernel/events/core.c
> > > index 3c92b75..fa6dab0 100644
> > > --- a/kernel/events/core.c
> > > +++ b/kernel/events/core.c
> > > @@ -1270,6 +1270,9 @@ static void put_ctx(struct perf_event_context *ctx)
> > > if (ctx->task && ctx->task != TASK_TOMBSTONE)
> > > put_task_struct(ctx->task);
> > > call_rcu(&ctx->rcu_head, free_ctx);
> > > + } else if (ctx->task == TASK_TOMBSTONE) {
> > > + smp_mb(); /* pairs with wait_var_event() */
> > > + wake_up_var(&ctx->refcount);
> >
> > So there are three situations:
> >
> > * If perf_event_free_task() has removed all the children from the parent list
> > before perf_event_release_kernel() got a chance to even iterate them, then
> > it's all good as there is no get_ctx() pending.
> >
> > * If perf_event_release_kernel() iterates a child event, but it gets freed
> > meanwhile by perf_event_free_task() while the mutexes are temporarily
> > unlocked, it's all good because while locking again the ctx mutex,
> > perf_event_release_kernel() observes TASK_TOMBSTONE.
> >
> > * But if perf_event_release_kernel() frees the child event before
> > perf_event_free_task() got a chance, we may face this scenario:
> >
> > perf_event_release_kernel() perf_event_free_task()
> > -------------------------- ------------------------
> > mutex_lock(&event->child_mutex)
> > get_ctx(child->ctx)
> > mutex_unlock(&event->child_mutex)
> >
> > mutex_lock(ctx->mutex)
> > mutex_lock(&event->child_mutex)
> > perf_remove_from_context(child)
> > mutex_unlock(&event->child_mutex)
> > mutex_unlock(ctx->mutex)
> >
> > // This lock acquires ctx->refcount == 2
> > // visibility
> > mutex_lock(ctx->mutex)
> > ctx->task = TASK_TOMBSTONE
> > mutex_unlock(ctx->mutex)
> >
> > wait_var_event()
> > // enters prepare_to_wait() since
> > // ctx->refcount == 2
> > // is guaranteed to be seen
> > set_current_state(TASK_INTERRUPTIBLE)
> > smp_mb()
> > if (ctx->refcount != 1)
> > schedule()
> > put_ctx()
> > // NOT fully ordered! Only RELEASE semantics
> > refcount_dec_and_test()
> > atomic_fetch_sub_release()
> > // So TASK_TOMBSTONE is not guaranteed to be seen
> > if (ctx->task == TASK_TOMBSTONE)
> > wake_up_var()
> >
> > Basically it's a broken store buffer:
> >
> > perf_event_release_kernel() perf_event_free_task()
> > -------------------------- ------------------------
> > ctx->task = TASK_TOMBSTONE smp_store_release(&ctx->refcount, ctx->refcount - 1)
> > smp_mb()
> > READ_ONCE(ctx->refcount) READ_ONCE(ctx->task)
> >
> >
> > So you need this:
> >
> > diff --git a/kernel/events/core.c b/kernel/events/core.c
> > index fa6dab08be47..c4fbbe25361a 100644
> > --- a/kernel/events/core.c
> > +++ b/kernel/events/core.c
> > @@ -1270,9 +1270,10 @@ static void put_ctx(struct perf_event_context *ctx)
> > if (ctx->task && ctx->task != TASK_TOMBSTONE)
> > put_task_struct(ctx->task);
> > call_rcu(&ctx->rcu_head, free_ctx);
> > - } else if (ctx->task == TASK_TOMBSTONE) {
> > + } else {
> > smp_mb(); /* pairs with wait_var_event() */
> > - wake_up_var(&ctx->refcount);
> > + if (ctx->task == TASK_TOMBSTONE)
> > + wake_up_var(&ctx->refcount);
> > }
> > }
>
> Very good, thanks!
>
> I'll make that smp_mb__after_atomic() instead, but yes, this barrier
> needs to move before the loading of ctx->task.
>
> I'll transform this into a patch and stuff on top.
Sure! Or feel free to fold it, though that imply a rebase...
Thanks.
--
Frederic Weisbecker
SUSE Labs
next prev parent reply other threads:[~2025-04-10 9:45 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-07 19:33 [PATCH v3 0/7] perf: Make perf_pmu_unregister() usable Peter Zijlstra
2025-03-07 19:33 ` [PATCH v3 1/7] perf: Ensure bpf_perf_link path is properly serialized Peter Zijlstra
2025-03-07 19:33 ` [PATCH v3 2/7] perf: Simplify child event tear-down Peter Zijlstra
2025-03-07 19:33 ` [PATCH v3 3/7] perf: Simplify perf_event_free_task() wait Peter Zijlstra
2025-03-17 6:49 ` Ravi Bangoria
2025-04-02 9:15 ` Peter Zijlstra
2025-04-08 19:05 ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-04-09 13:01 ` Frederic Weisbecker
2025-04-10 9:34 ` Peter Zijlstra
2025-04-10 9:45 ` Frederic Weisbecker [this message]
2025-04-17 12:03 ` Ingo Molnar
2025-04-17 13:01 ` [tip: perf/core] perf/core: Fix put_ctx() ordering tip-bot2 for Frederic Weisbecker
2025-03-07 19:33 ` [PATCH v3 4/7] perf: Simplify perf_event_release_kernel() Peter Zijlstra
2025-04-08 19:05 ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-03-07 19:33 ` [PATCH v3 5/7] perf: Unify perf_event_free_task() / perf_event_exit_task_context() Peter Zijlstra
2025-03-10 15:35 ` [PATCH v3a " Peter Zijlstra
2025-04-08 19:05 ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-03-07 19:33 ` [PATCH v3 6/7] perf: Rename perf_event_exit_task(.child) Peter Zijlstra
2025-03-10 11:08 ` Ravi Bangoria
2025-03-10 14:47 ` Peter Zijlstra
2025-03-10 15:20 ` Ravi Bangoria
2025-03-10 15:27 ` Peter Zijlstra
2025-03-10 15:37 ` [PATCH v3a " Peter Zijlstra
2025-03-12 6:31 ` Ravi Bangoria
2025-03-12 10:16 ` Peter Zijlstra
2025-03-07 19:33 ` [PATCH v3 7/7] perf: Make perf_pmu_unregister() useable Peter Zijlstra
2025-03-10 15:35 ` Ravi Bangoria
2025-03-10 16:14 ` Peter Zijlstra
2025-03-10 16:46 ` Ravi Bangoria
2025-03-12 12:57 ` Peter Zijlstra
2025-03-12 13:57 ` Ravi Bangoria
2025-04-08 19:05 ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-04-17 8:08 ` Peter Zijlstra
2025-04-17 13:01 ` [tip: perf/core] perf/core: Fix event timekeeping merge tip-bot2 for Peter Zijlstra
2025-04-14 0:37 ` [PATCH v3 7/7] perf: Make perf_pmu_unregister() useable Mi, Dapeng
2025-04-17 8:07 ` Peter Zijlstra
2025-04-17 8:24 ` Mi, Dapeng
2025-04-17 11:30 ` [tip: perf/core] perf/core: Fix perf-stat / read() tip-bot2 for Peter Zijlstra
2025-04-17 13:01 ` tip-bot2 for Peter Zijlstra
2025-03-17 6:54 ` [PATCH v3 0/7] perf: Make perf_pmu_unregister() usable Ravi Bangoria
2025-04-08 19:05 ` [tip: perf/core] perf: Rename perf_event_exit_task(.child) tip-bot2 for Peter Zijlstra
2025-04-08 19:05 ` [tip: perf/core] perf: Simplify child event tear-down tip-bot2 for Peter Zijlstra
2025-04-08 19:05 ` [tip: perf/core] perf: Ensure bpf_perf_link path is properly serialized tip-bot2 for 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=Z_eTTf6RBeHQ8bmT@localhost.localdomain \
--to=frederic@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=ravi.bangoria@amd.com \
--cc=x86@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.