[NETLINK]: Type-safe netlink messages/attributes interface
[hh.org.git] / kernel / posix-timers.c
blobea55c7a1cd75a3dbaa50040b3d55255c12a5786f
1 /*
2 * linux/kernel/posix_timers.c
5 * 2002-10-15 Posix Clocks & timers
6 * by George Anzinger george@mvista.com
8 * Copyright (C) 2002 2003 by MontaVista Software.
10 * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
11 * Copyright (C) 2004 Boris Hu
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA
30 /* These are all the functions necessary to implement
31 * POSIX clocks & timers
33 #include <linux/mm.h>
34 #include <linux/smp_lock.h>
35 #include <linux/interrupt.h>
36 #include <linux/slab.h>
37 #include <linux/time.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include <linux/list.h>
42 #include <linux/init.h>
43 #include <linux/compiler.h>
44 #include <linux/idr.h>
45 #include <linux/posix-timers.h>
46 #include <linux/syscalls.h>
47 #include <linux/wait.h>
48 #include <linux/workqueue.h>
49 #include <linux/module.h>
51 #ifndef div_long_long_rem
52 #include <asm/div64.h>
54 #define div_long_long_rem(dividend,divisor,remainder) ({ \
55 u64 result = dividend; \
56 *remainder = do_div(result,divisor); \
57 result; })
59 #endif
60 #define CLOCK_REALTIME_RES TICK_NSEC /* In nano seconds. */
62 static inline u64 mpy_l_X_l_ll(unsigned long mpy1,unsigned long mpy2)
64 return (u64)mpy1 * mpy2;
67 * Management arrays for POSIX timers. Timers are kept in slab memory
68 * Timer ids are allocated by an external routine that keeps track of the
69 * id and the timer. The external interface is:
71 * void *idr_find(struct idr *idp, int id); to find timer_id <id>
72 * int idr_get_new(struct idr *idp, void *ptr); to get a new id and
73 * related it to <ptr>
74 * void idr_remove(struct idr *idp, int id); to release <id>
75 * void idr_init(struct idr *idp); to initialize <idp>
76 * which we supply.
77 * The idr_get_new *may* call slab for more memory so it must not be
78 * called under a spin lock. Likewise idr_remore may release memory
79 * (but it may be ok to do this under a lock...).
80 * idr_find is just a memory look up and is quite fast. A -1 return
81 * indicates that the requested id does not exist.
85 * Lets keep our timers in a slab cache :-)
87 static kmem_cache_t *posix_timers_cache;
88 static struct idr posix_timers_id;
89 static DEFINE_SPINLOCK(idr_lock);
92 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
93 * SIGEV values. Here we put out an error if this assumption fails.
95 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
96 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
97 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
98 #endif
102 * The timer ID is turned into a timer address by idr_find().
103 * Verifying a valid ID consists of:
105 * a) checking that idr_find() returns other than -1.
106 * b) checking that the timer id matches the one in the timer itself.
107 * c) that the timer owner is in the callers thread group.
111 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
112 * to implement others. This structure defines the various
113 * clocks and allows the possibility of adding others. We
114 * provide an interface to add clocks to the table and expect
115 * the "arch" code to add at least one clock that is high
116 * resolution. Here we define the standard CLOCK_REALTIME as a
117 * 1/HZ resolution clock.
119 * RESOLUTION: Clock resolution is used to round up timer and interval
120 * times, NOT to report clock times, which are reported with as
121 * much resolution as the system can muster. In some cases this
122 * resolution may depend on the underlying clock hardware and
123 * may not be quantifiable until run time, and only then is the
124 * necessary code is written. The standard says we should say
125 * something about this issue in the documentation...
127 * FUNCTIONS: The CLOCKs structure defines possible functions to handle
128 * various clock functions. For clocks that use the standard
129 * system timer code these entries should be NULL. This will
130 * allow dispatch without the overhead of indirect function
131 * calls. CLOCKS that depend on other sources (e.g. WWV or GPS)
132 * must supply functions here, even if the function just returns
133 * ENOSYS. The standard POSIX timer management code assumes the
134 * following: 1.) The k_itimer struct (sched.h) is used for the
135 * timer. 2.) The list, it_lock, it_clock, it_id and it_process
136 * fields are not modified by timer code.
138 * At this time all functions EXCEPT clock_nanosleep can be
139 * redirected by the CLOCKS structure. Clock_nanosleep is in
140 * there, but the code ignores it.
142 * Permissions: It is assumed that the clock_settime() function defined
143 * for each clock will take care of permission checks. Some
144 * clocks may be set able by any user (i.e. local process
145 * clocks) others not. Currently the only set able clock we
146 * have is CLOCK_REALTIME and its high res counter part, both of
147 * which we beg off on and pass to do_sys_settimeofday().
150 static struct k_clock posix_clocks[MAX_CLOCKS];
152 * We only have one real clock that can be set so we need only one abs list,
153 * even if we should want to have several clocks with differing resolutions.
155 static struct k_clock_abs abs_list = {.list = LIST_HEAD_INIT(abs_list.list),
156 .lock = SPIN_LOCK_UNLOCKED};
158 static void posix_timer_fn(unsigned long);
159 static u64 do_posix_clock_monotonic_gettime_parts(
160 struct timespec *tp, struct timespec *mo);
161 int do_posix_clock_monotonic_gettime(struct timespec *tp);
162 static int do_posix_clock_monotonic_get(clockid_t, struct timespec *tp);
164 static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);
166 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
168 spin_unlock_irqrestore(&timr->it_lock, flags);
172 * Call the k_clock hook function if non-null, or the default function.
174 #define CLOCK_DISPATCH(clock, call, arglist) \
175 ((clock) < 0 ? posix_cpu_##call arglist : \
176 (posix_clocks[clock].call != NULL \
177 ? (*posix_clocks[clock].call) arglist : common_##call arglist))
180 * Default clock hook functions when the struct k_clock passed
181 * to register_posix_clock leaves a function pointer null.
183 * The function common_CALL is the default implementation for
184 * the function pointer CALL in struct k_clock.
187 static inline int common_clock_getres(clockid_t which_clock,
188 struct timespec *tp)
190 tp->tv_sec = 0;
191 tp->tv_nsec = posix_clocks[which_clock].res;
192 return 0;
195 static inline int common_clock_get(clockid_t which_clock, struct timespec *tp)
197 getnstimeofday(tp);
198 return 0;
201 static inline int common_clock_set(clockid_t which_clock, struct timespec *tp)
203 return do_sys_settimeofday(tp, NULL);
206 static inline int common_timer_create(struct k_itimer *new_timer)
208 INIT_LIST_HEAD(&new_timer->it.real.abs_timer_entry);
209 init_timer(&new_timer->it.real.timer);
210 new_timer->it.real.timer.data = (unsigned long) new_timer;
211 new_timer->it.real.timer.function = posix_timer_fn;
212 return 0;
216 * These ones are defined below.
218 static int common_nsleep(clockid_t, int flags, struct timespec *t);
219 static void common_timer_get(struct k_itimer *, struct itimerspec *);
220 static int common_timer_set(struct k_itimer *, int,
221 struct itimerspec *, struct itimerspec *);
222 static int common_timer_del(struct k_itimer *timer);
225 * Return nonzero iff we know a priori this clockid_t value is bogus.
227 static inline int invalid_clockid(clockid_t which_clock)
229 if (which_clock < 0) /* CPU clock, posix_cpu_* will check it */
230 return 0;
231 if ((unsigned) which_clock >= MAX_CLOCKS)
232 return 1;
233 if (posix_clocks[which_clock].clock_getres != NULL)
234 return 0;
235 #ifndef CLOCK_DISPATCH_DIRECT
236 if (posix_clocks[which_clock].res != 0)
237 return 0;
238 #endif
239 return 1;
244 * Initialize everything, well, just everything in Posix clocks/timers ;)
246 static __init int init_posix_timers(void)
248 struct k_clock clock_realtime = {.res = CLOCK_REALTIME_RES,
249 .abs_struct = &abs_list
251 struct k_clock clock_monotonic = {.res = CLOCK_REALTIME_RES,
252 .abs_struct = NULL,
253 .clock_get = do_posix_clock_monotonic_get,
254 .clock_set = do_posix_clock_nosettime
257 register_posix_clock(CLOCK_REALTIME, &clock_realtime);
258 register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
260 posix_timers_cache = kmem_cache_create("posix_timers_cache",
261 sizeof (struct k_itimer), 0, 0, NULL, NULL);
262 idr_init(&posix_timers_id);
263 return 0;
266 __initcall(init_posix_timers);
268 static void tstojiffie(struct timespec *tp, int res, u64 *jiff)
270 long sec = tp->tv_sec;
271 long nsec = tp->tv_nsec + res - 1;
273 if (nsec > NSEC_PER_SEC) {
274 sec++;
275 nsec -= NSEC_PER_SEC;
279 * The scaling constants are defined in <linux/time.h>
280 * The difference between there and here is that we do the
281 * res rounding and compute a 64-bit result (well so does that
282 * but it then throws away the high bits).
284 *jiff = (mpy_l_X_l_ll(sec, SEC_CONVERSION) +
285 (mpy_l_X_l_ll(nsec, NSEC_CONVERSION) >>
286 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
290 * This function adjusts the timer as needed as a result of the clock
291 * being set. It should only be called for absolute timers, and then
292 * under the abs_list lock. It computes the time difference and sets
293 * the new jiffies value in the timer. It also updates the timers
294 * reference wall_to_monotonic value. It is complicated by the fact
295 * that tstojiffies() only handles positive times and it needs to work
296 * with both positive and negative times. Also, for negative offsets,
297 * we need to defeat the res round up.
299 * Return is true if there is a new time, else false.
301 static long add_clockset_delta(struct k_itimer *timr,
302 struct timespec *new_wall_to)
304 struct timespec delta;
305 int sign = 0;
306 u64 exp;
308 set_normalized_timespec(&delta,
309 new_wall_to->tv_sec -
310 timr->it.real.wall_to_prev.tv_sec,
311 new_wall_to->tv_nsec -
312 timr->it.real.wall_to_prev.tv_nsec);
313 if (likely(!(delta.tv_sec | delta.tv_nsec)))
314 return 0;
315 if (delta.tv_sec < 0) {
316 set_normalized_timespec(&delta,
317 -delta.tv_sec,
318 1 - delta.tv_nsec -
319 posix_clocks[timr->it_clock].res);
320 sign++;
322 tstojiffie(&delta, posix_clocks[timr->it_clock].res, &exp);
323 timr->it.real.wall_to_prev = *new_wall_to;
324 timr->it.real.timer.expires += (sign ? -exp : exp);
325 return 1;
328 static void remove_from_abslist(struct k_itimer *timr)
330 if (!list_empty(&timr->it.real.abs_timer_entry)) {
331 spin_lock(&abs_list.lock);
332 list_del_init(&timr->it.real.abs_timer_entry);
333 spin_unlock(&abs_list.lock);
337 static void schedule_next_timer(struct k_itimer *timr)
339 struct timespec new_wall_to;
340 struct now_struct now;
341 unsigned long seq;
344 * Set up the timer for the next interval (if there is one).
345 * Note: this code uses the abs_timer_lock to protect
346 * it.real.wall_to_prev and must hold it until exp is set, not exactly
347 * obvious...
349 * This function is used for CLOCK_REALTIME* and
350 * CLOCK_MONOTONIC* timers. If we ever want to handle other
351 * CLOCKs, the calling code (do_schedule_next_timer) would need
352 * to pull the "clock" info from the timer and dispatch the
353 * "other" CLOCKs "next timer" code (which, I suppose should
354 * also be added to the k_clock structure).
356 if (!timr->it.real.incr)
357 return;
359 do {
360 seq = read_seqbegin(&xtime_lock);
361 new_wall_to = wall_to_monotonic;
362 posix_get_now(&now);
363 } while (read_seqretry(&xtime_lock, seq));
365 if (!list_empty(&timr->it.real.abs_timer_entry)) {
366 spin_lock(&abs_list.lock);
367 add_clockset_delta(timr, &new_wall_to);
369 posix_bump_timer(timr, now);
371 spin_unlock(&abs_list.lock);
372 } else {
373 posix_bump_timer(timr, now);
375 timr->it_overrun_last = timr->it_overrun;
376 timr->it_overrun = -1;
377 ++timr->it_requeue_pending;
378 add_timer(&timr->it.real.timer);
382 * This function is exported for use by the signal deliver code. It is
383 * called just prior to the info block being released and passes that
384 * block to us. It's function is to update the overrun entry AND to
385 * restart the timer. It should only be called if the timer is to be
386 * restarted (i.e. we have flagged this in the sys_private entry of the
387 * info block).
389 * To protect aginst the timer going away while the interrupt is queued,
390 * we require that the it_requeue_pending flag be set.
392 void do_schedule_next_timer(struct siginfo *info)
394 struct k_itimer *timr;
395 unsigned long flags;
397 timr = lock_timer(info->si_tid, &flags);
399 if (!timr || timr->it_requeue_pending != info->si_sys_private)
400 goto exit;
402 if (timr->it_clock < 0) /* CPU clock */
403 posix_cpu_timer_schedule(timr);
404 else
405 schedule_next_timer(timr);
406 info->si_overrun = timr->it_overrun_last;
407 exit:
408 if (timr)
409 unlock_timer(timr, flags);
412 int posix_timer_event(struct k_itimer *timr,int si_private)
414 memset(&timr->sigq->info, 0, sizeof(siginfo_t));
415 timr->sigq->info.si_sys_private = si_private;
417 * Send signal to the process that owns this timer.
419 * This code assumes that all the possible abs_lists share the
420 * same lock (there is only one list at this time). If this is
421 * not the case, the CLOCK info would need to be used to find
422 * the proper abs list lock.
425 timr->sigq->info.si_signo = timr->it_sigev_signo;
426 timr->sigq->info.si_errno = 0;
427 timr->sigq->info.si_code = SI_TIMER;
428 timr->sigq->info.si_tid = timr->it_id;
429 timr->sigq->info.si_value = timr->it_sigev_value;
431 if (timr->it_sigev_notify & SIGEV_THREAD_ID) {
432 struct task_struct *leader;
433 int ret = send_sigqueue(timr->it_sigev_signo, timr->sigq,
434 timr->it_process);
436 if (likely(ret >= 0))
437 return ret;
439 timr->it_sigev_notify = SIGEV_SIGNAL;
440 leader = timr->it_process->group_leader;
441 put_task_struct(timr->it_process);
442 timr->it_process = leader;
445 return send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
446 timr->it_process);
448 EXPORT_SYMBOL_GPL(posix_timer_event);
451 * This function gets called when a POSIX.1b interval timer expires. It
452 * is used as a callback from the kernel internal timer. The
453 * run_timer_list code ALWAYS calls with interrupts on.
455 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
457 static void posix_timer_fn(unsigned long __data)
459 struct k_itimer *timr = (struct k_itimer *) __data;
460 unsigned long flags;
461 unsigned long seq;
462 struct timespec delta, new_wall_to;
463 u64 exp = 0;
464 int do_notify = 1;
466 spin_lock_irqsave(&timr->it_lock, flags);
467 if (!list_empty(&timr->it.real.abs_timer_entry)) {
468 spin_lock(&abs_list.lock);
469 do {
470 seq = read_seqbegin(&xtime_lock);
471 new_wall_to = wall_to_monotonic;
472 } while (read_seqretry(&xtime_lock, seq));
473 set_normalized_timespec(&delta,
474 new_wall_to.tv_sec -
475 timr->it.real.wall_to_prev.tv_sec,
476 new_wall_to.tv_nsec -
477 timr->it.real.wall_to_prev.tv_nsec);
478 if (likely((delta.tv_sec | delta.tv_nsec ) == 0)) {
479 /* do nothing, timer is on time */
480 } else if (delta.tv_sec < 0) {
481 /* do nothing, timer is already late */
482 } else {
483 /* timer is early due to a clock set */
484 tstojiffie(&delta,
485 posix_clocks[timr->it_clock].res,
486 &exp);
487 timr->it.real.wall_to_prev = new_wall_to;
488 timr->it.real.timer.expires += exp;
489 add_timer(&timr->it.real.timer);
490 do_notify = 0;
492 spin_unlock(&abs_list.lock);
495 if (do_notify) {
496 int si_private=0;
498 if (timr->it.real.incr)
499 si_private = ++timr->it_requeue_pending;
500 else {
501 remove_from_abslist(timr);
504 if (posix_timer_event(timr, si_private))
506 * signal was not sent because of sig_ignor
507 * we will not get a call back to restart it AND
508 * it should be restarted.
510 schedule_next_timer(timr);
512 unlock_timer(timr, flags); /* hold thru abs lock to keep irq off */
516 static inline struct task_struct * good_sigevent(sigevent_t * event)
518 struct task_struct *rtn = current->group_leader;
520 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
521 (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
522 rtn->tgid != current->tgid ||
523 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
524 return NULL;
526 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
527 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
528 return NULL;
530 return rtn;
533 void register_posix_clock(clockid_t clock_id, struct k_clock *new_clock)
535 if ((unsigned) clock_id >= MAX_CLOCKS) {
536 printk("POSIX clock register failed for clock_id %d\n",
537 clock_id);
538 return;
541 posix_clocks[clock_id] = *new_clock;
543 EXPORT_SYMBOL_GPL(register_posix_clock);
545 static struct k_itimer * alloc_posix_timer(void)
547 struct k_itimer *tmr;
548 tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
549 if (!tmr)
550 return tmr;
551 memset(tmr, 0, sizeof (struct k_itimer));
552 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
553 kmem_cache_free(posix_timers_cache, tmr);
554 tmr = NULL;
556 return tmr;
559 #define IT_ID_SET 1
560 #define IT_ID_NOT_SET 0
561 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
563 if (it_id_set) {
564 unsigned long flags;
565 spin_lock_irqsave(&idr_lock, flags);
566 idr_remove(&posix_timers_id, tmr->it_id);
567 spin_unlock_irqrestore(&idr_lock, flags);
569 sigqueue_free(tmr->sigq);
570 if (unlikely(tmr->it_process) &&
571 tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
572 put_task_struct(tmr->it_process);
573 kmem_cache_free(posix_timers_cache, tmr);
576 /* Create a POSIX.1b interval timer. */
578 asmlinkage long
579 sys_timer_create(clockid_t which_clock,
580 struct sigevent __user *timer_event_spec,
581 timer_t __user * created_timer_id)
583 int error = 0;
584 struct k_itimer *new_timer = NULL;
585 int new_timer_id;
586 struct task_struct *process = NULL;
587 unsigned long flags;
588 sigevent_t event;
589 int it_id_set = IT_ID_NOT_SET;
591 if (invalid_clockid(which_clock))
592 return -EINVAL;
594 new_timer = alloc_posix_timer();
595 if (unlikely(!new_timer))
596 return -EAGAIN;
598 spin_lock_init(&new_timer->it_lock);
599 retry:
600 if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
601 error = -EAGAIN;
602 goto out;
604 spin_lock_irq(&idr_lock);
605 error = idr_get_new(&posix_timers_id,
606 (void *) new_timer,
607 &new_timer_id);
608 spin_unlock_irq(&idr_lock);
609 if (error == -EAGAIN)
610 goto retry;
611 else if (error) {
613 * Wierd looking, but we return EAGAIN if the IDR is
614 * full (proper POSIX return value for this)
616 error = -EAGAIN;
617 goto out;
620 it_id_set = IT_ID_SET;
621 new_timer->it_id = (timer_t) new_timer_id;
622 new_timer->it_clock = which_clock;
623 new_timer->it_overrun = -1;
624 error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer));
625 if (error)
626 goto out;
629 * return the timer_id now. The next step is hard to
630 * back out if there is an error.
632 if (copy_to_user(created_timer_id,
633 &new_timer_id, sizeof (new_timer_id))) {
634 error = -EFAULT;
635 goto out;
637 if (timer_event_spec) {
638 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
639 error = -EFAULT;
640 goto out;
642 new_timer->it_sigev_notify = event.sigev_notify;
643 new_timer->it_sigev_signo = event.sigev_signo;
644 new_timer->it_sigev_value = event.sigev_value;
646 read_lock(&tasklist_lock);
647 if ((process = good_sigevent(&event))) {
649 * We may be setting up this process for another
650 * thread. It may be exiting. To catch this
651 * case the we check the PF_EXITING flag. If
652 * the flag is not set, the siglock will catch
653 * him before it is too late (in exit_itimers).
655 * The exec case is a bit more invloved but easy
656 * to code. If the process is in our thread
657 * group (and it must be or we would not allow
658 * it here) and is doing an exec, it will cause
659 * us to be killed. In this case it will wait
660 * for us to die which means we can finish this
661 * linkage with our last gasp. I.e. no code :)
663 spin_lock_irqsave(&process->sighand->siglock, flags);
664 if (!(process->flags & PF_EXITING)) {
665 new_timer->it_process = process;
666 list_add(&new_timer->list,
667 &process->signal->posix_timers);
668 spin_unlock_irqrestore(&process->sighand->siglock, flags);
669 if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
670 get_task_struct(process);
671 } else {
672 spin_unlock_irqrestore(&process->sighand->siglock, flags);
673 process = NULL;
676 read_unlock(&tasklist_lock);
677 if (!process) {
678 error = -EINVAL;
679 goto out;
681 } else {
682 new_timer->it_sigev_notify = SIGEV_SIGNAL;
683 new_timer->it_sigev_signo = SIGALRM;
684 new_timer->it_sigev_value.sival_int = new_timer->it_id;
685 process = current->group_leader;
686 spin_lock_irqsave(&process->sighand->siglock, flags);
687 new_timer->it_process = process;
688 list_add(&new_timer->list, &process->signal->posix_timers);
689 spin_unlock_irqrestore(&process->sighand->siglock, flags);
693 * In the case of the timer belonging to another task, after
694 * the task is unlocked, the timer is owned by the other task
695 * and may cease to exist at any time. Don't use or modify
696 * new_timer after the unlock call.
699 out:
700 if (error)
701 release_posix_timer(new_timer, it_id_set);
703 return error;
707 * good_timespec
709 * This function checks the elements of a timespec structure.
711 * Arguments:
712 * ts : Pointer to the timespec structure to check
714 * Return value:
715 * If a NULL pointer was passed in, or the tv_nsec field was less than 0
716 * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0,
717 * this function returns 0. Otherwise it returns 1.
719 static int good_timespec(const struct timespec *ts)
721 if ((!ts) || (ts->tv_sec < 0) ||
722 ((unsigned) ts->tv_nsec >= NSEC_PER_SEC))
723 return 0;
724 return 1;
728 * Locking issues: We need to protect the result of the id look up until
729 * we get the timer locked down so it is not deleted under us. The
730 * removal is done under the idr spinlock so we use that here to bridge
731 * the find to the timer lock. To avoid a dead lock, the timer id MUST
732 * be release with out holding the timer lock.
734 static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
736 struct k_itimer *timr;
738 * Watch out here. We do a irqsave on the idr_lock and pass the
739 * flags part over to the timer lock. Must not let interrupts in
740 * while we are moving the lock.
743 spin_lock_irqsave(&idr_lock, *flags);
744 timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
745 if (timr) {
746 spin_lock(&timr->it_lock);
747 spin_unlock(&idr_lock);
749 if ((timr->it_id != timer_id) || !(timr->it_process) ||
750 timr->it_process->tgid != current->tgid) {
751 unlock_timer(timr, *flags);
752 timr = NULL;
754 } else
755 spin_unlock_irqrestore(&idr_lock, *flags);
757 return timr;
761 * Get the time remaining on a POSIX.1b interval timer. This function
762 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
763 * mess with irq.
765 * We have a couple of messes to clean up here. First there is the case
766 * of a timer that has a requeue pending. These timers should appear to
767 * be in the timer list with an expiry as if we were to requeue them
768 * now.
770 * The second issue is the SIGEV_NONE timer which may be active but is
771 * not really ever put in the timer list (to save system resources).
772 * This timer may be expired, and if so, we will do it here. Otherwise
773 * it is the same as a requeue pending timer WRT to what we should
774 * report.
776 static void
777 common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
779 unsigned long expires;
780 struct now_struct now;
783 expires = timr->it.real.timer.expires;
784 while ((volatile long) (timr->it.real.timer.expires) != expires);
786 posix_get_now(&now);
788 if (expires &&
789 ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) &&
790 !timr->it.real.incr &&
791 posix_time_before(&timr->it.real.timer, &now))
792 timr->it.real.timer.expires = expires = 0;
793 if (expires) {
794 if (timr->it_requeue_pending & REQUEUE_PENDING ||
795 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
796 posix_bump_timer(timr, now);
797 expires = timr->it.real.timer.expires;
799 else
800 if (!timer_pending(&timr->it.real.timer))
801 expires = 0;
802 if (expires)
803 expires -= now.jiffies;
805 jiffies_to_timespec(expires, &cur_setting->it_value);
806 jiffies_to_timespec(timr->it.real.incr, &cur_setting->it_interval);
808 if (cur_setting->it_value.tv_sec < 0) {
809 cur_setting->it_value.tv_nsec = 1;
810 cur_setting->it_value.tv_sec = 0;
814 /* Get the time remaining on a POSIX.1b interval timer. */
815 asmlinkage long
816 sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
818 struct k_itimer *timr;
819 struct itimerspec cur_setting;
820 unsigned long flags;
822 timr = lock_timer(timer_id, &flags);
823 if (!timr)
824 return -EINVAL;
826 CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting));
828 unlock_timer(timr, flags);
830 if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
831 return -EFAULT;
833 return 0;
836 * Get the number of overruns of a POSIX.1b interval timer. This is to
837 * be the overrun of the timer last delivered. At the same time we are
838 * accumulating overruns on the next timer. The overrun is frozen when
839 * the signal is delivered, either at the notify time (if the info block
840 * is not queued) or at the actual delivery time (as we are informed by
841 * the call back to do_schedule_next_timer(). So all we need to do is
842 * to pick up the frozen overrun.
845 asmlinkage long
846 sys_timer_getoverrun(timer_t timer_id)
848 struct k_itimer *timr;
849 int overrun;
850 long flags;
852 timr = lock_timer(timer_id, &flags);
853 if (!timr)
854 return -EINVAL;
856 overrun = timr->it_overrun_last;
857 unlock_timer(timr, flags);
859 return overrun;
862 * Adjust for absolute time
864 * If absolute time is given and it is not CLOCK_MONOTONIC, we need to
865 * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and
866 * what ever clock he is using.
868 * If it is relative time, we need to add the current (CLOCK_MONOTONIC)
869 * time to it to get the proper time for the timer.
871 static int adjust_abs_time(struct k_clock *clock, struct timespec *tp,
872 int abs, u64 *exp, struct timespec *wall_to)
874 struct timespec now;
875 struct timespec oc = *tp;
876 u64 jiffies_64_f;
877 int rtn =0;
879 if (abs) {
881 * The mask pick up the 4 basic clocks
883 if (!((clock - &posix_clocks[0]) & ~CLOCKS_MASK)) {
884 jiffies_64_f = do_posix_clock_monotonic_gettime_parts(
885 &now, wall_to);
887 * If we are doing a MONOTONIC clock
889 if((clock - &posix_clocks[0]) & CLOCKS_MONO){
890 now.tv_sec += wall_to->tv_sec;
891 now.tv_nsec += wall_to->tv_nsec;
893 } else {
895 * Not one of the basic clocks
897 clock->clock_get(clock - posix_clocks, &now);
898 jiffies_64_f = get_jiffies_64();
901 * Take away now to get delta and normalize
903 set_normalized_timespec(&oc, oc.tv_sec - now.tv_sec,
904 oc.tv_nsec - now.tv_nsec);
905 }else{
906 jiffies_64_f = get_jiffies_64();
909 * Check if the requested time is prior to now (if so set now)
911 if (oc.tv_sec < 0)
912 oc.tv_sec = oc.tv_nsec = 0;
914 if (oc.tv_sec | oc.tv_nsec)
915 set_normalized_timespec(&oc, oc.tv_sec,
916 oc.tv_nsec + clock->res);
917 tstojiffie(&oc, clock->res, exp);
920 * Check if the requested time is more than the timer code
921 * can handle (if so we error out but return the value too).
923 if (*exp > ((u64)MAX_JIFFY_OFFSET))
925 * This is a considered response, not exactly in
926 * line with the standard (in fact it is silent on
927 * possible overflows). We assume such a large
928 * value is ALMOST always a programming error and
929 * try not to compound it by setting a really dumb
930 * value.
932 rtn = -EINVAL;
934 * return the actual jiffies expire time, full 64 bits
936 *exp += jiffies_64_f;
937 return rtn;
940 /* Set a POSIX.1b interval timer. */
941 /* timr->it_lock is taken. */
942 static inline int
943 common_timer_set(struct k_itimer *timr, int flags,
944 struct itimerspec *new_setting, struct itimerspec *old_setting)
946 struct k_clock *clock = &posix_clocks[timr->it_clock];
947 u64 expire_64;
949 if (old_setting)
950 common_timer_get(timr, old_setting);
952 /* disable the timer */
953 timr->it.real.incr = 0;
955 * careful here. If smp we could be in the "fire" routine which will
956 * be spinning as we hold the lock. But this is ONLY an SMP issue.
958 if (try_to_del_timer_sync(&timr->it.real.timer) < 0) {
959 #ifdef CONFIG_SMP
961 * It can only be active if on an other cpu. Since
962 * we have cleared the interval stuff above, it should
963 * clear once we release the spin lock. Of course once
964 * we do that anything could happen, including the
965 * complete melt down of the timer. So return with
966 * a "retry" exit status.
968 return TIMER_RETRY;
969 #endif
972 remove_from_abslist(timr);
974 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
975 ~REQUEUE_PENDING;
976 timr->it_overrun_last = 0;
977 timr->it_overrun = -1;
979 *switch off the timer when it_value is zero
981 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) {
982 timr->it.real.timer.expires = 0;
983 return 0;
986 if (adjust_abs_time(clock,
987 &new_setting->it_value, flags & TIMER_ABSTIME,
988 &expire_64, &(timr->it.real.wall_to_prev))) {
989 return -EINVAL;
991 timr->it.real.timer.expires = (unsigned long)expire_64;
992 tstojiffie(&new_setting->it_interval, clock->res, &expire_64);
993 timr->it.real.incr = (unsigned long)expire_64;
996 * We do not even queue SIGEV_NONE timers! But we do put them
997 * in the abs list so we can do that right.
999 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE))
1000 add_timer(&timr->it.real.timer);
1002 if (flags & TIMER_ABSTIME && clock->abs_struct) {
1003 spin_lock(&clock->abs_struct->lock);
1004 list_add_tail(&(timr->it.real.abs_timer_entry),
1005 &(clock->abs_struct->list));
1006 spin_unlock(&clock->abs_struct->lock);
1008 return 0;
1011 /* Set a POSIX.1b interval timer */
1012 asmlinkage long
1013 sys_timer_settime(timer_t timer_id, int flags,
1014 const struct itimerspec __user *new_setting,
1015 struct itimerspec __user *old_setting)
1017 struct k_itimer *timr;
1018 struct itimerspec new_spec, old_spec;
1019 int error = 0;
1020 long flag;
1021 struct itimerspec *rtn = old_setting ? &old_spec : NULL;
1023 if (!new_setting)
1024 return -EINVAL;
1026 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
1027 return -EFAULT;
1029 if ((!good_timespec(&new_spec.it_interval)) ||
1030 (!good_timespec(&new_spec.it_value)))
1031 return -EINVAL;
1032 retry:
1033 timr = lock_timer(timer_id, &flag);
1034 if (!timr)
1035 return -EINVAL;
1037 error = CLOCK_DISPATCH(timr->it_clock, timer_set,
1038 (timr, flags, &new_spec, rtn));
1040 unlock_timer(timr, flag);
1041 if (error == TIMER_RETRY) {
1042 rtn = NULL; // We already got the old time...
1043 goto retry;
1046 if (old_setting && !error && copy_to_user(old_setting,
1047 &old_spec, sizeof (old_spec)))
1048 error = -EFAULT;
1050 return error;
1053 static inline int common_timer_del(struct k_itimer *timer)
1055 timer->it.real.incr = 0;
1057 if (try_to_del_timer_sync(&timer->it.real.timer) < 0) {
1058 #ifdef CONFIG_SMP
1060 * It can only be active if on an other cpu. Since
1061 * we have cleared the interval stuff above, it should
1062 * clear once we release the spin lock. Of course once
1063 * we do that anything could happen, including the
1064 * complete melt down of the timer. So return with
1065 * a "retry" exit status.
1067 return TIMER_RETRY;
1068 #endif
1071 remove_from_abslist(timer);
1073 return 0;
1076 static inline int timer_delete_hook(struct k_itimer *timer)
1078 return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer));
1081 /* Delete a POSIX.1b interval timer. */
1082 asmlinkage long
1083 sys_timer_delete(timer_t timer_id)
1085 struct k_itimer *timer;
1086 long flags;
1088 #ifdef CONFIG_SMP
1089 int error;
1090 retry_delete:
1091 #endif
1092 timer = lock_timer(timer_id, &flags);
1093 if (!timer)
1094 return -EINVAL;
1096 #ifdef CONFIG_SMP
1097 error = timer_delete_hook(timer);
1099 if (error == TIMER_RETRY) {
1100 unlock_timer(timer, flags);
1101 goto retry_delete;
1103 #else
1104 timer_delete_hook(timer);
1105 #endif
1106 spin_lock(&current->sighand->siglock);
1107 list_del(&timer->list);
1108 spin_unlock(&current->sighand->siglock);
1110 * This keeps any tasks waiting on the spin lock from thinking
1111 * they got something (see the lock code above).
1113 if (timer->it_process) {
1114 if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
1115 put_task_struct(timer->it_process);
1116 timer->it_process = NULL;
1118 unlock_timer(timer, flags);
1119 release_posix_timer(timer, IT_ID_SET);
1120 return 0;
1123 * return timer owned by the process, used by exit_itimers
1125 static inline void itimer_delete(struct k_itimer *timer)
1127 unsigned long flags;
1129 #ifdef CONFIG_SMP
1130 int error;
1131 retry_delete:
1132 #endif
1133 spin_lock_irqsave(&timer->it_lock, flags);
1135 #ifdef CONFIG_SMP
1136 error = timer_delete_hook(timer);
1138 if (error == TIMER_RETRY) {
1139 unlock_timer(timer, flags);
1140 goto retry_delete;
1142 #else
1143 timer_delete_hook(timer);
1144 #endif
1145 list_del(&timer->list);
1147 * This keeps any tasks waiting on the spin lock from thinking
1148 * they got something (see the lock code above).
1150 if (timer->it_process) {
1151 if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
1152 put_task_struct(timer->it_process);
1153 timer->it_process = NULL;
1155 unlock_timer(timer, flags);
1156 release_posix_timer(timer, IT_ID_SET);
1160 * This is called by do_exit or de_thread, only when there are no more
1161 * references to the shared signal_struct.
1163 void exit_itimers(struct signal_struct *sig)
1165 struct k_itimer *tmr;
1167 while (!list_empty(&sig->posix_timers)) {
1168 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1169 itimer_delete(tmr);
1174 * And now for the "clock" calls
1176 * These functions are called both from timer functions (with the timer
1177 * spin_lock_irq() held and from clock calls with no locking. They must
1178 * use the save flags versions of locks.
1182 * We do ticks here to avoid the irq lock ( they take sooo long).
1183 * The seqlock is great here. Since we a reader, we don't really care
1184 * if we are interrupted since we don't take lock that will stall us or
1185 * any other cpu. Voila, no irq lock is needed.
1189 static u64 do_posix_clock_monotonic_gettime_parts(
1190 struct timespec *tp, struct timespec *mo)
1192 u64 jiff;
1193 unsigned int seq;
1195 do {
1196 seq = read_seqbegin(&xtime_lock);
1197 getnstimeofday(tp);
1198 *mo = wall_to_monotonic;
1199 jiff = jiffies_64;
1201 } while(read_seqretry(&xtime_lock, seq));
1203 return jiff;
1206 static int do_posix_clock_monotonic_get(clockid_t clock, struct timespec *tp)
1208 struct timespec wall_to_mono;
1210 do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
1212 tp->tv_sec += wall_to_mono.tv_sec;
1213 tp->tv_nsec += wall_to_mono.tv_nsec;
1215 if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
1216 tp->tv_nsec -= NSEC_PER_SEC;
1217 tp->tv_sec++;
1219 return 0;
1222 int do_posix_clock_monotonic_gettime(struct timespec *tp)
1224 return do_posix_clock_monotonic_get(CLOCK_MONOTONIC, tp);
1227 int do_posix_clock_nosettime(clockid_t clockid, struct timespec *tp)
1229 return -EINVAL;
1231 EXPORT_SYMBOL_GPL(do_posix_clock_nosettime);
1233 int do_posix_clock_notimer_create(struct k_itimer *timer)
1235 return -EINVAL;
1237 EXPORT_SYMBOL_GPL(do_posix_clock_notimer_create);
1239 int do_posix_clock_nonanosleep(clockid_t clock, int flags, struct timespec *t)
1241 #ifndef ENOTSUP
1242 return -EOPNOTSUPP; /* aka ENOTSUP in userland for POSIX */
1243 #else /* parisc does define it separately. */
1244 return -ENOTSUP;
1245 #endif
1247 EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep);
1249 asmlinkage long
1250 sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
1252 struct timespec new_tp;
1254 if (invalid_clockid(which_clock))
1255 return -EINVAL;
1256 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1257 return -EFAULT;
1259 return CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp));
1262 asmlinkage long
1263 sys_clock_gettime(clockid_t which_clock, struct timespec __user *tp)
1265 struct timespec kernel_tp;
1266 int error;
1268 if (invalid_clockid(which_clock))
1269 return -EINVAL;
1270 error = CLOCK_DISPATCH(which_clock, clock_get,
1271 (which_clock, &kernel_tp));
1272 if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
1273 error = -EFAULT;
1275 return error;
1279 asmlinkage long
1280 sys_clock_getres(clockid_t which_clock, struct timespec __user *tp)
1282 struct timespec rtn_tp;
1283 int error;
1285 if (invalid_clockid(which_clock))
1286 return -EINVAL;
1288 error = CLOCK_DISPATCH(which_clock, clock_getres,
1289 (which_clock, &rtn_tp));
1291 if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) {
1292 error = -EFAULT;
1295 return error;
1299 * The standard says that an absolute nanosleep call MUST wake up at
1300 * the requested time in spite of clock settings. Here is what we do:
1301 * For each nanosleep call that needs it (only absolute and not on
1302 * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure
1303 * into the "nanosleep_abs_list". All we need is the task_struct pointer.
1304 * When ever the clock is set we just wake up all those tasks. The rest
1305 * is done by the while loop in clock_nanosleep().
1307 * On locking, clock_was_set() is called from update_wall_clock which
1308 * holds (or has held for it) a write_lock_irq( xtime_lock) and is
1309 * called from the timer bh code. Thus we need the irq save locks.
1311 * Also, on the call from update_wall_clock, that is done as part of a
1312 * softirq thing. We don't want to delay the system that much (possibly
1313 * long list of timers to fix), so we defer that work to keventd.
1316 static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);
1317 static DECLARE_WORK(clock_was_set_work, (void(*)(void*))clock_was_set, NULL);
1319 static DECLARE_MUTEX(clock_was_set_lock);
1321 void clock_was_set(void)
1323 struct k_itimer *timr;
1324 struct timespec new_wall_to;
1325 LIST_HEAD(cws_list);
1326 unsigned long seq;
1329 if (unlikely(in_interrupt())) {
1330 schedule_work(&clock_was_set_work);
1331 return;
1333 wake_up_all(&nanosleep_abs_wqueue);
1336 * Check if there exist TIMER_ABSTIME timers to correct.
1338 * Notes on locking: This code is run in task context with irq
1339 * on. We CAN be interrupted! All other usage of the abs list
1340 * lock is under the timer lock which holds the irq lock as
1341 * well. We REALLY don't want to scan the whole list with the
1342 * interrupt system off, AND we would like a sequence lock on
1343 * this code as well. Since we assume that the clock will not
1344 * be set often, it seems ok to take and release the irq lock
1345 * for each timer. In fact add_timer will do this, so this is
1346 * not an issue. So we know when we are done, we will move the
1347 * whole list to a new location. Then as we process each entry,
1348 * we will move it to the actual list again. This way, when our
1349 * copy is empty, we are done. We are not all that concerned
1350 * about preemption so we will use a semaphore lock to protect
1351 * aginst reentry. This way we will not stall another
1352 * processor. It is possible that this may delay some timers
1353 * that should have expired, given the new clock, but even this
1354 * will be minimal as we will always update to the current time,
1355 * even if it was set by a task that is waiting for entry to
1356 * this code. Timers that expire too early will be caught by
1357 * the expire code and restarted.
1359 * Absolute timers that repeat are left in the abs list while
1360 * waiting for the task to pick up the signal. This means we
1361 * may find timers that are not in the "add_timer" list, but are
1362 * in the abs list. We do the same thing for these, save
1363 * putting them back in the "add_timer" list. (Note, these are
1364 * left in the abs list mainly to indicate that they are
1365 * ABSOLUTE timers, a fact that is used by the re-arm code, and
1366 * for which we have no other flag.)
1370 down(&clock_was_set_lock);
1371 spin_lock_irq(&abs_list.lock);
1372 list_splice_init(&abs_list.list, &cws_list);
1373 spin_unlock_irq(&abs_list.lock);
1374 do {
1375 do {
1376 seq = read_seqbegin(&xtime_lock);
1377 new_wall_to = wall_to_monotonic;
1378 } while (read_seqretry(&xtime_lock, seq));
1380 spin_lock_irq(&abs_list.lock);
1381 if (list_empty(&cws_list)) {
1382 spin_unlock_irq(&abs_list.lock);
1383 break;
1385 timr = list_entry(cws_list.next, struct k_itimer,
1386 it.real.abs_timer_entry);
1388 list_del_init(&timr->it.real.abs_timer_entry);
1389 if (add_clockset_delta(timr, &new_wall_to) &&
1390 del_timer(&timr->it.real.timer)) /* timer run yet? */
1391 add_timer(&timr->it.real.timer);
1392 list_add(&timr->it.real.abs_timer_entry, &abs_list.list);
1393 spin_unlock_irq(&abs_list.lock);
1394 } while (1);
1396 up(&clock_was_set_lock);
1399 long clock_nanosleep_restart(struct restart_block *restart_block);
1401 asmlinkage long
1402 sys_clock_nanosleep(clockid_t which_clock, int flags,
1403 const struct timespec __user *rqtp,
1404 struct timespec __user *rmtp)
1406 struct timespec t;
1407 struct restart_block *restart_block =
1408 &(current_thread_info()->restart_block);
1409 int ret;
1411 if (invalid_clockid(which_clock))
1412 return -EINVAL;
1414 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1415 return -EFAULT;
1417 if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
1418 return -EINVAL;
1421 * Do this here as nsleep function does not have the real address.
1423 restart_block->arg1 = (unsigned long)rmtp;
1425 ret = CLOCK_DISPATCH(which_clock, nsleep, (which_clock, flags, &t));
1427 if ((ret == -ERESTART_RESTARTBLOCK) && rmtp &&
1428 copy_to_user(rmtp, &t, sizeof (t)))
1429 return -EFAULT;
1430 return ret;
1434 static int common_nsleep(clockid_t which_clock,
1435 int flags, struct timespec *tsave)
1437 struct timespec t, dum;
1438 DECLARE_WAITQUEUE(abs_wqueue, current);
1439 u64 rq_time = (u64)0;
1440 s64 left;
1441 int abs;
1442 struct restart_block *restart_block =
1443 &current_thread_info()->restart_block;
1445 abs_wqueue.flags = 0;
1446 abs = flags & TIMER_ABSTIME;
1448 if (restart_block->fn == clock_nanosleep_restart) {
1450 * Interrupted by a non-delivered signal, pick up remaining
1451 * time and continue. Remaining time is in arg2 & 3.
1453 restart_block->fn = do_no_restart_syscall;
1455 rq_time = restart_block->arg3;
1456 rq_time = (rq_time << 32) + restart_block->arg2;
1457 if (!rq_time)
1458 return -EINTR;
1459 left = rq_time - get_jiffies_64();
1460 if (left <= (s64)0)
1461 return 0; /* Already passed */
1464 if (abs && (posix_clocks[which_clock].clock_get !=
1465 posix_clocks[CLOCK_MONOTONIC].clock_get))
1466 add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue);
1468 do {
1469 t = *tsave;
1470 if (abs || !rq_time) {
1471 adjust_abs_time(&posix_clocks[which_clock], &t, abs,
1472 &rq_time, &dum);
1475 left = rq_time - get_jiffies_64();
1476 if (left >= (s64)MAX_JIFFY_OFFSET)
1477 left = (s64)MAX_JIFFY_OFFSET;
1478 if (left < (s64)0)
1479 break;
1481 schedule_timeout_interruptible(left);
1483 left = rq_time - get_jiffies_64();
1484 } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING));
1486 if (abs_wqueue.task_list.next)
1487 finish_wait(&nanosleep_abs_wqueue, &abs_wqueue);
1489 if (left > (s64)0) {
1492 * Always restart abs calls from scratch to pick up any
1493 * clock shifting that happened while we are away.
1495 if (abs)
1496 return -ERESTARTNOHAND;
1498 left *= TICK_NSEC;
1499 tsave->tv_sec = div_long_long_rem(left,
1500 NSEC_PER_SEC,
1501 &tsave->tv_nsec);
1503 * Restart works by saving the time remaing in
1504 * arg2 & 3 (it is 64-bits of jiffies). The other
1505 * info we need is the clock_id (saved in arg0).
1506 * The sys_call interface needs the users
1507 * timespec return address which _it_ saves in arg1.
1508 * Since we have cast the nanosleep call to a clock_nanosleep
1509 * both can be restarted with the same code.
1511 restart_block->fn = clock_nanosleep_restart;
1512 restart_block->arg0 = which_clock;
1514 * Caller sets arg1
1516 restart_block->arg2 = rq_time & 0xffffffffLL;
1517 restart_block->arg3 = rq_time >> 32;
1519 return -ERESTART_RESTARTBLOCK;
1522 return 0;
1525 * This will restart clock_nanosleep.
1527 long
1528 clock_nanosleep_restart(struct restart_block *restart_block)
1530 struct timespec t;
1531 int ret = common_nsleep(restart_block->arg0, 0, &t);
1533 if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 &&
1534 copy_to_user((struct timespec __user *)(restart_block->arg1), &t,
1535 sizeof (t)))
1536 return -EFAULT;
1537 return ret;