mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jason Wessel <jason.wessel@windriver.com>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org,
	Jason Wessel <jason.wessel@windriver.com>,
	John Stultz <johnstul@us.ibm.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	"H. Peter Anvin" <hpa@zytor.com>
Subject: [PATCH] timekeeping: Fix overflow in rawtime tv_nsec on 32 bit archs
Date: Thu,  5 Aug 2010 07:28:32 -0500	[thread overview]
Message-ID: <1281011312-23499-1-git-send-email-jason.wessel@windriver.com> (raw)

The tv_nsec is a long and when added to the shift value it can wrap
and become negative which later causes looping problems in the
getrawmonotonic().  The edge case occurs when the system has slept for
a short period of time of ~2 seconds.

A trace printk of the values in this patch illustrate the problem:

ftrace time stamp: log
43.716079: logarithmic_accumulation: raw_shift: 3d0913 tv_nsec d687faa
43.718513: logarithmic_accumulation: raw_shift: 3d0913 tv_nsec da588bd
43.722161: logarithmic_accumulation: raw_shift: 3d0913 tv_nsec de291d0
46.349925: logarithmic_accumulation: raw_shift: 7a122600 tv_nsec e1f9ae3
46.349930: logarithmic_accumulation: raw_shift: 1e848980 tv_nsec 8831c0e3

The kernel starts looping at 46.349925 in the getrawmonotonic() due to
the negative value from adding the raw_shift to tv_nsec.

A simple solution is to process the raw_shift separately from the
tv_nsec using the same type of loop in logarithmic_accumulation().

Signed-off-by: Jason Wessel <jason.wessel@windriver.com>

CC: John Stultz <johnstul@us.ibm.com>
CC: Thomas Gleixner <tglx@linutronix.de>
CC: H. Peter Anvin <hpa@zytor.com>
---
 kernel/time/timekeeping.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index caf8d4d..1762282 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -736,6 +736,7 @@ static void timekeeping_adjust(s64 offset)
 static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
 {
 	u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
+	u64 raw_shift;
 
 	/* If the offset is smaller then a shifted interval, do nothing */
 	if (offset < timekeeper.cycle_interval<<shift)
@@ -753,7 +754,12 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
 	}
 
 	/* Accumulate into raw time */
-	raw_time.tv_nsec += timekeeper.raw_interval << shift;;
+	raw_shift = timekeeper.raw_interval << shift;
+	while (raw_shift >= NSEC_PER_SEC) {
+		raw_shift -= NSEC_PER_SEC;
+		raw_time.tv_sec++;
+	}
+	raw_time.tv_nsec += raw_shift;
 	while (raw_time.tv_nsec >= NSEC_PER_SEC) {
 		raw_time.tv_nsec -= NSEC_PER_SEC;
 		raw_time.tv_sec++;
-- 
1.6.3.3


             reply	other threads:[~2010-08-05 12:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-05 12:28 Jason Wessel [this message]
2010-08-05 22:17 ` john stultz
2010-08-06 15:09   ` Jason Wessel

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=1281011312-23499-1-git-send-email-jason.wessel@windriver.com \
    --to=jason.wessel@windriver.com \
    --cc=hpa@zytor.com \
    --cc=johnstul@us.ibm.com \
    --cc=linux-kernel@vger.kernel.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

all inboxes | Powered by JetHome®