public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <a.p.zijlstra@chello.nl>
To: linux-tip-commits@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, paulus@samba.org, hpa@zytor.com,
	mingo@redhat.com, a.p.zijlstra@chello.nl, tglx@linutronix.de,
	mingo@elte.hu
Subject: [tip:perfcounters/core] perf_counter: avoid recursion
Date: Mon, 23 Mar 2009 20:56:31 GMT	[thread overview]
Message-ID: <tip-4d0ec5f64b60057b395f2d314189ccde71d2d0bd@git.kernel.org> (raw)
In-Reply-To: <20090323172417.152096433@chello.nl>

Commit-ID:  4d0ec5f64b60057b395f2d314189ccde71d2d0bd
Gitweb:     http://git.kernel.org/tip/4d0ec5f64b60057b395f2d314189ccde71d2d0bd
Author:     Peter Zijlstra <a.p.zijlstra@chello.nl>
AuthorDate: Mon, 23 Mar 2009 18:22:07 +0100
Committer:  Ingo Molnar <mingo@elte.hu>
CommitDate: Mon, 23 Mar 2009 21:45:09 +0100

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>
LKML-Reference: <20090323172417.152096433@chello.nl>
Signed-off-by: Ingo Molnar <mingo@elte.hu>


---
 include/linux/perf_counter.h |    7 +++++++
 kernel/perf_counter.c        |   26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/include/linux/perf_counter.h b/include/linux/perf_counter.h
index 56099e5..18dc17d 100644
--- a/include/linux/perf_counter.h
+++ b/include/linux/perf_counter.h
@@ -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];
 };
 
 /*
diff --git a/kernel/perf_counter.c b/kernel/perf_counter.c
index ca14fc4..ce34bff 100644
--- a/kernel/perf_counter.c
+++ b/kernel/perf_counter.c
@@ -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>
 
@@ -1532,10 +1533,31 @@ 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) {
@@ -1543,6 +1565,10 @@ static void __perf_swcounter_event(enum perf_event_types type, u32 event,
 				nr, nmi, regs);
 	}
 
+	barrier();
+	(*recursion)--;
+
+out:
 	put_cpu_var(perf_cpu_context);
 }
 

  reply	other threads:[~2009-03-23 20:57 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-23 17:22 [PATCH 0/7] perf_counter: syscall ABI cleanup and mmap() interface Peter Zijlstra
2009-03-23 17:22 ` [PATCH 1/7] perf_counter: remove the event config bitfields Peter Zijlstra
2009-03-23 20:56   ` [tip:perfcounters/core] " Peter Zijlstra
2009-03-23 17:22 ` [PATCH 2/7] perf_counter: avoid recursion Peter Zijlstra
2009-03-23 20:56   ` Peter Zijlstra [this message]
2009-03-23 17:22 ` [PATCH 3/7] perf_counter: add an mmap method to allow userspace to read hardware counters Peter Zijlstra
2009-03-23 20:56   ` [tip:perfcounters/core] " Paul Mackerras
2009-03-23 17:22 ` [PATCH 4/7] mutex: add atomic_dec_and_mutex_lock] Peter Zijlstra
2009-03-23 20:56   ` [tip:perfcounters/core] mutex: add atomic_dec_and_mutex_lock() Eric Paris
2009-04-02  0:42     ` [tip:perfcounters/core] mutex: drop "inline" from mutex_lock() inside kernel/mutex.c H. Peter Anvin
2009-03-23 17:22 ` [PATCH 5/7] perf_counter: new output ABI - part 1 Peter Zijlstra
2009-03-23 20:56   ` [tip:perfcounters/core] " Peter Zijlstra
2009-03-23 17:22 ` [PATCH 6/7] kerneltop: update to new syscall ABI Peter Zijlstra
2009-03-23 20:57   ` [tip:perfcounters/core] " Peter Zijlstra
2009-03-23 17:22 ` [PATCH 7/7] kerneltop: use mmap() output Peter Zijlstra
2009-03-23 20:57   ` [tip:perfcounters/core] " Peter Zijlstra
2009-03-23 20:57   ` [tip:perfcounters/core] perf_counter tools: tidy up in-kernel dependencies Ingo Molnar
2009-05-20  9:26     ` Jaswinder Singh Rajput

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-4d0ec5f64b60057b395f2d314189ccde71d2d0bd@git.kernel.org \
    --to=a.p.zijlstra@chello.nl \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=mingo@redhat.com \
    --cc=paulus@samba.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox