]> Pileus Git - ~andy/linux/blobdiff - kernel/time/timekeeping.c
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzik/libata-dev
[~andy/linux] / kernel / time / timekeeping.c
index f5d4d226defb74f5065b03ddf49f04c4f9b86e09..15be32e19c6e916dd2b52c29bac9d2b93f9cdef8 100644 (file)
@@ -25,6 +25,8 @@
 struct timekeeper {
        /* Current clocksource used for timekeeping. */
        struct clocksource *clock;
+       /* NTP adjusted clock multiplier */
+       u32     mult;
        /* The shift value of the current clocksource. */
        int     shift;
 
@@ -45,8 +47,6 @@ struct timekeeper {
        /* Shift conversion between clock shifted nano seconds and
         * ntp shifted nano seconds. */
        int     ntp_error_shift;
-       /* NTP adjusted clock multiplier */
-       u32     mult;
 
        /* The current time */
        struct timespec xtime;
@@ -172,18 +172,26 @@ static inline s64 timekeeping_get_ns_raw(void)
        return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
 }
 
-/* must hold xtime_lock */
+/* must hold write on timekeeper.lock */
+static void timekeeping_update(bool clearntp)
+{
+       if (clearntp) {
+               timekeeper.ntp_error = 0;
+               ntp_clear();
+       }
+       update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
+                        timekeeper.clock, timekeeper.mult);
+}
+
+
 void timekeeping_leap_insert(int leapsecond)
 {
        unsigned long flags;
 
        write_seqlock_irqsave(&timekeeper.lock, flags);
-
        timekeeper.xtime.tv_sec += leapsecond;
        timekeeper.wall_to_monotonic.tv_sec -= leapsecond;
-       update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
-                        timekeeper.clock, timekeeper.mult);
-
+       timekeeping_update(false);
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
 }
@@ -372,13 +380,12 @@ EXPORT_SYMBOL(do_gettimeofday);
 int do_settimeofday(const struct timespec *tv)
 {
        struct timespec ts_delta;
-       unsigned long flags1,flags2;
+       unsigned long flags;
 
        if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
                return -EINVAL;
 
-       write_seqlock_irqsave(&xtime_lock, flags1);
-       write_seqlock_irqsave(&timekeeper.lock, flags2);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
 
        timekeeping_forward_now();
 
@@ -388,15 +395,9 @@ int do_settimeofday(const struct timespec *tv)
                        timespec_sub(timekeeper.wall_to_monotonic, ts_delta);
 
        timekeeper.xtime = *tv;
+       timekeeping_update(true);
 
-       timekeeper.ntp_error = 0;
-       ntp_clear();
-
-       update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
-                       timekeeper.clock, timekeeper.mult);
-
-       write_sequnlock_irqrestore(&timekeeper.lock, flags2);
-       write_sequnlock_irqrestore(&xtime_lock, flags1);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        /* signal hrtimers about time change */
        clock_was_set();
@@ -415,13 +416,12 @@ EXPORT_SYMBOL(do_settimeofday);
  */
 int timekeeping_inject_offset(struct timespec *ts)
 {
-       unsigned long flags1,flags2;
+       unsigned long flags;
 
        if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
                return -EINVAL;
 
-       write_seqlock_irqsave(&xtime_lock, flags1);
-       write_seqlock_irqsave(&timekeeper.lock, flags2);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
 
        timekeeping_forward_now();
 
@@ -429,14 +429,9 @@ int timekeeping_inject_offset(struct timespec *ts)
        timekeeper.wall_to_monotonic =
                                timespec_sub(timekeeper.wall_to_monotonic, *ts);
 
-       timekeeper.ntp_error = 0;
-       ntp_clear();
-
-       update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
-                       timekeeper.clock, timekeeper.mult);
+       timekeeping_update(true);
 
-       write_sequnlock_irqrestore(&timekeeper.lock, flags2);
-       write_sequnlock_irqrestore(&xtime_lock, flags1);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        /* signal hrtimers about time change */
        clock_was_set();
@@ -598,9 +593,7 @@ void __init timekeeping_init(void)
 
        seqlock_init(&timekeeper.lock);
 
-       write_seqlock_irqsave(&xtime_lock, flags);
        ntp_init();
-       write_sequnlock_irqrestore(&xtime_lock, flags);
 
        write_seqlock_irqsave(&timekeeper.lock, flags);
        clock = clocksource_default_clock();
@@ -661,7 +654,7 @@ static void __timekeeping_inject_sleeptime(struct timespec *delta)
  */
 void timekeeping_inject_sleeptime(struct timespec *delta)
 {
-       unsigned long flags1,flags2;
+       unsigned long flags;
        struct timespec ts;
 
        /* Make sure we don't set the clock twice */
@@ -669,20 +662,15 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
        if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
                return;
 
-       write_seqlock_irqsave(&xtime_lock, flags1);
-       write_seqlock_irqsave(&timekeeper.lock, flags2);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
 
        timekeeping_forward_now();
 
        __timekeeping_inject_sleeptime(delta);
 
-       timekeeper.ntp_error = 0;
-       ntp_clear();
-       update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
-                       timekeeper.clock, timekeeper.mult);
+       timekeeping_update(true);
 
-       write_sequnlock_irqrestore(&timekeeper.lock, flags2);
-       write_sequnlock_irqrestore(&xtime_lock, flags1);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        /* signal hrtimers about time change */
        clock_was_set();
@@ -698,15 +686,14 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
  */
 static void timekeeping_resume(void)
 {
-       unsigned long flags1,flags2;
+       unsigned long flags;
        struct timespec ts;
 
        read_persistent_clock(&ts);
 
        clocksource_resume();
 
-       write_seqlock_irqsave(&xtime_lock, flags1);
-       write_seqlock_irqsave(&timekeeper.lock, flags2);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
 
        if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
                ts = timespec_sub(ts, timekeeping_suspend_time);
@@ -716,8 +703,7 @@ static void timekeeping_resume(void)
        timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
        timekeeper.ntp_error = 0;
        timekeeping_suspended = 0;
-       write_sequnlock_irqrestore(&timekeeper.lock, flags2);
-       write_sequnlock_irqrestore(&xtime_lock, flags1);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        touch_softlockup_watchdog();
 
@@ -729,14 +715,13 @@ static void timekeeping_resume(void)
 
 static int timekeeping_suspend(void)
 {
-       unsigned long flags1,flags2;
+       unsigned long flags;
        struct timespec         delta, delta_delta;
        static struct timespec  old_delta;
 
        read_persistent_clock(&timekeeping_suspend_time);
 
-       write_seqlock_irqsave(&xtime_lock, flags1);
-       write_seqlock_irqsave(&timekeeper.lock, flags2);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
        timekeeping_forward_now();
        timekeeping_suspended = 1;
 
@@ -759,8 +744,7 @@ static int timekeeping_suspend(void)
                timekeeping_suspend_time =
                        timespec_add(timekeeping_suspend_time, delta_delta);
        }
-       write_sequnlock_irqrestore(&timekeeper.lock, flags2);
-       write_sequnlock_irqrestore(&xtime_lock, flags1);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
        clocksource_suspend();
@@ -811,7 +795,7 @@ 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 = tick_length >> (timekeeper.ntp_error_shift + 1);
+       tick_error = ntp_tick_length() >> (timekeeper.ntp_error_shift + 1);
        tick_error -= timekeeper.xtime_interval >> 1;
        error = ((error - tick_error) >> look_ahead) + tick_error;
 
@@ -994,7 +978,7 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
        timekeeper.raw_time.tv_nsec = raw_nsecs;
 
        /* Accumulate error between NTP and clock interval */
-       timekeeper.ntp_error += tick_length << shift;
+       timekeeper.ntp_error += ntp_tick_length() << shift;
        timekeeper.ntp_error -=
            (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
                                (timekeeper.ntp_error_shift + shift);
@@ -1006,7 +990,6 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
 /**
  * update_wall_time - Uses the current clocksource to increment the wall time
  *
- * Called from the timer interrupt, must hold a write on xtime_lock.
  */
 static void update_wall_time(void)
 {
@@ -1042,7 +1025,7 @@ static void update_wall_time(void)
        shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
        shift = max(0, shift);
        /* Bound shift to one less then what overflows tick_length */
-       maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
+       maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
        shift = min(shift, maxshift);
        while (offset >= timekeeper.cycle_interval) {
                offset = logarithmic_accumulation(offset, shift);
@@ -1097,9 +1080,7 @@ static void update_wall_time(void)
                second_overflow();
        }
 
-       /* check to see if there is a new clocksource to use */
-       update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
-                       timekeeper.clock, timekeeper.mult);
+       timekeeping_update(false);
 
 out:
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
@@ -1279,6 +1260,8 @@ ktime_t ktime_get_monotonic_offset(void)
 
        return timespec_to_ktime(wtom);
 }
+EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
+
 
 /**
  * xtime_update() - advances the timekeeping infrastructure