2 * linux/kernel/time/timekeeping.c
4 * Kernel timekeeping code and accessor functions
6 * This code was moved from linux/kernel/timer.c.
7 * Please see that file for copyright and history logs.
11 #include <linux/timekeeper_internal.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/percpu.h>
15 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/syscore_ops.h>
19 #include <linux/clocksource.h>
20 #include <linux/jiffies.h>
21 #include <linux/time.h>
22 #include <linux/tick.h>
23 #include <linux/stop_machine.h>
24 #include <linux/pvclock_gtod.h>
25 #include <linux/compiler.h>
27 #include "tick-internal.h"
28 #include "ntp_internal.h"
29 #include "timekeeping_internal.h"
31 #define TK_CLEAR_NTP (1 << 0)
32 #define TK_MIRROR (1 << 1)
33 #define TK_CLOCK_WAS_SET (1 << 2)
36 * The most important data for readout fits into a single 64 byte
41 struct timekeeper timekeeper
;
42 } tk_core ____cacheline_aligned
;
44 static DEFINE_RAW_SPINLOCK(timekeeper_lock
);
45 static struct timekeeper shadow_timekeeper
;
48 * struct tk_fast - NMI safe timekeeper
49 * @seq: Sequence counter for protecting updates. The lowest bit
50 * is the index for the tk_read_base array
51 * @base: tk_read_base array. Access is indexed by the lowest bit of
54 * See @update_fast_timekeeper() below.
58 struct tk_read_base base
[2];
61 static struct tk_fast tk_fast_mono ____cacheline_aligned
;
63 /* flag for if timekeeping is suspended */
64 int __read_mostly timekeeping_suspended
;
66 /* Flag for if there is a persistent clock on this platform */
67 bool __read_mostly persistent_clock_exist
= false;
69 static inline void tk_normalize_xtime(struct timekeeper
*tk
)
71 while (tk
->tkr
.xtime_nsec
>= ((u64
)NSEC_PER_SEC
<< tk
->tkr
.shift
)) {
72 tk
->tkr
.xtime_nsec
-= (u64
)NSEC_PER_SEC
<< tk
->tkr
.shift
;
77 static inline struct timespec64
tk_xtime(struct timekeeper
*tk
)
81 ts
.tv_sec
= tk
->xtime_sec
;
82 ts
.tv_nsec
= (long)(tk
->tkr
.xtime_nsec
>> tk
->tkr
.shift
);
86 static void tk_set_xtime(struct timekeeper
*tk
, const struct timespec64
*ts
)
88 tk
->xtime_sec
= ts
->tv_sec
;
89 tk
->tkr
.xtime_nsec
= (u64
)ts
->tv_nsec
<< tk
->tkr
.shift
;
92 static void tk_xtime_add(struct timekeeper
*tk
, const struct timespec64
*ts
)
94 tk
->xtime_sec
+= ts
->tv_sec
;
95 tk
->tkr
.xtime_nsec
+= (u64
)ts
->tv_nsec
<< tk
->tkr
.shift
;
96 tk_normalize_xtime(tk
);
99 static void tk_set_wall_to_mono(struct timekeeper
*tk
, struct timespec64 wtm
)
101 struct timespec64 tmp
;
104 * Verify consistency of: offset_real = -wall_to_monotonic
105 * before modifying anything
107 set_normalized_timespec64(&tmp
, -tk
->wall_to_monotonic
.tv_sec
,
108 -tk
->wall_to_monotonic
.tv_nsec
);
109 WARN_ON_ONCE(tk
->offs_real
.tv64
!= timespec64_to_ktime(tmp
).tv64
);
110 tk
->wall_to_monotonic
= wtm
;
111 set_normalized_timespec64(&tmp
, -wtm
.tv_sec
, -wtm
.tv_nsec
);
112 tk
->offs_real
= timespec64_to_ktime(tmp
);
113 tk
->offs_tai
= ktime_add(tk
->offs_real
, ktime_set(tk
->tai_offset
, 0));
116 static inline void tk_update_sleep_time(struct timekeeper
*tk
, ktime_t delta
)
118 tk
->offs_boot
= ktime_add(tk
->offs_boot
, delta
);
122 * tk_setup_internals - Set up internals to use clocksource clock.
124 * @tk: The target timekeeper to setup.
125 * @clock: Pointer to clocksource.
127 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
128 * pair and interval request.
130 * Unless you're the timekeeping code, you should not be using this!
132 static void tk_setup_internals(struct timekeeper
*tk
, struct clocksource
*clock
)
135 u64 tmp
, ntpinterval
;
136 struct clocksource
*old_clock
;
138 old_clock
= tk
->tkr
.clock
;
139 tk
->tkr
.clock
= clock
;
140 tk
->tkr
.read
= clock
->read
;
141 tk
->tkr
.mask
= clock
->mask
;
142 tk
->tkr
.cycle_last
= tk
->tkr
.read(clock
);
144 /* Do the ns -> cycle conversion first, using original mult */
145 tmp
= NTP_INTERVAL_LENGTH
;
146 tmp
<<= clock
->shift
;
148 tmp
+= clock
->mult
/2;
149 do_div(tmp
, clock
->mult
);
153 interval
= (cycle_t
) tmp
;
154 tk
->cycle_interval
= interval
;
156 /* Go back from cycles -> shifted ns */
157 tk
->xtime_interval
= (u64
) interval
* clock
->mult
;
158 tk
->xtime_remainder
= ntpinterval
- tk
->xtime_interval
;
160 ((u64
) interval
* clock
->mult
) >> clock
->shift
;
162 /* if changing clocks, convert xtime_nsec shift units */
164 int shift_change
= clock
->shift
- old_clock
->shift
;
165 if (shift_change
< 0)
166 tk
->tkr
.xtime_nsec
>>= -shift_change
;
168 tk
->tkr
.xtime_nsec
<<= shift_change
;
170 tk
->tkr
.shift
= clock
->shift
;
173 tk
->ntp_error_shift
= NTP_SCALE_SHIFT
- clock
->shift
;
176 * The timekeeper keeps its own mult values for the currently
177 * active clocksource. These value will be adjusted via NTP
178 * to counteract clock drifting.
180 tk
->tkr
.mult
= clock
->mult
;
183 /* Timekeeper helper functions. */
185 #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
186 static u32
default_arch_gettimeoffset(void) { return 0; }
187 u32 (*arch_gettimeoffset
)(void) = default_arch_gettimeoffset
;
189 static inline u32
arch_gettimeoffset(void) { return 0; }
192 static inline s64
timekeeping_get_ns(struct tk_read_base
*tkr
)
194 cycle_t cycle_now
, delta
;
197 /* read clocksource: */
198 cycle_now
= tkr
->read(tkr
->clock
);
200 /* calculate the delta since the last update_wall_time: */
201 delta
= clocksource_delta(cycle_now
, tkr
->cycle_last
, tkr
->mask
);
203 nsec
= delta
* tkr
->mult
+ tkr
->xtime_nsec
;
206 /* If arch requires, add in get_arch_timeoffset() */
207 return nsec
+ arch_gettimeoffset();
210 static inline s64
timekeeping_get_ns_raw(struct timekeeper
*tk
)
212 struct clocksource
*clock
= tk
->tkr
.clock
;
213 cycle_t cycle_now
, delta
;
216 /* read clocksource: */
217 cycle_now
= tk
->tkr
.read(clock
);
219 /* calculate the delta since the last update_wall_time: */
220 delta
= clocksource_delta(cycle_now
, tk
->tkr
.cycle_last
, tk
->tkr
.mask
);
222 /* convert delta to nanoseconds. */
223 nsec
= clocksource_cyc2ns(delta
, clock
->mult
, clock
->shift
);
225 /* If arch requires, add in get_arch_timeoffset() */
226 return nsec
+ arch_gettimeoffset();
230 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
231 * @tk: The timekeeper from which we take the update
232 * @tkf: The fast timekeeper to update
233 * @tbase: The time base for the fast timekeeper (mono/raw)
235 * We want to use this from any context including NMI and tracing /
236 * instrumenting the timekeeping code itself.
238 * So we handle this differently than the other timekeeping accessor
239 * functions which retry when the sequence count has changed. The
242 * smp_wmb(); <- Ensure that the last base[1] update is visible
244 * smp_wmb(); <- Ensure that the seqcount update is visible
245 * update(tkf->base[0], tk);
246 * smp_wmb(); <- Ensure that the base[0] update is visible
248 * smp_wmb(); <- Ensure that the seqcount update is visible
249 * update(tkf->base[1], tk);
251 * The reader side does:
257 * now = now(tkf->base[idx]);
259 * } while (seq != tkf->seq)
261 * As long as we update base[0] readers are forced off to
262 * base[1]. Once base[0] is updated readers are redirected to base[0]
263 * and the base[1] update takes place.
265 * So if a NMI hits the update of base[0] then it will use base[1]
266 * which is still consistent. In the worst case this can result is a
267 * slightly wrong timestamp (a few nanoseconds). See
268 * @ktime_get_mono_fast_ns.
270 static void update_fast_timekeeper(struct timekeeper
*tk
)
272 struct tk_read_base
*base
= tk_fast_mono
.base
;
274 /* Force readers off to base[1] */
275 raw_write_seqcount_latch(&tk_fast_mono
.seq
);
278 memcpy(base
, &tk
->tkr
, sizeof(*base
));
280 /* Force readers back to base[0] */
281 raw_write_seqcount_latch(&tk_fast_mono
.seq
);
284 memcpy(base
+ 1, base
, sizeof(*base
));
288 * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
290 * This timestamp is not guaranteed to be monotonic across an update.
291 * The timestamp is calculated by:
293 * now = base_mono + clock_delta * slope
295 * So if the update lowers the slope, readers who are forced to the
296 * not yet updated second array are still using the old steeper slope.
305 * |12345678---> reader order
311 * So reader 6 will observe time going backwards versus reader 5.
313 * While other CPUs are likely to be able observe that, the only way
314 * for a CPU local observation is when an NMI hits in the middle of
315 * the update. Timestamps taken from that NMI context might be ahead
316 * of the following timestamps. Callers need to be aware of that and
319 u64 notrace
ktime_get_mono_fast_ns(void)
321 struct tk_read_base
*tkr
;
326 seq
= raw_read_seqcount(&tk_fast_mono
.seq
);
327 tkr
= tk_fast_mono
.base
+ (seq
& 0x01);
328 now
= ktime_to_ns(tkr
->base_mono
) + timekeeping_get_ns(tkr
);
330 } while (read_seqcount_retry(&tk_fast_mono
.seq
, seq
));
333 EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns
);
335 #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
337 static inline void update_vsyscall(struct timekeeper
*tk
)
341 xt
= timespec64_to_timespec(tk_xtime(tk
));
342 update_vsyscall_old(&xt
, &tk
->wall_to_monotonic
, tk
->tkr
.clock
, tk
->tkr
.mult
,
346 static inline void old_vsyscall_fixup(struct timekeeper
*tk
)
351 * Store only full nanoseconds into xtime_nsec after rounding
352 * it up and add the remainder to the error difference.
353 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
354 * by truncating the remainder in vsyscalls. However, it causes
355 * additional work to be done in timekeeping_adjust(). Once
356 * the vsyscall implementations are converted to use xtime_nsec
357 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
358 * users are removed, this can be killed.
360 remainder
= tk
->tkr
.xtime_nsec
& ((1ULL << tk
->tkr
.shift
) - 1);
361 tk
->tkr
.xtime_nsec
-= remainder
;
362 tk
->tkr
.xtime_nsec
+= 1ULL << tk
->tkr
.shift
;
363 tk
->ntp_error
+= remainder
<< tk
->ntp_error_shift
;
364 tk
->ntp_error
-= (1ULL << tk
->tkr
.shift
) << tk
->ntp_error_shift
;
367 #define old_vsyscall_fixup(tk)
370 static RAW_NOTIFIER_HEAD(pvclock_gtod_chain
);
372 static void update_pvclock_gtod(struct timekeeper
*tk
, bool was_set
)
374 raw_notifier_call_chain(&pvclock_gtod_chain
, was_set
, tk
);
378 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
380 int pvclock_gtod_register_notifier(struct notifier_block
*nb
)
382 struct timekeeper
*tk
= &tk_core
.timekeeper
;
386 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
387 ret
= raw_notifier_chain_register(&pvclock_gtod_chain
, nb
);
388 update_pvclock_gtod(tk
, true);
389 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
393 EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier
);
396 * pvclock_gtod_unregister_notifier - unregister a pvclock
397 * timedata update listener
399 int pvclock_gtod_unregister_notifier(struct notifier_block
*nb
)
404 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
405 ret
= raw_notifier_chain_unregister(&pvclock_gtod_chain
, nb
);
406 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
410 EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier
);
413 * Update the ktime_t based scalar nsec members of the timekeeper
415 static inline void tk_update_ktime_data(struct timekeeper
*tk
)
420 * The xtime based monotonic readout is:
421 * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
422 * The ktime based monotonic readout is:
423 * nsec = base_mono + now();
424 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
426 nsec
= (s64
)(tk
->xtime_sec
+ tk
->wall_to_monotonic
.tv_sec
);
427 nsec
*= NSEC_PER_SEC
;
428 nsec
+= tk
->wall_to_monotonic
.tv_nsec
;
429 tk
->tkr
.base_mono
= ns_to_ktime(nsec
);
431 /* Update the monotonic raw base */
432 tk
->base_raw
= timespec64_to_ktime(tk
->raw_time
);
435 /* must hold timekeeper_lock */
436 static void timekeeping_update(struct timekeeper
*tk
, unsigned int action
)
438 if (action
& TK_CLEAR_NTP
) {
443 update_pvclock_gtod(tk
, action
& TK_CLOCK_WAS_SET
);
445 tk_update_ktime_data(tk
);
447 if (action
& TK_MIRROR
)
448 memcpy(&shadow_timekeeper
, &tk_core
.timekeeper
,
449 sizeof(tk_core
.timekeeper
));
451 update_fast_timekeeper(tk
);
455 * timekeeping_forward_now - update clock to the current time
457 * Forward the current clock to update its state since the last call to
458 * update_wall_time(). This is useful before significant clock changes,
459 * as it avoids having to deal with this time offset explicitly.
461 static void timekeeping_forward_now(struct timekeeper
*tk
)
463 struct clocksource
*clock
= tk
->tkr
.clock
;
464 cycle_t cycle_now
, delta
;
467 cycle_now
= tk
->tkr
.read(clock
);
468 delta
= clocksource_delta(cycle_now
, tk
->tkr
.cycle_last
, tk
->tkr
.mask
);
469 tk
->tkr
.cycle_last
= cycle_now
;
471 tk
->tkr
.xtime_nsec
+= delta
* tk
->tkr
.mult
;
473 /* If arch requires, add in get_arch_timeoffset() */
474 tk
->tkr
.xtime_nsec
+= (u64
)arch_gettimeoffset() << tk
->tkr
.shift
;
476 tk_normalize_xtime(tk
);
478 nsec
= clocksource_cyc2ns(delta
, clock
->mult
, clock
->shift
);
479 timespec64_add_ns(&tk
->raw_time
, nsec
);
483 * __getnstimeofday64 - Returns the time of day in a timespec64.
484 * @ts: pointer to the timespec to be set
486 * Updates the time of day in the timespec.
487 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
489 int __getnstimeofday64(struct timespec64
*ts
)
491 struct timekeeper
*tk
= &tk_core
.timekeeper
;
496 seq
= read_seqcount_begin(&tk_core
.seq
);
498 ts
->tv_sec
= tk
->xtime_sec
;
499 nsecs
= timekeeping_get_ns(&tk
->tkr
);
501 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
504 timespec64_add_ns(ts
, nsecs
);
507 * Do not bail out early, in case there were callers still using
508 * the value, even in the face of the WARN_ON.
510 if (unlikely(timekeeping_suspended
))
514 EXPORT_SYMBOL(__getnstimeofday64
);
517 * getnstimeofday64 - Returns the time of day in a timespec64.
518 * @ts: pointer to the timespec to be set
520 * Returns the time of day in a timespec (WARN if suspended).
522 void getnstimeofday64(struct timespec64
*ts
)
524 WARN_ON(__getnstimeofday64(ts
));
526 EXPORT_SYMBOL(getnstimeofday64
);
528 ktime_t
ktime_get(void)
530 struct timekeeper
*tk
= &tk_core
.timekeeper
;
535 WARN_ON(timekeeping_suspended
);
538 seq
= read_seqcount_begin(&tk_core
.seq
);
539 base
= tk
->tkr
.base_mono
;
540 nsecs
= timekeeping_get_ns(&tk
->tkr
);
542 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
544 return ktime_add_ns(base
, nsecs
);
546 EXPORT_SYMBOL_GPL(ktime_get
);
548 static ktime_t
*offsets
[TK_OFFS_MAX
] = {
549 [TK_OFFS_REAL
] = &tk_core
.timekeeper
.offs_real
,
550 [TK_OFFS_BOOT
] = &tk_core
.timekeeper
.offs_boot
,
551 [TK_OFFS_TAI
] = &tk_core
.timekeeper
.offs_tai
,
554 ktime_t
ktime_get_with_offset(enum tk_offsets offs
)
556 struct timekeeper
*tk
= &tk_core
.timekeeper
;
558 ktime_t base
, *offset
= offsets
[offs
];
561 WARN_ON(timekeeping_suspended
);
564 seq
= read_seqcount_begin(&tk_core
.seq
);
565 base
= ktime_add(tk
->tkr
.base_mono
, *offset
);
566 nsecs
= timekeeping_get_ns(&tk
->tkr
);
568 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
570 return ktime_add_ns(base
, nsecs
);
573 EXPORT_SYMBOL_GPL(ktime_get_with_offset
);
576 * ktime_mono_to_any() - convert mononotic time to any other time
577 * @tmono: time to convert.
578 * @offs: which offset to use
580 ktime_t
ktime_mono_to_any(ktime_t tmono
, enum tk_offsets offs
)
582 ktime_t
*offset
= offsets
[offs
];
587 seq
= read_seqcount_begin(&tk_core
.seq
);
588 tconv
= ktime_add(tmono
, *offset
);
589 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
593 EXPORT_SYMBOL_GPL(ktime_mono_to_any
);
596 * ktime_get_raw - Returns the raw monotonic time in ktime_t format
598 ktime_t
ktime_get_raw(void)
600 struct timekeeper
*tk
= &tk_core
.timekeeper
;
606 seq
= read_seqcount_begin(&tk_core
.seq
);
608 nsecs
= timekeeping_get_ns_raw(tk
);
610 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
612 return ktime_add_ns(base
, nsecs
);
614 EXPORT_SYMBOL_GPL(ktime_get_raw
);
617 * ktime_get_ts64 - get the monotonic clock in timespec64 format
618 * @ts: pointer to timespec variable
620 * The function calculates the monotonic clock from the realtime
621 * clock and the wall_to_monotonic offset and stores the result
622 * in normalized timespec format in the variable pointed to by @ts.
624 void ktime_get_ts64(struct timespec64
*ts
)
626 struct timekeeper
*tk
= &tk_core
.timekeeper
;
627 struct timespec64 tomono
;
631 WARN_ON(timekeeping_suspended
);
634 seq
= read_seqcount_begin(&tk_core
.seq
);
635 ts
->tv_sec
= tk
->xtime_sec
;
636 nsec
= timekeeping_get_ns(&tk
->tkr
);
637 tomono
= tk
->wall_to_monotonic
;
639 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
641 ts
->tv_sec
+= tomono
.tv_sec
;
643 timespec64_add_ns(ts
, nsec
+ tomono
.tv_nsec
);
645 EXPORT_SYMBOL_GPL(ktime_get_ts64
);
647 #ifdef CONFIG_NTP_PPS
650 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
651 * @ts_raw: pointer to the timespec to be set to raw monotonic time
652 * @ts_real: pointer to the timespec to be set to the time of day
654 * This function reads both the time of day and raw monotonic time at the
655 * same time atomically and stores the resulting timestamps in timespec
658 void getnstime_raw_and_real(struct timespec
*ts_raw
, struct timespec
*ts_real
)
660 struct timekeeper
*tk
= &tk_core
.timekeeper
;
662 s64 nsecs_raw
, nsecs_real
;
664 WARN_ON_ONCE(timekeeping_suspended
);
667 seq
= read_seqcount_begin(&tk_core
.seq
);
669 *ts_raw
= timespec64_to_timespec(tk
->raw_time
);
670 ts_real
->tv_sec
= tk
->xtime_sec
;
671 ts_real
->tv_nsec
= 0;
673 nsecs_raw
= timekeeping_get_ns_raw(tk
);
674 nsecs_real
= timekeeping_get_ns(&tk
->tkr
);
676 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
678 timespec_add_ns(ts_raw
, nsecs_raw
);
679 timespec_add_ns(ts_real
, nsecs_real
);
681 EXPORT_SYMBOL(getnstime_raw_and_real
);
683 #endif /* CONFIG_NTP_PPS */
686 * do_gettimeofday - Returns the time of day in a timeval
687 * @tv: pointer to the timeval to be set
689 * NOTE: Users should be converted to using getnstimeofday()
691 void do_gettimeofday(struct timeval
*tv
)
693 struct timespec64 now
;
695 getnstimeofday64(&now
);
696 tv
->tv_sec
= now
.tv_sec
;
697 tv
->tv_usec
= now
.tv_nsec
/1000;
699 EXPORT_SYMBOL(do_gettimeofday
);
702 * do_settimeofday - Sets the time of day
703 * @tv: pointer to the timespec variable containing the new time
705 * Sets the time of day to the new time and update NTP and notify hrtimers
707 int do_settimeofday(const struct timespec
*tv
)
709 struct timekeeper
*tk
= &tk_core
.timekeeper
;
710 struct timespec64 ts_delta
, xt
, tmp
;
713 if (!timespec_valid_strict(tv
))
716 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
717 write_seqcount_begin(&tk_core
.seq
);
719 timekeeping_forward_now(tk
);
722 ts_delta
.tv_sec
= tv
->tv_sec
- xt
.tv_sec
;
723 ts_delta
.tv_nsec
= tv
->tv_nsec
- xt
.tv_nsec
;
725 tk_set_wall_to_mono(tk
, timespec64_sub(tk
->wall_to_monotonic
, ts_delta
));
727 tmp
= timespec_to_timespec64(*tv
);
728 tk_set_xtime(tk
, &tmp
);
730 timekeeping_update(tk
, TK_CLEAR_NTP
| TK_MIRROR
| TK_CLOCK_WAS_SET
);
732 write_seqcount_end(&tk_core
.seq
);
733 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
735 /* signal hrtimers about time change */
740 EXPORT_SYMBOL(do_settimeofday
);
743 * timekeeping_inject_offset - Adds or subtracts from the current time.
744 * @tv: pointer to the timespec variable containing the offset
746 * Adds or subtracts an offset value from the current time.
748 int timekeeping_inject_offset(struct timespec
*ts
)
750 struct timekeeper
*tk
= &tk_core
.timekeeper
;
752 struct timespec64 ts64
, tmp
;
755 if ((unsigned long)ts
->tv_nsec
>= NSEC_PER_SEC
)
758 ts64
= timespec_to_timespec64(*ts
);
760 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
761 write_seqcount_begin(&tk_core
.seq
);
763 timekeeping_forward_now(tk
);
765 /* Make sure the proposed value is valid */
766 tmp
= timespec64_add(tk_xtime(tk
), ts64
);
767 if (!timespec64_valid_strict(&tmp
)) {
772 tk_xtime_add(tk
, &ts64
);
773 tk_set_wall_to_mono(tk
, timespec64_sub(tk
->wall_to_monotonic
, ts64
));
775 error
: /* even if we error out, we forwarded the time, so call update */
776 timekeeping_update(tk
, TK_CLEAR_NTP
| TK_MIRROR
| TK_CLOCK_WAS_SET
);
778 write_seqcount_end(&tk_core
.seq
);
779 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
781 /* signal hrtimers about time change */
786 EXPORT_SYMBOL(timekeeping_inject_offset
);
790 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
793 s32
timekeeping_get_tai_offset(void)
795 struct timekeeper
*tk
= &tk_core
.timekeeper
;
800 seq
= read_seqcount_begin(&tk_core
.seq
);
801 ret
= tk
->tai_offset
;
802 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
808 * __timekeeping_set_tai_offset - Lock free worker function
811 static void __timekeeping_set_tai_offset(struct timekeeper
*tk
, s32 tai_offset
)
813 tk
->tai_offset
= tai_offset
;
814 tk
->offs_tai
= ktime_add(tk
->offs_real
, ktime_set(tai_offset
, 0));
818 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
821 void timekeeping_set_tai_offset(s32 tai_offset
)
823 struct timekeeper
*tk
= &tk_core
.timekeeper
;
826 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
827 write_seqcount_begin(&tk_core
.seq
);
828 __timekeeping_set_tai_offset(tk
, tai_offset
);
829 timekeeping_update(tk
, TK_MIRROR
| TK_CLOCK_WAS_SET
);
830 write_seqcount_end(&tk_core
.seq
);
831 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
836 * change_clocksource - Swaps clocksources if a new one is available
838 * Accumulates current time interval and initializes new clocksource
840 static int change_clocksource(void *data
)
842 struct timekeeper
*tk
= &tk_core
.timekeeper
;
843 struct clocksource
*new, *old
;
846 new = (struct clocksource
*) data
;
848 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
849 write_seqcount_begin(&tk_core
.seq
);
851 timekeeping_forward_now(tk
);
853 * If the cs is in module, get a module reference. Succeeds
854 * for built-in code (owner == NULL) as well.
856 if (try_module_get(new->owner
)) {
857 if (!new->enable
|| new->enable(new) == 0) {
859 tk_setup_internals(tk
, new);
862 module_put(old
->owner
);
864 module_put(new->owner
);
867 timekeeping_update(tk
, TK_CLEAR_NTP
| TK_MIRROR
| TK_CLOCK_WAS_SET
);
869 write_seqcount_end(&tk_core
.seq
);
870 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
876 * timekeeping_notify - Install a new clock source
877 * @clock: pointer to the clock source
879 * This function is called from clocksource.c after a new, better clock
880 * source has been registered. The caller holds the clocksource_mutex.
882 int timekeeping_notify(struct clocksource
*clock
)
884 struct timekeeper
*tk
= &tk_core
.timekeeper
;
886 if (tk
->tkr
.clock
== clock
)
888 stop_machine(change_clocksource
, clock
, NULL
);
890 return tk
->tkr
.clock
== clock
? 0 : -1;
894 * getrawmonotonic - Returns the raw monotonic time in a timespec
895 * @ts: pointer to the timespec to be set
897 * Returns the raw monotonic time (completely un-modified by ntp)
899 void getrawmonotonic(struct timespec
*ts
)
901 struct timekeeper
*tk
= &tk_core
.timekeeper
;
902 struct timespec64 ts64
;
907 seq
= read_seqcount_begin(&tk_core
.seq
);
908 nsecs
= timekeeping_get_ns_raw(tk
);
911 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
913 timespec64_add_ns(&ts64
, nsecs
);
914 *ts
= timespec64_to_timespec(ts64
);
916 EXPORT_SYMBOL(getrawmonotonic
);
919 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
921 int timekeeping_valid_for_hres(void)
923 struct timekeeper
*tk
= &tk_core
.timekeeper
;
928 seq
= read_seqcount_begin(&tk_core
.seq
);
930 ret
= tk
->tkr
.clock
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
;
932 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
938 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
940 u64
timekeeping_max_deferment(void)
942 struct timekeeper
*tk
= &tk_core
.timekeeper
;
947 seq
= read_seqcount_begin(&tk_core
.seq
);
949 ret
= tk
->tkr
.clock
->max_idle_ns
;
951 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
957 * read_persistent_clock - Return time from the persistent clock.
959 * Weak dummy function for arches that do not yet support it.
960 * Reads the time from the battery backed persistent clock.
961 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
963 * XXX - Do be sure to remove it once all arches implement it.
965 void __weak
read_persistent_clock(struct timespec
*ts
)
972 * read_boot_clock - Return time of the system start.
974 * Weak dummy function for arches that do not yet support it.
975 * Function to read the exact time the system has been started.
976 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
978 * XXX - Do be sure to remove it once all arches implement it.
980 void __weak
read_boot_clock(struct timespec
*ts
)
987 * timekeeping_init - Initializes the clocksource and common timekeeping values
989 void __init
timekeeping_init(void)
991 struct timekeeper
*tk
= &tk_core
.timekeeper
;
992 struct clocksource
*clock
;
994 struct timespec64 now
, boot
, tmp
;
997 read_persistent_clock(&ts
);
998 now
= timespec_to_timespec64(ts
);
999 if (!timespec64_valid_strict(&now
)) {
1000 pr_warn("WARNING: Persistent clock returned invalid value!\n"
1001 " Check your CMOS/BIOS settings.\n");
1004 } else if (now
.tv_sec
|| now
.tv_nsec
)
1005 persistent_clock_exist
= true;
1007 read_boot_clock(&ts
);
1008 boot
= timespec_to_timespec64(ts
);
1009 if (!timespec64_valid_strict(&boot
)) {
1010 pr_warn("WARNING: Boot clock returned invalid value!\n"
1011 " Check your CMOS/BIOS settings.\n");
1016 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1017 write_seqcount_begin(&tk_core
.seq
);
1020 clock
= clocksource_default_clock();
1022 clock
->enable(clock
);
1023 tk_setup_internals(tk
, clock
);
1025 tk_set_xtime(tk
, &now
);
1026 tk
->raw_time
.tv_sec
= 0;
1027 tk
->raw_time
.tv_nsec
= 0;
1028 tk
->base_raw
.tv64
= 0;
1029 if (boot
.tv_sec
== 0 && boot
.tv_nsec
== 0)
1030 boot
= tk_xtime(tk
);
1032 set_normalized_timespec64(&tmp
, -boot
.tv_sec
, -boot
.tv_nsec
);
1033 tk_set_wall_to_mono(tk
, tmp
);
1035 timekeeping_update(tk
, TK_MIRROR
);
1037 write_seqcount_end(&tk_core
.seq
);
1038 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1041 /* time in seconds when suspend began */
1042 static struct timespec64 timekeeping_suspend_time
;
1045 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1046 * @delta: pointer to a timespec delta value
1048 * Takes a timespec offset measuring a suspend interval and properly
1049 * adds the sleep offset to the timekeeping variables.
1051 static void __timekeeping_inject_sleeptime(struct timekeeper
*tk
,
1052 struct timespec64
*delta
)
1054 if (!timespec64_valid_strict(delta
)) {
1055 printk_deferred(KERN_WARNING
1056 "__timekeeping_inject_sleeptime: Invalid "
1057 "sleep delta value!\n");
1060 tk_xtime_add(tk
, delta
);
1061 tk_set_wall_to_mono(tk
, timespec64_sub(tk
->wall_to_monotonic
, *delta
));
1062 tk_update_sleep_time(tk
, timespec64_to_ktime(*delta
));
1063 tk_debug_account_sleep_time(delta
);
1067 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
1068 * @delta: pointer to a timespec delta value
1070 * This hook is for architectures that cannot support read_persistent_clock
1071 * because their RTC/persistent clock is only accessible when irqs are enabled.
1073 * This function should only be called by rtc_resume(), and allows
1074 * a suspend offset to be injected into the timekeeping values.
1076 void timekeeping_inject_sleeptime(struct timespec
*delta
)
1078 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1079 struct timespec64 tmp
;
1080 unsigned long flags
;
1083 * Make sure we don't set the clock twice, as timekeeping_resume()
1086 if (has_persistent_clock())
1089 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1090 write_seqcount_begin(&tk_core
.seq
);
1092 timekeeping_forward_now(tk
);
1094 tmp
= timespec_to_timespec64(*delta
);
1095 __timekeeping_inject_sleeptime(tk
, &tmp
);
1097 timekeeping_update(tk
, TK_CLEAR_NTP
| TK_MIRROR
| TK_CLOCK_WAS_SET
);
1099 write_seqcount_end(&tk_core
.seq
);
1100 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1102 /* signal hrtimers about time change */
1107 * timekeeping_resume - Resumes the generic timekeeping subsystem.
1109 * This is for the generic clocksource timekeeping.
1110 * xtime/wall_to_monotonic/jiffies/etc are
1111 * still managed by arch specific suspend/resume code.
1113 static void timekeeping_resume(void)
1115 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1116 struct clocksource
*clock
= tk
->tkr
.clock
;
1117 unsigned long flags
;
1118 struct timespec64 ts_new
, ts_delta
;
1119 struct timespec tmp
;
1120 cycle_t cycle_now
, cycle_delta
;
1121 bool suspendtime_found
= false;
1123 read_persistent_clock(&tmp
);
1124 ts_new
= timespec_to_timespec64(tmp
);
1126 clockevents_resume();
1127 clocksource_resume();
1129 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1130 write_seqcount_begin(&tk_core
.seq
);
1133 * After system resumes, we need to calculate the suspended time and
1134 * compensate it for the OS time. There are 3 sources that could be
1135 * used: Nonstop clocksource during suspend, persistent clock and rtc
1138 * One specific platform may have 1 or 2 or all of them, and the
1139 * preference will be:
1140 * suspend-nonstop clocksource -> persistent clock -> rtc
1141 * The less preferred source will only be tried if there is no better
1142 * usable source. The rtc part is handled separately in rtc core code.
1144 cycle_now
= tk
->tkr
.read(clock
);
1145 if ((clock
->flags
& CLOCK_SOURCE_SUSPEND_NONSTOP
) &&
1146 cycle_now
> tk
->tkr
.cycle_last
) {
1147 u64 num
, max
= ULLONG_MAX
;
1148 u32 mult
= clock
->mult
;
1149 u32 shift
= clock
->shift
;
1152 cycle_delta
= clocksource_delta(cycle_now
, tk
->tkr
.cycle_last
,
1156 * "cycle_delta * mutl" may cause 64 bits overflow, if the
1157 * suspended time is too long. In that case we need do the
1158 * 64 bits math carefully
1161 if (cycle_delta
> max
) {
1162 num
= div64_u64(cycle_delta
, max
);
1163 nsec
= (((u64
) max
* mult
) >> shift
) * num
;
1164 cycle_delta
-= num
* max
;
1166 nsec
+= ((u64
) cycle_delta
* mult
) >> shift
;
1168 ts_delta
= ns_to_timespec64(nsec
);
1169 suspendtime_found
= true;
1170 } else if (timespec64_compare(&ts_new
, &timekeeping_suspend_time
) > 0) {
1171 ts_delta
= timespec64_sub(ts_new
, timekeeping_suspend_time
);
1172 suspendtime_found
= true;
1175 if (suspendtime_found
)
1176 __timekeeping_inject_sleeptime(tk
, &ts_delta
);
1178 /* Re-base the last cycle value */
1179 tk
->tkr
.cycle_last
= cycle_now
;
1181 timekeeping_suspended
= 0;
1182 timekeeping_update(tk
, TK_MIRROR
| TK_CLOCK_WAS_SET
);
1183 write_seqcount_end(&tk_core
.seq
);
1184 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1186 touch_softlockup_watchdog();
1188 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME
, NULL
);
1190 /* Resume hrtimers */
1194 static int timekeeping_suspend(void)
1196 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1197 unsigned long flags
;
1198 struct timespec64 delta
, delta_delta
;
1199 static struct timespec64 old_delta
;
1200 struct timespec tmp
;
1202 read_persistent_clock(&tmp
);
1203 timekeeping_suspend_time
= timespec_to_timespec64(tmp
);
1206 * On some systems the persistent_clock can not be detected at
1207 * timekeeping_init by its return value, so if we see a valid
1208 * value returned, update the persistent_clock_exists flag.
1210 if (timekeeping_suspend_time
.tv_sec
|| timekeeping_suspend_time
.tv_nsec
)
1211 persistent_clock_exist
= true;
1213 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1214 write_seqcount_begin(&tk_core
.seq
);
1215 timekeeping_forward_now(tk
);
1216 timekeeping_suspended
= 1;
1219 * To avoid drift caused by repeated suspend/resumes,
1220 * which each can add ~1 second drift error,
1221 * try to compensate so the difference in system time
1222 * and persistent_clock time stays close to constant.
1224 delta
= timespec64_sub(tk_xtime(tk
), timekeeping_suspend_time
);
1225 delta_delta
= timespec64_sub(delta
, old_delta
);
1226 if (abs(delta_delta
.tv_sec
) >= 2) {
1228 * if delta_delta is too large, assume time correction
1229 * has occured and set old_delta to the current delta.
1233 /* Otherwise try to adjust old_system to compensate */
1234 timekeeping_suspend_time
=
1235 timespec64_add(timekeeping_suspend_time
, delta_delta
);
1238 timekeeping_update(tk
, TK_MIRROR
);
1239 write_seqcount_end(&tk_core
.seq
);
1240 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1242 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND
, NULL
);
1243 clocksource_suspend();
1244 clockevents_suspend();
1249 /* sysfs resume/suspend bits for timekeeping */
1250 static struct syscore_ops timekeeping_syscore_ops
= {
1251 .resume
= timekeeping_resume
,
1252 .suspend
= timekeeping_suspend
,
1255 static int __init
timekeeping_init_ops(void)
1257 register_syscore_ops(&timekeeping_syscore_ops
);
1261 device_initcall(timekeeping_init_ops
);
1264 * If the error is already larger, we look ahead even further
1265 * to compensate for late or lost adjustments.
1267 static __always_inline
int timekeeping_bigadjust(struct timekeeper
*tk
,
1268 s64 error
, s64
*interval
,
1272 u32 look_ahead
, adj
;
1276 * Use the current error value to determine how much to look ahead.
1277 * The larger the error the slower we adjust for it to avoid problems
1278 * with losing too many ticks, otherwise we would overadjust and
1279 * produce an even larger error. The smaller the adjustment the
1280 * faster we try to adjust for it, as lost ticks can do less harm
1281 * here. This is tuned so that an error of about 1 msec is adjusted
1282 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
1284 error2
= tk
->ntp_error
>> (NTP_SCALE_SHIFT
+ 22 - 2 * SHIFT_HZ
);
1285 error2
= abs(error2
);
1286 for (look_ahead
= 0; error2
> 0; look_ahead
++)
1290 * Now calculate the error in (1 << look_ahead) ticks, but first
1291 * remove the single look ahead already included in the error.
1293 tick_error
= ntp_tick_length() >> (tk
->ntp_error_shift
+ 1);
1294 tick_error
-= tk
->xtime_interval
>> 1;
1295 error
= ((error
- tick_error
) >> look_ahead
) + tick_error
;
1297 /* Finally calculate the adjustment shift value. */
1302 *interval
= -*interval
;
1306 for (adj
= 0; error
> i
; adj
++)
1315 * Adjust the multiplier to reduce the error value,
1316 * this is optimized for the most common adjustments of -1,0,1,
1317 * for other values we can do a bit more work.
1319 static void timekeeping_adjust(struct timekeeper
*tk
, s64 offset
)
1321 s64 error
, interval
= tk
->cycle_interval
;
1325 * The point of this is to check if the error is greater than half
1328 * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
1330 * Note we subtract one in the shift, so that error is really error*2.
1331 * This "saves" dividing(shifting) interval twice, but keeps the
1332 * (error > interval) comparison as still measuring if error is
1333 * larger than half an interval.
1335 * Note: It does not "save" on aggravation when reading the code.
1337 error
= tk
->ntp_error
>> (tk
->ntp_error_shift
- 1);
1338 if (error
> interval
) {
1340 * We now divide error by 4(via shift), which checks if
1341 * the error is greater than twice the interval.
1342 * If it is greater, we need a bigadjust, if its smaller,
1343 * we can adjust by 1.
1346 if (likely(error
<= interval
))
1349 adj
= timekeeping_bigadjust(tk
, error
, &interval
, &offset
);
1351 if (error
< -interval
) {
1352 /* See comment above, this is just switched for the negative */
1354 if (likely(error
>= -interval
)) {
1356 interval
= -interval
;
1359 adj
= timekeeping_bigadjust(tk
, error
, &interval
, &offset
);
1366 if (unlikely(tk
->tkr
.clock
->maxadj
&&
1367 (tk
->tkr
.mult
+ adj
> tk
->tkr
.clock
->mult
+ tk
->tkr
.clock
->maxadj
))) {
1368 printk_deferred_once(KERN_WARNING
1369 "Adjusting %s more than 11%% (%ld vs %ld)\n",
1370 tk
->tkr
.clock
->name
, (long)tk
->tkr
.mult
+ adj
,
1371 (long)tk
->tkr
.clock
->mult
+ tk
->tkr
.clock
->maxadj
);
1374 * So the following can be confusing.
1376 * To keep things simple, lets assume adj == 1 for now.
1378 * When adj != 1, remember that the interval and offset values
1379 * have been appropriately scaled so the math is the same.
1381 * The basic idea here is that we're increasing the multiplier
1382 * by one, this causes the xtime_interval to be incremented by
1383 * one cycle_interval. This is because:
1384 * xtime_interval = cycle_interval * mult
1385 * So if mult is being incremented by one:
1386 * xtime_interval = cycle_interval * (mult + 1)
1388 * xtime_interval = (cycle_interval * mult) + cycle_interval
1389 * Which can be shortened to:
1390 * xtime_interval += cycle_interval
1392 * So offset stores the non-accumulated cycles. Thus the current
1393 * time (in shifted nanoseconds) is:
1394 * now = (offset * adj) + xtime_nsec
1395 * Now, even though we're adjusting the clock frequency, we have
1396 * to keep time consistent. In other words, we can't jump back
1397 * in time, and we also want to avoid jumping forward in time.
1399 * So given the same offset value, we need the time to be the same
1400 * both before and after the freq adjustment.
1401 * now = (offset * adj_1) + xtime_nsec_1
1402 * now = (offset * adj_2) + xtime_nsec_2
1404 * (offset * adj_1) + xtime_nsec_1 =
1405 * (offset * adj_2) + xtime_nsec_2
1409 * (offset * adj_1) + xtime_nsec_1 =
1410 * (offset * (adj_1+1)) + xtime_nsec_2
1411 * (offset * adj_1) + xtime_nsec_1 =
1412 * (offset * adj_1) + offset + xtime_nsec_2
1413 * Canceling the sides:
1414 * xtime_nsec_1 = offset + xtime_nsec_2
1416 * xtime_nsec_2 = xtime_nsec_1 - offset
1417 * Which simplfies to:
1418 * xtime_nsec -= offset
1420 * XXX - TODO: Doc ntp_error calculation.
1422 tk
->tkr
.mult
+= adj
;
1423 tk
->xtime_interval
+= interval
;
1424 tk
->tkr
.xtime_nsec
-= offset
;
1425 tk
->ntp_error
-= (interval
- offset
) << tk
->ntp_error_shift
;
1429 * It may be possible that when we entered this function, xtime_nsec
1430 * was very small. Further, if we're slightly speeding the clocksource
1431 * in the code above, its possible the required corrective factor to
1432 * xtime_nsec could cause it to underflow.
1434 * Now, since we already accumulated the second, cannot simply roll
1435 * the accumulated second back, since the NTP subsystem has been
1436 * notified via second_overflow. So instead we push xtime_nsec forward
1437 * by the amount we underflowed, and add that amount into the error.
1439 * We'll correct this error next time through this function, when
1440 * xtime_nsec is not as small.
1442 if (unlikely((s64
)tk
->tkr
.xtime_nsec
< 0)) {
1443 s64 neg
= -(s64
)tk
->tkr
.xtime_nsec
;
1444 tk
->tkr
.xtime_nsec
= 0;
1445 tk
->ntp_error
+= neg
<< tk
->ntp_error_shift
;
1451 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1453 * Helper function that accumulates a the nsecs greater then a second
1454 * from the xtime_nsec field to the xtime_secs field.
1455 * It also calls into the NTP code to handle leapsecond processing.
1458 static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper
*tk
)
1460 u64 nsecps
= (u64
)NSEC_PER_SEC
<< tk
->tkr
.shift
;
1461 unsigned int clock_set
= 0;
1463 while (tk
->tkr
.xtime_nsec
>= nsecps
) {
1466 tk
->tkr
.xtime_nsec
-= nsecps
;
1469 /* Figure out if its a leap sec and apply if needed */
1470 leap
= second_overflow(tk
->xtime_sec
);
1471 if (unlikely(leap
)) {
1472 struct timespec64 ts
;
1474 tk
->xtime_sec
+= leap
;
1478 tk_set_wall_to_mono(tk
,
1479 timespec64_sub(tk
->wall_to_monotonic
, ts
));
1481 __timekeeping_set_tai_offset(tk
, tk
->tai_offset
- leap
);
1483 clock_set
= TK_CLOCK_WAS_SET
;
1490 * logarithmic_accumulation - shifted accumulation of cycles
1492 * This functions accumulates a shifted interval of cycles into
1493 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1496 * Returns the unconsumed cycles.
1498 static cycle_t
logarithmic_accumulation(struct timekeeper
*tk
, cycle_t offset
,
1500 unsigned int *clock_set
)
1502 cycle_t interval
= tk
->cycle_interval
<< shift
;
1505 /* If the offset is smaller then a shifted interval, do nothing */
1506 if (offset
< interval
)
1509 /* Accumulate one shifted interval */
1511 tk
->tkr
.cycle_last
+= interval
;
1513 tk
->tkr
.xtime_nsec
+= tk
->xtime_interval
<< shift
;
1514 *clock_set
|= accumulate_nsecs_to_secs(tk
);
1516 /* Accumulate raw time */
1517 raw_nsecs
= (u64
)tk
->raw_interval
<< shift
;
1518 raw_nsecs
+= tk
->raw_time
.tv_nsec
;
1519 if (raw_nsecs
>= NSEC_PER_SEC
) {
1520 u64 raw_secs
= raw_nsecs
;
1521 raw_nsecs
= do_div(raw_secs
, NSEC_PER_SEC
);
1522 tk
->raw_time
.tv_sec
+= raw_secs
;
1524 tk
->raw_time
.tv_nsec
= raw_nsecs
;
1526 /* Accumulate error between NTP and clock interval */
1527 tk
->ntp_error
+= ntp_tick_length() << shift
;
1528 tk
->ntp_error
-= (tk
->xtime_interval
+ tk
->xtime_remainder
) <<
1529 (tk
->ntp_error_shift
+ shift
);
1535 * update_wall_time - Uses the current clocksource to increment the wall time
1538 void update_wall_time(void)
1540 struct timekeeper
*real_tk
= &tk_core
.timekeeper
;
1541 struct timekeeper
*tk
= &shadow_timekeeper
;
1543 int shift
= 0, maxshift
;
1544 unsigned int clock_set
= 0;
1545 unsigned long flags
;
1547 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1549 /* Make sure we're fully resumed: */
1550 if (unlikely(timekeeping_suspended
))
1553 #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
1554 offset
= real_tk
->cycle_interval
;
1556 offset
= clocksource_delta(tk
->tkr
.read(tk
->tkr
.clock
),
1557 tk
->tkr
.cycle_last
, tk
->tkr
.mask
);
1560 /* Check if there's really nothing to do */
1561 if (offset
< real_tk
->cycle_interval
)
1565 * With NO_HZ we may have to accumulate many cycle_intervals
1566 * (think "ticks") worth of time at once. To do this efficiently,
1567 * we calculate the largest doubling multiple of cycle_intervals
1568 * that is smaller than the offset. We then accumulate that
1569 * chunk in one go, and then try to consume the next smaller
1572 shift
= ilog2(offset
) - ilog2(tk
->cycle_interval
);
1573 shift
= max(0, shift
);
1574 /* Bound shift to one less than what overflows tick_length */
1575 maxshift
= (64 - (ilog2(ntp_tick_length())+1)) - 1;
1576 shift
= min(shift
, maxshift
);
1577 while (offset
>= tk
->cycle_interval
) {
1578 offset
= logarithmic_accumulation(tk
, offset
, shift
,
1580 if (offset
< tk
->cycle_interval
<<shift
)
1584 /* correct the clock when NTP error is too big */
1585 timekeeping_adjust(tk
, offset
);
1588 * XXX This can be killed once everyone converts
1589 * to the new update_vsyscall.
1591 old_vsyscall_fixup(tk
);
1594 * Finally, make sure that after the rounding
1595 * xtime_nsec isn't larger than NSEC_PER_SEC
1597 clock_set
|= accumulate_nsecs_to_secs(tk
);
1599 write_seqcount_begin(&tk_core
.seq
);
1601 * Update the real timekeeper.
1603 * We could avoid this memcpy by switching pointers, but that
1604 * requires changes to all other timekeeper usage sites as
1605 * well, i.e. move the timekeeper pointer getter into the
1606 * spinlocked/seqcount protected sections. And we trade this
1607 * memcpy under the tk_core.seq against one before we start
1610 memcpy(real_tk
, tk
, sizeof(*tk
));
1611 timekeeping_update(real_tk
, clock_set
);
1612 write_seqcount_end(&tk_core
.seq
);
1614 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1616 /* Have to call _delayed version, since in irq context*/
1617 clock_was_set_delayed();
1621 * getboottime - Return the real time of system boot.
1622 * @ts: pointer to the timespec to be set
1624 * Returns the wall-time of boot in a timespec.
1626 * This is based on the wall_to_monotonic offset and the total suspend
1627 * time. Calls to settimeofday will affect the value returned (which
1628 * basically means that however wrong your real time clock is at boot time,
1629 * you get the right time here).
1631 void getboottime(struct timespec
*ts
)
1633 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1634 ktime_t t
= ktime_sub(tk
->offs_real
, tk
->offs_boot
);
1636 *ts
= ktime_to_timespec(t
);
1638 EXPORT_SYMBOL_GPL(getboottime
);
1640 unsigned long get_seconds(void)
1642 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1644 return tk
->xtime_sec
;
1646 EXPORT_SYMBOL(get_seconds
);
1648 struct timespec
__current_kernel_time(void)
1650 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1652 return timespec64_to_timespec(tk_xtime(tk
));
1655 struct timespec
current_kernel_time(void)
1657 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1658 struct timespec64 now
;
1662 seq
= read_seqcount_begin(&tk_core
.seq
);
1665 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
1667 return timespec64_to_timespec(now
);
1669 EXPORT_SYMBOL(current_kernel_time
);
1671 struct timespec
get_monotonic_coarse(void)
1673 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1674 struct timespec64 now
, mono
;
1678 seq
= read_seqcount_begin(&tk_core
.seq
);
1681 mono
= tk
->wall_to_monotonic
;
1682 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
1684 set_normalized_timespec64(&now
, now
.tv_sec
+ mono
.tv_sec
,
1685 now
.tv_nsec
+ mono
.tv_nsec
);
1687 return timespec64_to_timespec(now
);
1691 * Must hold jiffies_lock
1693 void do_timer(unsigned long ticks
)
1695 jiffies_64
+= ticks
;
1696 calc_global_load(ticks
);
1700 * ktime_get_update_offsets_tick - hrtimer helper
1701 * @offs_real: pointer to storage for monotonic -> realtime offset
1702 * @offs_boot: pointer to storage for monotonic -> boottime offset
1703 * @offs_tai: pointer to storage for monotonic -> clock tai offset
1705 * Returns monotonic time at last tick and various offsets
1707 ktime_t
ktime_get_update_offsets_tick(ktime_t
*offs_real
, ktime_t
*offs_boot
,
1710 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1716 seq
= read_seqcount_begin(&tk_core
.seq
);
1718 base
= tk
->tkr
.base_mono
;
1719 nsecs
= tk
->tkr
.xtime_nsec
>> tk
->tkr
.shift
;
1721 *offs_real
= tk
->offs_real
;
1722 *offs_boot
= tk
->offs_boot
;
1723 *offs_tai
= tk
->offs_tai
;
1724 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
1726 return ktime_add_ns(base
, nsecs
);
1729 #ifdef CONFIG_HIGH_RES_TIMERS
1731 * ktime_get_update_offsets_now - hrtimer helper
1732 * @offs_real: pointer to storage for monotonic -> realtime offset
1733 * @offs_boot: pointer to storage for monotonic -> boottime offset
1734 * @offs_tai: pointer to storage for monotonic -> clock tai offset
1736 * Returns current monotonic time and updates the offsets
1737 * Called from hrtimer_interrupt() or retrigger_next_event()
1739 ktime_t
ktime_get_update_offsets_now(ktime_t
*offs_real
, ktime_t
*offs_boot
,
1742 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1748 seq
= read_seqcount_begin(&tk_core
.seq
);
1750 base
= tk
->tkr
.base_mono
;
1751 nsecs
= timekeeping_get_ns(&tk
->tkr
);
1753 *offs_real
= tk
->offs_real
;
1754 *offs_boot
= tk
->offs_boot
;
1755 *offs_tai
= tk
->offs_tai
;
1756 } while (read_seqcount_retry(&tk_core
.seq
, seq
));
1758 return ktime_add_ns(base
, nsecs
);
1763 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
1765 int do_adjtimex(struct timex
*txc
)
1767 struct timekeeper
*tk
= &tk_core
.timekeeper
;
1768 unsigned long flags
;
1769 struct timespec64 ts
;
1773 /* Validate the data before disabling interrupts */
1774 ret
= ntp_validate_timex(txc
);
1778 if (txc
->modes
& ADJ_SETOFFSET
) {
1779 struct timespec delta
;
1780 delta
.tv_sec
= txc
->time
.tv_sec
;
1781 delta
.tv_nsec
= txc
->time
.tv_usec
;
1782 if (!(txc
->modes
& ADJ_NANO
))
1783 delta
.tv_nsec
*= 1000;
1784 ret
= timekeeping_inject_offset(&delta
);
1789 getnstimeofday64(&ts
);
1791 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1792 write_seqcount_begin(&tk_core
.seq
);
1794 orig_tai
= tai
= tk
->tai_offset
;
1795 ret
= __do_adjtimex(txc
, &ts
, &tai
);
1797 if (tai
!= orig_tai
) {
1798 __timekeeping_set_tai_offset(tk
, tai
);
1799 timekeeping_update(tk
, TK_MIRROR
| TK_CLOCK_WAS_SET
);
1801 write_seqcount_end(&tk_core
.seq
);
1802 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1804 if (tai
!= orig_tai
)
1807 ntp_notify_cmos_timer();
1812 #ifdef CONFIG_NTP_PPS
1814 * hardpps() - Accessor function to NTP __hardpps function
1816 void hardpps(const struct timespec
*phase_ts
, const struct timespec
*raw_ts
)
1818 unsigned long flags
;
1820 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1821 write_seqcount_begin(&tk_core
.seq
);
1823 __hardpps(phase_ts
, raw_ts
);
1825 write_seqcount_end(&tk_core
.seq
);
1826 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1828 EXPORT_SYMBOL(hardpps
);
1832 * xtime_update() - advances the timekeeping infrastructure
1833 * @ticks: number of ticks, that have elapsed since the last call.
1835 * Must be called with interrupts disabled.
1837 void xtime_update(unsigned long ticks
)
1839 write_seqlock(&jiffies_lock
);
1841 write_sequnlock(&jiffies_lock
);