Skip to content

Commit

Permalink
perf_counter: avoid recursion
Browse files Browse the repository at this point in the history
Tracepoint events like lock_acquire and software counters like
pagefaults can recurse into the perf counter code again, avoid that.

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
Orig-LKML-Reference: <20090323172417.152096433@chello.nl>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed Apr 6, 2009
1 parent f4a2deb commit 96f6d44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/linux/perf_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ struct perf_cpu_context {
int active_oncpu;
int max_pertask;
int exclusive;

/*
* Recursion avoidance:
*
* task, softirq, irq, nmi context
*/
int recursion[4];
};

/*
Expand Down
26 changes: 26 additions & 0 deletions kernel/perf_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <linux/mm.h>
#include <linux/vmstat.h>
#include <linux/rculist.h>
#include <linux/hardirq.h>

#include <asm/irq_regs.h>

Expand Down Expand Up @@ -1532,17 +1533,42 @@ static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
rcu_read_unlock();
}

static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
{
if (in_nmi())
return &cpuctx->recursion[3];

if (in_irq())
return &cpuctx->recursion[2];

if (in_softirq())
return &cpuctx->recursion[1];

return &cpuctx->recursion[0];
}

static void __perf_swcounter_event(enum perf_event_types type, u32 event,
u64 nr, int nmi, struct pt_regs *regs)
{
struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
int *recursion = perf_swcounter_recursion_context(cpuctx);

if (*recursion)
goto out;

(*recursion)++;
barrier();

perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
if (cpuctx->task_ctx) {
perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
nr, nmi, regs);
}

barrier();
(*recursion)--;

out:
put_cpu_var(perf_cpu_context);
}

Expand Down

0 comments on commit 96f6d44

Please sign in to comment.