[PATCH] hrtimers: pass current time to hrtimer_forward()
[safe/jmp/linux-2.6] / kernel / hrtimer.c
index efff949..e989c99 100644 (file)
  *  Credits:
  *     based on kernel/timer.c
  *
+ *     Help, testing, suggestions, bugfixes, improvements were
+ *     provided by:
+ *
+ *     George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
+ *     et. al.
+ *
  *  For licencing details see kernel-base/COPYING
  */
 
@@ -117,6 +123,26 @@ void ktime_get_ts(struct timespec *ts)
 EXPORT_SYMBOL_GPL(ktime_get_ts);
 
 /*
+ * Get the coarse grained time at the softirq based on xtime and
+ * wall_to_monotonic.
+ */
+static void hrtimer_get_softirq_time(struct hrtimer_base *base)
+{
+       ktime_t xtim, tomono;
+       unsigned long seq;
+
+       do {
+               seq = read_seqbegin(&xtime_lock);
+               xtim = timespec_to_ktime(xtime);
+               tomono = timespec_to_ktime(wall_to_monotonic);
+
+       } while (read_seqretry(&xtime_lock, seq));
+
+       base[CLOCK_REALTIME].softirq_time = xtim;
+       base[CLOCK_MONOTONIC].softirq_time = ktime_add(xtim, tomono);
+}
+
+/*
  * Functions and macros which are different for UP/SMP systems are kept in a
  * single place
  */
@@ -275,18 +301,17 @@ void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  * hrtimer_forward - forward the timer expiry
  *
  * @timer:     hrtimer to forward
+ * @now:       forward past this time
  * @interval:  the interval to forward
  *
  * Forward the timer expiry so it will expire in the future.
  * Returns the number of overruns.
  */
 unsigned long
-hrtimer_forward(struct hrtimer *timer, ktime_t interval)
+hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
 {
        unsigned long orun = 1;
-       ktime_t delta, now;
-
-       now = timer->base->get_time();
+       ktime_t delta;
 
        delta = ktime_sub(now, timer->expires);
 
@@ -412,8 +437,19 @@ hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
        /* Switch the timer base, if necessary: */
        new_base = switch_hrtimer_base(timer, base);
 
-       if (mode == HRTIMER_REL)
+       if (mode == HRTIMER_REL) {
                tim = ktime_add(tim, new_base->get_time());
+               /*
+                * CONFIG_TIME_LOW_RES is a temporary way for architectures
+                * to signal that they simply return xtime in
+                * do_gettimeoffset(). In this case we want to round up by
+                * resolution when starting a relative timer, to avoid short
+                * timeouts. This will go away with the GTOD framework.
+                */
+#ifdef CONFIG_TIME_LOW_RES
+               tim = ktime_add(tim, base->resolution);
+#endif
+       }
        timer->expires = tim;
 
        enqueue_hrtimer(timer, new_base);
@@ -488,6 +524,41 @@ ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
        return rem;
 }
 
+#ifdef CONFIG_NO_IDLE_HZ
+/**
+ * hrtimer_get_next_event - get the time until next expiry event
+ *
+ * Returns the delta to the next expiry event or KTIME_MAX if no timer
+ * is pending.
+ */
+ktime_t hrtimer_get_next_event(void)
+{
+       struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
+       ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
+       unsigned long flags;
+       int i;
+
+       for (i = 0; i < MAX_HRTIMER_BASES; i++, base++) {
+               struct hrtimer *timer;
+
+               spin_lock_irqsave(&base->lock, flags);
+               if (!base->first) {
+                       spin_unlock_irqrestore(&base->lock, flags);
+                       continue;
+               }
+               timer = rb_entry(base->first, struct hrtimer, node);
+               delta.tv64 = timer->expires.tv64;
+               spin_unlock_irqrestore(&base->lock, flags);
+               delta = ktime_sub(delta, base->get_time());
+               if (delta.tv64 < mindelta.tv64)
+                       mindelta.tv64 = delta.tv64;
+       }
+       if (mindelta.tv64 < 0)
+               mindelta.tv64 = 0;
+       return mindelta;
+}
+#endif
+
 /**
  * hrtimer_init - initialize a timer to the given clock
  *
@@ -534,9 +605,11 @@ int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  */
 static inline void run_hrtimer_queue(struct hrtimer_base *base)
 {
-       ktime_t now = base->get_time();
        struct rb_node *node;
 
+       if (base->get_softirq_time)
+               base->softirq_time = base->get_softirq_time();
+
        spin_lock_irq(&base->lock);
 
        while ((node = base->first)) {
@@ -546,7 +619,7 @@ static inline void run_hrtimer_queue(struct hrtimer_base *base)
                void *data;
 
                timer = rb_entry(node, struct hrtimer, node);
-               if (now.tv64 <= timer->expires.tv64)
+               if (base->softirq_time.tv64 <= timer->expires.tv64)
                        break;
 
                fn = timer->function;
@@ -589,6 +662,8 @@ void hrtimer_run_queues(void)
        struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
        int i;
 
+       hrtimer_get_softirq_time(base);
+
        for (i = 0; i < MAX_HRTIMER_BASES; i++)
                run_hrtimer_queue(&base[i]);
 }