]> Pileus Git - ~andy/linux/blobdiff - kernel/time/timekeeping.c
time: Clean up offs_real/wall_to_mono and offs_boot/total_sleep_time updates
[~andy/linux] / kernel / time / timekeeping.c
index cb4a433bab976dbf205e3209566ddd67da914019..4da65592b4d913f80a74d0ce782acc1958d1d0dd 100644 (file)
@@ -65,14 +65,14 @@ struct timekeeper {
         * used instead.
         */
        struct timespec         wall_to_monotonic;
-       /* time spent in suspend */
-       struct timespec         total_sleep_time;
-       /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
-       struct timespec         raw_time;
        /* Offset clock monotonic -> clock realtime */
        ktime_t                 offs_real;
+       /* time spent in suspend */
+       struct timespec         total_sleep_time;
        /* Offset clock monotonic -> clock boottime */
        ktime_t                 offs_boot;
+       /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
+       struct timespec         raw_time;
        /* Seqlock for all timekeeper values */
        seqlock_t               lock;
 };
@@ -108,13 +108,38 @@ static struct timespec tk_xtime(struct timekeeper *tk)
 static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
 {
        tk->xtime_sec = ts->tv_sec;
-       tk->xtime_nsec = ts->tv_nsec << tk->shift;
+       tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
 }
 
 static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
 {
        tk->xtime_sec += ts->tv_sec;
-       tk->xtime_nsec += ts->tv_nsec << tk->shift;
+       tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
+}
+
+static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
+{
+       struct timespec tmp;
+
+       /*
+        * Verify consistency of: offset_real = -wall_to_monotonic
+        * before modifying anything
+        */
+       set_normalized_timespec(&tmp, -tk->wall_to_monotonic.tv_sec,
+                                       -tk->wall_to_monotonic.tv_nsec);
+       WARN_ON_ONCE(tk->offs_real.tv64 != timespec_to_ktime(tmp).tv64);
+       tk->wall_to_monotonic = wtm;
+       set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
+       tk->offs_real = timespec_to_ktime(tmp);
+}
+
+static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
+{
+       /* Verify consistency before modifying */
+       WARN_ON_ONCE(tk->offs_boot.tv64 != timespec_to_ktime(tk->total_sleep_time).tv64);
+
+       tk->total_sleep_time    = t;
+       tk->offs_boot           = timespec_to_ktime(t);
 }
 
 /**
@@ -127,14 +152,14 @@ static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
  *
  * Unless you're the timekeeping code, you should not be using this!
  */
-static void timekeeper_setup_internals(struct clocksource *clock)
+static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
 {
        cycle_t interval;
        u64 tmp, ntpinterval;
        struct clocksource *old_clock;
 
-       old_clock = timekeeper.clock;
-       timekeeper.clock = clock;
+       old_clock = tk->clock;
+       tk->clock = clock;
        clock->cycle_last = clock->read(clock);
 
        /* Do the ns -> cycle conversion first, using original mult */
@@ -147,93 +172,89 @@ static void timekeeper_setup_internals(struct clocksource *clock)
                tmp = 1;
 
        interval = (cycle_t) tmp;
-       timekeeper.cycle_interval = interval;
+       tk->cycle_interval = interval;
 
        /* Go back from cycles -> shifted ns */
-       timekeeper.xtime_interval = (u64) interval * clock->mult;
-       timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval;
-       timekeeper.raw_interval =
+       tk->xtime_interval = (u64) interval * clock->mult;
+       tk->xtime_remainder = ntpinterval - tk->xtime_interval;
+       tk->raw_interval =
                ((u64) interval * clock->mult) >> clock->shift;
 
         /* if changing clocks, convert xtime_nsec shift units */
        if (old_clock) {
                int shift_change = clock->shift - old_clock->shift;
                if (shift_change < 0)
-                       timekeeper.xtime_nsec >>= -shift_change;
+                       tk->xtime_nsec >>= -shift_change;
                else
-                       timekeeper.xtime_nsec <<= shift_change;
+                       tk->xtime_nsec <<= shift_change;
        }
-       timekeeper.shift = clock->shift;
+       tk->shift = clock->shift;
 
-       timekeeper.ntp_error = 0;
-       timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
+       tk->ntp_error = 0;
+       tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
 
        /*
         * The timekeeper keeps its own mult values for the currently
         * active clocksource. These value will be adjusted via NTP
         * to counteract clock drifting.
         */
-       timekeeper.mult = clock->mult;
+       tk->mult = clock->mult;
 }
 
 /* Timekeeper helper functions. */
-static inline s64 timekeeping_get_ns(void)
+static inline s64 timekeeping_get_ns(struct timekeeper *tk)
 {
        cycle_t cycle_now, cycle_delta;
        struct clocksource *clock;
        s64 nsec;
 
        /* read clocksource: */
-       clock = timekeeper.clock;
+       clock = tk->clock;
        cycle_now = clock->read(clock);
 
        /* calculate the delta since the last update_wall_time: */
        cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
 
-       nsec = cycle_delta * timekeeper.mult + timekeeper.xtime_nsec;
-       return nsec >> timekeeper.shift;
+       nsec = cycle_delta * tk->mult + tk->xtime_nsec;
+       nsec >>= tk->shift;
+
+       /* If arch requires, add in gettimeoffset() */
+       return nsec + arch_gettimeoffset();
 }
 
-static inline s64 timekeeping_get_ns_raw(void)
+static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
 {
        cycle_t cycle_now, cycle_delta;
        struct clocksource *clock;
+       s64 nsec;
 
        /* read clocksource: */
-       clock = timekeeper.clock;
+       clock = tk->clock;
        cycle_now = clock->read(clock);
 
        /* calculate the delta since the last update_wall_time: */
        cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
 
-       /* return delta convert to nanoseconds. */
-       return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
-}
-
-static void update_rt_offset(void)
-{
-       struct timespec tmp, *wtm = &timekeeper.wall_to_monotonic;
+       /* convert delta to nanoseconds. */
+       nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
 
-       set_normalized_timespec(&tmp, -wtm->tv_sec, -wtm->tv_nsec);
-       timekeeper.offs_real = timespec_to_ktime(tmp);
+       /* If arch requires, add in gettimeoffset() */
+       return nsec + arch_gettimeoffset();
 }
 
 /* must hold write on timekeeper.lock */
-static void timekeeping_update(bool clearntp)
+static void timekeeping_update(struct timekeeper *tk, bool clearntp)
 {
        struct timespec xt;
 
        if (clearntp) {
-               timekeeper.ntp_error = 0;
+               tk->ntp_error = 0;
                ntp_clear();
        }
-       update_rt_offset();
-       xt = tk_xtime(&timekeeper);
-       update_vsyscall(&xt, &timekeeper.wall_to_monotonic,
-                        timekeeper.clock, timekeeper.mult);
+       xt = tk_xtime(tk);
+       update_vsyscall(&xt, &tk->wall_to_monotonic, tk->clock, tk->mult);
 }
 
-
 /**
  * timekeeping_forward_now - update clock to the current time
  *
@@ -241,26 +262,26 @@ static void timekeeping_update(bool clearntp)
  * update_wall_time(). This is useful before significant clock changes,
  * as it avoids having to deal with this time offset explicitly.
  */
-static void timekeeping_forward_now(void)
+static void timekeeping_forward_now(struct timekeeper *tk)
 {
        cycle_t cycle_now, cycle_delta;
        struct clocksource *clock;
        s64 nsec;
 
-       clock = timekeeper.clock;
+       clock = tk->clock;
        cycle_now = clock->read(clock);
        cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
        clock->cycle_last = cycle_now;
 
-       timekeeper.xtime_nsec += cycle_delta * timekeeper.mult;
+       tk->xtime_nsec += cycle_delta * tk->mult;
 
        /* If arch requires, add in gettimeoffset() */
-       timekeeper.xtime_nsec += arch_gettimeoffset() << timekeeper.shift;
+       tk->xtime_nsec += arch_gettimeoffset() << tk->shift;
 
-       tk_normalize_xtime(&timekeeper);
+       tk_normalize_xtime(tk);
 
        nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
-       timespec_add_ns(&timekeeper.raw_time, nsec);
+       timespec_add_ns(&tk->raw_time, nsec);
 }
 
 /**
@@ -280,10 +301,7 @@ void getnstimeofday(struct timespec *ts)
                seq = read_seqbegin(&timekeeper.lock);
 
                ts->tv_sec = timekeeper.xtime_sec;
-               ts->tv_nsec = timekeeping_get_ns();
-
-               /* If arch requires, add in gettimeoffset() */
-               nsecs += arch_gettimeoffset();
+               ts->tv_nsec = timekeeping_get_ns(&timekeeper);
 
        } while (read_seqretry(&timekeeper.lock, seq));
 
