aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/time')
-rw-r--r--kernel/time/jiffies.c2
-rw-r--r--kernel/time/ntp.c2
-rw-r--r--kernel/time/tick-sched.c1
-rw-r--r--kernel/time/timekeeping.c455
4 files changed, 265 insertions, 195 deletions
diff --git a/kernel/time/jiffies.c b/kernel/time/jiffies.c
index a470154e0408..46da0537c10b 100644
--- a/kernel/time/jiffies.c
+++ b/kernel/time/jiffies.c
@@ -37,7 +37,7 @@
37 * requested HZ value. It is also not recommended 37 * requested HZ value. It is also not recommended
38 * for "tick-less" systems. 38 * for "tick-less" systems.
39 */ 39 */
40#define NSEC_PER_JIFFY ((u32)((((u64)NSEC_PER_SEC)<<8)/ACTHZ)) 40#define NSEC_PER_JIFFY ((u32)((((u64)NSEC_PER_SEC)<<8)/SHIFTED_HZ))
41 41
42/* Since jiffies uses a simple NSEC_PER_JIFFY multiplier 42/* Since jiffies uses a simple NSEC_PER_JIFFY multiplier
43 * conversion, the .shift value could be zero. However 43 * conversion, the .shift value could be zero. However
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index b7fbadc5c973..24174b4d669b 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -28,7 +28,7 @@ DEFINE_SPINLOCK(ntp_lock);
28/* USER_HZ period (usecs): */ 28/* USER_HZ period (usecs): */
29unsigned long tick_usec = TICK_USEC; 29unsigned long tick_usec = TICK_USEC;
30 30
31/* ACTHZ period (nsecs): */ 31/* SHIFTED_HZ period (nsecs): */
32unsigned long tick_nsec; 32unsigned long tick_nsec;
33 33
34static u64 tick_length; 34static u64 tick_length;
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 024540f97f74..3a9e5d5c1091 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -573,6 +573,7 @@ static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
573 tick_do_update_jiffies64(now); 573 tick_do_update_jiffies64(now);
574 update_cpu_load_nohz(); 574 update_cpu_load_nohz();
575 575
576 calc_load_exit_idle();
576 touch_softlockup_watchdog(); 577 touch_softlockup_watchdog();
577 /* 578 /*
578 * Cancel the scheduled timer and restore the tick 579 * Cancel the scheduled timer and restore the tick
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index f045cc50832d..d3b91e75cecd 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -65,14 +65,14 @@ struct timekeeper {
65 * used instead. 65 * used instead.
66 */ 66 */
67 struct timespec wall_to_monotonic; 67 struct timespec wall_to_monotonic;
68 /* time spent in suspend */
69 struct timespec total_sleep_time;
70 /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
71 struct timespec raw_time;
72 /* Offset clock monotonic -> clock realtime */ 68 /* Offset clock monotonic -> clock realtime */
73 ktime_t offs_real; 69 ktime_t offs_real;
70 /* time spent in suspend */
71 struct timespec total_sleep_time;
74 /* Offset clock monotonic -> clock boottime */ 72 /* Offset clock monotonic -> clock boottime */
75 ktime_t offs_boot; 73 ktime_t offs_boot;
74 /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
75 struct timespec raw_time;
76 /* Seqlock for all timekeeper values */ 76 /* Seqlock for all timekeeper values */
77 seqlock_t lock; 77 seqlock_t lock;
78}; 78};
@@ -108,13 +108,39 @@ static struct timespec tk_xtime(struct timekeeper *tk)
108static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts) 108static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
109{ 109{
110 tk->xtime_sec = ts->tv_sec; 110 tk->xtime_sec = ts->tv_sec;
111 tk->xtime_nsec = ts->tv_nsec << tk->shift; 111 tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
112} 112}
113 113
114static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts) 114static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
115{ 115{
116 tk->xtime_sec += ts->tv_sec; 116 tk->xtime_sec += ts->tv_sec;
117 tk->xtime_nsec += ts->tv_nsec << tk->shift; 117 tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
118 tk_normalize_xtime(tk);
119}
120
121static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
122{
123 struct timespec tmp;
124
125 /*
126 * Verify consistency of: offset_real = -wall_to_monotonic
127 * before modifying anything
128 */
129 set_normalized_timespec(&tmp, -tk->wall_to_monotonic.tv_sec,
130 -tk->wall_to_monotonic.tv_nsec);
131 WARN_ON_ONCE(tk->offs_real.tv64 != timespec_to_ktime(tmp).tv64);
132 tk->wall_to_monotonic = wtm;
133 set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
134 tk->offs_real = timespec_to_ktime(tmp);
135}
136
137static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
138{
139 /* Verify consistency before modifying */
140 WARN_ON_ONCE(tk->offs_boot.tv64 != timespec_to_ktime(tk->total_sleep_time).tv64);
141
142 tk->total_sleep_time = t;
143 tk->offs_boot = timespec_to_ktime(t);
118} 144}
119 145
120/** 146/**
@@ -217,14 +243,6 @@ static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
217 return nsec + arch_gettimeoffset(); 243 return nsec + arch_gettimeoffset();
218} 244}
219 245
220static void update_rt_offset(struct timekeeper *tk)
221{
222 struct timespec tmp, *wtm = &tk->wall_to_monotonic;
223
224 set_normalized_timespec(&tmp, -wtm->tv_sec, -wtm->tv_nsec);
225 tk->offs_real = timespec_to_ktime(tmp);
226}
227
228/* must hold write on timekeeper.lock */ 246/* must hold write on timekeeper.lock */
229static void timekeeping_update(struct timekeeper *tk, bool clearntp) 247static void timekeeping_update(struct timekeeper *tk, bool clearntp)
230{ 248{
@@ -234,12 +252,10 @@ static void timekeeping_update(struct timekeeper *tk, bool clearntp)
234 tk->ntp_error = 0; 252 tk->ntp_error = 0;
235 ntp_clear(); 253 ntp_clear();
236 } 254 }
237 update_rt_offset(tk);
238 xt = tk_xtime(tk); 255 xt = tk_xtime(tk);
239 update_vsyscall(&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); 256 update_vsyscall(&xt, &tk->wall_to_monotonic, tk->clock, tk->mult);
240} 257}
241 258
242
243/** 259/**
244 * timekeeping_forward_now - update clock to the current time 260 * timekeeping_forward_now - update clock to the current time
245 * 261 *
@@ -261,7 +277,7 @@ static void timekeeping_forward_now(struct timekeeper *tk)
261 tk->xtime_nsec += cycle_delta * tk->mult; 277 tk->xtime_nsec += cycle_delta * tk->mult;
262 278
263 /* If arch requires, add in gettimeoffset() */ 279 /* If arch requires, add in gettimeoffset() */
264 tk->xtime_nsec += arch_gettimeoffset() << tk->shift; 280 tk->xtime_nsec += (u64)arch_gettimeoffset() << tk->shift;
265 281
266 tk_normalize_xtime(tk); 282 tk_normalize_xtime(tk);
267 283
@@ -277,38 +293,39 @@ static void timekeeping_forward_now(struct timekeeper *tk)
277 */ 293 */
278void getnstimeofday(struct timespec *ts) 294void getnstimeofday(struct timespec *ts)
279{ 295{
296 struct timekeeper *tk = &timekeeper;
280 unsigned long seq; 297 unsigned long seq;
281 s64 nsecs = 0; 298 s64 nsecs = 0;
282 299
283 WARN_ON(timekeeping_suspended); 300 WARN_ON(timekeeping_suspended);
284 301
285 do { 302 do {
286 seq = read_seqbegin(&timekeeper.lock); 303 seq = read_seqbegin(&tk->lock);
287 304
288 ts->tv_sec = timekeeper.xtime_sec; 305 ts->tv_sec = tk->xtime_sec;
289 ts->tv_nsec = timekeeping_get_ns(&timekeeper); 306 nsecs = timekeeping_get_ns(tk);
290 307
291 } while (read_seqretry(&timekeeper.lock, seq)); 308 } while (read_seqretry(&tk->lock, seq));
292 309
310 ts->tv_nsec = 0;
293 timespec_add_ns(ts, nsecs); 311 timespec_add_ns(ts, nsecs);
294} 312}
295EXPORT_SYMBOL(getnstimeofday); 313EXPORT_SYMBOL(getnstimeofday);
296 314
297ktime_t ktime_get(void) 315ktime_t ktime_get(void)
298{ 316{
317 struct timekeeper *tk = &timekeeper;
299 unsigned int seq; 318 unsigned int seq;
300 s64 secs, nsecs; 319 s64 secs, nsecs;
301 320
302 WARN_ON(timekeeping_suspended); 321 WARN_ON(timekeeping_suspended);
303 322
304 do { 323 do {
305 seq = read_seqbegin(&timekeeper.lock); 324 seq = read_seqbegin(&tk->lock);
306 secs = timekeeper.xtime_sec + 325 secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
307 timekeeper.wall_to_monotonic.tv_sec; 326 nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec;
308 nsecs = timekeeping_get_ns(&timekeeper) +
309 timekeeper.wall_to_monotonic.tv_nsec;
310 327
311 } while (read_seqretry(&timekeeper.lock, seq)); 328 } while (read_seqretry(&tk->lock, seq));
312 /* 329 /*
313 * Use ktime_set/ktime_add_ns to create a proper ktime on 330 * Use ktime_set/ktime_add_ns to create a proper ktime on
314 * 32-bit architectures without CONFIG_KTIME_SCALAR. 331 * 32-bit architectures without CONFIG_KTIME_SCALAR.
@@ -327,21 +344,24 @@ EXPORT_SYMBOL_GPL(ktime_get);
327 */ 344 */
328void ktime_get_ts(struct timespec *ts) 345void ktime_get_ts(struct timespec *ts)
329{ 346{
347 struct timekeeper *tk = &timekeeper;
330 struct timespec tomono; 348 struct timespec tomono;
349 s64 nsec;
331 unsigned int seq; 350 unsigned int seq;
332 351
333 WARN_ON(timekeeping_suspended); 352 WARN_ON(timekeeping_suspended);
334 353
335 do { 354 do {
336 seq = read_seqbegin(&timekeeper.lock); 355 seq = read_seqbegin(&tk->lock);
337 ts->tv_sec = timekeeper.xtime_sec; 356 ts->tv_sec = tk->xtime_sec;
338 ts->tv_nsec = timekeeping_get_ns(&timekeeper); 357 nsec = timekeeping_get_ns(tk);
339 tomono = timekeeper.wall_to_monotonic; 358 tomono = tk->wall_to_monotonic;
340 359
341 } while (read_seqretry(&timekeeper.lock, seq)); 360 } while (read_seqretry(&tk->lock, seq));
342 361
343 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, 362 ts->tv_sec += tomono.tv_sec;
344 ts->tv_nsec + tomono.tv_nsec); 363 ts->tv_nsec = 0;
364 timespec_add_ns(ts, nsec + tomono.tv_nsec);
345} 365}
346EXPORT_SYMBOL_GPL(ktime_get_ts); 366EXPORT_SYMBOL_GPL(ktime_get_ts);
347 367
@@ -358,22 +378,23 @@ EXPORT_SYMBOL_GPL(ktime_get_ts);
358 */ 378 */
359void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real) 379void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
360{ 380{
381 struct timekeeper *tk = &timekeeper;
361 unsigned long seq; 382 unsigned long seq;
362 s64 nsecs_raw, nsecs_real; 383 s64 nsecs_raw, nsecs_real;
363 384
364 WARN_ON_ONCE(timekeeping_suspended); 385 WARN_ON_ONCE(timekeeping_suspended);
365 386
366 do { 387 do {
367 seq = read_seqbegin(&timekeeper.lock); 388 seq = read_seqbegin(&tk->lock);
368 389
369 *ts_raw = timekeeper.raw_time; 390 *ts_raw = tk->raw_time;
370 ts_real->tv_sec = timekeeper.xtime_sec; 391 ts_real->tv_sec = tk->xtime_sec;
371 ts_real->tv_nsec = 0; 392 ts_real->tv_nsec = 0;
372 393
373 nsecs_raw = timekeeping_get_ns_raw(&timekeeper); 394 nsecs_raw = timekeeping_get_ns_raw(tk);
374 nsecs_real = timekeeping_get_ns(&timekeeper); 395 nsecs_real = timekeeping_get_ns(tk);
375 396
376 } while (read_seqretry(&timekeeper.lock, seq)); 397 } while (read_seqretry(&tk->lock, seq));
377 398
378 timespec_add_ns(ts_raw, nsecs_raw); 399 timespec_add_ns(ts_raw, nsecs_raw);
379 timespec_add_ns(ts_real, nsecs_real); 400 timespec_add_ns(ts_real, nsecs_real);
@@ -406,28 +427,28 @@ EXPORT_SYMBOL(do_gettimeofday);
406 */ 427 */
407int do_settimeofday(const struct timespec *tv) 428int do_settimeofday(const struct timespec *tv)
408{ 429{
430 struct timekeeper *tk = &timekeeper;
409 struct timespec ts_delta, xt; 431 struct timespec ts_delta, xt;
410 unsigned long flags; 432 unsigned long flags;
411 433
412 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) 434 if (!timespec_valid_strict(tv))
413 return -EINVAL; 435 return -EINVAL;
414 436
415 write_seqlock_irqsave(&timekeeper.lock, flags); 437 write_seqlock_irqsave(&tk->lock, flags);
416 438
417 timekeeping_forward_now(&timekeeper); 439 timekeeping_forward_now(tk);
418 440
419 xt = tk_xtime(&timekeeper); 441 xt = tk_xtime(tk);
420 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec; 442 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
421 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec; 443 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
422 444
423 timekeeper.wall_to_monotonic = 445 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, ts_delta));
424 timespec_sub(timekeeper.wall_to_monotonic, ts_delta);
425 446
426 tk_set_xtime(&timekeeper, tv); 447 tk_set_xtime(tk, tv);
427 448
428 timekeeping_update(&timekeeper, true); 449 timekeeping_update(tk, true);
429 450
430 write_sequnlock_irqrestore(&timekeeper.lock, flags); 451 write_sequnlock_irqrestore(&tk->lock, flags);
431 452
432 /* signal hrtimers about time change */ 453 /* signal hrtimers about time change */
433 clock_was_set(); 454 clock_was_set();
@@ -436,7 +457,6 @@ int do_settimeofday(const struct timespec *tv)
436} 457}
437EXPORT_SYMBOL(do_settimeofday); 458EXPORT_SYMBOL(do_settimeofday);
438 459
439
440/** 460/**
441 * timekeeping_inject_offset - Adds or subtracts from the current time. 461 * timekeeping_inject_offset - Adds or subtracts from the current time.
442 * @tv: pointer to the timespec variable containing the offset 462 * @tv: pointer to the timespec variable containing the offset
@@ -445,28 +465,37 @@ EXPORT_SYMBOL(do_settimeofday);
445 */ 465 */
446int timekeeping_inject_offset(struct timespec *ts) 466int timekeeping_inject_offset(struct timespec *ts)
447{ 467{
468 struct timekeeper *tk = &timekeeper;
448 unsigned long flags; 469 unsigned long flags;
470 struct timespec tmp;
471 int ret = 0;
449 472
450 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) 473 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
451 return -EINVAL; 474 return -EINVAL;
452 475
453 write_seqlock_irqsave(&timekeeper.lock, flags); 476 write_seqlock_irqsave(&tk->lock, flags);
454 477
455 timekeeping_forward_now(&timekeeper); 478 timekeeping_forward_now(tk);
456 479
480 /* Make sure the proposed value is valid */
481 tmp = timespec_add(tk_xtime(tk), *ts);
482 if (!timespec_valid_strict(&tmp)) {
483 ret = -EINVAL;
484 goto error;
485 }
457 486
458 tk_xtime_add(&timekeeper, ts); 487 tk_xtime_add(tk, ts);
459 timekeeper.wall_to_monotonic = 488 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts));
460 timespec_sub(timekeeper.wall_to_monotonic, *ts);
461 489
462 timekeeping_update(&timekeeper, true); 490error: /* even if we error out, we forwarded the time, so call update */
491 timekeeping_update(tk, true);
463 492
464 write_sequnlock_irqrestore(&timekeeper.lock, flags); 493 write_sequnlock_irqrestore(&tk->lock, flags);
465 494
466 /* signal hrtimers about time change */ 495 /* signal hrtimers about time change */
467 clock_was_set(); 496 clock_was_set();
468 497
469 return 0; 498 return ret;
470} 499}
471EXPORT_SYMBOL(timekeeping_inject_offset); 500EXPORT_SYMBOL(timekeeping_inject_offset);
472 501
@@ -477,23 +506,24 @@ EXPORT_SYMBOL(timekeeping_inject_offset);
477 */ 506 */
478static int change_clocksource(void *data) 507static int change_clocksource(void *data)
479{ 508{
509 struct timekeeper *tk = &timekeeper;
480 struct clocksource *new, *old; 510 struct clocksource *new, *old;
481 unsigned long flags; 511 unsigned long flags;
482 512
483 new = (struct clocksource *) data; 513 new = (struct clocksource *) data;
484 514
485 write_seqlock_irqsave(&timekeeper.lock, flags); 515 write_seqlock_irqsave(&tk->lock, flags);
486 516
487 timekeeping_forward_now(&timekeeper); 517 timekeeping_forward_now(tk);
488 if (!new->enable || new->enable(new) == 0) { 518 if (!new->enable || new->enable(new) == 0) {
489 old = timekeeper.clock; 519 old = tk->clock;
490 tk_setup_internals(&timekeeper, new); 520 tk_setup_internals(tk, new);
491 if (old->disable) 521 if (old->disable)
492 old->disable(old); 522 old->disable(old);
493 } 523 }
494 timekeeping_update(&timekeeper, true); 524 timekeeping_update(tk, true);
495 525
496 write_sequnlock_irqrestore(&timekeeper.lock, flags); 526 write_sequnlock_irqrestore(&tk->lock, flags);
497 527
498 return 0; 528 return 0;
499} 529}
@@ -507,7 +537,9 @@ static int change_clocksource(void *data)
507 */ 537 */
508void timekeeping_notify(struct clocksource *clock) 538void timekeeping_notify(struct clocksource *clock)
509{ 539{
510 if (timekeeper.clock == clock) 540 struct timekeeper *tk = &timekeeper;
541
542 if (tk->clock == clock)
511 return; 543 return;
512 stop_machine(change_clocksource, clock, NULL); 544 stop_machine(change_clocksource, clock, NULL);
513 tick_clock_notify(); 545 tick_clock_notify();
@@ -536,35 +568,36 @@ EXPORT_SYMBOL_GPL(ktime_get_real);
536 */ 568 */
537void getrawmonotonic(struct timespec *ts) 569void getrawmonotonic(struct timespec *ts)
538{ 570{
571 struct timekeeper *tk = &timekeeper;
539 unsigned long seq; 572 unsigned long seq;
540 s64 nsecs; 573 s64 nsecs;
541 574
542 do { 575 do {
543 seq = read_seqbegin(&timekeeper.lock); 576 seq = read_seqbegin(&tk->lock);
544 nsecs = timekeeping_get_ns_raw(&timekeeper); 577 nsecs = timekeeping_get_ns_raw(tk);
545 *ts = timekeeper.raw_time; 578 *ts = tk->raw_time;
546 579
547 } while (read_seqretry(&timekeeper.lock, seq)); 580 } while (read_seqretry(&tk->lock, seq));
548 581
549 timespec_add_ns(ts, nsecs); 582 timespec_add_ns(ts, nsecs);
550} 583}
551EXPORT_SYMBOL(getrawmonotonic); 584EXPORT_SYMBOL(getrawmonotonic);
552 585
553
554/** 586/**
555 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres 587 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
556 */ 588 */
557int timekeeping_valid_for_hres(void) 589int timekeeping_valid_for_hres(void)
558{ 590{
591 struct timekeeper *tk = &timekeeper;
559 unsigned long seq; 592 unsigned long seq;
560 int ret; 593 int ret;
561 594
562 do { 595 do {
563 seq = read_seqbegin(&timekeeper.lock); 596 seq = read_seqbegin(&tk->lock);
564 597
565 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; 598 ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
566 599
567 } while (read_seqretry(&timekeeper.lock, seq)); 600 } while (read_seqretry(&tk->lock, seq));
568 601
569 return ret; 602 return ret;
570} 603}
@@ -574,15 +607,16 @@ int timekeeping_valid_for_hres(void)
574 */ 607 */
575u64 timekeeping_max_deferment(void) 608u64 timekeeping_max_deferment(void)
576{ 609{
610 struct timekeeper *tk = &timekeeper;
577 unsigned long seq; 611 unsigned long seq;
578 u64 ret; 612 u64 ret;
579 613
580 do { 614 do {
581 seq = read_seqbegin(&timekeeper.lock); 615 seq = read_seqbegin(&tk->lock);
582 616
583 ret = timekeeper.clock->max_idle_ns; 617 ret = tk->clock->max_idle_ns;
584 618
585 } while (read_seqretry(&timekeeper.lock, seq)); 619 } while (read_seqretry(&tk->lock, seq));
586 620
587 return ret; 621 return ret;
588} 622}
@@ -622,46 +656,56 @@ void __attribute__((weak)) read_boot_clock(struct timespec *ts)
622 */ 656 */
623void __init timekeeping_init(void) 657void __init timekeeping_init(void)
624{ 658{
659 struct timekeeper *tk = &timekeeper;
625 struct clocksource *clock; 660 struct clocksource *clock;
626 unsigned long flags; 661 unsigned long flags;
627 struct timespec now, boot; 662 struct timespec now, boot, tmp;
628 663
629 read_persistent_clock(&now); 664 read_persistent_clock(&now);
665 if (!timespec_valid_strict(&now)) {
666 pr_warn("WARNING: Persistent clock returned invalid value!\n"
667 " Check your CMOS/BIOS settings.\n");
668 now.tv_sec = 0;
669 now.tv_nsec = 0;
670 }
671
630 read_boot_clock(&boot); 672 read_boot_clock(&boot);
673 if (!timespec_valid_strict(&boot)) {
674 pr_warn("WARNING: Boot clock returned invalid value!\n"
675 " Check your CMOS/BIOS settings.\n");
676 boot.tv_sec = 0;
677 boot.tv_nsec = 0;
678 }
631 679
632 seqlock_init(&timekeeper.lock); 680 seqlock_init(&tk->lock);
633 681
634 ntp_init(); 682 ntp_init();
635 683
636 write_seqlock_irqsave(&timekeeper.lock, flags); 684 write_seqlock_irqsave(&tk->lock, flags);
637 clock = clocksource_default_clock(); 685 clock = clocksource_default_clock();
638 if (clock->enable) 686 if (clock->enable)
639 clock->enable(clock); 687 clock->enable(clock);
640 tk_setup_internals(&timekeeper, clock); 688 tk_setup_internals(tk, clock);
641 689
642 tk_set_xtime(&timekeeper, &now); 690 tk_set_xtime(tk, &now);
643 timekeeper.raw_time.tv_sec = 0; 691 tk->raw_time.tv_sec = 0;
644 timekeeper.raw_time.tv_nsec = 0; 692 tk->raw_time.tv_nsec = 0;
645 if (boot.tv_sec == 0 && boot.tv_nsec == 0) 693 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
646 boot = tk_xtime(&timekeeper); 694 boot = tk_xtime(tk);
647 695
648 set_normalized_timespec(&timekeeper.wall_to_monotonic, 696 set_normalized_timespec(&tmp, -boot.tv_sec, -boot.tv_nsec);
649 -boot.tv_sec, -boot.tv_nsec); 697 tk_set_wall_to_mono(tk, tmp);
650 update_rt_offset(&timekeeper); 698
651 timekeeper.total_sleep_time.tv_sec = 0; 699 tmp.tv_sec = 0;
652 timekeeper.total_sleep_time.tv_nsec = 0; 700 tmp.tv_nsec = 0;
653 write_sequnlock_irqrestore(&timekeeper.lock, flags); 701 tk_set_sleep_time(tk, tmp);
702
703 write_sequnlock_irqrestore(&tk->lock, flags);
654} 704}
655 705
656/* time in seconds when suspend began */ 706/* time in seconds when suspend began */
657static struct timespec timekeeping_suspend_time; 707static struct timespec timekeeping_suspend_time;
658 708
659static void update_sleep_time(struct timespec t)
660{
661 timekeeper.total_sleep_time = t;
662 timekeeper.offs_boot = timespec_to_ktime(t);
663}
664
665/** 709/**
666 * __timekeeping_inject_sleeptime - Internal function to add sleep interval 710 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
667 * @delta: pointer to a timespec delta value 711 * @delta: pointer to a timespec delta value
@@ -672,18 +716,16 @@ static void update_sleep_time(struct timespec t)
672static void __timekeeping_inject_sleeptime(struct timekeeper *tk, 716static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
673 struct timespec *delta) 717 struct timespec *delta)
674{ 718{
675 if (!timespec_valid(delta)) { 719 if (!timespec_valid_strict(delta)) {
676 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid " 720 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
677 "sleep delta value!\n"); 721 "sleep delta value!\n");
678 return; 722 return;
679 } 723 }
680
681 tk_xtime_add(tk, delta); 724 tk_xtime_add(tk, delta);
682 tk->wall_to_monotonic = timespec_sub(tk->wall_to_monotonic, *delta); 725 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta));
683 update_sleep_time(timespec_add(tk->total_sleep_time, *delta)); 726 tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta));
684} 727}
685 728
686
687/** 729/**
688 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values 730 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
689 * @delta: pointer to a timespec delta value 731 * @delta: pointer to a timespec delta value
@@ -696,6 +738,7 @@ static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
696 */ 738 */
697void timekeeping_inject_sleeptime(struct timespec *delta) 739void timekeeping_inject_sleeptime(struct timespec *delta)
698{ 740{
741 struct timekeeper *tk = &timekeeper;
699 unsigned long flags; 742 unsigned long flags;
700 struct timespec ts; 743 struct timespec ts;
701 744
@@ -704,21 +747,20 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
704 if (!(ts.tv_sec == 0 && ts.tv_nsec == 0)) 747 if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
705 return; 748 return;
706 749
707 write_seqlock_irqsave(&timekeeper.lock, flags); 750 write_seqlock_irqsave(&tk->lock, flags);
708 751
709 timekeeping_forward_now(&timekeeper); 752 timekeeping_forward_now(tk);
710 753
711 __timekeeping_inject_sleeptime(&timekeeper, delta); 754 __timekeeping_inject_sleeptime(tk, delta);
712 755
713 timekeeping_update(&timekeeper, true); 756 timekeeping_update(tk, true);
714 757
715 write_sequnlock_irqrestore(&timekeeper.lock, flags); 758 write_sequnlock_irqrestore(&tk->lock, flags);
716 759
717 /* signal hrtimers about time change */ 760 /* signal hrtimers about time change */
718 clock_was_set(); 761 clock_was_set();
719} 762}
720 763
721
722/** 764/**
723 * timekeeping_resume - Resumes the generic timekeeping subsystem. 765 * timekeeping_resume - Resumes the generic timekeeping subsystem.
724 * 766 *
@@ -728,6 +770,7 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
728 */ 770 */
729static void timekeeping_resume(void) 771static void timekeeping_resume(void)
730{ 772{
773 struct timekeeper *tk = &timekeeper;
731 unsigned long flags; 774 unsigned long flags;
732 struct timespec ts; 775 struct timespec ts;
733 776
@@ -735,18 +778,18 @@ static void timekeeping_resume(void)
735 778
736 clocksource_resume(); 779 clocksource_resume();
737 780
738 write_seqlock_irqsave(&timekeeper.lock, flags); 781 write_seqlock_irqsave(&tk->lock, flags);
739 782
740 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { 783 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
741 ts = timespec_sub(ts, timekeeping_suspend_time); 784 ts = timespec_sub(ts, timekeeping_suspend_time);
742 __timekeeping_inject_sleeptime(&timekeeper, &ts); 785 __timekeeping_inject_sleeptime(tk, &ts);
743 } 786 }
744 /* re-base the last cycle value */ 787 /* re-base the last cycle value */
745 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock); 788 tk->clock->cycle_last = tk->clock->read(tk->clock);
746 timekeeper.ntp_error = 0; 789 tk->ntp_error = 0;
747 timekeeping_suspended = 0; 790 timekeeping_suspended = 0;
748 timekeeping_update(&timekeeper, false); 791 timekeeping_update(tk, false);
749 write_sequnlock_irqrestore(&timekeeper.lock, flags); 792 write_sequnlock_irqrestore(&tk->lock, flags);
750 793
751 touch_softlockup_watchdog(); 794 touch_softlockup_watchdog();
752 795
@@ -758,14 +801,15 @@ static void timekeeping_resume(void)
758 801
759static int timekeeping_suspend(void) 802static int timekeeping_suspend(void)
760{ 803{
804 struct timekeeper *tk = &timekeeper;
761 unsigned long flags; 805 unsigned long flags;
762 struct timespec delta, delta_delta; 806 struct timespec delta, delta_delta;
763 static struct timespec old_delta; 807 static struct timespec old_delta;
764 808
765 read_persistent_clock(&timekeeping_suspend_time); 809 read_persistent_clock(&timekeeping_suspend_time);
766 810
767 write_seqlock_irqsave(&timekeeper.lock, flags); 811 write_seqlock_irqsave(&tk->lock, flags);
768 timekeeping_forward_now(&timekeeper); 812 timekeeping_forward_now(tk);
769 timekeeping_suspended = 1; 813 timekeeping_suspended = 1;
770 814
771 /* 815 /*
@@ -774,7 +818,7 @@ static int timekeeping_suspend(void)
774 * try to compensate so the difference in system time 818 * try to compensate so the difference in system time
775 * and persistent_clock time stays close to constant. 819 * and persistent_clock time stays close to constant.
776 */ 820 */
777 delta = timespec_sub(tk_xtime(&timekeeper), timekeeping_suspend_time); 821 delta = timespec_sub(tk_xtime(tk), timekeeping_suspend_time);
778 delta_delta = timespec_sub(delta, old_delta); 822 delta_delta = timespec_sub(delta, old_delta);
779 if (abs(delta_delta.tv_sec) >= 2) { 823 if (abs(delta_delta.tv_sec) >= 2) {
780 /* 824 /*
@@ -787,7 +831,7 @@ static int timekeeping_suspend(void)
787 timekeeping_suspend_time = 831 timekeeping_suspend_time =
788 timespec_add(timekeeping_suspend_time, delta_delta); 832 timespec_add(timekeeping_suspend_time, delta_delta);
789 } 833 }
790 write_sequnlock_irqrestore(&timekeeper.lock, flags); 834 write_sequnlock_irqrestore(&tk->lock, flags);
791 835
792 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); 836 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
793 clocksource_suspend(); 837 clocksource_suspend();
@@ -898,27 +942,29 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
898 * the error. This causes the likely below to be unlikely. 942 * the error. This causes the likely below to be unlikely.
899 * 943 *
900 * The proper fix is to avoid rounding up by using 944 * The proper fix is to avoid rounding up by using
901 * the high precision timekeeper.xtime_nsec instead of 945 * the high precision tk->xtime_nsec instead of
902 * xtime.tv_nsec everywhere. Fixing this will take some 946 * xtime.tv_nsec everywhere. Fixing this will take some
903 * time. 947 * time.
904 */ 948 */
905 if (likely(error <= interval)) 949 if (likely(error <= interval))
906 adj = 1; 950 adj = 1;
907 else 951 else
908 adj = timekeeping_bigadjust(tk, error, &interval, 952 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
909 &offset); 953 } else {
910 } else if (error < -interval) { 954 if (error < -interval) {
911 /* See comment above, this is just switched for the negative */ 955 /* See comment above, this is just switched for the negative */
912 error >>= 2; 956 error >>= 2;
913 if (likely(error >= -interval)) { 957 if (likely(error >= -interval)) {
914 adj = -1; 958 adj = -1;
915 interval = -interval; 959 interval = -interval;
916 offset = -offset; 960 offset = -offset;
917 } else 961 } else {
918 adj = timekeeping_bigadjust(tk, error, &interval, 962 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
919 &offset); 963 }
920 } else 964 } else {
921 return; 965 goto out_adjust;
966 }
967 }
922 968
923 if (unlikely(tk->clock->maxadj && 969 if (unlikely(tk->clock->maxadj &&
924 (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) { 970 (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) {
@@ -981,6 +1027,7 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
981 tk->xtime_nsec -= offset; 1027 tk->xtime_nsec -= offset;
982 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; 1028 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
983 1029
1030out_adjust:
984 /* 1031 /*
985 * It may be possible that when we entered this function, xtime_nsec 1032 * It may be possible that when we entered this function, xtime_nsec
986 * was very small. Further, if we're slightly speeding the clocksource 1033 * was very small. Further, if we're slightly speeding the clocksource
@@ -1003,7 +1050,6 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1003 1050
1004} 1051}
1005 1052
1006
1007/** 1053/**
1008 * accumulate_nsecs_to_secs - Accumulates nsecs into secs 1054 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1009 * 1055 *
@@ -1024,15 +1070,21 @@ static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
1024 1070
1025 /* Figure out if its a leap sec and apply if needed */ 1071 /* Figure out if its a leap sec and apply if needed */
1026 leap = second_overflow(tk->xtime_sec); 1072 leap = second_overflow(tk->xtime_sec);
1027 tk->xtime_sec += leap; 1073 if (unlikely(leap)) {
1028 tk->wall_to_monotonic.tv_sec -= leap; 1074 struct timespec ts;
1029 if (leap) 1075
1030 clock_was_set_delayed(); 1076 tk->xtime_sec += leap;
1077
1078 ts.tv_sec = leap;
1079 ts.tv_nsec = 0;
1080 tk_set_wall_to_mono(tk,
1081 timespec_sub(tk->wall_to_monotonic, ts));
1031 1082
1083 clock_was_set_delayed();
1084 }
1032 } 1085 }
1033} 1086}
1034 1087
1035
1036/** 1088/**
1037 * logarithmic_accumulation - shifted accumulation of cycles 1089 * logarithmic_accumulation - shifted accumulation of cycles
1038 * 1090 *
@@ -1076,7 +1128,6 @@ static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1076 return offset; 1128 return offset;
1077} 1129}
1078 1130
1079
1080/** 1131/**
1081 * update_wall_time - Uses the current clocksource to increment the wall time 1132 * update_wall_time - Uses the current clocksource to increment the wall time
1082 * 1133 *
@@ -1084,25 +1135,30 @@ static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1084static void update_wall_time(void) 1135static void update_wall_time(void)
1085{ 1136{
1086 struct clocksource *clock; 1137 struct clocksource *clock;
1138 struct timekeeper *tk = &timekeeper;
1087 cycle_t offset; 1139 cycle_t offset;
1088 int shift = 0, maxshift; 1140 int shift = 0, maxshift;
1089 unsigned long flags; 1141 unsigned long flags;
1090 s64 remainder; 1142 s64 remainder;
1091 1143
1092 write_seqlock_irqsave(&timekeeper.lock, flags); 1144 write_seqlock_irqsave(&tk->lock, flags);
1093 1145
1094 /* Make sure we're fully resumed: */ 1146 /* Make sure we're fully resumed: */
1095 if (unlikely(timekeeping_suspended)) 1147 if (unlikely(timekeeping_suspended))
1096 goto out; 1148 goto out;
1097 1149
1098 clock = timekeeper.clock; 1150 clock = tk->clock;
1099 1151
1100#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET 1152#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
1101 offset = timekeeper.cycle_interval; 1153 offset = tk->cycle_interval;
1102#else 1154#else
1103 offset = (clock->read(clock) - clock->cycle_last) & clock->mask; 1155 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
1104#endif 1156#endif
1105 1157
1158 /* Check if there's really nothing to do */
1159 if (offset < tk->cycle_interval)
1160 goto out;
1161
1106 /* 1162 /*
1107 * With NO_HZ we may have to accumulate many cycle_intervals 1163 * With NO_HZ we may have to accumulate many cycle_intervals
1108 * (think "ticks") worth of time at once. To do this efficiently, 1164 * (think "ticks") worth of time at once. To do this efficiently,
@@ -1111,19 +1167,19 @@ static void update_wall_time(void)
1111 * chunk in one go, and then try to consume the next smaller 1167 * chunk in one go, and then try to consume the next smaller
1112 * doubled multiple. 1168 * doubled multiple.
1113 */ 1169 */
1114 shift = ilog2(offset) - ilog2(timekeeper.cycle_interval); 1170 shift = ilog2(offset) - ilog2(tk->cycle_interval);
1115 shift = max(0, shift); 1171 shift = max(0, shift);
1116 /* Bound shift to one less than what overflows tick_length */ 1172 /* Bound shift to one less than what overflows tick_length */
1117 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; 1173 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
1118 shift = min(shift, maxshift); 1174 shift = min(shift, maxshift);
1119 while (offset >= timekeeper.cycle_interval) { 1175 while (offset >= tk->cycle_interval) {
1120 offset = logarithmic_accumulation(&timekeeper, offset, shift); 1176 offset = logarithmic_accumulation(tk, offset, shift);
1121 if(offset < timekeeper.cycle_interval<<shift) 1177 if (offset < tk->cycle_interval<<shift)
1122 shift--; 1178 shift--;
1123 } 1179 }
1124 1180
1125 /* correct the clock when NTP error is too big */ 1181 /* correct the clock when NTP error is too big */
1126 timekeeping_adjust(&timekeeper, offset); 1182 timekeeping_adjust(tk, offset);
1127 1183
1128 1184
1129 /* 1185 /*
@@ -1135,21 +1191,21 @@ static void update_wall_time(void)
1135 * the vsyscall implementations are converted to use xtime_nsec 1191 * the vsyscall implementations are converted to use xtime_nsec
1136 * (shifted nanoseconds), this can be killed. 1192 * (shifted nanoseconds), this can be killed.
1137 */ 1193 */
1138 remainder = timekeeper.xtime_nsec & ((1 << timekeeper.shift) - 1); 1194 remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
1139 timekeeper.xtime_nsec -= remainder; 1195 tk->xtime_nsec -= remainder;
1140 timekeeper.xtime_nsec += 1 << timekeeper.shift; 1196 tk->xtime_nsec += 1ULL << tk->shift;
1141 timekeeper.ntp_error += remainder << timekeeper.ntp_error_shift; 1197 tk->ntp_error += remainder << tk->ntp_error_shift;
1142 1198
1143 /* 1199 /*
1144 * Finally, make sure that after the rounding 1200 * Finally, make sure that after the rounding
1145 * xtime_nsec isn't larger than NSEC_PER_SEC 1201 * xtime_nsec isn't larger than NSEC_PER_SEC
1146 */ 1202 */
1147 accumulate_nsecs_to_secs(&timekeeper); 1203 accumulate_nsecs_to_secs(tk);
1148 1204
1149 timekeeping_update(&timekeeper, false); 1205 timekeeping_update(tk, false);
1150 1206
1151out: 1207out:
1152 write_sequnlock_irqrestore(&timekeeper.lock, flags); 1208 write_sequnlock_irqrestore(&tk->lock, flags);
1153 1209
1154} 1210}
1155 1211
@@ -1166,18 +1222,18 @@ out:
1166 */ 1222 */
1167void getboottime(struct timespec *ts) 1223void getboottime(struct timespec *ts)
1168{ 1224{
1225 struct timekeeper *tk = &timekeeper;
1169 struct timespec boottime = { 1226 struct timespec boottime = {
1170 .tv_sec = timekeeper.wall_to_monotonic.tv_sec + 1227 .tv_sec = tk->wall_to_monotonic.tv_sec +
1171 timekeeper.total_sleep_time.tv_sec, 1228 tk->total_sleep_time.tv_sec,
1172 .tv_nsec = timekeeper.wall_to_monotonic.tv_nsec + 1229 .tv_nsec = tk->wall_to_monotonic.tv_nsec +
1173 timekeeper.total_sleep_time.tv_nsec 1230 tk->total_sleep_time.tv_nsec
1174 }; 1231 };
1175 1232
1176 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec); 1233 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
1177} 1234}
1178EXPORT_SYMBOL_GPL(getboottime); 1235EXPORT_SYMBOL_GPL(getboottime);
1179 1236
1180
1181/** 1237/**
1182 * get_monotonic_boottime - Returns monotonic time since boot 1238 * get_monotonic_boottime - Returns monotonic time since boot
1183 * @ts: pointer to the timespec to be set 1239 * @ts: pointer to the timespec to be set
@@ -1189,22 +1245,25 @@ EXPORT_SYMBOL_GPL(getboottime);
1189 */ 1245 */
1190void get_monotonic_boottime(struct timespec *ts) 1246void get_monotonic_boottime(struct timespec *ts)
1191{ 1247{
1248 struct timekeeper *tk = &timekeeper;
1192 struct timespec tomono, sleep; 1249 struct timespec tomono, sleep;
1250 s64 nsec;
1193 unsigned int seq; 1251 unsigned int seq;
1194 1252
1195 WARN_ON(timekeeping_suspended); 1253 WARN_ON(timekeeping_suspended);
1196 1254
1197 do { 1255 do {
1198 seq = read_seqbegin(&timekeeper.lock); 1256 seq = read_seqbegin(&tk->lock);
1199 ts->tv_sec = timekeeper.xtime_sec; 1257 ts->tv_sec = tk->xtime_sec;
1200 ts->tv_nsec = timekeeping_get_ns(&timekeeper); 1258 nsec = timekeeping_get_ns(tk);
1201 tomono = timekeeper.wall_to_monotonic; 1259 tomono = tk->wall_to_monotonic;
1202 sleep = timekeeper.total_sleep_time; 1260 sleep = tk->total_sleep_time;
1203 1261
1204 } while (read_seqretry(&timekeeper.lock, seq)); 1262 } while (read_seqretry(&tk->lock, seq));
1205 1263
1206 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec, 1264 ts->tv_sec += tomono.tv_sec + sleep.tv_sec;
1207 ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec); 1265 ts->tv_nsec = 0;
1266 timespec_add_ns(ts, nsec + tomono.tv_nsec + sleep.tv_nsec);
1208} 1267}
1209EXPORT_SYMBOL_GPL(get_monotonic_boottime); 1268EXPORT_SYMBOL_GPL(get_monotonic_boottime);
1210 1269
@@ -1231,31 +1290,38 @@ EXPORT_SYMBOL_GPL(ktime_get_boottime);
1231 */ 1290 */
1232void monotonic_to_bootbased(struct timespec *ts) 1291void monotonic_to_bootbased(struct timespec *ts)
1233{ 1292{
1234 *ts = timespec_add(*ts, timekeeper.total_sleep_time); 1293 struct timekeeper *tk = &timekeeper;
1294
1295 *ts = timespec_add(*ts, tk->total_sleep_time);
1235} 1296}
1236EXPORT_SYMBOL_GPL(monotonic_to_bootbased); 1297EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
1237 1298
1238unsigned long get_seconds(void) 1299unsigned long get_seconds(void)
1239{ 1300{
1240 return timekeeper.xtime_sec; 1301 struct timekeeper *tk = &timekeeper;
1302
1303 return tk->xtime_sec;
1241} 1304}
1242EXPORT_SYMBOL(get_seconds); 1305EXPORT_SYMBOL(get_seconds);
1243 1306
1244struct timespec __current_kernel_time(void) 1307struct timespec __current_kernel_time(void)
1245{ 1308{
1246 return tk_xtime(&timekeeper); 1309 struct timekeeper *tk = &timekeeper;
1310
1311 return tk_xtime(tk);
1247} 1312}
1248 1313
1249struct timespec current_kernel_time(void) 1314struct timespec current_kernel_time(void)
1250{ 1315{
1316 struct timekeeper *tk = &timekeeper;
1251 struct timespec now; 1317 struct timespec now;
1252 unsigned long seq; 1318 unsigned long seq;
1253 1319
1254 do { 1320 do {
1255 seq = read_seqbegin(&timekeeper.lock); 1321 seq = read_seqbegin(&tk->lock);
1256 1322
1257 now = tk_xtime(&timekeeper); 1323 now = tk_xtime(tk);
1258 } while (read_seqretry(&timekeeper.lock, seq)); 1324 } while (read_seqretry(&tk->lock, seq));
1259 1325
1260 return now; 1326 return now;
1261} 1327}
@@ -1263,15 +1329,16 @@ EXPORT_SYMBOL(current_kernel_time);
1263 1329
1264struct timespec get_monotonic_coarse(void) 1330struct timespec get_monotonic_coarse(void)
1265{ 1331{
1332 struct timekeeper *tk = &timekeeper;
1266 struct timespec now, mono; 1333 struct timespec now, mono;
1267 unsigned long seq; 1334 unsigned long seq;
1268 1335
1269 do { 1336 do {
1270 seq = read_seqbegin(&timekeeper.lock); 1337 seq = read_seqbegin(&tk->lock);
1271 1338
1272 now = tk_xtime(&timekeeper); 1339 now = tk_xtime(tk);
1273 mono = timekeeper.wall_to_monotonic; 1340 mono = tk->wall_to_monotonic;
1274 } while (read_seqretry(&timekeeper.lock, seq)); 1341 } while (read_seqretry(&tk->lock, seq));
1275 1342
1276 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec, 1343 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1277 now.tv_nsec + mono.tv_nsec); 1344 now.tv_nsec + mono.tv_nsec);
@@ -1300,14 +1367,15 @@ void do_timer(unsigned long ticks)
1300void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim, 1367void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1301 struct timespec *wtom, struct timespec *sleep) 1368 struct timespec *wtom, struct timespec *sleep)
1302{ 1369{
1370 struct timekeeper *tk = &timekeeper;
1303 unsigned long seq; 1371 unsigned long seq;
1304 1372
1305 do { 1373 do {
1306 seq = read_seqbegin(&timekeeper.lock); 1374 seq = read_seqbegin(&tk->lock);
1307 *xtim = tk_xtime(&timekeeper); 1375 *xtim = tk_xtime(tk);
1308 *wtom = timekeeper.wall_to_monotonic; 1376 *wtom = tk->wall_to_monotonic;
1309 *sleep = timekeeper.total_sleep_time; 1377 *sleep = tk->total_sleep_time;
1310 } while (read_seqretry(&timekeeper.lock, seq)); 1378 } while (read_seqretry(&tk->lock, seq));
1311} 1379}
1312 1380
1313#ifdef CONFIG_HIGH_RES_TIMERS 1381#ifdef CONFIG_HIGH_RES_TIMERS
@@ -1321,19 +1389,20 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1321 */ 1389 */
1322ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot) 1390ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
1323{ 1391{
1392 struct timekeeper *tk = &timekeeper;
1324 ktime_t now; 1393 ktime_t now;
1325 unsigned int seq; 1394 unsigned int seq;
1326 u64 secs, nsecs; 1395 u64 secs, nsecs;
1327 1396
1328 do { 1397 do {
1329 seq = read_seqbegin(&timekeeper.lock); 1398 seq = read_seqbegin(&tk->lock);
1330 1399
1331 secs = timekeeper.xtime_sec; 1400 secs = tk->xtime_sec;
1332 nsecs = timekeeping_get_ns(&timekeeper); 1401 nsecs = timekeeping_get_ns(tk);
1333 1402
1334 *offs_real = timekeeper.offs_real; 1403 *offs_real = tk->offs_real;
1335 *offs_boot = timekeeper.offs_boot; 1404 *offs_boot = tk->offs_boot;
1336 } while (read_seqretry(&timekeeper.lock, seq)); 1405 } while (read_seqretry(&tk->lock, seq));
1337 1406
1338 now = ktime_add_ns(ktime_set(secs, 0), nsecs); 1407 now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1339 now = ktime_sub(now, *offs_real); 1408 now = ktime_sub(now, *offs_real);
@@ -1346,19 +1415,19 @@ ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
1346 */ 1415 */
1347ktime_t ktime_get_monotonic_offset(void) 1416ktime_t ktime_get_monotonic_offset(void)
1348{ 1417{
1418 struct timekeeper *tk = &timekeeper;
1349 unsigned long seq; 1419 unsigned long seq;
1350 struct timespec wtom; 1420 struct timespec wtom;
1351 1421
1352 do { 1422 do {
1353 seq = read_seqbegin(&timekeeper.lock); 1423 seq = read_seqbegin(&tk->lock);
1354 wtom = timekeeper.wall_to_monotonic; 1424 wtom = tk->wall_to_monotonic;
1355 } while (read_seqretry(&timekeeper.lock, seq)); 1425 } while (read_seqretry(&tk->lock, seq));
1356 1426
1357 return timespec_to_ktime(wtom); 1427 return timespec_to_ktime(wtom);
1358} 1428}
1359EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset); 1429EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
1360 1430
1361
1362/** 1431/**
1363 * xtime_update() - advances the timekeeping infrastructure 1432 * xtime_update() - advances the timekeeping infrastructure
1364 * @ticks: number of ticks, that have elapsed since the last call. 1433 * @ticks: number of ticks, that have elapsed since the last call.