[PATCH] hrtimers: cleanup locking
[sfrench/cifs-2.6.git] / kernel / hrtimer.c
index be989efc785612c6331393bbe3f204a86877f379..89a9f535b4ceaa845722eeb6b6e588fc31f09653 100644 (file)
@@ -1,8 +1,9 @@
 /*
  *  linux/kernel/hrtimer.c
  *
- *  Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
- *  Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
+ *  Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
+ *  Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
+ *  Copyright(C) 2006      Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  *
  *  High-resolution kernel timers
  *
@@ -79,21 +80,22 @@ EXPORT_SYMBOL_GPL(ktime_get_real);
  * This ensures that we capture erroneous accesses to these clock ids
  * rather than moving them into the range of valid clock id's.
  */
-
-#define MAX_HRTIMER_BASES 2
-
-static DEFINE_PER_CPU(struct hrtimer_base, hrtimer_bases[MAX_HRTIMER_BASES]) =
+static DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
 {
+
+       .clock_base =
        {
-               .index = CLOCK_REALTIME,
-               .get_time = &ktime_get_real,
-               .resolution = KTIME_REALTIME_RES,
-       },
-       {
-               .index = CLOCK_MONOTONIC,
-               .get_time = &ktime_get,
-               .resolution = KTIME_MONOTONIC_RES,
-       },
+               {
+                       .index = CLOCK_REALTIME,
+                       .get_time = &ktime_get_real,
+                       .resolution = KTIME_REALTIME_RES,
+               },
+               {
+                       .index = CLOCK_MONOTONIC,
+                       .get_time = &ktime_get,
+                       .resolution = KTIME_MONOTONIC_RES,
+               },
+       }
 };
 
 /**
@@ -102,7 +104,7 @@ static DEFINE_PER_CPU(struct hrtimer_base, hrtimer_bases[MAX_HRTIMER_BASES]) =
  *
  * The function calculates the monotonic clock from the realtime
  * clock and the wall_to_monotonic offset and stores the result
- * in normalized timespec format in the variable pointed to by ts.
+ * in normalized timespec format in the variable pointed to by @ts.
  */
 void ktime_get_ts(struct timespec *ts)
 {
@@ -125,20 +127,26 @@ 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)
+static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
 {
        ktime_t xtim, tomono;
+       struct timespec xts;
        unsigned long seq;
 
        do {
                seq = read_seqbegin(&xtime_lock);
-               xtim = timespec_to_ktime(xtime);
-               tomono = timespec_to_ktime(wall_to_monotonic);
-
+#ifdef CONFIG_NO_HZ
+               getnstimeofday(&xts);
+#else
+               xts = xtime;
+#endif
        } while (read_seqretry(&xtime_lock, seq));
 
-       base[CLOCK_REALTIME].softirq_time = xtim;
-       base[CLOCK_MONOTONIC].softirq_time = ktime_add(xtim, tomono);
+       xtim = timespec_to_ktime(xts);
+       tomono = timespec_to_ktime(wall_to_monotonic);
+       base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
+       base->clock_base[CLOCK_MONOTONIC].softirq_time =
+               ktime_add(xtim, tomono);
 }
 
 /*
@@ -161,19 +169,20 @@ static void hrtimer_get_softirq_time(struct hrtimer_base *base)
  * possible to set timer->base = NULL and drop the lock: the timer remains
  * locked.
  */
