]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - kernel/time/ntp.c
timekeeping: fix rounding problem during clock update
[mirror_ubuntu-hirsute-kernel.git] / kernel / time / ntp.c
CommitLineData
4c7ee8de
JS
1/*
2 * linux/kernel/time/ntp.c
3 *
4 * NTP state machine interfaces and logic.
5 *
6 * This code was mainly moved from kernel/timer.c and kernel/time.c
7 * Please see those files for relevant copyright info and historical
8 * changelogs.
9 */
10
11#include <linux/mm.h>
12#include <linux/time.h>
13#include <linux/timex.h>
e8edc6e0
AD
14#include <linux/jiffies.h>
15#include <linux/hrtimer.h>
aa0ac365 16#include <linux/capability.h>
71abb3af 17#include <linux/math64.h>
7dffa3c6 18#include <linux/clocksource.h>
eb3f938f 19#include <linux/workqueue.h>
4c7ee8de
JS
20#include <asm/timex.h>
21
b0ee7556
RZ
22/*
23 * Timekeeping variables
24 */
25unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
26unsigned long tick_nsec; /* ACTHZ period (nsec) */
8383c423
RZ
27u64 tick_length;
28static u64 tick_length_base;
b0ee7556 29
7dffa3c6
RZ
30static struct hrtimer leap_timer;
31
8f807f8d
RZ
32#define MAX_TICKADJ 500 /* microsecs */
33#define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
7fc5c784 34 NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
4c7ee8de
JS
35
36/*
37 * phase-lock loop variables
38 */
39/* TIME_ERROR prevents overwriting the CMOS clock */
70bc42f9 40static int time_state = TIME_OK; /* clock synchronization status */
4c7ee8de 41int time_status = STA_UNSYNC; /* clock status bits */
153b5d05 42static long time_tai; /* TAI offset (s) */
ee9851b2 43static s64 time_offset; /* time adjustment (ns) */
70bc42f9 44static long time_constant = 2; /* pll time constant */
4c7ee8de
JS
45long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
46long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
074b3b87 47static s64 time_freq; /* frequency offset (scaled ns/s)*/
70bc42f9 48static long time_reftime; /* time at last adjustment (s) */
4c7ee8de 49long time_adjust;
10a398d0 50static long ntp_tick_adj;
4c7ee8de 51
70bc42f9
AB
52static void ntp_update_frequency(void)
53{
f4304ab2 54 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
7fc5c784
RZ
55 << NTP_SCALE_SHIFT;
56 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
074b3b87 57 second_length += time_freq;
70bc42f9 58
f4304ab2 59 tick_length_base = second_length;
70bc42f9 60
7fc5c784 61 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
71abb3af 62 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
70bc42f9
AB
63}
64
ee9851b2
RZ
65static void ntp_update_offset(long offset)
66{
67 long mtemp;
68 s64 freq_adj;
69
70 if (!(time_status & STA_PLL))
71 return;
72
eea83d89 73 if (!(time_status & STA_NANO))
9f14f669 74 offset *= NSEC_PER_USEC;
ee9851b2
RZ
75
76 /*
77 * Scale the phase adjustment and
78 * clamp to the operating range.
79 */
9f14f669
RZ
80 offset = min(offset, MAXPHASE);
81 offset = max(offset, -MAXPHASE);
ee9851b2
RZ
82
83 /*
84 * Select how the frequency is to be controlled
85 * and in which mode (PLL or FLL).
86 */
87 if (time_status & STA_FREQHOLD || time_reftime == 0)
88 time_reftime = xtime.tv_sec;
89 mtemp = xtime.tv_sec - time_reftime;
90 time_reftime = xtime.tv_sec;
91
9f14f669 92 freq_adj = (s64)offset * mtemp;
7fc5c784 93 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
eea83d89
RZ
94 time_status &= ~STA_MODE;
95 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
7fc5c784 96 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
074b3b87 97 mtemp);
eea83d89
RZ
98 time_status |= STA_MODE;
99 }
ee9851b2 100 freq_adj += time_freq;
074b3b87
RZ
101 freq_adj = min(freq_adj, MAXFREQ_SCALED);
102 time_freq = max(freq_adj, -MAXFREQ_SCALED);
9f14f669 103
7fc5c784 104 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
ee9851b2
RZ
105}
106
b0ee7556
RZ
107/**
108 * ntp_clear - Clears the NTP state variables
109 *
110 * Must be called while holding a write on the xtime_lock
111 */
112void ntp_clear(void)
113{
114 time_adjust = 0; /* stop active adjtime() */
115 time_status |= STA_UNSYNC;
116 time_maxerror = NTP_PHASE_LIMIT;
117 time_esterror = NTP_PHASE_LIMIT;
118
119 ntp_update_frequency();
120
121 tick_length = tick_length_base;
3d3675cc 122 time_offset = 0;
b0ee7556
RZ
123}
124
4c7ee8de 125/*
7dffa3c6
RZ
126 * Leap second processing. If in leap-insert state at the end of the
127 * day, the system clock is set back one second; if in leap-delete
128 * state, the system clock is set ahead one second.
4c7ee8de 129 */
7dffa3c6 130static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
4c7ee8de 131{
7dffa3c6 132 enum hrtimer_restart res = HRTIMER_NORESTART;
4c7ee8de 133
7dffa3c6 134 write_seqlock_irq(&xtime_lock);
4c7ee8de 135
4c7ee8de
JS
136 switch (time_state) {
137 case TIME_OK:
4c7ee8de
JS
138 break;
139 case TIME_INS:
7dffa3c6
RZ
140 xtime.tv_sec--;
141 wall_to_monotonic.tv_sec++;
142 time_state = TIME_OOP;
143 printk(KERN_NOTICE "Clock: "
144 "inserting leap second 23:59:60 UTC\n");
145 leap_timer.expires = ktime_add_ns(leap_timer.expires,
146 NSEC_PER_SEC);
147 res = HRTIMER_RESTART;
4c7ee8de
JS
148 break;
149 case TIME_DEL:
7dffa3c6
RZ
150 xtime.tv_sec++;
151 time_tai--;
152 wall_to_monotonic.tv_sec--;
153 time_state = TIME_WAIT;
154 printk(KERN_NOTICE "Clock: "
155 "deleting leap second 23:59:59 UTC\n");
4c7ee8de
JS
156 break;
157 case TIME_OOP:
153b5d05 158 time_tai++;
4c7ee8de 159 time_state = TIME_WAIT;
7dffa3c6 160 /* fall through */
4c7ee8de
JS
161 case TIME_WAIT:
162 if (!(time_status & (STA_INS | STA_DEL)))
ee9851b2 163 time_state = TIME_OK;
7dffa3c6
RZ
164 break;
165 }
166 update_vsyscall(&xtime, clock);
167
168 write_sequnlock_irq(&xtime_lock);
169
170 return res;
171}
172
173/*
174 * this routine handles the overflow of the microsecond field
175 *
176 * The tricky bits of code to handle the accurate clock support
177 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
178 * They were originally developed for SUN and DEC kernels.
179 * All the kudos should go to Dave for this stuff.
180 */
181void second_overflow(void)
182{
183 s64 time_adj;
184
185 /* Bump the maxerror field */
186 time_maxerror += MAXFREQ / NSEC_PER_USEC;
187 if (time_maxerror > NTP_PHASE_LIMIT) {
188 time_maxerror = NTP_PHASE_LIMIT;
189 time_status |= STA_UNSYNC;
4c7ee8de
JS
190 }
191
192 /*
f1992393
RZ
193 * Compute the phase adjustment for the next second. The offset is
194 * reduced by a fixed factor times the time constant.
4c7ee8de 195 */
b0ee7556 196 tick_length = tick_length_base;
f1992393 197 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
3d3675cc 198 time_offset -= time_adj;
9f14f669 199 tick_length += time_adj;
4c7ee8de 200
8f807f8d
RZ
201 if (unlikely(time_adjust)) {
202 if (time_adjust > MAX_TICKADJ) {
203 time_adjust -= MAX_TICKADJ;
204 tick_length += MAX_TICKADJ_SCALED;
205 } else if (time_adjust < -MAX_TICKADJ) {
206 time_adjust += MAX_TICKADJ;
207 tick_length -= MAX_TICKADJ_SCALED;
208 } else {
8f807f8d 209 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
7fc5c784 210 NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
bb1d8605 211 time_adjust = 0;
8f807f8d 212 }
4c7ee8de
JS
213 }
214}
215
82644459 216#ifdef CONFIG_GENERIC_CMOS_UPDATE
4c7ee8de 217
82644459
TG
218/* Disable the cmos update - used by virtualization and embedded */
219int no_sync_cmos_clock __read_mostly;
220
eb3f938f 221static void sync_cmos_clock(struct work_struct *work);
82644459 222
eb3f938f 223static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
82644459 224
eb3f938f 225static void sync_cmos_clock(struct work_struct *work)
82644459
TG
226{
227 struct timespec now, next;
228 int fail = 1;
229
230 /*
231 * If we have an externally synchronized Linux clock, then update
232 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
233 * called as close as possible to 500 ms before the new second starts.
234 * This code is run on a timer. If the clock is set, that timer
235 * may not expire at the correct time. Thus, we adjust...
236 */
237 if (!ntp_synced())
238 /*
239 * Not synced, exit, do not restart a timer (if one is
240 * running, let it run out).
241 */
242 return;
243
244 getnstimeofday(&now);
fa6a1a55 245 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
82644459
TG
246 fail = update_persistent_clock(now);
247
248 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
249 if (next.tv_nsec <= 0)
250 next.tv_nsec += NSEC_PER_SEC;
251
252 if (!fail)
253 next.tv_sec = 659;
254 else
255 next.tv_sec = 0;
256
257 if (next.tv_nsec >= NSEC_PER_SEC) {
258 next.tv_sec++;
259 next.tv_nsec -= NSEC_PER_SEC;
260 }
eb3f938f 261 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
82644459
TG
262}
263
264static void notify_cmos_timer(void)
4c7ee8de 265{
298a5df4 266 if (!no_sync_cmos_clock)
eb3f938f 267 schedule_delayed_work(&sync_cmos_work, 0);
4c7ee8de
JS
268}
269
82644459
TG
270#else
271static inline void notify_cmos_timer(void) { }
272#endif
273
4c7ee8de
JS
274/* adjtimex mainly allows reading (and writing, if superuser) of
275 * kernel time-keeping variables. used by xntpd.
276 */
277int do_adjtimex(struct timex *txc)
278{
eea83d89 279 struct timespec ts;
4c7ee8de
JS
280 int result;
281
916c7a85
RZ
282 /* Validate the data before disabling interrupts */
283 if (txc->modes & ADJ_ADJTIME) {
eea83d89 284 /* singleshot must not be used with any other mode bits */
916c7a85 285 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
4c7ee8de 286 return -EINVAL;
916c7a85
RZ
287 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
288 !capable(CAP_SYS_TIME))
289 return -EPERM;
290 } else {
291 /* In order to modify anything, you gotta be super-user! */
292 if (txc->modes && !capable(CAP_SYS_TIME))
293 return -EPERM;
294
295 /* if the quartz is off by more than 10% something is VERY wrong! */
296 if (txc->modes & ADJ_TICK &&
297 (txc->tick < 900000/USER_HZ ||
298 txc->tick > 1100000/USER_HZ))
299 return -EINVAL;
300
301 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
302 hrtimer_cancel(&leap_timer);
52bfb360 303 }
4c7ee8de 304
7dffa3c6
RZ
305 getnstimeofday(&ts);
306
4c7ee8de 307 write_seqlock_irq(&xtime_lock);
4c7ee8de 308
4c7ee8de 309 /* If there are input parameters, then process them */
916c7a85
RZ
310 if (txc->modes & ADJ_ADJTIME) {
311 long save_adjust = time_adjust;
312
313 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
314 /* adjtime() is independent from ntp_adjtime() */
315 time_adjust = txc->offset;
316 ntp_update_frequency();
317 }
318 txc->offset = save_adjust;
319 goto adj_done;
320 }
ee9851b2 321 if (txc->modes) {
916c7a85
RZ
322 long sec;
323
eea83d89
RZ
324 if (txc->modes & ADJ_STATUS) {
325 if ((time_status & STA_PLL) &&
326 !(txc->status & STA_PLL)) {
327 time_state = TIME_OK;
328 time_status = STA_UNSYNC;
329 }
330 /* only set allowed bits */
331 time_status &= STA_RONLY;
332 time_status |= txc->status & ~STA_RONLY;
7dffa3c6
RZ
333
334 switch (time_state) {
335 case TIME_OK:
336 start_timer:
337 sec = ts.tv_sec;
338 if (time_status & STA_INS) {
339 time_state = TIME_INS;
340 sec += 86400 - sec % 86400;
341 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
342 } else if (time_status & STA_DEL) {
343 time_state = TIME_DEL;
344 sec += 86400 - (sec + 1) % 86400;
345 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
346 }
347 break;
348 case TIME_INS:
349 case TIME_DEL:
350 time_state = TIME_OK;
351 goto start_timer;
352 break;
353 case TIME_WAIT:
354 if (!(time_status & (STA_INS | STA_DEL)))
355 time_state = TIME_OK;
356 break;
357 case TIME_OOP:
358 hrtimer_restart(&leap_timer);
359 break;
360 }
eea83d89
RZ
361 }
362
363 if (txc->modes & ADJ_NANO)
364 time_status |= STA_NANO;
365 if (txc->modes & ADJ_MICRO)
366 time_status &= ~STA_NANO;
ee9851b2
RZ
367
368 if (txc->modes & ADJ_FREQUENCY) {
074b3b87
RZ
369 time_freq = (s64)txc->freq * PPM_SCALE;
370 time_freq = min(time_freq, MAXFREQ_SCALED);
371 time_freq = max(time_freq, -MAXFREQ_SCALED);
4c7ee8de 372 }
ee9851b2 373
eea83d89 374 if (txc->modes & ADJ_MAXERROR)
ee9851b2 375 time_maxerror = txc->maxerror;
eea83d89 376 if (txc->modes & ADJ_ESTERROR)
ee9851b2 377 time_esterror = txc->esterror;
4c7ee8de 378
ee9851b2 379 if (txc->modes & ADJ_TIMECONST) {
eea83d89
RZ
380 time_constant = txc->constant;
381 if (!(time_status & STA_NANO))
382 time_constant += 4;
383 time_constant = min(time_constant, (long)MAXTC);
384 time_constant = max(time_constant, 0l);
4c7ee8de 385 }
4c7ee8de 386
153b5d05
RZ
387 if (txc->modes & ADJ_TAI && txc->constant > 0)
388 time_tai = txc->constant;
389
916c7a85
RZ
390 if (txc->modes & ADJ_OFFSET)
391 ntp_update_offset(txc->offset);
ee9851b2
RZ
392 if (txc->modes & ADJ_TICK)
393 tick_usec = txc->tick;
394
395 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
396 ntp_update_frequency();
397 }
eea83d89 398
916c7a85
RZ
399 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
400 NTP_SCALE_SHIFT);
401 if (!(time_status & STA_NANO))
402 txc->offset /= NSEC_PER_USEC;
403
404adj_done:
eea83d89 405 result = time_state; /* mostly `TIME_OK' */
ee9851b2 406 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
4c7ee8de
JS
407 result = TIME_ERROR;
408
074b3b87
RZ
409 txc->freq = shift_right((s32)(time_freq >> PPM_SCALE_INV_SHIFT) *
410 (s64)PPM_SCALE_INV,
7fc5c784 411 NTP_SCALE_SHIFT);
4c7ee8de
JS
412 txc->maxerror = time_maxerror;
413 txc->esterror = time_esterror;
414 txc->status = time_status;
415 txc->constant = time_constant;
70bc42f9 416 txc->precision = 1;
074b3b87 417 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
4c7ee8de 418 txc->tick = tick_usec;
153b5d05 419 txc->tai = time_tai;
4c7ee8de
JS
420
421 /* PPS is not implemented, so these are zero */
422 txc->ppsfreq = 0;
423 txc->jitter = 0;
424 txc->shift = 0;
425 txc->stabil = 0;
426 txc->jitcnt = 0;
427 txc->calcnt = 0;
428 txc->errcnt = 0;
429 txc->stbcnt = 0;
430 write_sequnlock_irq(&xtime_lock);
ee9851b2 431
eea83d89
RZ
432 txc->time.tv_sec = ts.tv_sec;
433 txc->time.tv_usec = ts.tv_nsec;
434 if (!(time_status & STA_NANO))
435 txc->time.tv_usec /= NSEC_PER_USEC;
ee9851b2 436
82644459 437 notify_cmos_timer();
ee9851b2
RZ
438
439 return result;
4c7ee8de 440}
10a398d0
RZ
441
442static int __init ntp_tick_adj_setup(char *str)
443{
444 ntp_tick_adj = simple_strtol(str, NULL, 0);
445 return 1;
446}
447
448__setup("ntp_tick_adj=", ntp_tick_adj_setup);
7dffa3c6
RZ
449
450void __init ntp_init(void)
451{
452 ntp_clear();
453 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
454 leap_timer.function = ntp_leap_second;
455}