All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tony Luck <tony.luck@intel.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: <linux-kernel@vger.kernel.org>, <mingo@redhat.com>,
	<tony.luck@intel.com>, <fweisbec@gmail.com>,
	"<m.chehab@samsung.com> Xie XiuQi" <xiexiuqi@huawei.com>
Subject: [PATCH-v2] tracing: Fix wraparound problems in "uptime" tracer
Date: Mon, 30 Jun 2014 13:31:37 -0700	[thread overview]
Message-ID: <b29c17f74d39d1b9240385e7479f63f44c988b8b.1404160039.git.tony.luck@intel.com> (raw)
In-Reply-To: <20140630144017.2abc48ba@gandalf.local.home>

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).

We can't provide a full features jiffies_to_nsec() function in
any safe way (32-bit systems need locking to read the full 64-bit
jiffies value).  Just do the best we can here and recognise that
32-bit systems may seem some timestamp anomolies if jiffies64
was in the middle of rolling over a 2^32 boundary.

Signed-off-by: Tony Luck <tony.luck@intel.com>
---

v1->v2: Use "do_div()" for 64-bit division

 kernel/timeconst.bc        |  6 ++++++
 kernel/trace/trace_clock.c | 12 ++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/kernel/timeconst.bc b/kernel/timeconst.bc
index 511bdf2cafda..a5fef7a7fb27 100644
--- a/kernel/timeconst.bc
+++ b/kernel/timeconst.bc
@@ -100,6 +100,12 @@ define timeconst(hz) {
 		print "#define USEC_TO_HZ_DEN\t\t", 1000000/cd, "\n"
 		print "\n"
 
+		obase=10
+		cd=gcd(hz,1000000000)
+		print "#define HZ_TO_NSEC_NUM\t\t", 1000000000/cd, "\n"
+		print "#define HZ_TO_NSEC_DEN\t\t", hz/cd, "\n"
+		print "\n"
+
 		print "#endif /* KERNEL_TIMECONST_H */\n"
 	}
 	halt
diff --git a/kernel/trace/trace_clock.c b/kernel/trace/trace_clock.c
index 26dc348332b7..3fcf8f4807d5 100644
--- a/kernel/trace/trace_clock.c
+++ b/kernel/trace/trace_clock.c
@@ -59,13 +59,21 @@ u64 notrace trace_clock(void)
 
 /*
  * trace_jiffy_clock(): Simply use jiffies as a clock counter.
+ * This usage of jiffies_64 isn't safe on 32-bit, but we may be
+ * called from NMI context, and we have no safe way to get a timestamp.
  */
 u64 notrace trace_clock_jiffies(void)
 {
-	u64 jiffy = jiffies - INITIAL_JIFFIES;
+	u64 jiffy = jiffies_64 - INITIAL_JIFFIES;
 
 	/* Return nsecs */
-	return (u64)jiffies_to_usecs(jiffy) * 1000ULL;
+#if !(NSEC_PER_SEC % HZ)
+	return (NSEC_PER_SEC / HZ) * jiffy;
+#else
+	jiffy *= HZ_TO_NSEC_NUM;
+	do_div(jiffy, HZ_TO_NSEC_DEN);
+	return jiffy;
+#endif
 }
 
 /*
-- 
1.8.4.1


  reply	other threads:[~2014-06-30 20:31 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 ` [PATCH 1/2] " Xie XiuQi
2014-06-30 15:10   ` 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         ` Tony Luck [this message]
2014-07-17 23:02           ` [PATCH-v2] " 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=b29c17f74d39d1b9240385e7479f63f44c988b8b.1404160039.git.tony.luck@intel.com \
    --to=tony.luck@intel.com \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=xiexiuqi@huawei.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.