[PATCH] v4l: videobuf update
[linux-2.6/history.git] / kernel / posix-timers.c
blob64940545cb841394d0752ace103251a836519f52
1 /*
2 * linux/kernel/posix_timers.c
5 * 2002-10-15 Posix Clocks & timers by George Anzinger
6 * Copyright (C) 2002 by MontaVista Software.
7 */
9 /* These are all the functions necessary to implement
10 * POSIX clocks & timers
12 #include <linux/mm.h>
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); \
33 result; })
35 #endif
36 #define CLOCK_REALTIME_RES TICK_NSEC // 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
49 * related it to <ptr>
50 * void idr_remove(struct idr *idp, int id); to release <id>
51 * void idr_init(struct idr *idp); to initialize <idp>
52 * which we supply.
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
72 #define TIMER_RETRY 1
74 #ifdef CONFIG_SMP
75 # define timer_active(tmr) \
76 ((tmr)->it_timer.entry.prev != (void *)TIMER_INACTIVE)
77 # define set_timer_inactive(tmr) \
78 do { \
79 (tmr)->it_timer.entry.prev = (void *)TIMER_INACTIVE; \
80 } while (0)
81 #else
82 # define timer_active(tmr) BARFY // error to use outside of SMP
83 # define set_timer_inactive(tmr) do { } while (0)
84 #endif
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 & \
93 SIGEV_SIGNAL & \
94 SIGEV_THREAD & \
95 SIGEV_THREAD_ID)
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);
196 return 0;
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) {
207 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) */
227 if (!timr->it_incr)
228 return;
230 posix_get_now(&now);
231 do {
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
247 * info block).
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;
255 unsigned long flags;
257 timr = lock_timer(info->si_tid, &flags);
259 if (!timr || timr->it_requeue_pending != info->si_sys_private)
260 goto exit;
262 schedule_next_timer(timr);
263 info->si_overrun = timr->it_overrun_last;
264 exit:
265 if (timr)
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)
291 int ret;
293 memset(&timr->sigq->info, 0, sizeof(siginfo_t));
295 /* Send signal to the process that owns this timer. */
296 timr->sigq->info.si_signo = timr->it_sigev_signo;
297 timr->sigq->info.si_errno = 0;
298 timr->sigq->info.si_code = SI_TIMER;
299 timr->sigq->info.si_tid = timr->it_id;
300 timr->sigq->info.si_value = timr->it_sigev_value;
301 if (timr->it_incr)
302 timr->sigq->info.si_sys_private = ++timr->it_requeue_pending;
304 if (timr->it_sigev_notify & SIGEV_THREAD_ID & MIPS_SIGEV)
305 ret = send_sigqueue(timr->it_sigev_signo, timr->sigq,
306 timr->it_process);
307 else
308 ret = send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
309 timr->it_process);
310 if (ret) {
312 * signal was not sent because of sig_ignor
313 * we will not get a call back to restart it AND
314 * it should be restarted.
316 schedule_next_timer(timr);
321 * This function gets called when a POSIX.1b interval timer expires. It
322 * is used as a callback from the kernel internal timer. The
323 * run_timer_list code ALWAYS calls with interrutps on.
325 static void posix_timer_fn(unsigned long __data)
327 struct k_itimer *timr = (struct k_itimer *) __data;
328 unsigned long flags;
330 spin_lock_irqsave(&timr->it_lock, flags);
331 set_timer_inactive(timr);
332 timer_notify_task(timr);
333 unlock_timer(timr, flags);
337 static inline struct task_struct * good_sigevent(sigevent_t * event)
339 struct task_struct *rtn = current;
341 if ((event->sigev_notify & SIGEV_THREAD_ID & MIPS_SIGEV) &&
342 (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
343 rtn->tgid != current->tgid))
344 return NULL;
346 if ((event->sigev_notify & ~SIGEV_NONE & MIPS_SIGEV) &&
347 event->sigev_signo &&
348 ((unsigned) (event->sigev_signo > SIGRTMAX)))
349 return NULL;
351 return rtn;
354 void register_posix_clock(int clock_id, struct k_clock *new_clock)
356 if ((unsigned) clock_id >= MAX_CLOCKS) {
357 printk("POSIX clock register failed for clock_id %d\n",
358 clock_id);
359 return;
361 posix_clocks[clock_id] = *new_clock;
364 static struct k_itimer * alloc_posix_timer(void)
366 struct k_itimer *tmr;
367 tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
368 memset(tmr, 0, sizeof (struct k_itimer));
369 tmr->it_id = (timer_t)-1;
370 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
371 kmem_cache_free(posix_timers_cache, tmr);
372 tmr = 0;
374 return tmr;
377 static void release_posix_timer(struct k_itimer *tmr)
379 if (tmr->it_id != -1) {
380 spin_lock_irq(&idr_lock);
381 idr_remove(&posix_timers_id, tmr->it_id);
382 spin_unlock_irq(&idr_lock);
384 sigqueue_free(tmr->sigq);
385 kmem_cache_free(posix_timers_cache, tmr);
388 /* Create a POSIX.1b interval timer. */
390 asmlinkage long
391 sys_timer_create(clockid_t which_clock,
392 struct sigevent __user *timer_event_spec,
393 timer_t __user * created_timer_id)
395 int error = 0;
396 struct k_itimer *new_timer = NULL;
397 timer_t new_timer_id;
398 struct task_struct *process = 0;
399 sigevent_t event;
401 if ((unsigned) which_clock >= MAX_CLOCKS ||
402 !posix_clocks[which_clock].res)
403 return -EINVAL;
405 new_timer = alloc_posix_timer();
406 if (unlikely(!new_timer))
407 return -EAGAIN;
409 spin_lock_init(&new_timer->it_lock);
410 do {
411 if (unlikely(!idr_pre_get(&posix_timers_id))) {
412 error = -EAGAIN;
413 new_timer->it_id = (timer_t)-1;
414 goto out;
416 spin_lock_irq(&idr_lock);
417 new_timer_id = (timer_t) idr_get_new(&posix_timers_id,
418 (void *) new_timer);
419 spin_unlock_irq(&idr_lock);
420 } while (unlikely(new_timer_id == -1));
422 new_timer->it_id = new_timer_id;
424 * return the timer_id now. The next step is hard to
425 * back out if there is an error.
427 if (copy_to_user(created_timer_id,
428 &new_timer_id, sizeof (new_timer_id))) {
429 error = -EFAULT;
430 goto out;
432 if (timer_event_spec) {
433 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
434 error = -EFAULT;
435 goto out;
437 read_lock(&tasklist_lock);
438 if ((process = good_sigevent(&event))) {
440 * We may be setting up this process for another
441 * thread. It may be exiting. To catch this
442 * case the we check the PF_EXITING flag. If
443 * the flag is not set, the task_lock will catch
444 * him before it is too late (in exit_itimers).
446 * The exec case is a bit more invloved but easy
447 * to code. If the process is in our thread
448 * group (and it must be or we would not allow
449 * it here) and is doing an exec, it will cause
450 * us to be killed. In this case it will wait
451 * for us to die which means we can finish this
452 * linkage with our last gasp. I.e. no code :)
454 task_lock(process);
455 if (!(process->flags & PF_EXITING)) {
456 list_add(&new_timer->list,
457 &process->posix_timers);
458 task_unlock(process);
459 } else {
460 task_unlock(process);
461 process = 0;
464 read_unlock(&tasklist_lock);
465 if (!process) {
466 error = -EINVAL;
467 goto out;
469 new_timer->it_sigev_notify = event.sigev_notify;
470 new_timer->it_sigev_signo = event.sigev_signo;
471 new_timer->it_sigev_value = event.sigev_value;
472 } else {
473 new_timer->it_sigev_notify = SIGEV_SIGNAL;
474 new_timer->it_sigev_signo = SIGALRM;
475 new_timer->it_sigev_value.sival_int = new_timer->it_id;
476 process = current;
477 task_lock(process);
478 list_add(&new_timer->list, &process->posix_timers);
479 task_unlock(process);
482 new_timer->it_clock = which_clock;
483 new_timer->it_incr = 0;
484 new_timer->it_overrun = -1;
485 init_timer(&new_timer->it_timer);
486 new_timer->it_timer.expires = 0;
487 new_timer->it_timer.data = (unsigned long) new_timer;
488 new_timer->it_timer.function = posix_timer_fn;
489 set_timer_inactive(new_timer);
492 * Once we set the process, it can be found so do it last...
494 new_timer->it_process = process;
495 out:
496 if (error)
497 release_posix_timer(new_timer);
499 return error;
503 * good_timespec
505 * This function checks the elements of a timespec structure.
507 * Arguments:
508 * ts : Pointer to the timespec structure to check
510 * Return value:
511 * If a NULL pointer was passed in, or the tv_nsec field was less than 0
512 * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0,
513 * this function returns 0. Otherwise it returns 1.
515 static int good_timespec(const struct timespec *ts)
517 if ((!ts) || (ts->tv_sec < 0) ||
518 ((unsigned) ts->tv_nsec >= NSEC_PER_SEC))
519 return 0;
520 return 1;
523 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
525 spin_unlock_irqrestore(&timr->it_lock, flags);
529 * Locking issues: We need to protect the result of the id look up until
530 * we get the timer locked down so it is not deleted under us. The
531 * removal is done under the idr spinlock so we use that here to bridge
532 * the find to the timer lock. To avoid a dead lock, the timer id MUST
533 * be release with out holding the timer lock.
535 static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
537 struct k_itimer *timr;
539 * Watch out here. We do a irqsave on the idr_lock and pass the
540 * flags part over to the timer lock. Must not let interrupts in
541 * while we are moving the lock.
544 spin_lock_irqsave(&idr_lock, *flags);
545 timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
546 if (timr) {
547 spin_lock(&timr->it_lock);
548 spin_unlock(&idr_lock);
550 if ((timr->it_id != timer_id) || !(timr->it_process) ||
551 timr->it_process->tgid != current->tgid) {
552 unlock_timer(timr, *flags);
553 timr = NULL;
555 } else
556 spin_unlock_irqrestore(&idr_lock, *flags);
558 return timr;
562 * Get the time remaining on a POSIX.1b interval timer. This function
563 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
564 * mess with irq.
566 * We have a couple of messes to clean up here. First there is the case
567 * of a timer that has a requeue pending. These timers should appear to
568 * be in the timer list with an expiry as if we were to requeue them
569 * now.
571 * The second issue is the SIGEV_NONE timer which may be active but is
572 * not really ever put in the timer list (to save system resources).
573 * This timer may be expired, and if so, we will do it here. Otherwise
574 * it is the same as a requeue pending timer WRT to what we should
575 * report.
577 void inline
578 do_timer_gettime(struct k_itimer *timr, struct itimerspec *cur_setting)
580 unsigned long expires;
581 struct now_struct now;
584 expires = timr->it_timer.expires;
585 while ((volatile long) (timr->it_timer.expires) != expires);
587 posix_get_now(&now);
589 if (expires && (timr->it_sigev_notify & SIGEV_NONE) && !timr->it_incr &&
590 posix_time_before(&timr->it_timer, &now))
591 timr->it_timer.expires = expires = 0;
592 if (expires) {
593 if (timr->it_requeue_pending & REQUEUE_PENDING ||
594 (timr->it_sigev_notify & SIGEV_NONE))
595 while (posix_time_before(&timr->it_timer, &now))
596 posix_bump_timer(timr);
597 else
598 if (!timer_pending(&timr->it_timer))
599 expires = 0;
600 if (expires)
601 expires -= now.jiffies;
603 jiffies_to_timespec(expires, &cur_setting->it_value);
604 jiffies_to_timespec(timr->it_incr, &cur_setting->it_interval);
606 if (cur_setting->it_value.tv_sec < 0) {
607 cur_setting->it_value.tv_nsec = 1;
608 cur_setting->it_value.tv_sec = 0;
612 /* Get the time remaining on a POSIX.1b interval timer. */
613 asmlinkage long
614 sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
616 struct k_itimer *timr;
617 struct itimerspec cur_setting;
618 unsigned long flags;
620 timr = lock_timer(timer_id, &flags);
621 if (!timr)
622 return -EINVAL;
624 p_timer_get(&posix_clocks[timr->it_clock], timr, &cur_setting);
626 unlock_timer(timr, flags);
628 if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
629 return -EFAULT;
631 return 0;
634 * Get the number of overruns of a POSIX.1b interval timer. This is to
635 * be the overrun of the timer last delivered. At the same time we are
636 * accumulating overruns on the next timer. The overrun is frozen when
637 * the signal is delivered, either at the notify time (if the info block
638 * is not queued) or at the actual delivery time (as we are informed by
639 * the call back to do_schedule_next_timer(). So all we need to do is
640 * to pick up the frozen overrun.
643 asmlinkage long
644 sys_timer_getoverrun(timer_t timer_id)
646 struct k_itimer *timr;
647 int overrun;
648 long flags;
650 timr = lock_timer(timer_id, &flags);
651 if (!timr)
652 return -EINVAL;
654 overrun = timr->it_overrun_last;
655 unlock_timer(timr, flags);
657 return overrun;
660 * Adjust for absolute time
662 * If absolute time is given and it is not CLOCK_MONOTONIC, we need to
663 * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and
664 * what ever clock he is using.
666 * If it is relative time, we need to add the current (CLOCK_MONOTONIC)
667 * time to it to get the proper time for the timer.
669 static int adjust_abs_time(struct k_clock *clock, struct timespec *tp,
670 int abs, u64 *exp)
672 struct timespec now;
673 struct timespec oc = *tp;
674 struct timespec wall_to_mono;
675 u64 jiffies_64_f;
676 int rtn =0;
678 if (abs) {
680 * The mask pick up the 4 basic clocks
682 if (!(clock - &posix_clocks[0]) & ~CLOCKS_MASK) {
683 jiffies_64_f = do_posix_clock_monotonic_gettime_parts(
684 &now, &wall_to_mono);
686 * If we are doing a MONOTONIC clock
688 if((clock - &posix_clocks[0]) & CLOCKS_MONO){
689 now.tv_sec += wall_to_mono.tv_sec;
690 now.tv_nsec += wall_to_mono.tv_nsec;
692 } else {
694 * Not one of the basic clocks
696 do_posix_gettime(clock, &now);
697 jiffies_64_f = get_jiffies_64();
700 * Take away now to get delta
702 oc.tv_sec -= now.tv_sec;
703 oc.tv_nsec -= now.tv_nsec;
705 * Normalize...
707 while ((oc.tv_nsec - NSEC_PER_SEC) >= 0) {
708 oc.tv_nsec -= NSEC_PER_SEC;
709 oc.tv_sec++;
711 while ((oc.tv_nsec) < 0) {
712 oc.tv_nsec += NSEC_PER_SEC;
713 oc.tv_sec--;
715 }else{
716 jiffies_64_f = get_jiffies_64();
719 * Check if the requested time is prior to now (if so set now)
721 if (oc.tv_sec < 0)
722 oc.tv_sec = oc.tv_nsec = 0;
723 tstojiffie(&oc, clock->res, exp);
726 * Check if the requested time is more than the timer code
727 * can handle (if so we error out but return the value too).
729 if (*exp > ((u64)MAX_JIFFY_OFFSET))
731 * This is a considered response, not exactly in
732 * line with the standard (in fact it is silent on
733 * possible overflows). We assume such a large
734 * value is ALMOST always a programming error and
735 * try not to compound it by setting a really dumb
736 * value.
738 rtn = -EINVAL;
740 * return the actual jiffies expire time, full 64 bits
742 *exp += jiffies_64_f;
743 return rtn;
746 /* Set a POSIX.1b interval timer. */
747 /* timr->it_lock is taken. */
748 static inline int
749 do_timer_settime(struct k_itimer *timr, int flags,
750 struct itimerspec *new_setting, struct itimerspec *old_setting)
752 struct k_clock *clock = &posix_clocks[timr->it_clock];
753 u64 expire_64;
755 if (old_setting)
756 do_timer_gettime(timr, old_setting);
758 /* disable the timer */
759 timr->it_incr = 0;
761 * careful here. If smp we could be in the "fire" routine which will
762 * be spinning as we hold the lock. But this is ONLY an SMP issue.
764 #ifdef CONFIG_SMP
765 if (timer_active(timr) && !del_timer(&timr->it_timer))
767 * It can only be active if on an other cpu. Since
768 * we have cleared the interval stuff above, it should
769 * clear once we release the spin lock. Of course once
770 * we do that anything could happen, including the
771 * complete melt down of the timer. So return with
772 * a "retry" exit status.
774 return TIMER_RETRY;
776 set_timer_inactive(timr);
777 #else
778 del_timer(&timr->it_timer);
779 #endif
780 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
781 ~REQUEUE_PENDING;
782 timr->it_overrun_last = 0;
783 timr->it_overrun = -1;
785 *switch off the timer when it_value is zero
787 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) {
788 timr->it_timer.expires = 0;
789 return 0;
792 if (adjust_abs_time(clock,
793 &new_setting->it_value, flags & TIMER_ABSTIME,
794 &expire_64)) {
795 return -EINVAL;
797 timr->it_timer.expires = (unsigned long)expire_64;
798 tstojiffie(&new_setting->it_interval, clock->res, &expire_64);
799 timr->it_incr = (unsigned long)expire_64;
803 * For some reason the timer does not fire immediately if expires is
804 * equal to jiffies, so the timer notify function is called directly.
805 * We do not even queue SIGEV_NONE timers!
807 if (!(timr->it_sigev_notify & SIGEV_NONE)) {
808 if (timr->it_timer.expires == jiffies)
809 timer_notify_task(timr);
810 else
811 add_timer(&timr->it_timer);
813 return 0;
816 /* Set a POSIX.1b interval timer */
817 asmlinkage long
818 sys_timer_settime(timer_t timer_id, int flags,
819 const struct itimerspec __user *new_setting,
820 struct itimerspec __user *old_setting)
822 struct k_itimer *timr;
823 struct itimerspec new_spec, old_spec;
824 int error = 0;
825 long flag;
826 struct itimerspec *rtn = old_setting ? &old_spec : NULL;
828 if (!new_setting)
829 return -EINVAL;
831 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
832 return -EFAULT;
834 if ((!good_timespec(&new_spec.it_interval)) ||
835 (!good_timespec(&new_spec.it_value)))
836 return -EINVAL;
837 retry:
838 timr = lock_timer(timer_id, &flag);
839 if (!timr)
840 return -EINVAL;
842 if (!posix_clocks[timr->it_clock].timer_set)
843 error = do_timer_settime(timr, flags, &new_spec, rtn);
844 else
845 error = posix_clocks[timr->it_clock].timer_set(timr,
846 flags,
847 &new_spec, rtn);
848 unlock_timer(timr, flag);
849 if (error == TIMER_RETRY) {
850 rtn = NULL; // We already got the old time...
851 goto retry;
854 if (old_setting && !error && copy_to_user(old_setting,
855 &old_spec, sizeof (old_spec)))
856 error = -EFAULT;
858 return error;
861 static inline int do_timer_delete(struct k_itimer *timer)
863 timer->it_incr = 0;
864 #ifdef CONFIG_SMP
865 if (timer_active(timer) && !del_timer(&timer->it_timer))
867 * It can only be active if on an other cpu. Since
868 * we have cleared the interval stuff above, it should
869 * clear once we release the spin lock. Of course once
870 * we do that anything could happen, including the
871 * complete melt down of the timer. So return with
872 * a "retry" exit status.
874 return TIMER_RETRY;
875 #else
876 del_timer(&timer->it_timer);
877 #endif
878 return 0;
881 /* Delete a POSIX.1b interval timer. */
882 asmlinkage long
883 sys_timer_delete(timer_t timer_id)
885 struct k_itimer *timer;
886 long flags;
888 #ifdef CONFIG_SMP
889 int error;
890 retry_delete:
891 #endif
892 timer = lock_timer(timer_id, &flags);
893 if (!timer)
894 return -EINVAL;
896 #ifdef CONFIG_SMP
897 error = p_timer_del(&posix_clocks[timer->it_clock], timer);
899 if (error == TIMER_RETRY) {
900 unlock_timer(timer, flags);
901 goto retry_delete;
903 #else
904 p_timer_del(&posix_clocks[timer->it_clock], timer);
905 #endif
906 task_lock(timer->it_process);
907 list_del(&timer->list);
908 task_unlock(timer->it_process);
910 * This keeps any tasks waiting on the spin lock from thinking
911 * they got something (see the lock code above).
913 timer->it_process = NULL;
914 unlock_timer(timer, flags);
915 release_posix_timer(timer);
916 return 0;
919 * return timer owned by the process, used by exit_itimers
921 static inline void itimer_delete(struct k_itimer *timer)
923 if (sys_timer_delete(timer->it_id))
924 BUG();
927 * This is exported to exit and exec
929 void exit_itimers(struct task_struct *tsk)
931 struct k_itimer *tmr;
933 task_lock(tsk);
934 while (!list_empty(&tsk->posix_timers)) {
935 tmr = list_entry(tsk->posix_timers.next, struct k_itimer, list);
936 task_unlock(tsk);
937 itimer_delete(tmr);
938 task_lock(tsk);
940 task_unlock(tsk);
944 * And now for the "clock" calls
946 * These functions are called both from timer functions (with the timer
947 * spin_lock_irq() held and from clock calls with no locking. They must
948 * use the save flags versions of locks.
950 static int do_posix_gettime(struct k_clock *clock, struct timespec *tp)
952 struct timeval tv;
954 if (clock->clock_get)
955 return clock->clock_get(tp);
957 do_gettimeofday(&tv);
958 tp->tv_sec = tv.tv_sec;
959 tp->tv_nsec = tv.tv_usec * NSEC_PER_USEC;
961 return 0;
965 * We do ticks here to avoid the irq lock ( they take sooo long).
966 * The seqlock is great here. Since we a reader, we don't really care
967 * if we are interrupted since we don't take lock that will stall us or
968 * any other cpu. Voila, no irq lock is needed.
970 * Note also that the while loop assures that the sub_jiff_offset
971 * will be less than a jiffie, thus no need to normalize the result.
972 * Well, not really, if called with ints off :(
975 static u64 do_posix_clock_monotonic_gettime_parts(
976 struct timespec *tp, struct timespec *mo)
978 u64 jiff;
979 struct timeval tpv;
980 unsigned int seq;
982 do {
983 seq = read_seqbegin(&xtime_lock);
984 do_gettimeofday(&tpv);
985 *mo = wall_to_monotonic;
986 jiff = jiffies_64;
988 } while(read_seqretry(&xtime_lock, seq));
991 * Love to get this before it is converted to usec.
992 * It would save a div AND a mpy.
994 tp->tv_sec = tpv.tv_sec;
995 tp->tv_nsec = tpv.tv_usec * NSEC_PER_USEC;
997 return jiff;
1000 int do_posix_clock_monotonic_gettime(struct timespec *tp)
1002 struct timespec wall_to_mono;
1004 do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
1006 tp->tv_sec += wall_to_mono.tv_sec;
1007 tp->tv_nsec += wall_to_mono.tv_nsec;
1009 if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
1010 tp->tv_nsec -= NSEC_PER_SEC;
1011 tp->tv_sec++;
1013 return 0;
1016 int do_posix_clock_monotonic_settime(struct timespec *tp)
1018 return -EINVAL;
1021 asmlinkage long
1022 sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
1024 struct timespec new_tp;
1026 if ((unsigned) which_clock >= MAX_CLOCKS ||
1027 !posix_clocks[which_clock].res)
1028 return -EINVAL;
1029 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1030 return -EFAULT;
1031 if (posix_clocks[which_clock].clock_set)
1032 return posix_clocks[which_clock].clock_set(&new_tp);
1034 return do_sys_settimeofday(&new_tp, NULL);
1037 asmlinkage long
1038 sys_clock_gettime(clockid_t which_clock, struct timespec __user *tp)
1040 struct timespec rtn_tp;
1041 int error = 0;
1043 if ((unsigned) which_clock >= MAX_CLOCKS ||
1044 !posix_clocks[which_clock].res)
1045 return -EINVAL;
1047 error = do_posix_gettime(&posix_clocks[which_clock], &rtn_tp);
1049 if (!error && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1050 error = -EFAULT;
1052 return error;
1056 asmlinkage long
1057 sys_clock_getres(clockid_t which_clock, struct timespec __user *tp)
1059 struct timespec rtn_tp;
1061 if ((unsigned) which_clock >= MAX_CLOCKS ||
1062 !posix_clocks[which_clock].res)
1063 return -EINVAL;
1065 rtn_tp.tv_sec = 0;
1066 rtn_tp.tv_nsec = posix_clocks[which_clock].res;
1067 if (tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1068 return -EFAULT;
1070 return 0;
1074 static void nanosleep_wake_up(unsigned long __data)
1076 struct task_struct *p = (struct task_struct *) __data;
1078 wake_up_process(p);
1082 * The standard says that an absolute nanosleep call MUST wake up at
1083 * the requested time in spite of clock settings. Here is what we do:
1084 * For each nanosleep call that needs it (only absolute and not on
1085 * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure
1086 * into the "nanosleep_abs_list". All we need is the task_struct pointer.
1087 * When ever the clock is set we just wake up all those tasks. The rest
1088 * is done by the while loop in clock_nanosleep().
1090 * On locking, clock_was_set() is called from update_wall_clock which
1091 * holds (or has held for it) a write_lock_irq( xtime_lock) and is
1092 * called from the timer bh code. Thus we need the irq save locks.
1095 static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);
1097 void clock_was_set(void)
1099 wake_up_all(&nanosleep_abs_wqueue);
1102 long clock_nanosleep_restart(struct restart_block *restart_block);
1104 extern long do_clock_nanosleep(clockid_t which_clock, int flags,
1105 struct timespec *t);
1107 #ifdef FOLD_NANO_SLEEP_INTO_CLOCK_NANO_SLEEP
1109 asmlinkage long
1110 sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1112 struct timespec t;
1113 long ret;
1115 if (copy_from_user(&t, rqtp, sizeof (t)))
1116 return -EFAULT;
1118 if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
1119 return -EINVAL;
1121 ret = do_clock_nanosleep(CLOCK_REALTIME, 0, &t);
1123 if (ret == -ERESTART_RESTARTBLOCK && rmtp &&
1124 copy_to_user(rmtp, &t, sizeof (t)))
1125 return -EFAULT;
1126 return ret;
1128 #endif // ! FOLD_NANO_SLEEP_INTO_CLOCK_NANO_SLEEP
1130 asmlinkage long
1131 sys_clock_nanosleep(clockid_t which_clock, int flags,
1132 const struct timespec __user *rqtp,
1133 struct timespec __user *rmtp)
1135 struct timespec t;
1136 int ret;
1138 if ((unsigned) which_clock >= MAX_CLOCKS ||
1139 !posix_clocks[which_clock].res)
1140 return -EINVAL;
1142 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1143 return -EFAULT;
1145 if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
1146 return -EINVAL;
1148 ret = do_clock_nanosleep(which_clock, flags, &t);
1150 if ((ret == -ERESTART_RESTARTBLOCK) && rmtp &&
1151 copy_to_user(rmtp, &t, sizeof (t)))
1152 return -EFAULT;
1153 return ret;
1156 long
1157 do_clock_nanosleep(clockid_t which_clock, int flags, struct timespec *tsave)
1159 struct timespec t;
1160 struct timer_list new_timer;
1161 DECLARE_WAITQUEUE(abs_wqueue, current);
1162 u64 rq_time = (u64)0;
1163 s64 left;
1164 int abs;
1165 struct restart_block *restart_block =
1166 &current_thread_info()->restart_block;
1168 abs_wqueue.flags = 0;
1169 init_timer(&new_timer);
1170 new_timer.expires = 0;
1171 new_timer.data = (unsigned long) current;
1172 new_timer.function = nanosleep_wake_up;
1173 abs = flags & TIMER_ABSTIME;
1175 if (restart_block->fn == clock_nanosleep_restart) {
1177 * Interrupted by a non-delivered signal, pick up remaining
1178 * time and continue.
1180 restart_block->fn = do_no_restart_syscall;
1182 rq_time = restart_block->arg3;
1183 rq_time = (rq_time << 32) + restart_block->arg2;
1184 if (!rq_time)
1185 return -EINTR;
1186 left = rq_time - get_jiffies_64();
1187 if (left <= (s64)0)
1188 return 0; /* Already passed */
1191 if (abs && (posix_clocks[which_clock].clock_get !=
1192 posix_clocks[CLOCK_MONOTONIC].clock_get))
1193 add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue);
1195 do {
1196 t = *tsave;
1197 if (abs || !rq_time) {
1198 adjust_abs_time(&posix_clocks[which_clock], &t, abs,
1199 &rq_time);
1200 rq_time += (t.tv_sec || t.tv_nsec);
1203 left = rq_time - get_jiffies_64();
1204 if (left >= (s64)MAX_JIFFY_OFFSET)
1205 left = (s64)MAX_JIFFY_OFFSET;
1206 if (left < (s64)0)
1207 break;
1209 new_timer.expires = jiffies + left;
1210 __set_current_state(TASK_INTERRUPTIBLE);
1211 add_timer(&new_timer);
1213 schedule();
1215 del_timer_sync(&new_timer);
1216 left = rq_time - get_jiffies_64();
1217 } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING));
1219 if (abs_wqueue.task_list.next)
1220 finish_wait(&nanosleep_abs_wqueue, &abs_wqueue);
1222 if (left > (s64)0) {
1225 * Always restart abs calls from scratch to pick up any
1226 * clock shifting that happened while we are away.
1228 if (abs)
1229 return -ERESTARTNOHAND;
1231 left *= TICK_NSEC;
1232 tsave->tv_sec = div_long_long_rem(left,
1233 NSEC_PER_SEC,
1234 &tsave->tv_nsec);
1235 restart_block->fn = clock_nanosleep_restart;
1236 restart_block->arg0 = which_clock;
1237 restart_block->arg1 = (unsigned long)tsave;
1238 restart_block->arg2 = rq_time & 0xffffffffLL;
1239 restart_block->arg3 = rq_time >> 32;
1241 return -ERESTART_RESTARTBLOCK;
1244 return 0;
1247 * This will restart either clock_nanosleep or clock_nanosleep
1249 long
1250 clock_nanosleep_restart(struct restart_block *restart_block)
1252 struct timespec t;
1253 int ret = do_clock_nanosleep(restart_block->arg0, 0, &t);
1255 if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 &&
1256 copy_to_user((struct timespec __user *)(restart_block->arg1), &t,
1257 sizeof (t)))
1258 return -EFAULT;
1259 return ret;