aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time/timekeeping.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/time/timekeeping.c')
-rw-r--r--kernel/time/timekeeping.c345
1 files changed, 233 insertions, 112 deletions
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 91db94136c10..c3fcff06d30a 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -59,6 +59,7 @@ struct tk_fast {
59}; 59};
60 60
61static struct tk_fast tk_fast_mono ____cacheline_aligned; 61static struct tk_fast tk_fast_mono ____cacheline_aligned;
62static struct tk_fast tk_fast_raw ____cacheline_aligned;
62 63
63/* flag for if timekeeping is suspended */ 64/* flag for if timekeeping is suspended */
64int __read_mostly timekeeping_suspended; 65int __read_mostly timekeeping_suspended;
@@ -68,8 +69,8 @@ bool __read_mostly persistent_clock_exist = false;
68 69
69static inline void tk_normalize_xtime(struct timekeeper *tk) 70static inline void tk_normalize_xtime(struct timekeeper *tk)
70{ 71{
71 while (tk->tkr.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr.shift)) { 72 while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
72 tk->tkr.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr.shift; 73 tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
73 tk->xtime_sec++; 74 tk->xtime_sec++;
74 } 75 }
75} 76}
@@ -79,20 +80,20 @@ static inline struct timespec64 tk_xtime(struct timekeeper *tk)
79 struct timespec64 ts; 80 struct timespec64 ts;
80 81
81 ts.tv_sec = tk->xtime_sec; 82 ts.tv_sec = tk->xtime_sec;
82 ts.tv_nsec = (long)(tk->tkr.xtime_nsec >> tk->tkr.shift); 83 ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
83 return ts; 84 return ts;
84} 85}
85 86
86static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts) 87static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
87{ 88{
88 tk->xtime_sec = ts->tv_sec; 89 tk->xtime_sec = ts->tv_sec;
89 tk->tkr.xtime_nsec = (u64)ts->tv_nsec << tk->tkr.shift; 90 tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
90} 91}
91 92
92static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts) 93static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
93{ 94{
94 tk->xtime_sec += ts->tv_sec; 95 tk->xtime_sec += ts->tv_sec;
95 tk->tkr.xtime_nsec += (u64)ts->tv_nsec << tk->tkr.shift; 96 tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
96 tk_normalize_xtime(tk); 97 tk_normalize_xtime(tk);
97} 98}
98 99
@@ -118,6 +119,117 @@ static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
118 tk->offs_boot = ktime_add(tk->offs_boot, delta); 119 tk->offs_boot = ktime_add(tk->offs_boot, delta);
119} 120}
120 121
122#ifdef CONFIG_DEBUG_TIMEKEEPING
123#define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
124/*
125 * These simple flag variables are managed
126 * without locks, which is racy, but ok since
127 * we don't really care about being super
128 * precise about how many events were seen,
129 * just that a problem was observed.
130 */
131static int timekeeping_underflow_seen;
132static int timekeeping_overflow_seen;
133
134/* last_warning is only modified under the timekeeping lock */
135static long timekeeping_last_warning;
136
137static void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
138{
139
140 cycle_t max_cycles = tk->tkr_mono.clock->max_cycles;
141 const char *name = tk->tkr_mono.clock->name;
142
143 if (offset > max_cycles) {
144 printk_deferred("WARNING: timekeeping: Cycle offset (%lld) is larger than allowed by the '%s' clock's max_cycles value (%lld): time overflow danger\n",
145 offset, name, max_cycles);
146 printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n");
147 } else {
148 if (offset > (max_cycles >> 1)) {
149 printk_deferred("INFO: timekeeping: Cycle offset (%lld) is larger than the the '%s' clock's 50%% safety margin (%lld)\n",
150 offset, name, max_cycles >> 1);
151 printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n");
152 }
153 }
154
155 if (timekeeping_underflow_seen) {
156 if (jiffies - timekeeping_last_warning > WARNING_FREQ) {
157 printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name);
158 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
159 printk_deferred(" Your kernel is probably still fine.\n");
160 timekeeping_last_warning = jiffies;
161 }
162 timekeeping_underflow_seen = 0;
163 }
164
165 if (timekeeping_overflow_seen) {
166 if (jiffies - timekeeping_last_warning > WARNING_FREQ) {
167 printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name);
168 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
169 printk_deferred(" Your kernel is probably still fine.\n");
170 timekeeping_last_warning = jiffies;
171 }
172 timekeeping_overflow_seen = 0;
173 }
174}
175
176static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
177{
178 cycle_t now, last, mask, max, delta;
179 unsigned int seq;
180
181 /*
182 * Since we're called holding a seqlock, the data may shift
183 * under us while we're doing the calculation. This can cause
184 * false positives, since we'd note a problem but throw the
185 * results away. So nest another seqlock here to atomically
186 * grab the points we are checking with.
187 */
188 do {
189 seq = read_seqcount_begin(&tk_core.seq);
190 now = tkr->read(tkr->clock);
191 last = tkr->cycle_last;
192 mask = tkr->mask;
193 max = tkr->clock->max_cycles;
194 } while (read_seqcount_retry(&tk_core.seq, seq));
195
196 delta = clocksource_delta(now, last, mask);
197
198 /*
199 * Try to catch underflows by checking if we are seeing small
200 * mask-relative negative values.
201 */
202 if (unlikely((~delta & mask) < (mask >> 3))) {
203 timekeeping_underflow_seen = 1;
204 delta = 0;
205 }
206
207 /* Cap delta value to the max_cycles values to avoid mult overflows */
208 if (unlikely(delta > max)) {
209 timekeeping_overflow_seen = 1;
210 delta = tkr->clock->max_cycles;
211 }
212
213 return delta;
214}
215#else
216static inline void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
217{
218}
219static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
220{
221 cycle_t cycle_now, delta;
222
223 /* read clocksource */
224 cycle_now = tkr->read(tkr->clock);
225
226 /* calculate the delta since the last update_wall_time */
227 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
228
229 return delta;
230}
231#endif
232
121/** 233/**
122 * tk_setup_internals - Set up internals to use clocksource clock. 234 * tk_setup_internals - Set up internals to use clocksource clock.
123 * 235 *
@@ -135,11 +247,16 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
135 u64 tmp, ntpinterval; 247 u64 tmp, ntpinterval;
136 struct clocksource *old_clock; 248 struct clocksource *old_clock;
137 249
138 old_clock = tk->tkr.clock; 250 old_clock = tk->tkr_mono.clock;
139 tk->tkr.clock = clock; 251 tk->tkr_mono.clock = clock;
140 tk->tkr.read = clock->read; 252 tk->tkr_mono.read = clock->read;
141 tk->tkr.mask = clock->mask; 253 tk->tkr_mono.mask = clock->mask;
142 tk->tkr.cycle_last = tk->tkr.read(clock); 254 tk->tkr_mono.cycle_last = tk->tkr_mono.read(clock);
255
256 tk->tkr_raw.clock = clock;
257 tk->tkr_raw.read = clock->read;
258 tk->tkr_raw.mask = clock->mask;
259 tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
143 260
144 /* Do the ns -> cycle conversion first, using original mult */ 261 /* Do the ns -> cycle conversion first, using original mult */
145 tmp = NTP_INTERVAL_LENGTH; 262 tmp = NTP_INTERVAL_LENGTH;
@@ -163,11 +280,14 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
163 if (old_clock) { 280 if (old_clock) {
164 int shift_change = clock->shift - old_clock->shift; 281 int shift_change = clock->shift - old_clock->shift;
165 if (shift_change < 0) 282 if (shift_change < 0)
166 tk->tkr.xtime_nsec >>= -shift_change; 283 tk->tkr_mono.xtime_nsec >>= -shift_change;
167 else 284 else
168 tk->tkr.xtime_nsec <<= shift_change; 285 tk->tkr_mono.xtime_nsec <<= shift_change;
169 } 286 }
170 tk->tkr.shift = clock->shift; 287 tk->tkr_raw.xtime_nsec = 0;
288
289 tk->tkr_mono.shift = clock->shift;
290 tk->tkr_raw.shift = clock->shift;
171 291
172 tk->ntp_error = 0; 292 tk->ntp_error = 0;
173 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; 293 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
@@ -178,7 +298,8 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
178 * active clocksource. These value will be adjusted via NTP 298 * active clocksource. These value will be adjusted via NTP
179 * to counteract clock drifting. 299 * to counteract clock drifting.
180 */ 300 */
181 tk->tkr.mult = clock->mult; 301 tk->tkr_mono.mult = clock->mult;
302 tk->tkr_raw.mult = clock->mult;
182 tk->ntp_err_mult = 0; 303 tk->ntp_err_mult = 0;
183} 304}
184 305
@@ -193,14 +314,10 @@ static inline u32 arch_gettimeoffset(void) { return 0; }
193 314
194static inline s64 timekeeping_get_ns(struct tk_read_base *tkr) 315static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
195{ 316{
196 cycle_t cycle_now, delta; 317 cycle_t delta;
197 s64 nsec; 318 s64 nsec;
198 319
199 /* read clocksource: */ 320 delta = timekeeping_get_delta(tkr);
200 cycle_now = tkr->read(tkr->clock);
201
202 /* calculate the delta since the last update_wall_time: */
203 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
204 321
205 nsec = delta * tkr->mult + tkr->xtime_nsec; 322 nsec = delta * tkr->mult + tkr->xtime_nsec;
206 nsec >>= tkr->shift; 323 nsec >>= tkr->shift;
@@ -209,25 +326,6 @@ static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
209 return nsec + arch_gettimeoffset(); 326 return nsec + arch_gettimeoffset();
210} 327}
211 328
212static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
213{
214 struct clocksource *clock = tk->tkr.clock;
215 cycle_t cycle_now, delta;
216 s64 nsec;
217
218 /* read clocksource: */
219 cycle_now = tk->tkr.read(clock);
220
221 /* calculate the delta since the last update_wall_time: */
222 delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
223
224 /* convert delta to nanoseconds. */
225 nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
226
227 /* If arch requires, add in get_arch_timeoffset() */
228 return nsec + arch_gettimeoffset();
229}
230
231/** 329/**
232 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper. 330 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
233 * @tkr: Timekeeping readout base from which we take the update 331 * @tkr: Timekeeping readout base from which we take the update
@@ -267,18 +365,18 @@ static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
267 * slightly wrong timestamp (a few nanoseconds). See 365 * slightly wrong timestamp (a few nanoseconds). See
268 * @ktime_get_mono_fast_ns. 366 * @ktime_get_mono_fast_ns.
269 */ 367 */
270static void update_fast_timekeeper(struct tk_read_base *tkr) 368static void update_fast_timekeeper(struct tk_read_base *tkr, struct tk_fast *tkf)
271{ 369{
272 struct tk_read_base *base = tk_fast_mono.base; 370 struct tk_read_base *base = tkf->base;
273 371
274 /* Force readers off to base[1] */ 372 /* Force readers off to base[1] */
275 raw_write_seqcount_latch(&tk_fast_mono.seq); 373 raw_write_seqcount_latch(&tkf->seq);
276 374
277 /* Update base[0] */ 375 /* Update base[0] */
278 memcpy(base, tkr, sizeof(*base)); 376 memcpy(base, tkr, sizeof(*base));
279 377
280 /* Force readers back to base[0] */ 378 /* Force readers back to base[0] */
281 raw_write_seqcount_latch(&tk_fast_mono.seq); 379 raw_write_seqcount_latch(&tkf->seq);
282 380
283 /* Update base[1] */ 381 /* Update base[1] */
284 memcpy(base + 1, base, sizeof(*base)); 382 memcpy(base + 1, base, sizeof(*base));
@@ -316,22 +414,33 @@ static void update_fast_timekeeper(struct tk_read_base *tkr)
316 * of the following timestamps. Callers need to be aware of that and 414 * of the following timestamps. Callers need to be aware of that and
317 * deal with it. 415 * deal with it.
318 */ 416 */
319u64 notrace ktime_get_mono_fast_ns(void) 417static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
320{ 418{
321 struct tk_read_base *tkr; 419 struct tk_read_base *tkr;
322 unsigned int seq; 420 unsigned int seq;
323 u64 now; 421 u64 now;
324 422
325 do { 423 do {
326 seq = raw_read_seqcount(&tk_fast_mono.seq); 424 seq = raw_read_seqcount(&tkf->seq);
327 tkr = tk_fast_mono.base + (seq & 0x01); 425 tkr = tkf->base + (seq & 0x01);
328 now = ktime_to_ns(tkr->base_mono) + timekeeping_get_ns(tkr); 426 now = ktime_to_ns(tkr->base) + timekeeping_get_ns(tkr);
427 } while (read_seqcount_retry(&tkf->seq, seq));
329 428
330 } while (read_seqcount_retry(&tk_fast_mono.seq, seq));
331 return now; 429 return now;
332} 430}
431
432u64 ktime_get_mono_fast_ns(void)
433{
434 return __ktime_get_fast_ns(&tk_fast_mono);
435}
333EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns); 436EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
334 437
438u64 ktime_get_raw_fast_ns(void)
439{
440 return __ktime_get_fast_ns(&tk_fast_raw);
441}
442EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
443
335/* Suspend-time cycles value for halted fast timekeeper. */ 444/* Suspend-time cycles value for halted fast timekeeper. */
336static cycle_t cycles_at_suspend; 445static cycle_t cycles_at_suspend;
337 446
@@ -353,12 +462,17 @@ static cycle_t dummy_clock_read(struct clocksource *cs)
353static void halt_fast_timekeeper(struct timekeeper *tk) 462static void halt_fast_timekeeper(struct timekeeper *tk)
354{ 463{
355 static struct tk_read_base tkr_dummy; 464 static struct tk_read_base tkr_dummy;
356 struct tk_read_base *tkr = &tk->tkr; 465 struct tk_read_base *tkr = &tk->tkr_mono;
357 466
358 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy)); 467 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
359 cycles_at_suspend = tkr->read(tkr->clock); 468 cycles_at_suspend = tkr->read(tkr->clock);
360 tkr_dummy.read = dummy_clock_read; 469 tkr_dummy.read = dummy_clock_read;
361 update_fast_timekeeper(&tkr_dummy); 470 update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
471
472 tkr = &tk->tkr_raw;
473 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
474 tkr_dummy.read = dummy_clock_read;
475 update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
362} 476}
363 477
364#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD 478#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
@@ -369,8 +483,8 @@ static inline void update_vsyscall(struct timekeeper *tk)
369 483
370 xt = timespec64_to_timespec(tk_xtime(tk)); 484 xt = timespec64_to_timespec(tk_xtime(tk));
371 wm = timespec64_to_timespec(tk->wall_to_monotonic); 485 wm = timespec64_to_timespec(tk->wall_to_monotonic);
372 update_vsyscall_old(&xt, &wm, tk->tkr.clock, tk->tkr.mult, 486 update_vsyscall_old(&xt, &wm, tk->tkr_mono.clock, tk->tkr_mono.mult,
373 tk->tkr.cycle_last); 487 tk->tkr_mono.cycle_last);
374} 488}
375 489
376static inline void old_vsyscall_fixup(struct timekeeper *tk) 490static inline void old_vsyscall_fixup(struct timekeeper *tk)
@@ -387,11 +501,11 @@ static inline void old_vsyscall_fixup(struct timekeeper *tk)
387 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD 501 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
388 * users are removed, this can be killed. 502 * users are removed, this can be killed.
389 */ 503 */
390 remainder = tk->tkr.xtime_nsec & ((1ULL << tk->tkr.shift) - 1); 504 remainder = tk->tkr_mono.xtime_nsec & ((1ULL << tk->tkr_mono.shift) - 1);
391 tk->tkr.xtime_nsec -= remainder; 505 tk->tkr_mono.xtime_nsec -= remainder;
392 tk->tkr.xtime_nsec += 1ULL << tk->tkr.shift; 506 tk->tkr_mono.xtime_nsec += 1ULL << tk->tkr_mono.shift;
393 tk->ntp_error += remainder << tk->ntp_error_shift; 507 tk->ntp_error += remainder << tk->ntp_error_shift;
394 tk->ntp_error -= (1ULL << tk->tkr.shift) << tk->ntp_error_shift; 508 tk->ntp_error -= (1ULL << tk->tkr_mono.shift) << tk->ntp_error_shift;
395} 509}
396#else 510#else
397#define old_vsyscall_fixup(tk) 511#define old_vsyscall_fixup(tk)
@@ -456,17 +570,17 @@ static inline void tk_update_ktime_data(struct timekeeper *tk)
456 */ 570 */
457 seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec); 571 seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
458 nsec = (u32) tk->wall_to_monotonic.tv_nsec; 572 nsec = (u32) tk->wall_to_monotonic.tv_nsec;
459 tk->tkr.base_mono = ns_to_ktime(seconds * NSEC_PER_SEC + nsec); 573 tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
460 574
461 /* Update the monotonic raw base */ 575 /* Update the monotonic raw base */
462 tk->base_raw = timespec64_to_ktime(tk->raw_time); 576 tk->tkr_raw.base = timespec64_to_ktime(tk->raw_time);
463 577
464 /* 578 /*
465 * The sum of the nanoseconds portions of xtime and 579 * The sum of the nanoseconds portions of xtime and
466 * wall_to_monotonic can be greater/equal one second. Take 580 * wall_to_monotonic can be greater/equal one second. Take
467 * this into account before updating tk->ktime_sec. 581 * this into account before updating tk->ktime_sec.
468 */ 582 */
469 nsec += (u32)(tk->tkr.xtime_nsec >> tk->tkr.shift); 583 nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
470 if (nsec >= NSEC_PER_SEC) 584 if (nsec >= NSEC_PER_SEC)
471 seconds++; 585 seconds++;
472 tk->ktime_sec = seconds; 586 tk->ktime_sec = seconds;
@@ -489,7 +603,8 @@ static void timekeeping_update(struct timekeeper *tk, unsigned int action)
489 memcpy(&shadow_timekeeper, &tk_core.timekeeper, 603 memcpy(&shadow_timekeeper, &tk_core.timekeeper,
490 sizeof(tk_core.timekeeper)); 604 sizeof(tk_core.timekeeper));
491 605
492 update_fast_timekeeper(&tk->tkr); 606 update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
607 update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
493} 608}
494 609
495/** 610/**
@@ -501,22 +616,23 @@ static void timekeeping_update(struct timekeeper *tk, unsigned int action)
501 */ 616 */
502static void timekeeping_forward_now(struct timekeeper *tk) 617static void timekeeping_forward_now(struct timekeeper *tk)
503{ 618{
504 struct clocksource *clock = tk->tkr.clock; 619 struct clocksource *clock = tk->tkr_mono.clock;
505 cycle_t cycle_now, delta; 620 cycle_t cycle_now, delta;
506 s64 nsec; 621 s64 nsec;
507 622
508 cycle_now = tk->tkr.read(clock); 623 cycle_now = tk->tkr_mono.read(clock);
509 delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask); 624 delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
510 tk->tkr.cycle_last = cycle_now; 625 tk->tkr_mono.cycle_last = cycle_now;
626 tk->tkr_raw.cycle_last = cycle_now;
511 627
512 tk->tkr.xtime_nsec += delta * tk->tkr.mult; 628 tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult;
513 629
514 /* If arch requires, add in get_arch_timeoffset() */ 630 /* If arch requires, add in get_arch_timeoffset() */
515 tk->tkr.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr.shift; 631 tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
516 632
517 tk_normalize_xtime(tk); 633 tk_normalize_xtime(tk);
518 634
519 nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift); 635 nsec = clocksource_cyc2ns(delta, tk->tkr_raw.mult, tk->tkr_raw.shift);
520 timespec64_add_ns(&tk->raw_time, nsec); 636 timespec64_add_ns(&tk->raw_time, nsec);
521} 637}
522 638
@@ -537,7 +653,7 @@ int __getnstimeofday64(struct timespec64 *ts)
537 seq = read_seqcount_begin(&tk_core.seq); 653 seq = read_seqcount_begin(&tk_core.seq);
538 654
539 ts->tv_sec = tk->xtime_sec; 655 ts->tv_sec = tk->xtime_sec;
540 nsecs = timekeeping_get_ns(&tk->tkr); 656 nsecs = timekeeping_get_ns(&tk->tkr_mono);
541 657
542 } while (read_seqcount_retry(&tk_core.seq, seq)); 658 } while (read_seqcount_retry(&tk_core.seq, seq));
543 659
@@ -577,8 +693,8 @@ ktime_t ktime_get(void)
577 693
578 do { 694 do {
579 seq = read_seqcount_begin(&tk_core.seq); 695 seq = read_seqcount_begin(&tk_core.seq);
580 base = tk->tkr.base_mono; 696 base = tk->tkr_mono.base;
581 nsecs = timekeeping_get_ns(&tk->tkr); 697 nsecs = timekeeping_get_ns(&tk->tkr_mono);
582 698
583 } while (read_seqcount_retry(&tk_core.seq, seq)); 699 } while (read_seqcount_retry(&tk_core.seq, seq));
584 700
@@ -603,8 +719,8 @@ ktime_t ktime_get_with_offset(enum tk_offsets offs)
603 719
604 do { 720 do {
605 seq = read_seqcount_begin(&tk_core.seq); 721 seq = read_seqcount_begin(&tk_core.seq);
606 base = ktime_add(tk->tkr.base_mono, *offset); 722 base = ktime_add(tk->tkr_mono.base, *offset);
607 nsecs = timekeeping_get_ns(&tk->tkr); 723 nsecs = timekeeping_get_ns(&tk->tkr_mono);
608 724
609 } while (read_seqcount_retry(&tk_core.seq, seq)); 725 } while (read_seqcount_retry(&tk_core.seq, seq));
610 726
@@ -645,8 +761,8 @@ ktime_t ktime_get_raw(void)
645 761
646 do { 762 do {
647 seq = read_seqcount_begin(&tk_core.seq); 763 seq = read_seqcount_begin(&tk_core.seq);
648 base = tk->base_raw; 764 base = tk->tkr_raw.base;
649 nsecs = timekeeping_get_ns_raw(tk); 765 nsecs = timekeeping_get_ns(&tk->tkr_raw);
650 766
651 } while (read_seqcount_retry(&tk_core.seq, seq)); 767 } while (read_seqcount_retry(&tk_core.seq, seq));
652 768
@@ -674,7 +790,7 @@ void ktime_get_ts64(struct timespec64 *ts)
674 do { 790 do {
675 seq = read_seqcount_begin(&tk_core.seq); 791 seq = read_seqcount_begin(&tk_core.seq);
676 ts->tv_sec = tk->xtime_sec; 792 ts->tv_sec = tk->xtime_sec;
677 nsec = timekeeping_get_ns(&tk->tkr); 793 nsec = timekeeping_get_ns(&tk->tkr_mono);
678 tomono = tk->wall_to_monotonic; 794 tomono = tk->wall_to_monotonic;
679 795
680 } while (read_seqcount_retry(&tk_core.seq, seq)); 796 } while (read_seqcount_retry(&tk_core.seq, seq));
@@ -759,8 +875,8 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
759 ts_real->tv_sec = tk->xtime_sec; 875 ts_real->tv_sec = tk->xtime_sec;
760 ts_real->tv_nsec = 0; 876 ts_real->tv_nsec = 0;
761 877
762 nsecs_raw = timekeeping_get_ns_raw(tk); 878 nsecs_raw = timekeeping_get_ns(&tk->tkr_raw);
763 nsecs_real = timekeeping_get_ns(&tk->tkr); 879 nsecs_real = timekeeping_get_ns(&tk->tkr_mono);
764 880
765 } while (read_seqcount_retry(&tk_core.seq, seq)); 881 } while (read_seqcount_retry(&tk_core.seq, seq));
766 882
@@ -943,7 +1059,7 @@ static int change_clocksource(void *data)
943 */ 1059 */
944 if (try_module_get(new->owner)) { 1060 if (try_module_get(new->owner)) {
945 if (!new->enable || new->enable(new) == 0) { 1061 if (!new->enable || new->enable(new) == 0) {
946 old = tk->tkr.clock; 1062 old = tk->tkr_mono.clock;
947 tk_setup_internals(tk, new); 1063 tk_setup_internals(tk, new);
948 if (old->disable) 1064 if (old->disable)
949 old->disable(old); 1065 old->disable(old);
@@ -971,11 +1087,11 @@ int timekeeping_notify(struct clocksource *clock)
971{ 1087{
972 struct timekeeper *tk = &tk_core.timekeeper; 1088 struct timekeeper *tk = &tk_core.timekeeper;
973 1089
974 if (tk->tkr.clock == clock) 1090 if (tk->tkr_mono.clock == clock)
975 return 0; 1091 return 0;
976 stop_machine(change_clocksource, clock, NULL); 1092 stop_machine(change_clocksource, clock, NULL);
977 tick_clock_notify(); 1093 tick_clock_notify();
978 return tk->tkr.clock == clock ? 0 : -1; 1094 return tk->tkr_mono.clock == clock ? 0 : -1;
979} 1095}
980 1096
981/** 1097/**
@@ -993,7 +1109,7 @@ void getrawmonotonic64(struct timespec64 *ts)
993 1109
994 do { 1110 do {
995 seq = read_seqcount_begin(&tk_core.seq); 1111 seq = read_seqcount_begin(&tk_core.seq);
996 nsecs = timekeeping_get_ns_raw(tk); 1112 nsecs = timekeeping_get_ns(&tk->tkr_raw);
997 ts64 = tk->raw_time; 1113 ts64 = tk->raw_time;
998 1114
999 } while (read_seqcount_retry(&tk_core.seq, seq)); 1115 } while (read_seqcount_retry(&tk_core.seq, seq));
@@ -1016,7 +1132,7 @@ int timekeeping_valid_for_hres(void)
1016 do { 1132 do {
1017 seq = read_seqcount_begin(&tk_core.seq); 1133 seq = read_seqcount_begin(&tk_core.seq);
1018 1134
1019 ret = tk->tkr.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; 1135 ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
1020 1136
1021 } while (read_seqcount_retry(&tk_core.seq, seq)); 1137 } while (read_seqcount_retry(&tk_core.seq, seq));
1022 1138
@@ -1035,7 +1151,7 @@ u64 timekeeping_max_deferment(void)
1035 do { 1151 do {
1036 seq = read_seqcount_begin(&tk_core.seq); 1152 seq = read_seqcount_begin(&tk_core.seq);
1037 1153
1038 ret = tk->tkr.clock->max_idle_ns; 1154 ret = tk->tkr_mono.clock->max_idle_ns;
1039 1155
1040 } while (read_seqcount_retry(&tk_core.seq, seq)); 1156 } while (read_seqcount_retry(&tk_core.seq, seq));
1041 1157
@@ -1114,7 +1230,6 @@ void __init timekeeping_init(void)
1114 tk_set_xtime(tk, &now); 1230 tk_set_xtime(tk, &now);
1115 tk->raw_time.tv_sec = 0; 1231 tk->raw_time.tv_sec = 0;
1116 tk->raw_time.tv_nsec = 0; 1232 tk->raw_time.tv_nsec = 0;
1117 tk->base_raw.tv64 = 0;
1118 if (boot.tv_sec == 0 && boot.tv_nsec == 0) 1233 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
1119 boot = tk_xtime(tk); 1234 boot = tk_xtime(tk);
1120 1235
@@ -1200,7 +1315,7 @@ void timekeeping_inject_sleeptime64(struct timespec64 *delta)
1200void timekeeping_resume(void) 1315void timekeeping_resume(void)
1201{ 1316{
1202 struct timekeeper *tk = &tk_core.timekeeper; 1317 struct timekeeper *tk = &tk_core.timekeeper;
1203 struct clocksource *clock = tk->tkr.clock; 1318 struct clocksource *clock = tk->tkr_mono.clock;
1204 unsigned long flags; 1319 unsigned long flags;
1205 struct timespec64 ts_new, ts_delta; 1320 struct timespec64 ts_new, ts_delta;
1206 struct timespec tmp; 1321 struct timespec tmp;
@@ -1228,16 +1343,16 @@ void timekeeping_resume(void)
1228 * The less preferred source will only be tried if there is no better 1343 * The less preferred source will only be tried if there is no better
1229 * usable source. The rtc part is handled separately in rtc core code. 1344 * usable source. The rtc part is handled separately in rtc core code.
1230 */ 1345 */
1231 cycle_now = tk->tkr.read(clock); 1346 cycle_now = tk->tkr_mono.read(clock);
1232 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) && 1347 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
1233 cycle_now > tk->tkr.cycle_last) { 1348 cycle_now > tk->tkr_mono.cycle_last) {
1234 u64 num, max = ULLONG_MAX; 1349 u64 num, max = ULLONG_MAX;
1235 u32 mult = clock->mult; 1350 u32 mult = clock->mult;
1236 u32 shift = clock->shift; 1351 u32 shift = clock->shift;
1237 s64 nsec = 0; 1352 s64 nsec = 0;
1238 1353
1239 cycle_delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, 1354 cycle_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
1240 tk->tkr.mask); 1355 tk->tkr_mono.mask);
1241 1356
1242 /* 1357 /*
1243 * "cycle_delta * mutl" may cause 64 bits overflow, if the 1358 * "cycle_delta * mutl" may cause 64 bits overflow, if the
@@ -1263,7 +1378,9 @@ void timekeeping_resume(void)
1263 __timekeeping_inject_sleeptime(tk, &ts_delta); 1378 __timekeeping_inject_sleeptime(tk, &ts_delta);
1264 1379
1265 /* Re-base the last cycle value */ 1380 /* Re-base the last cycle value */
1266 tk->tkr.cycle_last = cycle_now; 1381 tk->tkr_mono.cycle_last = cycle_now;
1382 tk->tkr_raw.cycle_last = cycle_now;
1383
1267 tk->ntp_error = 0; 1384 tk->ntp_error = 0;
1268 timekeeping_suspended = 0; 1385 timekeeping_suspended = 0;
1269 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); 1386 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
@@ -1416,15 +1533,15 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1416 * 1533 *
1417 * XXX - TODO: Doc ntp_error calculation. 1534 * XXX - TODO: Doc ntp_error calculation.
1418 */ 1535 */
1419 if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) { 1536 if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
1420 /* NTP adjustment caused clocksource mult overflow */ 1537 /* NTP adjustment caused clocksource mult overflow */
1421 WARN_ON_ONCE(1); 1538 WARN_ON_ONCE(1);
1422 return; 1539 return;
1423 } 1540 }
1424 1541
1425 tk->tkr.mult += mult_adj; 1542 tk->tkr_mono.mult += mult_adj;
1426 tk->xtime_interval += interval; 1543 tk->xtime_interval += interval;
1427 tk->tkr.xtime_nsec -= offset; 1544 tk->tkr_mono.xtime_nsec -= offset;
1428 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; 1545 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
1429} 1546}
1430 1547
@@ -1486,13 +1603,13 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1486 tk->ntp_err_mult = 0; 1603 tk->ntp_err_mult = 0;
1487 } 1604 }
1488 1605
1489 if (unlikely(tk->tkr.clock->maxadj && 1606 if (unlikely(tk->tkr_mono.clock->maxadj &&
1490 (abs(tk->tkr.mult - tk->tkr.clock->mult) 1607 (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
1491 > tk->tkr.clock->maxadj))) { 1608 > tk->tkr_mono.clock->maxadj))) {
1492 printk_once(KERN_WARNING 1609 printk_once(KERN_WARNING
1493 "Adjusting %s more than 11%% (%ld vs %ld)\n", 1610 "Adjusting %s more than 11%% (%ld vs %ld)\n",
1494 tk->tkr.clock->name, (long)tk->tkr.mult, 1611 tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
1495 (long)tk->tkr.clock->mult + tk->tkr.clock->maxadj); 1612 (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
1496 } 1613 }
1497 1614
1498 /* 1615 /*
@@ -1509,9 +1626,9 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1509 * We'll correct this error next time through this function, when 1626 * We'll correct this error next time through this function, when
1510 * xtime_nsec is not as small. 1627 * xtime_nsec is not as small.
1511 */ 1628 */
1512 if (unlikely((s64)tk->tkr.xtime_nsec < 0)) { 1629 if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
1513 s64 neg = -(s64)tk->tkr.xtime_nsec; 1630 s64 neg = -(s64)tk->tkr_mono.xtime_nsec;
1514 tk->tkr.xtime_nsec = 0; 1631 tk->tkr_mono.xtime_nsec = 0;
1515 tk->ntp_error += neg << tk->ntp_error_shift; 1632 tk->ntp_error += neg << tk->ntp_error_shift;
1516 } 1633 }
1517} 1634}
@@ -1526,13 +1643,13 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1526 */ 1643 */
1527static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk) 1644static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
1528{ 1645{
1529 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr.shift; 1646 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
1530 unsigned int clock_set = 0; 1647 unsigned int clock_set = 0;
1531 1648
1532 while (tk->tkr.xtime_nsec >= nsecps) { 1649 while (tk->tkr_mono.xtime_nsec >= nsecps) {
1533 int leap; 1650 int leap;
1534 1651
1535 tk->tkr.xtime_nsec -= nsecps; 1652 tk->tkr_mono.xtime_nsec -= nsecps;
1536 tk->xtime_sec++; 1653 tk->xtime_sec++;
1537 1654
1538 /* Figure out if its a leap sec and apply if needed */ 1655 /* Figure out if its a leap sec and apply if needed */
@@ -1577,9 +1694,10 @@ static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1577 1694
1578 /* Accumulate one shifted interval */ 1695 /* Accumulate one shifted interval */
1579 offset -= interval; 1696 offset -= interval;
1580 tk->tkr.cycle_last += interval; 1697 tk->tkr_mono.cycle_last += interval;
1698 tk->tkr_raw.cycle_last += interval;
1581 1699
1582 tk->tkr.xtime_nsec += tk->xtime_interval << shift; 1700 tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
1583 *clock_set |= accumulate_nsecs_to_secs(tk); 1701 *clock_set |= accumulate_nsecs_to_secs(tk);
1584 1702
1585 /* Accumulate raw time */ 1703 /* Accumulate raw time */
@@ -1622,14 +1740,17 @@ void update_wall_time(void)
1622#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET 1740#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
1623 offset = real_tk->cycle_interval; 1741 offset = real_tk->cycle_interval;
1624#else 1742#else
1625 offset = clocksource_delta(tk->tkr.read(tk->tkr.clock), 1743 offset = clocksource_delta(tk->tkr_mono.read(tk->tkr_mono.clock),
1626 tk->tkr.cycle_last, tk->tkr.mask); 1744 tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
1627#endif 1745#endif
1628 1746
1629 /* Check if there's really nothing to do */ 1747 /* Check if there's really nothing to do */
1630 if (offset < real_tk->cycle_interval) 1748 if (offset < real_tk->cycle_interval)
1631 goto out; 1749 goto out;
1632 1750
1751 /* Do some additional sanity checking */
1752 timekeeping_check_update(real_tk, offset);
1753
1633 /* 1754 /*
1634 * With NO_HZ we may have to accumulate many cycle_intervals 1755 * With NO_HZ we may have to accumulate many cycle_intervals
1635 * (think "ticks") worth of time at once. To do this efficiently, 1756 * (think "ticks") worth of time at once. To do this efficiently,
@@ -1784,8 +1905,8 @@ ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot,
1784 do { 1905 do {
1785 seq = read_seqcount_begin(&tk_core.seq); 1906 seq = read_seqcount_begin(&tk_core.seq);
1786 1907
1787 base = tk->tkr.base_mono; 1908 base = tk->tkr_mono.base;
1788 nsecs = tk->tkr.xtime_nsec >> tk->tkr.shift; 1909 nsecs = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
1789 1910
1790 *offs_real = tk->offs_real; 1911 *offs_real = tk->offs_real;
1791 *offs_boot = tk->offs_boot; 1912 *offs_boot = tk->offs_boot;
@@ -1816,8 +1937,8 @@ ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot,
1816 do { 1937 do {
1817 seq = read_seqcount_begin(&tk_core.seq); 1938 seq = read_seqcount_begin(&tk_core.seq);
1818 1939
1819 base = tk->tkr.base_mono; 1940 base = tk->tkr_mono.base;
1820 nsecs = timekeeping_get_ns(&tk->tkr); 1941 nsecs = timekeeping_get_ns(&tk->tkr_mono);
1821 1942
1822 *offs_real = tk->offs_real; 1943 *offs_real = tk->offs_real;
1823 *offs_boot = tk->offs_boot; 1944 *offs_boot = tk->offs_boot;