2 * linux/kernel/posix_timers.c
5 * 2002-10-15 Posix Clocks & timers by George Anzinger
6 * Copyright (C) 2002 by MontaVista Software.
9 /* These are all the functions necessary to implement
10 * POSIX clocks & timers
13 #include <linux/smp_lock.h>
14 #include <linux/interrupt.h>
15 #include <linux/slab.h>
16 #include <linux/time.h>
18 #include <asm/uaccess.h>
19 #include <asm/semaphore.h>
20 #include <linux/list.h>
21 #include <linux/init.h>
22 #include <linux/compiler.h>
23 #include <linux/idr.h>
24 #include <linux/posix-timers.h>
25 #include <linux/wait.h>
27 #ifndef div_long_long_rem
28 #include <asm/div64.h>
30 #define div_long_long_rem(dividend,divisor,remainder) ({ \
31 u64 result = dividend; \
32 *remainder = do_div(result,divisor); \
36 #define CLOCK_REALTIME_RES TICK_NSEC(TICK_USEC) // In nano seconds.
38 static inline u64
mpy_l_X_l_ll(unsigned long mpy1
,unsigned long mpy2
)
40 return (u64
)mpy1
* mpy2
;
43 * Management arrays for POSIX timers. Timers are kept in slab memory
44 * Timer ids are allocated by an external routine that keeps track of the
45 * id and the timer. The external interface is:
47 * void *idr_find(struct idr *idp, int id); to find timer_id <id>
48 * int idr_get_new(struct idr *idp, void *ptr); to get a new id and
50 * void idr_remove(struct idr *idp, int id); to release <id>
51 * void idr_init(struct idr *idp); to initialize <idp>
53 * The idr_get_new *may* call slab for more memory so it must not be
54 * called under a spin lock. Likewise idr_remore may release memory
55 * (but it may be ok to do this under a lock...).
56 * idr_find is just a memory look up and is quite fast. A -1 return
57 * indicates that the requested id does not exist.
61 * Lets keep our timers in a slab cache :-)
63 static kmem_cache_t
*posix_timers_cache
;
64 static struct idr posix_timers_id
;
65 static spinlock_t idr_lock
= SPIN_LOCK_UNLOCKED
;
68 * Just because the timer is not in the timer list does NOT mean it is
69 * inactive. It could be in the "fire" routine getting a new expire time.
71 #define TIMER_INACTIVE 1
75 # define timer_active(tmr) \
76 ((tmr)->it_timer.entry.prev != (void *)TIMER_INACTIVE)
77 # define set_timer_inactive(tmr) \
79 (tmr)->it_timer.entry.prev = (void *)TIMER_INACTIVE; \
82 # define timer_active(tmr) BARFY // error to use outside of SMP
83 # define set_timer_inactive(tmr) do { } while (0)
87 * For some reason mips/mips64 define the SIGEV constants plus 128.
88 * Here we define a mask to get rid of the common bits. The
89 * optimizer should make this costless to all but mips.
90 * Note that no common bits (the non-mips case) will give 0xffffffff.
92 #define MIPS_SIGEV ~(SIGEV_NONE & \
97 #define REQUEUE_PENDING 1
99 * The timer ID is turned into a timer address by idr_find().
100 * Verifying a valid ID consists of:
102 * a) checking that idr_find() returns other than -1.
103 * b) checking that the timer id matches the one in the timer itself.
104 * c) that the timer owner is in the callers thread group.
108 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
109 * to implement others. This structure defines the various
110 * clocks and allows the possibility of adding others. We
111 * provide an interface to add clocks to the table and expect
112 * the "arch" code to add at least one clock that is high
113 * resolution. Here we define the standard CLOCK_REALTIME as a
114 * 1/HZ resolution clock.
116 * CPUTIME & THREAD_CPUTIME: We are not, at this time, definding these
117 * two clocks (and the other process related clocks (Std
118 * 1003.1d-1999). The way these should be supported, we think,
119 * is to use large negative numbers for the two clocks that are
120 * pinned to the executing process and to use -pid for clocks
121 * pinned to particular pids. Calls which supported these clock
122 * ids would split early in the function.
124 * RESOLUTION: Clock resolution is used to round up timer and interval
125 * times, NOT to report clock times, which are reported with as
126 * much resolution as the system can muster. In some cases this
127 * resolution may depend on the underlaying clock hardware and
128 * may not be quantifiable until run time, and only then is the
129 * necessary code is written. The standard says we should say
130 * something about this issue in the documentation...
132 * FUNCTIONS: The CLOCKs structure defines possible functions to handle
133 * various clock functions. For clocks that use the standard
134 * system timer code these entries should be NULL. This will
135 * allow dispatch without the overhead of indirect function
136 * calls. CLOCKS that depend on other sources (e.g. WWV or GPS)
137 * must supply functions here, even if the function just returns
138 * ENOSYS. The standard POSIX timer management code assumes the
139 * following: 1.) The k_itimer struct (sched.h) is used for the
140 * timer. 2.) The list, it_lock, it_clock, it_id and it_process
141 * fields are not modified by timer code.
143 * At this time all functions EXCEPT clock_nanosleep can be
144 * redirected by the CLOCKS structure. Clock_nanosleep is in
145 * there, but the code ignors it.
147 * Permissions: It is assumed that the clock_settime() function defined
148 * for each clock will take care of permission checks. Some
149 * clocks may be set able by any user (i.e. local process
150 * clocks) others not. Currently the only set able clock we
151 * have is CLOCK_REALTIME and its high res counter part, both of
152 * which we beg off on and pass to do_sys_settimeofday().
155 static struct k_clock posix_clocks
[MAX_CLOCKS
];
157 #define if_clock_do(clock_fun,alt_fun,parms) \
158 (!clock_fun) ? alt_fun parms : clock_fun parms
160 #define p_timer_get(clock,a,b) \
161 if_clock_do((clock)->timer_get,do_timer_gettime, (a,b))
163 #define p_nsleep(clock,a,b,c) \
164 if_clock_do((clock)->nsleep, do_nsleep, (a,b,c))
166 #define p_timer_del(clock,a) \
167 if_clock_do((clock)->timer_del, do_timer_delete, (a))
169 void register_posix_clock(int clock_id
, struct k_clock
*new_clock
);
170 static int do_posix_gettime(struct k_clock
*clock
, struct timespec
*tp
);
171 static u64
do_posix_clock_monotonic_gettime_parts(
172 struct timespec
*tp
, struct timespec
*mo
);
173 int do_posix_clock_monotonic_gettime(struct timespec
*tp
);
174 int do_posix_clock_monotonic_settime(struct timespec
*tp
);
175 static struct k_itimer
*lock_timer(timer_t timer_id
, unsigned long *flags
);
176 static inline void unlock_timer(struct k_itimer
*timr
, unsigned long flags
);
179 * Initialize everything, well, just everything in Posix clocks/timers ;)
181 static __init
int init_posix_timers(void)
183 struct k_clock clock_realtime
= {.res
= CLOCK_REALTIME_RES
};
184 struct k_clock clock_monotonic
= {.res
= CLOCK_REALTIME_RES
,
185 .clock_get
= do_posix_clock_monotonic_gettime
,
186 .clock_set
= do_posix_clock_monotonic_settime
189 register_posix_clock(CLOCK_REALTIME
, &clock_realtime
);
190 register_posix_clock(CLOCK_MONOTONIC
, &clock_monotonic
);
192 posix_timers_cache
= kmem_cache_create("posix_timers_cache",
193 sizeof (struct k_itimer
), 0, 0, 0, 0);
194 idr_init(&posix_timers_id
);
199 __initcall(init_posix_timers
);
201 static void tstojiffie(struct timespec
*tp
, int res
, u64
*jiff
)
203 long sec
= tp
->tv_sec
;
204 long nsec
= tp
->tv_nsec
+ res
- 1;
206 if (nsec
> NSEC_PER_SEC
) {
208 nsec
-= NSEC_PER_SEC
;
212 * The scaling constants are defined in <linux/time.h>
213 * The difference between there and here is that we do the
214 * res rounding and compute a 64-bit result (well so does that
215 * but it then throws away the high bits).
217 *jiff
= (mpy_l_X_l_ll(sec
, SEC_CONVERSION
) +
218 (mpy_l_X_l_ll(nsec
, NSEC_CONVERSION
) >>
219 (NSEC_JIFFIE_SC
- SEC_JIFFIE_SC
))) >> SEC_JIFFIE_SC
;
222 static void schedule_next_timer(struct k_itimer
*timr
)
224 struct now_struct now
;
226 /* Set up the timer for the next interval (if there is one) */
232 posix_bump_timer(timr
);
233 }while (posix_time_before(&timr
->it_timer
, &now
));
235 timr
->it_overrun_last
= timr
->it_overrun
;
236 timr
->it_overrun
= -1;
237 ++timr
->it_requeue_pending
;
238 add_timer(&timr
->it_timer
);
242 * This function is exported for use by the signal deliver code. It is
243 * called just prior to the info block being released and passes that
244 * block to us. It's function is to update the overrun entry AND to
245 * restart the timer. It should only be called if the timer is to be
246 * restarted (i.e. we have flagged this in the sys_private entry of the
249 * To protect aginst the timer going away while the interrupt is queued,
250 * we require that the it_requeue_pending flag be set.
252 void do_schedule_next_timer(struct siginfo
*info
)
254 struct k_itimer
*timr
;
257 timr
= lock_timer(info
->si_tid
, &flags
);
259 if (!timr
|| timr
->it_requeue_pending
!= info
->si_sys_private
)
262 schedule_next_timer(timr
);
263 info
->si_overrun
= timr
->it_overrun_last
;
266 unlock_timer(timr
, flags
);
270 * Notify the task and set up the timer for the next expiration (if
271 * applicable). This function requires that the k_itimer structure
272 * it_lock is taken. This code will requeue the timer only if we get
273 * either an error return or a flag (ret > 0) from send_seg_info
274 * indicating that the signal was either not queued or was queued
275 * without an info block. In this case, we will not get a call back to
276 * do_schedule_next_timer() so we do it here. This should be rare...
278 * An interesting problem can occur if, while a signal, and thus a call
279 * back is pending, the timer is rearmed, i.e. stopped and restarted.
280 * We then need to sort out the call back and do the right thing. What
281 * we do is to put a counter in the info block and match it with the
282 * timers copy on the call back. If they don't match, we just ignore
283 * the call back. The counter is local to the timer and we use odd to
284 * indicate a call back is pending. Note that we do allow the timer to
285 * be deleted while a signal is pending. The standard says we can
286 * allow that signal to be delivered, and we do.
289 static void timer_notify_task(struct k_itimer
*timr
)
294 memset(&info
, 0, sizeof (info
));
296 /* Send signal to the process that owns this timer. */
297 info
.si_signo
= timr
->it_sigev_signo
;
299 info
.si_code
= SI_TIMER
;
300 info
.si_tid
= timr
->it_id
;
301 info
.si_value
= timr
->it_sigev_value
;
303 info
.si_sys_private
= ++timr
->it_requeue_pending
;
305 if (timr
->it_sigev_notify
& SIGEV_THREAD_ID
& MIPS_SIGEV
)
306 ret
= send_sig_info(info
.si_signo
, &info
, timr
->it_process
);
308 ret
= send_group_sig_info(info
.si_signo
, &info
,
314 * Signal was not sent. May or may not need to
317 printk(KERN_WARNING
"sending signal failed: %d\n", ret
);
320 * signal was not sent because of sig_ignor or,
321 * possibly no queue memory OR will be sent but,
322 * we will not get a call back to restart it AND
323 * it should be restarted.
325 schedule_next_timer(timr
);
328 * all's well new signal queued
335 * This function gets called when a POSIX.1b interval timer expires. It
336 * is used as a callback from the kernel internal timer. The
337 * run_timer_list code ALWAYS calls with interrutps on.
339 static void posix_timer_fn(unsigned long __data
)
341 struct k_itimer
*timr
= (struct k_itimer
*) __data
;
344 spin_lock_irqsave(&timr
->it_lock
, flags
);
345 set_timer_inactive(timr
);
346 timer_notify_task(timr
);
347 unlock_timer(timr
, flags
);
351 static inline struct task_struct
* good_sigevent(sigevent_t
* event
)
353 struct task_struct
*rtn
= current
;
355 if ((event
->sigev_notify
& SIGEV_THREAD_ID
& MIPS_SIGEV
) &&
356 (!(rtn
= find_task_by_pid(event
->sigev_notify_thread_id
)) ||
357 rtn
->tgid
!= current
->tgid
))
360 if ((event
->sigev_notify
& ~SIGEV_NONE
& MIPS_SIGEV
) &&
361 ((unsigned) (event
->sigev_signo
> SIGRTMAX
)))
367 void register_posix_clock(int clock_id
, struct k_clock
*new_clock
)
369 if ((unsigned) clock_id
>= MAX_CLOCKS
) {
370 printk("POSIX clock register failed for clock_id %d\n",
374 posix_clocks
[clock_id
] = *new_clock
;
377 static struct k_itimer
* alloc_posix_timer(void)
379 struct k_itimer
*tmr
;
380 tmr
= kmem_cache_alloc(posix_timers_cache
, GFP_KERNEL
);
381 memset(tmr
, 0, sizeof (struct k_itimer
));
386 static void release_posix_timer(struct k_itimer
*tmr
)
388 if (tmr
->it_id
!= -1) {
389 spin_lock_irq(&idr_lock
);
390 idr_remove(&posix_timers_id
, tmr
->it_id
);
391 spin_unlock_irq(&idr_lock
);
393 kmem_cache_free(posix_timers_cache
, tmr
);
396 /* Create a POSIX.1b interval timer. */
399 sys_timer_create(clockid_t which_clock
,
400 struct sigevent __user
*timer_event_spec
,
401 timer_t __user
* created_timer_id
)
404 struct k_itimer
*new_timer
= NULL
;
405 timer_t new_timer_id
;
406 struct task_struct
*process
= 0;
409 if ((unsigned) which_clock
>= MAX_CLOCKS
||
410 !posix_clocks
[which_clock
].res
)
413 new_timer
= alloc_posix_timer();
414 if (unlikely(!new_timer
))
417 spin_lock_init(&new_timer
->it_lock
);
419 if (unlikely(!idr_pre_get(&posix_timers_id
))) {
421 new_timer_id
= (timer_t
)-1;
424 spin_lock_irq(&idr_lock
);
425 new_timer_id
= (timer_t
) idr_get_new(&posix_timers_id
,
427 spin_unlock_irq(&idr_lock
);
428 } while (unlikely(new_timer_id
== -1));
430 new_timer
->it_id
= new_timer_id
;
432 * return the timer_id now. The next step is hard to
433 * back out if there is an error.
435 if (copy_to_user(created_timer_id
,
436 &new_timer_id
, sizeof (new_timer_id
))) {
440 if (timer_event_spec
) {
441 if (copy_from_user(&event
, timer_event_spec
, sizeof (event
))) {
445 read_lock(&tasklist_lock
);
446 if ((process
= good_sigevent(&event
))) {
448 * We may be setting up this process for another
449 * thread. It may be exiting. To catch this
450 * case the we check the PF_EXITING flag. If
451 * the flag is not set, the task_lock will catch
452 * him before it is too late (in exit_itimers).
454 * The exec case is a bit more invloved but easy
455 * to code. If the process is in our thread
456 * group (and it must be or we would not allow
457 * it here) and is doing an exec, it will cause
458 * us to be killed. In this case it will wait
459 * for us to die which means we can finish this
460 * linkage with our last gasp. I.e. no code :)
463 if (!(process
->flags
& PF_EXITING
)) {
464 list_add(&new_timer
->list
,
465 &process
->posix_timers
);
466 task_unlock(process
);
468 task_unlock(process
);
472 read_unlock(&tasklist_lock
);
477 new_timer
->it_sigev_notify
= event
.sigev_notify
;
478 new_timer
->it_sigev_signo
= event
.sigev_signo
;
479 new_timer
->it_sigev_value
= event
.sigev_value
;
481 new_timer
->it_sigev_notify
= SIGEV_SIGNAL
;
482 new_timer
->it_sigev_signo
= SIGALRM
;
483 new_timer
->it_sigev_value
.sival_int
= new_timer
->it_id
;
486 list_add(&new_timer
->list
, &process
->posix_timers
);
487 task_unlock(process
);
490 new_timer
->it_clock
= which_clock
;
491 new_timer
->it_incr
= 0;
492 new_timer
->it_overrun
= -1;
493 init_timer(&new_timer
->it_timer
);
494 new_timer
->it_timer
.expires
= 0;
495 new_timer
->it_timer
.data
= (unsigned long) new_timer
;
496 new_timer
->it_timer
.function
= posix_timer_fn
;
497 set_timer_inactive(new_timer
);
500 * Once we set the process, it can be found so do it last...
502 new_timer
->it_process
= process
;
505 release_posix_timer(new_timer
);
513 * This function checks the elements of a timespec structure.
516 * ts : Pointer to the timespec structure to check
519 * If a NULL pointer was passed in, or the tv_nsec field was less than 0
520 * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0,
521 * this function returns 0. Otherwise it returns 1.
523 static int good_timespec(const struct timespec
*ts
)
525 if ((!ts
) || (ts
->tv_sec
< 0) ||
526 ((unsigned) ts
->tv_nsec
>= NSEC_PER_SEC
))
531 static inline void unlock_timer(struct k_itimer
*timr
, unsigned long flags
)
533 spin_unlock_irqrestore(&timr
->it_lock
, flags
);
537 * Locking issues: We need to protect the result of the id look up until
538 * we get the timer locked down so it is not deleted under us. The
539 * removal is done under the idr spinlock so we use that here to bridge
540 * the find to the timer lock. To avoid a dead lock, the timer id MUST
541 * be release with out holding the timer lock.
543 static struct k_itimer
* lock_timer(timer_t timer_id
, unsigned long *flags
)
545 struct k_itimer
*timr
;
547 * Watch out here. We do a irqsave on the idr_lock and pass the
548 * flags part over to the timer lock. Must not let interrupts in
549 * while we are moving the lock.
552 spin_lock_irqsave(&idr_lock
, *flags
);
553 timr
= (struct k_itimer
*) idr_find(&posix_timers_id
, (int) timer_id
);
555 spin_lock(&timr
->it_lock
);
556 spin_unlock(&idr_lock
);
558 if ((timr
->it_id
!= timer_id
) || !(timr
->it_process
) ||
559 timr
->it_process
->tgid
!= current
->tgid
) {
560 unlock_timer(timr
, *flags
);
564 spin_unlock_irqrestore(&idr_lock
, *flags
);
570 * Get the time remaining on a POSIX.1b interval timer. This function
571 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
574 * We have a couple of messes to clean up here. First there is the case
575 * of a timer that has a requeue pending. These timers should appear to
576 * be in the timer list with an expiry as if we were to requeue them
579 * The second issue is the SIGEV_NONE timer which may be active but is
580 * not really ever put in the timer list (to save system resources).
581 * This timer may be expired, and if so, we will do it here. Otherwise
582 * it is the same as a requeue pending timer WRT to what we should
586 do_timer_gettime(struct k_itimer
*timr
, struct itimerspec
*cur_setting
)
588 unsigned long expires
;
589 struct now_struct now
;
592 expires
= timr
->it_timer
.expires
;
593 while ((volatile long) (timr
->it_timer
.expires
) != expires
);
597 if (expires
&& (timr
->it_sigev_notify
& SIGEV_NONE
) && !timr
->it_incr
&&
598 posix_time_before(&timr
->it_timer
, &now
))
599 timr
->it_timer
.expires
= expires
= 0;
601 if (timr
->it_requeue_pending
& REQUEUE_PENDING
||
602 (timr
->it_sigev_notify
& SIGEV_NONE
))
603 while (posix_time_before(&timr
->it_timer
, &now
))
604 posix_bump_timer(timr
);
606 if (!timer_pending(&timr
->it_timer
))
609 expires
-= now
.jiffies
;
611 jiffies_to_timespec(expires
, &cur_setting
->it_value
);
612 jiffies_to_timespec(timr
->it_incr
, &cur_setting
->it_interval
);
614 if (cur_setting
->it_value
.tv_sec
< 0) {
615 cur_setting
->it_value
.tv_nsec
= 1;
616 cur_setting
->it_value
.tv_sec
= 0;
620 /* Get the time remaining on a POSIX.1b interval timer. */
622 sys_timer_gettime(timer_t timer_id
, struct itimerspec __user
*setting
)
624 struct k_itimer
*timr
;
625 struct itimerspec cur_setting
;
628 timr
= lock_timer(timer_id
, &flags
);
632 p_timer_get(&posix_clocks
[timr
->it_clock
], timr
, &cur_setting
);
634 unlock_timer(timr
, flags
);
636 if (copy_to_user(setting
, &cur_setting
, sizeof (cur_setting
)))
642 * Get the number of overruns of a POSIX.1b interval timer. This is to
643 * be the overrun of the timer last delivered. At the same time we are
644 * accumulating overruns on the next timer. The overrun is frozen when
645 * the signal is delivered, either at the notify time (if the info block
646 * is not queued) or at the actual delivery time (as we are informed by
647 * the call back to do_schedule_next_timer(). So all we need to do is
648 * to pick up the frozen overrun.
652 sys_timer_getoverrun(timer_t timer_id
)
654 struct k_itimer
*timr
;
658 timr
= lock_timer(timer_id
, &flags
);
662 overrun
= timr
->it_overrun_last
;
663 unlock_timer(timr
, flags
);
668 * Adjust for absolute time
670 * If absolute time is given and it is not CLOCK_MONOTONIC, we need to
671 * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and
672 * what ever clock he is using.
674 * If it is relative time, we need to add the current (CLOCK_MONOTONIC)
675 * time to it to get the proper time for the timer.
677 static int adjust_abs_time(struct k_clock
*clock
, struct timespec
*tp
,
681 struct timespec oc
= *tp
;
682 struct timespec wall_to_mono
;
688 * The mask pick up the 4 basic clocks
690 if (!(clock
- &posix_clocks
[0]) & ~CLOCKS_MASK
) {
691 jiffies_64_f
= do_posix_clock_monotonic_gettime_parts(
692 &now
, &wall_to_mono
);
694 * If we are doing a MONOTONIC clock
696 if((clock
- &posix_clocks
[0]) & CLOCKS_MONO
){
697 now
.tv_sec
+= wall_to_mono
.tv_sec
;
698 now
.tv_nsec
+= wall_to_mono
.tv_nsec
;
702 * Not one of the basic clocks
704 do_posix_gettime(clock
, &now
);
705 jiffies_64_f
= get_jiffies_64();
708 * Take away now to get delta
710 oc
.tv_sec
-= now
.tv_sec
;
711 oc
.tv_nsec
-= now
.tv_nsec
;
715 while ((oc
.tv_nsec
- NSEC_PER_SEC
) >= 0) {
716 oc
.tv_nsec
-= NSEC_PER_SEC
;
719 while ((oc
.tv_nsec
) < 0) {
720 oc
.tv_nsec
+= NSEC_PER_SEC
;
724 jiffies_64_f
= get_jiffies_64();
727 * Check if the requested time is prior to now (if so set now)
730 oc
.tv_sec
= oc
.tv_nsec
= 0;
731 tstojiffie(&oc
, clock
->res
, exp
);
734 * Check if the requested time is more than the timer code
735 * can handle (if so we error out but return the value too).
737 if (*exp
> ((u64
)MAX_JIFFY_OFFSET
))
739 * This is a considered response, not exactly in
740 * line with the standard (in fact it is silent on
741 * possible overflows). We assume such a large
742 * value is ALMOST always a programming error and
743 * try not to compound it by setting a really dumb
748 * return the actual jiffies expire time, full 64 bits
750 *exp
+= jiffies_64_f
;
754 /* Set a POSIX.1b interval timer. */
755 /* timr->it_lock is taken. */
757 do_timer_settime(struct k_itimer
*timr
, int flags
,
758 struct itimerspec
*new_setting
, struct itimerspec
*old_setting
)
760 struct k_clock
*clock
= &posix_clocks
[timr
->it_clock
];
764 do_timer_gettime(timr
, old_setting
);
766 /* disable the timer */
769 * careful here. If smp we could be in the "fire" routine which will
770 * be spinning as we hold the lock. But this is ONLY an SMP issue.
773 if (timer_active(timr
) && !del_timer(&timr
->it_timer
))
775 * It can only be active if on an other cpu. Since
776 * we have cleared the interval stuff above, it should
777 * clear once we release the spin lock. Of course once
778 * we do that anything could happen, including the
779 * complete melt down of the timer. So return with
780 * a "retry" exit status.
784 set_timer_inactive(timr
);
786 del_timer(&timr
->it_timer
);
788 timr
->it_requeue_pending
= (timr
->it_requeue_pending
+ 2) &
790 timr
->it_overrun_last
= 0;
791 timr
->it_overrun
= -1;
793 *switch off the timer when it_value is zero
795 if (!new_setting
->it_value
.tv_sec
&& !new_setting
->it_value
.tv_nsec
) {
796 timr
->it_timer
.expires
= 0;
800 if (adjust_abs_time(clock
,
801 &new_setting
->it_value
, flags
& TIMER_ABSTIME
,
805 timr
->it_timer
.expires
= (unsigned long)expire_64
;
806 tstojiffie(&new_setting
->it_interval
, clock
->res
, &expire_64
);
807 timr
->it_incr
= (unsigned long)expire_64
;
811 * For some reason the timer does not fire immediately if expires is
812 * equal to jiffies, so the timer notify function is called directly.
813 * We do not even queue SIGEV_NONE timers!
815 if (!(timr
->it_sigev_notify
& SIGEV_NONE
)) {
816 if (timr
->it_timer
.expires
== jiffies
)
817 timer_notify_task(timr
);
819 add_timer(&timr
->it_timer
);
824 /* Set a POSIX.1b interval timer */
826 sys_timer_settime(timer_t timer_id
, int flags
,
827 const struct itimerspec __user
*new_setting
,
828 struct itimerspec __user
*old_setting
)
830 struct k_itimer
*timr
;
831 struct itimerspec new_spec
, old_spec
;
834 struct itimerspec
*rtn
= old_setting
? &old_spec
: NULL
;
839 if (copy_from_user(&new_spec
, new_setting
, sizeof (new_spec
)))
842 if ((!good_timespec(&new_spec
.it_interval
)) ||
843 (!good_timespec(&new_spec
.it_value
)))
846 timr
= lock_timer(timer_id
, &flag
);
850 if (!posix_clocks
[timr
->it_clock
].timer_set
)
851 error
= do_timer_settime(timr
, flags
, &new_spec
, rtn
);
853 error
= posix_clocks
[timr
->it_clock
].timer_set(timr
,
856 unlock_timer(timr
, flag
);
857 if (error
== TIMER_RETRY
) {
858 rtn
= NULL
; // We already got the old time...
862 if (old_setting
&& !error
&& copy_to_user(old_setting
,
863 &old_spec
, sizeof (old_spec
)))
869 static inline int do_timer_delete(struct k_itimer
*timer
)
873 if (timer_active(timer
) && !del_timer(&timer
->it_timer
))
875 * It can only be active if on an other cpu. Since
876 * we have cleared the interval stuff above, it should
877 * clear once we release the spin lock. Of course once
878 * we do that anything could happen, including the
879 * complete melt down of the timer. So return with
880 * a "retry" exit status.
884 del_timer(&timer
->it_timer
);
889 /* Delete a POSIX.1b interval timer. */
891 sys_timer_delete(timer_t timer_id
)
893 struct k_itimer
*timer
;
900 timer
= lock_timer(timer_id
, &flags
);
905 error
= p_timer_del(&posix_clocks
[timer
->it_clock
], timer
);
907 if (error
== TIMER_RETRY
) {
908 unlock_timer(timer
, flags
);
912 p_timer_del(&posix_clocks
[timer
->it_clock
], timer
);
914 task_lock(timer
->it_process
);
915 list_del(&timer
->list
);
916 task_unlock(timer
->it_process
);
918 * This keeps any tasks waiting on the spin lock from thinking
919 * they got something (see the lock code above).
921 timer
->it_process
= NULL
;
922 unlock_timer(timer
, flags
);
923 release_posix_timer(timer
);
927 * return timer owned by the process, used by exit_itimers
929 static inline void itimer_delete(struct k_itimer
*timer
)
931 if (sys_timer_delete(timer
->it_id
))
935 * This is exported to exit and exec
937 void exit_itimers(struct task_struct
*tsk
)
939 struct k_itimer
*tmr
;
942 while (!list_empty(&tsk
->posix_timers
)) {
943 tmr
= list_entry(tsk
->posix_timers
.next
, struct k_itimer
, list
);
952 * And now for the "clock" calls
954 * These functions are called both from timer functions (with the timer
955 * spin_lock_irq() held and from clock calls with no locking. They must
956 * use the save flags versions of locks.
958 static int do_posix_gettime(struct k_clock
*clock
, struct timespec
*tp
)
960 if (clock
->clock_get
)
961 return clock
->clock_get(tp
);
963 do_gettimeofday((struct timeval
*) tp
);
964 tp
->tv_nsec
*= NSEC_PER_USEC
;
969 * We do ticks here to avoid the irq lock ( they take sooo long).
970 * The seqlock is great here. Since we a reader, we don't really care
971 * if we are interrupted since we don't take lock that will stall us or
972 * any other cpu. Voila, no irq lock is needed.
974 * Note also that the while loop assures that the sub_jiff_offset
975 * will be less than a jiffie, thus no need to normalize the result.
976 * Well, not really, if called with ints off :(
979 static u64
do_posix_clock_monotonic_gettime_parts(
980 struct timespec
*tp
, struct timespec
*mo
)
987 seq
= read_seqbegin(&xtime_lock
);
988 do_gettimeofday(&tpv
);
989 *mo
= wall_to_monotonic
;
992 } while(read_seqretry(&xtime_lock
, seq
));
995 * Love to get this before it is converted to usec.
996 * It would save a div AND a mpy.
998 tp
->tv_sec
= tpv
.tv_sec
;
999 tp
->tv_nsec
= tpv
.tv_usec
* NSEC_PER_USEC
;
1004 int do_posix_clock_monotonic_gettime(struct timespec
*tp
)
1006 struct timespec wall_to_mono
;
1008 do_posix_clock_monotonic_gettime_parts(tp
, &wall_to_mono
);
1010 tp
->tv_sec
+= wall_to_mono
.tv_sec
;
1011 tp
->tv_nsec
+= wall_to_mono
.tv_nsec
;
1013 if ((tp
->tv_nsec
- NSEC_PER_SEC
) > 0) {
1014 tp
->tv_nsec
-= NSEC_PER_SEC
;
1020 int do_posix_clock_monotonic_settime(struct timespec
*tp
)
1026 sys_clock_settime(clockid_t which_clock
, const struct timespec __user
*tp
)
1028 struct timespec new_tp
;
1030 if ((unsigned) which_clock
>= MAX_CLOCKS
||
1031 !posix_clocks
[which_clock
].res
)
1033 if (copy_from_user(&new_tp
, tp
, sizeof (*tp
)))
1035 if (posix_clocks
[which_clock
].clock_set
)
1036 return posix_clocks
[which_clock
].clock_set(&new_tp
);
1038 new_tp
.tv_nsec
/= NSEC_PER_USEC
;
1039 return do_sys_settimeofday((struct timeval
*) &new_tp
, NULL
);
1043 sys_clock_gettime(clockid_t which_clock
, struct timespec __user
*tp
)
1045 struct timespec rtn_tp
;
1048 if ((unsigned) which_clock
>= MAX_CLOCKS
||
1049 !posix_clocks
[which_clock
].res
)
1052 error
= do_posix_gettime(&posix_clocks
[which_clock
], &rtn_tp
);
1054 if (!error
&& copy_to_user(tp
, &rtn_tp
, sizeof (rtn_tp
)))
1062 sys_clock_getres(clockid_t which_clock
, struct timespec __user
*tp
)
1064 struct timespec rtn_tp
;
1066 if ((unsigned) which_clock
>= MAX_CLOCKS
||
1067 !posix_clocks
[which_clock
].res
)
1071 rtn_tp
.tv_nsec
= posix_clocks
[which_clock
].res
;
1072 if (tp
&& copy_to_user(tp
, &rtn_tp
, sizeof (rtn_tp
)))
1079 static void nanosleep_wake_up(unsigned long __data
)
1081 struct task_struct
*p
= (struct task_struct
*) __data
;
1087 * The standard says that an absolute nanosleep call MUST wake up at
1088 * the requested time in spite of clock settings. Here is what we do:
1089 * For each nanosleep call that needs it (only absolute and not on
1090 * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure
1091 * into the "nanosleep_abs_list". All we need is the task_struct pointer.
1092 * When ever the clock is set we just wake up all those tasks. The rest
1093 * is done by the while loop in clock_nanosleep().
1095 * On locking, clock_was_set() is called from update_wall_clock which
1096 * holds (or has held for it) a write_lock_irq( xtime_lock) and is
1097 * called from the timer bh code. Thus we need the irq save locks.
1100 static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue
);
1102 void clock_was_set(void)
1104 wake_up_all(&nanosleep_abs_wqueue
);
1107 long clock_nanosleep_restart(struct restart_block
*restart_block
);
1109 extern long do_clock_nanosleep(clockid_t which_clock
, int flags
,
1110 struct timespec
*t
);
1112 #ifdef FOLD_NANO_SLEEP_INTO_CLOCK_NANO_SLEEP
1115 sys_nanosleep(struct timespec __user
*rqtp
, struct timespec __user
*rmtp
)
1120 if (copy_from_user(&t
, rqtp
, sizeof (t
)))
1123 if ((unsigned) t
.tv_nsec
>= NSEC_PER_SEC
|| t
.tv_sec
< 0)
1126 ret
= do_clock_nanosleep(CLOCK_REALTIME
, 0, &t
);
1128 if (ret
== -ERESTART_RESTARTBLOCK
&& rmtp
&&
1129 copy_to_user(rmtp
, &t
, sizeof (t
)))
1133 #endif // ! FOLD_NANO_SLEEP_INTO_CLOCK_NANO_SLEEP
1136 sys_clock_nanosleep(clockid_t which_clock
, int flags
,
1137 const struct timespec __user
*rqtp
,
1138 struct timespec __user
*rmtp
)
1143 if ((unsigned) which_clock
>= MAX_CLOCKS
||
1144 !posix_clocks
[which_clock
].res
)
1147 if (copy_from_user(&t
, rqtp
, sizeof (struct timespec
)))
1150 if ((unsigned) t
.tv_nsec
>= NSEC_PER_SEC
|| t
.tv_sec
< 0)
1153 ret
= do_clock_nanosleep(which_clock
, flags
, &t
);
1155 if ((ret
== -ERESTART_RESTARTBLOCK
) && rmtp
&&
1156 copy_to_user(rmtp
, &t
, sizeof (t
)))
1162 do_clock_nanosleep(clockid_t which_clock
, int flags
, struct timespec
*tsave
)
1165 struct timer_list new_timer
;
1166 DECLARE_WAITQUEUE(abs_wqueue
, current
);
1167 u64 rq_time
= (u64
)0;
1170 struct restart_block
*restart_block
=
1171 ¤t_thread_info()->restart_block
;
1173 abs_wqueue
.flags
= 0;
1174 init_timer(&new_timer
);
1175 new_timer
.expires
= 0;
1176 new_timer
.data
= (unsigned long) current
;
1177 new_timer
.function
= nanosleep_wake_up
;
1178 abs
= flags
& TIMER_ABSTIME
;
1180 if (restart_block
->fn
== clock_nanosleep_restart
) {
1182 * Interrupted by a non-delivered signal, pick up remaining
1183 * time and continue.
1185 restart_block
->fn
= do_no_restart_syscall
;
1187 rq_time
= restart_block
->arg3
;
1188 rq_time
= (rq_time
<< 32) + restart_block
->arg2
;
1191 left
= rq_time
- get_jiffies_64();
1193 return 0; /* Already passed */
1196 if (abs
&& (posix_clocks
[which_clock
].clock_get
!=
1197 posix_clocks
[CLOCK_MONOTONIC
].clock_get
))
1198 add_wait_queue(&nanosleep_abs_wqueue
, &abs_wqueue
);
1202 if (abs
|| !rq_time
) {
1203 adjust_abs_time(&posix_clocks
[which_clock
], &t
, abs
,
1207 left
= rq_time
- get_jiffies_64();
1208 if (left
>= (s64
)MAX_JIFFY_OFFSET
)
1209 left
= (s64
)MAX_JIFFY_OFFSET
;
1213 new_timer
.expires
= jiffies
+ left
;
1214 __set_current_state(TASK_INTERRUPTIBLE
);
1215 add_timer(&new_timer
);
1219 del_timer_sync(&new_timer
);
1220 left
= rq_time
- get_jiffies_64();
1221 } while (left
> (s64
)0 && !test_thread_flag(TIF_SIGPENDING
));
1223 if (abs_wqueue
.task_list
.next
)
1224 finish_wait(&nanosleep_abs_wqueue
, &abs_wqueue
);
1226 if (left
> (s64
)0) {
1229 * Always restart abs calls from scratch to pick up any
1230 * clock shifting that happened while we are away.
1233 return -ERESTARTNOHAND
;
1235 left
*= TICK_NSEC(TICK_USEC
);
1236 tsave
->tv_sec
= div_long_long_rem(left
,
1239 restart_block
->fn
= clock_nanosleep_restart
;
1240 restart_block
->arg0
= which_clock
;
1241 restart_block
->arg1
= (unsigned long)tsave
;
1242 restart_block
->arg2
= rq_time
& 0xffffffffLL
;
1243 restart_block
->arg3
= rq_time
>> 32;
1245 return -ERESTART_RESTARTBLOCK
;
1251 * This will restart either clock_nanosleep or clock_nanosleep
1254 clock_nanosleep_restart(struct restart_block
*restart_block
)
1257 int ret
= do_clock_nanosleep(restart_block
->arg0
, 0, &t
);
1259 if ((ret
== -ERESTART_RESTARTBLOCK
) && restart_block
->arg1
&&
1260 copy_to_user((struct timespec __user
*)(restart_block
->arg1
), &t
,