time, Fix setting of hardware clock in NTP code
PATCH 1/1 time, Fix setting of hardware clock in NTP code
At init time, if the system time is "warped" forward in warp_clock() it will differ from the hardware clock by sys_tz.tz_minuteswest. This time difference is not taken into account when ntp updates the hardware clock, and this causes the system time to jump forward by this offset every reboot.
The kernel must take this offset into account when writing the system time to the hardware clock in the ntp code. This patch adds persistent_clock_is_local which indicates that an offset has been applied in warp_clock() and accounts for the "warp" before writing the hardware clock.
x86 does not have this problem as rtc writes are software limited to a +/-15 minute window relative to the current rtc time. Other arches, such as powerpc, however do a full synchronization of the system time to the rtc and will see this problem.
--- include/linux/time.h | 1 + kernel/time.c | 8 ++++++++ kernel/time/ntp.c | 8 ++++++-- 3 files changed, 15 insertions(+), 2 deletions(-)
include/linux/time.h +1 -0
@@ -117,6 +117,7 @@ static inline bool timespec_valid_strict(const struct timespec *ts)extern void read_persistent_clock (struct timespec *ts);extern void read_boot_clock (struct timespec *ts); +extern int persistent_clock_is_local;extern int update_persistent_clock (struct timespec now);void timekeeping_init (void );extern int timekeeping_suspended;
kernel/time.c +8 -0
@@ -115,6 +115,12 @@ SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv, }/* +* Indicates if there is an offset between the system clock and the hardware +* clock/persistent clock/rtc. +*/ +int persistent_clock_is_local; + +/* * Adjust the time obtained from the CMOS to be UTC time instead of * local time. *
@@ -135,6 +141,8 @@ static inline void warp_clock(void)struct timespec adjust; adjust =current_kernel_time (); +if (sys_tz.tz_minuteswest !=0 ) + persistent_clock_is_local =1 ; adjust.tv_sec += sys_tz.tz_minuteswest *60 ;do_settimeofday (&adjust); }
kernel/time/ntp.c +6 -2
@@ -510,8 +510,12 @@ static void sync_cmos_clock(struct work_struct *work) }getnstimeofday (&now); -if (abs (now.tv_nsec - (NSEC_PER_SEC /2 )) <= tick_nsec /2 ) - fail =update_persistent_clock (now); +if (abs (now.tv_nsec - (NSEC_PER_SEC /2 )) <= tick_nsec /2 ) { +struct timespec adjust = now; +if (persistent_clock_is_local) + adjust.tv_sec -= (sys_tz.tz_minuteswest *60 ); + fail =update_persistent_clock (adjust); + } next.tv_nsec = (NSEC_PER_SEC /2 ) - now.tv_nsec - (TICK_NSEC /2 );if (next.tv_nsec <=0 )
Signature · 1 line
1.7.9.3
Quoted 7 lines · Prarit Bhargava
kernel/time/ntp.c +0
-0@@ -510,8 +510,12 @@ static void sync_cmos_clock(struct work_struct *work) }
kernel/time/ntp.c static void sync_cmos_clock(struct work_struct *work) · line 519
Quoted 10 lines
kernel/time/ntp.c +6
-2getnstimeofday (&now); -if (abs (now.tv_nsec - (NSEC_PER_SEC /2 )) <= tick_nsec /2 ) - fail =update_persistent_clock (now); +if (abs (now.tv_nsec - (NSEC_PER_SEC /2 )) <= tick_nsec /2 ) { +struct timespec adjust = now; +if (persistent_clock_is_local) + adjust.tv_sec -= (sys_tz.tz_minuteswest *60 ); + fail =update_persistent_clock (adjust); + }
Do you mind reworking this patch on top of tip/timers/core? There's some recent changes that interact here.
thanks
-john
At init time, if the system time is "warped" forward in warp_clock() it will differ from the hardware clock by sys_tz.tz_minuteswest. This time difference is not taken into account when ntp updates the hardware clock, and this causes the system time to jump forward by this offset every reboot.
The kernel must take this offset into account when writing the system time to the hardware clock in the ntp code. This patch adds persistent_clock_is_local which indicates that an offset has been applied in warp_clock() and accounts for the "warp" before writing the hardware clock.
x86 does not have this problem as rtc writes are software limited to a +/-15 minute window relative to the current rtc time. Other arches, such as powerpc, however do a full synchronization of the system time to the rtc and will see this problem.
[v2]: generated against tip/timers/core
--- include/linux/time.h | 1 + kernel/time.c | 8 ++++++++ kernel/time/ntp.c | 8 ++++++-- 3 files changed, 15 insertions(+), 2 deletions(-)
include/linux/time.h +1 -0
@@ -128,6 +128,7 @@ static inline bool has_persistent_clock(void)extern void read_persistent_clock (struct timespec *ts);extern void read_boot_clock (struct timespec *ts); +extern int persistent_clock_is_local;extern int update_persistent_clock (struct timespec now);void timekeeping_init (void );extern int timekeeping_suspended;
kernel/time.c +8 -0
@@ -115,6 +115,12 @@ SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv, }/* +* Indicates if there is an offset between the system clock and the hardware +* clock/persistent clock/rtc. +*/ +int persistent_clock_is_local; + +/* * Adjust the time obtained from the CMOS to be UTC time instead of * local time. *
@@ -135,6 +141,8 @@ static inline void warp_clock(void)struct timespec adjust; adjust =current_kernel_time (); +if (sys_tz.tz_minuteswest !=0 ) + persistent_clock_is_local =1 ; adjust.tv_sec += sys_tz.tz_minuteswest *60 ;do_settimeofday (&adjust); }
kernel/time/ntp.c +6 -2
@@ -511,13 +511,17 @@ static void sync_cmos_clock(struct work_struct *work)getnstimeofday (&now);if (abs (now.tv_nsec - (NSEC_PER_SEC /2 )) <= tick_nsec /2 ) { +struct timespec adjust = now; + fail = -ENODEV ; +if (persistent_clock_is_local) + adjust.tv_sec -= (sys_tz.tz_minuteswest *60 );#ifdef CONFIG_GENERIC_CMOS_UPDATE - fail =update_persistent_clock (now); + fail =update_persistent_clock (adjust);#endif #ifdef CONFIG_RTC_SYSTOHC if (fail == -ENODEV ) - fail =rtc_set_ntp_time (now); + fail =rtc_set_ntp_time (adjust);#endif }
Signature · 1 line
1.8.1.2
Quoted 11 lines · Prarit Bhargava
At init time, if the system time is "warped" forward in
warp_clock()it will differ from the hardware clock by sys_tz.tz_minuteswest. This time difference is not taken into account when ntp updates the hardware clock, and this causes the system time to jump forward by this offset every reboot.The kernel must take this offset into account when writing the system time to the hardware clock in the ntp code. This patch adds
persistent_clock_is_localwhich indicates that an offset has been applied inwarp_clock()and accounts for the "warp" before writing the hardware clock.
Quoted 4 lines
x86 does not have this problem as rtc writes are software limited to a +/-15 minute window relative to the current rtc time. Other arches, such as powerpc, however do a full synchronization of the system time to the rtc and will see this problem.
Ok, I've got this queued in my tree. What sort of testing did you do with it?
I want to make sure we don't run into any bad interactions with the existing 15min cap on x86.
thanks
-john
Quoted 7 lines · John Stultz
On 02/08/2013 02:59 PM, Prarit Bhargava wrote:
Ok, I've got this queued in my tree. What sort of testing did you do with it?
I want to make sure we don't run into any bad interactions with the existing 15min cap on x86.
John,
I did the following:
I used powerpc pseries systems and tested this using both positive and negative values of sys_tz.minuteswest, with both UTC and LOCAL in /etc/adjtime. I dumped values of 'hwclock -D' and date and confirmed that I no longer see time increasing by sys_tz.minuteswest each reboot.
I also tested x86 32-bit and 64-bit as a sanity check and verified that the current behaviour on those arches is the same; ie) I don't see *any* impact to the x86 rtc. I dumped values of 'hwclock -D' and date, and again confirmed that I see no differences in values. I did that with both UTC and LOCAL.
I also tested a powerpc box and set the hwclock (via BIOS) back to Dec 6 2012 to see what would happen when I enabled ntp. The system booted, set the system time to Dec 6 2012, and then properly ended up with both system time AND hwclock as Feb 8 2013 after systemd init .... (The *exact* time-of-day was correct as well. I just can't remember the time I did it ;) )
And I did the same thing (adjusting the BIOS date back) on x86. I only see the hours and minutes change, as we expect. The year, month, day are unaffected with both UTC and LOCAL.
tl;dr Yup. Tested as much as I could think of doing before submitting. Tested on a both x86, powerpc. Fixed the bug on powerpc. No change in behavior seen with x86.
If you want me to do some other test I certainly can give it a shot.
P.
Quoted 27 lines · Prarit Bhargava
Quoted 5 lines · John Stultz
On 02/08/2013 02:59 PM, Prarit Bhargava wrote:
Ok, I've got this queued in my tree. What sort of testing did you do with it?I want to make sure we don't run into any bad interactions with the existing 15min cap on x86.
John,
I did the following:
I used powerpc pseries systems and tested this using both positive and negative values of sys_tz.minuteswest, with both UTC and LOCAL in
/etc/adjtime. I dumped values of 'hwclock -D' and date and confirmed that I no longer see time increasing by sys_tz.minuteswest each reboot.I also tested x86 32-bit and 64-bit as a sanity check and verified that the current behaviour on those arches is the same; ie) I don't see *any* impact to the x86 rtc. I dumped values of 'hwclock -D' and date, and again confirmed that I see no differences in values. I did that with both UTC and LOCAL.
I also tested a powerpc box and set the hwclock (via BIOS) back to Dec 6 2012 to see what would happen when I enabled ntp. The system booted, set the system time to Dec 6 2012, and then properly ended up with both system time AND hwclock as Feb 8 2013 after systemd init .... (The *exact* time-of-day was correct as well. I just can't remember the time I did it ;) )
Quoted 7 lines
And I did the same thing (adjusting the BIOS date back) on x86. I only see the hours and minutes change, as we expect. The year, month, day are unaffected with both UTC and LOCAL.
tl;dr Yup. Tested as much as I could think of doing before submitting. Tested on a both x86, powerpc. Fixed the bug on powerpc. No change in behavior seen with x86.
Great! This is perfect!
Thanks for being so thorough!
-john