/* Delete a POSIX.1b interval timer. */ asmlinkage long sys_timer_delete(timer_t timer_id) { struct k_itimer *timer; long flags; retry_delete: timer = lock_timer(timer_id, &flags); if (!timer) return -EINVAL; if (timer_delete_hook(timer) == TIMER_RETRY) { unlock_timer(timer, flags); goto retry_delete; } spin_lock(¤t->sighand->siglock); list_del(&timer->list); spin_unlock(¤t->sighand->siglock); /* * This keeps any tasks waiting on the spin lock from thinking * they got something (see the lock code above). */ if (timer->it_process) { if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID)) put_task_struct(timer->it_process); timer->it_process = NULL; } unlock_timer(timer, flags); release_posix_timer(timer, IT_ID_SET); return 0; }
/* * return timer owned by the process, used by exit_itimers */ static void itimer_delete(struct k_itimer *timer) { unsigned long flags; retry_delete: spin_lock_irqsave(&timer->it_lock, flags); /* On RT we can race with a deletion */ if (!timer->it_signal) { unlock_timer(timer, flags); return; } if (timer_delete_hook(timer) == TIMER_RETRY) { rcu_read_lock(); unlock_timer(timer, flags); timer_wait_for_callback(clockid_to_kclock(timer->it_clock), timer); rcu_read_unlock(); goto retry_delete; } list_del(&timer->list); /* * This keeps any tasks waiting on the spin lock from thinking * they got something (see the lock code above). */ timer->it_signal = NULL; unlock_timer(timer, flags); release_posix_timer(timer, IT_ID_SET); }
/* Delete a POSIX.1b interval timer. */ SYSCALL_DEFINE1(timer_delete, timer_t, timer_id) { struct k_itimer *timer; unsigned long flags; retry_delete: timer = lock_timer(timer_id, &flags); if (!timer) return -EINVAL; rcu_read_lock(); if (timer_delete_hook(timer) == TIMER_RETRY) { unlock_timer(timer, flags); timer_wait_for_callback(clockid_to_kclock(timer->it_clock), timer); rcu_read_unlock(); goto retry_delete; } rcu_read_unlock(); spin_lock(¤t->sighand->siglock); list_del(&timer->list); spin_unlock(¤t->sighand->siglock); /* * This keeps any tasks waiting on the spin lock from thinking * they got something (see the lock code above). */ timer->it_signal = NULL; unlock_timer(timer, flags); release_posix_timer(timer, IT_ID_SET); return 0; }
/* * Locking issues: We need to protect the result of the id look up until * we get the timer locked down so it is not deleted under us. The * removal is done under the idr spinlock so we use that here to bridge * the find to the timer lock. To avoid a dead lock, the timer id MUST * be release with out holding the timer lock. */ static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags) { struct k_itimer *timr; /* * Watch out here. We do a irqsave on the idr_lock and pass the * flags part over to the timer lock. Must not let interrupts in * while we are moving the lock. */ spin_lock_irqsave(&idr_lock, *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); timr = NULL; } } else spin_unlock_irqrestore(&idr_lock, *flags); return timr; }
/* * This function gets called when a POSIX.1b interval timer expires. It * is used as a callback from the kernel internal timer. The * run_timer_list code ALWAYS calls with interrupts on. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers. */ static int posix_timer_fn(void *data) { struct k_itimer *timr = data; unsigned long flags; int si_private = 0; int ret = HRTIMER_NORESTART; spin_lock_irqsave(&timr->it_lock, flags); if (timr->it.real.interval.tv64 != 0) si_private = ++timr->it_requeue_pending; if (posix_timer_event(timr, si_private)) { /* * signal was not sent because of sig_ignor * we will not get a call back to restart it AND * it should be restarted. */ if (timr->it.real.interval.tv64 != 0) { timr->it_overrun += hrtimer_forward(&timr->it.real.timer, timr->it.real.interval); ret = HRTIMER_RESTART; } } unlock_timer(timr, flags); return ret; }
/***************************************************************************** * remove_timer * ****************************************************************************/ static void remove_timer(int id) { /* removes a timer from the timer list */ struct ddekit_timer_s *l,*m; lock_timer(); for (l = &list; l && l->next && l->next->id!=id; l = l->next ) ; if (l && l->next) { m = l->next; DDEBUG_MSG_VERBOSE( "deleting timer at for tick: %d fn: %p, (now: %d)\n", m->exp, m->fn, jiffies); l->next = m->next; DDEBUG_MSG_TIMER(m); ddekit_simple_free(m); } unlock_timer(); }
/***************************************************************************** * _ddekit_timer_update * ****************************************************************************/ void _ddekit_timer_update() { lock_timer(); static myclock_t next_timout; if(list.next) { if(!_ddekit_timer_pending || list.next->exp < next_timout) { unsigned to = list.next->exp - jiffies; _ddekit_timer_pending = 1; if (list.next->exp <= jiffies) { DDEBUG_MSG_WARN("Timeout lies in past to %d, now: %d", list.next->exp, jiffies); to = 1; } sys_setalarm(to, 0 /* REL */); DDEBUG_MSG_VERBOSE("requesting alarm for clock tick %d , now %d", list.next->exp, jiffies); } next_timout = list.next->exp; } unlock_timer(); }
/* Get the time remaining on a POSIX.1b interval timer. */ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, struct itimerspec __user *, setting) { struct itimerspec cur_setting; struct k_itimer *timr; struct k_clock *kc; unsigned long flags; int ret = 0; timr = lock_timer(timer_id, &flags); if (!timr) return -EINVAL; kc = clockid_to_kclock(timr->it_clock); if (WARN_ON_ONCE(!kc || !kc->timer_get)) ret = -EINVAL; else kc->timer_get(timr, &cur_setting); unlock_timer(timr, flags); if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting))) return -EFAULT; return ret; }
/***************************************************************************** * insert_timer * ****************************************************************************/ static int insert_timer(struct ddekit_timer_s *t) { /* inserts a timer to the timer list */ int ret; lock_timer(); struct ddekit_timer_s *l; for (l = &list; l->next && l->next->exp <= t->exp; l = l->next) { } t->next = l->next; l->next = t; t->id = ret = _id; _id++; if (_id==0) { DDEBUG_MSG_WARN("Timer ID overflow..."); } DDEBUG_MSG_TIMER(t); unlock_timer(); return ret; }
static void itimer_delete(struct k_itimer *timer) { unsigned long flags; retry_delete: spin_lock_irqsave(&timer->it_lock, flags); if (timer_delete_hook(timer) == TIMER_RETRY) { unlock_timer(timer, flags); goto retry_delete; } list_del(&timer->list); timer->it_signal = NULL; unlock_timer(timer, flags); release_posix_timer(timer, IT_ID_SET); }
/* * return timer owned by the process, used by exit_itimers */ static void itimer_delete(struct k_itimer *timer) { unsigned long flags; retry_delete: spin_lock_irqsave(&timer->it_lock, flags); if (timer_delete_hook(timer) == TIMER_RETRY) { unlock_timer(timer, flags); goto retry_delete; } list_del(&timer->list); /* * This keeps any tasks waiting on the spin lock from thinking * they got something (see the lock code above). */ timer->it_signal = NULL; unlock_timer(timer, flags); release_posix_timer(timer, IT_ID_SET); }
/***************************************************************************** * ddekit_timer_pending * ****************************************************************************/ int ddekit_timer_pending(int timer) { int ret=0; struct ddekit_timer_s *t; lock_timer(); for (t=list.next; t; t = t->next) { if (t->id==timer) { ret = 1; } } unlock_timer(); return ret; }
/* * Get the number of overruns of a POSIX.1b interval timer. This is to * be the overrun of the timer last delivered. At the same time we are * accumulating overruns on the next timer. The overrun is frozen when * the signal is delivered, either at the notify time (if the info block * is not queued) or at the actual delivery time (as we are informed by * the call back to do_schedule_next_timer(). So all we need to do is * to pick up the frozen overrun. */ SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) { struct k_itimer *timr; int overrun; unsigned long flags; timr = lock_timer(timer_id, &flags); if (!timr) return -EINVAL; overrun = timr->it_overrun_last; unlock_timer(timr, flags); return overrun; }
/* * return timer owned by the process, used by exit_itimers */ static inline void itimer_delete(struct k_itimer *timer) { unsigned long flags; retry_delete: spin_lock_irqsave(&timer->it_lock, flags); if (timer_delete_hook(timer) == TIMER_RETRY) { unlock_timer(timer, flags); goto retry_delete; } list_del(&timer->list); /* * This keeps any tasks waiting on the spin lock from thinking * they got something (see the lock code above). */ if (timer->it_process) { if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID)) put_task_struct(timer->it_process); timer->it_process = NULL; } unlock_timer(timer, flags); release_posix_timer(timer, IT_ID_SET); }
SYSCALL_DEFINE1(timer_delete, timer_t, timer_id) { struct k_itimer *timer; unsigned long flags; retry_delete: timer = lock_timer(timer_id, &flags); if (!timer) return -EINVAL; if (timer_delete_hook(timer) == TIMER_RETRY) { unlock_timer(timer, flags); goto retry_delete; } spin_lock(¤t->sighand->siglock); list_del(&timer->list); spin_unlock(¤t->sighand->siglock); timer->it_signal = NULL; unlock_timer(timer, flags); release_posix_timer(timer, IT_ID_SET); return 0; }
/* * Get the number of overruns of a POSIX.1b interval timer. This is to * be the overrun of the timer last delivered. At the same time we are * accumulating overruns on the next timer. The overrun is frozen when * the signal is delivered, either at the notify time (if the info block * is not queued) or at the actual delivery time (as we are informed by * the call back to do_schedule_next_timer(). So all we need to do is * to pick up the frozen overrun. */ asmlinkage long sys_timer_getoverrun(timer_t timer_id) { struct k_itimer *timr; int overrun; long flags; timr = lock_timer(timer_id, &flags); if (!timr) return -EINVAL; overrun = timr->it_overrun_last; unlock_timer(timr, flags); return overrun; }
/* Set a POSIX.1b interval timer */ SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, const struct itimerspec __user *, new_setting, struct itimerspec __user *, old_setting) { struct k_itimer *timr; struct itimerspec new_spec, old_spec; int error = 0; unsigned long flag; struct itimerspec *rtn = old_setting ? &old_spec : NULL; struct k_clock *kc; if (!new_setting) return -EINVAL; if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) return -EFAULT; if (!timespec_valid(&new_spec.it_interval) || !timespec_valid(&new_spec.it_value)) return -EINVAL; retry: timr = lock_timer(timer_id, &flag); if (!timr) return -EINVAL; rcu_read_lock(); kc = clockid_to_kclock(timr->it_clock); if (WARN_ON_ONCE(!kc || !kc->timer_set)) error = -EINVAL; else error = kc->timer_set(timr, flags, &new_spec, rtn); unlock_timer(timr, flag); if (error == TIMER_RETRY) { timer_wait_for_callback(kc, timr); rtn = NULL; // We already got the old time... rcu_read_unlock(); goto retry; } rcu_read_unlock(); if (old_setting && !error && copy_to_user(old_setting, &old_spec, sizeof (old_spec))) error = -EFAULT; return error; }
/* * This function is exported for use by the signal deliver code. It is * called just prior to the info block being released and passes that * block to us. It's function is to update the overrun entry AND to * restart the timer. It should only be called if the timer is to be * restarted (i.e. we have flagged this in the sys_private entry of the * info block). * * To protect aginst the timer going away while the interrupt is queued, * we require that the it_requeue_pending flag be set. */ void do_schedule_next_timer(struct siginfo *info) { struct k_itimer *timr; unsigned long flags; timr = lock_timer(info->si_tid, &flags); if (timr && timr->it_requeue_pending == info->si_sys_private) { if (timr->it_clock < 0) posix_cpu_timer_schedule(timr); else schedule_next_timer(timr); info->si_overrun = timr->it_overrun_last; } unlock_timer(timr, flags); }
/***************************************************************************** * get_next * ****************************************************************************/ static struct ddekit_timer_s * get_next( myclock_t exp ) { /* * this one get the next timer, which's timeout expired, * returns NULL if no timer is pending */ struct ddekit_timer_s * ret = 0; lock_timer(); if (list.next) { if (list.next->exp <= exp) { ret = list.next; list.next = ret->next; } } unlock_timer(); return ret; }
/* Get the time remaining on a POSIX.1b interval timer. */ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, struct itimerspec __user *, setting) { struct k_itimer *timr; struct itimerspec cur_setting; unsigned long flags; timr = lock_timer(timer_id, &flags); if (!timr) return -EINVAL; CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting)); unlock_timer(timr, flags); if (copy_to_user(setting, &cur_setting, sizeof (cur_setting))) return -EFAULT; return 0; }
/* Set a POSIX.1b interval timer */ SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, const struct itimerspec __user *, new_setting, struct itimerspec __user *, old_setting) { struct k_itimer *timr; struct itimerspec new_spec, old_spec; int error = 0; unsigned long flag; struct itimerspec *rtn = old_setting ? &old_spec : NULL; if (!new_setting) return -EINVAL; if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) return -EFAULT; if (!timespec_valid(&new_spec.it_interval) || !timespec_valid(&new_spec.it_value)) return -EINVAL; retry: timr = lock_timer(timer_id, &flag); if (!timr) return -EINVAL; error = CLOCK_DISPATCH(timr->it_clock, timer_set, (timr, flags, &new_spec, rtn)); unlock_timer(timr, flag); if (error == TIMER_RETRY) { hrtimer_wait_for_timer(&timr->it.real.timer); rtn = NULL; // We already got the old time... goto retry; } if (old_setting && !error && copy_to_user(old_setting, &old_spec, sizeof (old_spec))) error = -EFAULT; return error; }
static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) { struct k_itimer *timr; unsigned long flags; int si_private = 0; 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) si_private = ++timr->it_requeue_pending; if (posix_timer_event(timr, si_private)) { if (timr->it.real.interval.tv64 != 0) { ktime_t now = hrtimer_cb_get_time(timer); #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; } } unlock_timer(timr, flags); return ret; }
/* * This function gets called when a POSIX.1b interval timer expires. It * is used as a callback from the kernel internal timer. The * run_timer_list code ALWAYS calls with interrupts on. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers. */ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) { struct k_itimer *timr; unsigned long flags; int si_private = 0; 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) si_private = ++timr->it_requeue_pending; if (posix_timer_event(timr, si_private)) { /* * signal was not sent because of sig_ignor * we will not get a call back to restart it AND * it should be restarted. */ if (timr->it.real.interval.tv64 != 0) { 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; } } unlock_timer(timr, flags); return ret; }