perf_counter: Fix swcounter context invariance

perf_swcounter_is_counting() uses a lock, which means we cannot
use swcounters from NMI or when holding that particular lock,
this is unintended.

The below removes the lock, this opens up race window, but not
worse than the swcounters already experience due to RCU
traversal of the context in perf_swcounter_ctx_event().

This also fixes the hard lockups while opening a lockdep
tracepoint counter.

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Acked-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: stephane eranian <eranian@googlemail.com>
Cc: Corey J Ashford <cjashfor@us.ibm.com>
LKML-Reference: <1250149915.10001.66.camel@twins>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

authored by Peter Zijlstra and committed by Ingo Molnar bcfc2602 8fd101f2

+18 -26
+18 -26
kernel/perf_counter.c
··· 3444 3445 static int perf_swcounter_is_counting(struct perf_counter *counter) 3446 { 3447 - struct perf_counter_context *ctx; 3448 - unsigned long flags; 3449 - int count; 3450 - 3451 if (counter->state == PERF_COUNTER_STATE_ACTIVE) 3452 return 1; 3453 3454 if (counter->state != PERF_COUNTER_STATE_INACTIVE) 3455 return 0; 3456 3457 /* 3458 - * If the counter is inactive, it could be just because 3459 - * its task is scheduled out, or because it's in a group 3460 - * which could not go on the PMU. We want to count in 3461 - * the first case but not the second. If the context is 3462 - * currently active then an inactive software counter must 3463 - * be the second case. If it's not currently active then 3464 - * we need to know whether the counter was active when the 3465 - * context was last active, which we can determine by 3466 - * comparing counter->tstamp_stopped with ctx->time. 3467 - * 3468 - * We are within an RCU read-side critical section, 3469 - * which protects the existence of *ctx. 3470 */ 3471 - ctx = counter->ctx; 3472 - spin_lock_irqsave(&ctx->lock, flags); 3473 - count = 1; 3474 - /* Re-check state now we have the lock */ 3475 - if (counter->state < PERF_COUNTER_STATE_INACTIVE || 3476 - counter->ctx->is_active || 3477 - counter->tstamp_stopped < ctx->time) 3478 - count = 0; 3479 - spin_unlock_irqrestore(&ctx->lock, flags); 3480 - return count; 3481 } 3482 3483 static int perf_swcounter_match(struct perf_counter *counter,
··· 3444 3445 static int perf_swcounter_is_counting(struct perf_counter *counter) 3446 { 3447 + /* 3448 + * The counter is active, we're good! 3449 + */ 3450 if (counter->state == PERF_COUNTER_STATE_ACTIVE) 3451 return 1; 3452 3453 + /* 3454 + * The counter is off/error, not counting. 3455 + */ 3456 if (counter->state != PERF_COUNTER_STATE_INACTIVE) 3457 return 0; 3458 3459 /* 3460 + * The counter is inactive, if the context is active 3461 + * we're part of a group that didn't make it on the 'pmu', 3462 + * not counting. 3463 */ 3464 + if (counter->ctx->is_active) 3465 + return 0; 3466 + 3467 + /* 3468 + * We're inactive and the context is too, this means the 3469 + * task is scheduled out, we're counting events that happen 3470 + * to us, like migration events. 3471 + */ 3472 + return 1; 3473 } 3474 3475 static int perf_swcounter_match(struct perf_counter *counter,