@@ -302,10 +320,8 @@ ktime_t ktime_get(void)
                seq = read_seqbegin(&timekeeper.lock);
                secs = timekeeper.xtime_sec +
                                timekeeper.wall_to_monotonic.tv_sec;
-               nsecs = timekeeping_get_ns() +
+               nsecs = timekeeping_get_ns(&timekeeper) +
                                timekeeper.wall_to_monotonic.tv_nsec;
-               /* If arch requires, add in gettimeoffset() */
-               nsecs += arch_gettimeoffset();
 
        } while (read_seqretry(&timekeeper.lock, seq));
        /*
@@ -334,10 +350,8 @@ void ktime_get_ts(struct timespec *ts)
        do {
                seq = read_seqbegin(&timekeeper.lock);
                ts->tv_sec = timekeeper.xtime_sec;
-               ts->tv_nsec = timekeeping_get_ns();
+               ts->tv_nsec = timekeeping_get_ns(&timekeeper);
                tomono = timekeeper.wall_to_monotonic;
-               /* If arch requires, add in gettimeoffset() */
-               ts->tv_nsec += arch_gettimeoffset();
 
        } while (read_seqretry(&timekeeper.lock, seq));
 
@@ -365,21 +379,14 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
        WARN_ON_ONCE(timekeeping_suspended);
 
        do {
-               u32 arch_offset;
-
                seq = read_seqbegin(&timekeeper.lock);
 
                *ts_raw = timekeeper.raw_time;
                ts_real->tv_sec = timekeeper.xtime_sec;
                ts_real->tv_nsec = 0;
 
-               nsecs_raw = timekeeping_get_ns_raw();
-               nsecs_real = timekeeping_get_ns();
-
-               /* If arch requires, add in gettimeoffset() */
-               arch_offset = arch_gettimeoffset();
-               nsecs_raw += arch_offset;
-               nsecs_real += arch_offset;
+               nsecs_raw = timekeeping_get_ns_raw(&timekeeper);
+               nsecs_real = timekeeping_get_ns(&timekeeper);
 
        } while (read_seqretry(&timekeeper.lock, seq));
 
@@ -422,18 +429,18 @@ int do_settimeofday(const struct timespec *tv)
 
        write_seqlock_irqsave(&timekeeper.lock, flags);
 
-       timekeeping_forward_now();
+       timekeeping_forward_now(&timekeeper);
 
        xt = tk_xtime(&timekeeper);
        ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
        ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
 
-       timekeeper.wall_to_monotonic =
-                       timespec_sub(timekeeper.wall_to_monotonic, ts_delta);
+       tk_set_wall_to_mono(&timekeeper,
+                       timespec_sub(timekeeper.wall_to_monotonic, ts_delta));
 
        tk_set_xtime(&timekeeper, tv);
 
-       timekeeping_update(true);
+       timekeeping_update(&timekeeper, true);
 
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
@@ -444,7 +451,6 @@ int do_settimeofday(const struct timespec *tv)
 }
 EXPORT_SYMBOL(do_settimeofday);
 
