Timekeeping queue for 4.17
Hey Thomas,
Just wanted to send along my timekeeping queue for 4.17.
Its a bit short, and I want to apologize as I've not had much time for maintanance and review work. I worry if my current workload levels persist, I might need to downgrade my status in the maintainers file, as I'm not particularly effective these days. :/
Please let me know if you have any objections.
The following changes since commit 4a3928c6f8a53fa1aed28ccba227742486e8ddcb:
Linux 4.16-rc3 (2018-02-25 18:50:41 -0800)
are available in the git repository at:
https://git.linaro.org/people/john.stultz/linux.git fortglx/4.17/time
for you to fetch changes up to 54f2dcda2ac66a83588acbd6ef2dc98c01d8d3fb:
x86: rtc: Stop using rtc deprecated functions (2018-03-09 10:35:13 -0800)
----------------------------------------------------------------
Arnd Bergmann (1):
y2038: time: Introduce struct __kernel_old_timeval
Benjamin Gaignard (1):
x86: rtc: Stop using rtc deprecated functions
Miroslav Lichvar (2):
timekeeping: Don't align frequency adjustments to ticks
timekeeping: Determine multiplier directly from NTP tick length
arch/x86/kernel/rtc.c | 6 +- include/linux/time32.h | 1 + include/linux/timekeeper_internal.h | 2 + include/uapi/linux/time.h | 12 +++ kernel/time/time.c | 12 +++ kernel/time/timekeeping.c | 141 ++++++++++++------------------------ 6 files changed, 77 insertions(+), 97 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/4 timekeeping: Don't align frequency adjustments to ticks
From: Miroslav Lichvar <[email protected]>
When the timekeeping multiplier is changed, the NTP error is updated to correct the clock for the delay between the tick and the update of the clock. This error is corrected in later updates and the clock appears as if the frequency was changed exactly on the tick.
Remove this correction to keep the point where the frequency is effectively changed at the time of the update. This removes a major source of the NTP error.
--- kernel/time/timekeeping.c | 3 --- 1 file changed, 3 deletions(-)
kernel/time/timekeeping.c +0 -3
@@ -1860,8 +1860,6 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,* xtime_nsec_2 = xtime_nsec_1 - offset * Which simplfies to: * xtime_nsec -= offset -* -* XXX - TODO: Doc ntp_error calculation. */ if ((mult_adj >0 ) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {/* NTP adjustment caused clocksource mult overflow */
@@ -1872,7 +1870,6 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, tk->tkr_mono.mult += mult_adj; tk->xtime_interval += interval; tk->tkr_mono.xtime_nsec -= offset; - tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; }/*
Signature · 1 line
2.7.4
tip-bot for Miroslav Lichvar
merge bot[tip:timers/core] timekeeping/ntp: Don't align NTP frequency adjustments to ticks
timekeeping/ntp: Don't align NTP frequency adjustments to ticks
Commit-ID: c2cda2a5bda9f1369c9d1ab54a20571c13cf2743 Gitweb: https://git.kernel.org/tip/c2cda2a5bda9f1369c9d1ab54a20571c13cf2743 Author: Miroslav Lichvar <[email protected]> AuthorDate: Fri, 9 Mar 2018 10:42:47 -0800 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 10 Mar 2018 09:12:41 +0100
timekeeping/ntp: Don't align NTP frequency adjustments to ticks
When the timekeeping multiplier is changed, the NTP error is updated to correct the clock for the delay between the tick and the update of the clock. This error is corrected in later updates and the clock appears as if the frequency was changed exactly on the tick.
Remove this correction to keep the point where the frequency is effectively changed at the time of the update. This removes a major source of the NTP error.
--- kernel/time/timekeeping.c | 3 --- 1 file changed, 3 deletions(-)
kernel/time/timekeeping.c +0 -3
@@ -1860,8 +1860,6 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,* xtime_nsec_2 = xtime_nsec_1 - offset * Which simplfies to: * xtime_nsec -= offset -* -* XXX - TODO: Doc ntp_error calculation. */ if ((mult_adj >0 ) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {/* NTP adjustment caused clocksource mult overflow */
@@ -1872,7 +1870,6 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, tk->tkr_mono.mult += mult_adj; tk->xtime_interval += interval; tk->tkr_mono.xtime_nsec -= offset; - tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; }/*
PATCH 2/4 timekeeping: Determine multiplier directly from NTP tick length
In reply to John StultzFrom: Miroslav Lichvar <[email protected]>
When the length of the NTP tick changes significantly, e.g. when an NTP/PTP application is correcting the initial offset of the clock, a large value may accumulate in the NTP error before the multiplier converges to the correct value. It may then take a very long time (hours or even days) before the error is corrected. This causes the clock to have an unstable frequency offset, which has a negative impact on the stability of synchronization with precise time sources (e.g. NTP/PTP using hardware timestamping or the PTP KVM clock).
Use division to determine the correct multiplier directly from the NTP tick length and replace the iterative approach. This removes the last major source of the NTP error. The only remaining source is now limited resolution of the multiplier, which is corrected by adding 1 to the multiplier when the system clock is behind the NTP time.
--- include/linux/timekeeper_internal.h | 2 + kernel/time/timekeeping.c | 138 ++++++++++++------------------------ 2 files changed, 49 insertions(+), 91 deletions(-)
include/linux/timekeeper_internal.h +2 -0
@@ -117,6 +117,8 @@ struct timekeeper {s64 ntp_error;u32 ntp_error_shift;u32 ntp_err_mult; +/* Flag used to avoid updating NTP twice with same second */ +u32 skip_second_overflow;#ifdef CONFIG_DEBUG_TIMEKEEPING long last_warning;/*
kernel/time/timekeeping.c +47 -91
189 lines not on this page · raw message
Signature · 1 line
2.7.4
tip-bot for Miroslav Lichvar
merge bot[tip:timers/core] timekeeping/ntp: Determine the multiplier directly from NTP tick length
timekeeping/ntp: Determine the multiplier directly from NTP tick length
Commit-ID: 78b98e3c5a66d569a53b8f57b6a698f912794a43 Gitweb: https://git.kernel.org/tip/78b98e3c5a66d569a53b8f57b6a698f912794a43 Author: Miroslav Lichvar <[email protected]> AuthorDate: Fri, 9 Mar 2018 10:42:48 -0800 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 10 Mar 2018 09:12:41 +0100
timekeeping/ntp: Determine the multiplier directly from NTP tick length
When the length of the NTP tick changes significantly, e.g. when an NTP/PTP application is correcting the initial offset of the clock, a large value may accumulate in the NTP error before the multiplier converges to the correct value. It may then take a very long time (hours or even days) before the error is corrected. This causes the clock to have an unstable frequency offset, which has a negative impact on the stability of synchronization with precise time sources (e.g. NTP/PTP using hardware timestamping or the PTP KVM clock).
Use division to determine the correct multiplier directly from the NTP tick length and replace the iterative approach. This removes the last major source of the NTP error. The only remaining source is now limited resolution of the multiplier, which is corrected by adding 1 to the multiplier when the system clock is behind the NTP time.
--- include/linux/timekeeper_internal.h | 2 + kernel/time/timekeeping.c | 138 ++++++++++++------------------------ 2 files changed, 49 insertions(+), 91 deletions(-)
include/linux/timekeeper_internal.h +2 -0
@@ -117,6 +117,8 @@ struct timekeeper {s64 ntp_error;u32 ntp_error_shift;u32 ntp_err_mult; +/* Flag used to avoid updating NTP twice with same second */ +u32 skip_second_overflow;#ifdef CONFIG_DEBUG_TIMEKEEPING long last_warning;/*
kernel/time/timekeeping.c +47 -91
189 lines not on this page · raw message
PATCH 3/4 y2038: time: Introduce struct __kernel_old_timeval
In reply to John StultzFrom: Arnd Bergmann <[email protected]>
Dealing with 'struct timeval' users in the y2038 series is a bit tricky:
We have two definitions of timeval that are visible to user space, one comes from glibc (or some other C library), the other comes from linux/time.h. The kernel copy is what we want to be used for a number of structures defined by the kernel itself, e.g. elf_prstatus (used it core dumps), sysinfo and rusage (used in system calls). These generally tend to be used for passing time intervals rather than absolute (epoch-based) times, so they do not suffer from the y2038 overflow. Some of them could be changed to use 64-bit timestamps by creating new system calls, others like the core files cannot easily be changed.
An application using these interfaces likely also uses gettimeofday() or other interfaces that use absolute times, and pass 'struct timeval' pointers directly into kernel interfaces, so glibc must redefine their timeval based on a 64-bit time_t when they introduce their y2038-safe interfaces.
The only reasonable way forward I see is to remove the 'timeval' definition from the kernel's uapi headers, and change the interfaces that we do not want to (or cannot) duplicate for 64-bit times to use a new __kernel_old_timeval definition instead. This type should be avoided for all new interfaces (those can use 64-bit nanoseconds, or the 64-bit version of timespec instead), and should be used with great care when converting existing interfaces from timeval, to be sure they don't suffer from the y2038 overflow, and only with consensus for the particular user that using __kernel_old_timeval is better than moving to a 64-bit based interface. The structure name is intentionally chosen to not conflict with user space types, and to be ugly enough to discourage its use.
Note that ioctl based interfaces that pass a bare 'timeval' pointer cannot change to '__kernel_old_timeval' because the user space source code refers to 'timeval' instead, and we don't want to modify the user space sources if possible. However, any application that relies on a structure to contain an embedded 'timeval' (e.g. by passing a pointer to the member into a function call that expects a timeval pointer) is broken when that structure gets converted to __kernel_old_timeval. I don't see any way around that, and we have to rely on the compiler to produce a warning or compile failure that will alert users when they recompile their sources against a new libc.
--- include/linux/time32.h | 1 + include/uapi/linux/time.h | 12 ++++++++++++ kernel/time/time.c | 12 ++++++++++++ 3 files changed, 25 insertions(+)
include/linux/time32.h +1 -0
@@ -217,5 +217,6 @@ static inline s64 timeval_to_ns(const struct timeval *tv)* Returns the timeval representation of the nsec parameter. */ extern struct timeval ns_to_timeval (const s64 nsec); +extern struct __kernel_old_timeval ns_to_kernel_old_timeval (const s64 nsec);#endif
include/uapi/linux/time.h +12 -0
@@ -43,6 +43,18 @@ struct itimerval { };/* +* legacy timeval structure, only embedded in structures that +* traditionally used 'timeval' to pass time intervals (not absolute +* times). Do not add new users. If user space fails to compile +* here, this is probably because it is not y2038 safe and needs to +* be changed to use another interface. +*/ +struct __kernel_old_timeval { +__kernel_long_t tv_sec;/* seconds */ +__kernel_long_t tv_usec;/* seconds */ +}; + +/* * The IDs of the various system clocks (for POSIX.1b interval timers): */ #define CLOCK_REALTIME 0
kernel/time/time.c +12 -0
@@ -488,6 +488,18 @@ struct timeval ns_to_timeval(const s64 nsec) }EXPORT_SYMBOL (ns_to_timeval); +struct __kernel_old_timeval ns_to_kernel_old_timeval (const s64 nsec) +{ +struct timespec64 ts =ns_to_timespec64 (nsec); +struct __kernel_old_timeval tv; + + tv.tv_sec = ts.tv_sec; + tv.tv_usec = (suseconds_t ) ts.tv_nsec /1000 ; + +return tv; +} +EXPORT_SYMBOL (ns_to_kernel_old_timeval); +/** * set_normalized_timespec - set timespec sec and nsec parts and normalize *
Signature · 1 line
2.7.4
Re: [PATCH 3/4] y2038: time: Introduce struct __kernel_old_timeval
Quoted 2 lines
+extern struct
__kernel_old_timevalns_to_kernel_old_timeval(const s64 nsec);
Generally there's no need to mark arguments with arithmethic types as const, as they are never modified in the calling scope.
include/uapi/linux/time.h struct itimerval { · line 54
Quoted 9 lines
+ * legacy timeval structure, only embedded in structures that
+ * traditionally used 'timeval' to pass time intervals (not absolute
+ * times). Do not add new users. If user space fails to compile
+ * here, this is probably because it is not y2038 safe and needs to
+ * be changed to use another interface.
+ */include/uapi/linux/time.h +3
-0+struct __kernel_old_timeval { +__kernel_long_t tv_sec;/* seconds */ +__kernel_long_t tv_usec;/* seconds */
s/seconds/microseconds
kernel/time/time.c struct timeval ns_to_timeval(const s64 nsec) · line 497
Quoted 7 lines
kernel/time/time.c +7
-0+struct __kernel_old_timeval ns_to_kernel_old_timeval (const s64 nsec) +{ +struct timespec64 ts =ns_to_timespec64 (nsec); +struct __kernel_old_timeval tv; + + tv.tv_sec = ts.tv_sec; + tv.tv_usec = (suseconds_t ) ts.tv_nsec /1000 ;
Is ts.tv_nsec guaranteed to never have bits set in the high 32 bits?
In any case, the space before the type cast is a bit confusing to me, I think it should be written as:
tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000;
To better show was the higher precedence of the cast is going to result in.
Thanks,
Ingo
Re: [PATCH 3/4] y2038: time: Introduce struct __kernel_old_timeval
Quoted 5 lines · Ingo Molnar
Quoted 2 lines
+extern struct
__kernel_old_timevalns_to_kernel_old_timeval(const s64 nsec);Generally there's no need to mark arguments with arithmethic types as const, as they are never modified in the calling scope.
Sure. Here I just copied from the neighboring line, but that's an obvious change.
Quoted 11 lines
include/uapi/linux/time.h struct itimerval { · line 54
Quoted 9 lines
+ * legacy timeval structure, only embedded in structures that
+ * traditionally used 'timeval' to pass time intervals (not absolute
+ * times). Do not add new users. If user space fails to compile
+ * here, this is probably because it is not y2038 safe and needs to
+ * be changed to use another interface.
+ */include/uapi/linux/time.h +3
-0+struct __kernel_old_timeval { +__kernel_long_t tv_sec;/* seconds */ +__kernel_long_t tv_usec;/* seconds */ s/seconds/microseconds
Right.
Quoted 9 lines
kernel/time/time.c struct timeval ns_to_timeval(const s64 nsec) · line 497
Quoted 7 lines
kernel/time/time.c +7
-0+struct __kernel_old_timeval ns_to_kernel_old_timeval (const s64 nsec) +{ +struct timespec64 ts =ns_to_timespec64 (nsec); +struct __kernel_old_timeval tv; + + tv.tv_sec = ts.tv_sec; + tv.tv_usec = (suseconds_t ) ts.tv_nsec /1000 ;Is ts.tv_nsec guaranteed to never have bits set in the high 32 bits?
Yes, ns_to_timespec64() produces a valid timespec64 structure.
Quoted 6 lines
In any case, the space before the type cast is a bit confusing to me, I think it should be written as:
tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000;To better show was the higher precedence of the cast is going to result in.
Sure.
Thanks for taking a look, I'll send an updated version.
Arnd
PATCH v2 1/1 y2038: introduce struct __kernel_old_timeval
Dealing with 'struct timeval' users in the y2038 series is a bit tricky:
We have two definitions of timeval that are visible to user space, one comes from glibc (or some other C library), the other comes from linux/time.h. The kernel copy is what we want to be used for a number of structures defined by the kernel itself, e.g. elf_prstatus (used it core dumps), sysinfo and rusage (used in system calls). These generally tend to be used for passing time intervals rather than absolute (epoch-based) times, so they do not suffer from the y2038 overflow. Some of them could be changed to use 64-bit timestamps by creating new system calls, others like the core files cannot easily be changed.
An application using these interfaces likely also uses gettimeofday() or other interfaces that use absolute times, and pass 'struct timeval' pointers directly into kernel interfaces, so glibc must redefine their timeval based on a 64-bit time_t when they introduce their y2038-safe interfaces.
The only reasonable way forward I see is to remove the 'timeval' definion from the kernel's uapi headers, and change the interfaces that we do not want to (or cannot) duplicate for 64-bit times to use a new __kernel_old_timeval definition instead. This type should be avoided for all new interfaces (those can use 64-bit nanoseconds, or the 64-bit version of timespec instead), and should be used with great care when converting existing interfaces from timeval, to be sure they don't suffer from the y2038 overflow, and only with consensus for the particular user that using __kernel_old_timeval is better than moving to a 64-bit based interface. The structure name is intentionally chosen to not conflict with user space types, and to be ugly enough to discourage its use.
Note that ioctl based interfaces that pass a bare 'timeval' pointer cannot change to '__kernel_old_timeval' because the user space source code refers to 'timeval' instead, and we don't want to modify the user space sources if possible. However, any application that relies on a structure to contain an embedded 'timeval' (e.g. by passing a pointer to the member into a function call that expects a timeval pointer) is broken when that structure gets converted to __kernel_old_timeval. I don't see any way around that, and we have to rely on the compiler to produce a warning or compile failure that will alert users when they recompile their sources against a new libc.
---
v2: update for Ingo's comments
--- include/linux/time32.h | 1 + include/uapi/linux/time.h | 12 ++++++++++++ kernel/time/time.c | 12 ++++++++++++ 3 files changed, 25 insertions(+)
include/linux/time32.h +1 -0
@@ -205,5 +205,6 @@ static inline s64 timeval_to_ns(const struct timeval *tv)* Returns the timeval representation of the nsec parameter. */ extern struct timeval ns_to_timeval (const s64 nsec); +extern struct __kernel_old_timeval ns_to_kernel_old_timeval (s64 nsec);#endif
include/uapi/linux/time.h +12 -0
@@ -50,6 +50,18 @@ struct __kernel_timespec {#endif /* +* legacy timeval structure, only embedded in structures that +* traditionally used 'timeval' to pass time intervals (not absolute +* times). Do not add new users. If user space fails to compile +* here, this is probably because it is not y2038 safe and needs to +* be changed to use another interface. +*/ +struct __kernel_old_timeval { +__kernel_long_t tv_sec;/* seconds */ +__kernel_long_t tv_usec;/* microseconds */ +}; + +/* * The IDs of the various system clocks (for POSIX.1b interval timers): */ #define CLOCK_REALTIME 0
kernel/time/time.c +12 -0
@@ -486,6 +486,18 @@ struct timeval ns_to_timeval(const s64 nsec) }EXPORT_SYMBOL (ns_to_timeval); +struct __kernel_old_timeval ns_to_kernel_old_timeval (const s64 nsec) +{ +struct timespec64 ts =ns_to_timespec64 (nsec); +struct __kernel_old_timeval tv; + + tv.tv_sec = ts.tv_sec; + tv.tv_usec = (suseconds_t )ts.tv_nsec /1000 ; + +return tv; +} +EXPORT_SYMBOL (ns_to_kernel_old_timeval); +/** * set_normalized_timespec - set timespec sec and nsec parts and normalize *
Signature · 1 line
2.9.0
Re: [PATCH] [v2] y2038: introduce struct __kernel_old_timeval
include/uapi/linux/time.h struct __kernel_timespec { · line 61
Quoted 11 lines · Arnd Bergmann
/*
+ * legacy timeval structure, only embedded in structures that
+ * traditionally used 'timeval' to pass time intervals (not absolute
+ * times). Do not add new users. If user space fails to compile
+ * here, this is probably because it is not y2038 safe and needs to
+ * be changed to use another interface.
+ */include/uapi/linux/time.h +3
-0+struct __kernel_old_timeval { +__kernel_long_t tv_sec;/* seconds */ +__kernel_long_t tv_usec;/* microseconds */
Please don't use these horrible tail comments. If you want to document the obvious, then please use proper kernel doc.
Thanks,
tglx
Re: [PATCH] [v2] y2038: introduce struct __kernel_old_timeval
Quoted 13 lines · Thomas Gleixner
include/uapi/linux/time.h struct __kernel_timespec { · line 61
Quoted 11 lines · Arnd Bergmann
/*
+ * legacy timeval structure, only embedded in structures that
+ * traditionally used 'timeval' to pass time intervals (not absolute
+ * times). Do not add new users. If user space fails to compile
+ * here, this is probably because it is not y2038 safe and needs to
+ * be changed to use another interface.
+ */include/uapi/linux/time.h +3
-0+struct __kernel_old_timeval { +__kernel_long_t tv_sec;/* seconds */ +__kernel_long_t tv_usec;/* microseconds */
Quoted 2 lines
Please don't use these horrible tail comments. If you want to document the obvious, then please use proper kernel doc.
I just removed them now, the only reason they were there is to keep the style in sync with the rest of the file, but it's also just a lot of duplication.
Arnd
PATCH 4/4 x86: rtc: Stop using rtc deprecated functions
In reply to John StultzFrom: Benjamin Gaignard <[email protected]>
rtc_time_to_tm() and rtc_tm_to_time() are deprecated because they rely on 32bits variables and that will make rtc break in y2038/2016. Stop using those two functions to safer 64bits ones.
--- arch/x86/kernel/rtc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/rtc.c +3 -3
@@ -41,11 +41,11 @@ EXPORT_SYMBOL(rtc_lock);*/ int mach_set_rtc_mmss (const struct timespec *now) { -unsigned long nowtime = now->tv_sec; +unsigned long long nowtime = now->tv_sec;struct rtc_time tm;int retval =0 ; -rtc_time_to_tm (nowtime, &tm); +rtc_time64_to_tm (nowtime, &tm);if (!rtc_valid_tm (&tm)) { retval =mc146818_set_time (&tm);if (retval)
@@ -53,7 +53,7 @@ int mach_set_rtc_mmss(const struct timespec *now) __func__, retval); }else {printk (KERN_ERR -"%s: Invalid RTC value: write of %lx to RTC failed\n" , +"%s: Invalid RTC value: write of %llx to RTC failed\n" , __func__, nowtime); retval = -EINVAL ; }
Signature · 1 line
2.7.4
tip-bot for Benjamin Gaignard
merge bot[tip:x86/cleanups] x86/rtc: Stop using deprecated functions
x86/rtc: Stop using deprecated functions
Commit-ID: 13cc36d76bc4f5a9801ae32630bc8240ba0cc522 Gitweb: https://git.kernel.org/tip/13cc36d76bc4f5a9801ae32630bc8240ba0cc522 Author: Benjamin Gaignard <[email protected]> AuthorDate: Fri, 9 Mar 2018 10:42:50 -0800 Committer: Thomas Gleixner <[email protected]> CommitDate: Thu, 15 Mar 2018 09:47:24 +0100
x86/rtc: Stop using deprecated functions
rtc_time_to_tm() and rtc_tm_to_time() are deprecated because they rely on 32bits variables and that will make rtc break in y2038/2016.
Use the proper y2038 safe functions.
--- arch/x86/kernel/rtc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/rtc.c +3 -3
@@ -41,11 +41,11 @@ EXPORT_SYMBOL(rtc_lock);*/ int mach_set_rtc_mmss (const struct timespec *now) { -unsigned long nowtime = now->tv_sec; +unsigned long long nowtime = now->tv_sec;struct rtc_time tm;int retval =0 ; -rtc_time_to_tm (nowtime, &tm); +rtc_time64_to_tm (nowtime, &tm);if (!rtc_valid_tm (&tm)) { retval =mc146818_set_time (&tm);if (retval)
@@ -53,7 +53,7 @@ int mach_set_rtc_mmss(const struct timespec *now) __func__, retval); }else {printk (KERN_ERR -"%s: Invalid RTC value: write of %lx to RTC failed\n" , +"%s: Invalid RTC value: write of %llx to RTC failed\n" , __func__, nowtime); retval = -EINVAL ; }