xref: /linux-6.15/kernel/time/ntp.c (revision bb6400a2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NTP state machine interfaces and logic.
4  *
5  * This code was mainly moved from kernel/timer.c and kernel/time.c
6  * Please see those files for relevant copyright info and historical
7  * changelogs.
8  */
9 #include <linux/capability.h>
10 #include <linux/clocksource.h>
11 #include <linux/workqueue.h>
12 #include <linux/hrtimer.h>
13 #include <linux/jiffies.h>
14 #include <linux/math64.h>
15 #include <linux/timex.h>
16 #include <linux/time.h>
17 #include <linux/mm.h>
18 #include <linux/module.h>
19 #include <linux/rtc.h>
20 #include <linux/audit.h>
21 
22 #include "ntp_internal.h"
23 #include "timekeeping_internal.h"
24 
25 /**
26  * struct ntp_data - Structure holding all NTP related state
27  * @tick_usec:		USER_HZ period in microseconds
28  * @tick_length:	Adjusted tick length
29  * @tick_length_base:	Base value for @tick_length
30  * @time_state:		State of the clock synchronization
31  * @time_status:	Clock status bits
32  * @time_offset:	Time adjustment in nanoseconds
33  * @time_constant:	PLL time constant
34  * @time_maxerror:	Maximum error in microseconds holding the NTP sync distance
35  *			(NTP dispersion + delay / 2)
36  * @time_esterror:	Estimated error in microseconds holding NTP dispersion
37  * @time_freq:		Frequency offset scaled nsecs/secs
38  * @time_reftime:	Time at last adjustment in seconds
39  * @time_adjust:	Adjustment value
40  * @ntp_tick_adj:	Constant boot-param configurable NTP tick adjustment (upscaled)
41  *
42  * Protected by the timekeeping locks.
43  */
44 struct ntp_data {
45 	unsigned long		tick_usec;
46 	u64			tick_length;
47 	u64			tick_length_base;
48 	int			time_state;
49 	int			time_status;
50 	s64			time_offset;
51 	long			time_constant;
52 	long			time_maxerror;
53 	long			time_esterror;
54 	s64			time_freq;
55 	time64_t		time_reftime;
56 	long			time_adjust;
57 	s64			ntp_tick_adj;
58 };
59 
60 static struct ntp_data tk_ntp_data = {
61 	.tick_usec		= USER_TICK_USEC,
62 	.time_state		= TIME_OK,
63 	.time_status		= STA_UNSYNC,
64 	.time_constant		= 2,
65 	.time_maxerror		= NTP_PHASE_LIMIT,
66 	.time_esterror		= NTP_PHASE_LIMIT,
67 };
68 
69 #define SECS_PER_DAY		86400
70 #define MAX_TICKADJ		500LL		/* usecs */
71 #define MAX_TICKADJ_SCALED \
72 	(((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
73 #define MAX_TAI_OFFSET		100000
74 
75 /* second value of the next pending leapsecond, or TIME64_MAX if no leap */
76 static time64_t			ntp_next_leap_sec = TIME64_MAX;
77 
78 #ifdef CONFIG_NTP_PPS
79 
80 /*
81  * The following variables are used when a pulse-per-second (PPS) signal
82  * is available. They establish the engineering parameters of the clock
83  * discipline loop when controlled by the PPS signal.
84  */
85 #define PPS_VALID	10	/* PPS signal watchdog max (s) */
86 #define PPS_POPCORN	4	/* popcorn spike threshold (shift) */
87 #define PPS_INTMIN	2	/* min freq interval (s) (shift) */
88 #define PPS_INTMAX	8	/* max freq interval (s) (shift) */
89 #define PPS_INTCOUNT	4	/* number of consecutive good intervals to
90 				   increase pps_shift or consecutive bad
91 				   intervals to decrease it */
92 #define PPS_MAXWANDER	100000	/* max PPS freq wander (ns/s) */
93 
94 static int pps_valid;		/* signal watchdog counter */
95 static long pps_tf[3];		/* phase median filter */
96 static long pps_jitter;		/* current jitter (ns) */
97 static struct timespec64 pps_fbase; /* beginning of the last freq interval */
98 static int pps_shift;		/* current interval duration (s) (shift) */
99 static int pps_intcnt;		/* interval counter */
100 static s64 pps_freq;		/* frequency offset (scaled ns/s) */
101 static long pps_stabil;		/* current stability (scaled ns/s) */
102 
103 /*
104  * PPS signal quality monitors
105  */
106 static long pps_calcnt;		/* calibration intervals */
107 static long pps_jitcnt;		/* jitter limit exceeded */
108 static long pps_stbcnt;		/* stability limit exceeded */
109 static long pps_errcnt;		/* calibration errors */
110 
111 
112 /*
113  * PPS kernel consumer compensates the whole phase error immediately.
114  * Otherwise, reduce the offset by a fixed factor times the time constant.
115  */
116 static inline s64 ntp_offset_chunk(struct ntp_data *ntpdata, s64 offset)
117 {
118 	if (ntpdata->time_status & STA_PPSTIME && ntpdata->time_status & STA_PPSSIGNAL)
119 		return offset;
120 	else
121 		return shift_right(offset, SHIFT_PLL + ntpdata->time_constant);
122 }
123 
124 static inline void pps_reset_freq_interval(void)
125 {
126 	/* The PPS calibration interval may end surprisingly early */
127 	pps_shift = PPS_INTMIN;
128 	pps_intcnt = 0;
129 }
130 
131 /**
132  * pps_clear - Clears the PPS state variables
133  */
134 static inline void pps_clear(void)
135 {
136 	pps_reset_freq_interval();
137 	pps_tf[0] = 0;
138 	pps_tf[1] = 0;
139 	pps_tf[2] = 0;
140 	pps_fbase.tv_sec = pps_fbase.tv_nsec = 0;
141 	pps_freq = 0;
142 }
143 
144 /*
145  * Decrease pps_valid to indicate that another second has passed since the
146  * last PPS signal. When it reaches 0, indicate that PPS signal is missing.
147  */
148 static inline void pps_dec_valid(struct ntp_data *ntpdata)
149 {
150 	if (pps_valid > 0)
151 		pps_valid--;
152 	else {
153 		ntpdata->time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
154 					  STA_PPSWANDER | STA_PPSERROR);
155 		pps_clear();
156 	}
157 }
158 
159 static inline void pps_set_freq(s64 freq)
160 {
161 	pps_freq = freq;
162 }
163 
164 static inline bool is_error_status(int status)
165 {
166 	return (status & (STA_UNSYNC|STA_CLOCKERR))
167 		/*
168 		 * PPS signal lost when either PPS time or PPS frequency
169 		 * synchronization requested
170 		 */
171 		|| ((status & (STA_PPSFREQ|STA_PPSTIME))
172 			&& !(status & STA_PPSSIGNAL))
173 		/*
174 		 * PPS jitter exceeded when PPS time synchronization
175 		 * requested
176 		 */
177 		|| ((status & (STA_PPSTIME|STA_PPSJITTER))
178 			== (STA_PPSTIME|STA_PPSJITTER))
179 		/*
180 		 * PPS wander exceeded or calibration error when PPS
181 		 * frequency synchronization requested
182 		 */
183 		|| ((status & STA_PPSFREQ)
184 			&& (status & (STA_PPSWANDER|STA_PPSERROR)));
185 }
186 
187 static inline void pps_fill_timex(struct ntp_data *ntpdata, struct __kernel_timex *txc)
188 {
189 	txc->ppsfreq	   = shift_right((pps_freq >> PPM_SCALE_INV_SHIFT) *
190 					 PPM_SCALE_INV, NTP_SCALE_SHIFT);
191 	txc->jitter	   = pps_jitter;
192 	if (!(ntpdata->time_status & STA_NANO))
193 		txc->jitter = pps_jitter / NSEC_PER_USEC;
194 	txc->shift	   = pps_shift;
195 	txc->stabil	   = pps_stabil;
196 	txc->jitcnt	   = pps_jitcnt;
197 	txc->calcnt	   = pps_calcnt;
198 	txc->errcnt	   = pps_errcnt;
199 	txc->stbcnt	   = pps_stbcnt;
200 }
201 
202 #else /* !CONFIG_NTP_PPS */
203 
204 static inline s64 ntp_offset_chunk(struct ntp_data *ntpdata, s64 offset)
205 {
206 	return shift_right(offset, SHIFT_PLL + ntpdata->time_constant);
207 }
208 
209 static inline void pps_reset_freq_interval(void) {}
210 static inline void pps_clear(void) {}
211 static inline void pps_dec_valid(struct ntp_data *ntpdata) {}
212 static inline void pps_set_freq(s64 freq) {}
213 
214 static inline bool is_error_status(int status)
215 {
216 	return status & (STA_UNSYNC|STA_CLOCKERR);
217 }
218 
219 static inline void pps_fill_timex(struct ntp_data *ntpdata, struct __kernel_timex *txc)
220 {
221 	/* PPS is not implemented, so these are zero */
222 	txc->ppsfreq	   = 0;
223 	txc->jitter	   = 0;
224 	txc->shift	   = 0;
225 	txc->stabil	   = 0;
226 	txc->jitcnt	   = 0;
227 	txc->calcnt	   = 0;
228 	txc->errcnt	   = 0;
229 	txc->stbcnt	   = 0;
230 }
231 
232 #endif /* CONFIG_NTP_PPS */
233 
234 /*
235  * Update tick_length and tick_length_base, based on tick_usec, ntp_tick_adj and
236  * time_freq:
237  */
238 static void ntp_update_frequency(struct ntp_data *ntpdata)
239 {
240 	u64 second_length, new_base, tick_usec = (u64)ntpdata->tick_usec;
241 
242 	second_length		 = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) << NTP_SCALE_SHIFT;
243 
244 	second_length		+= ntpdata->ntp_tick_adj;
245 	second_length		+= ntpdata->time_freq;
246 
247 	new_base		 = div_u64(second_length, NTP_INTERVAL_FREQ);
248 
249 	/*
250 	 * Don't wait for the next second_overflow, apply the change to the
251 	 * tick length immediately:
252 	 */
253 	ntpdata->tick_length		+= new_base - ntpdata->tick_length_base;
254 	ntpdata->tick_length_base	 = new_base;
255 }
256 
257 static inline s64 ntp_update_offset_fll(struct ntp_data *ntpdata, s64 offset64, long secs)
258 {
259 	ntpdata->time_status &= ~STA_MODE;
260 
261 	if (secs < MINSEC)
262 		return 0;
263 
264 	if (!(ntpdata->time_status & STA_FLL) && (secs <= MAXSEC))
265 		return 0;
266 
267 	ntpdata->time_status |= STA_MODE;
268 
269 	return div64_long(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
270 }
271 
272 static void ntp_update_offset(struct ntp_data *ntpdata, long offset)
273 {
274 	s64 freq_adj, offset64;
275 	long secs, real_secs;
276 
277 	if (!(ntpdata->time_status & STA_PLL))
278 		return;
279 
280 	if (!(ntpdata->time_status & STA_NANO)) {
281 		/* Make sure the multiplication below won't overflow */
282 		offset = clamp(offset, -USEC_PER_SEC, USEC_PER_SEC);
283 		offset *= NSEC_PER_USEC;
284 	}
285 
286 	/* Scale the phase adjustment and clamp to the operating range. */
287 	offset = clamp(offset, -MAXPHASE, MAXPHASE);
288 
289 	/*
290 	 * Select how the frequency is to be controlled
291 	 * and in which mode (PLL or FLL).
292 	 */
293 	real_secs = __ktime_get_real_seconds();
294 	secs = (long)(real_secs - ntpdata->time_reftime);
295 	if (unlikely(ntpdata->time_status & STA_FREQHOLD))
296 		secs = 0;
297 
298 	ntpdata->time_reftime = real_secs;
299 
300 	offset64    = offset;
301 	freq_adj    = ntp_update_offset_fll(ntpdata, offset64, secs);
302 
303 	/*
304 	 * Clamp update interval to reduce PLL gain with low
305 	 * sampling rate (e.g. intermittent network connection)
306 	 * to avoid instability.
307 	 */
308 	if (unlikely(secs > 1 << (SHIFT_PLL + 1 + ntpdata->time_constant)))
309 		secs = 1 << (SHIFT_PLL + 1 + ntpdata->time_constant);
310 
311 	freq_adj    += (offset64 * secs) <<
312 			(NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + ntpdata->time_constant));
313 
314 	freq_adj    = min(freq_adj + ntpdata->time_freq, MAXFREQ_SCALED);
315 
316 	ntpdata->time_freq   = max(freq_adj, -MAXFREQ_SCALED);
317 
318 	ntpdata->time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
319 }
320 
321 static void __ntp_clear(struct ntp_data *ntpdata)
322 {
323 	/* Stop active adjtime() */
324 	ntpdata->time_adjust	= 0;
325 	ntpdata->time_status	|= STA_UNSYNC;
326 	ntpdata->time_maxerror	= NTP_PHASE_LIMIT;
327 	ntpdata->time_esterror	= NTP_PHASE_LIMIT;
328 
329 	ntp_update_frequency(ntpdata);
330 
331 	ntpdata->tick_length	= ntpdata->tick_length_base;
332 	ntpdata->time_offset	= 0;
333 
334 	ntp_next_leap_sec = TIME64_MAX;
335 	/* Clear PPS state variables */
336 	pps_clear();
337 }
338 
339 /**
340  * ntp_clear - Clears the NTP state variables
341  */
342 void ntp_clear(void)
343 {
344 	__ntp_clear(&tk_ntp_data);
345 }
346 
347 
348 u64 ntp_tick_length(void)
349 {
350 	return tk_ntp_data.tick_length;
351 }
352 
353 /**
354  * ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime_t
355  *
356  * Provides the time of the next leapsecond against CLOCK_REALTIME in
357  * a ktime_t format. Returns KTIME_MAX if no leapsecond is pending.
358  */
359 ktime_t ntp_get_next_leap(void)
360 {
361 	struct ntp_data *ntpdata = &tk_ntp_data;
362 	ktime_t ret;
363 
364 	if ((ntpdata->time_state == TIME_INS) && (ntpdata->time_status & STA_INS))
365 		return ktime_set(ntp_next_leap_sec, 0);
366 	ret = KTIME_MAX;
367 	return ret;
368 }
369 
370 /*
371  * This routine handles the overflow of the microsecond field
372  *
373  * The tricky bits of code to handle the accurate clock support
374  * were provided by Dave Mills ([email protected]) of NTP fame.
375  * They were originally developed for SUN and DEC kernels.
376  * All the kudos should go to Dave for this stuff.
377  *
378  * Also handles leap second processing, and returns leap offset
379  */
380 int second_overflow(time64_t secs)
381 {
382 	struct ntp_data *ntpdata = &tk_ntp_data;
383 	s64 delta;
384 	int leap = 0;
385 	s32 rem;
386 
387 	/*
388 	 * Leap second processing. If in leap-insert state at the end of the
389 	 * day, the system clock is set back one second; if in leap-delete
390 	 * state, the system clock is set ahead one second.
391 	 */
392 	switch (ntpdata->time_state) {
393 	case TIME_OK:
394 		if (ntpdata->time_status & STA_INS) {
395 			ntpdata->time_state = TIME_INS;
396 			div_s64_rem(secs, SECS_PER_DAY, &rem);
397 			ntp_next_leap_sec = secs + SECS_PER_DAY - rem;
398 		} else if (ntpdata->time_status & STA_DEL) {
399 			ntpdata->time_state = TIME_DEL;
400 			div_s64_rem(secs + 1, SECS_PER_DAY, &rem);
401 			ntp_next_leap_sec = secs + SECS_PER_DAY - rem;
402 		}
403 		break;
404 	case TIME_INS:
405 		if (!(ntpdata->time_status & STA_INS)) {
406 			ntp_next_leap_sec = TIME64_MAX;
407 			ntpdata->time_state = TIME_OK;
408 		} else if (secs == ntp_next_leap_sec) {
409 			leap = -1;
410 			ntpdata->time_state = TIME_OOP;
411 			pr_notice("Clock: inserting leap second 23:59:60 UTC\n");
412 		}
413 		break;
414 	case TIME_DEL:
415 		if (!(ntpdata->time_status & STA_DEL)) {
416 			ntp_next_leap_sec = TIME64_MAX;
417 			ntpdata->time_state = TIME_OK;
418 		} else if (secs == ntp_next_leap_sec) {
419 			leap = 1;
420 			ntp_next_leap_sec = TIME64_MAX;
421 			ntpdata->time_state = TIME_WAIT;
422 			pr_notice("Clock: deleting leap second 23:59:59 UTC\n");
423 		}
424 		break;
425 	case TIME_OOP:
426 		ntp_next_leap_sec = TIME64_MAX;
427 		ntpdata->time_state = TIME_WAIT;
428 		break;
429 	case TIME_WAIT:
430 		if (!(ntpdata->time_status & (STA_INS | STA_DEL)))
431 			ntpdata->time_state = TIME_OK;
432 		break;
433 	}
434 
435 	/* Bump the maxerror field */
436 	ntpdata->time_maxerror += MAXFREQ / NSEC_PER_USEC;
437 	if (ntpdata->time_maxerror > NTP_PHASE_LIMIT) {
438 		ntpdata->time_maxerror = NTP_PHASE_LIMIT;
439 		ntpdata->time_status |= STA_UNSYNC;
440 	}
441 
442 	/* Compute the phase adjustment for the next second */
443 	ntpdata->tick_length	 = ntpdata->tick_length_base;
444 
445 	delta			 = ntp_offset_chunk(ntpdata, ntpdata->time_offset);
446 	ntpdata->time_offset	-= delta;
447 	ntpdata->tick_length	+= delta;
448 
449 	/* Check PPS signal */
450 	pps_dec_valid(ntpdata);
451 
452 	if (!ntpdata->time_adjust)
453 		goto out;
454 
455 	if (ntpdata->time_adjust > MAX_TICKADJ) {
456 		ntpdata->time_adjust -= MAX_TICKADJ;
457 		ntpdata->tick_length += MAX_TICKADJ_SCALED;
458 		goto out;
459 	}
460 
461 	if (ntpdata->time_adjust < -MAX_TICKADJ) {
462 		ntpdata->time_adjust += MAX_TICKADJ;
463 		ntpdata->tick_length -= MAX_TICKADJ_SCALED;
464 		goto out;
465 	}
466 
467 	ntpdata->tick_length += (s64)(ntpdata->time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
468 				<< NTP_SCALE_SHIFT;
469 	ntpdata->time_adjust = 0;
470 
471 out:
472 	return leap;
473 }
474 
475 #if defined(CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC)
476 static void sync_hw_clock(struct work_struct *work);
477 static DECLARE_WORK(sync_work, sync_hw_clock);
478 static struct hrtimer sync_hrtimer;
479 #define SYNC_PERIOD_NS (11ULL * 60 * NSEC_PER_SEC)
480 
481 static enum hrtimer_restart sync_timer_callback(struct hrtimer *timer)
482 {
483 	queue_work(system_freezable_power_efficient_wq, &sync_work);
484 
485 	return HRTIMER_NORESTART;
486 }
487 
488 static void sched_sync_hw_clock(unsigned long offset_nsec, bool retry)
489 {
490 	ktime_t exp = ktime_set(ktime_get_real_seconds(), 0);
491 
492 	if (retry)
493 		exp = ktime_add_ns(exp, 2ULL * NSEC_PER_SEC - offset_nsec);
494 	else
495 		exp = ktime_add_ns(exp, SYNC_PERIOD_NS - offset_nsec);
496 
497 	hrtimer_start(&sync_hrtimer, exp, HRTIMER_MODE_ABS);
498 }
499 
500 /*
501  * Check whether @now is correct versus the required time to update the RTC
502  * and calculate the value which needs to be written to the RTC so that the
503  * next seconds increment of the RTC after the write is aligned with the next
504  * seconds increment of clock REALTIME.
505  *
506  * tsched     t1 write(t2.tv_sec - 1sec))	t2 RTC increments seconds
507  *
508  * t2.tv_nsec == 0
509  * tsched = t2 - set_offset_nsec
510  * newval = t2 - NSEC_PER_SEC
511  *
512  * ==> neval = tsched + set_offset_nsec - NSEC_PER_SEC
513  *
514  * As the execution of this code is not guaranteed to happen exactly at
515  * tsched this allows it to happen within a fuzzy region:
516  *
517  *	abs(now - tsched) < FUZZ
518  *
519  * If @now is not inside the allowed window the function returns false.
520  */
521 static inline bool rtc_tv_nsec_ok(unsigned long set_offset_nsec,
522 				  struct timespec64 *to_set,
523 				  const struct timespec64 *now)
524 {
525 	/* Allowed error in tv_nsec, arbitrarily set to 5 jiffies in ns. */
526 	const unsigned long TIME_SET_NSEC_FUZZ = TICK_NSEC * 5;
527 	struct timespec64 delay = {.tv_sec = -1,
528 				   .tv_nsec = set_offset_nsec};
529 
530 	*to_set = timespec64_add(*now, delay);
531 
532 	if (to_set->tv_nsec < TIME_SET_NSEC_FUZZ) {
533 		to_set->tv_nsec = 0;
534 		return true;
535 	}
536 
537 	if (to_set->tv_nsec > NSEC_PER_SEC - TIME_SET_NSEC_FUZZ) {
538 		to_set->tv_sec++;
539 		to_set->tv_nsec = 0;
540 		return true;
541 	}
542 	return false;
543 }
544 
545 #ifdef CONFIG_GENERIC_CMOS_UPDATE
546 int __weak update_persistent_clock64(struct timespec64 now64)
547 {
548 	return -ENODEV;
549 }
550 #else
551 static inline int update_persistent_clock64(struct timespec64 now64)
552 {
553 	return -ENODEV;
554 }
555 #endif
556 
557 #ifdef CONFIG_RTC_SYSTOHC
558 /* Save NTP synchronized time to the RTC */
559 static int update_rtc(struct timespec64 *to_set, unsigned long *offset_nsec)
560 {
561 	struct rtc_device *rtc;
562 	struct rtc_time tm;
563 	int err = -ENODEV;
564 
565 	rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE);
566 	if (!rtc)
567 		return -ENODEV;
568 
569 	if (!rtc->ops || !rtc->ops->set_time)
570 		goto out_close;
571 
572 	/* First call might not have the correct offset */
573 	if (*offset_nsec == rtc->set_offset_nsec) {
574 		rtc_time64_to_tm(to_set->tv_sec, &tm);
575 		err = rtc_set_time(rtc, &tm);
576 	} else {
577 		/* Store the update offset and let the caller try again */
578 		*offset_nsec = rtc->set_offset_nsec;
579 		err = -EAGAIN;
580 	}
581 out_close:
582 	rtc_class_close(rtc);
583 	return err;
584 }
585 #else
586 static inline int update_rtc(struct timespec64 *to_set, unsigned long *offset_nsec)
587 {
588 	return -ENODEV;
589 }
590 #endif
591 
592 /**
593  * ntp_synced - Tells whether the NTP status is not UNSYNC
594  * Returns:	true if not UNSYNC, false otherwise
595  */
596 static inline bool ntp_synced(void)
597 {
598 	return !(tk_ntp_data.time_status & STA_UNSYNC);
599 }
600 
601 /*
602  * If we have an externally synchronized Linux clock, then update RTC clock
603  * accordingly every ~11 minutes. Generally RTCs can only store second
604  * precision, but many RTCs will adjust the phase of their second tick to
605  * match the moment of update. This infrastructure arranges to call to the RTC
606  * set at the correct moment to phase synchronize the RTC second tick over
607  * with the kernel clock.
608  */
609 static void sync_hw_clock(struct work_struct *work)
610 {
611 	/*
612 	 * The default synchronization offset is 500ms for the deprecated
613 	 * update_persistent_clock64() under the assumption that it uses
614 	 * the infamous CMOS clock (MC146818).
615 	 */
616 	static unsigned long offset_nsec = NSEC_PER_SEC / 2;
617 	struct timespec64 now, to_set;
618 	int res = -EAGAIN;
619 
620 	/*
621 	 * Don't update if STA_UNSYNC is set and if ntp_notify_cmos_timer()
622 	 * managed to schedule the work between the timer firing and the
623 	 * work being able to rearm the timer. Wait for the timer to expire.
624 	 */
625 	if (!ntp_synced() || hrtimer_is_queued(&sync_hrtimer))
626 		return;
627 
628 	ktime_get_real_ts64(&now);
629 	/* If @now is not in the allowed window, try again */
630 	if (!rtc_tv_nsec_ok(offset_nsec, &to_set, &now))
631 		goto rearm;
632 
633 	/* Take timezone adjusted RTCs into account */
634 	if (persistent_clock_is_local)
635 		to_set.tv_sec -= (sys_tz.tz_minuteswest * 60);
636 
637 	/* Try the legacy RTC first. */
638 	res = update_persistent_clock64(to_set);
639 	if (res != -ENODEV)
640 		goto rearm;
641 
642 	/* Try the RTC class */
643 	res = update_rtc(&to_set, &offset_nsec);
644 	if (res == -ENODEV)
645 		return;
646 rearm:
647 	sched_sync_hw_clock(offset_nsec, res != 0);
648 }
649 
650 void ntp_notify_cmos_timer(bool offset_set)
651 {
652 	/*
653 	 * If the time jumped (using ADJ_SETOFFSET) cancels sync timer,
654 	 * which may have been running if the time was synchronized
655 	 * prior to the ADJ_SETOFFSET call.
656 	 */
657 	if (offset_set)
658 		hrtimer_cancel(&sync_hrtimer);
659 
660 	/*
661 	 * When the work is currently executed but has not yet the timer
662 	 * rearmed this queues the work immediately again. No big issue,
663 	 * just a pointless work scheduled.
664 	 */
665 	if (ntp_synced() && !hrtimer_is_queued(&sync_hrtimer))
666 		queue_work(system_freezable_power_efficient_wq, &sync_work);
667 }
668 
669 static void __init ntp_init_cmos_sync(void)
670 {
671 	hrtimer_init(&sync_hrtimer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
672 	sync_hrtimer.function = sync_timer_callback;
673 }
674 #else /* CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC) */
675 static inline void __init ntp_init_cmos_sync(void) { }
676 #endif /* !CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC) */
677 
678 /*
679  * Propagate a new txc->status value into the NTP state:
680  */
681 static inline void process_adj_status(struct ntp_data *ntpdata, const struct __kernel_timex *txc)
682 {
683 	if ((ntpdata->time_status & STA_PLL) && !(txc->status & STA_PLL)) {
684 		ntpdata->time_state = TIME_OK;
685 		ntpdata->time_status = STA_UNSYNC;
686 		ntp_next_leap_sec = TIME64_MAX;
687 		/* Restart PPS frequency calibration */
688 		pps_reset_freq_interval();
689 	}
690 
691 	/*
692 	 * If we turn on PLL adjustments then reset the
693 	 * reference time to current time.
694 	 */
695 	if (!(ntpdata->time_status & STA_PLL) && (txc->status & STA_PLL))
696 		ntpdata->time_reftime = __ktime_get_real_seconds();
697 
698 	/* only set allowed bits */
699 	ntpdata->time_status &= STA_RONLY;
700 	ntpdata->time_status |= txc->status & ~STA_RONLY;
701 }
702 
703 static inline void process_adjtimex_modes(struct ntp_data *ntpdata, const struct __kernel_timex *txc,
704 					  s32 *time_tai)
705 {
706 	if (txc->modes & ADJ_STATUS)
707 		process_adj_status(ntpdata, txc);
708 
709 	if (txc->modes & ADJ_NANO)
710 		ntpdata->time_status |= STA_NANO;
711 
712 	if (txc->modes & ADJ_MICRO)
713 		ntpdata->time_status &= ~STA_NANO;
714 
715 	if (txc->modes & ADJ_FREQUENCY) {
716 		ntpdata->time_freq = txc->freq * PPM_SCALE;
717 		ntpdata->time_freq = min(ntpdata->time_freq, MAXFREQ_SCALED);
718 		ntpdata->time_freq = max(ntpdata->time_freq, -MAXFREQ_SCALED);
719 		/* Update pps_freq */
720 		pps_set_freq(ntpdata->time_freq);
721 	}
722 
723 	if (txc->modes & ADJ_MAXERROR)
724 		ntpdata->time_maxerror = clamp(txc->maxerror, 0, NTP_PHASE_LIMIT);
725 
726 	if (txc->modes & ADJ_ESTERROR)
727 		ntpdata->time_esterror = clamp(txc->esterror, 0, NTP_PHASE_LIMIT);
728 
729 	if (txc->modes & ADJ_TIMECONST) {
730 		ntpdata->time_constant = clamp(txc->constant, 0, MAXTC);
731 		if (!(ntpdata->time_status & STA_NANO))
732 			ntpdata->time_constant += 4;
733 		ntpdata->time_constant = clamp(ntpdata->time_constant, 0, MAXTC);
734 	}
735 
736 	if (txc->modes & ADJ_TAI && txc->constant >= 0 && txc->constant <= MAX_TAI_OFFSET)
737 		*time_tai = txc->constant;
738 
739 	if (txc->modes & ADJ_OFFSET)
740 		ntp_update_offset(ntpdata, txc->offset);
741 
742 	if (txc->modes & ADJ_TICK)
743 		ntpdata->tick_usec = txc->tick;
744 
745 	if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
746 		ntp_update_frequency(ntpdata);
747 }
748 
749 /*
750  * adjtimex() mainly allows reading (and writing, if superuser) of
751  * kernel time-keeping variables. used by xntpd.
752  */
753 int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts,
754 		  s32 *time_tai, struct audit_ntp_data *ad)
755 {
756 	struct ntp_data *ntpdata = &tk_ntp_data;
757 	int result;
758 
759 	if (txc->modes & ADJ_ADJTIME) {
760 		long save_adjust = ntpdata->time_adjust;
761 
762 		if (!(txc->modes & ADJ_OFFSET_READONLY)) {
763 			/* adjtime() is independent from ntp_adjtime() */
764 			ntpdata->time_adjust = txc->offset;
765 			ntp_update_frequency(ntpdata);
766 
767 			audit_ntp_set_old(ad, AUDIT_NTP_ADJUST,	save_adjust);
768 			audit_ntp_set_new(ad, AUDIT_NTP_ADJUST,	ntpdata->time_adjust);
769 		}
770 		txc->offset = save_adjust;
771 	} else {
772 		/* If there are input parameters, then process them: */
773 		if (txc->modes) {
774 			audit_ntp_set_old(ad, AUDIT_NTP_OFFSET,	ntpdata->time_offset);
775 			audit_ntp_set_old(ad, AUDIT_NTP_FREQ,	ntpdata->time_freq);
776 			audit_ntp_set_old(ad, AUDIT_NTP_STATUS,	ntpdata->time_status);
777 			audit_ntp_set_old(ad, AUDIT_NTP_TAI,	*time_tai);
778 			audit_ntp_set_old(ad, AUDIT_NTP_TICK,	ntpdata->tick_usec);
779 
780 			process_adjtimex_modes(ntpdata, txc, time_tai);
781 
782 			audit_ntp_set_new(ad, AUDIT_NTP_OFFSET,	ntpdata->time_offset);
783 			audit_ntp_set_new(ad, AUDIT_NTP_FREQ,	ntpdata->time_freq);
784 			audit_ntp_set_new(ad, AUDIT_NTP_STATUS,	ntpdata->time_status);
785 			audit_ntp_set_new(ad, AUDIT_NTP_TAI,	*time_tai);
786 			audit_ntp_set_new(ad, AUDIT_NTP_TICK,	ntpdata->tick_usec);
787 		}
788 
789 		txc->offset = shift_right(ntpdata->time_offset * NTP_INTERVAL_FREQ, NTP_SCALE_SHIFT);
790 		if (!(ntpdata->time_status & STA_NANO))
791 			txc->offset = (u32)txc->offset / NSEC_PER_USEC;
792 	}
793 
794 	result = ntpdata->time_state;
795 	if (is_error_status(ntpdata->time_status))
796 		result = TIME_ERROR;
797 
798 	txc->freq	   = shift_right((ntpdata->time_freq >> PPM_SCALE_INV_SHIFT) *
799 					 PPM_SCALE_INV, NTP_SCALE_SHIFT);
800 	txc->maxerror	   = ntpdata->time_maxerror;
801 	txc->esterror	   = ntpdata->time_esterror;
802 	txc->status	   = ntpdata->time_status;
803 	txc->constant	   = ntpdata->time_constant;
804 	txc->precision	   = 1;
805 	txc->tolerance	   = MAXFREQ_SCALED / PPM_SCALE;
806 	txc->tick	   = ntpdata->tick_usec;
807 	txc->tai	   = *time_tai;
808 
809 	/* Fill PPS status fields */
810 	pps_fill_timex(ntpdata, txc);
811 
812 	txc->time.tv_sec = ts->tv_sec;
813 	txc->time.tv_usec = ts->tv_nsec;
814 	if (!(ntpdata->time_status & STA_NANO))
815 		txc->time.tv_usec = ts->tv_nsec / NSEC_PER_USEC;
816 
817 	/* Handle leapsec adjustments */
818 	if (unlikely(ts->tv_sec >= ntp_next_leap_sec)) {
819 		if ((ntpdata->time_state == TIME_INS) && (ntpdata->time_status & STA_INS)) {
820 			result = TIME_OOP;
821 			txc->tai++;
822 			txc->time.tv_sec--;
823 		}
824 		if ((ntpdata->time_state == TIME_DEL) && (ntpdata->time_status & STA_DEL)) {
825 			result = TIME_WAIT;
826 			txc->tai--;
827 			txc->time.tv_sec++;
828 		}
829 		if ((ntpdata->time_state == TIME_OOP) && (ts->tv_sec == ntp_next_leap_sec))
830 			result = TIME_WAIT;
831 	}
832 
833 	return result;
834 }
835 
836 #ifdef	CONFIG_NTP_PPS
837 
838 /*
839  * struct pps_normtime is basically a struct timespec, but it is
840  * semantically different (and it is the reason why it was invented):
841  * pps_normtime.nsec has a range of ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ]
842  * while timespec.tv_nsec has a range of [0, NSEC_PER_SEC)
843  */
844 struct pps_normtime {
845 	s64		sec;	/* seconds */
846 	long		nsec;	/* nanoseconds */
847 };
848 
849 /*
850  * Normalize the timestamp so that nsec is in the
851  * [ -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] interval
852  */
853 static inline struct pps_normtime pps_normalize_ts(struct timespec64 ts)
854 {
855 	struct pps_normtime norm = {
856 		.sec = ts.tv_sec,
857 		.nsec = ts.tv_nsec
858 	};
859 
860 	if (norm.nsec > (NSEC_PER_SEC >> 1)) {
861 		norm.nsec -= NSEC_PER_SEC;
862 		norm.sec++;
863 	}
864 
865 	return norm;
866 }
867 
868 /* Get current phase correction and jitter */
869 static inline long pps_phase_filter_get(long *jitter)
870 {
871 	*jitter = pps_tf[0] - pps_tf[1];
872 	if (*jitter < 0)
873 		*jitter = -*jitter;
874 
875 	/* TODO: test various filters */
876 	return pps_tf[0];
877 }
878 
879 /* Add the sample to the phase filter */
880 static inline void pps_phase_filter_add(long err)
881 {
882 	pps_tf[2] = pps_tf[1];
883 	pps_tf[1] = pps_tf[0];
884 	pps_tf[0] = err;
885 }
886 
887 /*
888  * Decrease frequency calibration interval length. It is halved after four
889  * consecutive unstable intervals.
890  */
891 static inline void pps_dec_freq_interval(void)
892 {
893 	if (--pps_intcnt <= -PPS_INTCOUNT) {
894 		pps_intcnt = -PPS_INTCOUNT;
895 		if (pps_shift > PPS_INTMIN) {
896 			pps_shift--;
897 			pps_intcnt = 0;
898 		}
899 	}
900 }
901 
902 /*
903  * Increase frequency calibration interval length. It is doubled after
904  * four consecutive stable intervals.
905  */
906 static inline void pps_inc_freq_interval(void)
907 {
908 	if (++pps_intcnt >= PPS_INTCOUNT) {
909 		pps_intcnt = PPS_INTCOUNT;
910 		if (pps_shift < PPS_INTMAX) {
911 			pps_shift++;
912 			pps_intcnt = 0;
913 		}
914 	}
915 }
916 
917 /*
918  * Update clock frequency based on MONOTONIC_RAW clock PPS signal
919  * timestamps
920  *
921  * At the end of the calibration interval the difference between the
922  * first and last MONOTONIC_RAW clock timestamps divided by the length
923  * of the interval becomes the frequency update. If the interval was
924  * too long, the data are discarded.
925  * Returns the difference between old and new frequency values.
926  */
927 static long hardpps_update_freq(struct ntp_data *ntpdata, struct pps_normtime freq_norm)
928 {
929 	long delta, delta_mod;
930 	s64 ftemp;
931 
932 	/* Check if the frequency interval was too long */
933 	if (freq_norm.sec > (2 << pps_shift)) {
934 		ntpdata->time_status |= STA_PPSERROR;
935 		pps_errcnt++;
936 		pps_dec_freq_interval();
937 		printk_deferred(KERN_ERR "hardpps: PPSERROR: interval too long - %lld s\n",
938 				freq_norm.sec);
939 		return 0;
940 	}
941 
942 	/*
943 	 * Here the raw frequency offset and wander (stability) is
944 	 * calculated. If the wander is less than the wander threshold the
945 	 * interval is increased; otherwise it is decreased.
946 	 */
947 	ftemp = div_s64(((s64)(-freq_norm.nsec)) << NTP_SCALE_SHIFT,
948 			freq_norm.sec);
949 	delta = shift_right(ftemp - pps_freq, NTP_SCALE_SHIFT);
950 	pps_freq = ftemp;
951 	if (delta > PPS_MAXWANDER || delta < -PPS_MAXWANDER) {
952 		printk_deferred(KERN_WARNING "hardpps: PPSWANDER: change=%ld\n", delta);
953 		ntpdata->time_status |= STA_PPSWANDER;
954 		pps_stbcnt++;
955 		pps_dec_freq_interval();
956 	} else {
957 		/* Good sample */
958 		pps_inc_freq_interval();
959 	}
960 
961 	/*
962 	 * The stability metric is calculated as the average of recent
963 	 * frequency changes, but is used only for performance monitoring
964 	 */
965 	delta_mod = delta;
966 	if (delta_mod < 0)
967 		delta_mod = -delta_mod;
968 	pps_stabil += (div_s64(((s64)delta_mod) << (NTP_SCALE_SHIFT - SHIFT_USEC),
969 			       NSEC_PER_USEC) - pps_stabil) >> PPS_INTMIN;
970 
971 	/* If enabled, the system clock frequency is updated */
972 	if ((ntpdata->time_status & STA_PPSFREQ) && !(ntpdata->time_status & STA_FREQHOLD)) {
973 		ntpdata->time_freq = pps_freq;
974 		ntp_update_frequency(ntpdata);
975 	}
976 
977 	return delta;
978 }
979 
980 /* Correct REALTIME clock phase error against PPS signal */
981 static void hardpps_update_phase(struct ntp_data *ntpdata, long error)
982 {
983 	long correction = -error;
984 	long jitter;
985 
986 	/* Add the sample to the median filter */
987 	pps_phase_filter_add(correction);
988 	correction = pps_phase_filter_get(&jitter);
989 
990 	/*
991 	 * Nominal jitter is due to PPS signal noise. If it exceeds the
992 	 * threshold, the sample is discarded; otherwise, if so enabled,
993 	 * the time offset is updated.
994 	 */
995 	if (jitter > (pps_jitter << PPS_POPCORN)) {
996 		printk_deferred(KERN_WARNING "hardpps: PPSJITTER: jitter=%ld, limit=%ld\n",
997 				jitter, (pps_jitter << PPS_POPCORN));
998 		ntpdata->time_status |= STA_PPSJITTER;
999 		pps_jitcnt++;
1000 	} else if (ntpdata->time_status & STA_PPSTIME) {
1001 		/* Correct the time using the phase offset */
1002 		ntpdata->time_offset = div_s64(((s64)correction) << NTP_SCALE_SHIFT,
1003 					       NTP_INTERVAL_FREQ);
1004 		/* Cancel running adjtime() */
1005 		ntpdata->time_adjust = 0;
1006 	}
1007 	/* Update jitter */
1008 	pps_jitter += (jitter - pps_jitter) >> PPS_INTMIN;
1009 }
1010 
1011 /*
1012  * __hardpps() - discipline CPU clock oscillator to external PPS signal
1013  *
1014  * This routine is called at each PPS signal arrival in order to
1015  * discipline the CPU clock oscillator to the PPS signal. It takes two
1016  * parameters: REALTIME and MONOTONIC_RAW clock timestamps. The former
1017  * is used to correct clock phase error and the latter is used to
1018  * correct the frequency.
1019  *
1020  * This code is based on David Mills's reference nanokernel
1021  * implementation. It was mostly rewritten but keeps the same idea.
1022  */
1023 void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
1024 {
1025 	struct pps_normtime pts_norm, freq_norm;
1026 	struct ntp_data *ntpdata = &tk_ntp_data;
1027 
1028 	pts_norm = pps_normalize_ts(*phase_ts);
1029 
1030 	/* Clear the error bits, they will be set again if needed */
1031 	ntpdata->time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
1032 
1033 	/* indicate signal presence */
1034 	ntpdata->time_status |= STA_PPSSIGNAL;
1035 	pps_valid = PPS_VALID;
1036 
1037 	/*
1038 	 * When called for the first time, just start the frequency
1039 	 * interval
1040 	 */
1041 	if (unlikely(pps_fbase.tv_sec == 0)) {
1042 		pps_fbase = *raw_ts;
1043 		return;
1044 	}
1045 
1046 	/* Ok, now we have a base for frequency calculation */
1047 	freq_norm = pps_normalize_ts(timespec64_sub(*raw_ts, pps_fbase));
1048 
1049 	/*
1050 	 * Check that the signal is in the range
1051 	 * [1s - MAXFREQ us, 1s + MAXFREQ us], otherwise reject it
1052 	 */
1053 	if ((freq_norm.sec == 0) || (freq_norm.nsec > MAXFREQ * freq_norm.sec) ||
1054 	    (freq_norm.nsec < -MAXFREQ * freq_norm.sec)) {
1055 		ntpdata->time_status |= STA_PPSJITTER;
1056 		/* Restart the frequency calibration interval */
1057 		pps_fbase = *raw_ts;
1058 		printk_deferred(KERN_ERR "hardpps: PPSJITTER: bad pulse\n");
1059 		return;
1060 	}
1061 
1062 	/* Signal is ok. Check if the current frequency interval is finished */
1063 	if (freq_norm.sec >= (1 << pps_shift)) {
1064 		pps_calcnt++;
1065 		/* Restart the frequency calibration interval */
1066 		pps_fbase = *raw_ts;
1067 		hardpps_update_freq(ntpdata, freq_norm);
1068 	}
1069 
1070 	hardpps_update_phase(ntpdata, pts_norm.nsec);
1071 
1072 }
1073 #endif	/* CONFIG_NTP_PPS */
1074 
1075 static int __init ntp_tick_adj_setup(char *str)
1076 {
1077 	int rc = kstrtos64(str, 0, &tk_ntp_data.ntp_tick_adj);
1078 	if (rc)
1079 		return rc;
1080 
1081 	tk_ntp_data.ntp_tick_adj <<= NTP_SCALE_SHIFT;
1082 	return 1;
1083 }
1084 
1085 __setup("ntp_tick_adj=", ntp_tick_adj_setup);
1086 
1087 void __init ntp_init(void)
1088 {
1089 	ntp_clear();
1090 	ntp_init_cmos_sync();
1091 }
1092