]> 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 5df2e7e556caa52d4e46715fd100dbd9af18f03d..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;
@@ -69,6 +69,9 @@ struct timekeeper {
        struct timespec total_sleep_time;
        /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
        struct timespec raw_time;
+
+       /* Seqlock for all timekeeper values */
+       seqlock_t lock;
 };
 
 static struct timekeeper timekeeper;
@@ -169,13 +172,28 @@ 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);
+
 }
 
 /**
@@ -222,7 +240,7 @@ void getnstimeofday(struct timespec *ts)
        WARN_ON(timekeeping_suspended);
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
 
                *ts = timekeeper.xtime;
                nsecs = timekeeping_get_ns();
@@ -230,7 +248,7 @@ void getnstimeofday(struct timespec *ts)
                /* If arch requires, add in gettimeoffset() */
                nsecs += arch_gettimeoffset();
 
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 
        timespec_add_ns(ts, nsecs);
 }
@@ -245,7 +263,7 @@ ktime_t ktime_get(void)
        WARN_ON(timekeeping_suspended);
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
                secs = timekeeper.xtime.tv_sec +
                                timekeeper.wall_to_monotonic.tv_sec;
                nsecs = timekeeper.xtime.tv_nsec +
@@ -254,7 +272,7 @@ ktime_t ktime_get(void)
                /* If arch requires, add in gettimeoffset() */
                nsecs += arch_gettimeoffset();
 
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
        /*
         * Use ktime_set/ktime_add_ns to create a proper ktime on
         * 32-bit architectures without CONFIG_KTIME_SCALAR.
@@ -280,14 +298,14 @@ void ktime_get_ts(struct timespec *ts)
        WARN_ON(timekeeping_suspended);
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
                *ts = timekeeper.xtime;
                tomono = timekeeper.wall_to_monotonic;
                nsecs = timekeeping_get_ns();
                /* If arch requires, add in gettimeoffset() */
                nsecs += arch_gettimeoffset();
 
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 
        set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
                                ts->tv_nsec + tomono.tv_nsec + nsecs);
@@ -315,7 +333,7 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
        do {
                u32 arch_offset;
 
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
 
                *ts_raw = timekeeper.raw_time;
                *ts_real = timekeeper.xtime;
@@ -328,7 +346,7 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
                nsecs_raw += arch_offset;
                nsecs_real += arch_offset;
 
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 
        timespec_add_ns(ts_raw, nsecs_raw);
        timespec_add_ns(ts_real, nsecs_real);
@@ -367,7 +385,7 @@ int do_settimeofday(const struct timespec *tv)
        if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
                return -EINVAL;
 
-       write_seqlock_irqsave(&xtime_lock, flags);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
 
        timekeeping_forward_now();
 
@@ -377,14 +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(&xtime_lock, flags);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        /* signal hrtimers about time change */
        clock_was_set();
@@ -408,7 +421,7 @@ int timekeeping_inject_offset(struct timespec *ts)
        if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
                return -EINVAL;
 
-       write_seqlock_irqsave(&xtime_lock, flags);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
 
        timekeeping_forward_now();
 
@@ -416,13 +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(&xtime_lock, flags);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        /* signal hrtimers about time change */
        clock_was_set();
@@ -494,11 +503,11 @@ void getrawmonotonic(struct timespec *ts)
        s64 nsecs;
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
                nsecs = timekeeping_get_ns_raw();
                *ts = timekeeper.raw_time;
 
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 
        timespec_add_ns(ts, nsecs);
 }
@@ -514,24 +523,30 @@ int timekeeping_valid_for_hres(void)
        int ret;
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
 
                ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
 
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 
        return ret;
 }
 
 /**
  * timekeeping_max_deferment - Returns max time the clocksource can be deferred
- *
- * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
- * ensure that the clocksource does not change!
  */
 u64 timekeeping_max_deferment(void)
 {
-       return timekeeper.clock->max_idle_ns;
+       unsigned long seq;
+       u64 ret;
+       do {
+               seq = read_seqbegin(&timekeeper.lock);
+
+               ret = timekeeper.clock->max_idle_ns;
+
+       } while (read_seqretry(&timekeeper.lock, seq));
+
+       return ret;
 }
 
 /**
@@ -576,10 +591,11 @@ void __init timekeeping_init(void)
        read_persistent_clock(&now);
        read_boot_clock(&boot);
 
-       write_seqlock_irqsave(&xtime_lock, flags);
+       seqlock_init(&timekeeper.lock);
 
        ntp_init();
 
+       write_seqlock_irqsave(&timekeeper.lock, flags);
        clock = clocksource_default_clock();
        if (clock->enable)
                clock->enable(clock);
@@ -597,7 +613,7 @@ void __init timekeeping_init(void)
                                -boot.tv_sec, -boot.tv_nsec);
        timekeeper.total_sleep_time.tv_sec = 0;
        timekeeper.total_sleep_time.tv_nsec = 0;
-       write_sequnlock_irqrestore(&xtime_lock, flags);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 }
 
 /* time in seconds when suspend began */
@@ -646,17 +662,15 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
        if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
                return;
 
-       write_seqlock_irqsave(&xtime_lock, flags);
+       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(&xtime_lock, flags);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        /* signal hrtimers about time change */
        clock_was_set();
@@ -679,7 +693,7 @@ static void timekeeping_resume(void)
 
        clocksource_resume();
 
-       write_seqlock_irqsave(&xtime_lock, flags);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
 
        if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
                ts = timespec_sub(ts, timekeeping_suspend_time);
@@ -689,7 +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(&xtime_lock, flags);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        touch_softlockup_watchdog();
 
@@ -707,7 +721,7 @@ static int timekeeping_suspend(void)
 
        read_persistent_clock(&timekeeping_suspend_time);
 
-       write_seqlock_irqsave(&xtime_lock, flags);
+       write_seqlock_irqsave(&timekeeper.lock, flags);
        timekeeping_forward_now();
        timekeeping_suspended = 1;
 
@@ -730,7 +744,7 @@ static int timekeeping_suspend(void)
                timekeeping_suspend_time =
                        timespec_add(timekeeping_suspend_time, delta_delta);
        }
-       write_sequnlock_irqrestore(&xtime_lock, flags);
+       write_sequnlock_irqrestore(&timekeeper.lock, flags);
 
        clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
        clocksource_suspend();
@@ -781,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;
 
@@ -964,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);
@@ -976,17 +990,19 @@ 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)
 {
        struct clocksource *clock;
        cycle_t offset;
        int shift = 0, maxshift;
+       unsigned long flags;
+
+       write_seqlock_irqsave(&timekeeper.lock, flags);
 
        /* Make sure we're fully resumed: */
        if (unlikely(timekeeping_suspended))
-               return;
+               goto out;
 
        clock = timekeeper.clock;
 
@@ -1009,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);
@@ -1064,9 +1080,11 @@ 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);
+
 }
 
 /**
@@ -1112,13 +1130,13 @@ void get_monotonic_boottime(struct timespec *ts)
        WARN_ON(timekeeping_suspended);
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
                *ts = timekeeper.xtime;
                tomono = timekeeper.wall_to_monotonic;
                sleep = timekeeper.total_sleep_time;
                nsecs = timekeeping_get_ns();
 
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 
        set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
                        ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
@@ -1169,10 +1187,10 @@ struct timespec current_kernel_time(void)
        unsigned long seq;
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
 
                now = timekeeper.xtime;
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 
        return now;
 }
@@ -1184,11 +1202,11 @@ struct timespec get_monotonic_coarse(void)
        unsigned long seq;
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
 
                now = timekeeper.xtime;
                mono = timekeeper.wall_to_monotonic;
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 
        set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
                                now.tv_nsec + mono.tv_nsec);
@@ -1220,11 +1238,11 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
        unsigned long seq;
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
                *xtim = timekeeper.xtime;
                *wtom = timekeeper.wall_to_monotonic;
                *sleep = timekeeper.total_sleep_time;
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
 }
 
 /**
@@ -1236,11 +1254,14 @@ ktime_t ktime_get_monotonic_offset(void)
        struct timespec wtom;
 
        do {
-               seq = read_seqbegin(&xtime_lock);
+               seq = read_seqbegin(&timekeeper.lock);
                wtom = timekeeper.wall_to_monotonic;
-       } while (read_seqretry(&xtime_lock, seq));
+       } while (read_seqretry(&timekeeper.lock, seq));
+
        return timespec_to_ktime(wtom);
 }
+EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
+
 
 /**
  * xtime_update() - advances the timekeeping infrastructure