ntp:Change the type of time_reftime and replace get_seconds()
cover ntp:Change the type of time_reftime and replace get_seconds()
The type of ntp static variant "time_reftime" and the type of get_seconds()'s return value are both unsign long now, which may cause y2038 problem on 32bit systems.
So change time_reftime' type to time64_t, and replace get_seconds with ktime_get_real_seconds() in ntp.ktime_get_real_seconds() does the same thing as get_seconds() and is y2038 safe, but ktime_get_real_seconds holds the timekeeping lock which is also held by the ntp code. We need timekeeping to provide a none lock version of ktime_get_real_seconds before we can start to solve the ntp.c's y2038 problem.
The [PATCH 1/2] provides the internal none lock version function. The [PATCH 2/2] does the real fix.
DengChao (2):
timekeeping: Provide internal function __ktime_get_real_seconds ntp:Change the type of time_reftime to time64_t and replace
get_seconds with __ktime_get_real_seconds
kernel/time/ntp.c | 10 ++++++---- kernel/time/timekeeping.c | 13 +++++++++++++ kernel/time/timekeeping_internal.h | 2 ++ 3 files changed, 21 insertions(+), 4 deletions(-)
Signature · 1 line
1.9.1
PATCH 1/2 timekeeping: Provide internal function __ktime_get_real_seconds
In order to fix Y2038 issues in the ntp code we will need replace get_seconds() with ktime_get_real_seconds() but as the ntp code uses the timekeeping lock which is also used by ktime_get_real_seconds(), we need a version without locking.
Add a new function __ktime_get_real_seconds() in timekeeping to do this.
--- kernel/time/timekeeping.c | 13 +++++++++++++ kernel/time/timekeeping_internal.h | 2 ++ 2 files changed, 15 insertions(+)
kernel/time/timekeeping.c +13 -0
@@ -846,6 +846,19 @@ time64_t ktime_get_real_seconds(void) }EXPORT_SYMBOL_GPL (ktime_get_real_seconds); +/** +* __ktime_get_real_seconds - The same as ktime_get_real_seconds +* but without the sequence counter protect. This internal function +* is called just when timekeeping lock is already held. +*/ +time64_t __ktime_get_real_seconds (void ) +{ +struct timekeeper *tk = &tk_core.timekeeper; + +return tk->xtime_sec; +} + +#ifdef CONFIG_NTP_PPS /**
kernel/time/timekeeping_internal.h +2 -0
@@ -26,4 +26,6 @@ static inline cycle_t clocksource_delta(cycle_t now, cycle_t last, cycle_t mask) }#endif +extern time64_t __ktime_get_real_seconds (void ); +#endif /* _TIMEKEEPING_INTERNAL_H */
Signature · 1 line
1.9.1
Re: [PATCH 1/2] timekeeping: Provide internal function __ktime_get_real_seconds
Quoted 9 lines · DengChao
In order to fix Y2038 issues in the ntp code we will need replace
get_seconds()withktime_get_real_seconds()but as the ntp code uses the timekeeping lock which is also used byktime_get_real_seconds(), we need a version without locking.
Add a new function__ktime_get_real_seconds()in timekeeping to do this.Reviewed-by John Stultz <[email protected]>Signed-off-by DengChao <[email protected]>
Queued for testing. Thanks for sending this in.
-john
PATCH 2/2 ntp:Change the type of time_reftime to time64_t and replace get_seconds with __ktime_get_real_seconds
In reply to DengChaoThe type of static variant "time_reftime" and the call of get_seconds in ntp are both not y2038 safe.
So change the type of time_reftime to time64_t and replace get_seconds with __ktime_get_real_seconds.
The local variant "secs" in ntp_update_offset represents seconds between now and last ntp adjustment, it seems impossible that this time will last more than 68 years, so keep its type as "long".
--- kernel/time/ntp.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/time/ntp.c +6 -4
@@ -18,6 +18,8 @@#include <linux/rtc.h> #include "ntp_internal.h" +#include "timekeeping_internal.h" +/* * NTP timekeeping variables:
@@ -70,7 +72,7 @@ static long time_esterror = NTP_PHASE_LIMIT;static s64 time_freq;/* time at last adjustment (secs): */ -static long time_reftime; +static time64_t time_reftime;static long time_adjust;
@@ -311,11 +313,11 @@ static void ntp_update_offset(long offset)* Select how the frequency is to be controlled * and in which mode (PLL or FLL). */ - secs =get_seconds () - time_reftime; + secs = (long )(__ktime_get_real_seconds () - time_reftime);if (unlikely (time_status &STA_FREQHOLD )) secs =0 ; - time_reftime =get_seconds (); + time_reftime =__ktime_get_real_seconds (); offset64 = offset; freq_adj =ntp_update_offset_fll (offset64, secs);
@@ -590,7 +592,7 @@ static inline void process_adj_status(struct timex *txc, struct timespec64 *ts)* reference time to current time. */ if (!(time_status &STA_PLL ) && (txc->status &STA_PLL )) - time_reftime =get_seconds (); + time_reftime =__ktime_get_real_seconds ();/* only set allowed bits */ time_status &=STA_RONLY ;
Signature · 1 line
1.9.1
Re: [PATCH 2/2] ntp:Change the type of time_reftime to time64_t and replace get_seconds with __ktime_get_real_seconds
Quoted 10 lines · DengChao
The type of static variant "
time_reftime" and the call ofget_secondsin ntp are both not y2038 safe.
So change the type oftime_reftimetotime64_tand replaceget_secondswith__ktime_get_real_seconds.
The local variant "secs" inntp_update_offsetrepresents seconds between now and last ntp adjustment, it seems impossible that this time will last more than 68 years, so keep its type as "long".Reviewed-by John Stultz <[email protected]>Signed-off-by DengChao <[email protected]>
Reworked the commit log a bit, but queued this for testing.
thanks
-john