perf/core: fix userpage->time_enabled of inactive events
authorSong Liu <songliubraving@fb.com>
Wed, 29 Sep 2021 19:43:13 +0000 (12:43 -0700)
committerPeter Zijlstra <peterz@infradead.org>
Fri, 1 Oct 2021 11:57:54 +0000 (13:57 +0200)
commitf792565326825ed806626da50c6f9a928f1079c1
tree09c2e29ea3fb5732a50748230db362bbd44fab5c
parentecc2123e09f9e71ddc6c53d71e283b8ada685fe2
perf/core: fix userpage->time_enabled of inactive events

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 merge_sched_in.

Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reported-and-tested-by: Lucian Grijincu <lucian@fb.com>
Signed-off-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20210929194313.2398474-1-songliubraving@fb.com
include/linux/perf_event.h
kernel/events/core.c