public inbox for linux-s390@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: acme@kernel.org, alexander.shishkin@linux.intel.com,
	jolsa@redhat.com, namhyung@kernel.org, songliubraving@fb.com,
	eranian@google.com, ak@linux.intel.com, mark.rutland@arm.com,
	frederic@kernel.org, maddy@linux.ibm.com, irogers@google.com,
	will@kernel.org, robh@kernel.org, mingo@redhat.com,
	catalin.marinas@arm.com, ndesaulniers@google.com,
	srw@sladewatkins.net, linux-arm-kernel@lists.infradead.org,
	linux-perf-users@vger.kernel.org, linuxppc-dev@lists.ozlabs.org,
	linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org,
	sandipan.das@amd.com, ananth.narayan@amd.com,
	kim.phillips@amd.com, santosh.shukla@amd.com
Subject: Re: [PATCH v2] perf: Rewrite core context handling
Date: Wed, 12 Oct 2022 14:16:29 +0200	[thread overview]
Message-ID: <Y0awHa8oS5yal5M9@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <174fb540-ec18-eeca-191d-c02e1f1005d2@amd.com>

On Wed, Oct 12, 2022 at 02:09:00PM +0530, Ravi Bangoria wrote:

> > @@ -3366,6 +3370,14 @@ static void perf_event_sync_stat(struct
> >  	}
> >  }
> >  
> > +#define list_for_each_entry_double(pos1, pos2, head1, head2, member)	\
> > +	for (pos1 = list_first_entry(head1, typeof(*pos1), member),	\
> > +	     pos2 = list_first_entry(head2, typeof(*pos2), member);	\
> > +	     !list_entry_is_head(pos1, head1, member) &&		\
> > +	     !list_entry_is_head(pos2, head2, member);			\
> > +	     pos1 = list_next_entry(pos1, member),			\
> > +	     pos2 = list_next_entry(pos2, member))
> > +
> >  static void perf_event_swap_task_ctx_data(struct perf_event_context *prev_ctx,
> >  					  struct perf_event_context *next_ctx)
> >  {
> > @@ -3374,16 +3386,9 @@ static void perf_event_swap_task_ctx_dat
> >  	if (!prev_ctx->nr_task_data)
> >  		return;
> >  
> > -	prev_epc = list_first_entry(&prev_ctx->pmu_ctx_list,
> > -				    struct perf_event_pmu_context,
> > -				    pmu_ctx_entry);
> > -	next_epc = list_first_entry(&next_ctx->pmu_ctx_list,
> > -				    struct perf_event_pmu_context,
> > -				    pmu_ctx_entry);
> > -
> > -	while (&prev_epc->pmu_ctx_entry != &prev_ctx->pmu_ctx_list &&
> > -	       &next_epc->pmu_ctx_entry != &next_ctx->pmu_ctx_list) {
> > -
> > +	list_for_each_entry_double(prev_epc, next_epc,
> > +				   &prev_ctx->pmu_ctx_list, &next_ctx->pmu_ctx_list,
> > +				   pmu_ctx_entry) {
> 
> There are more places which can use list_for_each_entry_double().
> I'll fix those.

I've gone and renamed it: double_list_for_each_entry(), but yeah, didn't
look too hard for other users.

> > @@ -4859,7 +4879,14 @@ static void put_pmu_ctx(struct perf_even
> >  	if (epc->ctx) {
> >  		struct perf_event_context *ctx = epc->ctx;
> >  
> > -		// XXX ctx->mutex
> > +		/*
> > +		 * XXX
> > +		 *
> > +		 * lockdep_assert_held(&ctx->mutex);
> > +		 *
> > +		 * can't because of the call-site in _free_event()/put_event()
> > +		 * which isn't always called under ctx->mutex.
> > +		 */
> 
> Yes. I came across the same and could not figure out how to solve
> this. So Just kept XXX as is.

Yeah, I can sorta fix it, but it's ugly so there we are.

> >  
> >  		WARN_ON_ONCE(list_empty(&epc->pmu_ctx_entry));
> >  		raw_spin_lock_irqsave(&ctx->lock, flags);

> > @@ -12657,6 +12675,13 @@ perf_event_create_kernel_counter(struct
> >  		goto err_unlock;
> >  	}
> >  
> > +	pmu_ctx = find_get_pmu_context(pmu, ctx, event);
> > +	if (IS_ERR(pmu_ctx)) {
> > +		err = PTR_ERR(pmu_ctx);
> > +		goto err_unlock;
> > +	}
> > +	event->pmu_ctx = pmu_ctx;
> 
> We should call find_get_pmu_context() with ctx->mutex held and thus
> above perf_event_create_kernel_counter() change. Is my understanding
> correct?

That's the intent yeah. But due to not always holding ctx->mutex over
put_pmu_ctx() this might be moot. I'm almost through auditing epc usage
and I think ctx->lock is sufficient, fingers crossed.

> > +
> >  	if (!task) {
> >  		/*
> >  		 * Check if the @cpu we're creating an event for is online.

> > @@ -12998,7 +13022,7 @@ void perf_event_free_task(struct task_st
> >  	struct perf_event_context *ctx;
> >  	struct perf_event *event, *tmp;
> >  
> > -	ctx = rcu_dereference(task->perf_event_ctxp);
> > +	ctx = rcu_access_pointer(task->perf_event_ctxp);
> 
> We dereference ctx pointer but with mutex and lock held. And thus
> rcu_access_pointer() is sufficient. Is my understanding correct?

We do not in fact hold ctx->lock here IIRC; but this is a NULL test, if
it is !NULL we know we have a reference on it and are good.

  reply	other threads:[~2022-10-12 12:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20221008062424.313-1-ravi.bangoria@amd.com>
2022-10-11 11:29 ` [PATCH v2] perf: Rewrite core context handling Peter Zijlstra
2022-10-11 13:19   ` Ravi Bangoria
2022-10-11 14:02     ` Peter Zijlstra
2022-10-11 17:47       ` Peter Zijlstra
2022-10-12  8:39         ` Ravi Bangoria
2022-10-12 12:16           ` Peter Zijlstra [this message]
2022-10-12 20:47             ` Peter Zijlstra
2022-10-13 10:07               ` Ravi Bangoria
2022-10-13 10:59                 ` Peter Zijlstra
2022-10-14  9:56                   ` Ravi Bangoria
2022-10-14 14:50                     ` Peter Zijlstra
2022-10-17  9:33                     ` Ravi Bangoria

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=Y0awHa8oS5yal5M9@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=ananth.narayan@amd.com \
    --cc=catalin.marinas@arm.com \
    --cc=eranian@google.com \
    --cc=frederic@kernel.org \
    --cc=irogers@google.com \
    --cc=jolsa@redhat.com \
    --cc=kim.phillips@amd.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=ravi.bangoria@amd.com \
    --cc=robh@kernel.org \
    --cc=sandipan.das@amd.com \
    --cc=santosh.shukla@amd.com \
    --cc=songliubraving@fb.com \
    --cc=srw@sladewatkins.net \
    --cc=will@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox