2 * Implement CPU time clocks for the POSIX clock interface.
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
30 cputime
= alloc_percpu(struct task_cputime
);
33 spin_lock_irq(&tsk
->sighand
->siglock
);
34 if (sig
->cputime
.totals
) {
35 spin_unlock_irq(&tsk
->sighand
->siglock
);
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
);
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
57 void thread_group_cputime(
58 struct task_struct
*tsk
,
59 struct task_cputime
*times
)
61 struct signal_struct
*sig
;
63 struct task_cputime
*tot
;
66 if (unlikely(!sig
) || !sig
->cputime
.totals
) {
67 times
->utime
= tsk
->utime
;
68 times
->stime
= tsk
->stime
;
69 times
->sum_exec_runtime
= tsk
->se
.sum_exec_runtime
;
72 times
->stime
= times
->utime
= cputime_zero
;
73 times
->sum_exec_runtime
= 0;
74 for_each_possible_cpu(i
) {
75 tot
= per_cpu_ptr(tsk
->signal
->cputime
.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
)
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(¤t
->sighand
->siglock
);
93 set_process_cpu_timer(current
, CPUCLOCK_PROF
, &cputime
, NULL
);
94 spin_unlock_irq(¤t
->sighand
->siglock
);
98 static int check_clock(const clockid_t which_clock
)
101 struct task_struct
*p
;
102 const pid_t pid
= CPUCLOCK_PID(which_clock
);
104 if (CPUCLOCK_WHICH(which_clock
) >= CPUCLOCK_MAX
)
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
))) {
116 read_unlock(&tasklist_lock
);
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
;
129 ret
.cpu
= timespec_to_cputime(tp
);
134 static void sample_to_timespec(const clockid_t which_clock
,
135 union cpu_time_count cpu
,
138 if (CPUCLOCK_WHICH(which_clock
) == CPUCLOCK_SCHED
)
139 *tp
= ns_to_timespec(cpu
.sched
);
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
;
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
;
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
) {
171 a
.cpu
= cputime_sub(a
.cpu
, b
.cpu
);
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
)
198 if (timer
->it
.cpu
.incr
.sched
== 0)
201 if (CPUCLOCK_WHICH(timer
->it_clock
) == CPUCLOCK_SCHED
) {
202 unsigned long long delta
, incr
;
204 if (now
.sched
< timer
->it
.cpu
.expires
.sched
)
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
++)
211 for (; i
>= 0; incr
>>= 1, i
--) {
214 timer
->it
.cpu
.expires
.sched
+= incr
;
215 timer
->it_overrun
+= 1 << i
;
219 cputime_t delta
, incr
;
221 if (cputime_lt(now
.cpu
, timer
->it
.cpu
.expires
.cpu
))
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
))
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
)
249 int posix_cpu_clock_getres(const clockid_t which_clock
, struct timespec
*tp
)
251 int error
= check_clock(which_clock
);
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.
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
);
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
)) {
291 cpu
->cpu
= prof_ticks(p
);
294 cpu
->cpu
= virt_ticks(p
);
297 cpu
->sched
= p
->se
.sum_exec_runtime
+ task_delta_exec(p
);
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 (which_clock
) {
318 cpu
->cpu
= cputime_add(cputime
.utime
, cputime
.stime
);
321 cpu
->cpu
= cputime
.utime
;
324 cpu
->sched
= cputime
.sum_exec_runtime
+ task_delta_exec(p
);
331 int posix_cpu_clock_get(const clockid_t which_clock
, struct timespec
*tp
)
333 const pid_t pid
= CPUCLOCK_PID(which_clock
);
335 union cpu_time_count rtn
;
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
,
349 read_lock(&tasklist_lock
);
350 error
= cpu_clock_sample_group(which_clock
,
352 read_unlock(&tasklist_lock
);
356 * Find the given PID, and validate that the caller
357 * should be able to see it.
359 struct task_struct
*p
;
361 p
= find_task_by_vpid(pid
);
363 if (CPUCLOCK_PERTHREAD(which_clock
)) {
364 if (same_thread_group(p
, current
)) {
365 error
= cpu_clock_sample(which_clock
,
369 read_lock(&tasklist_lock
);
370 if (thread_group_leader(p
) && p
->signal
) {
372 cpu_clock_sample_group(which_clock
,
375 read_unlock(&tasklist_lock
);
383 sample_to_timespec(which_clock
, rtn
, tp
);
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
)
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
)
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
)) {
410 p
= find_task_by_vpid(pid
);
411 if (p
&& !same_thread_group(p
, current
))
416 p
= current
->group_leader
;
418 p
= find_task_by_vpid(pid
);
419 if (p
&& !thread_group_leader(p
))
423 new_timer
->it
.cpu
.task
= p
;
429 read_unlock(&tasklist_lock
);
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
;
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
));
454 spin_lock(&p
->sighand
->siglock
);
455 if (timer
->it
.cpu
.firing
)
458 list_del(&timer
->it
.cpu
.entry
);
459 spin_unlock(&p
->sighand
->siglock
);
461 read_unlock(&tasklist_lock
);
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
;
488 timer
->expires
.cpu
= cputime_sub(timer
->expires
.cpu
,
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
;
499 timer
->expires
.cpu
= cputime_sub(timer
->expires
.cpu
,
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;
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
,
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
;
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
);
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
)
573 listpos
= &next
->entry
;
576 list_for_each_entry(next
, head
, entry
) {
577 if (cputime_gt(next
->expires
.cpu
, nt
->expires
.cpu
))
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
)) {
596 if (cputime_eq(p
->cputime_expires
.prof_exp
,
598 cputime_gt(p
->cputime_expires
.prof_exp
,
600 p
->cputime_expires
.prof_exp
=
604 if (cputime_eq(p
->cputime_expires
.virt_exp
,
606 cputime_gt(p
->cputime_expires
.virt_exp
,
608 p
->cputime_expires
.virt_exp
=
612 if (p
->cputime_expires
.sched_exp
== 0 ||
613 p
->cputime_expires
.sched_exp
>
615 p
->cputime_expires
.sched_exp
=
621 * For a process timer, set the cached expiration time.
623 switch (CPUCLOCK_WHICH(timer
->it_clock
)) {
627 if (!cputime_eq(p
->signal
->it_virt_expires
,
629 cputime_lt(p
->signal
->it_virt_expires
,
630 timer
->it
.cpu
.expires
.cpu
))
632 p
->signal
->cputime_expires
.virt_exp
=
633 timer
->it
.cpu
.expires
.cpu
;
636 if (!cputime_eq(p
->signal
->it_prof_expires
,
638 cputime_lt(p
->signal
->it_prof_expires
,
639 timer
->it
.cpu
.expires
.cpu
))
641 i
= p
->signal
->rlim
[RLIMIT_CPU
].rlim_cur
;
642 if (i
!= RLIM_INFINITY
&&
643 i
<= cputime_to_secs(timer
->it
.cpu
.expires
.cpu
))
645 p
->signal
->cputime_expires
.prof_exp
=
646 timer
->it
.cpu
.expires
.cpu
;
649 p
->signal
->cputime_expires
.sched_exp
=
650 timer
->it
.cpu
.expires
.sched
;
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
;
701 if (unlikely(p
== NULL
)) {
703 * Timer refers to a dead task's clock.
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
);
719 timer
->it
.cpu
.task
= NULL
;
724 * Disarm any old timer after extracting its expiry time.
726 BUG_ON(!irqs_disabled());
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;
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
);
749 cpu_clock_sample_group(timer
->it_clock
, p
, &val
);
753 if (old_expires
.sched
== 0) {
754 old
->it_value
.tv_sec
= 0;
755 old
->it_value
.tv_nsec
= 0;
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
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(
772 timer
->it
.cpu
.expires
, val
);
773 sample_to_timespec(timer
->it_clock
,
777 old
->it_value
.tv_nsec
= 1;
778 old
->it_value
.tv_sec
= 0;
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
);
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
,
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) &
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
);
843 sample_to_timespec(timer
->it_clock
,
844 timer
->it
.cpu
.incr
, &old
->it_interval
);
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
;
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;
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.
872 sample_to_timespec(timer
->it_clock
, timer
->it
.cpu
.expires
,
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
;
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.
892 timer
->it
.cpu
.task
= NULL
;
893 timer
->it
.cpu
.expires
.sched
= 0;
894 read_unlock(&tasklist_lock
);
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;
917 * Account for any expirations and reloads that should
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
929 clear_dead_task(timer
, now
);
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
),
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
)
957 struct list_head
*timers
= tsk
->cpu_timers
;
958 struct signal_struct
*const sig
= tsk
->signal
;
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
,
966 if (!--maxfire
|| cputime_lt(prof_ticks(tsk
), t
->expires
.cpu
)) {
967 tsk
->cputime_expires
.prof_exp
= t
->expires
.cpu
;
971 list_move_tail(&t
->entry
, firing
);
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
,
981 if (!--maxfire
|| cputime_lt(virt_ticks(tsk
), t
->expires
.cpu
)) {
982 tsk
->cputime_expires
.virt_exp
= t
->expires
.cpu
;
986 list_move_tail(&t
->entry
, firing
);
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
,
996 if (!--maxfire
|| tsk
->se
.sum_exec_runtime
< t
->expires
.sched
) {
997 tsk
->cputime_expires
.sched_exp
= t
->expires
.sched
;
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
);
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
+=
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
)
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
]))
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
;
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
,
1076 if (!--maxfire
|| cputime_lt(ptime
, tl
->expires
.cpu
)) {
1077 prof_expires
= tl
->expires
.cpu
;
1081 list_move_tail(&tl
->entry
, firing
);
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
,
1091 if (!--maxfire
|| cputime_lt(utime
, tl
->expires
.cpu
)) {
1092 virt_expires
= tl
->expires
.cpu
;
1096 list_move_tail(&tl
->entry
, firing
);
1102 while (!list_empty(timers
)) {
1103 struct cpu_timer_list
*tl
= list_first_entry(timers
,
1104 struct cpu_timer_list
,
1106 if (!--maxfire
|| sum_sched_runtime
< tl
->expires
.sched
) {
1107 sched_expires
= tl
->expires
.sched
;
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
);
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
);
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
)) {
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.
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
);
1216 read_lock(&tasklist_lock
); /* arm_timer needs it. */
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.
1225 timer
->it
.cpu
.task
= p
= NULL
;
1226 timer
->it
.cpu
.expires
.sched
= 0;
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
);
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
);
1248 read_unlock(&tasklist_lock
);
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)
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
))
1289 if (!cputime_eq(expires
->stime
, cputime_zero
) &&
1290 cputime_ge(cputime_add(sample
->utime
, sample
->stime
),
1293 if (expires
->sum_exec_runtime
!= 0 &&
1294 sample
->sum_exec_runtime
>= expires
->sum_exec_runtime
)
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
))
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
))
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
))
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
)
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
))
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
) {
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
);
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);
1422 *oldval
= cputime_sub(*oldval
, now
.cpu
);
1426 if (cputime_eq(*newval
, cputime_zero
))
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
))
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
,
1448 switch (clock_idx
) {
1450 tsk
->signal
->cputime_expires
.prof_exp
= *newval
;
1453 tsk
->signal
->cputime_expires
.virt_exp
= *newval
;
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
;
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
;
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
);
1483 spin_unlock_irq(&timer
.it_lock
);
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
);
1497 * Block until cpu_timer_fire (or a signal) wakes us.
1499 __set_current_state(TASK_INTERRUPTIBLE
);
1500 spin_unlock_irq(&timer
.it_lock
);
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.
1519 error
= -ERESTART_RESTARTBLOCK
;
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 ¤t_thread_info()->restart_block
;
1530 struct itimerspec it
;
1534 * Diagnose required errors first.
1536 if (CPUCLOCK_PERTHREAD(which_clock
) &&
1537 (CPUCLOCK_PID(which_clock
) == 0 ||
1538 CPUCLOCK_PID(which_clock
) == current
->pid
))
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
))
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
;
1562 long posix_cpu_nsleep_restart(struct restart_block
*restart_block
)
1564 clockid_t which_clock
= restart_block
->arg0
;
1565 struct timespec __user
*rmtp
;
1567 struct itimerspec it
;
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
))
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
;
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
)
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
)
1643 static long thread_cpu_nsleep_restart(struct restart_block
*restart_block
)
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
);
1672 __initcall(init_posix_cpu_timers
);