-
 /**
  * timekeeping_inject_offset - Adds or subtracts from the current time.
  * @tv:                pointer to the timespec variable containing the offset
@@ -460,14 +466,14 @@ int timekeeping_inject_offset(struct timespec *ts)
 
        write_seqlock_irqsave(&timekeeper.lock, flags);
 
-       timekeeping_forward_now();
+       timekeeping_forward_now(&timekeeper);
 
 
        tk_xtime_add(&timekeeper, ts);
-       timekeeper.wall_to_monotonic =
-                               timespec_sub(timekeeper.wall_to_monotonic, *ts);
+       tk_set_wall_to_mono(&timekeeper,
+                       timespec_sub(timekeeper.wall_to_monotonic, *ts));
 
-       timekeeping_update(true);
+       timekeeping_update(&timekeeper, true);
 
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
@@ -492,14 +498,14 @@ static int change_clocksource(void *data)
 
        write_seqlock_irqsave(&timekeeper.lock, flags);
 
-       timekeeping_forward_now();
+       timekeeping_forward_now(&timekeeper);
        if (!new->enable || new->enable(new) == 0) {
                old = timekeeper.clock;
-               timekeeper_setup_internals(new);
+               tk_setup_internals(&timekeeper, new);
                if (old->disable)
                        old->disable(old);
        }
-       timekeeping_update(true);
+       timekeeping_update(&timekeeper, true);
 
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
@@ -549,7 +555,7 @@ void getrawmonotonic(struct timespec *ts)
 
        do {
                seq = read_seqbegin(&timekeeper.lock);
-               nsecs = timekeeping_get_ns_raw();
+               nsecs = timekeeping_get_ns_raw(&timekeeper);
                *ts = timekeeper.raw_time;
 
        } while (read_seqretry(&timekeeper.lock, seq));
@@ -558,7 +564,6 @@ void getrawmonotonic(struct timespec *ts)
 }
 EXPORT_SYMBOL(getrawmonotonic);
 
-
 /**
  * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  */
@@ -632,7 +637,7 @@ void __init timekeeping_init(void)
 {
        struct clocksource *clock;
        unsigned long flags;
-       struct timespec now, boot;
+       struct timespec now, boot, tmp;
 
        read_persistent_clock(&now);
        read_boot_clock(&boot);
@@ -645,7 +650,7 @@ void __init timekeeping_init(void)
        clock = clocksource_default_clock();
        if (clock->enable)
                clock->enable(clock);
-       timekeeper_setup_internals(clock);
+       tk_setup_internals(&timekeeper, clock);
 
        tk_set_xtime(&timekeeper, &now);
        timekeeper.raw_time.tv_sec = 0;
@@ -653,23 +658,19 @@ void __init timekeeping_init(void)
        if (boot.tv_sec == 0 && boot.tv_nsec == 0)
                boot = tk_xtime(&timekeeper);
 
-       set_normalized_timespec(&timekeeper.wall_to_monotonic,
-                               -boot.tv_sec, -boot.tv_nsec);
-       update_rt_offset();
-       timekeeper.total_sleep_time.tv_sec = 0;
-       timekeeper.total_sleep_time.tv_nsec = 0;
+       set_normalized_timespec(&tmp, -boot.tv_sec, -boot.tv_nsec);
+       tk_set_wall_to_mono(&timekeeper, tmp);
+
+       tmp.tv_sec = 0;
+       tmp.tv_nsec = 0;
+       tk_set_sleep_time(&timekeeper, tmp);
+
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
 }
 
 /* time in seconds when suspend began */
 static struct timespec timekeeping_suspend_time;
 
-static void update_sleep_time(struct timespec t)
-{
-       timekeeper.total_sleep_time = t;
-       timekeeper.offs_boot = timespec_to_ktime(t);
-}
-
 /**
  * __timekeeping_inject_sleeptime - Internal function to add sleep interval
  * @delta: pointer to a timespec delta value
@@ -677,21 +678,19 @@ static void update_sleep_time(struct timespec t)
  * Takes a timespec offset measuring a suspend interval and properly
  * adds the sleep offset to the timekeeping variables.
  */
-static void __timekeeping_inject_sleeptime(struct timespec *delta)
+static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
+                                                       struct timespec *delta)
 {
        if (!timespec_valid(delta)) {
                printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
                                        "sleep delta value!\n");
                return;
        }
-
-       tk_xtime_add(&timekeeper, delta);
-       timekeeper.wall_to_monotonic =
-                       timespec_sub(timekeeper.wall_to_monotonic, *delta);
-       update_sleep_time(timespec_add(timekeeper.total_sleep_time, *delta));
+       tk_xtime_add(tk, delta);
+       tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta));
+       tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta));
 }
 
