Skip to content

Commit be80893

Browse files
johnstultz-workgregkh
authored andcommitted
clocksource: Avoid selecting mult values that might overflow when adjusted
commit d65670a upstream. For some frequencies, the clocks_calc_mult_shift() function will unfortunately select mult values very close to 0xffffffff. This has the potential to overflow when NTP adjusts the clock, adding to the mult value. This patch adds a clocksource.maxadj value, which provides an approximation of an 11% adjustment(NTP limits adjustments to 500ppm and the tick adjustment is limited to 10%), which could be made to the clocksource.mult value. This is then used to both check that the current mult value won't overflow/underflow, as well as warning us if the timekeeping_adjust() code pushes over that 11% boundary. v2: Fix max_adjustment calculation, and improve WARN_ONCE messages. v3: Don't warn before maxadj has actually been set CC: Yong Zhang <yong.zhang0@gmail.com> CC: David Daney <ddaney.cavm@gmail.com> CC: Thomas Gleixner <tglx@linutronix.de> CC: Chen Jie <chenj@lemote.com> CC: zhangfx <zhangfx@lemote.com> Reported-by: Chen Jie <chenj@lemote.com> Reported-by: zhangfx <zhangfx@lemote.com> Tested-by: Yong Zhang <yong.zhang0@gmail.com> Signed-off-by: John Stultz <john.stultz@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent eb5f9eb commit be80893

3 files changed

Lines changed: 57 additions & 11 deletions

File tree

‎include/linux/clocksource.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ extern u64 timecounter_cyc2time(struct timecounter *tc,
156156
* @mult: cycle to nanosecond multiplier
157157
* @shift: cycle to nanosecond divisor (power of two)
158158
* @max_idle_ns: max idle time permitted by the clocksource (nsecs)
159+
* @maxadj maximum adjustment value to mult (~11%)
159160
* @flags: flags describing special properties
160161
* @archdata: arch-specific data
161162
* @suspend: suspend function for the clocksource, if necessary
@@ -172,7 +173,7 @@ struct clocksource {
172173
u32 mult;
173174
u32 shift;
174175
u64 max_idle_ns;
175-
176+
u32 maxadj;
176177
#ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
177178
struct arch_clocksource_data archdata;
178179
#endif

‎kernel/time/clocksource.c‎

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,22 @@ void clocksource_touch_watchdog(void)
493493
clocksource_resume_watchdog();
494494
}
495495

496+
/**
497+
* clocksource_max_adjustment- Returns max adjustment amount
498+
* @cs: Pointer to clocksource
499+
*
500+
*/
501+
static u32 clocksource_max_adjustment(struct clocksource *cs)
502+
{
503+
u64 ret;
504+
/*
505+
* We won't try to correct for more then 11% adjustments (110,000 ppm),
506+
*/
507+
ret = (u64)cs->mult * 11;
508+
do_div(ret,100);
509+
return (u32)ret;
510+
}
511+
496512
/**
497513
* clocksource_max_deferment - Returns max time the clocksource can be deferred
498514
* @cs: Pointer to clocksource
@@ -505,25 +521,28 @@ static u64 clocksource_max_deferment(struct clocksource *cs)
505521
/*
506522
* Calculate the maximum number of cycles that we can pass to the
507523
* cyc2ns function without overflowing a 64-bit signed result. The
508-
* maximum number of cycles is equal to ULLONG_MAX/cs->mult which
509-
* is equivalent to the below.
510-
* max_cycles < (2^63)/cs->mult
511-
* max_cycles < 2^(log2((2^63)/cs->mult))
512-
* max_cycles < 2^(log2(2^63) - log2(cs->mult))
513-
* max_cycles < 2^(63 - log2(cs->mult))
514-
* max_cycles < 1 << (63 - log2(cs->mult))
524+
* maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj)
525+
* which is equivalent to the below.
526+
* max_cycles < (2^63)/(cs->mult + cs->maxadj)
527+
* max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj)))
528+
* max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj))
529+
* max_cycles < 2^(63 - log2(cs->mult + cs->maxadj))
530+
* max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj))
515531
* Please note that we add 1 to the result of the log2 to account for
516532
* any rounding errors, ensure the above inequality is satisfied and
517533
* no overflow will occur.
518534
*/
519-
max_cycles = 1ULL << (63 - (ilog2(cs->mult) + 1));
535+
max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1));
520536

521537
/*
522538
* The actual maximum number of cycles we can defer the clocksource is
523539
* determined by the minimum of max_cycles and cs->mask.
540+
* Note: Here we subtract the maxadj to make sure we don't sleep for
541+
* too long if there's a large negative adjustment.
524542
*/
525543
max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
526-
max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult, cs->shift);
544+
max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj,
545+
cs->shift);
527546

528547
/*
529548
* To ensure that the clocksource does not wrap whilst we are idle,
@@ -642,7 +661,6 @@ static void clocksource_enqueue(struct clocksource *cs)
642661
void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
643662
{
644663
u64 sec;
645-
646664
/*
647665
* Calc the maximum number of seconds which we can run before
648666
* wrapping around. For clocksources which have a mask > 32bit
@@ -663,6 +681,20 @@ void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
663681

664682
clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
665683
NSEC_PER_SEC / scale, sec * scale);
684+
685+
/*
686+
* for clocksources that have large mults, to avoid overflow.
687+
* Since mult may be adjusted by ntp, add an safety extra margin
688+
*
689+
*/
690+
cs->maxadj = clocksource_max_adjustment(cs);
691+
while ((cs->mult + cs->maxadj < cs->mult)
692+
|| (cs->mult - cs->maxadj > cs->mult)) {
693+
cs->mult >>= 1;
694+
cs->shift--;
695+
cs->maxadj = clocksource_max_adjustment(cs);
696+
}
697+
666698
cs->max_idle_ns = clocksource_max_deferment(cs);
667699
}
668700
EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
@@ -703,6 +735,12 @@ EXPORT_SYMBOL_GPL(__clocksource_register_scale);
703735
*/
704736
int clocksource_register(struct clocksource *cs)
705737
{
738+
/* calculate max adjustment for given mult/shift */
739+
cs->maxadj = clocksource_max_adjustment(cs);
740+
WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
741+
"Clocksource %s might overflow on 11%% adjustment\n",
742+
cs->name);
743+
706744
/* calculate max idle time permitted for this clocksource */
707745
cs->max_idle_ns = clocksource_max_deferment(cs);
708746

‎kernel/time/timekeeping.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,13 @@ static void timekeeping_adjust(s64 offset)
824824
} else
825825
return;
826826

827+
WARN_ONCE(timekeeper.clock->maxadj &&
828+
(timekeeper.mult + adj > timekeeper.clock->mult +
829+
timekeeper.clock->maxadj),
830+
"Adjusting %s more then 11%% (%ld vs %ld)\n",
831+
timekeeper.clock->name, (long)timekeeper.mult + adj,
832+
(long)timekeeper.clock->mult +
833+
timekeeper.clock->maxadj);
827834
timekeeper.mult += adj;
828835
timekeeper.xtime_interval += interval;
829836
timekeeper.xtime_nsec -= offset;

0 commit comments

Comments
 (0)