2023-03-13 17:19:27

by Song Liu

[permalink] [raw]
Subject: [PATCH v2] perf: fix perf_event_context->time

Time readers rely on perf_event_context->[time|timestamp|timeoffset] to get
accurate time_enabled and time_running for an event. The difference between
ctx->timestamp and ctx->time is the among of time when the context is not
enabled. __update_context_time(ctx, false) is used to increase timestamp,
but not time. Therefore, it should only be called in ctx_sched_in() when
EVENT_TIME was not enabled.

Fixes: 09f5e7dc7ad7 ("perf: Fix perf_event_read_local() time")
Cc: Peter Zijlstra (Intel) <[email protected]>
Cc: Namhyung Kim <[email protected]>
Acked-by: Namhyung Kim <[email protected]>
Signed-off-by: Song Liu <[email protected]>
---
kernel/events/core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 380476a934e8..67478f43e26e 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3872,7 +3872,7 @@ ctx_sched_in(struct perf_event_context *ctx, enum event_type_t event_type)
if (likely(!ctx->nr_events))
return;

- if (is_active ^ EVENT_TIME) {
+ if (!(is_active & EVENT_TIME)) {
/* start ctx time */
__update_context_time(ctx, false);
perf_cgroup_set_timestamp(cpuctx);
--
2.34.1



2023-03-14 19:58:47

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH v2] perf: fix perf_event_context->time

On Mon, Mar 13, 2023 at 10:16:08AM -0700, Song Liu wrote:
> Time readers rely on perf_event_context->[time|timestamp|timeoffset] to get
> accurate time_enabled and time_running for an event. The difference between
> ctx->timestamp and ctx->time is the among of time when the context is not
> enabled. __update_context_time(ctx, false) is used to increase timestamp,
> but not time. Therefore, it should only be called in ctx_sched_in() when
> EVENT_TIME was not enabled.
>
> Fixes: 09f5e7dc7ad7 ("perf: Fix perf_event_read_local() time")
> Cc: Peter Zijlstra (Intel) <[email protected]>
> Cc: Namhyung Kim <[email protected]>
> Acked-by: Namhyung Kim <[email protected]>
> Signed-off-by: Song Liu <[email protected]>

Looks like I'm wearing another brown paper bag tonight..

Thanks!

Subject: [tip: perf/urgent] perf: fix perf_event_context->time

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID: baf1b12a67f5b24f395baca03e442ce27cab0c18
Gitweb: https://git.kernel.org/tip/baf1b12a67f5b24f395baca03e442ce27cab0c18
Author: Song Liu <[email protected]>
AuthorDate: Mon, 13 Mar 2023 10:16:08 -07:00
Committer: Peter Zijlstra <[email protected]>
CommitterDate: Wed, 15 Mar 2023 21:49:46 +01:00

perf: fix perf_event_context->time

Time readers rely on perf_event_context->[time|timestamp|timeoffset] to get
accurate time_enabled and time_running for an event. The difference between
ctx->timestamp and ctx->time is the among of time when the context is not
enabled. __update_context_time(ctx, false) is used to increase timestamp,
but not time. Therefore, it should only be called in ctx_sched_in() when
EVENT_TIME was not enabled.

Fixes: 09f5e7dc7ad7 ("perf: Fix perf_event_read_local() time")
Signed-off-by: Song Liu <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Acked-by: Namhyung Kim <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
---
kernel/events/core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 296617e..52b4aa0 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3872,7 +3872,7 @@ ctx_sched_in(struct perf_event_context *ctx, enum event_type_t event_type)
if (likely(!ctx->nr_events))
return;

- if (is_active ^ EVENT_TIME) {
+ if (!(is_active & EVENT_TIME)) {
/* start ctx time */
__update_context_time(ctx, false);
perf_cgroup_set_timestamp(cpuctx);