All of lore.kernel.org
 help / color / mirror / Atom feed
From: Song Liu <songliubraving@fb.com>
To: open list <linux-kernel@vger.kernel.org>,
	Peter Zijlstra <peterz@infradead.org>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ingo Molnar <mingo@redhat.com>, Kernel Team <Kernel-team@fb.com>,
	Stephane Eranian <eranian@google.com>,
	Lucian Grijincu <lucian@fb.com>
Subject: Re: [PATCH] perf/core: fix userpage->time_enabled of inactive events
Date: Thu, 23 Sep 2021 06:42:35 +0000	[thread overview]
Message-ID: <7530C81E-594D-49AD-9E14-D3B2A338A704@fb.com> (raw)
In-Reply-To: <20210922011715.4154119-1-songliubraving@fb.com>

Hi Peter, 

> On Sep 21, 2021, at 6:17 PM, Song Liu <songliubraving@fb.com> wrote:
> 
> Users of rdpmc rely on the mmapped user page to calculate accurate
> time_enabled. Currently, userpage->time_enabled is only updated when the
> event is added to the pmu. As a result, inactive event (due to counter
> multiplexing) does not have accurate userpage->time_enabled. This can
> be reproduced with something like:
> 
>   /* open 20 task perf_event "cycles", to create multiplexing */
> 
>   fd = perf_event_open();  /* open task perf_event "cycles" */
>   userpage = mmap(fd);     /* use mmap and rdmpc */
> 
>   while (true) {
>     time_enabled_mmap = xxx; /* use logic in perf_event_mmap_page */
>     time_enabled_read = read(fd).time_enabled;
>     if (time_enabled_mmap > time_enabled_read)
>         BUG();
>   }
> 
> Fix this by updating userpage for inactive events in ctx_sched_in.
> 
> Reported-and-tested-by: Lucian Grijincu <lucian@fb.com>
> Signed-off-by: Song Liu <songliubraving@fb.com>

Could you please share your thoughts on this? It works well in our 
tests, but we would like to know your opinion before shipping it to
production. 

Thanks,
Song


> ---
> include/linux/perf_event.h |  4 +++-
> kernel/events/core.c       | 26 ++++++++++++++++++++++++++
> 2 files changed, 29 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index 2d510ad750edc..4aa52f7a48c16 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -683,7 +683,9 @@ struct perf_event {
> 	/*
> 	 * timestamp shadows the actual context timing but it can
> 	 * be safely used in NMI interrupt context. It reflects the
> -	 * context time as it was when the event was last scheduled in.
> +	 * context time as it was when the event was last scheduled in,
> +	 * or when ctx_sched_in failed to schedule the event because we
> +	 * run out of PMC.
> 	 *
> 	 * ctx_time already accounts for ctx->timestamp. Therefore to
> 	 * compute ctx_time for a sample, simply add perf_clock().
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 1cb1f9b8392e2..549ce22df7bc7 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -3766,6 +3766,15 @@ ctx_flexible_sched_in(struct perf_event_context *ctx,
> 			   merge_sched_in, &can_add_hw);
> }
> 
> +static inline void
> +perf_event_update_inactive_userpage(struct perf_event *event,
> +				    struct perf_event_context *ctx)
> +{
> +	perf_event_update_time(event);
> +	perf_set_shadow_time(event, ctx);
> +	perf_event_update_userpage(event);
> +}
> +
> static void
> ctx_sched_in(struct perf_event_context *ctx,
> 	     struct perf_cpu_context *cpuctx,
> @@ -3807,6 +3816,23 @@ ctx_sched_in(struct perf_event_context *ctx,
> 	/* Then walk through the lower prio flexible groups */
> 	if (is_active & EVENT_FLEXIBLE)
> 		ctx_flexible_sched_in(ctx, cpuctx);
> +
> +	/*
> +	 * Update userpage for inactive events. This is needed for accurate
> +	 * time_enabled.
> +	 */
> +	if (unlikely(ctx->rotate_necessary)) {
> +		struct perf_event *event;
> +
> +		perf_event_groups_for_each(event, &ctx->pinned_groups) {
> +			if (event->state == PERF_EVENT_STATE_INACTIVE)
> +				perf_event_update_inactive_userpage(event, ctx);
> +		}
> +		perf_event_groups_for_each(event, &ctx->flexible_groups) {
> +			if (event->state == PERF_EVENT_STATE_INACTIVE)
> +				perf_event_update_inactive_userpage(event, ctx);
> +		}
> +	}
> }
> 
> static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
> -- 
> 2.30.2
> 


  reply	other threads:[~2021-09-23  6:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-22  1:17 [PATCH] perf/core: fix userpage->time_enabled of inactive events Song Liu
2021-09-23  6:42 ` Song Liu [this message]
2021-09-23 10:08 ` Peter Zijlstra
2021-09-23 10:13   ` Peter Zijlstra
2021-09-23 17:47   ` Song Liu
2021-09-24 13:21     ` Peter Zijlstra

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=7530C81E-594D-49AD-9E14-D3B2A338A704@fb.com \
    --to=songliubraving@fb.com \
    --cc=Kernel-team@fb.com \
    --cc=acme@kernel.org \
    --cc=eranian@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lucian@fb.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.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.