[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230612093539.085862001@infradead.org>
Date: Mon, 12 Jun 2023 11:07:35 +0200
From: Peter Zijlstra <peterz@...radead.org>
To: torvalds@...ux-foundation.org, keescook@...omium.org,
gregkh@...uxfoundation.org, pbonzini@...hat.com
Cc: masahiroy@...nel.org, nathan@...nel.org, ndesaulniers@...gle.com,
nicolas@...sle.eu, catalin.marinas@....com, will@...nel.org,
vkoul@...nel.org, trix@...hat.com, ojeda@...nel.org,
peterz@...radead.org, mingo@...hat.com, longman@...hat.com,
boqun.feng@...il.com, dennis@...nel.org, tj@...nel.org,
cl@...ux.com, acme@...nel.org, mark.rutland@....com,
alexander.shishkin@...ux.intel.com, jolsa@...nel.org,
namhyung@...nel.org, irogers@...gle.com, adrian.hunter@...el.com,
juri.lelli@...hat.com, vincent.guittot@...aro.org,
dietmar.eggemann@....com, rostedt@...dmis.org, bsegall@...gle.com,
mgorman@...e.de, bristot@...hat.com, vschneid@...hat.com,
paulmck@...nel.org, frederic@...nel.org, quic_neeraju@...cinc.com,
joel@...lfernandes.org, josh@...htriplett.org,
mathieu.desnoyers@...icios.com, jiangshanlai@...il.com,
rientjes@...gle.com, vbabka@...e.cz, roman.gushchin@...ux.dev,
42.hyeyoo@...il.com, apw@...onical.com, joe@...ches.com,
dwaipayanray1@...il.com, lukas.bulwahn@...il.com,
john.johansen@...onical.com, paul@...l-moore.com,
jmorris@...ei.org, serge@...lyn.com, linux-kbuild@...r.kernel.org,
linux-kernel@...r.kernel.org, dmaengine@...r.kernel.org,
llvm@...ts.linux.dev, linux-perf-users@...r.kernel.org,
rcu@...r.kernel.org, linux-security-module@...r.kernel.org,
tglx@...utronix.de, ravi.bangoria@....com, error27@...il.com,
luc.vanoostenryck@...il.com
Subject: [PATCH v3 22/57] perf: Fix cpuctx refcounting
Fixes: bd2756811766 ("perf: Rewrite core context handling")
Signed-off-by: Peter Zijlstra (Intel) <peterz@...radead.org>
---
include/linux/perf_event.h | 13 ++++++++-----
kernel/events/core.c | 16 ++++++++++++++++
2 files changed, 24 insertions(+), 5 deletions(-)
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -841,11 +841,11 @@ struct perf_event {
};
/*
- * ,-----------------------[1:n]----------------------.
- * V V
- * perf_event_context <-[1:n]-> perf_event_pmu_context <--- perf_event
- * ^ ^ | |
- * `--------[1:n]---------' `-[n:1]-> pmu <-[1:n]-'
+ * ,-----------------------[1:n]------------------------.
+ * V V
+ * perf_event_context <-[1:n]-> perf_event_pmu_context <-[1:n]- perf_event
+ * | |
+ * `--[n:1]-> pmu <-[1:n]--'
*
*
* struct perf_event_pmu_context lifetime is refcount based and RCU freed
@@ -863,6 +863,9 @@ struct perf_event {
* ctx->mutex pinning the configuration. Since we hold a reference on
* group_leader (through the filedesc) it can't go away, therefore it's
* associated pmu_ctx must exist and cannot change due to ctx->mutex.
+ *
+ * perf_event holds a refcount on perf_event_context
+ * perf_event holds a refcount on perf_event_pmu_context
*/
struct perf_event_pmu_context {
struct pmu *pmu;
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -4809,6 +4809,11 @@ find_get_pmu_context(struct pmu *pmu, st
void *task_ctx_data = NULL;
if (!ctx->task) {
+ /*
+ * perf_pmu_migrate_context() / __perf_pmu_install_event()
+ * relies on the fact that find_get_pmu_context() cannot fail
+ * for CPU contexts.
+ */
struct perf_cpu_pmu_context *cpc;
cpc = per_cpu_ptr(pmu->cpu_pmu_context, event->cpu);
@@ -12832,6 +12837,13 @@ static void __perf_pmu_install_event(str
{
struct perf_event_pmu_context *epc;
+ /*
+ * Now that the events are unused, put their old ctx and grab a
+ * reference on the new context.
+ */
+ put_ctx(event->ctx);
+ get_ctx(ctx);
+
event->cpu = cpu;
epc = find_get_pmu_context(pmu, ctx, event);
event->pmu_ctx = epc;
@@ -12877,6 +12889,10 @@ void perf_pmu_migrate_context(struct pmu
struct perf_event_context *src_ctx, *dst_ctx;
LIST_HEAD(events);
+ /*
+ * Since per-cpu context is persistent, no need to grab an extra
+ * reference.
+ */
src_ctx = &per_cpu_ptr(&perf_cpu_context, src_cpu)->ctx;
dst_ctx = &per_cpu_ptr(&perf_cpu_context, dst_cpu)->ctx;
Powered by blists - more mailing lists