Commit d62ac21aa075c8ddf3d02a98d28afce635e77e8e
Committed by
Linus Torvalds
1 parent
b92c4f922b
Exists in
master
and in
7 other branches
[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>
Showing 1 changed file with 16 additions and 14 deletions Side-by-side Diff
kernel/time/ntp.c
... | ... | @@ -32,7 +32,7 @@ |
32 | 32 | /* TIME_ERROR prevents overwriting the CMOS clock */ |
33 | 33 | static int time_state = TIME_OK; /* clock synchronization status */ |
34 | 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 | 36 | static long time_constant = 2; /* pll time constant */ |
37 | 37 | long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ |
38 | 38 | long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */ |
... | ... | @@ -196,7 +196,7 @@ |
196 | 196 | */ |
197 | 197 | int do_adjtimex(struct timex *txc) |
198 | 198 | { |
199 | - long ltemp, mtemp, save_adjust; | |
199 | + long mtemp, save_adjust, rem; | |
200 | 200 | s64 freq_adj, temp64; |
201 | 201 | int result; |
202 | 202 | |
203 | 203 | |
... | ... | @@ -277,14 +277,14 @@ |
277 | 277 | time_adjust = txc->offset; |
278 | 278 | } |
279 | 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 | 283 | * Scale the phase adjustment and |
284 | 284 | * clamp to the operating range. |
285 | 285 | */ |
286 | - time_offset = min(ltemp, MAXPHASE * NSEC_PER_USEC); | |
287 | - time_offset = max(time_offset, -MAXPHASE * NSEC_PER_USEC); | |
286 | + time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC); | |
287 | + time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC); | |
288 | 288 | |
289 | 289 | /* |
290 | 290 | * Select whether the frequency is to be controlled |
291 | 291 | |
... | ... | @@ -297,11 +297,11 @@ |
297 | 297 | mtemp = xtime.tv_sec - time_reftime; |
298 | 298 | time_reftime = xtime.tv_sec; |
299 | 299 | |
300 | - freq_adj = (s64)time_offset * mtemp; | |
300 | + freq_adj = time_offset * mtemp; | |
301 | 301 | freq_adj = shift_right(freq_adj, time_constant * 2 + |
302 | 302 | (SHIFT_PLL + 2) * 2 - SHIFT_NSEC); |
303 | 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 | 305 | if (time_offset < 0) { |
306 | 306 | temp64 = -temp64; |
307 | 307 | do_div(temp64, mtemp); |
... | ... | @@ -314,8 +314,10 @@ |
314 | 314 | freq_adj += time_freq; |
315 | 315 | freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC); |
316 | 316 | time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC); |
317 | - time_offset = (time_offset / NTP_INTERVAL_FREQ) | |
318 | - << SHIFT_UPDATE; | |
317 | + time_offset = div_long_long_rem_signed(time_offset, | |
318 | + NTP_INTERVAL_FREQ, | |
319 | + &rem); | |
320 | + time_offset <<= SHIFT_UPDATE; | |
319 | 321 | } /* STA_PLL */ |
320 | 322 | } /* txc->modes & ADJ_OFFSET */ |
321 | 323 | if (txc->modes & ADJ_TICK) |
322 | 324 | |
... | ... | @@ -328,12 +330,12 @@ |
328 | 330 | result = TIME_ERROR; |
329 | 331 | |
330 | 332 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) |
331 | - txc->offset = save_adjust; | |
333 | + txc->offset = save_adjust; | |
332 | 334 | else |
333 | - txc->offset = shift_right(time_offset, SHIFT_UPDATE) | |
334 | - * NTP_INTERVAL_FREQ / 1000; | |
335 | - txc->freq = (time_freq / NSEC_PER_USEC) | |
336 | - << (SHIFT_USEC - SHIFT_NSEC); | |
335 | + txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) * | |
336 | + NTP_INTERVAL_FREQ / 1000; | |
337 | + txc->freq = (time_freq / NSEC_PER_USEC) << | |
338 | + (SHIFT_USEC - SHIFT_NSEC); | |
337 | 339 | txc->maxerror = time_maxerror; |
338 | 340 | txc->esterror = time_esterror; |
339 | 341 | txc->status = time_status; |