-
 /**
  * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
  * @delta: pointer to a timespec delta value
@@ -714,11 +713,11 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
 
        write_seqlock_irqsave(&timekeeper.lock, flags);
 
-       timekeeping_forward_now();
+       timekeeping_forward_now(&timekeeper);
 
-       __timekeeping_inject_sleeptime(delta);
+       __timekeeping_inject_sleeptime(&timekeeper, delta);
 
-       timekeeping_update(true);
+       timekeeping_update(&timekeeper, true);
 
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
@@ -726,7 +725,6 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
        clock_was_set();
 }
 
-
 /**
  * timekeeping_resume - Resumes the generic timekeeping subsystem.
  *
@@ -747,12 +745,13 @@ static void timekeeping_resume(void)
 
        if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
                ts = timespec_sub(ts, timekeeping_suspend_time);
-               __timekeeping_inject_sleeptime(&ts);
+               __timekeeping_inject_sleeptime(&timekeeper, &ts);
        }
        /* re-base the last cycle value */
        timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
        timekeeper.ntp_error = 0;
        timekeeping_suspended = 0;
+       timekeeping_update(&timekeeper, false);
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        touch_softlockup_watchdog();
@@ -772,7 +771,7 @@ static int timekeeping_suspend(void)
        read_persistent_clock(&timekeeping_suspend_time);
 
        write_seqlock_irqsave(&timekeeper.lock, flags);
-       timekeeping_forward_now();
+       timekeeping_forward_now(&timekeeper);
        timekeeping_suspended = 1;
 
        /*
@@ -820,7 +819,8 @@ device_initcall(timekeeping_init_ops);
  * If the error is already larger, we look ahead even further
  * to compensate for late or lost adjustments.
  */
-static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
+static __always_inline int timekeeping_bigadjust(struct timekeeper *tk,
+                                                s64 error, s64 *interval,
                                                 s64 *offset)
 {
        s64 tick_error, i;
@@ -836,7 +836,7 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
         * here.  This is tuned so that an error of about 1 msec is adjusted
         * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
         */
-       error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
+       error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
        error2 = abs(error2);
        for (look_ahead = 0; error2 > 0; look_ahead++)
                error2 >>= 2;
@@ -845,8 +845,8 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
         * Now calculate the error in (1 << look_ahead) ticks, but first
         * remove the single look ahead already included in the error.
         */
-       tick_error = ntp_tick_length() >> (timekeeper.ntp_error_shift + 1);
-       tick_error -= timekeeper.xtime_interval >> 1;
+       tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1);
+       tick_error -= tk->xtime_interval >> 1;
        error = ((error - tick_error) >> look_ahead) + tick_error;
 
        /* Finally calculate the adjustment shift value.  */
@@ -871,9 +871,9 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
  * this is optimized for the most common adjustments of -1,0,1,
  * for other values we can do a bit more work.
  */
-static void timekeeping_adjust(s64 offset)
+static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
 {
-       s64 error, interval = timekeeper.cycle_interval;
+       s64 error, interval = tk->cycle_interval;
        int adj;
 
        /*
@@ -889,7 +889,7 @@ static void timekeeping_adjust(s64 offset)
         *
         * Note: It does not "save" on aggravation when reading the code.
         */
-       error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
+       error = tk->ntp_error >> (tk->ntp_error_shift - 1);
        if (error > interval) {
                /*
                 * We now divide error by 4(via shift), which checks if
@@ -911,7 +911,8 @@ static void timekeeping_adjust(s64 offset)
                if (likely(error <= interval))
                        adj = 1;
                else
-                       adj = timekeeping_bigadjust(error, &interval, &offset);
+                       adj = timekeeping_bigadjust(tk, error, &interval,
+                                                       &offset);
        } else if (error < -interval) {
                /* See comment above, this is just switched for the negative */
                error >>= 2;
@@ -920,18 +921,17 @@ static void timekeeping_adjust(s64 offset)
                        interval = -interval;
                        offset = -offset;
                } else
-                       adj = timekeeping_bigadjust(error, &interval, &offset);
-       } else /* No adjustment needed */
+                       adj = timekeeping_bigadjust(tk, error, &interval,
+                                                       &offset);
+       } else
                return;
 
