Skip to contentSkip to conversation

Time fixes for 3.6

PATCH In review John Stultz 3 participants 10 messages Aug 22, 2012 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

cover Time fixes for 3.6

Thomas, Ingo,
Here are four small time fixes for 3.6
that apply against tip/timers/urgent.

One patch resolves the suspend issue Andreas saw.

The next two are casting fixes for potential overflows on 32bits, that Andreas found, but haven't been observed in the wild.

Finally the last one fixes an issue I found in my own testing w/ the _COARSE clocksources.

Please take a look at them and let me know if you have any thoughts or objections.

Cc Ingo Molnar <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andreas Schwab <[email protected]>

Andreas Schwab (1):
time: Fix casting issue in timekeeping_forward_now

John Stultz (3):
time: Ensure we normalize the timekeeper in tk_xtime_add
time: Avoid potential shift-overflow with large shift values
time: Avoid making adjustments if we havne't accumulated anything

 kernel/time/timekeeping.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/4 time: Ensure we normalize the timekeeper in tk_xtime_add

Andreas noticed problems with resume on specific hardware after commit 1e75fa8be9fb61e1af46b5b3b176347a4c958ca1 combined with commit b44d50dcacea0d485ca2ff9140f8cc28ee22f28d

After some digging I realized we aren't normalizing the timekeeper after the add. This patch to correct this resolved the issue.

---
 kernel/time/timekeeping.c |    1 +
 1 file changed, 1 insertion(+)
kernel/time/timekeeping.c +1 -0
index 898bef0..258164a 100644
@@ -115,6 +115,7 @@ static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
 {
 	tk->xtime_sec += ts->tv_sec;
 	tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
+	tk_normalize_xtime(tk);
 }
 
 static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
Signature · 1 line
1.7.9.5
#2 lore raw

[tip:timers/urgent] time: Ensure we normalize the timekeeper in tk_xtime_add

Commit-ID:  784ffcbb96c3a97b4c64fd48b1dfe12ef3fcbcda
Gitweb:     http://git.kernel.org/tip/784ffcbb96c3a97b4c64fd48b1dfe12ef3fcbcda
Author:     John Stultz <[email protected]>
AuthorDate: Tue, 21 Aug 2012 20:30:46 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 22 Aug 2012 10:42:12 +0200

time: Ensure we normalize the timekeeper in tk_xtime_add

Andreas noticed problems with resume on specific hardware after commit 1e75fa8b (time: Condense timekeeper.xtime into xtime_sec) combined with commit b44d50dca (time: Fix casting issue in tk_set_xtime and tk_xtime_add)

After some digging I realized we aren't normalizing the timekeeper after the add. Add the missing normalize call.

---
 kernel/time/timekeeping.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/time/timekeeping.c +1 -0
index 898bef0..258164a 100644
@@ -115,6 +115,7 @@ static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
 {
 	tk->xtime_sec += ts->tv_sec;
 	tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
+	tk_normalize_xtime(tk);
 }
 
 static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
#3 lore raw

PATCH 2/4 time: Fix casting issue in timekeeping_forward_now

In reply to John Stultz

From: Andreas Schwab <[email protected]>

arch_gettimeoffset returns a u32 value which when shifted by tk->shift can overflow. Cast it to u64 first.

This issue was introduced with 1e75fa8be9fb61e1af46b5b3b176347a4c958ca1

Cc Ingo Molnar <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/time/timekeeping.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
index 258164a..1dbf80e 100644
@@ -277,7 +277,7 @@ static void timekeeping_forward_now(struct timekeeper *tk)
 	tk->xtime_nsec += cycle_delta * tk->mult;
 
 	/* If arch requires, add in gettimeoffset() */
-	tk->xtime_nsec += arch_gettimeoffset() << tk->shift;
+	tk->xtime_nsec += (u64)arch_gettimeoffset() << tk->shift;
 
 	tk_normalize_xtime(tk);
 
Signature · 1 line
1.7.9.5
#4 lore raw

