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
= task_sched_runtime(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 switch (CPUCLOCK_WHICH(which_clock
)) {
317 thread_group_cputime(p
, &cputime
);
318 cpu
->cpu
= cputime_add(cputime
.utime
, cputime
.stime
);
321 thread_group_cputime(p
, &cputime
);
322 cpu
->cpu
= cputime
.utime
;
325 cpu
->sched
= thread_group_sched_runtime(p
);
332 int posix_cpu_clock_get(const clockid_t which_clock
, struct timespec
*tp
)
334 const pid_t pid
= CPUCLOCK_PID(which_clock
);
336 union cpu_time_count rtn
;
340 * Special case constant value for our own clocks.
341 * We don't have to do any lookup to find ourselves.
343 if (CPUCLOCK_PERTHREAD(which_clock
)) {
345 * Sampling just ourselves we can do with no locking.
347 error
= cpu_clock_sample(which_clock
,
350 read_lock(&tasklist_lock
);
351 error
= cpu_clock_sample_group(which_clock
,
353 read_unlock(&tasklist_lock
);
357 * Find the given PID, and validate that the caller
358 * should be able to see it.
360 struct task_struct
*p
;
362 p
= find_task_by_vpid(pid
);
364 if (CPUCLOCK_PERTHREAD(which_clock
)) {
365 if (same_thread_group(p
, current
)) {
366 error
= cpu_clock_sample(which_clock
,
370 read_lock(&tasklist_lock
);
371 if (thread_group_leader(p
) && p
->signal
) {
373 cpu_clock_sample_group(which_clock
,
376 read_unlock(&tasklist_lock
);
384 sample_to_timespec(which_clock
, rtn
, tp
);
390 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
391 * This is called from sys_timer_create with the new timer already locked.
393 int posix_cpu_timer_create(struct k_itimer
*new_timer
)
396 const pid_t pid
= CPUCLOCK_PID(new_timer
->it_clock
);
397 struct task_struct
*p
;
399 if (CPUCLOCK_WHICH(new_timer
->it_clock
) >= CPUCLOCK_MAX
)
402 INIT_LIST_HEAD(&new_timer
->it
.cpu
.entry
);
403 new_timer
->it
.cpu
.incr
.sched
= 0;
404 new_timer
->it
.cpu
.expires
.sched
= 0;
406 read_lock(&tasklist_lock
);
407 if (CPUCLOCK_PERTHREAD(new_timer
->it_clock
)) {
411 p
= find_task_by_vpid(pid
);
412 if (p
&& !same_thread_group(p
, current
))
417 p
= current
->group_leader
;
419 p
= find_task_by_vpid(pid
);
420 if (p
&& !thread_group_leader(p
))
424 new_timer
->it
.cpu
.task
= p
;
430 read_unlock(&tasklist_lock
);
436 * Clean up a CPU-clock timer that is about to be destroyed.
437 * This is called from timer deletion with the timer already locked.
438 * If we return TIMER_RETRY, it's necessary to release the timer's lock
439 * and try again. (This happens when the timer is in the middle of firing.)
441 int posix_cpu_timer_del(struct k_itimer
*timer
)
443 struct task_struct
*p
= timer
->it
.cpu
.task
;
446 if (likely(p
!= NULL
)) {
447 read_lock(&tasklist_lock
);
448 if (unlikely(p
->signal
== NULL
)) {
450 * We raced with the reaping of the task.
451 * The deletion should have cleared us off the list.
453 BUG_ON(!list_empty(&timer
->it
.cpu
.entry
));
455 spin_lock(&p
->sighand
->siglock
);
456 if (timer
->it
.cpu
.firing
)
459 list_del(&timer
->it
.cpu
.entry
);
460 spin_unlock(&p
->sighand
->siglock
);
462 read_unlock(&tasklist_lock
);
472 * Clean out CPU timers still ticking when a thread exited. The task
473 * pointer is cleared, and the expiry time is replaced with the residual
474 * time for later timer_gettime calls to return.
475 * This must be called with the siglock held.
477 static void cleanup_timers(struct list_head
*head
,
478 cputime_t utime
, cputime_t stime
,
479 unsigned long long sum_exec_runtime
)
481 struct cpu_timer_list
*timer
, *next
;
482 cputime_t ptime
= cputime_add(utime
, stime
);
484 list_for_each_entry_safe(timer
, next
, head
, entry
) {
485 list_del_init(&timer
->entry
);
486 if (cputime_lt(timer
->expires
.cpu
, ptime
)) {
487 timer
->expires
.cpu
= cputime_zero
;
489 timer
->expires
.cpu
= cputime_sub(timer
->expires
.cpu
,
495 list_for_each_entry_safe(timer
, next
, head
, entry
) {
496 list_del_init(&timer
->entry
);
497 if (cputime_lt(timer
->expires
.cpu
, utime
)) {
498 timer
->expires
.cpu
= cputime_zero
;
500 timer
->expires
.cpu
= cputime_sub(timer
->expires
.cpu
,
506 list_for_each_entry_safe(timer
, next
, head
, entry
) {
507 list_del_init(&timer
->entry
);
508 if (timer
->expires
.sched
< sum_exec_runtime
) {
509 timer
->expires
.sched
= 0;
511 timer
->expires
.sched
-= sum_exec_runtime
;
517 * These are both called with the siglock held, when the current thread
518 * is being reaped. When the final (leader) thread in the group is reaped,
519 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
521 void posix_cpu_timers_exit(struct task_struct
*tsk
)
523 cleanup_timers(tsk
->cpu_timers
,
524 tsk
->utime
, tsk
->stime
, tsk
->se
.sum_exec_runtime
);
527 void posix_cpu_timers_exit_group(struct task_struct
*tsk
)
529 struct task_cputime cputime
;
531 thread_group_cputime(tsk
, &cputime
);
532 cleanup_timers(tsk
->signal
->cpu_timers
,
533 cputime
.utime
, cputime
.stime
, cputime
.sum_exec_runtime
);
536 static void clear_dead_task(struct k_itimer
*timer
, union cpu_time_count now
)
539 * That's all for this thread or process.
540 * We leave our residual in expires to be reported.
542 put_task_struct(timer
->it
.cpu
.task
);
543 timer
->it
.cpu
.task
= NULL
;
544 timer
->it
.cpu
.expires
= cpu_time_sub(timer
->it_clock
,
545 timer
->it
.cpu
.expires
,
550 * Insert the timer on the appropriate list before any timers that
551 * expire later. This must be called with the tasklist_lock held
552 * for reading, and interrupts disabled.
554 static void arm_timer(struct k_itimer
*timer
, union cpu_time_count now
)
556 struct task_struct
*p
= timer
->it
.cpu
.task
;
557 struct list_head
*head
, *listpos
;
558 struct cpu_timer_list
*const nt
= &timer
->it
.cpu
;
559 struct cpu_timer_list
*next
;
562 head
= (CPUCLOCK_PERTHREAD(timer
->it_clock
) ?
563 p
->cpu_timers
: p
->signal
->cpu_timers
);
564 head
+= CPUCLOCK_WHICH(timer
->it_clock
);
566 BUG_ON(!irqs_disabled());
567 spin_lock(&p
->sighand
->siglock
);
570 if (CPUCLOCK_WHICH(timer
->it_clock
) == CPUCLOCK_SCHED
) {
571 list_for_each_entry(next
, head
, entry
) {
572 if (next
->expires
.sched
> nt
->expires
.sched
)
574 listpos
= &next
->entry
;
577 list_for_each_entry(next
, head
, entry
) {
578 if (cputime_gt(next
->expires
.cpu
, nt
->expires
.cpu
))
580 listpos
= &next
->entry
;
583 list_add(&nt
->entry
, listpos
);
585 if (listpos
== head
) {
587 * We are the new earliest-expiring timer.
588 * If we are a thread timer, there can always
589 * be a process timer telling us to stop earlier.
592 if (CPUCLOCK_PERTHREAD(timer
->it_clock
)) {
593 switch (CPUCLOCK_WHICH(timer
->it_clock
)) {
597 if (cputime_eq(p
->cputime_expires
.prof_exp
,
599 cputime_gt(p
->cputime_expires
.prof_exp
,
601 p
->cputime_expires
.prof_exp
=
605 if (cputime_eq(p
->cputime_expires
.virt_exp
,
607 cputime_gt(p
->cputime_expires
.virt_exp
,
609 p
->cputime_expires
.virt_exp
=
613 if (p
->cputime_expires
.sched_exp
== 0 ||
614 p
->cputime_expires
.sched_exp
>
616 p
->cputime_expires
.sched_exp
=
622 * For a process timer, set the cached expiration time.
624 switch (CPUCLOCK_WHICH(timer
->it_clock
)) {
628 if (!cputime_eq(p
->signal
->it_virt_expires
,
630 cputime_lt(p
->signal
->it_virt_expires
,
631 timer
->it
.cpu
.expires
.cpu
))
633 p
->signal
->cputime_expires
.virt_exp
=
634 timer
->it
.cpu
.expires
.cpu
;
637 if (!cputime_eq(p
->signal
->it_prof_expires
,
639 cputime_lt(p
->signal
->it_prof_expires
,
640 timer
->it
.cpu
.expires
.cpu
))
642 i
= p
->signal
->rlim
[RLIMIT_CPU
].rlim_cur
;
643 if (i
!= RLIM_INFINITY
&&
644 i
<= cputime_to_secs(timer
->it
.cpu
.expires
.cpu
))
646 p
->signal
->cputime_expires
.prof_exp
=
647 timer
->it
.cpu
.expires
.cpu
;
650 p
->signal
->cputime_expires
.sched_exp
=
651 timer
->it
.cpu
.expires
.sched
;
657 spin_unlock(&p
->sighand
->siglock
);
661 * The timer is locked, fire it and arrange for its reload.
663 static void cpu_timer_fire(struct k_itimer
*timer
)
665 if (unlikely(timer
->sigq
== NULL
)) {
667 * This a special case for clock_nanosleep,
668 * not a normal timer from sys_timer_create.
670 wake_up_process(timer
->it_process
);
671 timer
->it
.cpu
.expires
.sched
= 0;
672 } else if (timer
->it
.cpu
.incr
.sched
== 0) {
674 * One-shot timer. Clear it as soon as it's fired.
676 posix_timer_event(timer
, 0);
677 timer
->it
.cpu
.expires
.sched
= 0;
678 } else if (posix_timer_event(timer
, ++timer
->it_requeue_pending
)) {
680 * The signal did not get queued because the signal
681 * was ignored, so we won't get any callback to
682 * reload the timer. But we need to keep it
683 * ticking in case the signal is deliverable next time.
685 posix_cpu_timer_schedule(timer
);
690 * Guts of sys_timer_settime for CPU timers.
691 * This is called with the timer locked and interrupts disabled.
692 * If we return TIMER_RETRY, it's necessary to release the timer's lock
693 * and try again. (This happens when the timer is in the middle of firing.)
695 int posix_cpu_timer_set(struct k_itimer
*timer
, int flags
,
696 struct itimerspec
*new, struct itimerspec
*old
)
698 struct task_struct
*p
= timer
->it
.cpu
.task
;
699 union cpu_time_count old_expires
, new_expires
, val
;
702 if (unlikely(p
== NULL
)) {
704 * Timer refers to a dead task's clock.
709 new_expires
= timespec_to_sample(timer
->it_clock
, &new->it_value
);
711 read_lock(&tasklist_lock
);
713 * We need the tasklist_lock to protect against reaping that
714 * clears p->signal. If p has just been reaped, we can no
715 * longer get any information about it at all.
717 if (unlikely(p
->signal
== NULL
)) {
718 read_unlock(&tasklist_lock
);
720 timer
->it
.cpu
.task
= NULL
;
725 * Disarm any old timer after extracting its expiry time.
727 BUG_ON(!irqs_disabled());
730 spin_lock(&p
->sighand
->siglock
);
731 old_expires
= timer
->it
.cpu
.expires
;
732 if (unlikely(timer
->it
.cpu
.firing
)) {
733 timer
->it
.cpu
.firing
= -1;
736 list_del_init(&timer
->it
.cpu
.entry
);
737 spin_unlock(&p
->sighand
->siglock
);
740 * We need to sample the current value to convert the new
741 * value from to relative and absolute, and to convert the
742 * old value from absolute to relative. To set a process
743 * timer, we need a sample to balance the thread expiry
744 * times (in arm_timer). With an absolute time, we must
745 * check if it's already passed. In short, we need a sample.
747 if (CPUCLOCK_PERTHREAD(timer
->it_clock
)) {
748 cpu_clock_sample(timer
->it_clock
, p
, &val
);
750 cpu_clock_sample_group(timer
->it_clock
, p
, &val
);
754 if (old_expires
.sched
== 0) {
755 old
->it_value
.tv_sec
= 0;
756 old
->it_value
.tv_nsec
= 0;
759 * Update the timer in case it has
760 * overrun already. If it has,
761 * we'll report it as having overrun
762 * and with the next reloaded timer
763 * already ticking, though we are
764 * swallowing that pending
765 * notification here to install the
768 bump_cpu_timer(timer
, val
);
769 if (cpu_time_before(timer
->it_clock
, val
,
770 timer
->it
.cpu
.expires
)) {
771 old_expires
= cpu_time_sub(
773 timer
->it
.cpu
.expires
, val
);
774 sample_to_timespec(timer
->it_clock
,
778 old
->it_value
.tv_nsec
= 1;
779 old
->it_value
.tv_sec
= 0;
786 * We are colliding with the timer actually firing.
787 * Punt after filling in the timer's old value, and
788 * disable this firing since we are already reporting
789 * it as an overrun (thanks to bump_cpu_timer above).
791 read_unlock(&tasklist_lock
);
795 if (new_expires
.sched
!= 0 && !(flags
& TIMER_ABSTIME
)) {
796 cpu_time_add(timer
->it_clock
, &new_expires
, val
);
800 * Install the new expiry time (or zero).
801 * For a timer with no notification action, we don't actually
802 * arm the timer (we'll just fake it for timer_gettime).
804 timer
->it
.cpu
.expires
= new_expires
;
805 if (new_expires
.sched
!= 0 &&
806 (timer
->it_sigev_notify
& ~SIGEV_THREAD_ID
) != SIGEV_NONE
&&
807 cpu_time_before(timer
->it_clock
, val
, new_expires
)) {
808 arm_timer(timer
, val
);
811 read_unlock(&tasklist_lock
);
814 * Install the new reload setting, and
815 * set up the signal and overrun bookkeeping.
817 timer
->it
.cpu
.incr
= timespec_to_sample(timer
->it_clock
,
821 * This acts as a modification timestamp for the timer,
822 * so any automatic reload attempt will punt on seeing
823 * that we have reset the timer manually.
825 timer
->it_requeue_pending
= (timer
->it_requeue_pending
+ 2) &
827 timer
->it_overrun_last
= 0;
828 timer
->it_overrun
= -1;
830 if (new_expires
.sched
!= 0 &&
831 (timer
->it_sigev_notify
& ~SIGEV_THREAD_ID
) != SIGEV_NONE
&&
832 !cpu_time_before(timer
->it_clock
, val
, new_expires
)) {
834 * The designated time already passed, so we notify
835 * immediately, even if the thread never runs to
836 * accumulate more time on this clock.
838 cpu_timer_fire(timer
);
844 sample_to_timespec(timer
->it_clock
,
845 timer
->it
.cpu
.incr
, &old
->it_interval
);
850 void posix_cpu_timer_get(struct k_itimer
*timer
, struct itimerspec
*itp
)
852 union cpu_time_count now
;
853 struct task_struct
*p
= timer
->it
.cpu
.task
;
857 * Easy part: convert the reload time.
859 sample_to_timespec(timer
->it_clock
,
860 timer
->it
.cpu
.incr
, &itp
->it_interval
);
862 if (timer
->it
.cpu
.expires
.sched
== 0) { /* Timer not armed at all. */
863 itp
->it_value
.tv_sec
= itp
->it_value
.tv_nsec
= 0;
867 if (unlikely(p
== NULL
)) {
869 * This task already died and the timer will never fire.
870 * In this case, expires is actually the dead value.
873 sample_to_timespec(timer
->it_clock
, timer
->it
.cpu
.expires
,
879 * Sample the clock to take the difference with the expiry time.
881 if (CPUCLOCK_PERTHREAD(timer
->it_clock
)) {
882 cpu_clock_sample(timer
->it_clock
, p
, &now
);
883 clear_dead
= p
->exit_state
;
885 read_lock(&tasklist_lock
);
886 if (unlikely(p
->signal
== NULL
)) {
888 * The process has been reaped.
889 * We can't even collect a sample any more.
890 * Call the timer disarmed, nothing else to do.
893 timer
->it
.cpu
.task
= NULL
;
894 timer
->it
.cpu
.expires
.sched
= 0;
895 read_unlock(&tasklist_lock
);
898 cpu_clock_sample_group(timer
->it_clock
, p
, &now
);
899 clear_dead
= (unlikely(p
->exit_state
) &&
900 thread_group_empty(p
));
902 read_unlock(&tasklist_lock
);
905 if ((timer
->it_sigev_notify
& ~SIGEV_THREAD_ID
) == SIGEV_NONE
) {
906 if (timer
->it
.cpu
.incr
.sched
== 0 &&
907 cpu_time_before(timer
->it_clock
,
908 timer
->it
.cpu
.expires
, now
)) {
910 * Do-nothing timer expired and has no reload,
911 * so it's as if it was never set.
913 timer
->it
.cpu
.expires
.sched
= 0;
914 itp
->it_value
.tv_sec
= itp
->it_value
.tv_nsec
= 0;
918 * Account for any expirations and reloads that should
921 bump_cpu_timer(timer
, now
);
924 if (unlikely(clear_dead
)) {
926 * We've noticed that the thread is dead, but
927 * not yet reaped. Take this opportunity to
930 clear_dead_task(timer
, now
);
934 if (cpu_time_before(timer
->it_clock
, now
, timer
->it
.cpu
.expires
)) {
935 sample_to_timespec(timer
->it_clock
,
936 cpu_time_sub(timer
->it_clock
,
937 timer
->it
.cpu
.expires
, now
),
941 * The timer should have expired already, but the firing
942 * hasn't taken place yet. Say it's just about to expire.
944 itp
->it_value
.tv_nsec
= 1;
945 itp
->it_value
.tv_sec
= 0;
950 * Check for any per-thread CPU timers that have fired and move them off
951 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
952 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
954 static void check_thread_timers(struct task_struct
*tsk
,
955 struct list_head
*firing
)
958 struct list_head
*timers
= tsk
->cpu_timers
;
959 struct signal_struct
*const sig
= tsk
->signal
;
962 tsk
->cputime_expires
.prof_exp
= cputime_zero
;
963 while (!list_empty(timers
)) {
964 struct cpu_timer_list
*t
= list_first_entry(timers
,
965 struct cpu_timer_list
,
967 if (!--maxfire
|| cputime_lt(prof_ticks(tsk
), t
->expires
.cpu
)) {
968 tsk
->cputime_expires
.prof_exp
= t
->expires
.cpu
;
972 list_move_tail(&t
->entry
, firing
);
977 tsk
->cputime_expires
.virt_exp
= cputime_zero
;
978 while (!list_empty(timers
)) {
979 struct cpu_timer_list
*t
= list_first_entry(timers
,
980 struct cpu_timer_list
,
982 if (!--maxfire
|| cputime_lt(virt_ticks(tsk
), t
->expires
.cpu
)) {
983 tsk
->cputime_expires
.virt_exp
= t
->expires
.cpu
;
987 list_move_tail(&t
->entry
, firing
);
992 tsk
->cputime_expires
.sched_exp
= 0;
993 while (!list_empty(timers
)) {
994 struct cpu_timer_list
*t
= list_first_entry(timers
,
995 struct cpu_timer_list
,
997 if (!--maxfire
|| tsk
->se
.sum_exec_runtime
< t
->expires
.sched
) {
998 tsk
->cputime_expires
.sched_exp
= t
->expires
.sched
;
1002 list_move_tail(&t
->entry
, firing
);
1006 * Check for the special case thread timers.
1008 if (sig
->rlim
[RLIMIT_RTTIME
].rlim_cur
!= RLIM_INFINITY
) {
1009 unsigned long hard
= sig
->rlim
[RLIMIT_RTTIME
].rlim_max
;
1010 unsigned long *soft
= &sig
->rlim
[RLIMIT_RTTIME
].rlim_cur
;
1012 if (hard
!= RLIM_INFINITY
&&
1013 tsk
->rt
.timeout
> DIV_ROUND_UP(hard
, USEC_PER_SEC
/HZ
)) {
1015 * At the hard limit, we just die.
1016 * No need to calculate anything else now.
1018 __group_send_sig_info(SIGKILL
, SEND_SIG_PRIV
, tsk
);
1021 if (tsk
->rt
.timeout
> DIV_ROUND_UP(*soft
, USEC_PER_SEC
/HZ
)) {
1023 * At the soft limit, send a SIGXCPU every second.
1025 if (sig
->rlim
[RLIMIT_RTTIME
].rlim_cur
1026 < sig
->rlim
[RLIMIT_RTTIME
].rlim_max
) {
1027 sig
->rlim
[RLIMIT_RTTIME
].rlim_cur
+=
1031 "RT Watchdog Timeout: %s[%d]\n",
1032 tsk
->comm
, task_pid_nr(tsk
));
1033 __group_send_sig_info(SIGXCPU
, SEND_SIG_PRIV
, tsk
);
1039 * Check for any per-thread CPU timers that have fired and move them
1040 * off the tsk->*_timers list onto the firing list. Per-thread timers
1041 * have already been taken off.
1043 static void check_process_timers(struct task_struct
*tsk
,
1044 struct list_head
*firing
)
1047 struct signal_struct
*const sig
= tsk
->signal
;
1048 cputime_t utime
, ptime
, virt_expires
, prof_expires
;
1049 unsigned long long sum_sched_runtime
, sched_expires
;
1050 struct list_head
*timers
= sig
->cpu_timers
;
1051 struct task_cputime cputime
;
1054 * Don't sample the current process CPU clocks if there are no timers.
1056 if (list_empty(&timers
[CPUCLOCK_PROF
]) &&
1057 cputime_eq(sig
->it_prof_expires
, cputime_zero
) &&
1058 sig
->rlim
[RLIMIT_CPU
].rlim_cur
== RLIM_INFINITY
&&
1059 list_empty(&timers
[CPUCLOCK_VIRT
]) &&
1060 cputime_eq(sig
->it_virt_expires
, cputime_zero
) &&
1061 list_empty(&timers
[CPUCLOCK_SCHED
]))
1065 * Collect the current process totals.
1067 thread_group_cputime(tsk
, &cputime
);
1068 utime
= cputime
.utime
;
1069 ptime
= cputime_add(utime
, cputime
.stime
);
1070 sum_sched_runtime
= cputime
.sum_exec_runtime
;
1072 prof_expires
= cputime_zero
;
1073 while (!list_empty(timers
)) {
1074 struct cpu_timer_list
*tl
= list_first_entry(timers
,
1075 struct cpu_timer_list
,
1077 if (!--maxfire
|| cputime_lt(ptime
, tl
->expires
.cpu
)) {
1078 prof_expires
= tl
->expires
.cpu
;
1082 list_move_tail(&tl
->entry
, firing
);
1087 virt_expires
= cputime_zero
;
1088 while (!list_empty(timers
)) {
1089 struct cpu_timer_list
*tl
= list_first_entry(timers
,
1090 struct cpu_timer_list
,
1092 if (!--maxfire
|| cputime_lt(utime
, tl
->expires
.cpu
)) {
1093 virt_expires
= tl
->expires
.cpu
;
1097 list_move_tail(&tl
->entry
, firing
);
1103 while (!list_empty(timers
)) {
1104 struct cpu_timer_list
*tl
= list_first_entry(timers
,
1105 struct cpu_timer_list
,
1107 if (!--maxfire
|| sum_sched_runtime
< tl
->expires
.sched
) {
1108 sched_expires
= tl
->expires
.sched
;
1112 list_move_tail(&tl
->entry
, firing
);
1116 * Check for the special case process timers.
1118 if (!cputime_eq(sig
->it_prof_expires
, cputime_zero
)) {
1119 if (cputime_ge(ptime
, sig
->it_prof_expires
)) {
1120 /* ITIMER_PROF fires and reloads. */
1121 sig
->it_prof_expires
= sig
->it_prof_incr
;
1122 if (!cputime_eq(sig
->it_prof_expires
, cputime_zero
)) {
1123 sig
->it_prof_expires
= cputime_add(
1124 sig
->it_prof_expires
, ptime
);
1126 __group_send_sig_info(SIGPROF
, SEND_SIG_PRIV
, tsk
);
1128 if (!cputime_eq(sig
->it_prof_expires
, cputime_zero
) &&
1129 (cputime_eq(prof_expires
, cputime_zero
) ||
1130 cputime_lt(sig
->it_prof_expires
, prof_expires
))) {
1131 prof_expires
= sig
->it_prof_expires
;
1134 if (!cputime_eq(sig
->it_virt_expires
, cputime_zero
)) {
1135 if (cputime_ge(utime
, sig
->it_virt_expires
)) {
1136 /* ITIMER_VIRTUAL fires and reloads. */
1137 sig
->it_virt_expires
= sig
->it_virt_incr
;
1138 if (!cputime_eq(sig
->it_virt_expires
, cputime_zero
)) {
1139 sig
->it_virt_expires
= cputime_add(
1140 sig
->it_virt_expires
, utime
);
1142 __group_send_sig_info(SIGVTALRM
, SEND_SIG_PRIV
, tsk
);
1144 if (!cputime_eq(sig
->it_virt_expires
, cputime_zero
) &&
1145 (cputime_eq(virt_expires
, cputime_zero
) ||
1146 cputime_lt(sig
->it_virt_expires
, virt_expires
))) {
1147 virt_expires
= sig
->it_virt_expires
;
1150 if (sig
->rlim
[RLIMIT_CPU
].rlim_cur
!= RLIM_INFINITY
) {
1151 unsigned long psecs
= cputime_to_secs(ptime
);
1153 if (psecs
>= sig
->rlim
[RLIMIT_CPU
].rlim_max
) {
1155 * At the hard limit, we just die.
1156 * No need to calculate anything else now.
1158 __group_send_sig_info(SIGKILL
, SEND_SIG_PRIV
, tsk
);
1161 if (psecs
>= sig
->rlim
[RLIMIT_CPU
].rlim_cur
) {
1163 * At the soft limit, send a SIGXCPU every second.
1165 __group_send_sig_info(SIGXCPU
, SEND_SIG_PRIV
, tsk
);
1166 if (sig
->rlim
[RLIMIT_CPU
].rlim_cur
1167 < sig
->rlim
[RLIMIT_CPU
].rlim_max
) {
1168 sig
->rlim
[RLIMIT_CPU
].rlim_cur
++;
1171 x
= secs_to_cputime(sig
->rlim
[RLIMIT_CPU
].rlim_cur
);
1172 if (cputime_eq(prof_expires
, cputime_zero
) ||
1173 cputime_lt(x
, prof_expires
)) {
1178 if (!cputime_eq(prof_expires
, cputime_zero
) &&
1179 (cputime_eq(sig
->cputime_expires
.prof_exp
, cputime_zero
) ||
1180 cputime_gt(sig
->cputime_expires
.prof_exp
, prof_expires
)))
1181 sig
->cputime_expires
.prof_exp
= prof_expires
;
1182 if (!cputime_eq(virt_expires
, cputime_zero
) &&
1183 (cputime_eq(sig
->cputime_expires
.virt_exp
, cputime_zero
) ||
1184 cputime_gt(sig
->cputime_expires
.virt_exp
, virt_expires
)))
1185 sig
->cputime_expires
.virt_exp
= virt_expires
;
1186 if (sched_expires
!= 0 &&
1187 (sig
->cputime_expires
.sched_exp
== 0 ||
1188 sig
->cputime_expires
.sched_exp
> sched_expires
))
1189 sig
->cputime_expires
.sched_exp
= sched_expires
;
1193 * This is called from the signal code (via do_schedule_next_timer)
1194 * when the last timer signal was delivered and we have to reload the timer.
1196 void posix_cpu_timer_schedule(struct k_itimer
*timer
)
1198 struct task_struct
*p
= timer
->it
.cpu
.task
;
1199 union cpu_time_count now
;
1201 if (unlikely(p
== NULL
))
1203 * The task was cleaned up already, no future firings.
1208 * Fetch the current sample and update the timer's expiry time.
1210 if (CPUCLOCK_PERTHREAD(timer
->it_clock
)) {
1211 cpu_clock_sample(timer
->it_clock
, p
, &now
);
1212 bump_cpu_timer(timer
, now
);
1213 if (unlikely(p
->exit_state
)) {
1214 clear_dead_task(timer
, now
);
1217 read_lock(&tasklist_lock
); /* arm_timer needs it. */
1219 read_lock(&tasklist_lock
);
1220 if (unlikely(p
->signal
== NULL
)) {
1222 * The process has been reaped.
1223 * We can't even collect a sample any more.
1226 timer
->it
.cpu
.task
= p
= NULL
;
1227 timer
->it
.cpu
.expires
.sched
= 0;
1229 } else if (unlikely(p
->exit_state
) && thread_group_empty(p
)) {
1231 * We've noticed that the thread is dead, but
1232 * not yet reaped. Take this opportunity to
1233 * drop our task ref.
1235 clear_dead_task(timer
, now
);
1238 cpu_clock_sample_group(timer
->it_clock
, p
, &now
);
1239 bump_cpu_timer(timer
, now
);
1240 /* Leave the tasklist_lock locked for the call below. */
1244 * Now re-arm for the new expiry time.
1246 arm_timer(timer
, now
);
1249 read_unlock(&tasklist_lock
);
1252 timer
->it_overrun_last
= timer
->it_overrun
;
1253 timer
->it_overrun
= -1;
1254 ++timer
->it_requeue_pending
;
1258 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1260 * @cputime: The struct to compare.
1262 * Checks @cputime to see if all fields are zero. Returns true if all fields
1263 * are zero, false if any field is nonzero.
1265 static inline int task_cputime_zero(const struct task_cputime
*cputime
)
1267 if (cputime_eq(cputime
->utime
, cputime_zero
) &&
1268 cputime_eq(cputime
->stime
, cputime_zero
) &&
1269 cputime
->sum_exec_runtime
== 0)
1275 * task_cputime_expired - Compare two task_cputime entities.
1277 * @sample: The task_cputime structure to be checked for expiration.
1278 * @expires: Expiration times, against which @sample will be checked.
1280 * Checks @sample against @expires to see if any field of @sample has expired.
1281 * Returns true if any field of the former is greater than the corresponding
1282 * field of the latter if the latter field is set. Otherwise returns false.
1284 static inline int task_cputime_expired(const struct task_cputime
*sample
,
1285 const struct task_cputime
*expires
)
1287 if (!cputime_eq(expires
->utime
, cputime_zero
) &&
1288 cputime_ge(sample
->utime
, expires
->utime
))
1290 if (!cputime_eq(expires
->stime
, cputime_zero
) &&
1291 cputime_ge(cputime_add(sample
->utime
, sample
->stime
),
1294 if (expires
->sum_exec_runtime
!= 0 &&
1295 sample
->sum_exec_runtime
>= expires
->sum_exec_runtime
)
1301 * fastpath_timer_check - POSIX CPU timers fast path.
1303 * @tsk: The task (thread) being checked.
1305 * Check the task and thread group timers. If both are zero (there are no
1306 * timers set) return false. Otherwise snapshot the task and thread group
1307 * timers and compare them with the corresponding expiration times. Return
1308 * true if a timer has expired, else return false.
1310 static inline int fastpath_timer_check(struct task_struct
*tsk
)
1312 struct signal_struct
*sig
;
1314 /* tsk == current, ensure it is safe to use ->signal/sighand */
1315 if (unlikely(tsk
->exit_state
))
1318 if (!task_cputime_zero(&tsk
->cputime_expires
)) {
1319 struct task_cputime task_sample
= {
1320 .utime
= tsk
->utime
,
1321 .stime
= tsk
->stime
,
1322 .sum_exec_runtime
= tsk
->se
.sum_exec_runtime
1325 if (task_cputime_expired(&task_sample
, &tsk
->cputime_expires
))
1330 if (!task_cputime_zero(&sig
->cputime_expires
)) {
1331 struct task_cputime group_sample
;
1333 thread_group_cputime(tsk
, &group_sample
);
1334 if (task_cputime_expired(&group_sample
, &sig
->cputime_expires
))
1341 * This is called from the timer interrupt handler. The irq handler has
1342 * already updated our counts. We need to check if any timers fire now.
1343 * Interrupts are disabled.
1345 void run_posix_cpu_timers(struct task_struct
*tsk
)
1348 struct k_itimer
*timer
, *next
;
1350 BUG_ON(!irqs_disabled());
1353 * The fast path checks that there are no expired thread or thread
1354 * group timers. If that's so, just return.
1356 if (!fastpath_timer_check(tsk
))
1359 spin_lock(&tsk
->sighand
->siglock
);
1361 * Here we take off tsk->signal->cpu_timers[N] and
1362 * tsk->cpu_timers[N] all the timers that are firing, and
1363 * put them on the firing list.
1365 check_thread_timers(tsk
, &firing
);
1366 check_process_timers(tsk
, &firing
);
1369 * We must release these locks before taking any timer's lock.
1370 * There is a potential race with timer deletion here, as the
1371 * siglock now protects our private firing list. We have set
1372 * the firing flag in each timer, so that a deletion attempt
1373 * that gets the timer lock before we do will give it up and
1374 * spin until we've taken care of that timer below.
1376 spin_unlock(&tsk
->sighand
->siglock
);
1379 * Now that all the timers on our list have the firing flag,
1380 * noone will touch their list entries but us. We'll take
1381 * each timer's lock before clearing its firing flag, so no
1382 * timer call will interfere.
1384 list_for_each_entry_safe(timer
, next
, &firing
, it
.cpu
.entry
) {
1386 spin_lock(&timer
->it_lock
);
1387 list_del_init(&timer
->it
.cpu
.entry
);
1388 firing
= timer
->it
.cpu
.firing
;
1389 timer
->it
.cpu
.firing
= 0;
1391 * The firing flag is -1 if we collided with a reset
1392 * of the timer, which already reported this
1393 * almost-firing as an overrun. So don't generate an event.
1395 if (likely(firing
>= 0)) {
1396 cpu_timer_fire(timer
);
1398 spin_unlock(&timer
->it_lock
);
1403 * Set one of the process-wide special case CPU timers.
1404 * The tsk->sighand->siglock must be held by the caller.
1405 * The *newval argument is relative and we update it to be absolute, *oldval
1406 * is absolute and we update it to be relative.
1408 void set_process_cpu_timer(struct task_struct
*tsk
, unsigned int clock_idx
,
1409 cputime_t
*newval
, cputime_t
*oldval
)
1411 union cpu_time_count now
;
1412 struct list_head
*head
;
1414 BUG_ON(clock_idx
== CPUCLOCK_SCHED
);
1415 cpu_clock_sample_group(clock_idx
, tsk
, &now
);
1418 if (!cputime_eq(*oldval
, cputime_zero
)) {
1419 if (cputime_le(*oldval
, now
.cpu
)) {
1420 /* Just about to fire. */
1421 *oldval
= jiffies_to_cputime(1);
1423 *oldval
= cputime_sub(*oldval
, now
.cpu
);
1427 if (cputime_eq(*newval
, cputime_zero
))
1429 *newval
= cputime_add(*newval
, now
.cpu
);
1432 * If the RLIMIT_CPU timer will expire before the
1433 * ITIMER_PROF timer, we have nothing else to do.
1435 if (tsk
->signal
->rlim
[RLIMIT_CPU
].rlim_cur
1436 < cputime_to_secs(*newval
))
1441 * Check whether there are any process timers already set to fire
1442 * before this one. If so, we don't have anything more to do.
1444 head
= &tsk
->signal
->cpu_timers
[clock_idx
];
1445 if (list_empty(head
) ||
1446 cputime_ge(list_first_entry(head
,
1447 struct cpu_timer_list
, entry
)->expires
.cpu
,
1449 switch (clock_idx
) {
1451 tsk
->signal
->cputime_expires
.prof_exp
= *newval
;
1454 tsk
->signal
->cputime_expires
.virt_exp
= *newval
;
1460 static int do_cpu_nanosleep(const clockid_t which_clock
, int flags
,
1461 struct timespec
*rqtp
, struct itimerspec
*it
)
1463 struct k_itimer timer
;
1467 * Set up a temporary timer and then wait for it to go off.
1469 memset(&timer
, 0, sizeof timer
);
1470 spin_lock_init(&timer
.it_lock
);
1471 timer
.it_clock
= which_clock
;
1472 timer
.it_overrun
= -1;
1473 error
= posix_cpu_timer_create(&timer
);
1474 timer
.it_process
= current
;
1476 static struct itimerspec zero_it
;
1478 memset(it
, 0, sizeof *it
);
1479 it
->it_value
= *rqtp
;
1481 spin_lock_irq(&timer
.it_lock
);
1482 error
= posix_cpu_timer_set(&timer
, flags
, it
, NULL
);
1484 spin_unlock_irq(&timer
.it_lock
);
1488 while (!signal_pending(current
)) {
1489 if (timer
.it
.cpu
.expires
.sched
== 0) {
1491 * Our timer fired and was reset.
1493 spin_unlock_irq(&timer
.it_lock
);
1498 * Block until cpu_timer_fire (or a signal) wakes us.
1500 __set_current_state(TASK_INTERRUPTIBLE
);
1501 spin_unlock_irq(&timer
.it_lock
);
1503 spin_lock_irq(&timer
.it_lock
);
1507 * We were interrupted by a signal.
1509 sample_to_timespec(which_clock
, timer
.it
.cpu
.expires
, rqtp
);
1510 posix_cpu_timer_set(&timer
, 0, &zero_it
, it
);
1511 spin_unlock_irq(&timer
.it_lock
);
1513 if ((it
->it_value
.tv_sec
| it
->it_value
.tv_nsec
) == 0) {
1515 * It actually did fire already.
1520 error
= -ERESTART_RESTARTBLOCK
;
1526 int posix_cpu_nsleep(const clockid_t which_clock
, int flags
,
1527 struct timespec
*rqtp
, struct timespec __user
*rmtp
)
1529 struct restart_block
*restart_block
=
1530 ¤t_thread_info()->restart_block
;
1531 struct itimerspec it
;
1535 * Diagnose required errors first.
1537 if (CPUCLOCK_PERTHREAD(which_clock
) &&
1538 (CPUCLOCK_PID(which_clock
) == 0 ||
1539 CPUCLOCK_PID(which_clock
) == current
->pid
))
1542 error
= do_cpu_nanosleep(which_clock
, flags
, rqtp
, &it
);
1544 if (error
== -ERESTART_RESTARTBLOCK
) {
1546 if (flags
& TIMER_ABSTIME
)
1547 return -ERESTARTNOHAND
;
1549 * Report back to the user the time still remaining.
1551 if (rmtp
!= NULL
&& copy_to_user(rmtp
, &it
.it_value
, sizeof *rmtp
))
1554 restart_block
->fn
= posix_cpu_nsleep_restart
;
1555 restart_block
->arg0
= which_clock
;
1556 restart_block
->arg1
= (unsigned long) rmtp
;
1557 restart_block
->arg2
= rqtp
->tv_sec
;
1558 restart_block
->arg3
= rqtp
->tv_nsec
;
1563 long posix_cpu_nsleep_restart(struct restart_block
*restart_block
)
1565 clockid_t which_clock
= restart_block
->arg0
;
1566 struct timespec __user
*rmtp
;
1568 struct itimerspec it
;
1571 rmtp
= (struct timespec __user
*) restart_block
->arg1
;
1572 t
.tv_sec
= restart_block
->arg2
;
1573 t
.tv_nsec
= restart_block
->arg3
;
1575 restart_block
->fn
= do_no_restart_syscall
;
1576 error
= do_cpu_nanosleep(which_clock
, TIMER_ABSTIME
, &t
, &it
);
1578 if (error
== -ERESTART_RESTARTBLOCK
) {
1580 * Report back to the user the time still remaining.
1582 if (rmtp
!= NULL
&& copy_to_user(rmtp
, &it
.it_value
, sizeof *rmtp
))
1585 restart_block
->fn
= posix_cpu_nsleep_restart
;
1586 restart_block
->arg0
= which_clock
;
1587 restart_block
->arg1
= (unsigned long) rmtp
;
1588 restart_block
->arg2
= t
.tv_sec
;
1589 restart_block
->arg3
= t
.tv_nsec
;
1596 #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1597 #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1599 static int process_cpu_clock_getres(const clockid_t which_clock
,
1600 struct timespec
*tp
)
1602 return posix_cpu_clock_getres(PROCESS_CLOCK
, tp
);
1604 static int process_cpu_clock_get(const clockid_t which_clock
,
1605 struct timespec
*tp
)
1607 return posix_cpu_clock_get(PROCESS_CLOCK
, tp
);
1609 static int process_cpu_timer_create(struct k_itimer
*timer
)
1611 timer
->it_clock
= PROCESS_CLOCK
;
1612 return posix_cpu_timer_create(timer
);
1614 static int process_cpu_nsleep(const clockid_t which_clock
, int flags
,
1615 struct timespec
*rqtp
,
1616 struct timespec __user
*rmtp
)
1618 return posix_cpu_nsleep(PROCESS_CLOCK
, flags
, rqtp
, rmtp
);
1620 static long process_cpu_nsleep_restart(struct restart_block
*restart_block
)
1624 static int thread_cpu_clock_getres(const clockid_t which_clock
,
1625 struct timespec
*tp
)
1627 return posix_cpu_clock_getres(THREAD_CLOCK
, tp
);
1629 static int thread_cpu_clock_get(const clockid_t which_clock
,
1630 struct timespec
*tp
)
1632 return posix_cpu_clock_get(THREAD_CLOCK
, tp
);
1634 static int thread_cpu_timer_create(struct k_itimer
*timer
)
1636 timer
->it_clock
= THREAD_CLOCK
;
1637 return posix_cpu_timer_create(timer
);
1639 static int thread_cpu_nsleep(const clockid_t which_clock
, int flags
,
1640 struct timespec
*rqtp
, struct timespec __user
*rmtp
)
1644 static long thread_cpu_nsleep_restart(struct restart_block
*restart_block
)
1649 static __init
int init_posix_cpu_timers(void)
1651 struct k_clock process
= {
1652 .clock_getres
= process_cpu_clock_getres
,
1653 .clock_get
= process_cpu_clock_get
,
1654 .clock_set
= do_posix_clock_nosettime
,
1655 .timer_create
= process_cpu_timer_create
,
1656 .nsleep
= process_cpu_nsleep
,
1657 .nsleep_restart
= process_cpu_nsleep_restart
,
1659 struct k_clock thread
= {
1660 .clock_getres
= thread_cpu_clock_getres
,
1661 .clock_get
= thread_cpu_clock_get
,
1662 .clock_set
= do_posix_clock_nosettime
,
1663 .timer_create
= thread_cpu_timer_create
,
1664 .nsleep
= thread_cpu_nsleep
,
1665 .nsleep_restart
= thread_cpu_nsleep_restart
,
1668 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID
, &process
);
1669 register_posix_clock(CLOCK_THREAD_CPUTIME_ID
, &thread
);
1673 __initcall(init_posix_cpu_timers
);