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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA
27 /* These are all the functions necessary to implement
28 * POSIX clocks & timers
31 #include <linux/smp_lock.h>
32 #include <linux/interrupt.h>
33 #include <linux/slab.h>
34 #include <linux/time.h>
36 #include <asm/uaccess.h>
37 #include <asm/semaphore.h>
38 #include <linux/list.h>
39 #include <linux/init.h>
40 #include <linux/compiler.h>
41 #include <linux/idr.h>
42 #include <linux/posix-timers.h>
43 #include <linux/wait.h>
45 #ifndef div_long_long_rem
46 #include <asm/div64.h>
48 #define div_long_long_rem(dividend,divisor,remainder) ({ \
49 u64 result = dividend; \
50 *remainder = do_div(result,divisor); \
54 #define CLOCK_REALTIME_RES TICK_NSEC /* In nano seconds. */
56 static inline u64 mpy_l_X_l_ll(unsigned long mpy1,unsigned long mpy2)
58 return (u64)mpy1 * mpy2;
61 * Management arrays for POSIX timers. Timers are kept in slab memory
62 * Timer ids are allocated by an external routine that keeps track of the
63 * id and the timer. The external interface is:
65 * void *idr_find(struct idr *idp, int id); to find timer_id <id>
66 * int idr_get_new(struct idr *idp, void *ptr); to get a new id and
68 * void idr_remove(struct idr *idp, int id); to release <id>
69 * void idr_init(struct idr *idp); to initialize <idp>
71 * The idr_get_new *may* call slab for more memory so it must not be
72 * called under a spin lock. Likewise idr_remore may release memory
73 * (but it may be ok to do this under a lock...).
74 * idr_find is just a memory look up and is quite fast. A -1 return
75 * indicates that the requested id does not exist.
79 * Lets keep our timers in a slab cache :-)
81 static kmem_cache_t *posix_timers_cache;
82 static struct idr posix_timers_id;
83 static spinlock_t idr_lock = SPIN_LOCK_UNLOCKED;
86 * Just because the timer is not in the timer list does NOT mean it is
87 * inactive. It could be in the "fire" routine getting a new expire time.
89 #define TIMER_INACTIVE 1
93 # define timer_active(tmr) \
94 ((tmr)->it_timer.entry.prev != (void *)TIMER_INACTIVE)
95 # define set_timer_inactive(tmr) \
97 (tmr)->it_timer.entry.prev = (void *)TIMER_INACTIVE; \
100 # define timer_active(tmr) BARFY // error to use outside of SMP
101 # define set_timer_inactive(tmr) do { } while (0)
104 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
105 * SIGEV values. Here we put out an error if this assumption fails.
107 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
108 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
109 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
113 #define REQUEUE_PENDING 1
115 * The timer ID is turned into a timer address by idr_find().
116 * Verifying a valid ID consists of:
118 * a) checking that idr_find() returns other than -1.
119 * b) checking that the timer id matches the one in the timer itself.
120 * c) that the timer owner is in the callers thread group.
124 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
125 * to implement others. This structure defines the various
126 * clocks and allows the possibility of adding others. We
127 * provide an interface to add clocks to the table and expect
128 * the "arch" code to add at least one clock that is high
129 * resolution. Here we define the standard CLOCK_REALTIME as a
130 * 1/HZ resolution clock.
132 * CPUTIME & THREAD_CPUTIME: We are not, at this time, definding these
133 * two clocks (and the other process related clocks (Std
134 * 1003.1d-1999). The way these should be supported, we think,
135 * is to use large negative numbers for the two clocks that are
136 * pinned to the executing process and to use -pid for clocks
137 * pinned to particular pids. Calls which supported these clock
138 * ids would split early in the function.
140 * RESOLUTION: Clock resolution is used to round up timer and interval
141 * times, NOT to report clock times, which are reported with as
142 * much resolution as the system can muster. In some cases this
143 * resolution may depend on the underlaying clock hardware and
144 * may not be quantifiable until run time, and only then is the
145 * necessary code is written. The standard says we should say
146 * something about this issue in the documentation...
148 * FUNCTIONS: The CLOCKs structure defines possible functions to handle
149 * various clock functions. For clocks that use the standard
150 * system timer code these entries should be NULL. This will
151 * allow dispatch without the overhead of indirect function
152 * calls. CLOCKS that depend on other sources (e.g. WWV or GPS)
153 * must supply functions here, even if the function just returns
154 * ENOSYS. The standard POSIX timer management code assumes the
155 * following: 1.) The k_itimer struct (sched.h) is used for the
156 * timer. 2.) The list, it_lock, it_clock, it_id and it_process
157 * fields are not modified by timer code.
159 * At this time all functions EXCEPT clock_nanosleep can be
160 * redirected by the CLOCKS structure. Clock_nanosleep is in
161 * there, but the code ignors it.
163 * Permissions: It is assumed that the clock_settime() function defined
164 * for each clock will take care of permission checks. Some
165 * clocks may be set able by any user (i.e. local process
166 * clocks) others not. Currently the only set able clock we
167 * have is CLOCK_REALTIME and its high res counter part, both of
168 * which we beg off on and pass to do_sys_settimeofday().
171 static struct k_clock posix_clocks[MAX_CLOCKS];
173 #define if_clock_do(clock_fun,alt_fun,parms) \
174 (!clock_fun) ? alt_fun parms : clock_fun parms
176 #define p_timer_get(clock,a,b) \
177 if_clock_do((clock)->timer_get,do_timer_gettime, (a,b))
179 #define p_nsleep(clock,a,b,c) \
180 if_clock_do((clock)->nsleep, do_nsleep, (a,b,c))
182 #define p_timer_del(clock,a) \
183 if_clock_do((clock)->timer_del, do_timer_delete, (a))
185 void register_posix_clock(int clock_id, struct k_clock *new_clock);
186 static int do_posix_gettime(struct k_clock *clock, struct timespec *tp);
187 static u64 do_posix_clock_monotonic_gettime_parts(
188 struct timespec *tp, struct timespec *mo);
189 int do_posix_clock_monotonic_gettime(struct timespec *tp);
190 int do_posix_clock_monotonic_settime(struct timespec *tp);
191 static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);
192 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags);
195 * Initialize everything, well, just everything in Posix clocks/timers ;)
197 static __init int init_posix_timers(void)
199 struct k_clock clock_realtime = {.res = CLOCK_REALTIME_RES };
200 struct k_clock clock_monotonic = {.res = CLOCK_REALTIME_RES,
201 .clock_get = do_posix_clock_monotonic_gettime,
202 .clock_set = do_posix_clock_monotonic_settime
205 register_posix_clock(CLOCK_REALTIME, &clock_realtime);
206 register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
208 posix_timers_cache = kmem_cache_create("posix_timers_cache",
209 sizeof (struct k_itimer), 0, 0, 0, 0);
210 idr_init(&posix_timers_id);
215 __initcall(init_posix_timers);
217 static void tstojiffie(struct timespec *tp, int res, u64 *jiff)
219 long sec = tp->tv_sec;
220 long nsec = tp->tv_nsec + res - 1;
222 if (nsec > NSEC_PER_SEC) {
224 nsec -= NSEC_PER_SEC;
228 * The scaling constants are defined in <linux/time.h>
229 * The difference between there and here is that we do the
230 * res rounding and compute a 64-bit result (well so does that
231 * but it then throws away the high bits).
233 *jiff = (mpy_l_X_l_ll(sec, SEC_CONVERSION) +
234 (mpy_l_X_l_ll(nsec, NSEC_CONVERSION) >>
235 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
238 static void schedule_next_timer(struct k_itimer *timr)
240 struct now_struct now;
242 /* Set up the timer for the next interval (if there is one) */
248 posix_bump_timer(timr);
249 }while (posix_time_before(&timr->it_timer, &now));
251 timr->it_overrun_last = timr->it_overrun;
252 timr->it_overrun = -1;
253 ++timr->it_requeue_pending;
254 add_timer(&timr->it_timer);
258 * This function is exported for use by the signal deliver code. It is
259 * called just prior to the info block being released and passes that
260 * block to us. It's function is to update the overrun entry AND to
261 * restart the timer. It should only be called if the timer is to be
262 * restarted (i.e. we have flagged this in the sys_private entry of the
265 * To protect aginst the timer going away while the interrupt is queued,
266 * we require that the it_requeue_pending flag be set.
268 void do_schedule_next_timer(struct siginfo *info)
270 struct k_itimer *timr;
273 timr = lock_timer(info->si_tid, &flags);
275 if (!timr || timr->it_requeue_pending != info->si_sys_private)
278 schedule_next_timer(timr);
279 info->si_overrun = timr->it_overrun_last;
282 unlock_timer(timr, flags);
286 * Notify the task and set up the timer for the next expiration (if
287 * applicable). This function requires that the k_itimer structure
288 * it_lock is taken. This code will requeue the timer only if we get
289 * either an error return or a flag (ret > 0) from send_seg_info
290 * indicating that the signal was either not queued or was queued
291 * without an info block. In this case, we will not get a call back to
292 * do_schedule_next_timer() so we do it here. This should be rare...
294 * An interesting problem can occur if, while a signal, and thus a call
295 * back is pending, the timer is rearmed, i.e. stopped and restarted.
296 * We then need to sort out the call back and do the right thing. What
297 * we do is to put a counter in the info block and match it with the
298 * timers copy on the call back. If they don't match, we just ignore
299 * the call back. The counter is local to the timer and we use odd to
300 * indicate a call back is pending. Note that we do allow the timer to
301 * be deleted while a signal is pending. The standard says we can
302 * allow that signal to be delivered, and we do.
305 static void timer_notify_task(struct k_itimer *timr)
309 memset(&timr->sigq->info, 0, sizeof(siginfo_t));
311 /* Send signal to the process that owns this timer. */
312 timr->sigq->info.si_signo = timr->it_sigev_signo;
313 timr->sigq->info.si_errno = 0;
314 timr->sigq->info.si_code = SI_TIMER;
315 timr->sigq->info.si_tid = timr->it_id;
316 timr->sigq->info.si_value = timr->it_sigev_value;
318 timr->sigq->info.si_sys_private = ++timr->it_requeue_pending;
320 if (timr->it_sigev_notify & SIGEV_THREAD_ID )
321 ret = send_sigqueue(timr->it_sigev_signo, timr->sigq,
324 ret = send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
328 * signal was not sent because of sig_ignor
329 * we will not get a call back to restart it AND
330 * it should be restarted.
332 schedule_next_timer(timr);
337 * This function gets called when a POSIX.1b interval timer expires. It
338 * is used as a callback from the kernel internal timer. The
339 * run_timer_list code ALWAYS calls with interrutps on.
341 static void posix_timer_fn(unsigned long __data)
343 struct k_itimer *timr = (struct k_itimer *) __data;
346 spin_lock_irqsave(&timr->it_lock, flags);
347 set_timer_inactive(timr);
348 timer_notify_task(timr);
349 unlock_timer(timr, flags);
353 static inline struct task_struct * good_sigevent(sigevent_t * event)
355 struct task_struct *rtn = current;
357 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
358 (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
359 rtn->tgid != current->tgid ||
360 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
363 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
364 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
370 void register_posix_clock(int clock_id, struct k_clock *new_clock)
372 if ((unsigned) clock_id >= MAX_CLOCKS) {
373 printk("POSIX clock register failed for clock_id %d\n",
377 posix_clocks[clock_id] = *new_clock;
380 static struct k_itimer * alloc_posix_timer(void)
382 struct k_itimer *tmr;
383 tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
386 memset(tmr, 0, sizeof (struct k_itimer));
387 tmr->it_id = (timer_t)-1;
388 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
389 kmem_cache_free(posix_timers_cache, tmr);
395 static void release_posix_timer(struct k_itimer *tmr)
397 if (tmr->it_id != -1) {
398 spin_lock_irq(&idr_lock);
399 idr_remove(&posix_timers_id, tmr->it_id);
400 spin_unlock_irq(&idr_lock);
402 sigqueue_free(tmr->sigq);
403 kmem_cache_free(posix_timers_cache, tmr);
406 /* Create a POSIX.1b interval timer. */
409 sys_timer_create(clockid_t which_clock,
410 struct sigevent __user *timer_event_spec,
411 timer_t __user * created_timer_id)
414 struct k_itimer *new_timer = NULL;
415 timer_t new_timer_id;
416 struct task_struct *process = 0;
419 if ((unsigned) which_clock >= MAX_CLOCKS ||
420 !posix_clocks[which_clock].res)
423 new_timer = alloc_posix_timer();
424 if (unlikely(!new_timer))
427 spin_lock_init(&new_timer->it_lock);
429 if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
431 new_timer->it_id = (timer_t)-1;
434 spin_lock_irq(&idr_lock);
435 new_timer_id = (timer_t) idr_get_new(&posix_timers_id,
437 spin_unlock_irq(&idr_lock);
438 } while (unlikely(new_timer_id == -1));
440 new_timer->it_id = new_timer_id;
442 * return the timer_id now. The next step is hard to
443 * back out if there is an error.
445 if (copy_to_user(created_timer_id,
446 &new_timer_id, sizeof (new_timer_id))) {
450 if (timer_event_spec) {
451 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
455 read_lock(&tasklist_lock);
456 if ((process = good_sigevent(&event))) {
458 * We may be setting up this process for another
459 * thread. It may be exiting. To catch this
460 * case the we check the PF_EXITING flag. If
461 * the flag is not set, the task_lock will catch
462 * him before it is too late (in exit_itimers).
464 * The exec case is a bit more invloved but easy
465 * to code. If the process is in our thread
466 * group (and it must be or we would not allow
467 * it here) and is doing an exec, it will cause
468 * us to be killed. In this case it will wait
469 * for us to die which means we can finish this
470 * linkage with our last gasp. I.e. no code :)
473 if (!(process->flags & PF_EXITING)) {
474 list_add(&new_timer->list,
475 &process->posix_timers);
476 task_unlock(process);
478 task_unlock(process);
482 read_unlock(&tasklist_lock);
487 new_timer->it_sigev_notify = event.sigev_notify;
488 new_timer->it_sigev_signo = event.sigev_signo;
489 new_timer->it_sigev_value = event.sigev_value;
491 new_timer->it_sigev_notify = SIGEV_SIGNAL;
492 new_timer->it_sigev_signo = SIGALRM;
493 new_timer->it_sigev_value.sival_int = new_timer->it_id;
496 list_add(&new_timer->list, &process->posix_timers);
497 task_unlock(process);
500 new_timer->it_clock = which_clock;
501 new_timer->it_incr = 0;
502 new_timer->it_overrun = -1;
503 init_timer(&new_timer->it_timer);
504 new_timer->it_timer.expires = 0;
505 new_timer->it_timer.data = (unsigned long) new_timer;
506 new_timer->it_timer.function = posix_timer_fn;
507 set_timer_inactive(new_timer);
510 * Once we set the process, it can be found so do it last...
512 new_timer->it_process = process;
515 release_posix_timer(new_timer);
523 * This function checks the elements of a timespec structure.
526 * ts : Pointer to the timespec structure to check
529 * If a NULL pointer was passed in, or the tv_nsec field was less than 0
530 * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0,
531 * this function returns 0. Otherwise it returns 1.
533 static int good_timespec(const struct timespec *ts)
535 if ((!ts) || (ts->tv_sec < 0) ||
536 ((unsigned) ts->tv_nsec >= NSEC_PER_SEC))
541 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
543 spin_unlock_irqrestore(&timr->it_lock, flags);
547 * Locking issues: We need to protect the result of the id look up until
548 * we get the timer locked down so it is not deleted under us. The
549 * removal is done under the idr spinlock so we use that here to bridge
550 * the find to the timer lock. To avoid a dead lock, the timer id MUST
551 * be release with out holding the timer lock.
553 static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
555 struct k_itimer *timr;
557 * Watch out here. We do a irqsave on the idr_lock and pass the
558 * flags part over to the timer lock. Must not let interrupts in
559 * while we are moving the lock.
562 spin_lock_irqsave(&idr_lock, *flags);
563 timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
565 spin_lock(&timr->it_lock);
566 spin_unlock(&idr_lock);
568 if ((timr->it_id != timer_id) || !(timr->it_process) ||
569 timr->it_process->tgid != current->tgid) {
570 unlock_timer(timr, *flags);
574 spin_unlock_irqrestore(&idr_lock, *flags);
580 * Get the time remaining on a POSIX.1b interval timer. This function
581 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
584 * We have a couple of messes to clean up here. First there is the case
585 * of a timer that has a requeue pending. These timers should appear to
586 * be in the timer list with an expiry as if we were to requeue them
589 * The second issue is the SIGEV_NONE timer which may be active but is
590 * not really ever put in the timer list (to save system resources).
591 * This timer may be expired, and if so, we will do it here. Otherwise
592 * it is the same as a requeue pending timer WRT to what we should
596 do_timer_gettime(struct k_itimer *timr, struct itimerspec *cur_setting)
598 unsigned long expires;
599 struct now_struct now;
602 expires = timr->it_timer.expires;
603 while ((volatile long) (timr->it_timer.expires) != expires);
608 ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) &&
610 posix_time_before(&timr->it_timer, &now))
611 timr->it_timer.expires = expires = 0;
613 if (timr->it_requeue_pending & REQUEUE_PENDING ||
614 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
615 while (posix_time_before(&timr->it_timer, &now))
616 posix_bump_timer(timr);
617 expires = timr->it_timer.expires;
620 if (!timer_pending(&timr->it_timer))
623 expires -= now.jiffies;
625 jiffies_to_timespec(expires, &cur_setting->it_value);
626 jiffies_to_timespec(timr->it_incr, &cur_setting->it_interval);
628 if (cur_setting->it_value.tv_sec < 0) {
629 cur_setting->it_value.tv_nsec = 1;
630 cur_setting->it_value.tv_sec = 0;
634 /* Get the time remaining on a POSIX.1b interval timer. */
636 sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
638 struct k_itimer *timr;
639 struct itimerspec cur_setting;
642 timr = lock_timer(timer_id, &flags);
646 p_timer_get(&posix_clocks[timr->it_clock], timr, &cur_setting);
648 unlock_timer(timr, flags);
650 if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
656 * Get the number of overruns of a POSIX.1b interval timer. This is to
657 * be the overrun of the timer last delivered. At the same time we are
658 * accumulating overruns on the next timer. The overrun is frozen when
659 * the signal is delivered, either at the notify time (if the info block
660 * is not queued) or at the actual delivery time (as we are informed by
661 * the call back to do_schedule_next_timer(). So all we need to do is
662 * to pick up the frozen overrun.
666 sys_timer_getoverrun(timer_t timer_id)
668 struct k_itimer *timr;
672 timr = lock_timer(timer_id, &flags);
676 overrun = timr->it_overrun_last;
677 unlock_timer(timr, flags);
682 * Adjust for absolute time
684 * If absolute time is given and it is not CLOCK_MONOTONIC, we need to
685 * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and
686 * what ever clock he is using.
688 * If it is relative time, we need to add the current (CLOCK_MONOTONIC)
689 * time to it to get the proper time for the timer.
691 static int adjust_abs_time(struct k_clock *clock, struct timespec *tp,
695 struct timespec oc = *tp;
696 struct timespec wall_to_mono;
702 * The mask pick up the 4 basic clocks
704 if (!(clock - &posix_clocks[0]) & ~CLOCKS_MASK) {
705 jiffies_64_f = do_posix_clock_monotonic_gettime_parts(
706 &now, &wall_to_mono);
708 * If we are doing a MONOTONIC clock
710 if((clock - &posix_clocks[0]) & CLOCKS_MONO){
711 now.tv_sec += wall_to_mono.tv_sec;
712 now.tv_nsec += wall_to_mono.tv_nsec;
716 * Not one of the basic clocks
718 do_posix_gettime(clock, &now);
719 jiffies_64_f = get_jiffies_64();
722 * Take away now to get delta
724 oc.tv_sec -= now.tv_sec;
725 oc.tv_nsec -= now.tv_nsec;
729 while ((oc.tv_nsec - NSEC_PER_SEC) >= 0) {
730 oc.tv_nsec -= NSEC_PER_SEC;
733 while ((oc.tv_nsec) < 0) {
734 oc.tv_nsec += NSEC_PER_SEC;
738 jiffies_64_f = get_jiffies_64();
741 * Check if the requested time is prior to now (if so set now)
744 oc.tv_sec = oc.tv_nsec = 0;
745 tstojiffie(&oc, clock->res, exp);
748 * Check if the requested time is more than the timer code
749 * can handle (if so we error out but return the value too).
751 if (*exp > ((u64)MAX_JIFFY_OFFSET))
753 * This is a considered response, not exactly in
754 * line with the standard (in fact it is silent on
755 * possible overflows). We assume such a large
756 * value is ALMOST always a programming error and
757 * try not to compound it by setting a really dumb
762 * return the actual jiffies expire time, full 64 bits
764 *exp += jiffies_64_f;
768 /* Set a POSIX.1b interval timer. */
769 /* timr->it_lock is taken. */
771 do_timer_settime(struct k_itimer *timr, int flags,
772 struct itimerspec *new_setting, struct itimerspec *old_setting)
774 struct k_clock *clock = &posix_clocks[timr->it_clock];
778 do_timer_gettime(timr, old_setting);
780 /* disable the timer */
783 * careful here. If smp we could be in the "fire" routine which will
784 * be spinning as we hold the lock. But this is ONLY an SMP issue.
787 if (timer_active(timr) && !del_timer(&timr->it_timer))
789 * It can only be active if on an other cpu. Since
790 * we have cleared the interval stuff above, it should
791 * clear once we release the spin lock. Of course once
792 * we do that anything could happen, including the
793 * complete melt down of the timer. So return with
794 * a "retry" exit status.
798 set_timer_inactive(timr);
800 del_timer(&timr->it_timer);
802 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
804 timr->it_overrun_last = 0;
805 timr->it_overrun = -1;
807 *switch off the timer when it_value is zero
809 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) {
810 timr->it_timer.expires = 0;
814 if (adjust_abs_time(clock,
815 &new_setting->it_value, flags & TIMER_ABSTIME,
819 timr->it_timer.expires = (unsigned long)expire_64;
820 tstojiffie(&new_setting->it_interval, clock->res, &expire_64);
821 timr->it_incr = (unsigned long)expire_64;
825 * For some reason the timer does not fire immediately if expires is
826 * equal to jiffies, so the timer notify function is called directly.
827 * We do not even queue SIGEV_NONE timers!
829 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)) {
830 if (timr->it_timer.expires == jiffies)
831 timer_notify_task(timr);
833 add_timer(&timr->it_timer);
838 /* Set a POSIX.1b interval timer */
840 sys_timer_settime(timer_t timer_id, int flags,
841 const struct itimerspec __user *new_setting,
842 struct itimerspec __user *old_setting)
844 struct k_itimer *timr;
845 struct itimerspec new_spec, old_spec;
848 struct itimerspec *rtn = old_setting ? &old_spec : NULL;
853 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
856 if ((!good_timespec(&new_spec.it_interval)) ||
857 (!good_timespec(&new_spec.it_value)))
860 timr = lock_timer(timer_id, &flag);
864 if (!posix_clocks[timr->it_clock].timer_set)
865 error = do_timer_settime(timr, flags, &new_spec, rtn);
867 error = posix_clocks[timr->it_clock].timer_set(timr,
870 unlock_timer(timr, flag);
871 if (error == TIMER_RETRY) {
872 rtn = NULL; // We already got the old time...
876 if (old_setting && !error && copy_to_user(old_setting,
877 &old_spec, sizeof (old_spec)))
883 static inline int do_timer_delete(struct k_itimer *timer)
887 if (timer_active(timer) && !del_timer(&timer->it_timer))
889 * It can only be active if on an other cpu. Since
890 * we have cleared the interval stuff above, it should
891 * clear once we release the spin lock. Of course once
892 * we do that anything could happen, including the
893 * complete melt down of the timer. So return with
894 * a "retry" exit status.
898 del_timer(&timer->it_timer);
903 /* Delete a POSIX.1b interval timer. */
905 sys_timer_delete(timer_t timer_id)
907 struct k_itimer *timer;
914 timer = lock_timer(timer_id, &flags);
919 error = p_timer_del(&posix_clocks[timer->it_clock], timer);
921 if (error == TIMER_RETRY) {
922 unlock_timer(timer, flags);
926 p_timer_del(&posix_clocks[timer->it_clock], timer);
928 task_lock(timer->it_process);
929 list_del(&timer->list);
930 task_unlock(timer->it_process);
932 * This keeps any tasks waiting on the spin lock from thinking
933 * they got something (see the lock code above).
935 timer->it_process = NULL;
936 unlock_timer(timer, flags);
937 release_posix_timer(timer);
941 * return timer owned by the process, used by exit_itimers
943 static inline void itimer_delete(struct k_itimer *timer)
945 if (sys_timer_delete(timer->it_id))
949 * This is exported to exit and exec
951 void exit_itimers(struct task_struct *tsk)
953 struct k_itimer *tmr;
956 while (!list_empty(&tsk->posix_timers)) {
957 tmr = list_entry(tsk->posix_timers.next, struct k_itimer, list);
966 * And now for the "clock" calls
968 * These functions are called both from timer functions (with the timer
969 * spin_lock_irq() held and from clock calls with no locking. They must
970 * use the save flags versions of locks.
972 static int do_posix_gettime(struct k_clock *clock, struct timespec *tp)
976 if (clock->clock_get)
977 return clock->clock_get(tp);
979 do_gettimeofday(&tv);
980 tp->tv_sec = tv.tv_sec;
981 tp->tv_nsec = tv.tv_usec * NSEC_PER_USEC;
987 * We do ticks here to avoid the irq lock ( they take sooo long).
988 * The seqlock is great here. Since we a reader, we don't really care
989 * if we are interrupted since we don't take lock that will stall us or
990 * any other cpu. Voila, no irq lock is needed.
994 static u64 do_posix_clock_monotonic_gettime_parts(
995 struct timespec *tp, struct timespec *mo)
1002 seq = read_seqbegin(&xtime_lock);
1003 do_gettimeofday(&tpv);
1004 *mo = wall_to_monotonic;
1007 } while(read_seqretry(&xtime_lock, seq));
1010 * Love to get this before it is converted to usec.
1011 * It would save a div AND a mpy.
1013 tp->tv_sec = tpv.tv_sec;
1014 tp->tv_nsec = tpv.tv_usec * NSEC_PER_USEC;
1019 int do_posix_clock_monotonic_gettime(struct timespec *tp)
1021 struct timespec wall_to_mono;
1023 do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
1025 tp->tv_sec += wall_to_mono.tv_sec;
1026 tp->tv_nsec += wall_to_mono.tv_nsec;
1028 if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
1029 tp->tv_nsec -= NSEC_PER_SEC;
1035 int do_posix_clock_monotonic_settime(struct timespec *tp)
1041 sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
1043 struct timespec new_tp;
1045 if ((unsigned) which_clock >= MAX_CLOCKS ||
1046 !posix_clocks[which_clock].res)
1048 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1050 if (posix_clocks[which_clock].clock_set)
1051 return posix_clocks[which_clock].clock_set(&new_tp);
1053 return do_sys_settimeofday(&new_tp, NULL);
1057 sys_clock_gettime(clockid_t which_clock, struct timespec __user *tp)
1059 struct timespec rtn_tp;
1062 if ((unsigned) which_clock >= MAX_CLOCKS ||
1063 !posix_clocks[which_clock].res)
1066 error = do_posix_gettime(&posix_clocks[which_clock], &rtn_tp);
1068 if (!error && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1076 sys_clock_getres(clockid_t which_clock, struct timespec __user *tp)
1078 struct timespec rtn_tp;
1080 if ((unsigned) which_clock >= MAX_CLOCKS ||
1081 !posix_clocks[which_clock].res)
1085 rtn_tp.tv_nsec = posix_clocks[which_clock].res;
1086 if (tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1093 static void nanosleep_wake_up(unsigned long __data)
1095 struct task_struct *p = (struct task_struct *) __data;
1101 * The standard says that an absolute nanosleep call MUST wake up at
1102 * the requested time in spite of clock settings. Here is what we do:
1103 * For each nanosleep call that needs it (only absolute and not on
1104 * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure
1105 * into the "nanosleep_abs_list". All we need is the task_struct pointer.
1106 * When ever the clock is set we just wake up all those tasks. The rest
1107 * is done by the while loop in clock_nanosleep().
1109 * On locking, clock_was_set() is called from update_wall_clock which
1110 * holds (or has held for it) a write_lock_irq( xtime_lock) and is
1111 * called from the timer bh code. Thus we need the irq save locks.
1114 static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);
1116 void clock_was_set(void)
1118 wake_up_all(&nanosleep_abs_wqueue);
1121 long clock_nanosleep_restart(struct restart_block *restart_block);
1123 extern long do_clock_nanosleep(clockid_t which_clock, int flags,
1124 struct timespec *t);
1127 sys_clock_nanosleep(clockid_t which_clock, int flags,
1128 const struct timespec __user *rqtp,
1129 struct timespec __user *rmtp)
1132 struct restart_block *restart_block =
1133 &(current_thread_info()->restart_block);
1136 if ((unsigned) which_clock >= MAX_CLOCKS ||
1137 !posix_clocks[which_clock].res)
1140 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1143 if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
1146 ret = do_clock_nanosleep(which_clock, flags, &t);
1148 * Do this here as do_clock_nanosleep does not have the real address
1150 restart_block->arg1 = (unsigned long)rmtp;
1152 if ((ret == -ERESTART_RESTARTBLOCK) && rmtp &&
1153 copy_to_user(rmtp, &t, sizeof (t)))
1159 do_clock_nanosleep(clockid_t which_clock, int flags, struct timespec *tsave)
1162 struct timer_list new_timer;
1163 DECLARE_WAITQUEUE(abs_wqueue, current);
1164 u64 rq_time = (u64)0;
1167 struct restart_block *restart_block =
1168 ¤t_thread_info()->restart_block;
1170 abs_wqueue.flags = 0;
1171 init_timer(&new_timer);
1172 new_timer.expires = 0;
1173 new_timer.data = (unsigned long) current;
1174 new_timer.function = nanosleep_wake_up;
1175 abs = flags & TIMER_ABSTIME;
1177 if (restart_block->fn == clock_nanosleep_restart) {
1179 * Interrupted by a non-delivered signal, pick up remaining
1180 * time and continue. Remaining time is in arg2 & 3.
1182 restart_block->fn = do_no_restart_syscall;
1184 rq_time = restart_block->arg3;
1185 rq_time = (rq_time << 32) + restart_block->arg2;
1188 left = rq_time - get_jiffies_64();
1190 return 0; /* Already passed */
1193 if (abs && (posix_clocks[which_clock].clock_get !=
1194 posix_clocks[CLOCK_MONOTONIC].clock_get))
1195 add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue);
1199 if (abs || !rq_time) {
1200 adjust_abs_time(&posix_clocks[which_clock], &t, abs,
1202 rq_time += (t.tv_sec || t.tv_nsec);
1205 left = rq_time - get_jiffies_64();
1206 if (left >= (s64)MAX_JIFFY_OFFSET)
1207 left = (s64)MAX_JIFFY_OFFSET;
1211 new_timer.expires = jiffies + left;
1212 __set_current_state(TASK_INTERRUPTIBLE);
1213 add_timer(&new_timer);
1217 del_timer_sync(&new_timer);
1218 left = rq_time - get_jiffies_64();
1219 } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING));
1221 if (abs_wqueue.task_list.next)
1222 finish_wait(&nanosleep_abs_wqueue, &abs_wqueue);
1224 if (left > (s64)0) {
1227 * Always restart abs calls from scratch to pick up any
1228 * clock shifting that happened while we are away.
1231 return -ERESTARTNOHAND;
1234 tsave->tv_sec = div_long_long_rem(left,
1238 * Restart works by saving the time remaing in
1239 * arg2 & 3 (it is 64-bits of jiffies). The other
1240 * info we need is the clock_id (saved in arg0).
1241 * The sys_call interface needs the users
1242 * timespec return address which _it_ saves in arg1.
1243 * Since we have cast the nanosleep call to a clock_nanosleep
1244 * both can be restarted with the same code.
1246 restart_block->fn = clock_nanosleep_restart;
1247 restart_block->arg0 = which_clock;
1251 restart_block->arg2 = rq_time & 0xffffffffLL;
1252 restart_block->arg3 = rq_time >> 32;
1254 return -ERESTART_RESTARTBLOCK;
1260 * This will restart clock_nanosleep.
1263 clock_nanosleep_restart(struct restart_block *restart_block)
1266 int ret = do_clock_nanosleep(restart_block->arg0, 0, &t);
1268 if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 &&
1269 copy_to_user((struct timespec __user *)(restart_block->arg1), &t,