libertas: unify various CF-related defines
[safe/jmp/linux-2.6] / kernel / posix-timers.c
index fa895fc..dbd8398 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * linux/kernel/posix_timers.c
+ * linux/kernel/posix-timers.c
  *
  *
  * 2002-10-15  Posix Clocks & timers
  * POSIX clocks & timers
  */
 #include <linux/mm.h>
-#include <linux/smp_lock.h>
 #include <linux/interrupt.h>
 #include <linux/slab.h>
 #include <linux/time.h>
+#include <linux/mutex.h>
 
 #include <asm/uaccess.h>
-#include <asm/semaphore.h>
 #include <linux/list.h>
 #include <linux/init.h>
 #include <linux/compiler.h>
@@ -69,7 +68,7 @@
 /*
  * Lets keep our timers in a slab cache :-)
  */
-static kmem_cache_t *posix_timers_cache;
+static struct kmem_cache *posix_timers_cache;
 static struct idr posix_timers_id;
 static DEFINE_SPINLOCK(idr_lock);
 
@@ -144,7 +143,7 @@ static int common_timer_set(struct k_itimer *, int,
                            struct itimerspec *, struct itimerspec *);
 static int common_timer_del(struct k_itimer *timer);
 
-static int posix_timer_fn(void *data);
+static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
 
 static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);
 
@@ -241,7 +240,8 @@ static __init int init_posix_timers(void)
        register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
 
        posix_timers_cache = kmem_cache_create("posix_timers_cache",
-                                       sizeof (struct k_itimer), 0, 0, NULL, NULL);
+                                       sizeof (struct k_itimer), 0, SLAB_PANIC,
+                                       NULL);
        idr_init(&posix_timers_id);
        return 0;
 }
@@ -250,15 +250,19 @@ __initcall(init_posix_timers);
 
 static void schedule_next_timer(struct k_itimer *timr)
 {
+       struct hrtimer *timer = &timr->it.real.timer;
+
        if (timr->it.real.interval.tv64 == 0)
                return;
 
-       timr->it_overrun += hrtimer_forward(&timr->it.real.timer,
-                                           timr->it.real.interval);
+       timr->it_overrun += (unsigned int) hrtimer_forward(timer,
+                                               timer->base->get_time(),
+                                               timr->it.real.interval);
+
        timr->it_overrun_last = timr->it_overrun;
        timr->it_overrun = -1;
        ++timr->it_requeue_pending;
-       hrtimer_restart(&timr->it.real.timer);
+       hrtimer_restart(timer);
 }
 
 /*
@@ -306,8 +310,7 @@ int posix_timer_event(struct k_itimer *timr,int si_private)
 
        if (timr->it_sigev_notify & SIGEV_THREAD_ID) {
                struct task_struct *leader;
-               int ret = send_sigqueue(timr->it_sigev_signo, timr->sigq,
-                                       timr->it_process);
+               int ret = send_sigqueue(timr->sigq, timr->it_process, 0);
 
                if (likely(ret >= 0))
                        return ret;
@@ -318,8 +321,7 @@ int posix_timer_event(struct k_itimer *timr,int si_private)
                timr->it_process = leader;
        }
 
-       return send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
-                                  timr->it_process);
+       return send_sigqueue(timr->sigq, timr->it_process, 1);
 }
 EXPORT_SYMBOL_GPL(posix_timer_event);
 
@@ -330,13 +332,14 @@ EXPORT_SYMBOL_GPL(posix_timer_event);
 
  * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
  */
