MIPS: Alchemy: remove get/set_au1x00_lcd_clock().
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / posix-cpu-timers.c
blob157de3a478321ab80c8c05cee5ffd07da68bab39
1 /*
2 * Implement CPU time clocks for the POSIX clock interface.
3 */
5 #include <linux/sched.h>
6 #include <linux/posix-timers.h>
7 #include <linux/errno.h>
8 #include <linux/math64.h>
9 #include <asm/uaccess.h>
10 #include <linux/kernel_stat.h>
13 * Allocate the thread_group_cputime structure appropriately and fill in the
14 * current values of the fields. Called from copy_signal() via
15 * thread_group_cputime_clone_thread() when adding a second or subsequent
16 * thread to a thread group. Assumes interrupts are enabled when called.
18 int thread_group_cputime_alloc(struct task_struct *tsk)
20 struct signal_struct *sig = tsk->signal;
21 struct task_cputime *cputime;
24 * If we have multiple threads and we don't already have a
25 * per-CPU task_cputime struct (checked in the caller), allocate
26 * one and fill it in with the times accumulated so far. We may
27 * race with another thread so recheck after we pick up the sighand
28 * lock.
30 cputime = alloc_percpu(struct task_cputime);
31 if (cputime == NULL)
32 return -ENOMEM;
33 spin_lock_irq(&tsk->sighand->siglock);
34 if (sig->cputime.totals) {
35 spin_unlock_irq(&tsk->sighand->siglock);
36 free_percpu(cputime);
37 return 0;
39 sig->cputime.totals = cputime;
40 cputime = per_cpu_ptr(sig->cputime.totals, smp_processor_id());
41 cputime->utime = tsk->utime;
42 cputime->stime = tsk->stime;
43 cputime->sum_exec_runtime = tsk->se.sum_exec_runtime;
44 spin_unlock_irq(&tsk->sighand->siglock);
45 return 0;
48 /**
49 * thread_group_cputime - Sum the thread group time fields across all CPUs.
51 * @tsk: The task we use to identify the thread group.
52 * @times: task_cputime structure in which we return the summed fields.
54 * Walk the list of CPUs to sum the per-CPU time fields in the thread group
55 * time structure.
57 void thread_group_cputime(
58 struct task_struct *tsk,
59 struct task_cputime *times)
61 struct task_cputime *totals, *tot;
62 int i;
64 totals = tsk->signal->cputime.totals;
65 if (!totals) {
66 times->utime = tsk->utime;
67 times->stime = tsk->stime;
68 times->sum_exec_runtime = tsk->se.sum_exec_runtime;
69 return;
72 times->stime = times->utime = cputime_zero;
73 times->sum_exec_runtime = 0;
74 for_each_possible_cpu(i) {
75 tot = per_cpu_ptr(totals, i);
76 times->utime = cputime_add(times->utime, tot->utime);
77 times->stime = cputime_add(times->stime, tot->stime);
78 times->sum_exec_runtime += tot->sum_exec_runtime;
83 * Called after updating RLIMIT_CPU to set timer expiration if necessary.
85 void update_rlimit_cpu(unsigned long rlim_new)
87 cputime_t cputime;
89 cputime = secs_to_cputime(rlim_new);
90 if (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
91 cputime_lt(current->signal->it_prof_expires, cputime)) {
92 spin_lock_irq(&current->sighand->siglock);
93 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
94 spin_unlock_irq(&current->sighand->siglock);
98 static int check_clock(const clockid_t which_clock)
100 int error = 0;
101 struct task_struct *p;
102 const pid_t pid = CPUCLOCK_PID(which_clock);
104 if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
105 return -EINVAL;
107 if (pid == 0)
108 return 0;
110 read_lock(&tasklist_lock);
111 p = find_task_by_vpid(pid);
112 if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
113 same_thread_group(p, current) : thread_group_leader(p))) {
114 error = -EINVAL;
116 read_unlock(&tasklist_lock);
118 return error;
121 static inline union cpu_time_count
122 timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
124 union cpu_time_count ret;
125 ret.sched = 0; /* high half always zero when .cpu used */
126 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
127 ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
128 } else {
129 ret.cpu = timespec_to_cputime(tp);
131 return ret;
134 static void sample_to_timespec(const clockid_t which_clock,
135 union cpu_time_count cpu,
136 struct timespec *tp)
138 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED)
139 *tp = ns_to_timespec(cpu.sched);
140 else
141 cputime_to_timespec(cpu.cpu, tp);
144 static inline int cpu_time_before(const clockid_t which_clock,
145 union cpu_time_count now,
146 union cpu_time_count then)
148 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
149 return now.sched < then.sched;
150 } else {
151 return cputime_lt(now.cpu, then.cpu);
154 static inline void cpu_time_add(const clockid_t which_clock,
155 union cpu_time_count *acc,
156 union cpu_time_count val)
158 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
159 acc->sched += val.sched;
160 } else {
161 acc->cpu = cputime_add(acc->cpu, val.cpu);
164 static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock,
165 union cpu_time_count a,
166 union cpu_time_count b)
168 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
169 a.sched -= b.sched;
170 } else {
171 a.cpu = cputime_sub(a.cpu, b.cpu);
173 return a;
177 * Divide and limit the result to res >= 1
179 * This is necessary to prevent signal delivery starvation, when the result of
180 * the division would be rounded down to 0.
182 static inline cputime_t cputime_div_non_zero(cputime_t time, unsigned long div)
184 cputime_t res = cputime_div(time, div);
186 return max_t(cputime_t, res, 1);
190 * Update expiry time from increment, and increase overrun count,
191 * given the current clock sample.
193 static void bump_cpu_timer(struct k_itimer *timer,
194 union cpu_time_count now)
196 int i;
198 if (timer->it.cpu.incr.sched == 0)
199 return;
201 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
202 unsigned long long delta, incr;
204 if (now.sched < timer->it.cpu.expires.sched)
205 return;
206 incr = timer->it.cpu.incr.sched;
207 delta = now.sched + incr - timer->it.cpu.expires.sched;
208 /* Don't use (incr*2 < delta), incr*2 might overflow. */
209 for (i = 0; incr < delta - incr; i++)
210 incr = incr << 1;
211 for (; i >= 0; incr >>= 1, i--) {
212 if (delta < incr)
213 continue;
214 timer->it.cpu.expires.sched += incr;
215 timer->it_overrun += 1 << i;
216 delta -= incr;
218 } else {
219 cputime_t delta, incr;
221 if (cputime_lt(now.cpu, timer->it.cpu.expires.cpu))
222 return;
223 incr = timer->it.cpu.incr.cpu;
224 delta = cputime_sub(cputime_add(now.cpu, incr),
225 timer->it.cpu.expires.cpu);
226 /* Don't use (incr*2 < delta), incr*2 might overflow. */
227 for (i = 0; cputime_lt(incr, cputime_sub(delta, incr)); i++)
228 incr = cputime_add(incr, incr);
229 for (; i >= 0; incr = cputime_halve(incr), i--) {
230 if (cputime_lt(delta, incr))
231 continue;
232 timer->it.cpu.expires.cpu =
233 cputime_add(timer->it.cpu.expires.cpu, incr);
234 timer->it_overrun += 1 << i;
235 delta = cputime_sub(delta, incr);
240 static inline cputime_t prof_ticks(struct task_struct *p)
242 return cputime_add(p->utime, p->stime);
244 static inline cputime_t virt_ticks(struct task_struct *p)
246 return p->utime;
249 int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
251 int error = check_clock(which_clock);
252 if (!error) {
253 tp->tv_sec = 0;
254 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
255 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
257 * If sched_clock is using a cycle counter, we
258 * don't have any idea of its true resolution
259 * exported, but it is much more than 1s/HZ.
261 tp->tv_nsec = 1;
264 return error;
267 int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
270 * You can never reset a CPU clock, but we check for other errors
271 * in the call before failing with EPERM.
273 int error = check_clock(which_clock);
274 if (error == 0) {
275 error = -EPERM;
277 return error;
282 * Sample a per-thread clock for the given task.
284 static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
285 union cpu_time_count *cpu)
287 switch (CPUCLOCK_WHICH(which_clock)) {
288 default:
289 return -EINVAL;
290 case CPUCLOCK_PROF:
291 cpu->cpu = prof_ticks(p);
292 break;
293 case CPUCLOCK_VIRT:
294 cpu->cpu = virt_ticks(p);
295 break;
296 case CPUCLOCK_SCHED:
297 cpu->sched = p->se.sum_exec_runtime + task_delta_exec(p);
298 break;
300 return 0;
304 * Sample a process (thread group) clock for the given group_leader task.
305 * Must be called with tasklist_lock held for reading.
307 static int cpu_clock_sample_group(const clockid_t which_clock,
308 struct task_struct *p,
309 union cpu_time_count *cpu)
311 struct task_cputime cputime;
313 thread_group_cputime(p, &cputime);
314 switch (CPUCLOCK_WHICH(which_clock)) {
315 default:
316 return -EINVAL;
317 case CPUCLOCK_PROF:
318 cpu->cpu = cputime_add(cputime.utime, cputime.stime);
319 break;
320 case CPUCLOCK_VIRT:
321 cpu->cpu = cputime.utime;
322 break;
323 case CPUCLOCK_SCHED:
324 cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
325 break;
327 return 0;
331 int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
333 const pid_t pid = CPUCLOCK_PID(which_clock);
334 int error = -EINVAL;
335 union cpu_time_count rtn;
337 if (pid == 0) {
339 * Special case constant value for our own clocks.
340 * We don't have to do any lookup to find ourselves.
342 if (CPUCLOCK_PERTHREAD(which_clock)) {
344 * Sampling just ourselves we can do with no locking.
346 error = cpu_clock_sample(which_clock,
347 current, &rtn);
348 } else {
349 read_lock(&tasklist_lock);
350 error = cpu_clock_sample_group(which_clock,
351 current, &rtn);
352 read_unlock(&tasklist_lock);
354 } else {
356 * Find the given PID, and validate that the caller
357 * should be able to see it.
359 struct task_struct *p;
360 rcu_read_lock();
361 p = find_task_by_vpid(pid);
362 if (p) {
363 if (CPUCLOCK_PERTHREAD(which_clock)) {
364 if (same_thread_group(p, current)) {
365 error = cpu_clock_sample(which_clock,
366 p, &rtn);
368 } else {
369 read_lock(&tasklist_lock);
370 if (thread_group_leader(p) && p->signal) {
371 error =
372 cpu_clock_sample_group(which_clock,
373 p, &rtn);
375 read_unlock(&tasklist_lock);
378 rcu_read_unlock();
381 if (error)
382 return error;
383 sample_to_timespec(which_clock, rtn, tp);
384 return 0;
389 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
390 * This is called from sys_timer_create with the new timer already locked.
392 int posix_cpu_timer_create(struct k_itimer *new_timer)
394 int ret = 0;
395 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
396 struct task_struct *p;
398 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
399 return -EINVAL;
401 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
402 new_timer->it.cpu.incr.sched = 0;
403 new_timer->it.cpu.expires.sched = 0;
405 read_lock(&tasklist_lock);
406 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
407 if (pid == 0) {
408 p = current;
409 } else {
410 p = find_task_by_vpid(pid);
411 if (p && !same_thread_group(p, current))
412 p = NULL;
414 } else {
415 if (pid == 0) {
416 p = current->group_leader;
417 } else {
418 p = find_task_by_vpid(pid);
419 if (p && !thread_group_leader(p))
420 p = NULL;
423 new_timer->it.cpu.task = p;
424 if (p) {
425 get_task_struct(p);
426 } else {
427 ret = -EINVAL;
429 read_unlock(&tasklist_lock);
431 return ret;
435 * Clean up a CPU-clock timer that is about to be destroyed.
436 * This is called from timer deletion with the timer already locked.
437 * If we return TIMER_RETRY, it's necessary to release the timer's lock
438 * and try again. (This happens when the timer is in the middle of firing.)
440 int posix_cpu_timer_del(struct k_itimer *timer)
442 struct task_struct *p = timer->it.cpu.task;
443 int ret = 0;
445 if (likely(p != NULL)) {
446 read_lock(&tasklist_lock);
447 if (unlikely(p->signal == NULL)) {
449 * We raced with the reaping of the task.
450 * The deletion should have cleared us off the list.
452 BUG_ON(!list_empty(&timer->it.cpu.entry));
453 } else {
454 spin_lock(&p->sighand->siglock);
455 if (timer->it.cpu.firing)
456 ret = TIMER_RETRY;
457 else
458 list_del(&timer->it.cpu.entry);
459 spin_unlock(&p->sighand->siglock);
461 read_unlock(&tasklist_lock);
463 if (!ret)
464 put_task_struct(p);
467 return ret;
471 * Clean out CPU timers still ticking when a thread exited. The task
472 * pointer is cleared, and the expiry time is replaced with the residual
473 * time for later timer_gettime calls to return.
474 * This must be called with the siglock held.
476 static void cleanup_timers(struct list_head *head,
477 cputime_t utime, cputime_t stime,
478 unsigned long long sum_exec_runtime)
480 struct cpu_timer_list *timer, *next;
481 cputime_t ptime = cputime_add(utime, stime);
483 list_for_each_entry_safe(timer, next, head, entry) {
484 list_del_init(&timer->entry);
485 if (cputime_lt(timer->expires.cpu, ptime)) {
486 timer->expires.cpu = cputime_zero;
487 } else {
488 timer->expires.cpu = cputime_sub(timer->expires.cpu,
489 ptime);
493 ++head;
494 list_for_each_entry_safe(timer, next, head, entry) {
495 list_del_init(&timer->entry);
496 if (cputime_lt(timer->expires.cpu, utime)) {
497 timer->expires.cpu = cputime_zero;
498 } else {
499 timer->expires.cpu = cputime_sub(timer->expires.cpu,
500 utime);
504 ++head;
505 list_for_each_entry_safe(timer, next, head, entry) {
506 list_del_init(&timer->entry);
507 if (timer->expires.sched < sum_exec_runtime) {
508 timer->expires.sched = 0;
509 } else {
510 timer->expires.sched -= sum_exec_runtime;
516 * These are both called with the siglock held, when the current thread
517 * is being reaped. When the final (leader) thread in the group is reaped,
518 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
520 void posix_cpu_timers_exit(struct task_struct *tsk)
522 cleanup_timers(tsk->cpu_timers,
523 tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
526 void posix_cpu_timers_exit_group(struct task_struct *tsk)
528 struct task_cputime cputime;
530 thread_group_cputime(tsk, &cputime);
531 cleanup_timers(tsk->signal->cpu_timers,
532 cputime.utime, cputime.stime, cputime.sum_exec_runtime);
535 static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
538 * That's all for this thread or process.
539 * We leave our residual in expires to be reported.
541 put_task_struct(timer->it.cpu.task);
542 timer->it.cpu.task = NULL;
543 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
544 timer->it.cpu.expires,
545 now);
549 * Insert the timer on the appropriate list before any timers that
550 * expire later. This must be called with the tasklist_lock held
551 * for reading, and interrupts disabled.
553 static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
555 struct task_struct *p = timer->it.cpu.task;
556 struct list_head *head, *listpos;
557 struct cpu_timer_list *const nt = &timer->it.cpu;
558 struct cpu_timer_list *next;
559 unsigned long i;
561 head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
562 p->cpu_timers : p->signal->cpu_timers);
563 head += CPUCLOCK_WHICH(timer->it_clock);
565 BUG_ON(!irqs_disabled());
566 spin_lock(&p->sighand->siglock);
568 listpos = head;
569 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
570 list_for_each_entry(next, head, entry) {
571 if (next->expires.sched > nt->expires.sched)
572 break;
573 listpos = &next->entry;
575 } else {
576 list_for_each_entry(next, head, entry) {
577 if (cputime_gt(next->expires.cpu, nt->expires.cpu))
578 break;
579 listpos = &next->entry;
582 list_add(&nt->entry, listpos);
584 if (listpos == head) {
586 * We are the new earliest-expiring timer.
587 * If we are a thread timer, there can always
588 * be a process timer telling us to stop earlier.
591 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
592 switch (CPUCLOCK_WHICH(timer->it_clock)) {
593 default:
594 BUG();
595 case CPUCLOCK_PROF:
596 if (cputime_eq(p->cputime_expires.prof_exp,
597 cputime_zero) ||
598 cputime_gt(p->cputime_expires.prof_exp,
599 nt->expires.cpu))
600 p->cputime_expires.prof_exp =
601 nt->expires.cpu;
602 break;
603 case CPUCLOCK_VIRT:
604 if (cputime_eq(p->cputime_expires.virt_exp,
605 cputime_zero) ||
606 cputime_gt(p->cputime_expires.virt_exp,
607 nt->expires.cpu))
608 p->cputime_expires.virt_exp =
609 nt->expires.cpu;
610 break;
611 case CPUCLOCK_SCHED:
612 if (p->cputime_expires.sched_exp == 0 ||
613 p->cputime_expires.sched_exp >
614 nt->expires.sched)
615 p->cputime_expires.sched_exp =
616 nt->expires.sched;
617 break;
619 } else {
621 * For a process timer, set the cached expiration time.
623 switch (CPUCLOCK_WHICH(timer->it_clock)) {
624 default:
625 BUG();
626 case CPUCLOCK_VIRT:
627 if (!cputime_eq(p->signal->it_virt_expires,
628 cputime_zero) &&
629 cputime_lt(p->signal->it_virt_expires,
630 timer->it.cpu.expires.cpu))
631 break;
632 p->signal->cputime_expires.virt_exp =
633 timer->it.cpu.expires.cpu;
634 break;
635 case CPUCLOCK_PROF:
636 if (!cputime_eq(p->signal->it_prof_expires,
637 cputime_zero) &&
638 cputime_lt(p->signal->it_prof_expires,
639 timer->it.cpu.expires.cpu))
640 break;
641 i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
642 if (i != RLIM_INFINITY &&
643 i <= cputime_to_secs(timer->it.cpu.expires.cpu))
644 break;
645 p->signal->cputime_expires.prof_exp =
646 timer->it.cpu.expires.cpu;
647 break;
648 case CPUCLOCK_SCHED:
649 p->signal->cputime_expires.sched_exp =
650 timer->it.cpu.expires.sched;
651 break;
656 spin_unlock(&p->sighand->siglock);
660 * The timer is locked, fire it and arrange for its reload.
662 static void cpu_timer_fire(struct k_itimer *timer)
664 if (unlikely(timer->sigq == NULL)) {
666 * This a special case for clock_nanosleep,
667 * not a normal timer from sys_timer_create.
669 wake_up_process(timer->it_process);
670 timer->it.cpu.expires.sched = 0;
671 } else if (timer->it.cpu.incr.sched == 0) {
673 * One-shot timer. Clear it as soon as it's fired.
675 posix_timer_event(timer, 0);
676 timer->it.cpu.expires.sched = 0;
677 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
679 * The signal did not get queued because the signal
680 * was ignored, so we won't get any callback to
681 * reload the timer. But we need to keep it
682 * ticking in case the signal is deliverable next time.
684 posix_cpu_timer_schedule(timer);
689 * Guts of sys_timer_settime for CPU timers.
690 * This is called with the timer locked and interrupts disabled.
691 * If we return TIMER_RETRY, it's necessary to release the timer's lock
692 * and try again. (This happens when the timer is in the middle of firing.)
694 int posix_cpu_timer_set(struct k_itimer *timer, int flags,
695 struct itimerspec *new, struct itimerspec *old)
697 struct task_struct *p = timer->it.cpu.task;
698 union cpu_time_count old_expires, new_expires, val;
699 int ret;
701 if (unlikely(p == NULL)) {
703 * Timer refers to a dead task's clock.
705 return -ESRCH;
708 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
710 read_lock(&tasklist_lock);
712 * We need the tasklist_lock to protect against reaping that
713 * clears p->signal. If p has just been reaped, we can no
714 * longer get any information about it at all.
716 if (unlikely(p->signal == NULL)) {
717 read_unlock(&tasklist_lock);
718 put_task_struct(p);
719 timer->it.cpu.task = NULL;
720 return -ESRCH;
724 * Disarm any old timer after extracting its expiry time.
726 BUG_ON(!irqs_disabled());
728 ret = 0;
729 spin_lock(&p->sighand->siglock);
730 old_expires = timer->it.cpu.expires;
731 if (unlikely(timer->it.cpu.firing)) {
732 timer->it.cpu.firing = -1;
733 ret = TIMER_RETRY;
734 } else
735 list_del_init(&timer->it.cpu.entry);
736 spin_unlock(&p->sighand->siglock);
739 * We need to sample the current value to convert the new
740 * value from to relative and absolute, and to convert the
741 * old value from absolute to relative. To set a process
742 * timer, we need a sample to balance the thread expiry
743 * times (in arm_timer). With an absolute time, we must
744 * check if it's already passed. In short, we need a sample.
746 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
747 cpu_clock_sample(timer->it_clock, p, &val);
748 } else {
749 cpu_clock_sample_group(timer->it_clock, p, &val);
752 if (old) {
753 if (old_expires.sched == 0) {
754 old->it_value.tv_sec = 0;
755 old->it_value.tv_nsec = 0;
756 } else {
758 * Update the timer in case it has
759 * overrun already. If it has,
760 * we'll report it as having overrun
761 * and with the next reloaded timer
762 * already ticking, though we are
763 * swallowing that pending
764 * notification here to install the
765 * new setting.
767 bump_cpu_timer(timer, val);
768 if (cpu_time_before(timer->it_clock, val,
769 timer->it.cpu.expires)) {
770 old_expires = cpu_time_sub(
771 timer->it_clock,
772 timer->it.cpu.expires, val);
773 sample_to_timespec(timer->it_clock,
774 old_expires,
775 &old->it_value);
776 } else {
777 old->it_value.tv_nsec = 1;
778 old->it_value.tv_sec = 0;
783 if (unlikely(ret)) {
785 * We are colliding with the timer actually firing.
786 * Punt after filling in the timer's old value, and
787 * disable this firing since we are already reporting
788 * it as an overrun (thanks to bump_cpu_timer above).
790 read_unlock(&tasklist_lock);
791 goto out;
794 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
795 cpu_time_add(timer->it_clock, &new_expires, val);
799 * Install the new expiry time (or zero).
800 * For a timer with no notification action, we don't actually
801 * arm the timer (we'll just fake it for timer_gettime).
803 timer->it.cpu.expires = new_expires;
804 if (new_expires.sched != 0 &&
805 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
806 cpu_time_before(timer->it_clock, val, new_expires)) {
807 arm_timer(timer, val);
810 read_unlock(&tasklist_lock);
813 * Install the new reload setting, and
814 * set up the signal and overrun bookkeeping.
816 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
817 &new->it_interval);
820 * This acts as a modification timestamp for the timer,
821 * so any automatic reload attempt will punt on seeing
822 * that we have reset the timer manually.
824 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
825 ~REQUEUE_PENDING;
826 timer->it_overrun_last = 0;
827 timer->it_overrun = -1;
829 if (new_expires.sched != 0 &&
830 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
831 !cpu_time_before(timer->it_clock, val, new_expires)) {
833 * The designated time already passed, so we notify
834 * immediately, even if the thread never runs to
835 * accumulate more time on this clock.
837 cpu_timer_fire(timer);
840 ret = 0;
841 out:
842 if (old) {
843 sample_to_timespec(timer->it_clock,
844 timer->it.cpu.incr, &old->it_interval);
846 return ret;
849 void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
851 union cpu_time_count now;
852 struct task_struct *p = timer->it.cpu.task;
853 int clear_dead;
856 * Easy part: convert the reload time.
858 sample_to_timespec(timer->it_clock,
859 timer->it.cpu.incr, &itp->it_interval);
861 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
862 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
863 return;
866 if (unlikely(p == NULL)) {
868 * This task already died and the timer will never fire.
869 * In this case, expires is actually the dead value.
871 dead:
872 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
873 &itp->it_value);
874 return;
878 * Sample the clock to take the difference with the expiry time.
880 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
881 cpu_clock_sample(timer->it_clock, p, &now);
882 clear_dead = p->exit_state;
883 } else {
884 read_lock(&tasklist_lock);
885 if (unlikely(p->signal == NULL)) {
887 * The process has been reaped.
888 * We can't even collect a sample any more.
889 * Call the timer disarmed, nothing else to do.
891 put_task_struct(p);
892 timer->it.cpu.task = NULL;
893 timer->it.cpu.expires.sched = 0;
894 read_unlock(&tasklist_lock);
895 goto dead;
896 } else {
897 cpu_clock_sample_group(timer->it_clock, p, &now);
898 clear_dead = (unlikely(p->exit_state) &&
899 thread_group_empty(p));
901 read_unlock(&tasklist_lock);
904 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
905 if (timer->it.cpu.incr.sched == 0 &&
906 cpu_time_before(timer->it_clock,
907 timer->it.cpu.expires, now)) {
909 * Do-nothing timer expired and has no reload,
910 * so it's as if it was never set.
912 timer->it.cpu.expires.sched = 0;
913 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
914 return;
917 * Account for any expirations and reloads that should
918 * have happened.
920 bump_cpu_timer(timer, now);
923 if (unlikely(clear_dead)) {
925 * We've noticed that the thread is dead, but
926 * not yet reaped. Take this opportunity to
927 * drop our task ref.
929 clear_dead_task(timer, now);
930 goto dead;
933 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
934 sample_to_timespec(timer->it_clock,
935 cpu_time_sub(timer->it_clock,
936 timer->it.cpu.expires, now),
937 &itp->it_value);
938 } else {
940 * The timer should have expired already, but the firing
941 * hasn't taken place yet. Say it's just about to expire.
943 itp->it_value.tv_nsec = 1;
944 itp->it_value.tv_sec = 0;
949 * Check for any per-thread CPU timers that have fired and move them off
950 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
951 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
953 static void check_thread_timers(struct task_struct *tsk,
954 struct list_head *firing)
956 int maxfire;
957 struct list_head *timers = tsk->cpu_timers;
958 struct signal_struct *const sig = tsk->signal;
960 maxfire = 20;
961 tsk->cputime_expires.prof_exp = cputime_zero;
962 while (!list_empty(timers)) {
963 struct cpu_timer_list *t = list_first_entry(timers,
964 struct cpu_timer_list,
965 entry);
966 if (!--maxfire || cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
967 tsk->cputime_expires.prof_exp = t->expires.cpu;
968 break;
970 t->firing = 1;
971 list_move_tail(&t->entry, firing);
974 ++timers;
975 maxfire = 20;
976 tsk->cputime_expires.virt_exp = cputime_zero;
977 while (!list_empty(timers)) {
978 struct cpu_timer_list *t = list_first_entry(timers,
979 struct cpu_timer_list,
980 entry);
981 if (!--maxfire || cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
982 tsk->cputime_expires.virt_exp = t->expires.cpu;
983 break;
985 t->firing = 1;
986 list_move_tail(&t->entry, firing);
989 ++timers;
990 maxfire = 20;
991 tsk->cputime_expires.sched_exp = 0;
992 while (!list_empty(timers)) {
993 struct cpu_timer_list *t = list_first_entry(timers,
994 struct cpu_timer_list,
995 entry);
996 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
997 tsk->cputime_expires.sched_exp = t->expires.sched;
998 break;
1000 t->firing = 1;
1001 list_move_tail(&t->entry, firing);
1005 * Check for the special case thread timers.
1007 if (sig->rlim[RLIMIT_RTTIME].rlim_cur != RLIM_INFINITY) {
1008 unsigned long hard = sig->rlim[RLIMIT_RTTIME].rlim_max;
1009 unsigned long *soft = &sig->rlim[RLIMIT_RTTIME].rlim_cur;
1011 if (hard != RLIM_INFINITY &&
1012 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
1014 * At the hard limit, we just die.
1015 * No need to calculate anything else now.
1017 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1018 return;
1020 if (tsk->rt.timeout > DIV_ROUND_UP(*soft, USEC_PER_SEC/HZ)) {
1022 * At the soft limit, send a SIGXCPU every second.
1024 if (sig->rlim[RLIMIT_RTTIME].rlim_cur
1025 < sig->rlim[RLIMIT_RTTIME].rlim_max) {
1026 sig->rlim[RLIMIT_RTTIME].rlim_cur +=
1027 USEC_PER_SEC;
1029 printk(KERN_INFO
1030 "RT Watchdog Timeout: %s[%d]\n",
1031 tsk->comm, task_pid_nr(tsk));
1032 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1038 * Check for any per-thread CPU timers that have fired and move them
1039 * off the tsk->*_timers list onto the firing list. Per-thread timers
1040 * have already been taken off.
1042 static void check_process_timers(struct task_struct *tsk,
1043 struct list_head *firing)
1045 int maxfire;
1046 struct signal_struct *const sig = tsk->signal;
1047 cputime_t utime, ptime, virt_expires, prof_expires;
1048 unsigned long long sum_sched_runtime, sched_expires;
1049 struct list_head *timers = sig->cpu_timers;
1050 struct task_cputime cputime;
1053 * Don't sample the current process CPU clocks if there are no timers.
1055 if (list_empty(&timers[CPUCLOCK_PROF]) &&
1056 cputime_eq(sig->it_prof_expires, cputime_zero) &&
1057 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
1058 list_empty(&timers[CPUCLOCK_VIRT]) &&
1059 cputime_eq(sig->it_virt_expires, cputime_zero) &&
1060 list_empty(&timers[CPUCLOCK_SCHED]))
1061 return;
1064 * Collect the current process totals.
1066 thread_group_cputime(tsk, &cputime);
1067 utime = cputime.utime;
1068 ptime = cputime_add(utime, cputime.stime);
1069 sum_sched_runtime = cputime.sum_exec_runtime;
1070 maxfire = 20;
1071 prof_expires = cputime_zero;
1072 while (!list_empty(timers)) {
1073 struct cpu_timer_list *tl = list_first_entry(timers,
1074 struct cpu_timer_list,
1075 entry);
1076 if (!--maxfire || cputime_lt(ptime, tl->expires.cpu)) {
1077 prof_expires = tl->expires.cpu;
1078 break;
1080 tl->firing = 1;
1081 list_move_tail(&tl->entry, firing);
1084 ++timers;
1085 maxfire = 20;
1086 virt_expires = cputime_zero;
1087 while (!list_empty(timers)) {
1088 struct cpu_timer_list *tl = list_first_entry(timers,
1089 struct cpu_timer_list,
1090 entry);
1091 if (!--maxfire || cputime_lt(utime, tl->expires.cpu)) {
1092 virt_expires = tl->expires.cpu;
1093 break;
1095 tl->firing = 1;
1096 list_move_tail(&tl->entry, firing);
1099 ++timers;
1100 maxfire = 20;
1101 sched_expires = 0;
1102 while (!list_empty(timers)) {
1103 struct cpu_timer_list *tl = list_first_entry(timers,
1104 struct cpu_timer_list,
1105 entry);
1106 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1107 sched_expires = tl->expires.sched;
1108 break;
1110 tl->firing = 1;
1111 list_move_tail(&tl->entry, firing);
1115 * Check for the special case process timers.
1117 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1118 if (cputime_ge(ptime, sig->it_prof_expires)) {
1119 /* ITIMER_PROF fires and reloads. */
1120 sig->it_prof_expires = sig->it_prof_incr;
1121 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1122 sig->it_prof_expires = cputime_add(
1123 sig->it_prof_expires, ptime);
1125 __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
1127 if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
1128 (cputime_eq(prof_expires, cputime_zero) ||
1129 cputime_lt(sig->it_prof_expires, prof_expires))) {
1130 prof_expires = sig->it_prof_expires;
1133 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1134 if (cputime_ge(utime, sig->it_virt_expires)) {
1135 /* ITIMER_VIRTUAL fires and reloads. */
1136 sig->it_virt_expires = sig->it_virt_incr;
1137 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1138 sig->it_virt_expires = cputime_add(
1139 sig->it_virt_expires, utime);
1141 __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
1143 if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
1144 (cputime_eq(virt_expires, cputime_zero) ||
1145 cputime_lt(sig->it_virt_expires, virt_expires))) {
1146 virt_expires = sig->it_virt_expires;
1149 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1150 unsigned long psecs = cputime_to_secs(ptime);
1151 cputime_t x;
1152 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
1154 * At the hard limit, we just die.
1155 * No need to calculate anything else now.
1157 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1158 return;
1160 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
1162 * At the soft limit, send a SIGXCPU every second.
1164 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1165 if (sig->rlim[RLIMIT_CPU].rlim_cur
1166 < sig->rlim[RLIMIT_CPU].rlim_max) {
1167 sig->rlim[RLIMIT_CPU].rlim_cur++;
1170 x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
1171 if (cputime_eq(prof_expires, cputime_zero) ||
1172 cputime_lt(x, prof_expires)) {
1173 prof_expires = x;
1177 if (!cputime_eq(prof_expires, cputime_zero) &&
1178 (cputime_eq(sig->cputime_expires.prof_exp, cputime_zero) ||
1179 cputime_gt(sig->cputime_expires.prof_exp, prof_expires)))
1180 sig->cputime_expires.prof_exp = prof_expires;
1181 if (!cputime_eq(virt_expires, cputime_zero) &&
1182 (cputime_eq(sig->cputime_expires.virt_exp, cputime_zero) ||
1183 cputime_gt(sig->cputime_expires.virt_exp, virt_expires)))
1184 sig->cputime_expires.virt_exp = virt_expires;
1185 if (sched_expires != 0 &&
1186 (sig->cputime_expires.sched_exp == 0 ||
1187 sig->cputime_expires.sched_exp > sched_expires))
1188 sig->cputime_expires.sched_exp = sched_expires;
1192 * This is called from the signal code (via do_schedule_next_timer)
1193 * when the last timer signal was delivered and we have to reload the timer.
1195 void posix_cpu_timer_schedule(struct k_itimer *timer)
1197 struct task_struct *p = timer->it.cpu.task;
1198 union cpu_time_count now;
1200 if (unlikely(p == NULL))
1202 * The task was cleaned up already, no future firings.
1204 goto out;
1207 * Fetch the current sample and update the timer's expiry time.
1209 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1210 cpu_clock_sample(timer->it_clock, p, &now);
1211 bump_cpu_timer(timer, now);
1212 if (unlikely(p->exit_state)) {
1213 clear_dead_task(timer, now);
1214 goto out;
1216 read_lock(&tasklist_lock); /* arm_timer needs it. */
1217 } else {
1218 read_lock(&tasklist_lock);
1219 if (unlikely(p->signal == NULL)) {
1221 * The process has been reaped.
1222 * We can't even collect a sample any more.
1224 put_task_struct(p);
1225 timer->it.cpu.task = p = NULL;
1226 timer->it.cpu.expires.sched = 0;
1227 goto out_unlock;
1228 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1230 * We've noticed that the thread is dead, but
1231 * not yet reaped. Take this opportunity to
1232 * drop our task ref.
1234 clear_dead_task(timer, now);
1235 goto out_unlock;
1237 cpu_clock_sample_group(timer->it_clock, p, &now);
1238 bump_cpu_timer(timer, now);
1239 /* Leave the tasklist_lock locked for the call below. */
1243 * Now re-arm for the new expiry time.
1245 arm_timer(timer, now);
1247 out_unlock:
1248 read_unlock(&tasklist_lock);
1250 out:
1251 timer->it_overrun_last = timer->it_overrun;
1252 timer->it_overrun = -1;
1253 ++timer->it_requeue_pending;
1257 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1259 * @cputime: The struct to compare.
1261 * Checks @cputime to see if all fields are zero. Returns true if all fields
1262 * are zero, false if any field is nonzero.
1264 static inline int task_cputime_zero(const struct task_cputime *cputime)
1266 if (cputime_eq(cputime->utime, cputime_zero) &&
1267 cputime_eq(cputime->stime, cputime_zero) &&
1268 cputime->sum_exec_runtime == 0)
1269 return 1;
1270 return 0;
1274 * task_cputime_expired - Compare two task_cputime entities.
1276 * @sample: The task_cputime structure to be checked for expiration.
1277 * @expires: Expiration times, against which @sample will be checked.
1279 * Checks @sample against @expires to see if any field of @sample has expired.
1280 * Returns true if any field of the former is greater than the corresponding
1281 * field of the latter if the latter field is set. Otherwise returns false.
1283 static inline int task_cputime_expired(const struct task_cputime *sample,
1284 const struct task_cputime *expires)
1286 if (!cputime_eq(expires->utime, cputime_zero) &&
1287 cputime_ge(sample->utime, expires->utime))
1288 return 1;
1289 if (!cputime_eq(expires->stime, cputime_zero) &&
1290 cputime_ge(cputime_add(sample->utime, sample->stime),
1291 expires->stime))
1292 return 1;
1293 if (expires->sum_exec_runtime != 0 &&
1294 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1295 return 1;
1296 return 0;
1300 * fastpath_timer_check - POSIX CPU timers fast path.
1302 * @tsk: The task (thread) being checked.
1304 * Check the task and thread group timers. If both are zero (there are no
1305 * timers set) return false. Otherwise snapshot the task and thread group
1306 * timers and compare them with the corresponding expiration times. Return
1307 * true if a timer has expired, else return false.
1309 static inline int fastpath_timer_check(struct task_struct *tsk)
1311 struct signal_struct *sig;
1313 /* tsk == current, ensure it is safe to use ->signal/sighand */
1314 if (unlikely(tsk->exit_state))
1315 return 0;
1317 if (!task_cputime_zero(&tsk->cputime_expires)) {
1318 struct task_cputime task_sample = {
1319 .utime = tsk->utime,
1320 .stime = tsk->stime,
1321 .sum_exec_runtime = tsk->se.sum_exec_runtime
1324 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1325 return 1;
1328 sig = tsk->signal;
1329 if (!task_cputime_zero(&sig->cputime_expires)) {
1330 struct task_cputime group_sample;
1332 thread_group_cputime(tsk, &group_sample);
1333 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1334 return 1;
1336 return 0;
1340 * This is called from the timer interrupt handler. The irq handler has
1341 * already updated our counts. We need to check if any timers fire now.
1342 * Interrupts are disabled.
1344 void run_posix_cpu_timers(struct task_struct *tsk)
1346 LIST_HEAD(firing);
1347 struct k_itimer *timer, *next;
1349 BUG_ON(!irqs_disabled());
1352 * The fast path checks that there are no expired thread or thread
1353 * group timers. If that's so, just return.
1355 if (!fastpath_timer_check(tsk))
1356 return;
1358 spin_lock(&tsk->sighand->siglock);
1360 * Here we take off tsk->signal->cpu_timers[N] and
1361 * tsk->cpu_timers[N] all the timers that are firing, and
1362 * put them on the firing list.
1364 check_thread_timers(tsk, &firing);
1365 check_process_timers(tsk, &firing);
1368 * We must release these locks before taking any timer's lock.
1369 * There is a potential race with timer deletion here, as the
1370 * siglock now protects our private firing list. We have set
1371 * the firing flag in each timer, so that a deletion attempt
1372 * that gets the timer lock before we do will give it up and
1373 * spin until we've taken care of that timer below.
1375 spin_unlock(&tsk->sighand->siglock);
1378 * Now that all the timers on our list have the firing flag,
1379 * noone will touch their list entries but us. We'll take
1380 * each timer's lock before clearing its firing flag, so no
1381 * timer call will interfere.
1383 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1384 int firing;
1385 spin_lock(&timer->it_lock);
1386 list_del_init(&timer->it.cpu.entry);
1387 firing = timer->it.cpu.firing;
1388 timer->it.cpu.firing = 0;
1390 * The firing flag is -1 if we collided with a reset
1391 * of the timer, which already reported this
1392 * almost-firing as an overrun. So don't generate an event.
1394 if (likely(firing >= 0)) {
1395 cpu_timer_fire(timer);
1397 spin_unlock(&timer->it_lock);
1402 * Set one of the process-wide special case CPU timers.
1403 * The tsk->sighand->siglock must be held by the caller.
1404 * The *newval argument is relative and we update it to be absolute, *oldval
1405 * is absolute and we update it to be relative.
1407 void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1408 cputime_t *newval, cputime_t *oldval)
1410 union cpu_time_count now;
1411 struct list_head *head;
1413 BUG_ON(clock_idx == CPUCLOCK_SCHED);
1414 cpu_clock_sample_group(clock_idx, tsk, &now);
1416 if (oldval) {
1417 if (!cputime_eq(*oldval, cputime_zero)) {
1418 if (cputime_le(*oldval, now.cpu)) {
1419 /* Just about to fire. */
1420 *oldval = jiffies_to_cputime(1);
1421 } else {
1422 *oldval = cputime_sub(*oldval, now.cpu);
1426 if (cputime_eq(*newval, cputime_zero))
1427 return;
1428 *newval = cputime_add(*newval, now.cpu);
1431 * If the RLIMIT_CPU timer will expire before the
1432 * ITIMER_PROF timer, we have nothing else to do.
1434 if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1435 < cputime_to_secs(*newval))
1436 return;
1440 * Check whether there are any process timers already set to fire
1441 * before this one. If so, we don't have anything more to do.
1443 head = &tsk->signal->cpu_timers[clock_idx];
1444 if (list_empty(head) ||
1445 cputime_ge(list_first_entry(head,
1446 struct cpu_timer_list, entry)->expires.cpu,
1447 *newval)) {
1448 switch (clock_idx) {
1449 case CPUCLOCK_PROF:
1450 tsk->signal->cputime_expires.prof_exp = *newval;
1451 break;
1452 case CPUCLOCK_VIRT:
1453 tsk->signal->cputime_expires.virt_exp = *newval;
1454 break;
1459 static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1460 struct timespec *rqtp, struct itimerspec *it)
1462 struct k_itimer timer;
1463 int error;
1466 * Set up a temporary timer and then wait for it to go off.
1468 memset(&timer, 0, sizeof timer);
1469 spin_lock_init(&timer.it_lock);
1470 timer.it_clock = which_clock;
1471 timer.it_overrun = -1;
1472 error = posix_cpu_timer_create(&timer);
1473 timer.it_process = current;
1474 if (!error) {
1475 static struct itimerspec zero_it;
1477 memset(it, 0, sizeof *it);
1478 it->it_value = *rqtp;
1480 spin_lock_irq(&timer.it_lock);
1481 error = posix_cpu_timer_set(&timer, flags, it, NULL);
1482 if (error) {
1483 spin_unlock_irq(&timer.it_lock);
1484 return error;
1487 while (!signal_pending(current)) {
1488 if (timer.it.cpu.expires.sched == 0) {
1490 * Our timer fired and was reset.
1492 spin_unlock_irq(&timer.it_lock);
1493 return 0;
1497 * Block until cpu_timer_fire (or a signal) wakes us.
1499 __set_current_state(TASK_INTERRUPTIBLE);
1500 spin_unlock_irq(&timer.it_lock);
1501 schedule();
1502 spin_lock_irq(&timer.it_lock);
1506 * We were interrupted by a signal.
1508 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
1509 posix_cpu_timer_set(&timer, 0, &zero_it, it);
1510 spin_unlock_irq(&timer.it_lock);
1512 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
1514 * It actually did fire already.
1516 return 0;
1519 error = -ERESTART_RESTARTBLOCK;
1522 return error;
1525 int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1526 struct timespec *rqtp, struct timespec __user *rmtp)
1528 struct restart_block *restart_block =
1529 &current_thread_info()->restart_block;
1530 struct itimerspec it;
1531 int error;
1534 * Diagnose required errors first.
1536 if (CPUCLOCK_PERTHREAD(which_clock) &&
1537 (CPUCLOCK_PID(which_clock) == 0 ||
1538 CPUCLOCK_PID(which_clock) == current->pid))
1539 return -EINVAL;
1541 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1543 if (error == -ERESTART_RESTARTBLOCK) {
1545 if (flags & TIMER_ABSTIME)
1546 return -ERESTARTNOHAND;
1548 * Report back to the user the time still remaining.
1550 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1551 return -EFAULT;
1553 restart_block->fn = posix_cpu_nsleep_restart;
1554 restart_block->arg0 = which_clock;
1555 restart_block->arg1 = (unsigned long) rmtp;
1556 restart_block->arg2 = rqtp->tv_sec;
1557 restart_block->arg3 = rqtp->tv_nsec;
1559 return error;
1562 long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1564 clockid_t which_clock = restart_block->arg0;
1565 struct timespec __user *rmtp;
1566 struct timespec t;
1567 struct itimerspec it;
1568 int error;
1570 rmtp = (struct timespec __user *) restart_block->arg1;
1571 t.tv_sec = restart_block->arg2;
1572 t.tv_nsec = restart_block->arg3;
1574 restart_block->fn = do_no_restart_syscall;
1575 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1577 if (error == -ERESTART_RESTARTBLOCK) {
1579 * Report back to the user the time still remaining.
1581 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1582 return -EFAULT;
1584 restart_block->fn = posix_cpu_nsleep_restart;
1585 restart_block->arg0 = which_clock;
1586 restart_block->arg1 = (unsigned long) rmtp;
1587 restart_block->arg2 = t.tv_sec;
1588 restart_block->arg3 = t.tv_nsec;
1590 return error;
1595 #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1596 #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1598 static int process_cpu_clock_getres(const clockid_t which_clock,
1599 struct timespec *tp)
1601 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1603 static int process_cpu_clock_get(const clockid_t which_clock,
1604 struct timespec *tp)
1606 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1608 static int process_cpu_timer_create(struct k_itimer *timer)
1610 timer->it_clock = PROCESS_CLOCK;
1611 return posix_cpu_timer_create(timer);
1613 static int process_cpu_nsleep(const clockid_t which_clock, int flags,
1614 struct timespec *rqtp,
1615 struct timespec __user *rmtp)
1617 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
1619 static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1621 return -EINVAL;
1623 static int thread_cpu_clock_getres(const clockid_t which_clock,
1624 struct timespec *tp)
1626 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1628 static int thread_cpu_clock_get(const clockid_t which_clock,
1629 struct timespec *tp)
1631 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1633 static int thread_cpu_timer_create(struct k_itimer *timer)
1635 timer->it_clock = THREAD_CLOCK;
1636 return posix_cpu_timer_create(timer);
1638 static int thread_cpu_nsleep(const clockid_t which_clock, int flags,
1639 struct timespec *rqtp, struct timespec __user *rmtp)
1641 return -EINVAL;
1643 static long thread_cpu_nsleep_restart(struct restart_block *restart_block)
1645 return -EINVAL;
1648 static __init int init_posix_cpu_timers(void)
1650 struct k_clock process = {
1651 .clock_getres = process_cpu_clock_getres,
1652 .clock_get = process_cpu_clock_get,
1653 .clock_set = do_posix_clock_nosettime,
1654 .timer_create = process_cpu_timer_create,
1655 .nsleep = process_cpu_nsleep,
1656 .nsleep_restart = process_cpu_nsleep_restart,
1658 struct k_clock thread = {
1659 .clock_getres = thread_cpu_clock_getres,
1660 .clock_get = thread_cpu_clock_get,
1661 .clock_set = do_posix_clock_nosettime,
1662 .timer_create = thread_cpu_timer_create,
1663 .nsleep = thread_cpu_nsleep,
1664 .nsleep_restart = thread_cpu_nsleep_restart,
1667 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1668 register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1670 return 0;
1672 __initcall(init_posix_cpu_timers);