linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>,
	Ingo Molnar <mingo@redhat.com>,
	linux-kernel@vger.kernel.org, kan.liang@linux.intel.com
Subject: Re: [PATCH v1 1/7] perf: Allow normal events to be sources of AUX data
Date: Mon, 29 Jul 2019 13:31:13 +0200	[thread overview]
Message-ID: <20190729113113.GB31381@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <20190704160024.56600-2-alexander.shishkin@linux.intel.com>

On Thu, Jul 04, 2019 at 07:00:18PM +0300, Alexander Shishkin wrote:
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 8cfb721bb284..fc586da37067 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -1887,6 +1887,57 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
>  	ctx->generation++;
>  }
>  
> +static int
> +perf_aux_source_match(struct perf_event *event, struct perf_event *aux_event)
> +{
> +	if (!has_aux(aux_event))
> +		return 0;
> +
> +	if (!event->pmu->aux_source_match)
> +		return 0;
> +
> +	return event->pmu->aux_source_match(aux_event);
> +}
> +
> +static void put_event(struct perf_event *event);
> +static void event_sched_out(struct perf_event *event,
> +			    struct perf_cpu_context *cpuctx,
> +			    struct perf_event_context *ctx);
> +
> +static void perf_put_aux_event(struct perf_event *event)
> +{
> +	struct perf_event_context *ctx = event->ctx;
> +	struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
> +	struct perf_event *iter = NULL;
> +
> +	/*
> +	 * If event uses aux_event tear down the link
> +	 */
> +	if (event->aux_event) {
> +		put_event(event->aux_event);
> +		event->aux_event = NULL;
> +		return;
> +	}
> +
> +	/*
> +	 * If the event is an aux_event, tear down all links to
> +	 * it from other events.
> +	 */
> +	for_each_sibling_event(iter, event->group_leader) {
> +		if (iter->aux_event != event)
> +			continue;
> +
> +		iter->aux_event = NULL;
> +		put_event(event);
> +
> +		/*
> +		 * If it's ACTIVE, schedule it out. It won't schedule
> +		 * again because !aux_event.
> +		 */
> +		event_sched_out(iter, cpuctx, ctx);
> +	}
> +}

I'm thinking we can use a perf_get_aux_event() to use below.

> +
>  static void perf_group_detach(struct perf_event *event)
>  {
>  	struct perf_event *sibling, *tmp;
> @@ -1902,6 +1953,8 @@ static void perf_group_detach(struct perf_event *event)
>  
>  	event->attach_state &= ~PERF_ATTACH_GROUP;
>  
> +	perf_put_aux_event(event);
> +
>  	/*
>  	 * If this is a sibling, remove it from its group.
>  	 */
> @@ -10396,6 +10449,12 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
>  		goto err_ns;
>  	}
>  
> +	if (event->attr.aux_source &&
> +	    !(pmu->capabilities & PERF_PMU_CAP_AUX_SOURCE)) {
> +		err = -EOPNOTSUPP;
> +		goto err_pmu;
> +	}
> +
>  	err = exclusive_event_init(event);
>  	if (err)
>  		goto err_pmu;
> @@ -11052,6 +11111,39 @@ SYSCALL_DEFINE5(perf_event_open,
>  		}
>  	}
>  
> +	if (event->attr.aux_source) {
> +		struct perf_event *aux_event = group_leader;
> +
> +		/*
> +		 * One of the events in the group must be an aux event
> +		 * if we want to be an aux_source. This way, the aux event
> +		 * will precede its aux_source events in the group, and
> +		 * therefore will always schedule first.

Can't we mandate that the group leader is the AUX event?

> +		 */
> +		err = -EINVAL;
> +		if (!aux_event)
> +			goto err_locked;
> +
> +		if (perf_aux_source_match(event, aux_event))
> +			goto found_aux;
> +
> +		for_each_sibling_event(aux_event, group_leader) {
> +			if (perf_aux_source_match(event, aux_event))
> +				goto found_aux;
> +		}
> +
> +		goto err_locked;
> +
> +found_aux:
> +		/*
> +		 * Link aux_sources to their aux event; this is undone in
> +		 * perf_group_detach(). When the group in torn down, the
> +		 * aux_source events loose their link to the aux_event and
> +		 * can't schedule any more.
> +		 */
> +		if (atomic_long_inc_not_zero(&aux_event->refcount))
> +			event->aux_event = aux_event;

I'm thinking failing that inc_not_zero() is BAD (tm) ?!

> +	}

With the sugggested perf_get_aux_event() this would become something
like:

	if (event->attr.aux_source && !perf_get_aux_event(event, group_leader))
		goto err_locked;

or something.

>  
>  	/*
>  	 * Must be under the same ctx::mutex as perf_install_in_context(),
> -- 
> 2.20.1
> 

  reply	other threads:[~2019-07-29 11:31 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-04 16:00 [PATCH v1 0/7] perf, intel: Add support for PEBS output to Intel PT Alexander Shishkin
2019-07-04 16:00 ` [PATCH v1 1/7] perf: Allow normal events to be sources of AUX data Alexander Shishkin
2019-07-29 11:31   ` Peter Zijlstra [this message]
2019-07-04 16:00 ` [PATCH v1 2/7] perf/x86/intel: Support PEBS output to PT Alexander Shishkin
2019-07-29 13:37   ` Peter Zijlstra
2019-07-31 14:02     ` Alexander Shishkin
2019-07-04 16:00 ` [PATCH v1 3/7] perf tools: Add aux_source attribute flag Alexander Shishkin
2019-07-04 16:00 ` [PATCH v1 4/7] perf tools: Add itrace option 'o' to synthesize aux-source events Alexander Shishkin
2019-07-04 16:00 ` [PATCH v1 5/7] perf intel-pt: Process options for PEBS event synthesis Alexander Shishkin
2019-07-04 16:00 ` [PATCH v1 6/7] perf tools: Add aux-source config term Alexander Shishkin
2019-07-04 16:00 ` [PATCH v1 7/7] perf intel-pt: Add brief documentation for PEBS via Intel PT Alexander Shishkin
2019-07-29  8:10 ` [PATCH v1 0/7] perf, intel: Add support for PEBS output to " 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=20190729113113.GB31381@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@redhat.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).