All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xie XiuQi <xiexiuqi@huawei.com>
To: <linux-kernel@vger.kernel.org>
Cc: <rostedt@goodmis.org>, <mingo@redhat.com>, <tony.luck@intel.com>,
	<fweisbec@gmail.com>, <m.chehab@samsung.com>
Subject: [PATCH 1/2] tracing: fix uptime overflow problem
Date: Sat, 28 Jun 2014 19:10:00 +0800	[thread overview]
Message-ID: <1403953801-19068-2-git-send-email-xiexiuqi@huawei.com> (raw)
In-Reply-To: <1403953801-19068-1-git-send-email-xiexiuqi@huawei.com>

The "uptime" tracer added in:
    commit 8aacf017b065a805d27467843490c976835eb4a5
    tracing: Add "uptime" trace clock that uses jiffies
has wraparound problems when the system has been up more
than 1 hour 11 minutes and 34 seconds. It converts jiffies
to nanoseconds using:
        (u64)jiffies_to_usecs(jiffy) * 1000ULL
but since jiffies_to_usecs() only returns a 32-bit value, it
truncates at 2^32 microseconds.  An additional problem on 32-bit
systems is that the argument is "unsigned long", so fixing the
return value only helps until 2^32 jiffies (49.7 days on a HZ=1000
system).

Tony provide full featured jiffies_to_nsecs() function, but
can't resolve another problem that jiffies_lock is not safe
in NMI context.

Now we use the lockless function __current_kernel_time() and
getboottime() to calculate the uptime.

The former discussion is here:
http://lkml.org/lkml/2014/4/8/525

Additional, I changed trace_clock_jiffies to trace_clock_uptime,
in order to better describe its function.

Reported-by: Tony Luck <tony.luck@intel.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: stable@vger.kernel.org	# 3.10+
Signed-off-by: Xie XiuQi <xiexiuqi@huawei.com>
---
 include/linux/trace_clock.h |  2 +-
 kernel/trace/trace.c        |  2 +-
 kernel/trace/trace_clock.c  | 15 +++++++++++----
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/include/linux/trace_clock.h b/include/linux/trace_clock.h
index 1d7ca27..2961ac7 100644
--- a/include/linux/trace_clock.h
+++ b/include/linux/trace_clock.h
@@ -16,7 +16,7 @@
 
 extern u64 notrace trace_clock_local(void);
 extern u64 notrace trace_clock(void);
-extern u64 notrace trace_clock_jiffies(void);
+extern u64 notrace trace_clock_uptime(void);
 extern u64 notrace trace_clock_global(void);
 extern u64 notrace trace_clock_counter(void);
 
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 384ede3..867e849 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -809,7 +809,7 @@ static struct {
 	{ trace_clock_local,	"local",	1 },
 	{ trace_clock_global,	"global",	1 },
 	{ trace_clock_counter,	"counter",	0 },
-	{ trace_clock_jiffies,	"uptime",	1 },
+	{ trace_clock_uptime,	"uptime",	1 },
 	{ trace_clock,		"perf",		1 },
 	ARCH_TRACE_CLOCKS
 };
diff --git a/kernel/trace/trace_clock.c b/kernel/trace/trace_clock.c
index 26dc348..59e4d4d 100644
--- a/kernel/trace/trace_clock.c
+++ b/kernel/trace/trace_clock.c
@@ -19,6 +19,7 @@
 #include <linux/percpu.h>
 #include <linux/sched.h>
 #include <linux/ktime.h>
+#include <linux/time.h>
 #include <linux/trace_clock.h>
 
 /*
@@ -58,14 +59,20 @@ u64 notrace trace_clock(void)
 }
 
 /*
- * trace_jiffy_clock(): Simply use jiffies as a clock counter.
+ * trace_clock_uptime(): Use lockless version __current_kernel_time,
+ * so it's safe in NMI context.
  */
-u64 notrace trace_clock_jiffies(void)
+u64 notrace trace_clock_uptime(void)
 {
-	u64 jiffy = jiffies - INITIAL_JIFFIES;
+	struct timespec uptime, now, boottime;
+
+	/* Does not take xtime_lock, so it's safe in NMI context. */
+	now = __current_kernel_time();
+	getboottime(&boottime);
+	uptime = timespec_sub(now, boottime);
 
 	/* Return nsecs */
-	return (u64)jiffies_to_usecs(jiffy) * 1000ULL;
+	return timespec_to_ns(&uptime);
 }
 
 /*
-- 
2.0.0


  reply	other threads:[~2014-06-28 10:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-28 11:09 [PATCH 0/2] tracing: fix uptime overflow problem Xie XiuQi
2014-06-28 11:10 ` Xie XiuQi [this message]
2014-06-30 15:10   ` [PATCH 1/2] " Steven Rostedt
2014-06-30 18:17     ` [PATCH] tracing: Fix wraparound problems in "uptime" tracer Tony Luck
2014-06-30 18:40       ` Steven Rostedt
2014-06-30 20:31         ` [PATCH-v2] " Tony Luck
2014-07-17 23:02           ` Tony Luck
2014-07-18  2:08             ` Steven Rostedt
2014-07-18 17:05               ` Tony Luck
2014-07-18 17:36                 ` Steven Rostedt
2014-07-18 18:43                   ` [PATCH] " Tony Luck
2014-07-18 18:47                     ` Steven Rostedt
2014-07-18 18:54                       ` Tony Luck
2014-06-28 11:10 ` [PATCH 2/2] tracing: update Documentation/trace/ftrace.txt for uptime Xie XiuQi

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=1403953801-19068-2-git-send-email-xiexiuqi@huawei.com \
    --to=xiexiuqi@huawei.com \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.chehab@samsung.com \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=tony.luck@intel.com \
    /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.