diff options
author | john stultz <johnstul@us.ibm.com> | 2007-03-27 01:32:26 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-03-27 12:05:15 -0400 |
commit | d62ac21aa075c8ddf3d02a98d28afce635e77e8e (patch) | |
tree | d315a4aaa57b219c151a5e2d9ae0e557517a0bd2 /kernel | |
parent | b92c4f922b2e8c70f8790d42e45bb9401c57be63 (diff) |
[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: John 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: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/time/ntp.c | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c index eb12509e00bd..cb25649c6f50 100644 --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c | |||
@@ -32,7 +32,7 @@ static u64 tick_length, tick_length_base; | |||
32 | /* TIME_ERROR prevents overwriting the CMOS clock */ | 32 | /* TIME_ERROR prevents overwriting the CMOS clock */ |
33 | static int time_state = TIME_OK; /* clock synchronization status */ | 33 | static int time_state = TIME_OK; /* clock synchronization status */ |
34 | int time_status = STA_UNSYNC; /* clock status bits */ | 34 | int time_status = STA_UNSYNC; /* clock status bits */ |
35 | static long time_offset; /* time adjustment (ns) */ | 35 | static s64 time_offset; /* time adjustment (ns) */ |
36 | static long time_constant = 2; /* pll time constant */ | 36 | static long time_constant = 2; /* pll time constant */ |
37 | long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ | 37 | long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ |
38 | long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ | 38 | long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ |
@@ -196,7 +196,7 @@ void __attribute__ ((weak)) notify_arch_cmos_timer(void) | |||
196 | */ | 196 | */ |
197 | int do_adjtimex(struct timex *txc) | 197 | int do_adjtimex(struct timex *txc) |
198 | { | 198 | { |
199 | long ltemp, mtemp, save_adjust; | 199 | long mtemp, save_adjust, rem; |
200 | s64 freq_adj, temp64; | 200 | s64 freq_adj, temp64; |
201 | int result; | 201 | int result; |
202 | 202 | ||
@@ -277,14 +277,14 @@ int do_adjtimex(struct timex *txc) | |||
277 | time_adjust = txc->offset; | 277 | time_adjust = txc->offset; |
278 | } | 278 | } |
279 | else if (time_status & STA_PLL) { | 279 | else if (time_status & STA_PLL) { |
280 | ltemp = txc->offset * NSEC_PER_USEC; | 280 | time_offset = txc->offset * NSEC_PER_USEC; |
281 | 281 | ||
282 | /* | 282 | /* |
283 | * Scale the phase adjustment and | 283 | * Scale the phase adjustment and |
284 | * clamp to the operating range. | 284 | * clamp to the operating range. |
285 | */ | 285 | */ |
286 | time_offset = min(ltemp, MAXPHASE * NSEC_PER_USEC); | 286 | time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC); |
287 | time_offset = max(time_offset, -MAXPHASE * NSEC_PER_USEC); | 287 | time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC); |
288 | 288 | ||
289 | /* | 289 | /* |
290 | * Select whether the frequency is to be controlled | 290 | * Select whether the frequency is to be controlled |
@@ -297,11 +297,11 @@ int do_adjtimex(struct timex *txc) | |||
297 | mtemp = xtime.tv_sec - time_reftime; | 297 | mtemp = xtime.tv_sec - time_reftime; |
298 | time_reftime = xtime.tv_sec; | 298 | time_reftime = xtime.tv_sec; |
299 | 299 | ||
300 | freq_adj = (s64)time_offset * mtemp; | 300 | freq_adj = time_offset * mtemp; |
301 | freq_adj = shift_right(freq_adj, time_constant * 2 + | 301 | freq_adj = shift_right(freq_adj, time_constant * 2 + |
302 | (SHIFT_PLL + 2) * 2 - SHIFT_NSEC); | 302 | (SHIFT_PLL + 2) * 2 - SHIFT_NSEC); |
303 | if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) { | 303 | if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) { |
304 | temp64 = (s64)time_offset << (SHIFT_NSEC - SHIFT_FLL); | 304 | temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL); |
305 | if (time_offset < 0) { | 305 | if (time_offset < 0) { |
306 | temp64 = -temp64; | 306 | temp64 = -temp64; |
307 | do_div(temp64, mtemp); | 307 | do_div(temp64, mtemp); |
@@ -314,8 +314,10 @@ int do_adjtimex(struct timex *txc) | |||
314 | freq_adj += time_freq; | 314 | freq_adj += time_freq; |
315 | freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC); | 315 | freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC); |
316 | time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC); | 316 | time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC); |
317 | time_offset = (time_offset / NTP_INTERVAL_FREQ) | 317 | time_offset = div_long_long_rem_signed(time_offset, |
318 | << SHIFT_UPDATE; | 318 | NTP_INTERVAL_FREQ, |
319 | &rem); | ||
320 | time_offset <<= SHIFT_UPDATE; | ||
319 | } /* STA_PLL */ | 321 | } /* STA_PLL */ |
320 | } /* txc->modes & ADJ_OFFSET */ | 322 | } /* txc->modes & ADJ_OFFSET */ |
321 | if (txc->modes & ADJ_TICK) | 323 | if (txc->modes & ADJ_TICK) |
@@ -328,12 +330,12 @@ leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0) | |||
328 | result = TIME_ERROR; | 330 | result = TIME_ERROR; |
329 | 331 | ||
330 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) | 332 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) |
331 | txc->offset = save_adjust; | 333 | txc->offset = save_adjust; |
332 | else | 334 | else |
333 | txc->offset = shift_right(time_offset, SHIFT_UPDATE) | 335 | txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) * |
334 | * NTP_INTERVAL_FREQ / 1000; | 336 | NTP_INTERVAL_FREQ / 1000; |
335 | txc->freq = (time_freq / NSEC_PER_USEC) | 337 | txc->freq = (time_freq / NSEC_PER_USEC) << |
336 | << (SHIFT_USEC - SHIFT_NSEC); | 338 | (SHIFT_USEC - SHIFT_NSEC); |
337 | txc->maxerror = time_maxerror; | 339 | txc->maxerror = time_maxerror; |
338 | txc->esterror = time_esterror; | 340 | txc->esterror = time_esterror; |
339 | txc->status = time_status; | 341 | txc->status = time_status; |