Re: [PATCH v2] perf/core: fix userpage->time_enabled of inactive events

From: Peter Zijlstra
Date: Wed Sep 29 2021 - 05:18:38 EST


On Thu, Sep 23, 2021 at 06:28:00PM -0700, Song Liu wrote:

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 1cb1f9b8392e2..d73f986eef7b3 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -3707,6 +3707,46 @@ static noinline int visit_groups_merge(struct perf_cpu_context *cpuctx,
> return 0;
> }
>
> +static inline bool event_update_userpage(struct perf_event *event)
> +{
> + /*
> + * Checking mmap_count to avoid unnecessary work. This does leave a
> + * corner case: if the event is enabled before mmap(), the first
> + * time the event gets scheduled is via:
> + *
> + * __perf_event_enable (or __perf_install_in_context)
> + * -> ctx_resched
> + * -> perf_event_sched_in
> + * -> ctx_sched_in
> + *
> + * with mmap_count of 0, so we will skip here. As a result,
> + * userpage->offset is not accurate after mmap and before the
> + * first rotation.
> + *
> + * To avoid the discrepancy of this window, the user space should
> + * mmap the event before enabling it.
> + */

It seems to me that writing that comment was more work than actually
fixing perf_mmap() to DTRT, no? AFAICT all we need is something like:

diff --git a/kernel/events/core.c b/kernel/events/core.c
index fd2ae70fa6c4..1e33c2e6b0dc 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6324,6 +6324,8 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)

ring_buffer_attach(event, rb);

+ perf_event_update_time(event);
+ perf_set_shadow_time(event, event->ctx);
perf_event_init_userpage(event);
perf_event_update_userpage(event);
} else {