Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/perex/alsa
[sfrench/cifs-2.6.git] / kernel / hrtimer.c
index dc8a4451d79b15d64a37caf55c9dc4595c6ec5d2..bd5d6b5060bcd5704c278ea38954862fb463889f 100644 (file)
@@ -325,6 +325,22 @@ unsigned long ktime_divns(const ktime_t kt, s64 div)
 }
 #endif /* BITS_PER_LONG >= 64 */
 
+/*
+ * Check, whether the timer is on the callback pending list
+ */
+static inline int hrtimer_cb_pending(const struct hrtimer *timer)
+{
+       return timer->state & HRTIMER_STATE_PENDING;
+}
+
+/*
+ * Remove a timer from the callback pending list
+ */
+static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
+{
+       list_del_init(&timer->cb_entry);
+}
+
 /* High resolution timer related functions */
 #ifdef CONFIG_HIGH_RES_TIMERS
 
@@ -412,7 +428,7 @@ static int hrtimer_reprogram(struct hrtimer *timer,
        /*
         * When the callback is running, we do not reprogram the clock event
         * device. The timer callback is either running on a different CPU or
-        * the callback is executed in the hrtimer_interupt context. The
+        * the callback is executed in the hrtimer_interrupt context. The
         * reprogramming is handled either by the softirq, which called the
         * callback or at the end of the hrtimer_interrupt.
         */
@@ -493,22 +509,6 @@ void hres_timers_resume(void)
        retrigger_next_event(NULL);
 }
 
-/*
- * Check, whether the timer is on the callback pending list
- */
-static inline int hrtimer_cb_pending(const struct hrtimer *timer)
-{
-       return timer->state & HRTIMER_STATE_PENDING;
-}
-
-/*
- * Remove a timer from the callback pending list
- */
-static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
-{
-       list_del_init(&timer->cb_entry);
-}
-
 /*
  * Initialize the high resolution related parts of cpu_base
  */
@@ -516,7 +516,6 @@ static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
 {
        base->expires_next.tv64 = KTIME_MAX;
        base->hres_active = 0;
-       INIT_LIST_HEAD(&base->cb_pending);
 }
 
 /*
@@ -524,7 +523,6 @@ static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  */
 static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
 {
-       INIT_LIST_HEAD(&timer->cb_entry);
 }
 
 /*
@@ -602,7 +600,7 @@ static int hrtimer_switch_to_hres(void)
        /* "Retrigger" the interrupt to get things going */
        retrigger_next_event(NULL);
        local_irq_restore(flags);
-       printk(KERN_INFO "Switched to high resolution mode on CPU %d\n",
+       printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
               smp_processor_id());
        return 1;
 }
@@ -618,10 +616,13 @@ static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
 {
        return 0;
 }
-static inline int hrtimer_cb_pending(struct hrtimer *timer) { return 0; }
-static inline void hrtimer_remove_cb_pending(struct hrtimer *timer) { }
 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
 static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
+static inline int hrtimer_reprogram(struct hrtimer *timer,
+                                   struct hrtimer_clock_base *base)
+{
+       return 0;
+}
 
 #endif /* CONFIG_HIGH_RES_TIMERS */
 
@@ -638,7 +639,7 @@ void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
 #endif
 
 /*
- * Counterpart to lock_timer_base above:
+ * Counterpart to lock_hrtimer_base above:
  */
 static inline
 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
@@ -850,6 +851,14 @@ hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
 #ifdef CONFIG_TIME_LOW_RES
                tim = ktime_add(tim, base->resolution);
 #endif
+               /*
+                * Careful here: User space might have asked for a
+                * very long sleep, so the add above might result in a
+                * negative number, which enqueues the timer in front
+                * of the queue.
+                */
+               if (tim.tv64 < 0)
+                       tim.tv64 = KTIME_MAX;
        }
        timer->expires = tim;
 
@@ -993,6 +1002,7 @@ void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
                clock_id = CLOCK_MONOTONIC;
 
        timer->base = &cpu_base->clock_base[clock_id];
+       INIT_LIST_HEAD(&timer->cb_entry);
        hrtimer_init_timer_hres(timer);
 
 #ifdef CONFIG_TIMER_STATS
