diff options
| -rw-r--r-- | include/linux/clocksource.h | 3 | ||||
| -rw-r--r-- | kernel/time/clocksource.c | 58 | ||||
| -rw-r--r-- | kernel/time/timekeeping.c | 7 |
3 files changed, 57 insertions, 11 deletions
diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h index 139c4db55f17..c86c940d1de3 100644 --- a/include/linux/clocksource.h +++ b/include/linux/clocksource.h | |||
| @@ -156,6 +156,7 @@ extern u64 timecounter_cyc2time(struct timecounter *tc, | |||
| 156 | * @mult: cycle to nanosecond multiplier | 156 | * @mult: cycle to nanosecond multiplier |
| 157 | * @shift: cycle to nanosecond divisor (power of two) | 157 | * @shift: cycle to nanosecond divisor (power of two) |
| 158 | * @max_idle_ns: max idle time permitted by the clocksource (nsecs) | 158 | * @max_idle_ns: max idle time permitted by the clocksource (nsecs) |
| 159 | * @maxadj maximum adjustment value to mult (~11%) | ||
| 159 | * @flags: flags describing special properties | 160 | * @flags: flags describing special properties |
| 160 | * @archdata: arch-specific data | 161 | * @archdata: arch-specific data |
| 161 | * @suspend: suspend function for the clocksource, if necessary | 162 | * @suspend: suspend function for the clocksource, if necessary |
| @@ -172,7 +173,7 @@ struct clocksource { | |||
| 172 | u32 mult; | 173 | u32 mult; |
| 173 | u32 shift; | 174 | u32 shift; |
| 174 | u64 max_idle_ns; | 175 | u64 max_idle_ns; |
| 175 | 176 | u32 maxadj; | |
| 176 | #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA | 177 | #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA |
| 177 | struct arch_clocksource_data archdata; | 178 | struct arch_clocksource_data archdata; |
| 178 | #endif | 179 | #endif |
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c index cf52fda2e096..cfc65e1eb9fb 100644 --- a/kernel/time/clocksource.c +++ b/kernel/time/clocksource.c | |||
| @@ -492,6 +492,22 @@ void clocksource_touch_watchdog(void) | |||
| 492 | } | 492 | } |
| 493 | 493 | ||
| 494 | /** | 494 | /** |
| 495 | * clocksource_max_adjustment- Returns max adjustment amount | ||
| 496 | * @cs: Pointer to clocksource | ||
| 497 | * | ||
| 498 | */ | ||
| 499 | static u32 clocksource_max_adjustment(struct clocksource *cs) | ||
| 500 | { | ||
| 501 | u64 ret; | ||
| 502 | /* | ||
| 503 | * We won't try to correct for more then 11% adjustments (110,000 ppm), | ||
| 504 | */ | ||
| 505 | ret = (u64)cs->mult * 11; | ||
| 506 | do_div(ret,100); | ||
| 507 | return (u32)ret; | ||
| 508 | } | ||
| 509 | |||
| 510 | /** | ||
| 495 | * clocksource_max_deferment - Returns max time the clocksource can be deferred | 511 | * clocksource_max_deferment - Returns max time the clocksource can be deferred |
| 496 | * @cs: Pointer to clocksource | 512 | * @cs: Pointer to clocksource |
| 497 | * | 513 | * |
| @@ -503,25 +519,28 @@ static u64 clocksource_max_deferment(struct clocksource *cs) | |||
| 503 | /* | 519 | /* |
| 504 | * Calculate the maximum number of cycles that we can pass to the | 520 | * Calculate the maximum number of cycles that we can pass to the |
| 505 | * cyc2ns function without overflowing a 64-bit signed result. The | 521 | * cyc2ns function without overflowing a 64-bit signed result. The |
| 506 | * maximum number of cycles is equal to ULLONG_MAX/cs->mult which | 522 | * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj) |
| 507 | * is equivalent to the below. | 523 | * which is equivalent to the below. |
| 508 | * max_cycles < (2^63)/cs->mult | 524 | * max_cycles < (2^63)/(cs->mult + cs->maxadj) |
| 509 | * max_cycles < 2^(log2((2^63)/cs->mult)) | 525 | * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj))) |
| 510 | * max_cycles < 2^(log2(2^63) - log2(cs->mult)) | 526 | * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj)) |
| 511 | * max_cycles < 2^(63 - log2(cs->mult)) | 527 | * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj)) |
| 512 | * max_cycles < 1 << (63 - log2(cs->mult)) | 528 | * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj)) |
| 513 | * Please note that we add 1 to the result of the log2 to account for | 529 | * Please note that we add 1 to the result of the log2 to account for |
| 514 | * any rounding errors, ensure the above inequality is satisfied and | 530 | * any rounding errors, ensure the above inequality is satisfied and |
| 515 | * no overflow will occur. | 531 | * no overflow will occur. |
| 516 | */ | 532 | */ |
| 517 | max_cycles = 1ULL << (63 - (ilog2(cs->mult) + 1)); | 533 | max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1)); |
| 518 | 534 | ||
| 519 | /* | 535 | /* |
| 520 | * The actual maximum number of cycles we can defer the clocksource is | 536 | * The actual maximum number of cycles we can defer the clocksource is |
| 521 | * determined by the minimum of max_cycles and cs->mask. | 537 | * determined by the minimum of max_cycles and cs->mask. |
| 538 | * Note: Here we subtract the maxadj to make sure we don't sleep for | ||
| 539 | * too long if there's a large negative adjustment. | ||
| 522 | */ | 540 | */ |
| 523 | max_cycles = min_t(u64, max_cycles, (u64) cs->mask); | 541 | max_cycles = min_t(u64, max_cycles, (u64) cs->mask); |
| 524 | max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult, cs->shift); | 542 | max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj, |
| 543 | cs->shift); | ||
| 525 | 544 | ||
| 526 | /* | 545 | /* |
| 527 | * To ensure that the clocksource does not wrap whilst we are idle, | 546 | * To ensure that the clocksource does not wrap whilst we are idle, |
| @@ -640,7 +659,6 @@ static void clocksource_enqueue(struct clocksource *cs) | |||
| 640 | void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) | 659 | void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) |
| 641 | { | 660 | { |
| 642 | u64 sec; | 661 | u64 sec; |
| 643 | |||
| 644 | /* | 662 | /* |
| 645 | * Calc the maximum number of seconds which we can run before | 663 | * Calc the maximum number of seconds which we can run before |
| 646 | * wrapping around. For clocksources which have a mask > 32bit | 664 | * wrapping around. For clocksources which have a mask > 32bit |
| @@ -661,6 +679,20 @@ void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) | |||
| 661 | 679 | ||
| 662 | clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, | 680 | clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, |
| 663 | NSEC_PER_SEC / scale, sec * scale); | 681 | NSEC_PER_SEC / scale, sec * scale); |
| 682 | |||
| 683 | /* | ||
| 684 | * for clocksources that have large mults, to avoid overflow. | ||
| 685 | * Since mult may be adjusted by ntp, add an safety extra margin | ||
| 686 | * | ||
| 687 | */ | ||
| 688 | cs->maxadj = clocksource_max_adjustment(cs); | ||
| 689 | while ((cs->mult + cs->maxadj < cs->mult) | ||
| 690 | || (cs->mult - cs->maxadj > cs->mult)) { | ||
| 691 | cs->mult >>= 1; | ||
| 692 | cs->shift--; | ||
| 693 | cs->maxadj = clocksource_max_adjustment(cs); | ||
| 694 | } | ||
| 695 | |||
| 664 | cs->max_idle_ns = clocksource_max_deferment(cs); | 696 | cs->max_idle_ns = clocksource_max_deferment(cs); |
| 665 | } | 697 | } |
| 666 | EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale); | 698 | EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale); |
| @@ -701,6 +733,12 @@ EXPORT_SYMBOL_GPL(__clocksource_register_scale); | |||
| 701 | */ | 733 | */ |
| 702 | int clocksource_register(struct clocksource *cs) | 734 | int clocksource_register(struct clocksource *cs) |
| 703 | { | 735 | { |
| 736 | /* calculate max adjustment for given mult/shift */ | ||
| 737 | cs->maxadj = clocksource_max_adjustment(cs); | ||
| 738 | WARN_ONCE(cs->mult + cs->maxadj < cs->mult, | ||
| 739 | "Clocksource %s might overflow on 11%% adjustment\n", | ||
| 740 | cs->name); | ||
| 741 | |||
| 704 | /* calculate max idle time permitted for this clocksource */ | 742 | /* calculate max idle time permitted for this clocksource */ |
| 705 | cs->max_idle_ns = clocksource_max_deferment(cs); | 743 | cs->max_idle_ns = clocksource_max_deferment(cs); |
| 706 | 744 | ||
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 025e136f3881..e9f60d311436 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c | |||
| @@ -850,6 +850,13 @@ static void timekeeping_adjust(s64 offset) | |||
| 850 | } else /* No adjustment needed */ | 850 | } else /* No adjustment needed */ |
| 851 | return; | 851 | return; |
| 852 | 852 | ||
| 853 | WARN_ONCE(timekeeper.clock->maxadj && | ||
| 854 | (timekeeper.mult + adj > timekeeper.clock->mult + | ||
| 855 | timekeeper.clock->maxadj), | ||
| 856 | "Adjusting %s more then 11%% (%ld vs %ld)\n", | ||
| 857 | timekeeper.clock->name, (long)timekeeper.mult + adj, | ||
| 858 | (long)timekeeper.clock->mult + | ||
| 859 | timekeeper.clock->maxadj); | ||
| 853 | /* | 860 | /* |
| 854 | * So the following can be confusing. | 861 | * So the following can be confusing. |
| 855 | * | 862 | * |
