2 * Implement CPU time clocks for the POSIX clock interface.
5 #include <linux/sched.h>
6 #include <linux/posix-timers.h>
7 #include <asm/uaccess.h>
8 #include <linux/errno.h>
10 static int check_clock(clockid_t which_clock
)
13 struct task_struct
*p
;
14 const pid_t pid
= CPUCLOCK_PID(which_clock
);
16 if (CPUCLOCK_WHICH(which_clock
) >= CPUCLOCK_MAX
)
22 read_lock(&tasklist_lock
);
23 p
= find_task_by_pid(pid
);
24 if (!p
|| (CPUCLOCK_PERTHREAD(which_clock
) ?
25 p
->tgid
!= current
->tgid
: p
->tgid
!= pid
)) {
28 read_unlock(&tasklist_lock
);
33 static inline union cpu_time_count
34 timespec_to_sample(clockid_t which_clock
, const struct timespec
*tp
)
36 union cpu_time_count ret
;
37 ret
.sched
= 0; /* high half always zero when .cpu used */
38 if (CPUCLOCK_WHICH(which_clock
) == CPUCLOCK_SCHED
) {
39 ret
.sched
= tp
->tv_sec
* NSEC_PER_SEC
+ tp
->tv_nsec
;
41 ret
.cpu
= timespec_to_cputime(tp
);
46 static void sample_to_timespec(clockid_t which_clock
,
47 union cpu_time_count cpu
,
50 if (CPUCLOCK_WHICH(which_clock
) == CPUCLOCK_SCHED
) {
51 tp
->tv_sec
= div_long_long_rem(cpu
.sched
,
52 NSEC_PER_SEC
, &tp
->tv_nsec
);
54 cputime_to_timespec(cpu
.cpu
, tp
);
58 static inline int cpu_time_before(clockid_t which_clock
,
59 union cpu_time_count now
,
60 union cpu_time_count then
)
62 if (CPUCLOCK_WHICH(which_clock
) == CPUCLOCK_SCHED
) {
63 return now
.sched
< then
.sched
;
65 return cputime_lt(now
.cpu
, then
.cpu
);
68 static inline void cpu_time_add(clockid_t which_clock
,
69 union cpu_time_count
*acc
,
70 union cpu_time_count val
)
72 if (CPUCLOCK_WHICH(which_clock
) == CPUCLOCK_SCHED
) {
73 acc
->sched
+= val
.sched
;
75 acc
->cpu
= cputime_add(acc
->cpu
, val
.cpu
);
78 static inline union cpu_time_count
cpu_time_sub(clockid_t which_clock
,
79 union cpu_time_count a
,
80 union cpu_time_count b
)
82 if (CPUCLOCK_WHICH(which_clock
) == CPUCLOCK_SCHED
) {
85 a
.cpu
= cputime_sub(a
.cpu
, b
.cpu
);
91 * Update expiry time from increment, and increase overrun count,
92 * given the current clock sample.
94 static void bump_cpu_timer(struct k_itimer
*timer
,
95 union cpu_time_count now
)
99 if (timer
->it
.cpu
.incr
.sched
== 0)
102 if (CPUCLOCK_WHICH(timer
->it_clock
) == CPUCLOCK_SCHED
) {
103 unsigned long long delta
, incr
;
105 if (now
.sched
< timer
->it
.cpu
.expires
.sched
)
107 incr
= timer
->it
.cpu
.incr
.sched
;
108 delta
= now
.sched
+ incr
- timer
->it
.cpu
.expires
.sched
;
109 /* Don't use (incr*2 < delta), incr*2 might overflow. */
110 for (i
= 0; incr
< delta
- incr
; i
++)
112 for (; i
>= 0; incr
>>= 1, i
--) {
115 timer
->it
.cpu
.expires
.sched
+= incr
;
116 timer
->it_overrun
+= 1 << i
;
120 cputime_t delta
, incr
;
122 if (cputime_lt(now
.cpu
, timer
->it
.cpu
.expires
.cpu
))
124 incr
= timer
->it
.cpu
.incr
.cpu
;
125 delta
= cputime_sub(cputime_add(now
.cpu
, incr
),
126 timer
->it
.cpu
.expires
.cpu
);
127 /* Don't use (incr*2 < delta), incr*2 might overflow. */
128 for (i
= 0; cputime_lt(incr
, cputime_sub(delta
, incr
)); i
++)
129 incr
= cputime_add(incr
, incr
);
130 for (; i
>= 0; incr
= cputime_halve(incr
), i
--) {
131 if (cputime_lt(delta
, incr
))
133 timer
->it
.cpu
.expires
.cpu
=
134 cputime_add(timer
->it
.cpu
.expires
.cpu
, incr
);
135 timer
->it_overrun
+= 1 << i
;
136 delta
= cputime_sub(delta
, incr
);
141 static inline cputime_t
prof_ticks(struct task_struct
*p
)
143 return cputime_add(p
->utime
, p
->stime
);
145 static inline cputime_t
virt_ticks(struct task_struct
*p
)
149 static inline unsigned long long sched_ns(struct task_struct
*p
)
151 return (p
== current
) ? current_sched_time(p
) : p
->sched_time
;
154 int posix_cpu_clock_getres(clockid_t which_clock
, struct timespec
*tp
)
156 int error
= check_clock(which_clock
);
159 tp
->tv_nsec
= ((NSEC_PER_SEC
+ HZ
- 1) / HZ
);
160 if (CPUCLOCK_WHICH(which_clock
) == CPUCLOCK_SCHED
) {
162 * If sched_clock is using a cycle counter, we
163 * don't have any idea of its true resolution
164 * exported, but it is much more than 1s/HZ.
172 int posix_cpu_clock_set(clockid_t which_clock
, const struct timespec
*tp
)
175 * You can never reset a CPU clock, but we check for other errors
176 * in the call before failing with EPERM.
178 int error
= check_clock(which_clock
);
187 * Sample a per-thread clock for the given task.
189 static int cpu_clock_sample(clockid_t which_clock
, struct task_struct
*p
,
190 union cpu_time_count
*cpu
)
192 switch (CPUCLOCK_WHICH(which_clock
)) {
196 cpu
->cpu
= prof_ticks(p
);
199 cpu
->cpu
= virt_ticks(p
);
202 cpu
->sched
= sched_ns(p
);
209 * Sample a process (thread group) clock for the given group_leader task.
210 * Must be called with tasklist_lock held for reading.
211 * Must be called with tasklist_lock held for reading, and p->sighand->siglock.
213 static int cpu_clock_sample_group_locked(unsigned int clock_idx
,
214 struct task_struct
*p
,
215 union cpu_time_count
*cpu
)
217 struct task_struct
*t
= p
;
222 cpu
->cpu
= cputime_add(p
->signal
->utime
, p
->signal
->stime
);
224 cpu
->cpu
= cputime_add(cpu
->cpu
, prof_ticks(t
));
229 cpu
->cpu
= p
->signal
->utime
;
231 cpu
->cpu
= cputime_add(cpu
->cpu
, virt_ticks(t
));
236 cpu
->sched
= p
->signal
->sched_time
;
237 /* Add in each other live thread. */
238 while ((t
= next_thread(t
)) != p
) {
239 cpu
->sched
+= t
->sched_time
;
241 if (p
->tgid
== current
->tgid
) {
243 * We're sampling ourselves, so include the
244 * cycles not yet banked. We still omit
245 * other threads running on other CPUs,
246 * so the total can always be behind as
247 * much as max(nthreads-1,ncpus) * (NSEC_PER_SEC/HZ).
249 cpu
->sched
+= current_sched_time(current
);
251 cpu
->sched
+= p
->sched_time
;
259 * Sample a process (thread group) clock for the given group_leader task.
260 * Must be called with tasklist_lock held for reading.
262 static int cpu_clock_sample_group(clockid_t which_clock
,
263 struct task_struct
*p
,
264 union cpu_time_count
*cpu
)
268 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
269 ret
= cpu_clock_sample_group_locked(CPUCLOCK_WHICH(which_clock
), p
,
271 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
276 int posix_cpu_clock_get(clockid_t which_clock
, struct timespec
*tp
)
278 const pid_t pid
= CPUCLOCK_PID(which_clock
);
280 union cpu_time_count rtn
;
284 * Special case constant value for our own clocks.
285 * We don't have to do any lookup to find ourselves.
287 if (CPUCLOCK_PERTHREAD(which_clock
)) {
289 * Sampling just ourselves we can do with no locking.
291 error
= cpu_clock_sample(which_clock
,
294 read_lock(&tasklist_lock
);
295 error
= cpu_clock_sample_group(which_clock
,
297 read_unlock(&tasklist_lock
);
301 * Find the given PID, and validate that the caller
302 * should be able to see it.
304 struct task_struct
*p
;
305 read_lock(&tasklist_lock
);
306 p
= find_task_by_pid(pid
);
308 if (CPUCLOCK_PERTHREAD(which_clock
)) {
309 if (p
->tgid
== current
->tgid
) {
310 error
= cpu_clock_sample(which_clock
,
313 } else if (p
->tgid
== pid
&& p
->signal
) {
314 error
= cpu_clock_sample_group(which_clock
,
318 read_unlock(&tasklist_lock
);
323 sample_to_timespec(which_clock
, rtn
, tp
);
329 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
330 * This is called from sys_timer_create with the new timer already locked.
332 int posix_cpu_timer_create(struct k_itimer
*new_timer
)
335 const pid_t pid
= CPUCLOCK_PID(new_timer
->it_clock
);
336 struct task_struct
*p
;
338 if (CPUCLOCK_WHICH(new_timer
->it_clock
) >= CPUCLOCK_MAX
)
341 INIT_LIST_HEAD(&new_timer
->it
.cpu
.entry
);
342 new_timer
->it
.cpu
.incr
.sched
= 0;
343 new_timer
->it
.cpu
.expires
.sched
= 0;
345 read_lock(&tasklist_lock
);
346 if (CPUCLOCK_PERTHREAD(new_timer
->it_clock
)) {
350 p
= find_task_by_pid(pid
);
351 if (p
&& p
->tgid
!= current
->tgid
)
356 p
= current
->group_leader
;
358 p
= find_task_by_pid(pid
);
359 if (p
&& p
->tgid
!= pid
)
363 new_timer
->it
.cpu
.task
= p
;
369 read_unlock(&tasklist_lock
);
375 * Clean up a CPU-clock timer that is about to be destroyed.
376 * This is called from timer deletion with the timer already locked.
377 * If we return TIMER_RETRY, it's necessary to release the timer's lock
378 * and try again. (This happens when the timer is in the middle of firing.)
380 int posix_cpu_timer_del(struct k_itimer
*timer
)
382 struct task_struct
*p
= timer
->it
.cpu
.task
;
385 if (likely(p
!= NULL
)) {
386 read_lock(&tasklist_lock
);
387 if (unlikely(p
->signal
== NULL
)) {
389 * We raced with the reaping of the task.
390 * The deletion should have cleared us off the list.
392 BUG_ON(!list_empty(&timer
->it
.cpu
.entry
));
394 spin_lock(&p
->sighand
->siglock
);
395 if (timer
->it
.cpu
.firing
)
398 list_del(&timer
->it
.cpu
.entry
);
399 spin_unlock(&p
->sighand
->siglock
);
401 read_unlock(&tasklist_lock
);
411 * Clean out CPU timers still ticking when a thread exited. The task
412 * pointer is cleared, and the expiry time is replaced with the residual
413 * time for later timer_gettime calls to return.
414 * This must be called with the siglock held.
416 static void cleanup_timers(struct list_head
*head
,
417 cputime_t utime
, cputime_t stime
,
418 unsigned long long sched_time
)
420 struct cpu_timer_list
*timer
, *next
;
421 cputime_t ptime
= cputime_add(utime
, stime
);
423 list_for_each_entry_safe(timer
, next
, head
, entry
) {
424 list_del_init(&timer
->entry
);
425 if (cputime_lt(timer
->expires
.cpu
, ptime
)) {
426 timer
->expires
.cpu
= cputime_zero
;
428 timer
->expires
.cpu
= cputime_sub(timer
->expires
.cpu
,
434 list_for_each_entry_safe(timer
, next
, head
, entry
) {
435 list_del_init(&timer
->entry
);
436 if (cputime_lt(timer
->expires
.cpu
, utime
)) {
437 timer
->expires
.cpu
= cputime_zero
;
439 timer
->expires
.cpu
= cputime_sub(timer
->expires
.cpu
,
445 list_for_each_entry_safe(timer
, next
, head
, entry
) {
446 list_del_init(&timer
->entry
);
447 if (timer
->expires
.sched
< sched_time
) {
448 timer
->expires
.sched
= 0;
450 timer
->expires
.sched
-= sched_time
;
456 * These are both called with the siglock held, when the current thread
457 * is being reaped. When the final (leader) thread in the group is reaped,
458 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
460 void posix_cpu_timers_exit(struct task_struct
*tsk
)
462 cleanup_timers(tsk
->cpu_timers
,
463 tsk
->utime
, tsk
->stime
, tsk
->sched_time
);
466 void posix_cpu_timers_exit_group(struct task_struct
*tsk
)
468 cleanup_timers(tsk
->signal
->cpu_timers
,
469 cputime_add(tsk
->utime
, tsk
->signal
->utime
),
470 cputime_add(tsk
->stime
, tsk
->signal
->stime
),
471 tsk
->sched_time
+ tsk
->signal
->sched_time
);
476 * Set the expiry times of all the threads in the process so one of them
477 * will go off before the process cumulative expiry total is reached.
479 static void process_timer_rebalance(struct task_struct
*p
,
480 unsigned int clock_idx
,
481 union cpu_time_count expires
,
482 union cpu_time_count val
)
484 cputime_t ticks
, left
;
485 unsigned long long ns
, nsleft
;
486 struct task_struct
*t
= p
;
487 unsigned int nthreads
= atomic_read(&p
->signal
->live
);
497 left
= cputime_div(cputime_sub(expires
.cpu
, val
.cpu
),
500 if (!unlikely(t
->flags
& PF_EXITING
)) {
501 ticks
= cputime_add(prof_ticks(t
), left
);
502 if (cputime_eq(t
->it_prof_expires
,
504 cputime_gt(t
->it_prof_expires
, ticks
)) {
505 t
->it_prof_expires
= ticks
;
512 left
= cputime_div(cputime_sub(expires
.cpu
, val
.cpu
),
515 if (!unlikely(t
->flags
& PF_EXITING
)) {
516 ticks
= cputime_add(virt_ticks(t
), left
);
517 if (cputime_eq(t
->it_virt_expires
,
519 cputime_gt(t
->it_virt_expires
, ticks
)) {
520 t
->it_virt_expires
= ticks
;
527 nsleft
= expires
.sched
- val
.sched
;
528 do_div(nsleft
, nthreads
);
530 if (!unlikely(t
->flags
& PF_EXITING
)) {
531 ns
= t
->sched_time
+ nsleft
;
532 if (t
->it_sched_expires
== 0 ||
533 t
->it_sched_expires
> ns
) {
534 t
->it_sched_expires
= ns
;
543 static void clear_dead_task(struct k_itimer
*timer
, union cpu_time_count now
)
546 * That's all for this thread or process.
547 * We leave our residual in expires to be reported.
549 put_task_struct(timer
->it
.cpu
.task
);
550 timer
->it
.cpu
.task
= NULL
;
551 timer
->it
.cpu
.expires
= cpu_time_sub(timer
->it_clock
,
552 timer
->it
.cpu
.expires
,
557 * Insert the timer on the appropriate list before any timers that
558 * expire later. This must be called with the tasklist_lock held
559 * for reading, and interrupts disabled.
561 static void arm_timer(struct k_itimer
*timer
, union cpu_time_count now
)
563 struct task_struct
*p
= timer
->it
.cpu
.task
;
564 struct list_head
*head
, *listpos
;
565 struct cpu_timer_list
*const nt
= &timer
->it
.cpu
;
566 struct cpu_timer_list
*next
;
569 if (CPUCLOCK_PERTHREAD(timer
->it_clock
) && (p
->flags
& PF_EXITING
))
572 head
= (CPUCLOCK_PERTHREAD(timer
->it_clock
) ?
573 p
->cpu_timers
: p
->signal
->cpu_timers
);
574 head
+= CPUCLOCK_WHICH(timer
->it_clock
);
576 BUG_ON(!irqs_disabled());
577 spin_lock(&p
->sighand
->siglock
);
580 if (CPUCLOCK_WHICH(timer
->it_clock
) == CPUCLOCK_SCHED
) {
581 list_for_each_entry(next
, head
, entry
) {
582 if (next
->expires
.sched
> nt
->expires
.sched
)
584 listpos
= &next
->entry
;
587 list_for_each_entry(next
, head
, entry
) {
588 if (cputime_gt(next
->expires
.cpu
, nt
->expires
.cpu
))
590 listpos
= &next
->entry
;
593 list_add(&nt
->entry
, listpos
);
595 if (listpos
== head
) {
597 * We are the new earliest-expiring timer.
598 * If we are a thread timer, there can always
599 * be a process timer telling us to stop earlier.
602 if (CPUCLOCK_PERTHREAD(timer
->it_clock
)) {
603 switch (CPUCLOCK_WHICH(timer
->it_clock
)) {
607 if (cputime_eq(p
->it_prof_expires
,
609 cputime_gt(p
->it_prof_expires
,
611 p
->it_prof_expires
= nt
->expires
.cpu
;
614 if (cputime_eq(p
->it_virt_expires
,
616 cputime_gt(p
->it_virt_expires
,
618 p
->it_virt_expires
= nt
->expires
.cpu
;
621 if (p
->it_sched_expires
== 0 ||
622 p
->it_sched_expires
> nt
->expires
.sched
)
623 p
->it_sched_expires
= nt
->expires
.sched
;
628 * For a process timer, we must balance
629 * all the live threads' expirations.
631 switch (CPUCLOCK_WHICH(timer
->it_clock
)) {
635 if (!cputime_eq(p
->signal
->it_virt_expires
,
637 cputime_lt(p
->signal
->it_virt_expires
,
638 timer
->it
.cpu
.expires
.cpu
))
642 if (!cputime_eq(p
->signal
->it_prof_expires
,
644 cputime_lt(p
->signal
->it_prof_expires
,
645 timer
->it
.cpu
.expires
.cpu
))
647 i
= p
->signal
->rlim
[RLIMIT_CPU
].rlim_cur
;
648 if (i
!= RLIM_INFINITY
&&
649 i
<= cputime_to_secs(timer
->it
.cpu
.expires
.cpu
))
654 process_timer_rebalance(
656 CPUCLOCK_WHICH(timer
->it_clock
),
657 timer
->it
.cpu
.expires
, now
);
663 spin_unlock(&p
->sighand
->siglock
);
667 * The timer is locked, fire it and arrange for its reload.
669 static void cpu_timer_fire(struct k_itimer
*timer
)
671 if (unlikely(timer
->sigq
== NULL
)) {
673 * This a special case for clock_nanosleep,
674 * not a normal timer from sys_timer_create.
676 wake_up_process(timer
->it_process
);
677 timer
->it
.cpu
.expires
.sched
= 0;
678 } else if (timer
->it
.cpu
.incr
.sched
== 0) {
680 * One-shot timer. Clear it as soon as it's fired.
682 posix_timer_event(timer
, 0);
683 timer
->it
.cpu
.expires
.sched
= 0;
684 } else if (posix_timer_event(timer
, ++timer
->it_requeue_pending
)) {
686 * The signal did not get queued because the signal
687 * was ignored, so we won't get any callback to
688 * reload the timer. But we need to keep it
689 * ticking in case the signal is deliverable next time.
691 posix_cpu_timer_schedule(timer
);
696 * Guts of sys_timer_settime for CPU timers.
697 * This is called with the timer locked and interrupts disabled.
698 * If we return TIMER_RETRY, it's necessary to release the timer's lock
699 * and try again. (This happens when the timer is in the middle of firing.)
701 int posix_cpu_timer_set(struct k_itimer
*timer
, int flags
,
702 struct itimerspec
*new, struct itimerspec
*old
)
704 struct task_struct
*p
= timer
->it
.cpu
.task
;
705 union cpu_time_count old_expires
, new_expires
, val
;
708 if (unlikely(p
== NULL
)) {
710 * Timer refers to a dead task's clock.
715 new_expires
= timespec_to_sample(timer
->it_clock
, &new->it_value
);
717 read_lock(&tasklist_lock
);
719 * We need the tasklist_lock to protect against reaping that
720 * clears p->signal. If p has just been reaped, we can no
721 * longer get any information about it at all.
723 if (unlikely(p
->signal
== NULL
)) {
724 read_unlock(&tasklist_lock
);
726 timer
->it
.cpu
.task
= NULL
;
731 * Disarm any old timer after extracting its expiry time.
733 BUG_ON(!irqs_disabled());
736 spin_lock(&p
->sighand
->siglock
);
737 old_expires
= timer
->it
.cpu
.expires
;
738 if (unlikely(timer
->it
.cpu
.firing
)) {
739 timer
->it
.cpu
.firing
= -1;
742 list_del_init(&timer
->it
.cpu
.entry
);
743 spin_unlock(&p
->sighand
->siglock
);
746 * We need to sample the current value to convert the new
747 * value from to relative and absolute, and to convert the
748 * old value from absolute to relative. To set a process
749 * timer, we need a sample to balance the thread expiry
750 * times (in arm_timer). With an absolute time, we must
751 * check if it's already passed. In short, we need a sample.
753 if (CPUCLOCK_PERTHREAD(timer
->it_clock
)) {
754 cpu_clock_sample(timer
->it_clock
, p
, &val
);
756 cpu_clock_sample_group(timer
->it_clock
, p
, &val
);
760 if (old_expires
.sched
== 0) {
761 old
->it_value
.tv_sec
= 0;
762 old
->it_value
.tv_nsec
= 0;
765 * Update the timer in case it has
766 * overrun already. If it has,
767 * we'll report it as having overrun
768 * and with the next reloaded timer
769 * already ticking, though we are
770 * swallowing that pending
771 * notification here to install the
774 bump_cpu_timer(timer
, val
);
775 if (cpu_time_before(timer
->it_clock
, val
,
776 timer
->it
.cpu
.expires
)) {
777 old_expires
= cpu_time_sub(
779 timer
->it
.cpu
.expires
, val
);
780 sample_to_timespec(timer
->it_clock
,
784 old
->it_value
.tv_nsec
= 1;
785 old
->it_value
.tv_sec
= 0;
792 * We are colliding with the timer actually firing.
793 * Punt after filling in the timer's old value, and
794 * disable this firing since we are already reporting
795 * it as an overrun (thanks to bump_cpu_timer above).
797 read_unlock(&tasklist_lock
);
801 if (new_expires
.sched
!= 0 && !(flags
& TIMER_ABSTIME
)) {
802 cpu_time_add(timer
->it_clock
, &new_expires
, val
);
806 * Install the new expiry time (or zero).
807 * For a timer with no notification action, we don't actually
808 * arm the timer (we'll just fake it for timer_gettime).
810 timer
->it
.cpu
.expires
= new_expires
;
811 if (new_expires
.sched
!= 0 &&
812 (timer
->it_sigev_notify
& ~SIGEV_THREAD_ID
) != SIGEV_NONE
&&
813 cpu_time_before(timer
->it_clock
, val
, new_expires
)) {
814 arm_timer(timer
, val
);
817 read_unlock(&tasklist_lock
);
820 * Install the new reload setting, and
821 * set up the signal and overrun bookkeeping.
823 timer
->it
.cpu
.incr
= timespec_to_sample(timer
->it_clock
,
827 * This acts as a modification timestamp for the timer,
828 * so any automatic reload attempt will punt on seeing
829 * that we have reset the timer manually.
831 timer
->it_requeue_pending
= (timer
->it_requeue_pending
+ 2) &
833 timer
->it_overrun_last
= 0;
834 timer
->it_overrun
= -1;
836 if (new_expires
.sched
!= 0 &&
837 (timer
->it_sigev_notify
& ~SIGEV_THREAD_ID
) != SIGEV_NONE
&&
838 !cpu_time_before(timer
->it_clock
, val
, new_expires
)) {
840 * The designated time already passed, so we notify
841 * immediately, even if the thread never runs to
842 * accumulate more time on this clock.
844 cpu_timer_fire(timer
);
850 sample_to_timespec(timer
->it_clock
,
851 timer
->it
.cpu
.incr
, &old
->it_interval
);
856 void posix_cpu_timer_get(struct k_itimer
*timer
, struct itimerspec
*itp
)
858 union cpu_time_count now
;
859 struct task_struct
*p
= timer
->it
.cpu
.task
;
863 * Easy part: convert the reload time.
865 sample_to_timespec(timer
->it_clock
,
866 timer
->it
.cpu
.incr
, &itp
->it_interval
);
868 if (timer
->it
.cpu
.expires
.sched
== 0) { /* Timer not armed at all. */
869 itp
->it_value
.tv_sec
= itp
->it_value
.tv_nsec
= 0;
873 if (unlikely(p
== NULL
)) {
875 * This task already died and the timer will never fire.
876 * In this case, expires is actually the dead value.
879 sample_to_timespec(timer
->it_clock
, timer
->it
.cpu
.expires
,
885 * Sample the clock to take the difference with the expiry time.
887 if (CPUCLOCK_PERTHREAD(timer
->it_clock
)) {
888 cpu_clock_sample(timer
->it_clock
, p
, &now
);
889 clear_dead
= p
->exit_state
;
891 read_lock(&tasklist_lock
);
892 if (unlikely(p
->signal
== NULL
)) {
894 * The process has been reaped.
895 * We can't even collect a sample any more.
896 * Call the timer disarmed, nothing else to do.
899 timer
->it
.cpu
.task
= NULL
;
900 timer
->it
.cpu
.expires
.sched
= 0;
901 read_unlock(&tasklist_lock
);
904 cpu_clock_sample_group(timer
->it_clock
, p
, &now
);
905 clear_dead
= (unlikely(p
->exit_state
) &&
906 thread_group_empty(p
));
908 read_unlock(&tasklist_lock
);
911 if ((timer
->it_sigev_notify
& ~SIGEV_THREAD_ID
) == SIGEV_NONE
) {
912 if (timer
->it
.cpu
.incr
.sched
== 0 &&
913 cpu_time_before(timer
->it_clock
,
914 timer
->it
.cpu
.expires
, now
)) {
916 * Do-nothing timer expired and has no reload,
917 * so it's as if it was never set.
919 timer
->it
.cpu
.expires
.sched
= 0;
920 itp
->it_value
.tv_sec
= itp
->it_value
.tv_nsec
= 0;
924 * Account for any expirations and reloads that should
927 bump_cpu_timer(timer
, now
);
930 if (unlikely(clear_dead
)) {
932 * We've noticed that the thread is dead, but
933 * not yet reaped. Take this opportunity to
936 clear_dead_task(timer
, now
);
940 if (cpu_time_before(timer
->it_clock
, now
, timer
->it
.cpu
.expires
)) {
941 sample_to_timespec(timer
->it_clock
,
942 cpu_time_sub(timer
->it_clock
,
943 timer
->it
.cpu
.expires
, now
),
947 * The timer should have expired already, but the firing
948 * hasn't taken place yet. Say it's just about to expire.
950 itp
->it_value
.tv_nsec
= 1;
951 itp
->it_value
.tv_sec
= 0;
956 * Check for any per-thread CPU timers that have fired and move them off
957 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
958 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
960 static void check_thread_timers(struct task_struct
*tsk
,
961 struct list_head
*firing
)
964 struct list_head
*timers
= tsk
->cpu_timers
;
967 tsk
->it_prof_expires
= cputime_zero
;
968 while (!list_empty(timers
)) {
969 struct cpu_timer_list
*t
= list_entry(timers
->next
,
970 struct cpu_timer_list
,
972 if (!--maxfire
|| cputime_lt(prof_ticks(tsk
), t
->expires
.cpu
)) {
973 tsk
->it_prof_expires
= t
->expires
.cpu
;
977 list_move_tail(&t
->entry
, firing
);
982 tsk
->it_virt_expires
= cputime_zero
;
983 while (!list_empty(timers
)) {
984 struct cpu_timer_list
*t
= list_entry(timers
->next
,
985 struct cpu_timer_list
,
987 if (!--maxfire
|| cputime_lt(virt_ticks(tsk
), t
->expires
.cpu
)) {
988 tsk
->it_virt_expires
= t
->expires
.cpu
;
992 list_move_tail(&t
->entry
, firing
);
997 tsk
->it_sched_expires
= 0;
998 while (!list_empty(timers
)) {
999 struct cpu_timer_list
*t
= list_entry(timers
->next
,
1000 struct cpu_timer_list
,
1002 if (!--maxfire
|| tsk
->sched_time
< t
->expires
.sched
) {
1003 tsk
->it_sched_expires
= t
->expires
.sched
;
1007 list_move_tail(&t
->entry
, firing
);
1012 * Check for any per-thread CPU timers that have fired and move them
1013 * off the tsk->*_timers list onto the firing list. Per-thread timers
1014 * have already been taken off.
1016 static void check_process_timers(struct task_struct
*tsk
,
1017 struct list_head
*firing
)
1020 struct signal_struct
*const sig
= tsk
->signal
;
1021 cputime_t utime
, stime
, ptime
, virt_expires
, prof_expires
;
1022 unsigned long long sched_time
, sched_expires
;
1023 struct task_struct
*t
;
1024 struct list_head
*timers
= sig
->cpu_timers
;
1027 * Don't sample the current process CPU clocks if there are no timers.
1029 if (list_empty(&timers
[CPUCLOCK_PROF
]) &&
1030 cputime_eq(sig
->it_prof_expires
, cputime_zero
) &&
1031 sig
->rlim
[RLIMIT_CPU
].rlim_cur
== RLIM_INFINITY
&&
1032 list_empty(&timers
[CPUCLOCK_VIRT
]) &&
1033 cputime_eq(sig
->it_virt_expires
, cputime_zero
) &&
1034 list_empty(&timers
[CPUCLOCK_SCHED
]))
1038 * Collect the current process totals.
1042 sched_time
= sig
->sched_time
;
1045 utime
= cputime_add(utime
, t
->utime
);
1046 stime
= cputime_add(stime
, t
->stime
);
1047 sched_time
+= t
->sched_time
;
1050 ptime
= cputime_add(utime
, stime
);
1053 prof_expires
= cputime_zero
;
1054 while (!list_empty(timers
)) {
1055 struct cpu_timer_list
*t
= list_entry(timers
->next
,
1056 struct cpu_timer_list
,
1058 if (!--maxfire
|| cputime_lt(ptime
, t
->expires
.cpu
)) {
1059 prof_expires
= t
->expires
.cpu
;
1063 list_move_tail(&t
->entry
, firing
);
1068 virt_expires
= cputime_zero
;
1069 while (!list_empty(timers
)) {
1070 struct cpu_timer_list
*t
= list_entry(timers
->next
,
1071 struct cpu_timer_list
,
1073 if (!--maxfire
|| cputime_lt(utime
, t
->expires
.cpu
)) {
1074 virt_expires
= t
->expires
.cpu
;
1078 list_move_tail(&t
->entry
, firing
);
1084 while (!list_empty(timers
)) {
1085 struct cpu_timer_list
*t
= list_entry(timers
->next
,
1086 struct cpu_timer_list
,
1088 if (!--maxfire
|| sched_time
< t
->expires
.sched
) {
1089 sched_expires
= t
->expires
.sched
;
1093 list_move_tail(&t
->entry
, firing
);
1097 * Check for the special case process timers.
1099 if (!cputime_eq(sig
->it_prof_expires
, cputime_zero
)) {
1100 if (cputime_ge(ptime
, sig
->it_prof_expires
)) {
1101 /* ITIMER_PROF fires and reloads. */
1102 sig
->it_prof_expires
= sig
->it_prof_incr
;
1103 if (!cputime_eq(sig
->it_prof_expires
, cputime_zero
)) {
1104 sig
->it_prof_expires
= cputime_add(
1105 sig
->it_prof_expires
, ptime
);
1107 __group_send_sig_info(SIGPROF
, SEND_SIG_PRIV
, tsk
);
1109 if (!cputime_eq(sig
->it_prof_expires
, cputime_zero
) &&
1110 (cputime_eq(prof_expires
, cputime_zero
) ||
1111 cputime_lt(sig
->it_prof_expires
, prof_expires
))) {
1112 prof_expires
= sig
->it_prof_expires
;
1115 if (!cputime_eq(sig
->it_virt_expires
, cputime_zero
)) {
1116 if (cputime_ge(utime
, sig
->it_virt_expires
)) {
1117 /* ITIMER_VIRTUAL fires and reloads. */
1118 sig
->it_virt_expires
= sig
->it_virt_incr
;
1119 if (!cputime_eq(sig
->it_virt_expires
, cputime_zero
)) {
1120 sig
->it_virt_expires
= cputime_add(
1121 sig
->it_virt_expires
, utime
);
1123 __group_send_sig_info(SIGVTALRM
, SEND_SIG_PRIV
, tsk
);
1125 if (!cputime_eq(sig
->it_virt_expires
, cputime_zero
) &&
1126 (cputime_eq(virt_expires
, cputime_zero
) ||
1127 cputime_lt(sig
->it_virt_expires
, virt_expires
))) {
1128 virt_expires
= sig
->it_virt_expires
;
1131 if (sig
->rlim
[RLIMIT_CPU
].rlim_cur
!= RLIM_INFINITY
) {
1132 unsigned long psecs
= cputime_to_secs(ptime
);
1134 if (psecs
>= sig
->rlim
[RLIMIT_CPU
].rlim_max
) {
1136 * At the hard limit, we just die.
1137 * No need to calculate anything else now.
1139 __group_send_sig_info(SIGKILL
, SEND_SIG_PRIV
, tsk
);
1142 if (psecs
>= sig
->rlim
[RLIMIT_CPU
].rlim_cur
) {
1144 * At the soft limit, send a SIGXCPU every second.
1146 __group_send_sig_info(SIGXCPU
, SEND_SIG_PRIV
, tsk
);
1147 if (sig
->rlim
[RLIMIT_CPU
].rlim_cur
1148 < sig
->rlim
[RLIMIT_CPU
].rlim_max
) {
1149 sig
->rlim
[RLIMIT_CPU
].rlim_cur
++;
1152 x
= secs_to_cputime(sig
->rlim
[RLIMIT_CPU
].rlim_cur
);
1153 if (cputime_eq(prof_expires
, cputime_zero
) ||
1154 cputime_lt(x
, prof_expires
)) {
1159 if (!cputime_eq(prof_expires
, cputime_zero
) ||
1160 !cputime_eq(virt_expires
, cputime_zero
) ||
1161 sched_expires
!= 0) {
1163 * Rebalance the threads' expiry times for the remaining
1164 * process CPU timers.
1167 cputime_t prof_left
, virt_left
, ticks
;
1168 unsigned long long sched_left
, sched
;
1169 const unsigned int nthreads
= atomic_read(&sig
->live
);
1174 prof_left
= cputime_sub(prof_expires
, utime
);
1175 prof_left
= cputime_sub(prof_left
, stime
);
1176 prof_left
= cputime_div(prof_left
, nthreads
);
1177 virt_left
= cputime_sub(virt_expires
, utime
);
1178 virt_left
= cputime_div(virt_left
, nthreads
);
1179 if (sched_expires
) {
1180 sched_left
= sched_expires
- sched_time
;
1181 do_div(sched_left
, nthreads
);
1187 ticks
= cputime_add(cputime_add(t
->utime
, t
->stime
),
1189 if (!cputime_eq(prof_expires
, cputime_zero
) &&
1190 (cputime_eq(t
->it_prof_expires
, cputime_zero
) ||
1191 cputime_gt(t
->it_prof_expires
, ticks
))) {
1192 t
->it_prof_expires
= ticks
;
1195 ticks
= cputime_add(t
->utime
, virt_left
);
1196 if (!cputime_eq(virt_expires
, cputime_zero
) &&
1197 (cputime_eq(t
->it_virt_expires
, cputime_zero
) ||
1198 cputime_gt(t
->it_virt_expires
, ticks
))) {
1199 t
->it_virt_expires
= ticks
;
1202 sched
= t
->sched_time
+ sched_left
;
1203 if (sched_expires
&& (t
->it_sched_expires
== 0 ||
1204 t
->it_sched_expires
> sched
)) {
1205 t
->it_sched_expires
= sched
;
1210 } while (unlikely(t
->flags
& PF_EXITING
));
1216 * This is called from the signal code (via do_schedule_next_timer)
1217 * when the last timer signal was delivered and we have to reload the timer.
1219 void posix_cpu_timer_schedule(struct k_itimer
*timer
)
1221 struct task_struct
*p
= timer
->it
.cpu
.task
;
1222 union cpu_time_count now
;
1224 if (unlikely(p
== NULL
))
1226 * The task was cleaned up already, no future firings.
1231 * Fetch the current sample and update the timer's expiry time.
1233 if (CPUCLOCK_PERTHREAD(timer
->it_clock
)) {
1234 cpu_clock_sample(timer
->it_clock
, p
, &now
);
1235 bump_cpu_timer(timer
, now
);
1236 if (unlikely(p
->exit_state
)) {
1237 clear_dead_task(timer
, now
);
1240 read_lock(&tasklist_lock
); /* arm_timer needs it. */
1242 read_lock(&tasklist_lock
);
1243 if (unlikely(p
->signal
== NULL
)) {
1245 * The process has been reaped.
1246 * We can't even collect a sample any more.
1249 timer
->it
.cpu
.task
= p
= NULL
;
1250 timer
->it
.cpu
.expires
.sched
= 0;
1251 read_unlock(&tasklist_lock
);
1253 } else if (unlikely(p
->exit_state
) && thread_group_empty(p
)) {
1255 * We've noticed that the thread is dead, but
1256 * not yet reaped. Take this opportunity to
1257 * drop our task ref.
1259 clear_dead_task(timer
, now
);
1260 read_unlock(&tasklist_lock
);
1263 cpu_clock_sample_group(timer
->it_clock
, p
, &now
);
1264 bump_cpu_timer(timer
, now
);
1265 /* Leave the tasklist_lock locked for the call below. */
1269 * Now re-arm for the new expiry time.
1271 arm_timer(timer
, now
);
1273 read_unlock(&tasklist_lock
);
1277 * This is called from the timer interrupt handler. The irq handler has
1278 * already updated our counts. We need to check if any timers fire now.
1279 * Interrupts are disabled.
1281 void run_posix_cpu_timers(struct task_struct
*tsk
)
1284 struct k_itimer
*timer
, *next
;
1286 BUG_ON(!irqs_disabled());
1288 #define UNEXPIRED(clock) \
1289 (cputime_eq(tsk->it_##clock##_expires, cputime_zero) || \
1290 cputime_lt(clock##_ticks(tsk), tsk->it_##clock##_expires))
1292 if (UNEXPIRED(prof
) && UNEXPIRED(virt
) &&
1293 (tsk
->it_sched_expires
== 0 ||
1294 tsk
->sched_time
< tsk
->it_sched_expires
))
1299 BUG_ON(tsk
->exit_state
);
1302 * Double-check with locks held.
1304 read_lock(&tasklist_lock
);
1305 spin_lock(&tsk
->sighand
->siglock
);
1308 * Here we take off tsk->cpu_timers[N] and tsk->signal->cpu_timers[N]
1309 * all the timers that are firing, and put them on the firing list.
1311 check_thread_timers(tsk
, &firing
);
1312 check_process_timers(tsk
, &firing
);
1315 * We must release these locks before taking any timer's lock.
1316 * There is a potential race with timer deletion here, as the
1317 * siglock now protects our private firing list. We have set
1318 * the firing flag in each timer, so that a deletion attempt
1319 * that gets the timer lock before we do will give it up and
1320 * spin until we've taken care of that timer below.
1322 spin_unlock(&tsk
->sighand
->siglock
);
1323 read_unlock(&tasklist_lock
);
1326 * Now that all the timers on our list have the firing flag,
1327 * noone will touch their list entries but us. We'll take
1328 * each timer's lock before clearing its firing flag, so no
1329 * timer call will interfere.
1331 list_for_each_entry_safe(timer
, next
, &firing
, it
.cpu
.entry
) {
1333 spin_lock(&timer
->it_lock
);
1334 list_del_init(&timer
->it
.cpu
.entry
);
1335 firing
= timer
->it
.cpu
.firing
;
1336 timer
->it
.cpu
.firing
= 0;
1338 * The firing flag is -1 if we collided with a reset
1339 * of the timer, which already reported this
1340 * almost-firing as an overrun. So don't generate an event.
1342 if (likely(firing
>= 0)) {
1343 cpu_timer_fire(timer
);
1345 spin_unlock(&timer
->it_lock
);
1350 * Set one of the process-wide special case CPU timers.
1351 * The tasklist_lock and tsk->sighand->siglock must be held by the caller.
1352 * The oldval argument is null for the RLIMIT_CPU timer, where *newval is
1353 * absolute; non-null for ITIMER_*, where *newval is relative and we update
1354 * it to be absolute, *oldval is absolute and we update it to be relative.
1356 void set_process_cpu_timer(struct task_struct
*tsk
, unsigned int clock_idx
,
1357 cputime_t
*newval
, cputime_t
*oldval
)
1359 union cpu_time_count now
;
1360 struct list_head
*head
;
1362 BUG_ON(clock_idx
== CPUCLOCK_SCHED
);
1363 cpu_clock_sample_group_locked(clock_idx
, tsk
, &now
);
1366 if (!cputime_eq(*oldval
, cputime_zero
)) {
1367 if (cputime_le(*oldval
, now
.cpu
)) {
1368 /* Just about to fire. */
1369 *oldval
= jiffies_to_cputime(1);
1371 *oldval
= cputime_sub(*oldval
, now
.cpu
);
1375 if (cputime_eq(*newval
, cputime_zero
))
1377 *newval
= cputime_add(*newval
, now
.cpu
);
1380 * If the RLIMIT_CPU timer will expire before the
1381 * ITIMER_PROF timer, we have nothing else to do.
1383 if (tsk
->signal
->rlim
[RLIMIT_CPU
].rlim_cur
1384 < cputime_to_secs(*newval
))
1389 * Check whether there are any process timers already set to fire
1390 * before this one. If so, we don't have anything more to do.
1392 head
= &tsk
->signal
->cpu_timers
[clock_idx
];
1393 if (list_empty(head
) ||
1394 cputime_ge(list_entry(head
->next
,
1395 struct cpu_timer_list
, entry
)->expires
.cpu
,
1398 * Rejigger each thread's expiry time so that one will
1399 * notice before we hit the process-cumulative expiry time.
1401 union cpu_time_count expires
= { .sched
= 0 };
1402 expires
.cpu
= *newval
;
1403 process_timer_rebalance(tsk
, clock_idx
, expires
, now
);
1407 static long posix_cpu_clock_nanosleep_restart(struct restart_block
*);
1409 int posix_cpu_nsleep(clockid_t which_clock
, int flags
,
1410 struct timespec
*rqtp
)
1412 struct restart_block
*restart_block
=
1413 ¤t_thread_info()->restart_block
;
1414 struct k_itimer timer
;
1418 * Diagnose required errors first.
1420 if (CPUCLOCK_PERTHREAD(which_clock
) &&
1421 (CPUCLOCK_PID(which_clock
) == 0 ||
1422 CPUCLOCK_PID(which_clock
) == current
->pid
))
1426 * Set up a temporary timer and then wait for it to go off.
1428 memset(&timer
, 0, sizeof timer
);
1429 spin_lock_init(&timer
.it_lock
);
1430 timer
.it_clock
= which_clock
;
1431 timer
.it_overrun
= -1;
1432 error
= posix_cpu_timer_create(&timer
);
1433 timer
.it_process
= current
;
1435 struct timespec __user
*rmtp
;
1436 static struct itimerspec zero_it
;
1437 struct itimerspec it
= { .it_value
= *rqtp
,
1438 .it_interval
= {} };
1440 spin_lock_irq(&timer
.it_lock
);
1441 error
= posix_cpu_timer_set(&timer
, flags
, &it
, NULL
);
1443 spin_unlock_irq(&timer
.it_lock
);
1447 while (!signal_pending(current
)) {
1448 if (timer
.it
.cpu
.expires
.sched
== 0) {
1450 * Our timer fired and was reset.
1452 spin_unlock_irq(&timer
.it_lock
);
1457 * Block until cpu_timer_fire (or a signal) wakes us.
1459 __set_current_state(TASK_INTERRUPTIBLE
);
1460 spin_unlock_irq(&timer
.it_lock
);
1462 spin_lock_irq(&timer
.it_lock
);
1466 * We were interrupted by a signal.
1468 sample_to_timespec(which_clock
, timer
.it
.cpu
.expires
, rqtp
);
1469 posix_cpu_timer_set(&timer
, 0, &zero_it
, &it
);
1470 spin_unlock_irq(&timer
.it_lock
);
1472 if ((it
.it_value
.tv_sec
| it
.it_value
.tv_nsec
) == 0) {
1474 * It actually did fire already.
1480 * Report back to the user the time still remaining.
1482 rmtp
= (struct timespec __user
*) restart_block
->arg1
;
1483 if (rmtp
!= NULL
&& !(flags
& TIMER_ABSTIME
) &&
1484 copy_to_user(rmtp
, &it
.it_value
, sizeof *rmtp
))
1487 restart_block
->fn
= posix_cpu_clock_nanosleep_restart
;
1488 /* Caller already set restart_block->arg1 */
1489 restart_block
->arg0
= which_clock
;
1490 restart_block
->arg2
= rqtp
->tv_sec
;
1491 restart_block
->arg3
= rqtp
->tv_nsec
;
1493 error
= -ERESTART_RESTARTBLOCK
;
1500 posix_cpu_clock_nanosleep_restart(struct restart_block
*restart_block
)
1502 clockid_t which_clock
= restart_block
->arg0
;
1503 struct timespec t
= { .tv_sec
= restart_block
->arg2
,
1504 .tv_nsec
= restart_block
->arg3
};
1505 restart_block
->fn
= do_no_restart_syscall
;
1506 return posix_cpu_nsleep(which_clock
, TIMER_ABSTIME
, &t
);
1510 #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1511 #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1513 static int process_cpu_clock_getres(clockid_t which_clock
, struct timespec
*tp
)
1515 return posix_cpu_clock_getres(PROCESS_CLOCK
, tp
);
1517 static int process_cpu_clock_get(clockid_t which_clock
, struct timespec
*tp
)
1519 return posix_cpu_clock_get(PROCESS_CLOCK
, tp
);
1521 static int process_cpu_timer_create(struct k_itimer
*timer
)
1523 timer
->it_clock
= PROCESS_CLOCK
;
1524 return posix_cpu_timer_create(timer
);
1526 static int process_cpu_nsleep(clockid_t which_clock
, int flags
,
1527 struct timespec
*rqtp
)
1529 return posix_cpu_nsleep(PROCESS_CLOCK
, flags
, rqtp
);
1531 static int thread_cpu_clock_getres(clockid_t which_clock
, struct timespec
*tp
)
1533 return posix_cpu_clock_getres(THREAD_CLOCK
, tp
);
1535 static int thread_cpu_clock_get(clockid_t which_clock
, struct timespec
*tp
)
1537 return posix_cpu_clock_get(THREAD_CLOCK
, tp
);
1539 static int thread_cpu_timer_create(struct k_itimer
*timer
)
1541 timer
->it_clock
= THREAD_CLOCK
;
1542 return posix_cpu_timer_create(timer
);
1544 static int thread_cpu_nsleep(clockid_t which_clock
, int flags
,
1545 struct timespec
*rqtp
)
1550 static __init
int init_posix_cpu_timers(void)
1552 struct k_clock process
= {
1553 .clock_getres
= process_cpu_clock_getres
,
1554 .clock_get
= process_cpu_clock_get
,
1555 .clock_set
= do_posix_clock_nosettime
,
1556 .timer_create
= process_cpu_timer_create
,
1557 .nsleep
= process_cpu_nsleep
,
1559 struct k_clock thread
= {
1560 .clock_getres
= thread_cpu_clock_getres
,
1561 .clock_get
= thread_cpu_clock_get
,
1562 .clock_set
= do_posix_clock_nosettime
,
1563 .timer_create
= thread_cpu_timer_create
,
1564 .nsleep
= thread_cpu_nsleep
,
1567 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID
, &process
);
1568 register_posix_clock(CLOCK_THREAD_CPUTIME_ID
, &thread
);
1572 __initcall(init_posix_cpu_timers
);