-static struct hrtimer_base *lock_hrtimer_base(const struct hrtimer *timer,
-                                             unsigned long *flags)
+static
+struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
+                                            unsigned long *flags)
 {
-       struct hrtimer_base *base;
+       struct hrtimer_clock_base *base;
 
        for (;;) {
                base = timer->base;
                if (likely(base != NULL)) {
-                       spin_lock_irqsave(&base->lock, *flags);
+                       spin_lock_irqsave(&base->cpu_base->lock, *flags);
                        if (likely(base == timer->base))
                                return base;
                        /* The timer has migrated to another CPU: */
-                       spin_unlock_irqrestore(&base->lock, *flags);
+                       spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
                }
                cpu_relax();
        }
@@ -182,12 +191,14 @@ static struct hrtimer_base *lock_hrtimer_base(const struct hrtimer *timer,
 /*
  * Switch the timer base to the current CPU when possible.
  */
-static inline struct hrtimer_base *
-switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_base *base)
+static inline struct hrtimer_clock_base *
+switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
 {
-       struct hrtimer_base *new_base;
+       struct hrtimer_clock_base *new_base;
+       struct hrtimer_cpu_base *new_cpu_base;
 
-       new_base = &__get_cpu_var(hrtimer_bases[base->index]);
+       new_cpu_base = &__get_cpu_var(hrtimer_bases);
+       new_base = &new_cpu_base->clock_base[base->index];
 
        if (base != new_base) {
                /*
@@ -199,13 +210,13 @@ switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_base *base)
                 * completed. There is no conflict as we hold the lock until
                 * the timer is enqueued.
                 */
-               if (unlikely(base->curr_timer == timer))
+               if (unlikely(base->cpu_base->curr_timer == timer))
                        return base;
 
                /* See the comment in lock_timer_base() */
                timer->base = NULL;
-               spin_unlock(&base->lock);
-               spin_lock(&new_base->lock);
+               spin_unlock(&base->cpu_base->lock);
+               spin_lock(&new_base->cpu_base->lock);
                timer->base = new_base;
        }
        return new_base;
@@ -215,12 +226,12 @@ switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_base *base)
 
 #define set_curr_timer(b, t)           do { } while (0)
 
-static inline struct hrtimer_base *
+static inline struct hrtimer_clock_base *
 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
 {
-       struct hrtimer_base *base = timer->base;
+       struct hrtimer_clock_base *base = timer->base;
 
-       spin_lock_irqsave(&base->lock, *flags);
+       spin_lock_irqsave(&base->cpu_base->lock, *flags);
 
        return base;
 }
@@ -286,13 +297,21 @@ static unsigned long ktime_divns(const ktime_t kt, s64 div)
 # define ktime_divns(kt, div)          (unsigned long)((kt).tv64 / (div))
 #endif /* BITS_PER_LONG >= 64 */
 
+/*
+ * Timekeeping resumed notification
+ */
+void hrtimer_notify_resume(void)
+{
+       clock_was_set();
+}
+
 /*
  * Counterpart to lock_timer_base above:
  */
 static inline
 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
 {
-       spin_unlock_irqrestore(&timer->base->lock, *flags);
+       spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
 }
 
 /**
@@ -342,7 +361,8 @@ hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  * The timer is inserted in expiry order. Insertion into the
  * red black tree is O(log(n)). Must hold the base lock.
  */
-static void enqueue_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
+static void enqueue_hrtimer(struct hrtimer *timer,
+                           struct hrtimer_clock_base *base)
 {
        struct rb_node **link = &base->active.rb_node;
        struct rb_node *parent = NULL;
@@ -381,7 +401,8 @@ static void enqueue_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  *
  * Caller must hold the base lock.
  */
-static void __remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
+static void __remove_hrtimer(struct hrtimer *timer,
+                            struct hrtimer_clock_base *base)
 {
        /*
         * Remove the timer from the rbtree and replace the
@@ -397,7 +418,7 @@ static void __remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  * remove hrtimer, called with base lock held
  */
 static inline int
-remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
+remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
 {
        if (hrtimer_active(timer)) {
                __remove_hrtimer(timer, base);
@@ -419,7 +440,7 @@ remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
 int
 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
 {
-       struct hrtimer_base *base, *new_base;
+       struct hrtimer_clock_base *base, *new_base;
        unsigned long flags;
        int ret;
 
@@ -431,7 +452,7 @@ 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_MODE_REL) {
                tim = ktime_add(tim, new_base->get_time());
                /*
                 * CONFIG_TIME_LOW_RES is a temporary way for architectures
@@ -466,13 +487,13 @@ EXPORT_SYMBOL_GPL(hrtimer_start);
  */
 int hrtimer_try_to_cancel(struct hrtimer *timer)
 {
-       struct hrtimer_base *base;
+       struct hrtimer_clock_base *base;
        unsigned long flags;
        int ret = -1;
 
        base = lock_hrtimer_base(timer, &flags);
 
-       if (base->curr_timer != timer)
+       if (base->cpu_base->curr_timer != timer)
                ret = remove_hrtimer(timer, base);
 
        unlock_hrtimer_base(timer, &flags);
@@ -508,19 +529,19 @@ EXPORT_SYMBOL_GPL(hrtimer_cancel);
  */
 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
 {
-       struct hrtimer_base *base;
+       struct hrtimer_clock_base *base;
        unsigned long flags;
        ktime_t rem;
 
        base = lock_hrtimer_base(timer, &flags);
-       rem = ktime_sub(timer->expires, timer->base->get_time());
+       rem = ktime_sub(timer->expires, base->get_time());
        unlock_hrtimer_base(timer, &flags);
 
        return rem;
 }
 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
 
-#ifdef CONFIG_NO_IDLE_HZ
+#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
 /**
  * hrtimer_get_next_event - get the time until next expiry event
  *
@@ -529,26 +550,29 @@ EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  */
 ktime_t hrtimer_get_next_event(void)
 {
-       struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
+       struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
+       struct hrtimer_clock_base *base = cpu_base->clock_base;
        ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
        unsigned long flags;
        int i;
 
-       for (i = 0; i < MAX_HRTIMER_BASES; i++, base++) {
+       spin_lock_irqsave(&cpu_base->lock, flags);
+
+       for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
                struct hrtimer *timer;
 
-               spin_lock_irqsave(&base->lock, flags);
-               if (!base->first) {
-                       spin_unlock_irqrestore(&base->lock, flags);
+               if (!base->first)
                        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;
        }
+
+       spin_unlock_irqrestore(&cpu_base->lock, flags);
+
        if (mindelta.tv64 < 0)
                mindelta.tv64 = 0;
        return mindelta;
@@ -564,16 +588,16 @@ ktime_t hrtimer_get_next_event(void)
 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
                  enum hrtimer_mode mode)
 {
-       struct hrtimer_base *bases;
+       struct hrtimer_cpu_base *cpu_base;
 
        memset(timer, 0, sizeof(struct hrtimer));
 
-       bases = __raw_get_cpu_var(hrtimer_bases);
+       cpu_base = &__raw_get_cpu_var(hrtimer_bases);
 
-       if (clock_id == CLOCK_REALTIME && mode != HRTIMER_ABS)
+       if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
                clock_id = CLOCK_MONOTONIC;
 
-       timer->base = &bases[clock_id];
+       timer->base = &cpu_base->clock_base[clock_id];
        rb_set_parent(&timer->node, &timer->node);
 }
 EXPORT_SYMBOL_GPL(hrtimer_init);
@@ -583,15 +607,15 @@ EXPORT_SYMBOL_GPL(hrtimer_init);
  * @which_clock: which clock to query
  * @tp:                 pointer to timespec variable to store the resolution
  *
- * Store the resolution of the clock selected by which_clock in the
- * variable pointed to by tp.
+ * Store the resolution of the clock selected by @which_clock in the
+ * variable pointed to by @tp.
  */
 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
 {
-       struct hrtimer_base *bases;
+       struct hrtimer_cpu_base *cpu_base;
 
-       bases = __raw_get_cpu_var(hrtimer_bases);
-       *tp = ktime_to_timespec(bases[which_clock].resolution);
+       cpu_base = &__raw_get_cpu_var(hrtimer_bases);
+       *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
 
        return 0;
 }
@@ -600,9 +624,11 @@ EXPORT_SYMBOL_GPL(hrtimer_get_res);
 /*
  * Expire the per base hrtimer-queue:
  */
-static inline void run_hrtimer_queue(struct hrtimer_base *base)
+static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
+                                    int index)
 {
        struct rb_node *node;
+       struct hrtimer_clock_base *base = &cpu_base->clock_base[index];
 
        if (!base->first)
                return;
@@ -610,11 +636,11 @@ static inline void run_hrtimer_queue(struct hrtimer_base *base)
        if (base->get_softirq_time)
                base->softirq_time = base->get_softirq_time();
 
-       spin_lock_irq(&base->lock);
+       spin_lock_irq(&cpu_base->lock);
 
        while ((node = base->first)) {
                struct hrtimer *timer;
-               int (*fn)(struct hrtimer *);
+               enum hrtimer_restart (*fn)(struct hrtimer *);
                int restart;
 
                timer = rb_entry(node, struct hrtimer, node);
@@ -622,21 +648,21 @@ static inline void run_hrtimer_queue(struct hrtimer_base *base)
                        break;
 
                fn = timer->function;
-               set_curr_timer(base, timer);
+               set_curr_timer(cpu_base, timer);
                __remove_hrtimer(timer, base);
-               spin_unlock_irq(&base->lock);
+               spin_unlock_irq(&cpu_base->lock);
 
                restart = fn(timer);
 
-               spin_lock_irq(&base->lock);
+               spin_lock_irq(&cpu_base->lock);
 
                if (restart != HRTIMER_NORESTART) {
                        BUG_ON(hrtimer_active(timer));
                        enqueue_hrtimer(timer, base);
                }
        }
-       set_curr_timer(base, NULL);
-       spin_unlock_irq(&base->lock);
+       set_curr_timer(cpu_base, NULL);
+       spin_unlock_irq(&cpu_base->lock);
 }
 
 /*
@@ -644,19 +670,19 @@ static inline void run_hrtimer_queue(struct hrtimer_base *base)
  */
 void hrtimer_run_queues(void)
 {
-       struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
+       struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
        int i;
 
-       hrtimer_get_softirq_time(base);
+       hrtimer_get_softirq_time(cpu_base);
 
-       for (i = 0; i < MAX_HRTIMER_BASES; i++)
-               run_hrtimer_queue(&base[i]);
+       for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
+               run_hrtimer_queue(cpu_base, i);
 }
 
 /*
  * Sleep related functions:
  */
-static int hrtimer_wakeup(struct hrtimer *timer)
+static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
 {
        struct hrtimer_sleeper *t =
                container_of(timer, struct hrtimer_sleeper, timer);
@@ -686,14 +712,14 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod
                schedule();
 
                hrtimer_cancel(&t->timer);
-               mode = HRTIMER_ABS;
+               mode = HRTIMER_MODE_ABS;
 
        } while (t->task && !signal_pending(current));
 
        return t->task == NULL;
 }
 
-static long __sched nanosleep_restart(struct restart_block *restart)
+long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
 {
        struct hrtimer_sleeper t;
        struct timespec __user *rmtp;
@@ -702,13 +728,13 @@ static long __sched nanosleep_restart(struct restart_block *restart)
 
        restart->fn = do_no_restart_syscall;
 
-       hrtimer_init(&t.timer, restart->arg3, HRTIMER_ABS);
-       t.timer.expires.tv64 = ((u64)restart->arg1 << 32) | (u64) restart->arg0;
+       hrtimer_init(&t.timer, restart->arg0, HRTIMER_MODE_ABS);
+       t.timer.expires.tv64 = ((u64)restart->arg3 << 32) | (u64) restart->arg2;
 
-       if (do_nanosleep(&t, HRTIMER_ABS))
+       if (do_nanosleep(&t, HRTIMER_MODE_ABS))
                return 0;
 
-       rmtp = (struct timespec __user *) restart->arg2;
+       rmtp = (struct timespec __user *) restart->arg1;
        if (rmtp) {
                time = ktime_sub(t.timer.expires, t.timer.base->get_time());
                if (time.tv64 <= 0)
@@ -718,7 +744,7 @@ static long __sched nanosleep_restart(struct restart_block *restart)
                        return -EFAULT;
        }
 
-       restart->fn = nanosleep_restart;
+       restart->fn = hrtimer_nanosleep_restart;
 
        /* The other values in restart are already filled in */
        return -ERESTART_RESTARTBLOCK;
@@ -738,7 +764,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
                return 0;
 
        /* Absolute timers do not update the rmtp value and restart: */
-       if (mode == HRTIMER_ABS)
+       if (mode == HRTIMER_MODE_ABS)
                return -ERESTARTNOHAND;
 
        if (rmtp) {
@@ -751,11 +777,11 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
        }
 
        restart = &current_thread_info()->restart_block;
-       restart->fn = nanosleep_restart;
-       restart->arg0 = t.timer.expires.tv64 & 0xFFFFFFFF;
-       restart->arg1 = t.timer.expires.tv64 >> 32;
-       restart->arg2 = (unsigned long) rmtp;
-       restart->arg3 = (unsigned long) t.timer.base->index;
+       restart->fn = hrtimer_nanosleep_restart;
+       restart->arg0 = (unsigned long) t.timer.base->index;
+       restart->arg1 = (unsigned long) rmtp;
+       restart->arg2 = t.timer.expires.tv64 & 0xFFFFFFFF;
+       restart->arg3 = t.timer.expires.tv64 >> 32;
 
        return -ERESTART_RESTARTBLOCK;
 }
@@ -771,7 +797,7 @@ sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
        if (!timespec_valid(&tu))
                return -EINVAL;
 
-       return hrtimer_nanosleep(&tu, rmtp, HRTIMER_REL, CLOCK_MONOTONIC);
+       return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
 }
 
 /*
@@ -779,19 +805,21 @@ sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  */
 static void __devinit init_hrtimers_cpu(int cpu)
 {
-       struct hrtimer_base *base = per_cpu(hrtimer_bases, cpu);
+       struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
        int i;
 
-       for (i = 0; i < MAX_HRTIMER_BASES; i++, base++) {
-               spin_lock_init(&base->lock);
-               lockdep_set_class(&base->lock, &base->lock_key);
-       }
+       spin_lock_init(&cpu_base->lock);
+       lockdep_set_class(&cpu_base->lock, &cpu_base->lock_key);
+
+       for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
+               cpu_base->clock_base[i].cpu_base = cpu_base;
+
 }
 
 #ifdef CONFIG_HOTPLUG_CPU
 
-static void migrate_hrtimer_list(struct hrtimer_base *old_base,
-                               struct hrtimer_base *new_base)
+static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
+                               struct hrtimer_clock_base *new_base)
 {
        struct hrtimer *timer;
        struct rb_node *node;
@@ -806,29 +834,26 @@ static void migrate_hrtimer_list(struct hrtimer_base *old_base,
 
 static void migrate_hrtimers(int cpu)
 {
-       struct hrtimer_base *old_base, *new_base;
+       struct hrtimer_cpu_base *old_base, *new_base;
        int i;
 
        BUG_ON(cpu_online(cpu));
-       old_base = per_cpu(hrtimer_bases, cpu);
-       new_base = get_cpu_var(hrtimer_bases);
+       old_base = &per_cpu(hrtimer_bases, cpu);
+       new_base = &get_cpu_var(hrtimer_bases);
 
        local_irq_disable();
 
-       for (i = 0; i < MAX_HRTIMER_BASES; i++) {
-
-               spin_lock(&new_base->lock);
-               spin_lock(&old_base->lock);
+       spin_lock(&new_base->lock);
+       spin_lock(&old_base->lock);
 
+       for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
                BUG_ON(old_base->curr_timer);
 
-               migrate_hrtimer_list(old_base, new_base);
-
-               spin_unlock(&old_base->lock);
-               spin_unlock(&new_base->lock);
-               old_base++;
-               new_base++;
+               migrate_hrtimer_list(&old_base->clock_base[i],
+                                    &new_base->clock_base[i]);
        }
+       spin_unlock(&old_base->lock);
+       spin_unlock(&new_base->lock);
 
        local_irq_enable();
        put_cpu_var(hrtimer_bases);