@@ -1022,6 +1032,85 @@ int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
 }
 EXPORT_SYMBOL_GPL(hrtimer_get_res);
 
+static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
+{
+       spin_lock_irq(&cpu_base->lock);
+
+       while (!list_empty(&cpu_base->cb_pending)) {
+               enum hrtimer_restart (*fn)(struct hrtimer *);
+               struct hrtimer *timer;
+               int restart;
+
+               timer = list_entry(cpu_base->cb_pending.next,
+                                  struct hrtimer, cb_entry);
+
+               timer_stats_account_hrtimer(timer);
+
+               fn = timer->function;
+               __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
+               spin_unlock_irq(&cpu_base->lock);
+
+               restart = fn(timer);
+
+               spin_lock_irq(&cpu_base->lock);
+
+               timer->state &= ~HRTIMER_STATE_CALLBACK;
+               if (restart == HRTIMER_RESTART) {
+                       BUG_ON(hrtimer_active(timer));
+                       /*
+                        * Enqueue the timer, allow reprogramming of the event
+                        * device
+                        */
+                       enqueue_hrtimer(timer, timer->base, 1);
+               } else if (hrtimer_active(timer)) {
+                       /*
+                        * If the timer was rearmed on another CPU, reprogram
+                        * the event device.
+                        */
+                       if (timer->base->first == &timer->node)
+                               hrtimer_reprogram(timer, timer->base);
+               }
+       }
+       spin_unlock_irq(&cpu_base->lock);
+}
+
+static void __run_hrtimer(struct hrtimer *timer)
+{
+       struct hrtimer_clock_base *base = timer->base;
+       struct hrtimer_cpu_base *cpu_base = base->cpu_base;
+       enum hrtimer_restart (*fn)(struct hrtimer *);
+       int restart;
+
+       __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
+       timer_stats_account_hrtimer(timer);
+
+       fn = timer->function;
+       if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
+               /*
+                * Used for scheduler timers, avoid lock inversion with
+                * rq->lock and tasklist_lock.
+                *
+                * These timers are required to deal with enqueue expiry
+                * themselves and are not allowed to migrate.
+                */
+               spin_unlock(&cpu_base->lock);
+               restart = fn(timer);
+               spin_lock(&cpu_base->lock);
+       } else
+               restart = fn(timer);
+
+       /*
+        * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
+        * reprogramming of the event hardware. This happens at the end of this
+        * function anyway.
+        */
+       if (restart != HRTIMER_NORESTART) {
+               BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
+               enqueue_hrtimer(timer, base, 0);
+       }
+       timer->state &= ~HRTIMER_STATE_CALLBACK;
+}
+
 #ifdef CONFIG_HIGH_RES_TIMERS
 
 /*
@@ -1079,21 +1168,7 @@ void hrtimer_interrupt(struct clock_event_device *dev)
                                continue;
                        }
 
-                       __remove_hrtimer(timer, base,
-                                        HRTIMER_STATE_CALLBACK, 0);
-                       timer_stats_account_hrtimer(timer);
-
-                       /*
-                        * Note: We clear the CALLBACK bit after
-                        * enqueue_hrtimer to avoid reprogramming of
-                        * the event hardware. This happens at the end
-                        * of this function anyway.
-                        */
-                       if (timer->function(timer) != HRTIMER_NORESTART) {
-                               BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
-                               enqueue_hrtimer(timer, base, 0);
-                       }
-                       timer->state &= ~HRTIMER_STATE_CALLBACK;
+                       __run_hrtimer(timer);
                }
                spin_unlock(&cpu_base->lock);
                base++;