[tip:timers/urgent] time: Fix casting issue in timekeeping_forward_now

Commit-ID:  85dc8f05c93c8105987de9d7e7cebf15a72ff4ec
Gitweb:     http://git.kernel.org/tip/85dc8f05c93c8105987de9d7e7cebf15a72ff4ec
Author:     Andreas Schwab <[email protected]>
AuthorDate: Tue, 21 Aug 2012 20:30:47 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 22 Aug 2012 10:42:13 +0200

time: Fix casting issue in timekeeping_forward_now

arch_gettimeoffset returns a u32 value which when shifted by tk->shift can overflow. This issue was introduced with 1e75fa8be (time: Condense timekeeper.xtime into xtime_sec)

Cast it to u64 first.

---
 kernel/time/timekeeping.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/time/timekeeping.c +1 -1
index 258164a..1dbf80e 100644
@@ -277,7 +277,7 @@ static void timekeeping_forward_now(struct timekeeper *tk)
 	tk->xtime_nsec += cycle_delta * tk->mult;
 
 	/* If arch requires, add in gettimeoffset() */
-	tk->xtime_nsec += arch_gettimeoffset() << tk->shift;
+	tk->xtime_nsec += (u64)arch_gettimeoffset() << tk->shift;
 
 	tk_normalize_xtime(tk);
 
#5 lore raw

PATCH 3/4 time: Avoid potential shift-overflow with large shift values

In reply to John Stultz

Andreas Schwab noted that the 1 << tk->shift could overflow if the shift value was greater then 30, since 1 would be a 32bit long on 32bit architectures.

This patch uses 1ULL instead to ensure we don't overflow on the shift.

This issue was introduced by 1e75fa8be9fb61e1af46b5b3b176347a4c958ca1

Cc Ingo Molnar <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andreas Schwab <[email protected]>
---
 kernel/time/timekeeping.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timekeeping.c +2 -2
index 1dbf80e..a5a9389 100644
@@ -1184,9 +1184,9 @@ static void update_wall_time(void)
 	* the vsyscall implementations are converted to use xtime_nsec
 	* (shifted nanoseconds), this can be killed.
 	*/
-	remainder = tk->xtime_nsec & ((1 << tk->shift) - 1);
+	remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
 	tk->xtime_nsec -= remainder;
-	tk->xtime_nsec += 1 << tk->shift;
+	tk->xtime_nsec += 1ULL << tk->shift;
 	tk->ntp_error += remainder << tk->ntp_error_shift;
 
 	/*
Signature · 1 line
1.7.9.5
#6 lore raw

Re: [PATCH 3/4] time: Avoid potential shift-overflow with large shift values

Hi John,

Quoted 3 lines · John Stultz

Andreas Schwab noted that the 1 << tk->shift could overflow if the shift value was greater then 30, since 1 would be a 32bit long on 32bit architectures.

This comment is not entirely correct: "1" is not a long, but an (32-bit signed) int,
so it can overflow on 64-bit platforms, too.

Quoted 21 lines

This patch uses 1ULL instead to ensure we don't overflow on the shift.

This issue was introduced by 1e75fa8be9fb61e1af46b5b3b176347a4c958ca1

Cc Ingo Molnar <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andreas Schwab <[email protected]>
---
 kernel/time/timekeeping.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/timekeeping.c +0 -0
index 1dbf80e..a5a9389 100644
@@ -1184,9 +1184,9 @@ static void update_wall_time(void)
        * the vsyscall implementations are converted to use xtime_nsec

kernel/time/timekeeping.c static void update_wall_time(void) · line 1192

Quoted 10 lines
kernel/time/timekeeping.c +2 -2
        * (shifted nanoseconds), this can be killed.
        */
-       remainder = tk->xtime_nsec & ((1 << tk->shift) - 1);
+       remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
        tk->xtime_nsec -= remainder;
