Skip to content

Commit 04b617e

Browse files
Roman ZippelLinus Torvalds
authored andcommitted
[PATCH] ntp: convert time_freq to nsec value
This converts time_freq to a scaled nsec value and adds around 6bit of extra resolution. This pushes the time_freq to its 32bit limits so the calculatons have to be done with 64bit. Signed-off-by: Roman Zippel <[email protected]> Cc: john stultz <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 97eebe1 commit 04b617e

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

include/linux/timex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@
9191
#define SHIFT_SCALE 22 /* phase scale (shift) */
9292
#define SHIFT_UPDATE (SHIFT_HZ + 1) /* time offset scale (shift) */
9393
#define SHIFT_USEC 16 /* frequency offset scale (shift) */
94+
#define SHIFT_NSEC 12 /* kernel frequency offset scale */
9495
#define FINENSEC (1L << (SHIFT_SCALE - 10)) /* ~1 ns in phase units */
9596

9697
#define MAXPHASE 512000L /* max phase error (us) */
9798
#define MAXFREQ (512L << SHIFT_USEC) /* max frequency error (ppm) */
99+
#define MAXFREQ_NSEC (512000L << SHIFT_NSEC) /* max frequency error (ppb) */
98100
#define MINSEC 16L /* min interval between updates (s) */
99101
#define MAXSEC 1200L /* max interval between updates (s) */
100102
#define NTP_PHASE_LIMIT (MAXPHASE << 5) /* beyond max. dispersion */

kernel/time/ntp.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void ntp_update_frequency(void)
6666
{
6767
tick_length_base = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) << TICK_LENGTH_SHIFT;
6868
tick_length_base += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
69-
tick_length_base += ((s64)time_freq * NSEC_PER_USEC) << (TICK_LENGTH_SHIFT - SHIFT_USEC);
69+
tick_length_base += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
7070

7171
do_div(tick_length_base, HZ);
7272

@@ -200,6 +200,7 @@ void __attribute__ ((weak)) notify_arch_cmos_timer(void)
200200
int do_adjtimex(struct timex *txc)
201201
{
202202
long ltemp, mtemp, save_adjust;
203+
s64 freq_adj;
203204
int result;
204205

205206
/* In order to modify anything, you gotta be super-user! */
@@ -245,7 +246,7 @@ int do_adjtimex(struct timex *txc)
245246
result = -EINVAL;
246247
goto leave;
247248
}
248-
time_freq = txc->freq;
249+
time_freq = ((s64)txc->freq * NSEC_PER_USEC) >> (SHIFT_USEC - SHIFT_NSEC);
249250
}
250251

251252
if (txc->modes & ADJ_MAXERROR) {
@@ -278,14 +279,14 @@ int do_adjtimex(struct timex *txc)
278279
time_adjust = txc->offset;
279280
}
280281
else if (time_status & STA_PLL) {
281-
ltemp = txc->offset;
282+
ltemp = txc->offset * NSEC_PER_USEC;
282283

283284
/*
284285
* Scale the phase adjustment and
285286
* clamp to the operating range.
286287
*/
287-
time_offset = min(ltemp, MAXPHASE);
288-
time_offset = max(time_offset, -MAXPHASE);
288+
time_offset = min(ltemp, MAXPHASE * NSEC_PER_USEC);
289+
time_offset = max(time_offset, -MAXPHASE * NSEC_PER_USEC);
289290

290291
/*
291292
* Select whether the frequency is to be controlled
@@ -297,24 +298,31 @@ int do_adjtimex(struct timex *txc)
297298
time_reftime = xtime.tv_sec;
298299
mtemp = xtime.tv_sec - time_reftime;
299300
time_reftime = xtime.tv_sec;
301+
freq_adj = 0;
300302
if (time_status & STA_FLL) {
301303
if (mtemp >= MINSEC) {
302-
ltemp = ((time_offset << 12) / mtemp) << (SHIFT_USEC - 12);
303-
time_freq += shift_right(ltemp, SHIFT_KH);
304+
freq_adj = (s64)time_offset << (SHIFT_NSEC - SHIFT_KH);
305+
if (time_offset < 0) {
306+
freq_adj = -freq_adj;
307+
do_div(freq_adj, mtemp);
308+
freq_adj = -freq_adj;
309+
} else
310+
do_div(freq_adj, mtemp);
304311
} else /* calibration interval too short (p. 12) */
305312
result = TIME_ERROR;
306313
} else { /* PLL mode */
307314
if (mtemp < MAXSEC) {
308-
ltemp *= mtemp;
309-
time_freq += shift_right(ltemp,(time_constant +
315+
freq_adj = (s64)ltemp * mtemp;
316+
freq_adj = shift_right(freq_adj,(time_constant +
310317
time_constant +
311-
SHIFT_KF - SHIFT_USEC));
318+
SHIFT_KF - SHIFT_NSEC));
312319
} else /* calibration interval too long (p. 12) */
313320
result = TIME_ERROR;
314321
}
315-
time_freq = min(time_freq, MAXFREQ);
316-
time_freq = max(time_freq, -MAXFREQ);
317-
time_offset = (time_offset * NSEC_PER_USEC / HZ) << SHIFT_UPDATE;
322+
freq_adj += time_freq;
323+
freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
324+
time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
325+
time_offset = (time_offset / HZ) << SHIFT_UPDATE;
318326
} /* STA_PLL */
319327
} /* txc->modes & ADJ_OFFSET */
320328
if (txc->modes & ADJ_TICK)
@@ -330,7 +338,7 @@ leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
330338
txc->offset = save_adjust;
331339
else
332340
txc->offset = shift_right(time_offset, SHIFT_UPDATE) * HZ / 1000;
333-
txc->freq = time_freq;
341+
txc->freq = (time_freq / NSEC_PER_USEC) << (SHIFT_USEC - SHIFT_NSEC);
334342
txc->maxerror = time_maxerror;
335343
txc->esterror = time_esterror;
336344
txc->status = time_status;

0 commit comments

Comments
 (0)