@@ -1114,52 +1189,41 @@ void hrtimer_interrupt(struct clock_event_device *dev)
 
 static void run_hrtimer_softirq(struct softirq_action *h)
 {
-       struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
-
-       spin_lock_irq(&cpu_base->lock);
-
-       while (!list_empty(&cpu_base->cb_pending)) {
-               enum hrtimer_restart (*fn)(struct hrtimer *);
-               struct hrtimer *timer;
-               int restart;
-
-               timer = list_entry(cpu_base->cb_pending.next,
-                                  struct hrtimer, cb_entry);
+       run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
+}
 
-               timer_stats_account_hrtimer(timer);
+#endif /* CONFIG_HIGH_RES_TIMERS */
 
-               fn = timer->function;
-               __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
-               spin_unlock_irq(&cpu_base->lock);
+/*
+ * Called from timer softirq every jiffy, expire hrtimers:
+ *
+ * For HRT its the fall back code to run the softirq in the timer
+ * softirq context in case the hrtimer initialization failed or has
+ * not been done yet.
+ */
+void hrtimer_run_pending(void)
+{
+       struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
 
-               restart = fn(timer);
+       if (hrtimer_hres_active())
+               return;
 
-               spin_lock_irq(&cpu_base->lock);
+       /*
+        * This _is_ ugly: We have to check in the softirq context,
+        * whether we can switch to highres and / or nohz mode. The
+        * clocksource switch happens in the timer interrupt with
+        * xtime_lock held. Notification from there only sets the
+        * check bit in the tick_oneshot code, otherwise we might
+        * deadlock vs. xtime_lock.
+        */
+       if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
+               hrtimer_switch_to_hres();
 
-               timer->state &= ~HRTIMER_STATE_CALLBACK;
-               if (restart == HRTIMER_RESTART) {
-                       BUG_ON(hrtimer_active(timer));
-                       /*
-                        * Enqueue the timer, allow reprogramming of the event
-                        * device
-                        */
-                       enqueue_hrtimer(timer, timer->base, 1);
-               } else if (hrtimer_active(timer)) {
-                       /*
-                        * If the timer was rearmed on another CPU, reprogram
-                        * the event device.
-                        */
-                       if (timer->base->first == &timer->node)
-                               hrtimer_reprogram(timer, timer->base);
-               }
-       }
-       spin_unlock_irq(&cpu_base->lock);
+       run_hrtimer_pending(cpu_base);
 }
 
-#endif /* CONFIG_HIGH_RES_TIMERS */
-
 /*
- * Expire the per base hrtimer-queue:
+ * Called from hardirq context every jiffy
  */
 static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
                                     int index)
@@ -1173,46 +1237,27 @@ static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
        if (base->get_softirq_time)
                base->softirq_time = base->get_softirq_time();
 
-       spin_lock_irq(&cpu_base->lock);
+       spin_lock(&cpu_base->lock);
 
        while ((node = base->first)) {
                struct hrtimer *timer;
-               enum hrtimer_restart (*fn)(struct hrtimer *);
-               int restart;
 
                timer = rb_entry(node, struct hrtimer, node);
                if (base->softirq_time.tv64 <= timer->expires.tv64)
                        break;
 
-#ifdef CONFIG_HIGH_RES_TIMERS
-               WARN_ON_ONCE(timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ);
-#endif
-               timer_stats_account_hrtimer(timer);
-
-               fn = timer->function;
-               __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
-               spin_unlock_irq(&cpu_base->lock);
-
-               restart = fn(timer);
-
-               spin_lock_irq(&cpu_base->lock);
-
-               timer->state &= ~HRTIMER_STATE_CALLBACK;
-               if (restart != HRTIMER_NORESTART) {
-                       BUG_ON(hrtimer_active(timer));
-                       enqueue_hrtimer(timer, base, 0);
+               if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
+                       __remove_hrtimer(timer, base, HRTIMER_STATE_PENDING, 0);
+                       list_add_tail(&timer->cb_entry,
+                                       &base->cpu_base->cb_pending);
+                       continue;
                }
+
+               __run_hrtimer(timer);
        }
-       spin_unlock_irq(&cpu_base->lock);
+       spin_unlock(&cpu_base->lock);
 }
 
