Re: perf_events: question about __perf_event_read()

From: Stephane Eranian
Date: Wed Jan 26 2011 - 09:50:48 EST


Yeah, something like that.

I didn't quite understand why the event->pmu->read() was outside of the
lock.

Thanks.

On Wed, Jan 26, 2011 at 3:40 PM, Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> 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/