All of lore.kernel.org
 help / color / mirror / Atom feed
From: kan.liang@linux.intel.com
To: peterz@infradead.org, mingo@redhat.com, tglx@linutronix.de,
	bp@alien8.de, acme@kernel.org, namhyung@kernel.org,
	irogers@google.com, linux-kernel@vger.kernel.org
Cc: ak@linux.intel.com, eranian@google.com,
	Kan Liang <kan.liang@linux.intel.com>
Subject: [PATCH] perf: Only one Kmem cache for the per-task PMU-specific data
Date: Wed, 26 Mar 2025 06:52:52 -0700	[thread overview]
Message-ID: <20250326135252.1690293-1-kan.liang@linux.intel.com> (raw)

From: Kan Liang <kan.liang@linux.intel.com>

Some PMU-specific data has to be saved/restored during context switch,
e.g. LBR call stack data. The data is saved in the kmem_cache *ctx_cache
of task_struct. However, the current implementation only supports one
user.

There is no problem for now, because the Intel LBR call stack is the
only user. But if other PMUs also want to add a new kmem_cache to save
their specific data later, there must be a bug.

Add a global pointer perf_ctx_cache to save the kmem_cache address of a
PMU for the PMU-specific data. If a later PMU claims a different
address, fail the registration.
(There could be two or more PMUs which share the same kmem_cache
address, e.g., Intel hybrid. So, here can only use the address rather
than the number of PMUs for the check.)

Suggested-by: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Closes: https://lore.kernel.org/lkml/20250317111045.GA36386@noisy.programming.kicks-ass.net/
---
 kernel/events/core.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4ce9795e5519..106fd554fc42 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -443,6 +443,7 @@ static cpumask_var_t perf_online_cluster_mask;
 static cpumask_var_t perf_online_pkg_mask;
 static cpumask_var_t perf_online_sys_mask;
 static struct kmem_cache *perf_event_cache;
+static struct kmem_cache *perf_ctx_cache;
 
 /*
  * perf event paranoia level:
@@ -12189,6 +12190,21 @@ int perf_pmu_register(struct pmu *_pmu, const char *name, int type)
 		      "Can not register a pmu with an invalid scope.\n"))
 		return -EINVAL;
 
+	if (pmu->task_ctx_cache) {
+		/*
+		 * The PMU-specific data is saved in the Kmem cache
+		 * ctx_cache of task_struct. It only supports one
+		 * user. Check and fail the registration if there
+		 * are more potential users.
+		 */
+		if (!perf_ctx_cache)
+			perf_ctx_cache = pmu->task_ctx_cache;
+		if (WARN_ONCE(perf_ctx_cache != pmu->task_ctx_cache,
+			      "The PMU-specific buffer of task has been reserved by other PMUs.\n"))
+			return -EINVAL;
+
+	}
+
 	pmu->name = name;
 
 	if (type >= 0)
-- 
2.38.1


                 reply	other threads:[~2025-03-26 13:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20250326135252.1690293-1-kan.liang@linux.intel.com \
    --to=kan.liang@linux.intel.com \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /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.