2 * linux/kernel/posix_timers.c
5 * 2002-10-15 Posix Clocks & timers
6 * by George Anzinger george@mvista.com
8 * Copyright (C) 2002 2003 by MontaVista Software.
10 * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
11 * Copyright (C) 2004 Boris Hu
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA
30 /* These are all the functions necessary to implement
31 * POSIX clocks & timers
34 #include <linux/smp_lock.h>
35 #include <linux/interrupt.h>
36 #include <linux/slab.h>
37 #include <linux/time.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include <linux/list.h>
42 #include <linux/init.h>
43 #include <linux/compiler.h>
44 #include <linux/idr.h>
45 #include <linux/posix-timers.h>
46 #include <linux/syscalls.h>
47 #include <linux/wait.h>
48 #include <linux/workqueue.h>
50 #ifndef div_long_long_rem
51 #include <asm/div64.h>
53 #define div_long_long_rem(dividend,divisor,remainder) ({ \
54 u64 result = dividend; \
55 *remainder = do_div(result,divisor); \
59 #define CLOCK_REALTIME_RES TICK_NSEC /* In nano seconds. */
61 static inline u64 mpy_l_X_l_ll(unsigned long mpy1,unsigned long mpy2)
63 return (u64)mpy1 * mpy2;
66 * Management arrays for POSIX timers. Timers are kept in slab memory
67 * Timer ids are allocated by an external routine that keeps track of the
68 * id and the timer. The external interface is:
70 * void *idr_find(struct idr *idp, int id); to find timer_id <id>
71 * int idr_get_new(struct idr *idp, void *ptr); to get a new id and
73 * void idr_remove(struct idr *idp, int id); to release <id>
74 * void idr_init(struct idr *idp); to initialize <idp>
76 * The idr_get_new *may* call slab for more memory so it must not be
77 * called under a spin lock. Likewise idr_remore may release memory
78 * (but it may be ok to do this under a lock...).
79 * idr_find is just a memory look up and is quite fast. A -1 return
80 * indicates that the requested id does not exist.
84 * Lets keep our timers in a slab cache :-)
86 static kmem_cache_t *posix_timers_cache;
87 static struct idr posix_timers_id;
88 static spinlock_t idr_lock = SPIN_LOCK_UNLOCKED;
91 * Just because the timer is not in the timer list does NOT mean it is
92 * inactive. It could be in the "fire" routine getting a new expire time.
94 #define TIMER_INACTIVE 1
98 # define timer_active(tmr) \
99 ((tmr)->it_timer.entry.prev != (void *)TIMER_INACTIVE)
100 # define set_timer_inactive(tmr) \
102 (tmr)->it_timer.entry.prev = (void *)TIMER_INACTIVE; \
105 # define timer_active(tmr) BARFY // error to use outside of SMP
106 # define set_timer_inactive(tmr) do { } while (0)
109 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
110 * SIGEV values. Here we put out an error if this assumption fails.
112 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
113 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
114 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
118 #define REQUEUE_PENDING 1
120 * The timer ID is turned into a timer address by idr_find().
121 * Verifying a valid ID consists of:
123 * a) checking that idr_find() returns other than -1.
124 * b) checking that the timer id matches the one in the timer itself.
125 * c) that the timer owner is in the callers thread group.
129 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
130 * to implement others. This structure defines the various
131 * clocks and allows the possibility of adding others. We
132 * provide an interface to add clocks to the table and expect
133 * the "arch" code to add at least one clock that is high
134 * resolution. Here we define the standard CLOCK_REALTIME as a
135 * 1/HZ resolution clock.
137 * RESOLUTION: Clock resolution is used to round up timer and interval
138 * times, NOT to report clock times, which are reported with as
139 * much resolution as the system can muster. In some cases this
140 * resolution may depend on the underlying clock hardware and
141 * may not be quantifiable until run time, and only then is the
142 * necessary code is written. The standard says we should say
143 * something about this issue in the documentation...
145 * FUNCTIONS: The CLOCKs structure defines possible functions to handle
146 * various clock functions. For clocks that use the standard
147 * system timer code these entries should be NULL. This will
148 * allow dispatch without the overhead of indirect function
149 * calls. CLOCKS that depend on other sources (e.g. WWV or GPS)
150 * must supply functions here, even if the function just returns
151 * ENOSYS. The standard POSIX timer management code assumes the
152 * following: 1.) The k_itimer struct (sched.h) is used for the
153 * timer. 2.) The list, it_lock, it_clock, it_id and it_process
154 * fields are not modified by timer code.
156 * At this time all functions EXCEPT clock_nanosleep can be
157 * redirected by the CLOCKS structure. Clock_nanosleep is in
158 * there, but the code ignores it.
160 * Permissions: It is assumed that the clock_settime() function defined
161 * for each clock will take care of permission checks. Some
162 * clocks may be set able by any user (i.e. local process
163 * clocks) others not. Currently the only set able clock we
164 * have is CLOCK_REALTIME and its high res counter part, both of
165 * which we beg off on and pass to do_sys_settimeofday().
168 static struct k_clock posix_clocks[MAX_CLOCKS];
170 * We only have one real clock that can be set so we need only one abs list,
171 * even if we should want to have several clocks with differing resolutions.
173 static struct k_clock_abs abs_list = {.list = LIST_HEAD_INIT(abs_list.list),
174 .lock = SPIN_LOCK_UNLOCKED};
176 #define if_clock_do(clock_fun,alt_fun,parms) \
177 (!clock_fun) ? alt_fun parms : clock_fun parms
179 #define p_timer_get(clock,a,b) \
180 if_clock_do((clock)->timer_get,do_timer_gettime, (a,b))
182 #define p_nsleep(clock,a,b,c) \
183 if_clock_do((clock)->nsleep, do_nsleep, (a,b,c))
185 #define p_timer_del(clock,a) \
186 if_clock_do((clock)->timer_del, do_timer_delete, (a))
188 static int do_posix_gettime(struct k_clock *clock, struct timespec *tp);
189 static u64 do_posix_clock_monotonic_gettime_parts(
190 struct timespec *tp, struct timespec *mo);
191 int do_posix_clock_monotonic_gettime(struct timespec *tp);
192 static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);
194 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
196 spin_unlock_irqrestore(&timr->it_lock, flags);
200 * Initialize everything, well, just everything in Posix clocks/timers ;)
202 static __init int init_posix_timers(void)
204 struct k_clock clock_realtime = {.res = CLOCK_REALTIME_RES,
205 .abs_struct = &abs_list
207 struct k_clock clock_monotonic = {.res = CLOCK_REALTIME_RES,
209 .clock_get = do_posix_clock_monotonic_gettime,
210 .clock_set = do_posix_clock_nosettime
213 register_posix_clock(CLOCK_REALTIME, &clock_realtime);
214 register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
216 posix_timers_cache = kmem_cache_create("posix_timers_cache",
217 sizeof (struct k_itimer), 0, 0, NULL, NULL);
218 idr_init(&posix_timers_id);
222 __initcall(init_posix_timers);
224 static void tstojiffie(struct timespec *tp, int res, u64 *jiff)
226 long sec = tp->tv_sec;
227 long nsec = tp->tv_nsec + res - 1;
229 if (nsec > NSEC_PER_SEC) {
231 nsec -= NSEC_PER_SEC;
235 * The scaling constants are defined in <linux/time.h>
236 * The difference between there and here is that we do the
237 * res rounding and compute a 64-bit result (well so does that
238 * but it then throws away the high bits).
240 *jiff = (mpy_l_X_l_ll(sec, SEC_CONVERSION) +
241 (mpy_l_X_l_ll(nsec, NSEC_CONVERSION) >>
242 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
246 * This function adjusts the timer as needed as a result of the clock
247 * being set. It should only be called for absolute timers, and then
248 * under the abs_list lock. It computes the time difference and sets
249 * the new jiffies value in the timer. It also updates the timers
250 * reference wall_to_monotonic value. It is complicated by the fact
251 * that tstojiffies() only handles positive times and it needs to work
252 * with both positive and negative times. Also, for negative offsets,
253 * we need to defeat the res round up.
255 * Return is true if there is a new time, else false.
257 static long add_clockset_delta(struct k_itimer *timr,
258 struct timespec *new_wall_to)
260 struct timespec delta;
264 set_normalized_timespec(&delta,
265 new_wall_to->tv_sec -
266 timr->wall_to_prev.tv_sec,
267 new_wall_to->tv_nsec -
268 timr->wall_to_prev.tv_nsec);
269 if (likely(!(delta.tv_sec | delta.tv_nsec)))
271 if (delta.tv_sec < 0) {
272 set_normalized_timespec(&delta,
275 posix_clocks[timr->it_clock].res);
278 tstojiffie(&delta, posix_clocks[timr->it_clock].res, &exp);
279 timr->wall_to_prev = *new_wall_to;
280 timr->it_timer.expires += (sign ? -exp : exp);
284 static void remove_from_abslist(struct k_itimer *timr)
286 if (!list_empty(&timr->abs_timer_entry)) {
287 spin_lock(&abs_list.lock);
288 list_del_init(&timr->abs_timer_entry);
289 spin_unlock(&abs_list.lock);
293 static void schedule_next_timer(struct k_itimer *timr)
295 struct timespec new_wall_to;
296 struct now_struct now;
300 * Set up the timer for the next interval (if there is one).
301 * Note: this code uses the abs_timer_lock to protect
302 * wall_to_prev and must hold it until exp is set, not exactly
305 * This function is used for CLOCK_REALTIME* and
306 * CLOCK_MONOTONIC* timers. If we ever want to handle other
307 * CLOCKs, the calling code (do_schedule_next_timer) would need
308 * to pull the "clock" info from the timer and dispatch the
309 * "other" CLOCKs "next timer" code (which, I suppose should
310 * also be added to the k_clock structure).
316 seq = read_seqbegin(&xtime_lock);
317 new_wall_to = wall_to_monotonic;
319 } while (read_seqretry(&xtime_lock, seq));
321 if (!list_empty(&timr->abs_timer_entry)) {
322 spin_lock(&abs_list.lock);
323 add_clockset_delta(timr, &new_wall_to);
325 posix_bump_timer(timr, now);
327 spin_unlock(&abs_list.lock);
329 posix_bump_timer(timr, now);
331 timr->it_overrun_last = timr->it_overrun;
332 timr->it_overrun = -1;
333 ++timr->it_requeue_pending;
334 add_timer(&timr->it_timer);
338 * This function is exported for use by the signal deliver code. It is
339 * called just prior to the info block being released and passes that
340 * block to us. It's function is to update the overrun entry AND to
341 * restart the timer. It should only be called if the timer is to be
342 * restarted (i.e. we have flagged this in the sys_private entry of the
345 * To protect aginst the timer going away while the interrupt is queued,
346 * we require that the it_requeue_pending flag be set.
348 void do_schedule_next_timer(struct siginfo *info)
350 struct k_itimer *timr;
353 timr = lock_timer(info->si_tid, &flags);
355 if (!timr || timr->it_requeue_pending != info->si_sys_private)
358 schedule_next_timer(timr);
359 info->si_overrun = timr->it_overrun_last;
362 unlock_timer(timr, flags);
365 int posix_timer_event(struct k_itimer *timr,int si_private)
367 memset(&timr->sigq->info, 0, sizeof(siginfo_t));
368 timr->sigq->info.si_sys_private = si_private;
370 * Send signal to the process that owns this timer.
372 * This code assumes that all the possible abs_lists share the
373 * same lock (there is only one list at this time). If this is
374 * not the case, the CLOCK info would need to be used to find
375 * the proper abs list lock.
378 timr->sigq->info.si_signo = timr->it_sigev_signo;
379 timr->sigq->info.si_errno = 0;
380 timr->sigq->info.si_code = SI_TIMER;
381 timr->sigq->info.si_tid = timr->it_id;
382 timr->sigq->info.si_value = timr->it_sigev_value;
383 if (timr->it_sigev_notify & SIGEV_THREAD_ID) {
384 if (unlikely(timr->it_process->flags & PF_EXITING)) {
385 timr->it_sigev_notify = SIGEV_SIGNAL;
386 put_task_struct(timr->it_process);
387 timr->it_process = timr->it_process->group_leader;
390 return send_sigqueue(timr->it_sigev_signo, timr->sigq,
395 return send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
401 * This function gets called when a POSIX.1b interval timer expires. It
402 * is used as a callback from the kernel internal timer. The
403 * run_timer_list code ALWAYS calls with interrupts on.
405 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
407 static void posix_timer_fn(unsigned long __data)
409 struct k_itimer *timr = (struct k_itimer *) __data;
412 struct timespec delta, new_wall_to;
416 spin_lock_irqsave(&timr->it_lock, flags);
417 set_timer_inactive(timr);
418 if (!list_empty(&timr->abs_timer_entry)) {
419 spin_lock(&abs_list.lock);
421 seq = read_seqbegin(&xtime_lock);
422 new_wall_to = wall_to_monotonic;
423 } while (read_seqretry(&xtime_lock, seq));
424 set_normalized_timespec(&delta,
426 timr->wall_to_prev.tv_sec,
427 new_wall_to.tv_nsec -
428 timr->wall_to_prev.tv_nsec);
429 if (likely((delta.tv_sec | delta.tv_nsec ) == 0)) {
430 /* do nothing, timer is on time */
431 } else if (delta.tv_sec < 0) {
432 /* do nothing, timer is already late */
434 /* timer is early due to a clock set */
436 posix_clocks[timr->it_clock].res,
438 timr->wall_to_prev = new_wall_to;
439 timr->it_timer.expires += exp;
440 add_timer(&timr->it_timer);
443 spin_unlock(&abs_list.lock);
450 si_private = ++timr->it_requeue_pending;
452 remove_from_abslist(timr);
455 if (posix_timer_event(timr, si_private))
457 * signal was not sent because of sig_ignor
458 * we will not get a call back to restart it AND
459 * it should be restarted.
461 schedule_next_timer(timr);
463 unlock_timer(timr, flags); /* hold thru abs lock to keep irq off */
467 static inline struct task_struct * good_sigevent(sigevent_t * event)
469 struct task_struct *rtn = current->group_leader;
471 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
472 (!(rtn = find_task_by_real_pid(event->sigev_notify_thread_id)) ||
473 rtn->tgid != current->tgid ||
474 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
477 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
478 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
484 void register_posix_clock(int clock_id, struct k_clock *new_clock)
486 if ((unsigned) clock_id >= MAX_CLOCKS) {
487 printk("POSIX clock register failed for clock_id %d\n",
491 posix_clocks[clock_id] = *new_clock;
494 static struct k_itimer * alloc_posix_timer(void)
496 struct k_itimer *tmr;
497 tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
500 memset(tmr, 0, sizeof (struct k_itimer));
501 INIT_LIST_HEAD(&tmr->abs_timer_entry);
502 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
503 kmem_cache_free(posix_timers_cache, tmr);
510 #define IT_ID_NOT_SET 0
511 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
515 spin_lock_irqsave(&idr_lock, flags);
516 idr_remove(&posix_timers_id, tmr->it_id);
517 spin_unlock_irqrestore(&idr_lock, flags);
519 sigqueue_free(tmr->sigq);
520 if (unlikely(tmr->it_process) &&
521 tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
522 put_task_struct(tmr->it_process);
523 kmem_cache_free(posix_timers_cache, tmr);
526 /* Create a POSIX.1b interval timer. */
529 sys_timer_create(clockid_t which_clock,
530 struct sigevent __user *timer_event_spec,
531 timer_t __user * created_timer_id)
534 struct k_itimer *new_timer = NULL;
536 struct task_struct *process = NULL;
539 int it_id_set = IT_ID_NOT_SET;
541 if ((unsigned) which_clock >= MAX_CLOCKS ||
542 !posix_clocks[which_clock].res)
545 new_timer = alloc_posix_timer();
546 if (unlikely(!new_timer))
549 spin_lock_init(&new_timer->it_lock);
551 if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
555 spin_lock_irq(&idr_lock);
556 error = idr_get_new(&posix_timers_id,
559 spin_unlock_irq(&idr_lock);
560 if (error == -EAGAIN)
564 * Wierd looking, but we return EAGAIN if the IDR is
565 * full (proper POSIX return value for this)
571 it_id_set = IT_ID_SET;
572 new_timer->it_id = (timer_t) new_timer_id;
573 new_timer->it_clock = which_clock;
574 new_timer->it_incr = 0;
575 new_timer->it_overrun = -1;
576 if (posix_clocks[which_clock].timer_create) {
577 error = posix_clocks[which_clock].timer_create(new_timer);
581 init_timer(&new_timer->it_timer);
582 new_timer->it_timer.expires = 0;
583 new_timer->it_timer.data = (unsigned long) new_timer;
584 new_timer->it_timer.function = posix_timer_fn;
585 set_timer_inactive(new_timer);
589 * return the timer_id now. The next step is hard to
590 * back out if there is an error.
592 if (copy_to_user(created_timer_id,
593 &new_timer_id, sizeof (new_timer_id))) {
597 if (timer_event_spec) {
598 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
602 new_timer->it_sigev_notify = event.sigev_notify;
603 new_timer->it_sigev_signo = event.sigev_signo;
604 new_timer->it_sigev_value = event.sigev_value;
606 read_lock(&tasklist_lock);
607 if ((process = good_sigevent(&event))) {
609 * We may be setting up this process for another
610 * thread. It may be exiting. To catch this
611 * case the we check the PF_EXITING flag. If
612 * the flag is not set, the siglock will catch
613 * him before it is too late (in exit_itimers).
615 * The exec case is a bit more invloved but easy
616 * to code. If the process is in our thread
617 * group (and it must be or we would not allow
618 * it here) and is doing an exec, it will cause
619 * us to be killed. In this case it will wait
620 * for us to die which means we can finish this
621 * linkage with our last gasp. I.e. no code :)
623 spin_lock_irqsave(&process->sighand->siglock, flags);
624 if (!(process->flags & PF_EXITING)) {
625 new_timer->it_process = process;
626 list_add(&new_timer->list,
627 &process->signal->posix_timers);
628 spin_unlock_irqrestore(&process->sighand->siglock, flags);
629 if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
630 get_task_struct(process);
632 spin_unlock_irqrestore(&process->sighand->siglock, flags);
636 read_unlock(&tasklist_lock);
642 new_timer->it_sigev_notify = SIGEV_SIGNAL;
643 new_timer->it_sigev_signo = SIGALRM;
644 new_timer->it_sigev_value.sival_int = new_timer->it_id;
645 process = current->group_leader;
646 spin_lock_irqsave(&process->sighand->siglock, flags);
647 new_timer->it_process = process;
648 list_add(&new_timer->list, &process->signal->posix_timers);
649 spin_unlock_irqrestore(&process->sighand->siglock, flags);
653 * In the case of the timer belonging to another task, after
654 * the task is unlocked, the timer is owned by the other task
655 * and may cease to exist at any time. Don't use or modify
656 * new_timer after the unlock call.
661 release_posix_timer(new_timer, it_id_set);
669 * This function checks the elements of a timespec structure.
672 * ts : Pointer to the timespec structure to check
675 * If a NULL pointer was passed in, or the tv_nsec field was less than 0
676 * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0,
677 * this function returns 0. Otherwise it returns 1.
679 static int good_timespec(const struct timespec *ts)
681 if ((!ts) || (ts->tv_sec < 0) ||
682 ((unsigned) ts->tv_nsec >= NSEC_PER_SEC))
688 * Locking issues: We need to protect the result of the id look up until
689 * we get the timer locked down so it is not deleted under us. The
690 * removal is done under the idr spinlock so we use that here to bridge
691 * the find to the timer lock. To avoid a dead lock, the timer id MUST
692 * be release with out holding the timer lock.
694 static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
696 struct k_itimer *timr;
698 * Watch out here. We do a irqsave on the idr_lock and pass the
699 * flags part over to the timer lock. Must not let interrupts in
700 * while we are moving the lock.
703 spin_lock_irqsave(&idr_lock, *flags);
704 timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
706 spin_lock(&timr->it_lock);
707 spin_unlock(&idr_lock);
709 if ((timr->it_id != timer_id) || !(timr->it_process) ||
710 timr->it_process->tgid != current->tgid) {
711 unlock_timer(timr, *flags);
715 spin_unlock_irqrestore(&idr_lock, *flags);
721 * Get the time remaining on a POSIX.1b interval timer. This function
722 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
725 * We have a couple of messes to clean up here. First there is the case
726 * of a timer that has a requeue pending. These timers should appear to
727 * be in the timer list with an expiry as if we were to requeue them
730 * The second issue is the SIGEV_NONE timer which may be active but is
731 * not really ever put in the timer list (to save system resources).
732 * This timer may be expired, and if so, we will do it here. Otherwise
733 * it is the same as a requeue pending timer WRT to what we should
737 do_timer_gettime(struct k_itimer *timr, struct itimerspec *cur_setting)
739 unsigned long expires;
740 struct now_struct now;
743 expires = timr->it_timer.expires;
744 while ((volatile long) (timr->it_timer.expires) != expires);
749 ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) &&
751 posix_time_before(&timr->it_timer, &now))
752 timr->it_timer.expires = expires = 0;
754 if (timr->it_requeue_pending & REQUEUE_PENDING ||
755 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
756 posix_bump_timer(timr, now);
757 expires = timr->it_timer.expires;
760 if (!timer_pending(&timr->it_timer))
763 expires -= now.jiffies;
765 jiffies_to_timespec(expires, &cur_setting->it_value);
766 jiffies_to_timespec(timr->it_incr, &cur_setting->it_interval);
768 if (cur_setting->it_value.tv_sec < 0) {
769 cur_setting->it_value.tv_nsec = 1;
770 cur_setting->it_value.tv_sec = 0;
774 /* Get the time remaining on a POSIX.1b interval timer. */
776 sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
778 struct k_itimer *timr;
779 struct itimerspec cur_setting;
782 timr = lock_timer(timer_id, &flags);
786 p_timer_get(&posix_clocks[timr->it_clock], timr, &cur_setting);
788 unlock_timer(timr, flags);
790 if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
796 * Get the number of overruns of a POSIX.1b interval timer. This is to
797 * be the overrun of the timer last delivered. At the same time we are
798 * accumulating overruns on the next timer. The overrun is frozen when
799 * the signal is delivered, either at the notify time (if the info block
800 * is not queued) or at the actual delivery time (as we are informed by
801 * the call back to do_schedule_next_timer(). So all we need to do is
802 * to pick up the frozen overrun.
806 sys_timer_getoverrun(timer_t timer_id)
808 struct k_itimer *timr;
812 timr = lock_timer(timer_id, &flags);
816 overrun = timr->it_overrun_last;
817 unlock_timer(timr, flags);
822 * Adjust for absolute time
824 * If absolute time is given and it is not CLOCK_MONOTONIC, we need to
825 * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and
826 * what ever clock he is using.
828 * If it is relative time, we need to add the current (CLOCK_MONOTONIC)
829 * time to it to get the proper time for the timer.
831 static int adjust_abs_time(struct k_clock *clock, struct timespec *tp,
832 int abs, u64 *exp, struct timespec *wall_to)
835 struct timespec oc = *tp;
841 * The mask pick up the 4 basic clocks
843 if (!((clock - &posix_clocks[0]) & ~CLOCKS_MASK)) {
844 jiffies_64_f = do_posix_clock_monotonic_gettime_parts(
847 * If we are doing a MONOTONIC clock
849 if((clock - &posix_clocks[0]) & CLOCKS_MONO){
850 now.tv_sec += wall_to->tv_sec;
851 now.tv_nsec += wall_to->tv_nsec;
855 * Not one of the basic clocks
857 do_posix_gettime(clock, &now);
858 jiffies_64_f = get_jiffies_64();
861 * Take away now to get delta
863 oc.tv_sec -= now.tv_sec;
864 oc.tv_nsec -= now.tv_nsec;
868 while ((oc.tv_nsec - NSEC_PER_SEC) >= 0) {
869 oc.tv_nsec -= NSEC_PER_SEC;
872 while ((oc.tv_nsec) < 0) {
873 oc.tv_nsec += NSEC_PER_SEC;
877 jiffies_64_f = get_jiffies_64();
880 * Check if the requested time is prior to now (if so set now)
883 oc.tv_sec = oc.tv_nsec = 0;
884 tstojiffie(&oc, clock->res, exp);
887 * Check if the requested time is more than the timer code
888 * can handle (if so we error out but return the value too).
890 if (*exp > ((u64)MAX_JIFFY_OFFSET))
892 * This is a considered response, not exactly in
893 * line with the standard (in fact it is silent on
894 * possible overflows). We assume such a large
895 * value is ALMOST always a programming error and
896 * try not to compound it by setting a really dumb
901 * return the actual jiffies expire time, full 64 bits
903 *exp += jiffies_64_f;
907 /* Set a POSIX.1b interval timer. */
908 /* timr->it_lock is taken. */
910 do_timer_settime(struct k_itimer *timr, int flags,
911 struct itimerspec *new_setting, struct itimerspec *old_setting)
913 struct k_clock *clock = &posix_clocks[timr->it_clock];
917 do_timer_gettime(timr, old_setting);
919 /* disable the timer */
922 * careful here. If smp we could be in the "fire" routine which will
923 * be spinning as we hold the lock. But this is ONLY an SMP issue.
926 if (timer_active(timr) && !del_timer(&timr->it_timer))
928 * It can only be active if on an other cpu. Since
929 * we have cleared the interval stuff above, it should
930 * clear once we release the spin lock. Of course once
931 * we do that anything could happen, including the
932 * complete melt down of the timer. So return with
933 * a "retry" exit status.
937 set_timer_inactive(timr);
939 del_timer(&timr->it_timer);
941 remove_from_abslist(timr);
943 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
945 timr->it_overrun_last = 0;
946 timr->it_overrun = -1;
948 *switch off the timer when it_value is zero
950 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) {
951 timr->it_timer.expires = 0;
955 if (adjust_abs_time(clock,
956 &new_setting->it_value, flags & TIMER_ABSTIME,
957 &expire_64, &(timr->wall_to_prev))) {
960 timr->it_timer.expires = (unsigned long)expire_64;
961 tstojiffie(&new_setting->it_interval, clock->res, &expire_64);
962 timr->it_incr = (unsigned long)expire_64;
965 * We do not even queue SIGEV_NONE timers! But we do put them
966 * in the abs list so we can do that right.
968 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE))
969 add_timer(&timr->it_timer);
971 if (flags & TIMER_ABSTIME && clock->abs_struct) {
972 spin_lock(&clock->abs_struct->lock);
973 list_add_tail(&(timr->abs_timer_entry),
974 &(clock->abs_struct->list));
975 spin_unlock(&clock->abs_struct->lock);
980 /* Set a POSIX.1b interval timer */
982 sys_timer_settime(timer_t timer_id, int flags,
983 const struct itimerspec __user *new_setting,
984 struct itimerspec __user *old_setting)
986 struct k_itimer *timr;
987 struct itimerspec new_spec, old_spec;
990 struct itimerspec *rtn = old_setting ? &old_spec : NULL;
995 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
998 if ((!good_timespec(&new_spec.it_interval)) ||
999 (!good_timespec(&new_spec.it_value)))
1002 timr = lock_timer(timer_id, &flag);
1006 if (!posix_clocks[timr->it_clock].timer_set)
1007 error = do_timer_settime(timr, flags, &new_spec, rtn);
1009 error = posix_clocks[timr->it_clock].timer_set(timr,
1012 unlock_timer(timr, flag);
1013 if (error == TIMER_RETRY) {
1014 rtn = NULL; // We already got the old time...
1018 if (old_setting && !error && copy_to_user(old_setting,
1019 &old_spec, sizeof (old_spec)))
1025 static inline int do_timer_delete(struct k_itimer *timer)
1029 if (timer_active(timer) && !del_timer(&timer->it_timer))
1031 * It can only be active if on an other cpu. Since
1032 * we have cleared the interval stuff above, it should
1033 * clear once we release the spin lock. Of course once
1034 * we do that anything could happen, including the
1035 * complete melt down of the timer. So return with
1036 * a "retry" exit status.
1040 del_timer(&timer->it_timer);
1042 remove_from_abslist(timer);
1047 /* Delete a POSIX.1b interval timer. */
1049 sys_timer_delete(timer_t timer_id)
1051 struct k_itimer *timer;
1058 timer = lock_timer(timer_id, &flags);
1063 error = p_timer_del(&posix_clocks[timer->it_clock], timer);
1065 if (error == TIMER_RETRY) {
1066 unlock_timer(timer, flags);
1070 p_timer_del(&posix_clocks[timer->it_clock], timer);
1072 spin_lock(¤t->sighand->siglock);
1073 list_del(&timer->list);
1074 spin_unlock(¤t->sighand->siglock);
1076 * This keeps any tasks waiting on the spin lock from thinking
1077 * they got something (see the lock code above).
1079 if (timer->it_process) {
1080 if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
1081 put_task_struct(timer->it_process);
1082 timer->it_process = NULL;
1084 unlock_timer(timer, flags);
1085 release_posix_timer(timer, IT_ID_SET);
1089 * return timer owned by the process, used by exit_itimers
1091 static inline void itimer_delete(struct k_itimer *timer)
1093 unsigned long flags;
1099 spin_lock_irqsave(&timer->it_lock, flags);
1102 error = p_timer_del(&posix_clocks[timer->it_clock], timer);
1104 if (error == TIMER_RETRY) {
1105 unlock_timer(timer, flags);
1109 p_timer_del(&posix_clocks[timer->it_clock], timer);
1111 list_del(&timer->list);
1113 * This keeps any tasks waiting on the spin lock from thinking
1114 * they got something (see the lock code above).
1116 if (timer->it_process) {
1117 if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
1118 put_task_struct(timer->it_process);
1119 timer->it_process = NULL;
1121 unlock_timer(timer, flags);
1122 release_posix_timer(timer, IT_ID_SET);
1126 * This is called by __exit_signal, only when there are no more
1127 * references to the shared signal_struct.
1129 void exit_itimers(struct signal_struct *sig)
1131 struct k_itimer *tmr;
1133 while (!list_empty(&sig->posix_timers)) {
1134 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1140 * And now for the "clock" calls
1142 * These functions are called both from timer functions (with the timer
1143 * spin_lock_irq() held and from clock calls with no locking. They must
1144 * use the save flags versions of locks.
1146 static int do_posix_gettime(struct k_clock *clock, struct timespec *tp)
1148 if (clock->clock_get)
1149 return clock->clock_get(tp);
1156 * We do ticks here to avoid the irq lock ( they take sooo long).
1157 * The seqlock is great here. Since we a reader, we don't really care
1158 * if we are interrupted since we don't take lock that will stall us or
1159 * any other cpu. Voila, no irq lock is needed.
1163 static u64 do_posix_clock_monotonic_gettime_parts(
1164 struct timespec *tp, struct timespec *mo)
1170 seq = read_seqbegin(&xtime_lock);
1172 *mo = wall_to_monotonic;
1175 } while(read_seqretry(&xtime_lock, seq));
1180 int do_posix_clock_monotonic_gettime(struct timespec *tp)
1182 struct timespec wall_to_mono;
1184 do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
1186 tp->tv_sec += wall_to_mono.tv_sec;
1187 tp->tv_nsec += wall_to_mono.tv_nsec;
1189 if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
1190 tp->tv_nsec -= NSEC_PER_SEC;
1196 int do_posix_clock_nosettime(struct timespec *tp)
1201 int do_posix_clock_notimer_create(struct k_itimer *timer)
1206 int do_posix_clock_nonanosleep(int which_clock, int flags, struct timespec *t)
1209 return -EOPNOTSUPP; /* aka ENOTSUP in userland for POSIX */
1210 #else /* parisc does define it separately. */
1216 sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
1218 struct timespec new_tp;
1220 if ((unsigned) which_clock >= MAX_CLOCKS ||
1221 !posix_clocks[which_clock].res)
1223 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1225 if (posix_clocks[which_clock].clock_set)
1226 return posix_clocks[which_clock].clock_set(&new_tp);
1228 return do_sys_settimeofday(&new_tp, NULL);
1231 static int do_clock_gettime(clockid_t which_clock, struct timespec *tp)
1233 if ((unsigned) which_clock >= MAX_CLOCKS ||
1234 !posix_clocks[which_clock].res)
1237 return do_posix_gettime(&posix_clocks[which_clock], tp);
1241 sys_clock_gettime(clockid_t which_clock, struct timespec __user *tp)
1243 struct timespec kernel_tp;
1246 error = do_clock_gettime(which_clock, &kernel_tp);
1247 if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
1255 sys_clock_getres(clockid_t which_clock, struct timespec __user *tp)
1257 struct timespec rtn_tp;
1259 if ((unsigned) which_clock >= MAX_CLOCKS ||
1260 !posix_clocks[which_clock].res)
1264 rtn_tp.tv_nsec = posix_clocks[which_clock].res;
1265 if (tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1272 static void nanosleep_wake_up(unsigned long __data)
1274 struct task_struct *p = (struct task_struct *) __data;
1280 * The standard says that an absolute nanosleep call MUST wake up at
1281 * the requested time in spite of clock settings. Here is what we do:
1282 * For each nanosleep call that needs it (only absolute and not on
1283 * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure
1284 * into the "nanosleep_abs_list". All we need is the task_struct pointer.
1285 * When ever the clock is set we just wake up all those tasks. The rest
1286 * is done by the while loop in clock_nanosleep().
1288 * On locking, clock_was_set() is called from update_wall_clock which
1289 * holds (or has held for it) a write_lock_irq( xtime_lock) and is
1290 * called from the timer bh code. Thus we need the irq save locks.
1292 * Also, on the call from update_wall_clock, that is done as part of a
1293 * softirq thing. We don't want to delay the system that much (possibly
1294 * long list of timers to fix), so we defer that work to keventd.
1297 static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);
1298 static DECLARE_WORK(clock_was_set_work, (void(*)(void*))clock_was_set, NULL);
1300 static DECLARE_MUTEX(clock_was_set_lock);
1302 void clock_was_set(void)
1304 struct k_itimer *timr;
1305 struct timespec new_wall_to;
1306 LIST_HEAD(cws_list);
1310 if (unlikely(in_interrupt())) {
1311 schedule_work(&clock_was_set_work);
1314 wake_up_all(&nanosleep_abs_wqueue);
1317 * Check if there exist TIMER_ABSTIME timers to correct.
1319 * Notes on locking: This code is run in task context with irq
1320 * on. We CAN be interrupted! All other usage of the abs list
1321 * lock is under the timer lock which holds the irq lock as
1322 * well. We REALLY don't want to scan the whole list with the
1323 * interrupt system off, AND we would like a sequence lock on
1324 * this code as well. Since we assume that the clock will not
1325 * be set often, it seems ok to take and release the irq lock
1326 * for each timer. In fact add_timer will do this, so this is
1327 * not an issue. So we know when we are done, we will move the
1328 * whole list to a new location. Then as we process each entry,
1329 * we will move it to the actual list again. This way, when our
1330 * copy is empty, we are done. We are not all that concerned
1331 * about preemption so we will use a semaphore lock to protect
1332 * aginst reentry. This way we will not stall another
1333 * processor. It is possible that this may delay some timers
1334 * that should have expired, given the new clock, but even this
1335 * will be minimal as we will always update to the current time,
1336 * even if it was set by a task that is waiting for entry to
1337 * this code. Timers that expire too early will be caught by
1338 * the expire code and restarted.
1340 * Absolute timers that repeat are left in the abs list while
1341 * waiting for the task to pick up the signal. This means we
1342 * may find timers that are not in the "add_timer" list, but are
1343 * in the abs list. We do the same thing for these, save
1344 * putting them back in the "add_timer" list. (Note, these are
1345 * left in the abs list mainly to indicate that they are
1346 * ABSOLUTE timers, a fact that is used by the re-arm code, and
1347 * for which we have no other flag.)
1351 down(&clock_was_set_lock);
1352 spin_lock_irq(&abs_list.lock);
1353 list_splice_init(&abs_list.list, &cws_list);
1354 spin_unlock_irq(&abs_list.lock);
1357 seq = read_seqbegin(&xtime_lock);
1358 new_wall_to = wall_to_monotonic;
1359 } while (read_seqretry(&xtime_lock, seq));
1361 spin_lock_irq(&abs_list.lock);
1362 if (list_empty(&cws_list)) {
1363 spin_unlock_irq(&abs_list.lock);
1366 timr = list_entry(cws_list.next, struct k_itimer,
1369 list_del_init(&timr->abs_timer_entry);
1370 if (add_clockset_delta(timr, &new_wall_to) &&
1371 del_timer(&timr->it_timer)) /* timer run yet? */
1372 add_timer(&timr->it_timer);
1373 list_add(&timr->abs_timer_entry, &abs_list.list);
1374 spin_unlock_irq(&abs_list.lock);
1377 up(&clock_was_set_lock);
1380 long clock_nanosleep_restart(struct restart_block *restart_block);
1382 extern long do_clock_nanosleep(clockid_t which_clock, int flags,
1383 struct timespec *t);
1386 sys_clock_nanosleep(clockid_t which_clock, int flags,
1387 const struct timespec __user *rqtp,
1388 struct timespec __user *rmtp)
1391 struct restart_block *restart_block =
1392 &(current_thread_info()->restart_block);
1395 if ((unsigned) which_clock >= MAX_CLOCKS ||
1396 !posix_clocks[which_clock].res)
1399 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1402 if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
1405 if (posix_clocks[which_clock].nsleep)
1406 ret = posix_clocks[which_clock].nsleep(which_clock, flags, &t);
1408 ret = do_clock_nanosleep(which_clock, flags, &t);
1410 * Do this here as do_clock_nanosleep does not have the real address
1412 restart_block->arg1 = (unsigned long)rmtp;
1414 if ((ret == -ERESTART_RESTARTBLOCK) && rmtp &&
1415 copy_to_user(rmtp, &t, sizeof (t)))
1421 do_clock_nanosleep(clockid_t which_clock, int flags, struct timespec *tsave)
1423 struct timespec t, dum;
1424 struct timer_list new_timer;
1425 DECLARE_WAITQUEUE(abs_wqueue, current);
1426 u64 rq_time = (u64)0;
1429 struct restart_block *restart_block =
1430 ¤t_thread_info()->restart_block;
1432 abs_wqueue.flags = 0;
1433 init_timer(&new_timer);
1434 new_timer.expires = 0;
1435 new_timer.data = (unsigned long) current;
1436 new_timer.function = nanosleep_wake_up;
1437 abs = flags & TIMER_ABSTIME;
1439 if (restart_block->fn == clock_nanosleep_restart) {
1441 * Interrupted by a non-delivered signal, pick up remaining
1442 * time and continue. Remaining time is in arg2 & 3.
1444 restart_block->fn = do_no_restart_syscall;
1446 rq_time = restart_block->arg3;
1447 rq_time = (rq_time << 32) + restart_block->arg2;
1450 left = rq_time - get_jiffies_64();
1452 return 0; /* Already passed */
1455 if (abs && (posix_clocks[which_clock].clock_get !=
1456 posix_clocks[CLOCK_MONOTONIC].clock_get))
1457 add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue);
1461 if (abs || !rq_time) {
1462 adjust_abs_time(&posix_clocks[which_clock], &t, abs,
1464 rq_time += (t.tv_sec || t.tv_nsec);
1467 left = rq_time - get_jiffies_64();
1468 if (left >= (s64)MAX_JIFFY_OFFSET)
1469 left = (s64)MAX_JIFFY_OFFSET;
1473 new_timer.expires = jiffies + left;
1474 __set_current_state(TASK_INTERRUPTIBLE);
1475 add_timer(&new_timer);
1479 del_timer_sync(&new_timer);
1480 left = rq_time - get_jiffies_64();
1481 } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING));
1483 if (abs_wqueue.task_list.next)
1484 finish_wait(&nanosleep_abs_wqueue, &abs_wqueue);
1486 if (left > (s64)0) {
1489 * Always restart abs calls from scratch to pick up any
1490 * clock shifting that happened while we are away.
1493 return -ERESTARTNOHAND;
1496 tsave->tv_sec = div_long_long_rem(left,
1500 * Restart works by saving the time remaing in
1501 * arg2 & 3 (it is 64-bits of jiffies). The other
1502 * info we need is the clock_id (saved in arg0).
1503 * The sys_call interface needs the users
1504 * timespec return address which _it_ saves in arg1.
1505 * Since we have cast the nanosleep call to a clock_nanosleep
1506 * both can be restarted with the same code.
1508 restart_block->fn = clock_nanosleep_restart;
1509 restart_block->arg0 = which_clock;
1513 restart_block->arg2 = rq_time & 0xffffffffLL;
1514 restart_block->arg3 = rq_time >> 32;
1516 return -ERESTART_RESTARTBLOCK;
1522 * This will restart clock_nanosleep.
1525 clock_nanosleep_restart(struct restart_block *restart_block)
1528 int ret = do_clock_nanosleep(restart_block->arg0, 0, &t);
1530 if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 &&
1531 copy_to_user((struct timespec __user *)(restart_block->arg1), &t,