-       tk->xtime_nsec += 1 << tk->shift;
+       tk->xtime_nsec += 1ULL << tk->shift;
        tk->ntp_error += remainder << tk->ntp_error_shift;
 
        /*

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#7 lore raw

[tip:timers/urgent] time: Avoid potential shift overflow with large shift values

In reply to John Stultz
Commit-ID:  6ea565a9be32a3c8d1092017686f183b6d8c4514
Gitweb:     http://git.kernel.org/tip/6ea565a9be32a3c8d1092017686f183b6d8c4514
Author:     John Stultz <[email protected]>
AuthorDate: Tue, 21 Aug 2012 20:30:48 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 22 Aug 2012 10:42:13 +0200

time: Avoid potential shift overflow with large shift values

Andreas Schwab noticed that the 1 << tk->shift could overflow if the shift value was greater than 30, since 1 would be a 32bit long on 32bit architectures. This issue was introduced by 1e75fa8be (time:
Condense timekeeper.xtime into xtime_sec)

Use 1ULL instead to ensure we don't overflow on the shift.

---
 kernel/time/timekeeping.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/time/timekeeping.c +2 -2
index 1dbf80e..a5a9389 100644
@@ -1184,9 +1184,9 @@ static void update_wall_time(void)
 	* the vsyscall implementations are converted to use xtime_nsec
 	* (shifted nanoseconds), this can be killed.
 	*/
-	remainder = tk->xtime_nsec & ((1 << tk->shift) - 1);
+	remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
 	tk->xtime_nsec -= remainder;
-	tk->xtime_nsec += 1 << tk->shift;
+	tk->xtime_nsec += 1ULL << tk->shift;
 	tk->ntp_error += remainder << tk->ntp_error_shift;
 
 	/*
#8 lore raw

PATCH 4/4 time: Avoid making adjustments if we havne't accumulated anything

In reply to John Stultz

If update_wall_time() is called and the current offset isn't large enough to accumulate, avoid re-calling timekeeping_adjust which may change the clock freq and can cause 1ns inconsistencies with CLOCK_REALTIME_COARSE/CLOCK_MONOTONIC_COARSE.

Cc Ingo Molnar <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/time/timekeeping.c |    4 ++++
 1 file changed, 4 insertions(+)
kernel/time/timekeeping.c +4 -0
index a5a9389..0c1485e 100644
@@ -1152,6 +1152,10 @@ static void update_wall_time(void)
 	offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
 #endif
 
+	/* Check if there's really nothing to do */
+	if (offset < tk->cycle_interval)
+		goto out;
+
 	/*
 	 * With NO_HZ we may have to accumulate many cycle_intervals
 	 * (think "ticks") worth of time at once. To do this efficiently,
Signature · 1 line
1.7.9.5
#9 lore raw

[tip:timers/urgent] time: Avoid making adjustments if we haven' t accumulated anything

Commit-ID:  bf2ac312195155511a0f79325515cbb61929898a
Gitweb:     http://git.kernel.org/tip/bf2ac312195155511a0f79325515cbb61929898a
Author:     John Stultz <[email protected]>
AuthorDate: Tue, 21 Aug 2012 20:30:49 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 22 Aug 2012 10:42:13 +0200

time: Avoid making adjustments if we haven't accumulated anything

If update_wall_time() is called and the current offset isn't large enough to accumulate, avoid re-calling timekeeping_adjust which may change the clock freq and can cause 1ns inconsistencies with CLOCK_REALTIME_COARSE/CLOCK_MONOTONIC_COARSE.

---
 kernel/time/timekeeping.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/time/timekeeping.c +4 -0
index a5a9389..0c1485e 100644
@@ -1152,6 +1152,10 @@ static void update_wall_time(void)
 	offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
 #endif
 
+	/* Check if there's really nothing to do */
+	if (offset < tk->cycle_interval)
+		goto out;
+
 	/*
 	 * With NO_HZ we may have to accumulate many cycle_intervals
 	 * (think "ticks") worth of time at once. To do this efficiently,