Re: perf_events: question about __perf_event_read()

From: Peter Zijlstra
Date: Wed Jan 26 2011 - 09:40:03 EST


On Fri, 2011-01-21 at 19:25 +0100, Stephane Eranian wrote:

> I remember checking this about a month ago and tip-x86 had
> some checks in this routine, but now the're gone.

I did a quick look through history and I couldn't find anything like
that, a well...

> I think you need something like:
>
> + if (ctx->is_active)
> + update_context_time(ctx);
>
> + if (event->state == PERF_EVENT_STATE_ACTIVE)
> + event->pmu->read(event);

Something like so?

---
Subject: perf: Fix race in perf_event_read()
From: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
Date: Wed Jan 26 15:38:35 CET 2011

It is quite possible for the event to have been disabled between
perf_event_read() sending the IPI and the CPU servicing the IPI and
calling __perf_event_read(), hence revalidate the state.

Reported-by: Stephane Eranian <eranian@xxxxxxxxxx>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
LKML-Reference: <new-submission>
---
kernel/perf_event.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

Index: linux-2.6/kernel/perf_event.c
===================================================================
--- linux-2.6.orig/kernel/perf_event.c
+++ linux-2.6/kernel/perf_event.c
@@ -1901,11 +1901,12 @@ static void __perf_event_read(void *info
return;

raw_spin_lock(&ctx->lock);
- update_context_time(ctx);
+ if (ctx->is_active)
+ update_context_time(ctx);
update_event_times(event);
+ if (event->state == PERF_EVENT_STATE_ACTIVE)
+ event->pmu->read(event);
raw_spin_unlock(&ctx->lock);
-
- event->pmu->read(event);
}

static inline u64 perf_event_count(struct perf_event *event)

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/