Skip to content

Commit 96f6d44

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf_counter: avoid recursion
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>
1 parent f4a2deb commit 96f6d44

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎include/linux/perf_counter.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ struct perf_cpu_context {
328328
int active_oncpu;
329329
int max_pertask;
330330
int exclusive;
331+
332+
/*
333+
* Recursion avoidance:
334+
*
335+
* task, softirq, irq, nmi context
336+
*/
337+
int recursion[4];
331338
};
332339

333340
/*

‎kernel/perf_counter.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/mm.h>
2424
#include <linux/vmstat.h>
2525
#include <linux/rculist.h>
26+
#include <linux/hardirq.h>
2627

2728
#include <asm/irq_regs.h>
2829

@@ -1532,17 +1533,42 @@ static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
15321533
rcu_read_unlock();
15331534
}
15341535

1536+
static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
1537+
{
1538+
if (in_nmi())
1539+
return &cpuctx->recursion[3];
1540+
1541+
if (in_irq())
1542+
return &cpuctx->recursion[2];
1543+
1544+
if (in_softirq())
1545+
return &cpuctx->recursion[1];
1546+
1547+
return &cpuctx->recursion[0];
1548+
}
1549+
15351550
static void __perf_swcounter_event(enum perf_event_types type, u32 event,
15361551
u64 nr, int nmi, struct pt_regs *regs)
15371552
{
15381553
struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
1554+
int *recursion = perf_swcounter_recursion_context(cpuctx);
1555+
1556+
if (*recursion)
1557+
goto out;
1558+
1559+
(*recursion)++;
1560+
barrier();
15391561

15401562
perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
15411563
if (cpuctx->task_ctx) {
15421564
perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
15431565
nr, nmi, regs);
15441566
}
15451567

1568+
barrier();
1569+
(*recursion)--;
1570+
1571+
out:
15461572
put_cpu_var(perf_cpu_context);
15471573
}
15481574

0 commit comments

Comments
 (0)