All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <a.p.zijlstra@chello.nl>
To: Ingo Molnar <mingo@elte.hu>
Cc: Paul Mackerras <paulus@samba.org>,
	Corey Ashford <cjashfor@linux.vnet.ibm.com>,
	linux-kernel@vger.kernel.org,
	Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [PATCH 1/9] perf_counter: fix NMI race in task clock
Date: Wed, 08 Apr 2009 15:01:25 +0200	[thread overview]
Message-ID: <20090408130408.681326666@chello.nl> (raw)
In-Reply-To: 20090408130124.715559390@chello.nl

[-- Attachment #1: perf_counter-task-clock-read.patch --]
[-- Type: text/plain, Size: 1923 bytes --]

We should not be updating ctx->time from NMI context, work around that.

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
---
 kernel/perf_counter.c |   25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

Index: linux-2.6/kernel/perf_counter.c
===================================================================
--- linux-2.6.orig/kernel/perf_counter.c
+++ linux-2.6/kernel/perf_counter.c
@@ -319,8 +319,6 @@ static void __perf_counter_disable(void 
 
 	spin_lock_irqsave(&ctx->lock, flags);
 
-	update_context_time(ctx);
-
 	/*
 	 * If the counter is on, turn it off.
 	 * If it is in error state, leave it in error state.
@@ -2335,13 +2333,11 @@ static const struct hw_perf_counter_ops 
  * Software counter: task time clock
  */
 
-static void task_clock_perf_counter_update(struct perf_counter *counter)
+static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
 {
-	u64 prev, now;
+	u64 prev;
 	s64 delta;
 
-	now = counter->ctx->time;
-
 	prev = atomic64_xchg(&counter->hw.prev_count, now);
 	delta = now - prev;
 	atomic64_add(delta, &counter->count);
@@ -2369,13 +2365,24 @@ static int task_clock_perf_counter_enabl
 static void task_clock_perf_counter_disable(struct perf_counter *counter)
 {
 	hrtimer_cancel(&counter->hw.hrtimer);
-	task_clock_perf_counter_update(counter);
+	task_clock_perf_counter_update(counter, counter->ctx->time);
+
 }
 
 static void task_clock_perf_counter_read(struct perf_counter *counter)
 {
-	update_context_time(counter->ctx);
-	task_clock_perf_counter_update(counter);
+	u64 time;
+
+	if (!in_nmi()) {
+		update_context_time(counter->ctx);
+		time = counter->ctx->time;
+	} else {
+		u64 now = perf_clock();
+		u64 delta = now - counter->ctx->timestamp;
+		time = counter->ctx->time + delta;
+	}
+
+	task_clock_perf_counter_update(counter, time);
 }
 
 static const struct hw_perf_counter_ops perf_ops_task_clock = {

-- 


  reply	other threads:[~2009-04-08 13:06 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-08 13:01 [PATCH 0/9] yet another batch of perf_counter patches Peter Zijlstra
2009-04-08 13:01 ` Peter Zijlstra [this message]
2009-04-08 16:57   ` [tip:perfcounters/core] perf_counter: fix NMI race in task clock Peter Zijlstra
2009-04-08 13:01 ` [PATCH 2/9] perf_counter: provide misc bits in the event header Peter Zijlstra
2009-04-08 16:57   ` [tip:perfcounters/core] " Peter Zijlstra
2009-04-08 13:01 ` [PATCH 3/9] perf_counter: use misc field to widen type Peter Zijlstra
2009-04-08 16:57   ` [tip:perfcounters/core] " Peter Zijlstra
2009-04-08 13:01 ` [PATCH 4/9] perf_counter: kerneltop: keep up with ABI changes Peter Zijlstra
2009-04-08 16:58   ` [tip:perfcounters/core] " Peter Zijlstra
2009-04-08 13:01 ` [PATCH 5/9] perf_counter: add some comments Peter Zijlstra
2009-04-08 16:58   ` [tip:perfcounters/core] " Peter Zijlstra
2009-04-08 13:01 ` [PATCH 6/9] perf_counter: track task-comm data Peter Zijlstra
2009-04-08 16:58   ` [tip:perfcounters/core] " Peter Zijlstra
2009-04-08 17:03   ` [PATCH 6.5/9] perf_counter: fix " Peter Zijlstra
2009-04-08 17:09   ` [tip:perfcounters/core] perf_counter: " Peter Zijlstra
2009-04-08 13:01 ` [PATCH 7/9] perf_counter: some simple userspace profiling Peter Zijlstra
2009-04-08 16:58   ` [tip:perfcounters/core] " Peter Zijlstra
2009-04-08 17:09   ` Peter Zijlstra
2009-04-08 13:01 ` [PATCH 8/9] perf_counter: move PERF_RECORD_TIME Peter Zijlstra
2009-04-08 16:58   ` [tip:perfcounters/core] " Peter Zijlstra
2009-04-08 17:09   ` Peter Zijlstra
2009-04-08 13:01 ` [PATCH 9/9] perf_counter: allow for data addresses to be recorded Peter Zijlstra
2009-04-08 16:59   ` [tip:perfcounters/core] " Peter Zijlstra
2009-04-08 17:10   ` Peter Zijlstra

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=20090408130408.681326666@chello.nl \
    --to=a.p.zijlstra@chello.nl \
    --cc=cjashfor@linux.vnet.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=paulus@samba.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.