Skip to contentSkip to conversation

time: Fix 1ns/tick drift w/ GENERIC_TIME_VSYSCALL_OLD

PATCH New John Stultz 2 participants 2 messages Nov 22, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 time: Fix 1ns/tick drift w/ GENERIC_TIME_VSYSCALL_OLD

From: Martin Schwidefsky <[email protected]>

Since commit 1e75fa8be9f (time: Condense timekeeper.xtime into xtime_sec - merged in v3.6), there has been an problem with the error accounting in the timekeeping code, such that when truncating to nanoseconds, we round up to the next nsec, but the balancing adjustment to the ntp_error value was dropped.

This causes 1ns per tick drift forward of the clock.

In 3.7, this logic was isolated to only GENERIC_TIME_VSYSCALL_OLD architectures (s390, ia64, powerpc).

The fix is simply to balance the accounting and to subtract the added nanosecond from ntp_error. This allows the internal long-term clock steering to keep the clock accuate.

While this fix removes the regression added in 1e75fa8be9f, the ideal solution is to move away from GENERIC_TIME_VSYSCALL_OLD and use the new VSYSCALL method, which avoids entirely the nanosecond granular rounding, and the resulting short-term clock adjustment oscillation needed to keep long term accurate time.

Many thanks to Martin for his efforts identifying this subtle bug, and providing the fix.

Cc Tony Luck <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Paul Turner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Fenghua Yu <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc stable <[email protected]> #v3.6+
---
 kernel/time/timekeeping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
index 3abf534..87b4f00 100644
@@ -1347,7 +1347,7 @@ static inline void old_vsyscall_fixup(struct timekeeper *tk)
 	tk->xtime_nsec -= remainder;
 	tk->xtime_nsec += 1ULL << tk->shift;
 	tk->ntp_error += remainder << tk->ntp_error_shift;
-
+	tk->ntp_error -= (1ULL << tk->shift) << tk->ntp_error_shift;
 }
 #else
 #define old_vsyscall_fixup(tk)
Signature · 1 line
1.8.3.2
#1 lore raw
time: Fix 1ns/tick drift w/ GENERIC_TIME_VSYSCALL_OLD
Commit-ID:  4be77398ac9d948773116b6be4a3c91b3d6ea18c
Gitweb:     http://git.kernel.org/tip/4be77398ac9d948773116b6be4a3c91b3d6ea18c
Author:     Martin Schwidefsky <[email protected]>
AuthorDate: Fri, 22 Nov 2013 11:44:51 -0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 22 Nov 2013 21:08:11 +0100

time: Fix 1ns/tick drift w/ GENERIC_TIME_VSYSCALL_OLD

Since commit 1e75fa8be9f (time: Condense timekeeper.xtime into xtime_sec - merged in v3.6), there has been an problem with the error accounting in the timekeeping code, such that when truncating to nanoseconds, we round up to the next nsec, but the balancing adjustment to the ntp_error value was dropped.

This causes 1ns per tick drift forward of the clock.

In 3.7, this logic was isolated to only GENERIC_TIME_VSYSCALL_OLD architectures (s390, ia64, powerpc).

The fix is simply to balance the accounting and to subtract the added nanosecond from ntp_error. This allows the internal long-term clock steering to keep the clock accurate.

While this fix removes the regression added in 1e75fa8be9f, the ideal solution is to move away from GENERIC_TIME_VSYSCALL_OLD and use the new VSYSCALL method, which avoids entirely the nanosecond granular rounding, and the resulting short-term clock adjustment oscillation needed to keep long term accurate time.

[ jstultz: Many thanks to Martin for his efforts identifying this
subtle bug, and providing the fix. ]

Originally-from: Martin Schwidefsky <[email protected]>

Cc Tony Luck <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Paul Turner <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Fenghua Yu <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc stable <[email protected]> #v3.6+
---
 kernel/time/timekeeping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
index 3abf534..87b4f00 100644
@@ -1347,7 +1347,7 @@ static inline void old_vsyscall_fixup(struct timekeeper *tk)
 	tk->xtime_nsec -= remainder;
 	tk->xtime_nsec += 1ULL << tk->shift;
 	tk->ntp_error += remainder << tk->ntp_error_shift;
-
+	tk->ntp_error -= (1ULL << tk->shift) << tk->ntp_error_shift;
 }
 #else
 #define old_vsyscall_fixup(tk)