All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Shishkin <alexander.shishkin@linux.intel.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Ingo Molnar <mingo@redhat.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Robert Richter <rric@kernel.org>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Mike Galbraith <efault@gmx.de>, Paul Mackerras <paulus@samba.org>,
	Stephane Eranian <eranian@google.com>,
	Andi Kleen <ak@linux.intel.com>,
	kan.liang@intel.com, adrian.hunter@intel.com,
	markus.t.metzger@intel.com,
	Mathieu Poirier <mathieu.poirier@linaro.org>,
	Kaixu Xia <kaixu.xia@linaro.org>,
	acme@infradead.org
Subject: Re: [PATCH v9 12/14] x86: perf: intel_pt: Intel PT PMU driver
Date: Fri, 30 Jan 2015 11:48:59 +0200	[thread overview]
Message-ID: <CAB7JpEGjwBZatta4A+2mQGGAzhnWQEUqoSGxE7jCxTNvMOm8Rw@mail.gmail.com> (raw)
In-Reply-To: <20150129152022.GC26304@twins.programming.kicks-ass.net>

On 29 January 2015 at 17:20, Peter Zijlstra <peterz@infradead.org> wrote:
> On Thu, Jan 29, 2015 at 05:03:21PM +0200, Alexander Shishkin wrote:
>> > We're already holding ctx->mutex, this should have made lockdep scream.
>>
>> As I mentioned offline, cpuctx->ctx.mutex is set to a lockdep class of
>> its own, so lockdep doesn't see this. It is, of course, still a
>> problem.
>
> Right; I don't think we currently use that annotation of
> cpuctx->ctx.mutex but I had indeed overlooked that.
>
> Per perf_ctx_lock() the nesting order is:
>
>  cpuctx
>    ctx
>
> And here we would have done the inverse. Still I'm not entirely sure it
> would've resulted in a deadlock, we typically don't take multiple
> ctx->mutex locks, and where we do its either between different context
> on the same CPU (eg, moving a software event to the hardware lists), or
> between the same context on different CPUs (perf_pmu_migrate_context),
> or between inherited contexts (put_event, in child->parent nesting).
>
> None of those cases seem to overlap with this order.

Indeed.

> However,
>
>> But as you pointed out, if we grab the exclusive_cnt for per-task
>> cpu!=-1 events as well, we don't need to look into other contexts here
>> at all.
>
> its irrelevant because we can avoid the entire ordeal ;-)

Exactly. :)

>> > --- a/kernel/events/core.c
>> > +++ b/kernel/events/core.c
>> > @@ -3487,6 +3487,9 @@ static void __free_event(struct perf_eve
>> >         if (event->destroy)
>> >                 event->destroy(event);
>> >
>> > +       if (event->pmu && event->ctx)
>> > +               exclusive_event_release(event);
>>
>> It looks like event can be already removed from its context at this
>> point, so event->ctx will be NULL and the counter would leak or am I
>> missing something?
>
> Yeah, event->ctx is _magic_ ;-)
>
> event->ctx is never NULL, although it can change. Much fun because of
> that; see: lkml.kernel.org/r/20150123125159.696530128@infradead.org if
> you like to torture yourself a wee bit.

Yeah, I already did, having noticed mutex_lock_double() in the context
of your diff. And I'll probably re-read it a few more times. :)

>> I used the event->attach_state & PERF_ATTACH_TASK to see if it's a
>> per-task counter, which seems reliable even though it might need
>> documenting.
>
> Yes, I suppose that is indeed better, I had feared we clear the
> ATTACH_TASK state on remove_from_context() like we do all the other
> ATTACH states, but we do not.

Yep. It probably needs documenting though, in case somebody decides to
change this in future.

>> > +static bool exclusive_event_ok(struct perf_event *event,
>> > +                              struct perf_event_context *ctx)
>>
>> Then, maybe exclusive_event_installable() is a better name, because
>> this one only deals with the current context; cpu-wide vs per-task
>> case is taken care of in perf_event_alloc()/__free_event().
>
> OK.
>
>> > +       /*
>> > +        * exclusive_cnt <0: cpu
>> > +        *               >0: tsk
>> > +        */
>> > +       if (ctx->task) {
>> > +               if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
>> > +                       return false;
>> > +       } else {
>> > +               if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
>> > +                       return false;
>> > +       }
>>
>> So I would like to keep this bit in perf_event_alloc() path and the
>> reverse in __free_event(),
>
> Fair enough; exclusive_event_init() ?

Ok.

>
>> > +
>> > +       mutex_lock(&ctx->lock);
>> > +       ret = __exclusive_event_ok(event, ctx);
>> > +       mutex_unlock(&ctx->lock);
>
> And as you pointed out on IRC, this needs to be in the same section as
> install_in_context() otherwise we have a race.
>
>> > +       if (!ret) {
>> > +               if (ctx->task)
>> > +                       atomic_dec(&pmu->exclusive_cnt);
>> > +               else
>> > +                       atomic_inc(&pmu->exclusive_cnt);
>> > +       }
>>
>> in which case we don't need to undo the counter here, because it will
>> still go through __free_event() in the error path.
>
> OK; exclusive_event_destroy() ?