-       if (unlikely(timekeeper.clock->maxadj &&
-                       (timekeeper.mult + adj >
-                       timekeeper.clock->mult + timekeeper.clock->maxadj))) {
+       if (unlikely(tk->clock->maxadj &&
+               (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) {
                printk_once(KERN_WARNING
                        "Adjusting %s more than 11%% (%ld vs %ld)\n",
-                       timekeeper.clock->name, (long)timekeeper.mult + adj,
-                       (long)timekeeper.clock->mult +
-                               timekeeper.clock->maxadj);
+                       tk->clock->name, (long)tk->mult + adj,
+                       (long)tk->clock->mult + tk->clock->maxadj);
        }
        /*
         * So the following can be confusing.
@@ -982,13 +982,32 @@ static void timekeeping_adjust(s64 offset)
         *
         * XXX - TODO: Doc ntp_error calculation.
         */
-       timekeeper.mult += adj;
-       timekeeper.xtime_interval += interval;
-       timekeeper.xtime_nsec -= offset;
-       timekeeper.ntp_error -= (interval - offset) <<
-                               timekeeper.ntp_error_shift;
-}
+       tk->mult += adj;
+       tk->xtime_interval += interval;
+       tk->xtime_nsec -= offset;
+       tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
+
+       /*
+        * It may be possible that when we entered this function, xtime_nsec
+        * was very small.  Further, if we're slightly speeding the clocksource
+        * in the code above, its possible the required corrective factor to
+        * xtime_nsec could cause it to underflow.
+        *
+        * Now, since we already accumulated the second, cannot simply roll
+        * the accumulated second back, since the NTP subsystem has been
+        * notified via second_overflow. So instead we push xtime_nsec forward
+        * by the amount we underflowed, and add that amount into the error.
+        *
+        * We'll correct this error next time through this function, when
+        * xtime_nsec is not as small.
+        */
+       if (unlikely((s64)tk->xtime_nsec < 0)) {
+               s64 neg = -(s64)tk->xtime_nsec;
+               tk->xtime_nsec = 0;
+               tk->ntp_error += neg << tk->ntp_error_shift;
+       }
 
+}
 
 /**
  * accumulate_nsecs_to_secs - Accumulates nsecs into secs
@@ -1010,15 +1029,21 @@ static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
 
                /* Figure out if its a leap sec and apply if needed */
                leap = second_overflow(tk->xtime_sec);
-               tk->xtime_sec += leap;
-               tk->wall_to_monotonic.tv_sec -= leap;
-               if (leap)
-                       clock_was_set_delayed();
+               if (unlikely(leap)) {
+                       struct timespec ts;
+
+                       tk->xtime_sec += leap;
 
+                       ts.tv_sec = leap;
+                       ts.tv_nsec = 0;
+                       tk_set_wall_to_mono(tk,
+                               timespec_sub(tk->wall_to_monotonic, ts));
+
+                       clock_was_set_delayed();
+               }
        }
 }
 
-
 /**
  * logarithmic_accumulation - shifted accumulation of cycles
  *
@@ -1028,42 +1053,40 @@ static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
  *
  * Returns the unconsumed cycles.
  */