-/*
- * Called from timer softirq every jiffy, expire hrtimers:
- *
- * For HRT its the fall back code to run the softirq in the timer
- * softirq context in case the hrtimer initialization failed or has
- * not been done yet.
- */
 void hrtimer_run_queues(void)
 {
        struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
@@ -1221,18 +1266,6 @@ void hrtimer_run_queues(void)
        if (hrtimer_hres_active())
                return;
 
-       /*
-        * This _is_ ugly: We have to check in the softirq context,
-        * whether we can switch to highres and / or nohz mode. The
-        * clocksource switch happens in the timer interrupt with
-        * xtime_lock held. Notification from there only sets the
-        * check bit in the tick_oneshot code, otherwise we might
-        * deadlock vs. xtime_lock.
-        */
-       if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
-               if (hrtimer_switch_to_hres())
-                       return;
-
        hrtimer_get_softirq_time(cpu_base);
 
        for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
@@ -1260,7 +1293,7 @@ void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
        sl->timer.function = hrtimer_wakeup;
        sl->task = task;
 #ifdef CONFIG_HIGH_RES_TIMERS
-       sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_RESTART;
+       sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
 #endif
 }
 
@@ -1271,6 +1304,8 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod
        do {
                set_current_state(TASK_INTERRUPTIBLE);
                hrtimer_start(&t->timer, t->timer.expires, mode);
+               if (!hrtimer_active(&t->timer))
+                       t->task = NULL;
 
                if (likely(t->task))
                        schedule();
@@ -1286,8 +1321,7 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod
 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
 {
        struct hrtimer_sleeper t;
-       struct timespec __user *rmtp;
-       struct timespec tu;
+       struct timespec *rmtp;
        ktime_t time;
 
        restart->fn = do_no_restart_syscall;
@@ -1298,14 +1332,12 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
        if (do_nanosleep(&t, HRTIMER_MODE_ABS))
                return 0;
 
-       rmtp = (struct timespec __user *) restart->arg1;
+       rmtp = (struct timespec *)restart->arg1;
        if (rmtp) {
                time = ktime_sub(t.timer.expires, t.timer.base->get_time());
                if (time.tv64 <= 0)
                        return 0;
-               tu = ktime_to_timespec(time);
-               if (copy_to_user(rmtp, &tu, sizeof(tu)))
-                       return -EFAULT;
+               *rmtp = ktime_to_timespec(time);
        }
 
        restart->fn = hrtimer_nanosleep_restart;
@@ -1314,12 +1346,11 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
        return -ERESTART_RESTARTBLOCK;
 }
 
-long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
+long hrtimer_nanosleep(struct timespec *rqtp, struct timespec *rmtp,
                       const enum hrtimer_mode mode, const clockid_t clockid)
 {
        struct restart_block *restart;
        struct hrtimer_sleeper t;
-       struct timespec tu;
        ktime_t rem;
 
        hrtimer_init(&t.timer, clockid, mode);
@@ -1335,9 +1366,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
                rem = ktime_sub(t.timer.expires, t.timer.base->get_time());
                if (rem.tv64 <= 0)
                        return 0;
-               tu = ktime_to_timespec(rem);
-               if (copy_to_user(rmtp, &tu, sizeof(tu)))
-                       return -EFAULT;
+               *rmtp = ktime_to_timespec(rem);
        }
 
        restart = &current_thread_info()->restart_block;
@@ -1353,7 +1382,8 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
 asmlinkage long
 sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
 {
-       struct timespec tu;
+       struct timespec tu, rmt;
+       int ret;
 
        if (copy_from_user(&tu, rqtp, sizeof(tu)))
                return -EFAULT;
@@ -1361,13 +1391,21 @@ sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
        if (!timespec_valid(&tu))
                return -EINVAL;
 
-       return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
+       ret = hrtimer_nanosleep(&tu, rmtp ? &rmt : NULL, HRTIMER_MODE_REL,
+                               CLOCK_MONOTONIC);
+
+       if (ret && rmtp) {
+               if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
+                       return -EFAULT;
+       }
+
+       return ret;
 }
 
 /*
  * Functions related to boot-time initialization:
  */
-static void __devinit init_hrtimers_cpu(int cpu)
+static void __cpuinit init_hrtimers_cpu(int cpu)
 {
        struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
        int i;
@@ -1378,6 +1416,7 @@ static void __devinit init_hrtimers_cpu(int cpu)
        for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
                cpu_base->clock_base[i].cpu_base = cpu_base;
 
+       INIT_LIST_HEAD(&cpu_base->cb_pending);
        hrtimer_init_hres(cpu_base);
 }