Yep.

Cheers,
--
Alex

  parent reply	other threads:[~2015-01-30  9:49 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-14 12:18 [PATCH v9 00/14] perf: Add infrastructure and support for Intel PT Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 01/14] perf: Add data_{offset,size} to user_page Alexander Shishkin
2015-04-02 18:37   ` [tip:perf/core] " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 02/14] perf: Add AUX area to ring buffer for raw data streams Alexander Shishkin
2015-04-02 18:37   ` [tip:perf/core] " tip-bot for Peter Zijlstra
2015-01-14 12:18 ` [PATCH v9 03/14] perf: Support high-order allocations for AUX space Alexander Shishkin
2015-04-02 18:37   ` [tip:perf/core] " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 04/14] perf: Add a capability for AUX_NO_SG pmus to do software double buffering Alexander Shishkin
2015-04-02 18:38   ` [tip:perf/core] " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 05/14] perf: Add a pmu capability for "exclusive" events Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 06/14] perf: Add AUX record Alexander Shishkin
2015-03-24 11:07   ` Jiri Olsa
2015-03-24 11:27     ` Adrian Hunter
2015-03-24 13:06       ` Jiri Olsa
2015-04-02 18:38   ` [tip:perf/core] " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 07/14] perf: Add api for pmus to write to AUX area Alexander Shishkin
2015-04-02 18:39   ` [tip:perf/core] perf: Add API for PMUs to write to the " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 08/14] perf: Support overwrite mode for " Alexander Shishkin
2015-04-02 18:39   ` [tip:perf/core] perf: Support overwrite mode for the " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 09/14] perf: Add wakeup watermark control to " Alexander Shishkin
2015-04-02 18:39   ` [tip:perf/core] perf: Add wakeup watermark control to the " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 10/14] x86: Add Intel Processor Trace (INTEL_PT) cpu feature detection Alexander Shishkin
2015-04-02 18:40   ` [tip:perf/core] " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 11/14] x86: perf: Intel PT and LBR/BTS are mutually exclusive Alexander Shishkin
2015-04-02 18:40   ` [tip:perf/core] perf/x86: Mark Intel PT and LBR/ BTS as " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 12/14] x86: perf: intel_pt: Intel PT PMU driver Alexander Shishkin
2015-01-15  9:06   ` Peter Zijlstra
2015-01-15 12:31     ` Alexander Shishkin
2015-01-20 13:20       ` Alexander Shishkin
2015-01-26 16:55         ` Peter Zijlstra
2015-01-27 18:03           ` Alexander Shishkin
2015-01-29 11:59             ` Peter Zijlstra
2015-01-29 15:03               ` Alexander Shishkin
2015-01-29 15:20                 ` Peter Zijlstra
2015-01-29 15:28                   ` Peter Zijlstra
2015-01-30  9:48                   ` Alexander Shishkin [this message]
2015-01-30 10:31                   ` [PATCH] perf: Add a pmu capability for "exclusive" events Alexander Shishkin
2015-04-02 18:38                     ` [tip:perf/core] " tip-bot for Alexander Shishkin
2015-01-30 10:39     ` [PATCH] x86: perf: intel_pt: Intel PT PMU driver Alexander Shishkin
2015-04-02 18:40       ` [tip:perf/core] perf/x86/intel/pt: Add " tip-bot for Alexander Shishkin
2015-01-30 10:40     ` [PATCH] x86: perf: intel_bts: Add BTS " Alexander Shishkin
2015-04-02 18:41       ` [tip:perf/core] perf/x86/intel/bts: " tip-bot for Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 13/14] x86: perf: intel_bts: " Alexander Shishkin
2015-01-14 12:18 ` [PATCH v9 14/14] perf: add ITRACE_START record to indicate that tracing has started Alexander Shishkin
2015-04-02 18:39   ` [tip:perf/core] perf: Add " tip-bot for Alexander Shishkin
2015-01-14 12:43 ` [PATCH v9 00/14] perf: Add infrastructure and support for Intel PT Alexander Shishkin
2015-01-14 14:38   ` Peter Zijlstra
2015-01-14 14:49     ` [PATCH v10 14/14] perf: add ITRACE_START record to indicate that tracing has started Alexander Shishkin

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=CAB7JpEGjwBZatta4A+2mQGGAzhnWQEUqoSGxE7jCxTNvMOm8Rw@mail.gmail.com \
    --to=alexander.shishkin@linux.intel.com \
    --cc=acme@infradead.org \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=efault@gmx.de \
    --cc=eranian@google.com \
    --cc=fweisbec@gmail.com \
    --cc=kaixu.xia@linaro.org \
    --cc=kan.liang@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=markus.t.metzger@intel.com \
    --cc=mathieu.poirier@linaro.org \
    --cc=mingo@redhat.com \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    --cc=rric@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 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.