Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Minwoo Ahn" <mwahn402@gmail.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v3] perf/core: Fix sampling period inconsistency across CPU migration
Date: Wed, 29 Apr 2026 09:06:04 +0000	[thread overview]
Message-ID: <20260429090606.1C51EC19425@smtp.kernel.org> (raw)
In-Reply-To: <20260429074328.41991-1-mwahn402@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing put_ctx() in perf_get_task_ctxp() causes a permanent reference leak of perf_event_context and task_struct.
- [High] Erroneous perf_task_ctxp sharing clobbers states of independent profiling sessions.
--

commit 8823a0e265a5483fceb1f344697e122c9270c810
Author: Minwoo Ahn <mwahn402@gmail.com>

perf/core: Fix sampling period inconsistency across CPU migration

This commit introduces struct perf_task_context to preserve period_left across
CPU migrations for per-task software sampling events. It adds allocation and
refcounting logic to share this context among related events on the same task.

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 6d1f8bad7e1c..f5cb1a273fbf 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
[ ... ]
> @@ -13259,6 +13291,41 @@ static void account_event(struct perf_event *event)
>  	account_pmu_sb_event(event);
>  }
>  
> +static struct perf_task_context *
> +perf_get_task_ctxp(struct perf_event *event, struct task_struct *task,
> +		   bool inherit)
> +{
> +	struct perf_task_context *ctxp = NULL;
> +	struct perf_event_context *ctx;
> +	struct perf_event *iter;
> +	unsigned long flags;
> +
> +	ctx = perf_lock_task_context(task, &flags);
> +	if (ctx) {
> +		list_for_each_entry(iter, &ctx->event_list, event_entry) {
> +			if (iter->perf_task_ctxp &&
> +			    (iter->owner == current ||
> +			     (inherit && !iter->owner)) &&

For inherited child events, both iter->owner and event->owner will be NULL.
If a parent task is being profiled by two separate and independent perf
sessions, will this (inherit && !iter->owner) condition inadvertently merge
both child events into a single perf_task_context?

Would it be safer to verify parent lineage, for example by checking if
iter->parent->perf_task_ctxp equals event->parent->perf_task_ctxp, to
ensure the inherited events actually belong to the same profiling session?

> +			    perf_event_equal_task_ctx(&iter->attr,
> +						     &event->attr)) {

Could this logic mistakenly merge the sampling states of completely
independent perf events in the same task?

If a process opens multiple distinct sampling events for the same task with
identical configurations and sample periods, they appear to pass this
equality check. Since they share the same perf_task_context, won't they
corrupt each other's period_left states when the task schedules out?

> +				ctxp = iter->perf_task_ctxp;
> +				refcount_inc(&ctxp->refcount);
> +				break;
> +			}
> +		}
> +		raw_spin_unlock_irqrestore(&ctx->lock, flags);

The perf_lock_task_context() helper takes a reference on the returned ctx
using refcount_inc_not_zero().

Does this path leak the perf_event_context (and consequently the pinned
task_struct) by returning without calling put_ctx(ctx) to drop that
reference?

> +	}
> +
> +	if (!ctxp) {
> +		ctxp = kzalloc_obj(struct perf_task_context);
> +		if (!ctxp)
> +			return NULL;
> +		refcount_set(&ctxp->refcount, 1);
> +	}
> +
> +	return ctxp;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260429074328.41991-1-mwahn402@gmail.com?part=1

      reply	other threads:[~2026-04-29  9:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-29  7:43 [PATCH v3] perf/core: Fix sampling period inconsistency across CPU migration Minwoo Ahn
2026-04-29  9:06 ` sashiko-bot [this message]

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=20260429090606.1C51EC19425@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mwahn402@gmail.com \
    --cc=sashiko@lists.linux.dev \
    /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