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