-static int posix_timer_fn(void *data)
+static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
 {
-       struct k_itimer *timr = data;
+       struct k_itimer *timr;
        unsigned long flags;
        int si_private = 0;
-       int ret = HRTIMER_NORESTART;
+       enum hrtimer_restart ret = HRTIMER_NORESTART;
 
+       timr = container_of(timer, struct k_itimer, it.real.timer);
        spin_lock_irqsave(&timr->it_lock, flags);
 
        if (timr->it.real.interval.tv64 != 0)
@@ -349,8 +352,40 @@ static int posix_timer_fn(void *data)
                 * it should be restarted.
                 */
                if (timr->it.real.interval.tv64 != 0) {
-                       timr->it_overrun +=
-                               hrtimer_forward(&timr->it.real.timer,
+                       ktime_t now = hrtimer_cb_get_time(timer);
+
+                       /*
+                        * FIXME: What we really want, is to stop this
+                        * timer completely and restart it in case the
+                        * SIG_IGN is removed. This is a non trivial
+                        * change which involves sighand locking
+                        * (sigh !), which we don't want to do late in
+                        * the release cycle.
+                        *
+                        * For now we just let timers with an interval
+                        * less than a jiffie expire every jiffie to
+                        * avoid softirq starvation in case of SIG_IGN
+                        * and a very small interval, which would put
+                        * the timer right back on the softirq pending
+                        * list. By moving now ahead of time we trick
+                        * hrtimer_forward() to expire the timer
+                        * later, while we still maintain the overrun
+                        * accuracy, but have some inconsistency in
+                        * the timer_gettime() case. This is at least
+                        * better than a starved softirq. A more
+                        * complex fix which solves also another related
+                        * inconsistency is already in the pipeline.
+                        */
+#ifdef CONFIG_HIGH_RES_TIMERS
+                       {
+                               ktime_t kj = ktime_set(0, NSEC_PER_SEC / HZ);
+
+                               if (timr->it.real.interval.tv64 < kj.tv64)
+                                       now = ktime_add(now, kj);
+                       }
+#endif
+                       timr->it_overrun += (unsigned int)
+                               hrtimer_forward(timer, now,
                                                timr->it.real.interval);
                        ret = HRTIMER_RESTART;
                        ++timr->it_requeue_pending;
@@ -366,8 +401,8 @@ static struct task_struct * good_sigevent(sigevent_t * event)
        struct task_struct *rtn = current->group_leader;
 
        if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
-               (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
-                rtn->tgid != current->tgid ||
+               (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
+                !same_thread_group(rtn, current) ||
                 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
                return NULL;
 
@@ -393,10 +428,9 @@ EXPORT_SYMBOL_GPL(register_posix_clock);
 static struct k_itimer * alloc_posix_timer(void)
 {
        struct k_itimer *tmr;
-       tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
+       tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
        if (!tmr)
                return tmr;
-       memset(tmr, 0, sizeof (struct k_itimer));
        if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
                kmem_cache_free(posix_timers_cache, tmr);
                tmr = NULL;
@@ -457,7 +491,7 @@ sys_timer_create(const clockid_t which_clock,
                goto retry;
        else if (error) {
                /*
-                * Wierd looking, but we return EAGAIN if the IDR is
+                * Weird looking, but we return EAGAIN if the IDR is
                 * full (proper POSIX return value for this)
                 */
                error = -EAGAIN;
@@ -512,9 +546,9 @@ sys_timer_create(const clockid_t which_clock,
                                new_timer->it_process = process;
                                list_add(&new_timer->list,
                                         &process->signal->posix_timers);
-                               spin_unlock_irqrestore(&process->sighand->siglock, flags);
                                if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
                                        get_task_struct(process);
+                               spin_unlock_irqrestore(&process->sighand->siglock, flags);
                        } else {
                                spin_unlock_irqrestore(&process->sighand->siglock, flags);
                                process = NULL;
@@ -570,13 +604,14 @@ static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
        timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
        if (timr) {
                spin_lock(&timr->it_lock);
-               spin_unlock(&idr_lock);
 
                if ((timr->it_id != timer_id) || !(timr->it_process) ||
-                               timr->it_process->tgid != current->tgid) {
-                       unlock_timer(timr, *flags);
+                               !same_thread_group(timr->it_process, current)) {
+                       spin_unlock(&timr->it_lock);
+                       spin_unlock_irqrestore(&idr_lock, *flags);
                        timr = NULL;
-               }
+               } else
+                       spin_unlock(&idr_lock);
        } else
                spin_unlock_irqrestore(&idr_lock, *flags);
 
@@ -602,38 +637,41 @@ static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
 static void
 common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
 {
-       ktime_t remaining;
+       ktime_t now, remaining, iv;
        struct hrtimer *timer = &timr->it.real.timer;
 
        memset(cur_setting, 0, sizeof(struct itimerspec));
-       remaining = hrtimer_get_remaining(timer);
 
-       /* Time left ? or timer pending */
-       if (remaining.tv64 > 0 || hrtimer_active(timer))
-               goto calci;
+       iv = timr->it.real.interval;
+
        /* interval timer ? */
-       if (timr->it.real.interval.tv64 == 0)
+       if (iv.tv64)
+               cur_setting->it_interval = ktime_to_timespec(iv);
+       else if (!hrtimer_active(timer) &&
+                (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
                return;
+
+       now = timer->base->get_time();
+
        /*
-        * When a requeue is pending or this is a SIGEV_NONE timer
-        * move the expiry time forward by intervals, so expiry is >
-        * now.
+        * When a requeue is pending or this is a SIGEV_NONE
+        * timer move the expiry time forward by intervals, so
+        * expiry is > now.
         */
-       if (timr->it_requeue_pending & REQUEUE_PENDING ||
-           (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
-               timr->it_overrun +=
-                       hrtimer_forward(timer, timr->it.real.interval);
-               remaining = hrtimer_get_remaining(timer);
-       }
- calci:
-       /* interval timer ? */
-       if (timr->it.real.interval.tv64 != 0)
-               cur_setting->it_interval =
-                       ktime_to_timespec(timr->it.real.interval);
+       if (iv.tv64 && (timr->it_requeue_pending & REQUEUE_PENDING ||
+           (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
+               timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
+
+       remaining = ktime_sub(timer->expires, now);
        /* Return 0 only, when the timer is expired and not pending */
-       if (remaining.tv64 <= 0)
-               cur_setting->it_value.tv_nsec = 1;
-       else
+       if (remaining.tv64 <= 0) {
+               /*
+                * A single shot SIGEV_NONE timer must return 0, when
+                * it is expired !
+                */
+               if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
+                       cur_setting->it_value.tv_nsec = 1;
+       } else
                cur_setting->it_value = ktime_to_timespec(remaining);
 }
 
@@ -673,7 +711,7 @@ sys_timer_getoverrun(timer_t timer_id)
 {
        struct k_itimer *timr;
        int overrun;
-       long flags;
+       unsigned long flags;
 
        timr = lock_timer(timer_id, &flags);
        if (!timr)
@@ -714,9 +752,8 @@ common_timer_set(struct k_itimer *timr, int flags,
        if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
                return 0;
 
-       mode = flags & TIMER_ABSTIME ? HRTIMER_ABS : HRTIMER_REL;
+       mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
        hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
-       timr->it.real.timer.data = timr;
        timr->it.real.timer.function = posix_timer_fn;
 
        timer->expires = timespec_to_ktime(new_setting->it_value);
@@ -727,9 +764,11 @@ common_timer_set(struct k_itimer *timr, int flags,
        /* SIGEV_NONE timers are not queued ! See common_timer_get */
        if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
                /* Setup correct expiry time for relative timers */
-               if (mode == HRTIMER_REL)
-                       timer->expires = ktime_add(timer->expires,
-                                                  timer->base->get_time());
+               if (mode == HRTIMER_MODE_REL) {
+                       timer->expires =
+                               ktime_add_safe(timer->expires,
+                                              timer->base->get_time());
+               }
                return 0;
        }
 
@@ -746,7 +785,7 @@ sys_timer_settime(timer_t timer_id, int flags,
        struct k_itimer *timr;
        struct itimerspec new_spec, old_spec;
        int error = 0;
-       long flag;
+       unsigned long flag;
        struct itimerspec *rtn = old_setting ? &old_spec : NULL;
 
        if (!new_setting)
@@ -798,7 +837,7 @@ asmlinkage long
 sys_timer_delete(timer_t timer_id)
 {
        struct k_itimer *timer;
-       long flags;
+       unsigned long flags;
 
 retry_delete:
        timer = lock_timer(timer_id, &flags);
@@ -943,7 +982,8 @@ static int common_nsleep(const clockid_t which_clock, int flags,
                         struct timespec *tsave, struct timespec __user *rmtp)
 {
        return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
-                                HRTIMER_ABS : HRTIMER_REL, which_clock);
+                                HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
+                                which_clock);
 }
 
 asmlinkage long
@@ -965,3 +1005,24 @@ sys_clock_nanosleep(const clockid_t which_clock, int flags,
        return CLOCK_DISPATCH(which_clock, nsleep,
                              (which_clock, flags, &t, rmtp));
 }
+
+/*
+ * nanosleep_restart for monotonic and realtime clocks
+ */
+static int common_nsleep_restart(struct restart_block *restart_block)
+{
+       return hrtimer_nanosleep_restart(restart_block);
+}
+
+/*
+ * This will restart clock_nanosleep. This is required only by
+ * compat_clock_nanosleep_restart for now.
+ */
+long
+clock_nanosleep_restart(struct restart_block *restart_block)
+{
+       clockid_t which_clock = restart_block->arg0;
+
+       return CLOCK_DISPATCH(which_clock, nsleep_restart,
+                             (restart_block));
+}