time: Avoid possible NTP adjustment mult overflow.
authorpang.xunlei <pang.xunlei@linaro.org>
Wed, 8 Oct 2014 07:03:34 +0000 (15:03 +0800)
committerJohn Stultz <john.stultz@linaro.org>
Fri, 21 Nov 2014 19:59:56 +0000 (11:59 -0800)
Ideally, __clocksource_updatefreq_scale, selects the largest shift
value possible for a clocksource. This results in the mult memember of
struct clocksource being particularly large, although not so large
that NTP would adjust the clock to cause it to overflow.

That said, nothing actually prohibits an overflow from occuring, its
just that it "shouldn't" occur.

So while very unlikely, and so far never observed, the value of
(cs->mult+cs->maxadj) may have a chance to reach very near 0xFFFFFFFF,
so there is a possibility it may overflow when doing NTP positive
adjustment

See the following detail: When NTP slewes the clock, kernel goes
through update_wall_time()->...->timekeeping_apply_adjustment():
tk->tkr.mult += mult_adj;

Since there is no guard against it, its possible tk->tkr.mult may
overflow during this operation.

This patch avoids any possible mult overflow by judging the overflow
case before adding mult_adj to mult, also adds the WARNING message
when capturing such case.

Signed-off-by: pang.xunlei <pang.xunlei@linaro.org>
[jstultz: Reworded commit message]
Signed-off-by: John Stultz <john.stultz@linaro.org>
kernel/time/timekeeping.c

index ec1791fae96575ffe49c9aa1a2f42cbc6b21df2a..cad61b3f6beaa2983b6dda33a2b1b23a0c444a5b 100644 (file)
@@ -1332,6 +1332,12 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
         *
         * XXX - TODO: Doc ntp_error calculation.
         */
+       if (tk->tkr.mult + mult_adj < mult_adj) {
+               /* NTP adjustment caused clocksource mult overflow */
+               WARN_ON_ONCE(1);
+               return;
+       }
+
        tk->tkr.mult += mult_adj;
        tk->xtime_interval += interval;
        tk->tkr.xtime_nsec -= offset;