All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: kan.liang@linux.intel.com
Cc: mingo@redhat.com, tglx@linutronix.de, bp@alien8.de,
	acme@kernel.org, namhyung@kernel.org, irogers@google.com,
	linux-kernel@vger.kernel.org, ak@linux.intel.com,
	eranian@google.com
Subject: Re: [PATCH V10 3/7] perf: attach/detach PMU specific data
Date: Fri, 14 Mar 2025 22:05:45 +0100	[thread overview]
Message-ID: <20250314210545.GB21786@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20250314172700.438923-3-kan.liang@linux.intel.com>

On Fri, Mar 14, 2025 at 10:26:56AM -0700, kan.liang@linux.intel.com wrote:

> @@ -5393,6 +5607,9 @@ static void __free_event(struct perf_event *event)
>  	if (is_cgroup_event(event))
>  		perf_detach_cgroup(event);
>  
> +	if (event->attach_state & PERF_ATTACH_TASK_DATA)
> +		detach_perf_ctx_data(event);
> +
>  	if (event->destroy)
>  		event->destroy(event);
>  

> @@ -12481,6 +12746,18 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
>  	if (IS_ERR(pmu))
>  		return (void*)pmu;
>  
> +	/*
> +	 * The PERF_ATTACH_TASK_DATA is set in the event_init()->hw_config().
> +	 * The attach should be right after the perf_init_event().
> +	 * Otherwise, the __free_event() would mistakenly detach the non-exist
> +	 * perf_ctx_data because of the other errors between them.
> +	 */
> +	if (event->attach_state & PERF_ATTACH_TASK_DATA) {
> +		err = attach_perf_ctx_data(event);
> +		if (err)
> +			return ERR_PTR(err);
> +	}
> +
>  	/*
>  	 * Disallow uncore-task events. Similarly, disallow uncore-cgroup
>  	 * events (they don't make sense as the cgroup will be different

I've stuck this on top. Let me see if it all compiles and push out to
queue/perf/core.

--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -666,11 +666,12 @@ struct swevent_hlist {
 #define PERF_ATTACH_GROUP	0x0002
 #define PERF_ATTACH_TASK	0x0004
 #define PERF_ATTACH_TASK_DATA	0x0008
-#define PERF_ATTACH_ITRACE	0x0010
+#define PERF_ATTACH_GLOBAL_DATA	0x0010
 #define PERF_ATTACH_SCHED_CB	0x0020
 #define PERF_ATTACH_CHILD	0x0040
 #define PERF_ATTACH_EXCLUSIVE	0x0080
 #define PERF_ATTACH_CALLCHAIN	0x0100
+#define PERF_ATTACH_ITRACE	0x0200
 
 struct bpf_prog;
 struct perf_cgroup;
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5278,14 +5278,19 @@ attach_perf_ctx_data(struct perf_event *
 {
 	struct task_struct *task = event->hw.target;
 	struct kmem_cache *ctx_cache = event->pmu->task_ctx_cache;
+	int ret;
 
 	if (!ctx_cache)
 		return -ENOMEM;
 
 	if (task)
 		return attach_task_ctx_data(task, ctx_cache, false);
-	else
-		return attach_global_ctx_data(ctx_cache);
+
+	ret = attach_global_ctx_data(ctx_cache);
+	if (ret)
+		return ret;
+
+	event->attach_state |= PERF_ATTACH_GLOBAL_DATA;
 }
 
 static void
@@ -5348,13 +5353,15 @@ static void detach_perf_ctx_data(struct
 {
 	struct task_struct *task = event->hw.target;
 
-	if (!event->pmu->task_ctx_cache)
-		return;
+	event->attach_state &= ~PERF_ATTACH_TASK_DATA;
 
 	if (task)
-		detach_task_ctx_data(task);
-	else
+		return detach_task_ctx_data(task);
+
+	if (event->attach_state & PERF_ATTACH_GLOBAL_DATA) {
 		detach_global_ctx_data();
+		event->attach_state &= ~PERF_ATTACH_GLOBAL_DATA;
+	}
 }
 
 static void unaccount_event(struct perf_event *event)

  reply	other threads:[~2025-03-14 21:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-14 17:26 [PATCH V10 1/7] perf: Save PMU specific data in task_struct kan.liang
2025-03-14 17:26 ` [PATCH V10 2/7] locking/percpu-rwsem: Add guard support kan.liang
2025-03-17 10:34   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra (Intel)
2025-03-14 17:26 ` [PATCH V10 3/7] perf: attach/detach PMU specific data kan.liang
2025-03-14 21:05   ` Peter Zijlstra [this message]
2025-03-17 10:34   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-03-14 17:26 ` [PATCH V10 4/7] perf: Supply task information to sched_task() kan.liang
2025-03-17 10:34   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-03-14 17:26 ` [PATCH V10 5/7] perf/x86/lbr: Fix shorter LBRs call stacks for the system-wide mode kan.liang
2025-03-17 10:34   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-03-14 17:26 ` [PATCH V10 6/7] perf/x86: Remove swap_task_ctx() kan.liang
2025-03-17 10:34   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-03-14 17:27 ` [PATCH V10 7/7] perf: Clean up pmu specific data kan.liang
2025-03-17 10:34   ` [tip: perf/core] " tip-bot2 for Kan Liang
2025-03-17 10:34 ` [tip: perf/core] perf: Save PMU specific data in task_struct tip-bot2 for Kan Liang
2025-03-17 11:10 ` [PATCH V10 1/7] " Peter Zijlstra
2025-03-24 14:07   ` Liang, Kan

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=20250314210545.GB21786@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.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.