-static cycle_t logarithmic_accumulation(cycle_t offset, u32 shift)
+static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
+                                               u32 shift)
 {
        u64 raw_nsecs;
 
-       /* If the offset is smaller than a shifted interval, do nothing */
-       if (offset < timekeeper.cycle_interval<<shift)
+       /* If the offset is smaller then a shifted interval, do nothing */
+       if (offset < tk->cycle_interval<<shift)
                return offset;
 
        /* Accumulate one shifted interval */
-       offset -= timekeeper.cycle_interval << shift;
-       timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
-
-       timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
+       offset -= tk->cycle_interval << shift;
+       tk->clock->cycle_last += tk->cycle_interval << shift;
 
-       accumulate_nsecs_to_secs(&timekeeper);
+       tk->xtime_nsec += tk->xtime_interval << shift;
+       accumulate_nsecs_to_secs(tk);
 
        /* Accumulate raw time */
-       raw_nsecs = timekeeper.raw_interval << shift;
-       raw_nsecs += timekeeper.raw_time.tv_nsec;
+       raw_nsecs = tk->raw_interval << shift;
+       raw_nsecs += tk->raw_time.tv_nsec;
        if (raw_nsecs >= NSEC_PER_SEC) {
                u64 raw_secs = raw_nsecs;
                raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
-               timekeeper.raw_time.tv_sec += raw_secs;
+               tk->raw_time.tv_sec += raw_secs;
        }
-       timekeeper.raw_time.tv_nsec = raw_nsecs;
+       tk->raw_time.tv_nsec = raw_nsecs;
 
        /* Accumulate error between NTP and clock interval */
-       timekeeper.ntp_error += ntp_tick_length() << shift;
-       timekeeper.ntp_error -=
-           (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
-                               (timekeeper.ntp_error_shift + shift);
+       tk->ntp_error += ntp_tick_length() << shift;
+       tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
+                                               (tk->ntp_error_shift + shift);
 
        return offset;
 }
 
-
 /**
  * update_wall_time - Uses the current clocksource to increment the wall time
  *
@@ -1104,35 +1127,14 @@ static void update_wall_time(void)
        maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
        shift = min(shift, maxshift);
        while (offset >= timekeeper.cycle_interval) {
-               offset = logarithmic_accumulation(offset, shift);
+               offset = logarithmic_accumulation(&timekeeper, offset, shift);
                if(offset < timekeeper.cycle_interval<<shift)
                        shift--;
        }
 
        /* correct the clock when NTP error is too big */
-       timekeeping_adjust(offset);
+       timekeeping_adjust(&timekeeper, offset);
 
-       /*
-        * Since in the loop above, we accumulate any amount of time
-        * in xtime_nsec over a second into xtime.tv_sec, its possible for
-        * xtime_nsec to be fairly small after the loop. Further, if we're
-        * slightly speeding the clocksource up in timekeeping_adjust(),
-        * its possible the required corrective factor to xtime_nsec could
-        * cause it to underflow.
-        *
-        * Now, we cannot simply roll the accumulated second back, since
-        * the NTP subsystem has been notified via second_overflow. So
-        * instead we push xtime_nsec forward by the amount we underflowed,
-        * and add that amount into the error.
-        *
-        * We'll correct this error next time through this function, when
-        * xtime_nsec is not as small.
-        */
-       if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
-               s64 neg = -(s64)timekeeper.xtime_nsec;
-               timekeeper.xtime_nsec = 0;
-               timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
-       }
 
        /*
        * Store only full nanoseconds into xtime_nsec after rounding
@@ -1154,7 +1156,7 @@ static void update_wall_time(void)
         */
        accumulate_nsecs_to_secs(&timekeeper);
 
-       timekeeping_update(false);
+       timekeeping_update(&timekeeper, false);
 
 out:
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
@@ -1185,7 +1187,6 @@ void getboottime(struct timespec *ts)
 }
 EXPORT_SYMBOL_GPL(getboottime);
 
-
 /**
  * get_monotonic_boottime - Returns monotonic time since boot
  * @ts:                pointer to the timespec to be set
@@ -1205,7 +1206,7 @@ void get_monotonic_boottime(struct timespec *ts)
        do {
                seq = read_seqbegin(&timekeeper.lock);
                ts->tv_sec = timekeeper.xtime_sec;
-               ts->tv_nsec = timekeeping_get_ns();
+               ts->tv_nsec = timekeeping_get_ns(&timekeeper);
                tomono = timekeeper.wall_to_monotonic;
                sleep = timekeeper.total_sleep_time;
 
@@ -1337,9 +1338,7 @@ ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
                seq = read_seqbegin(&timekeeper.lock);
 
                secs = timekeeper.xtime_sec;
-               nsecs = timekeeping_get_ns();
-               /* If arch requires, add in gettimeoffset() */
-               nsecs += arch_gettimeoffset();
+               nsecs = timekeeping_get_ns(&timekeeper);
 
                *offs_real = timekeeper.offs_real;
                *offs_boot = timekeeper.offs_boot;
@@ -1368,7 +1367,6 @@ ktime_t ktime_get_monotonic_offset(void)
 }
 EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
 
-
 /**
  * xtime_update() - advances the timekeeping infrastructure
  * @ticks:     number of ticks, that have elapsed since the last call.