Commit d62ac21a authored by john stultz's avatar john stultz Committed by Linus Torvalds

[PATCH] ntp: avoid time_offset overflows

I've been seeing some odd NTP behavior recently on a few boxes and
finally narrowed it down to time_offset overflowing when converted to
SHIFT_UPDATE units (which was a side effect from my HZfreeNTP patch).

This patch converts time_offset from a long to a s64 which resolves the
issue.

[tglx@linutronix.de: signedness fixes]
Signed-off-by: default avatarJohn Stultz <johnstul@us.ibm.com>
Cc: Roman Zippel <zippel@linux-m68k.org>
Cc: john stultz <johnstul@us.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent b92c4f92
...@@ -32,7 +32,7 @@ static u64 tick_length, tick_length_base; ...@@ -32,7 +32,7 @@ static u64 tick_length, tick_length_base;
/* TIME_ERROR prevents overwriting the CMOS clock */ /* TIME_ERROR prevents overwriting the CMOS clock */
static int time_state = TIME_OK; /* clock synchronization status */ static int time_state = TIME_OK; /* clock synchronization status */
int time_status = STA_UNSYNC; /* clock status bits */ int time_status = STA_UNSYNC; /* clock status bits */
static long time_offset; /* time adjustment (ns) */ static s64 time_offset; /* time adjustment (ns) */
static long time_constant = 2; /* pll time constant */ static long time_constant = 2; /* pll time constant */
long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
...@@ -196,7 +196,7 @@ void __attribute__ ((weak)) notify_arch_cmos_timer(void) ...@@ -196,7 +196,7 @@ void __attribute__ ((weak)) notify_arch_cmos_timer(void)
*/ */
int do_adjtimex(struct timex *txc) int do_adjtimex(struct timex *txc)
{ {
long ltemp, mtemp, save_adjust; long mtemp, save_adjust, rem;
s64 freq_adj, temp64; s64 freq_adj, temp64;
int result; int result;
...@@ -277,14 +277,14 @@ int do_adjtimex(struct timex *txc) ...@@ -277,14 +277,14 @@ int do_adjtimex(struct timex *txc)
time_adjust = txc->offset; time_adjust = txc->offset;
} }
else if (time_status & STA_PLL) { else if (time_status & STA_PLL) {
ltemp = txc->offset * NSEC_PER_USEC; time_offset = txc->offset * NSEC_PER_USEC;
/* /*
* Scale the phase adjustment and * Scale the phase adjustment and
* clamp to the operating range. * clamp to the operating range.
*/ */
time_offset = min(ltemp, MAXPHASE * NSEC_PER_USEC); time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
time_offset = max(time_offset, -MAXPHASE * NSEC_PER_USEC); time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
/* /*
* Select whether the frequency is to be controlled * Select whether the frequency is to be controlled
...@@ -297,11 +297,11 @@ int do_adjtimex(struct timex *txc) ...@@ -297,11 +297,11 @@ int do_adjtimex(struct timex *txc)
mtemp = xtime.tv_sec - time_reftime; mtemp = xtime.tv_sec - time_reftime;
time_reftime = xtime.tv_sec; time_reftime = xtime.tv_sec;
freq_adj = (s64)time_offset * mtemp; freq_adj = time_offset * mtemp;
freq_adj = shift_right(freq_adj, time_constant * 2 + freq_adj = shift_right(freq_adj, time_constant * 2 +
(SHIFT_PLL + 2) * 2 - SHIFT_NSEC); (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) { if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
temp64 = (s64)time_offset << (SHIFT_NSEC - SHIFT_FLL); temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL);
if (time_offset < 0) { if (time_offset < 0) {
temp64 = -temp64; temp64 = -temp64;
do_div(temp64, mtemp); do_div(temp64, mtemp);
...@@ -314,8 +314,10 @@ int do_adjtimex(struct timex *txc) ...@@ -314,8 +314,10 @@ int do_adjtimex(struct timex *txc)
freq_adj += time_freq; freq_adj += time_freq;
freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC); freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC); time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
time_offset = (time_offset / NTP_INTERVAL_FREQ) time_offset = div_long_long_rem_signed(time_offset,
<< SHIFT_UPDATE; NTP_INTERVAL_FREQ,
&rem);
time_offset <<= SHIFT_UPDATE;
} /* STA_PLL */ } /* STA_PLL */
} /* txc->modes & ADJ_OFFSET */ } /* txc->modes & ADJ_OFFSET */
if (txc->modes & ADJ_TICK) if (txc->modes & ADJ_TICK)
...@@ -328,12 +330,12 @@ leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0) ...@@ -328,12 +330,12 @@ leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
result = TIME_ERROR; result = TIME_ERROR;
if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
txc->offset = save_adjust; txc->offset = save_adjust;
else else
txc->offset = shift_right(time_offset, SHIFT_UPDATE) txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
* NTP_INTERVAL_FREQ / 1000; NTP_INTERVAL_FREQ / 1000;
txc->freq = (time_freq / NSEC_PER_USEC) txc->freq = (time_freq / NSEC_PER_USEC) <<
<< (SHIFT_USEC - SHIFT_NSEC); (SHIFT_USEC - SHIFT_NSEC);
txc->maxerror = time_maxerror; txc->maxerror = time_maxerror;
txc->esterror = time_esterror; txc->esterror = time_esterror;
txc->status = time_status; txc->status = time_status;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment