]> Pileus Git - ~andy/linux/blobdiff - kernel/time/timekeeping.c
time: Explicitly use u32 instead of int for shift values
[~andy/linux] / kernel / time / timekeeping.c
index 6f46a00a1e8a703118ab9d8be3905758f08185c8..4fd83df0b14dd57d5f55d14c7bf254244443e7d8 100644 (file)
 /* Structure holding internal timekeeping values. */
 struct timekeeper {
        /* Current clocksource used for timekeeping. */
-       struct clocksource *clock;
+       struct clocksource      *clock;
        /* NTP adjusted clock multiplier */
-       u32     mult;
+       u32                     mult;
        /* The shift value of the current clocksource. */
-       int     shift;
-
+       u32                     shift;
        /* Number of clock cycles in one NTP interval. */
-       cycle_t cycle_interval;
+       cycle_t                 cycle_interval;
        /* Number of clock shifted nano seconds in one NTP interval. */
-       u64     xtime_interval;
+       u64                     xtime_interval;
        /* shifted nano seconds left over when rounding cycle_interval */
-       s64     xtime_remainder;
+       s64                     xtime_remainder;
        /* Raw nano seconds accumulated per NTP interval. */
-       u32     raw_interval;
+       u32                     raw_interval;
 
        /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
-       u64     xtime_nsec;
+       u64                     xtime_nsec;
        /* Difference between accumulated time and NTP time in ntp
         * shifted nano seconds. */
-       s64     ntp_error;
+       s64                     ntp_error;
        /* Shift conversion between clock shifted nano seconds and
         * ntp shifted nano seconds. */
-       int     ntp_error_shift;
+       u32                     ntp_error_shift;
 
        /* The current time */
-       struct timespec xtime;
+       struct timespec         xtime;
        /*
         * wall_to_monotonic is what we need to add to xtime (or xtime corrected
         * for sub jiffie times) to get to monotonic time.  Monotonic is pegged
@@ -64,14 +63,17 @@ struct timekeeper {
         * - wall_to_monotonic is no longer the boot time, getboottime must be
         * used instead.
         */
-       struct timespec wall_to_monotonic;
+       struct timespec         wall_to_monotonic;
        /* time spent in suspend */
-       struct timespec total_sleep_time;
+       struct timespec         total_sleep_time;
        /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
-       struct timespec raw_time;
-
+       struct timespec         raw_time;
+       /* Offset clock monotonic -> clock realtime */
+       ktime_t                 offs_real;
+       /* Offset clock monotonic -> clock boottime */
+       ktime_t                 offs_boot;
        /* Seqlock for all timekeeper values */
-       seqlock_t lock;
+       seqlock_t               lock;
 };
 
 static struct timekeeper timekeeper;
@@ -172,6 +174,14 @@ static inline s64 timekeeping_get_ns_raw(void)
        return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
 }
 
+static void update_rt_offset(void)
+{
+       struct timespec tmp, *wtm = &timekeeper.wall_to_monotonic;
+
+       set_normalized_timespec(&tmp, -wtm->tv_sec, -wtm->tv_nsec);
+       timekeeper.offs_real = timespec_to_ktime(tmp);
+}
+
 /* must hold write on timekeeper.lock */
 static void timekeeping_update(bool clearntp)
 {
@@ -179,6 +189,7 @@ static void timekeeping_update(bool clearntp)
                timekeeper.ntp_error = 0;
                ntp_clear();
        }
+       update_rt_offset();
        update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
                         timekeeper.clock, timekeeper.mult);
 }
@@ -532,6 +543,7 @@ u64 timekeeping_max_deferment(void)
 {
        unsigned long seq;
        u64 ret;
+
        do {
                seq = read_seqbegin(&timekeeper.lock);
 
@@ -604,6 +616,7 @@ void __init timekeeping_init(void)
        }
        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;
        write_sequnlock_irqrestore(&timekeeper.lock, flags);
@@ -612,6 +625,12 @@ void __init timekeeping_init(void)
 /* 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
@@ -630,8 +649,7 @@ static void __timekeeping_inject_sleeptime(struct timespec *delta)
        timekeeper.xtime = timespec_add(timekeeper.xtime, *delta);
        timekeeper.wall_to_monotonic =
                        timespec_sub(timekeeper.wall_to_monotonic, *delta);
-       timekeeper.total_sleep_time = timespec_add(
-                                       timekeeper.total_sleep_time, *delta);
+       update_sleep_time(timespec_add(timekeeper.total_sleep_time, *delta));
 }
 
 
@@ -942,7 +960,7 @@ static void timekeeping_adjust(s64 offset)
  *
  * Returns the unconsumed cycles.
  */
-static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
+static cycle_t logarithmic_accumulation(cycle_t offset, u32 shift)
 {
        u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
        u64 raw_nsecs;
@@ -963,6 +981,8 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
                leap = second_overflow(timekeeper.xtime.tv_sec);
                timekeeper.xtime.tv_sec += leap;
                timekeeper.wall_to_monotonic.tv_sec -= leap;
+               if (leap)
+                       clock_was_set_delayed();
        }
 
        /* Accumulate raw time */
@@ -1079,6 +1099,8 @@ static void update_wall_time(void)
                leap = second_overflow(timekeeper.xtime.tv_sec);
                timekeeper.xtime.tv_sec += leap;
                timekeeper.wall_to_monotonic.tv_sec -= leap;
+               if (leap)
+                       clock_was_set_delayed();
        }
 
        timekeeping_update(false);
@@ -1246,6 +1268,40 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
        } while (read_seqretry(&timekeeper.lock, seq));
 }
 
+#ifdef CONFIG_HIGH_RES_TIMERS
+/**
+ * ktime_get_update_offsets - hrtimer helper
+ * @offs_real: pointer to storage for monotonic -> realtime offset
+ * @offs_boot: pointer to storage for monotonic -> boottime offset
+ *
+ * Returns current monotonic time and updates the offsets
+ * Called from hrtimer_interupt() or retrigger_next_event()
+ */
+ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
+{
+       ktime_t now;
+       unsigned int seq;
+       u64 secs, nsecs;
+
+       do {
+               seq = read_seqbegin(&timekeeper.lock);
+
+               secs = timekeeper.xtime.tv_sec;
+               nsecs = timekeeper.xtime.tv_nsec;
+               nsecs += timekeeping_get_ns();
+               /* If arch requires, add in gettimeoffset() */
+               nsecs += arch_gettimeoffset();
+
+               *offs_real = timekeeper.offs_real;
+               *offs_boot = timekeeper.offs_boot;
+       } while (read_seqretry(&timekeeper.lock, seq));
+
+       now = ktime_add_ns(ktime_set(secs, 0), nsecs);
+       now = ktime_sub(now, *offs_real);
+       return now;
+}
+#endif
+
 /**
  * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
  */