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 * Called after updating RLIMIT_CPU to set timer expiration if necessary.
15 void update_rlimit_cpu(unsigned long rlim_new)
19 cputime = secs_to_cputime(rlim_new);
20 if (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
21 cputime_lt(current->signal->it_prof_expires, cputime)) {
22 spin_lock_irq(¤t->sighand->siglock);
23 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
24 spin_unlock_irq(¤t->sighand->siglock);
28 static int check_clock(const clockid_t which_clock)
31 struct task_struct *p;
32 const pid_t pid = CPUCLOCK_PID(which_clock);
34 if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
40 read_lock(&tasklist_lock);
41 p = find_task_by_vpid(pid);
42 if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
43 same_thread_group(p, current) : thread_group_leader(p))) {
46 read_unlock(&tasklist_lock);
51 static inline union cpu_time_count
52 timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
54 union cpu_time_count ret;
55 ret.sched = 0; /* high half always zero when .cpu used */
56 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
57 ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
59 ret.cpu = timespec_to_cputime(tp);
64 static void sample_to_timespec(const clockid_t which_clock,
65 union cpu_time_count cpu,
68 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED)
69 *tp = ns_to_timespec(cpu.sched);
71 cputime_to_timespec(cpu.cpu, tp);
74 static inline int cpu_time_before(const clockid_t which_clock,
75 union cpu_time_count now,
76 union cpu_time_count then)
78 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
79 return now.sched < then.sched;
81 return cputime_lt(now.cpu, then.cpu);
84 static inline void cpu_time_add(const clockid_t which_clock,
85 union cpu_time_count *acc,
86 union cpu_time_count val)
88 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
89 acc->sched += val.sched;
91 acc->cpu = cputime_add(acc->cpu, val.cpu);
94 static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock,
95 union cpu_time_count a,
96 union cpu_time_count b)
98 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
101 a.cpu = cputime_sub(a.cpu, b.cpu);
107 * Divide and limit the result to res >= 1
109 * This is necessary to prevent signal delivery starvation, when the result of
110 * the division would be rounded down to 0.
112 static inline cputime_t cputime_div_non_zero(cputime_t time, unsigned long div)
114 cputime_t res = cputime_div(time, div);
116 return max_t(cputime_t, res, 1);
120 * Update expiry time from increment, and increase overrun count,
121 * given the current clock sample.
123 static void bump_cpu_timer(struct k_itimer *timer,
124 union cpu_time_count now)
128 if (timer->it.cpu.incr.sched == 0)
131 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
132 unsigned long long delta, incr;
134 if (now.sched < timer->it.cpu.expires.sched)
136 incr = timer->it.cpu.incr.sched;
137 delta = now.sched + incr - timer->it.cpu.expires.sched;
138 /* Don't use (incr*2 < delta), incr*2 might overflow. */
139 for (i = 0; incr < delta - incr; i++)
141 for (; i >= 0; incr >>= 1, i--) {
144 timer->it.cpu.expires.sched += incr;
145 timer->it_overrun += 1 << i;
149 cputime_t delta, incr;
151 if (cputime_lt(now.cpu, timer->it.cpu.expires.cpu))
153 incr = timer->it.cpu.incr.cpu;
154 delta = cputime_sub(cputime_add(now.cpu, incr),
155 timer->it.cpu.expires.cpu);
156 /* Don't use (incr*2 < delta), incr*2 might overflow. */
157 for (i = 0; cputime_lt(incr, cputime_sub(delta, incr)); i++)
158 incr = cputime_add(incr, incr);
159 for (; i >= 0; incr = cputime_halve(incr), i--) {
160 if (cputime_lt(delta, incr))
162 timer->it.cpu.expires.cpu =
163 cputime_add(timer->it.cpu.expires.cpu, incr);
164 timer->it_overrun += 1 << i;
165 delta = cputime_sub(delta, incr);
170 static inline cputime_t prof_ticks(struct task_struct *p)
172 return cputime_add(p->utime, p->stime);
174 static inline cputime_t virt_ticks(struct task_struct *p)
179 int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
181 int error = check_clock(which_clock);
184 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
185 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
187 * If sched_clock is using a cycle counter, we
188 * don't have any idea of its true resolution
189 * exported, but it is much more than 1s/HZ.
197 int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
200 * You can never reset a CPU clock, but we check for other errors
201 * in the call before failing with EPERM.
203 int error = check_clock(which_clock);
212 * Sample a per-thread clock for the given task.
214 static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
215 union cpu_time_count *cpu)
217 switch (CPUCLOCK_WHICH(which_clock)) {
221 cpu->cpu = prof_ticks(p);
224 cpu->cpu = virt_ticks(p);
227 cpu->sched = p->se.sum_exec_runtime + task_delta_exec(p);
233 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
235 struct sighand_struct *sighand;
236 struct signal_struct *sig;
237 struct task_struct *t;
239 *times = INIT_CPUTIME;
242 sighand = rcu_dereference(tsk->sighand);
250 times->utime = cputime_add(times->utime, t->utime);
251 times->stime = cputime_add(times->stime, t->stime);
252 times->sum_exec_runtime += t->se.sum_exec_runtime;
257 times->utime = cputime_add(times->utime, sig->utime);
258 times->stime = cputime_add(times->stime, sig->stime);
259 times->sum_exec_runtime += sig->sum_sched_runtime;
265 * Sample a process (thread group) clock for the given group_leader task.
266 * Must be called with tasklist_lock held for reading.
268 static int cpu_clock_sample_group(const clockid_t which_clock,
269 struct task_struct *p,
270 union cpu_time_count *cpu)
272 struct task_cputime cputime;
274 thread_group_cputime(p, &cputime);
275 switch (CPUCLOCK_WHICH(which_clock)) {
279 cpu->cpu = cputime_add(cputime.utime, cputime.stime);
282 cpu->cpu = cputime.utime;
285 cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
292 int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
294 const pid_t pid = CPUCLOCK_PID(which_clock);
296 union cpu_time_count rtn;
300 * Special case constant value for our own clocks.
301 * We don't have to do any lookup to find ourselves.
303 if (CPUCLOCK_PERTHREAD(which_clock)) {
305 * Sampling just ourselves we can do with no locking.
307 error = cpu_clock_sample(which_clock,
310 read_lock(&tasklist_lock);
311 error = cpu_clock_sample_group(which_clock,
313 read_unlock(&tasklist_lock);
317 * Find the given PID, and validate that the caller
318 * should be able to see it.
320 struct task_struct *p;
322 p = find_task_by_vpid(pid);
324 if (CPUCLOCK_PERTHREAD(which_clock)) {
325 if (same_thread_group(p, current)) {
326 error = cpu_clock_sample(which_clock,
330 read_lock(&tasklist_lock);
331 if (thread_group_leader(p) && p->signal) {
333 cpu_clock_sample_group(which_clock,
336 read_unlock(&tasklist_lock);
344 sample_to_timespec(which_clock, rtn, tp);
350 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
351 * This is called from sys_timer_create with the new timer already locked.
353 int posix_cpu_timer_create(struct k_itimer *new_timer)
356 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
357 struct task_struct *p;
359 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
362 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
363 new_timer->it.cpu.incr.sched = 0;
364 new_timer->it.cpu.expires.sched = 0;
366 read_lock(&tasklist_lock);
367 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
371 p = find_task_by_vpid(pid);
372 if (p && !same_thread_group(p, current))
377 p = current->group_leader;
379 p = find_task_by_vpid(pid);
380 if (p && !thread_group_leader(p))
384 new_timer->it.cpu.task = p;
390 read_unlock(&tasklist_lock);
396 * Clean up a CPU-clock timer that is about to be destroyed.
397 * This is called from timer deletion with the timer already locked.
398 * If we return TIMER_RETRY, it's necessary to release the timer's lock
399 * and try again. (This happens when the timer is in the middle of firing.)
401 int posix_cpu_timer_del(struct k_itimer *timer)
403 struct task_struct *p = timer->it.cpu.task;
406 if (likely(p != NULL)) {
407 read_lock(&tasklist_lock);
408 if (unlikely(p->signal == NULL)) {
410 * We raced with the reaping of the task.
411 * The deletion should have cleared us off the list.
413 BUG_ON(!list_empty(&timer->it.cpu.entry));
415 spin_lock(&p->sighand->siglock);
416 if (timer->it.cpu.firing)
419 list_del(&timer->it.cpu.entry);
420 spin_unlock(&p->sighand->siglock);
422 read_unlock(&tasklist_lock);
432 * Clean out CPU timers still ticking when a thread exited. The task
433 * pointer is cleared, and the expiry time is replaced with the residual
434 * time for later timer_gettime calls to return.
435 * This must be called with the siglock held.
437 static void cleanup_timers(struct list_head *head,
438 cputime_t utime, cputime_t stime,
439 unsigned long long sum_exec_runtime)
441 struct cpu_timer_list *timer, *next;
442 cputime_t ptime = cputime_add(utime, stime);
444 list_for_each_entry_safe(timer, next, head, entry) {
445 list_del_init(&timer->entry);
446 if (cputime_lt(timer->expires.cpu, ptime)) {
447 timer->expires.cpu = cputime_zero;
449 timer->expires.cpu = cputime_sub(timer->expires.cpu,
455 list_for_each_entry_safe(timer, next, head, entry) {
456 list_del_init(&timer->entry);
457 if (cputime_lt(timer->expires.cpu, utime)) {
458 timer->expires.cpu = cputime_zero;
460 timer->expires.cpu = cputime_sub(timer->expires.cpu,
466 list_for_each_entry_safe(timer, next, head, entry) {
467 list_del_init(&timer->entry);
468 if (timer->expires.sched < sum_exec_runtime) {
469 timer->expires.sched = 0;
471 timer->expires.sched -= sum_exec_runtime;
477 * These are both called with the siglock held, when the current thread
478 * is being reaped. When the final (leader) thread in the group is reaped,
479 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
481 void posix_cpu_timers_exit(struct task_struct *tsk)
483 cleanup_timers(tsk->cpu_timers,
484 tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
487 void posix_cpu_timers_exit_group(struct task_struct *tsk)
489 struct task_cputime cputime;
491 thread_group_cputime(tsk, &cputime);
492 cleanup_timers(tsk->signal->cpu_timers,
493 cputime.utime, cputime.stime, cputime.sum_exec_runtime);
496 static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
499 * That's all for this thread or process.
500 * We leave our residual in expires to be reported.
502 put_task_struct(timer->it.cpu.task);
503 timer->it.cpu.task = NULL;
504 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
505 timer->it.cpu.expires,
510 * Enable the process wide cpu timer accounting.
512 * serialized using ->sighand->siglock
514 static void start_process_timers(struct task_struct *tsk)
516 tsk->signal->cputimer.running = 1;
521 * Release the process wide timer accounting -- timer stops ticking when
522 * nobody cares about it.
524 * serialized using ->sighand->siglock
526 static void stop_process_timers(struct task_struct *tsk)
528 tsk->signal->cputimer.running = 0;
533 * Insert the timer on the appropriate list before any timers that
534 * expire later. This must be called with the tasklist_lock held
535 * for reading, and interrupts disabled.
537 static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
539 struct task_struct *p = timer->it.cpu.task;
540 struct list_head *head, *listpos;
541 struct cpu_timer_list *const nt = &timer->it.cpu;
542 struct cpu_timer_list *next;
545 head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
546 p->cpu_timers : p->signal->cpu_timers);
547 head += CPUCLOCK_WHICH(timer->it_clock);
549 BUG_ON(!irqs_disabled());
550 spin_lock(&p->sighand->siglock);
552 if (!CPUCLOCK_PERTHREAD(timer->it_clock))
553 start_process_timers(p);
556 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
557 list_for_each_entry(next, head, entry) {
558 if (next->expires.sched > nt->expires.sched)
560 listpos = &next->entry;
563 list_for_each_entry(next, head, entry) {
564 if (cputime_gt(next->expires.cpu, nt->expires.cpu))
566 listpos = &next->entry;
569 list_add(&nt->entry, listpos);
571 if (listpos == head) {
573 * We are the new earliest-expiring timer.
574 * If we are a thread timer, there can always
575 * be a process timer telling us to stop earlier.
578 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
579 switch (CPUCLOCK_WHICH(timer->it_clock)) {
583 if (cputime_eq(p->cputime_expires.prof_exp,
585 cputime_gt(p->cputime_expires.prof_exp,
587 p->cputime_expires.prof_exp =
591 if (cputime_eq(p->cputime_expires.virt_exp,
593 cputime_gt(p->cputime_expires.virt_exp,
595 p->cputime_expires.virt_exp =
599 if (p->cputime_expires.sched_exp == 0 ||
600 p->cputime_expires.sched_exp >
602 p->cputime_expires.sched_exp =
608 * For a process timer, set the cached expiration time.
610 switch (CPUCLOCK_WHICH(timer->it_clock)) {
614 if (!cputime_eq(p->signal->it_virt_expires,
616 cputime_lt(p->signal->it_virt_expires,
617 timer->it.cpu.expires.cpu))
619 p->signal->cputime_expires.virt_exp =
620 timer->it.cpu.expires.cpu;
623 if (!cputime_eq(p->signal->it_prof_expires,
625 cputime_lt(p->signal->it_prof_expires,
626 timer->it.cpu.expires.cpu))
628 i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
629 if (i != RLIM_INFINITY &&
630 i <= cputime_to_secs(timer->it.cpu.expires.cpu))
632 p->signal->cputime_expires.prof_exp =
633 timer->it.cpu.expires.cpu;
636 p->signal->cputime_expires.sched_exp =
637 timer->it.cpu.expires.sched;
643 spin_unlock(&p->sighand->siglock);
647 * The timer is locked, fire it and arrange for its reload.
649 static void cpu_timer_fire(struct k_itimer *timer)
651 if (unlikely(timer->sigq == NULL)) {
653 * This a special case for clock_nanosleep,
654 * not a normal timer from sys_timer_create.
656 wake_up_process(timer->it_process);
657 timer->it.cpu.expires.sched = 0;
658 } else if (timer->it.cpu.incr.sched == 0) {
660 * One-shot timer. Clear it as soon as it's fired.
662 posix_timer_event(timer, 0);
663 timer->it.cpu.expires.sched = 0;
664 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
666 * The signal did not get queued because the signal
667 * was ignored, so we won't get any callback to
668 * reload the timer. But we need to keep it
669 * ticking in case the signal is deliverable next time.
671 posix_cpu_timer_schedule(timer);
676 * Guts of sys_timer_settime for CPU timers.
677 * This is called with the timer locked and interrupts disabled.
678 * If we return TIMER_RETRY, it's necessary to release the timer's lock
679 * and try again. (This happens when the timer is in the middle of firing.)
681 int posix_cpu_timer_set(struct k_itimer *timer, int flags,
682 struct itimerspec *new, struct itimerspec *old)
684 struct task_struct *p = timer->it.cpu.task;
685 union cpu_time_count old_expires, new_expires, val;
688 if (unlikely(p == NULL)) {
690 * Timer refers to a dead task's clock.
695 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
697 read_lock(&tasklist_lock);
699 * We need the tasklist_lock to protect against reaping that
700 * clears p->signal. If p has just been reaped, we can no
701 * longer get any information about it at all.
703 if (unlikely(p->signal == NULL)) {
704 read_unlock(&tasklist_lock);
706 timer->it.cpu.task = NULL;
711 * Disarm any old timer after extracting its expiry time.
713 BUG_ON(!irqs_disabled());
716 spin_lock(&p->sighand->siglock);
717 old_expires = timer->it.cpu.expires;
718 if (unlikely(timer->it.cpu.firing)) {
719 timer->it.cpu.firing = -1;
722 list_del_init(&timer->it.cpu.entry);
723 spin_unlock(&p->sighand->siglock);
726 * We need to sample the current value to convert the new
727 * value from to relative and absolute, and to convert the
728 * old value from absolute to relative. To set a process
729 * timer, we need a sample to balance the thread expiry
730 * times (in arm_timer). With an absolute time, we must
731 * check if it's already passed. In short, we need a sample.
733 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
734 cpu_clock_sample(timer->it_clock, p, &val);
736 cpu_clock_sample_group(timer->it_clock, p, &val);
740 if (old_expires.sched == 0) {
741 old->it_value.tv_sec = 0;
742 old->it_value.tv_nsec = 0;
745 * Update the timer in case it has
746 * overrun already. If it has,
747 * we'll report it as having overrun
748 * and with the next reloaded timer
749 * already ticking, though we are
750 * swallowing that pending
751 * notification here to install the
754 bump_cpu_timer(timer, val);
755 if (cpu_time_before(timer->it_clock, val,
756 timer->it.cpu.expires)) {
757 old_expires = cpu_time_sub(
759 timer->it.cpu.expires, val);
760 sample_to_timespec(timer->it_clock,
764 old->it_value.tv_nsec = 1;
765 old->it_value.tv_sec = 0;
772 * We are colliding with the timer actually firing.
773 * Punt after filling in the timer's old value, and
774 * disable this firing since we are already reporting
775 * it as an overrun (thanks to bump_cpu_timer above).
777 read_unlock(&tasklist_lock);
781 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
782 cpu_time_add(timer->it_clock, &new_expires, val);
786 * Install the new expiry time (or zero).
787 * For a timer with no notification action, we don't actually
788 * arm the timer (we'll just fake it for timer_gettime).
790 timer->it.cpu.expires = new_expires;
791 if (new_expires.sched != 0 &&
792 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
793 cpu_time_before(timer->it_clock, val, new_expires)) {
794 arm_timer(timer, val);
797 read_unlock(&tasklist_lock);
800 * Install the new reload setting, and
801 * set up the signal and overrun bookkeeping.
803 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
807 * This acts as a modification timestamp for the timer,
808 * so any automatic reload attempt will punt on seeing
809 * that we have reset the timer manually.
811 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
813 timer->it_overrun_last = 0;
814 timer->it_overrun = -1;
816 if (new_expires.sched != 0 &&
817 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
818 !cpu_time_before(timer->it_clock, val, new_expires)) {
820 * The designated time already passed, so we notify
821 * immediately, even if the thread never runs to
822 * accumulate more time on this clock.
824 cpu_timer_fire(timer);
830 sample_to_timespec(timer->it_clock,
831 timer->it.cpu.incr, &old->it_interval);
836 void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
838 union cpu_time_count now;
839 struct task_struct *p = timer->it.cpu.task;
843 * Easy part: convert the reload time.
845 sample_to_timespec(timer->it_clock,
846 timer->it.cpu.incr, &itp->it_interval);
848 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
849 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
853 if (unlikely(p == NULL)) {
855 * This task already died and the timer will never fire.
856 * In this case, expires is actually the dead value.
859 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
865 * Sample the clock to take the difference with the expiry time.
867 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
868 cpu_clock_sample(timer->it_clock, p, &now);
869 clear_dead = p->exit_state;
871 read_lock(&tasklist_lock);
872 if (unlikely(p->signal == NULL)) {
874 * The process has been reaped.
875 * We can't even collect a sample any more.
876 * Call the timer disarmed, nothing else to do.
879 timer->it.cpu.task = NULL;
880 timer->it.cpu.expires.sched = 0;
881 read_unlock(&tasklist_lock);
884 cpu_clock_sample_group(timer->it_clock, p, &now);
885 clear_dead = (unlikely(p->exit_state) &&
886 thread_group_empty(p));
888 read_unlock(&tasklist_lock);
891 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
892 if (timer->it.cpu.incr.sched == 0 &&
893 cpu_time_before(timer->it_clock,
894 timer->it.cpu.expires, now)) {
896 * Do-nothing timer expired and has no reload,
897 * so it's as if it was never set.
899 timer->it.cpu.expires.sched = 0;
900 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
904 * Account for any expirations and reloads that should
907 bump_cpu_timer(timer, now);
910 if (unlikely(clear_dead)) {
912 * We've noticed that the thread is dead, but
913 * not yet reaped. Take this opportunity to
916 clear_dead_task(timer, now);
920 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
921 sample_to_timespec(timer->it_clock,
922 cpu_time_sub(timer->it_clock,
923 timer->it.cpu.expires, now),
927 * The timer should have expired already, but the firing
928 * hasn't taken place yet. Say it's just about to expire.
930 itp->it_value.tv_nsec = 1;
931 itp->it_value.tv_sec = 0;
936 * Check for any per-thread CPU timers that have fired and move them off
937 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
938 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
940 static void check_thread_timers(struct task_struct *tsk,
941 struct list_head *firing)
944 struct list_head *timers = tsk->cpu_timers;
945 struct signal_struct *const sig = tsk->signal;
948 tsk->cputime_expires.prof_exp = cputime_zero;
949 while (!list_empty(timers)) {
950 struct cpu_timer_list *t = list_first_entry(timers,
951 struct cpu_timer_list,
953 if (!--maxfire || cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
954 tsk->cputime_expires.prof_exp = t->expires.cpu;
958 list_move_tail(&t->entry, firing);
963 tsk->cputime_expires.virt_exp = cputime_zero;
964 while (!list_empty(timers)) {
965 struct cpu_timer_list *t = list_first_entry(timers,
966 struct cpu_timer_list,
968 if (!--maxfire || cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
969 tsk->cputime_expires.virt_exp = t->expires.cpu;
973 list_move_tail(&t->entry, firing);
978 tsk->cputime_expires.sched_exp = 0;
979 while (!list_empty(timers)) {
980 struct cpu_timer_list *t = list_first_entry(timers,
981 struct cpu_timer_list,
983 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
984 tsk->cputime_expires.sched_exp = t->expires.sched;
988 list_move_tail(&t->entry, firing);
992 * Check for the special case thread timers.
994 if (sig->rlim[RLIMIT_RTTIME].rlim_cur != RLIM_INFINITY) {
995 unsigned long hard = sig->rlim[RLIMIT_RTTIME].rlim_max;
996 unsigned long *soft = &sig->rlim[RLIMIT_RTTIME].rlim_cur;
998 if (hard != RLIM_INFINITY &&
999 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
1001 * At the hard limit, we just die.
1002 * No need to calculate anything else now.
1004 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1007 if (tsk->rt.timeout > DIV_ROUND_UP(*soft, USEC_PER_SEC/HZ)) {
1009 * At the soft limit, send a SIGXCPU every second.
1011 if (sig->rlim[RLIMIT_RTTIME].rlim_cur
1012 < sig->rlim[RLIMIT_RTTIME].rlim_max) {
1013 sig->rlim[RLIMIT_RTTIME].rlim_cur +=
1017 "RT Watchdog Timeout: %s[%d]\n",
1018 tsk->comm, task_pid_nr(tsk));
1019 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1025 * Check for any per-thread CPU timers that have fired and move them
1026 * off the tsk->*_timers list onto the firing list. Per-thread timers
1027 * have already been taken off.
1029 static void check_process_timers(struct task_struct *tsk,
1030 struct list_head *firing)
1033 struct signal_struct *const sig = tsk->signal;
1034 cputime_t utime, ptime, virt_expires, prof_expires;
1035 unsigned long long sum_sched_runtime, sched_expires;
1036 struct list_head *timers = sig->cpu_timers;
1037 struct task_cputime cputime;
1040 * Don't sample the current process CPU clocks if there are no timers.
1042 if (list_empty(&timers[CPUCLOCK_PROF]) &&
1043 cputime_eq(sig->it_prof_expires, cputime_zero) &&
1044 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
1045 list_empty(&timers[CPUCLOCK_VIRT]) &&
1046 cputime_eq(sig->it_virt_expires, cputime_zero) &&
1047 list_empty(&timers[CPUCLOCK_SCHED])) {
1048 stop_process_timers(tsk);
1053 * Collect the current process totals.
1055 thread_group_cputimer(tsk, &cputime);
1056 utime = cputime.utime;
1057 ptime = cputime_add(utime, cputime.stime);
1058 sum_sched_runtime = cputime.sum_exec_runtime;
1060 prof_expires = cputime_zero;
1061 while (!list_empty(timers)) {
1062 struct cpu_timer_list *tl = list_first_entry(timers,
1063 struct cpu_timer_list,
1065 if (!--maxfire || cputime_lt(ptime, tl->expires.cpu)) {
1066 prof_expires = tl->expires.cpu;
1070 list_move_tail(&tl->entry, firing);
1075 virt_expires = cputime_zero;
1076 while (!list_empty(timers)) {
1077 struct cpu_timer_list *tl = list_first_entry(timers,
1078 struct cpu_timer_list,
1080 if (!--maxfire || cputime_lt(utime, tl->expires.cpu)) {
1081 virt_expires = tl->expires.cpu;
1085 list_move_tail(&tl->entry, firing);
1091 while (!list_empty(timers)) {
1092 struct cpu_timer_list *tl = list_first_entry(timers,
1093 struct cpu_timer_list,
1095 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1096 sched_expires = tl->expires.sched;
1100 list_move_tail(&tl->entry, firing);
1104 * Check for the special case process timers.
1106 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1107 if (cputime_ge(ptime, sig->it_prof_expires)) {
1108 /* ITIMER_PROF fires and reloads. */
1109 sig->it_prof_expires = sig->it_prof_incr;
1110 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1111 sig->it_prof_expires = cputime_add(
1112 sig->it_prof_expires, ptime);
1114 __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
1116 if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
1117 (cputime_eq(prof_expires, cputime_zero) ||
1118 cputime_lt(sig->it_prof_expires, prof_expires))) {
1119 prof_expires = sig->it_prof_expires;
1122 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1123 if (cputime_ge(utime, sig->it_virt_expires)) {
1124 /* ITIMER_VIRTUAL fires and reloads. */
1125 sig->it_virt_expires = sig->it_virt_incr;
1126 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1127 sig->it_virt_expires = cputime_add(
1128 sig->it_virt_expires, utime);
1130 __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
1132 if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
1133 (cputime_eq(virt_expires, cputime_zero) ||
1134 cputime_lt(sig->it_virt_expires, virt_expires))) {
1135 virt_expires = sig->it_virt_expires;
1138 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1139 unsigned long psecs = cputime_to_secs(ptime);
1141 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
1143 * At the hard limit, we just die.
1144 * No need to calculate anything else now.
1146 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1149 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
1151 * At the soft limit, send a SIGXCPU every second.
1153 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1154 if (sig->rlim[RLIMIT_CPU].rlim_cur
1155 < sig->rlim[RLIMIT_CPU].rlim_max) {
1156 sig->rlim[RLIMIT_CPU].rlim_cur++;
1159 x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
1160 if (cputime_eq(prof_expires, cputime_zero) ||
1161 cputime_lt(x, prof_expires)) {
1166 if (!cputime_eq(prof_expires, cputime_zero) &&
1167 (cputime_eq(sig->cputime_expires.prof_exp, cputime_zero) ||
1168 cputime_gt(sig->cputime_expires.prof_exp, prof_expires)))
1169 sig->cputime_expires.prof_exp = prof_expires;
1170 if (!cputime_eq(virt_expires, cputime_zero) &&
1171 (cputime_eq(sig->cputime_expires.virt_exp, cputime_zero) ||
1172 cputime_gt(sig->cputime_expires.virt_exp, virt_expires)))
1173 sig->cputime_expires.virt_exp = virt_expires;
1174 if (sched_expires != 0 &&
1175 (sig->cputime_expires.sched_exp == 0 ||
1176 sig->cputime_expires.sched_exp > sched_expires))
1177 sig->cputime_expires.sched_exp = sched_expires;
1181 * This is called from the signal code (via do_schedule_next_timer)
1182 * when the last timer signal was delivered and we have to reload the timer.
1184 void posix_cpu_timer_schedule(struct k_itimer *timer)
1186 struct task_struct *p = timer->it.cpu.task;
1187 union cpu_time_count now;
1189 if (unlikely(p == NULL))
1191 * The task was cleaned up already, no future firings.
1196 * Fetch the current sample and update the timer's expiry time.
1198 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1199 cpu_clock_sample(timer->it_clock, p, &now);
1200 bump_cpu_timer(timer, now);
1201 if (unlikely(p->exit_state)) {
1202 clear_dead_task(timer, now);
1205 read_lock(&tasklist_lock); /* arm_timer needs it. */
1207 read_lock(&tasklist_lock);
1208 if (unlikely(p->signal == NULL)) {
1210 * The process has been reaped.
1211 * We can't even collect a sample any more.
1214 timer->it.cpu.task = p = NULL;
1215 timer->it.cpu.expires.sched = 0;
1217 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1219 * We've noticed that the thread is dead, but
1220 * not yet reaped. Take this opportunity to
1221 * drop our task ref.
1223 clear_dead_task(timer, now);
1226 cpu_clock_sample_group(timer->it_clock, p, &now);
1227 bump_cpu_timer(timer, now);
1228 /* Leave the tasklist_lock locked for the call below. */
1232 * Now re-arm for the new expiry time.
1234 arm_timer(timer, now);
1237 read_unlock(&tasklist_lock);
1240 timer->it_overrun_last = timer->it_overrun;
1241 timer->it_overrun = -1;
1242 ++timer->it_requeue_pending;
1246 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1248 * @cputime: The struct to compare.
1250 * Checks @cputime to see if all fields are zero. Returns true if all fields
1251 * are zero, false if any field is nonzero.
1253 static inline int task_cputime_zero(const struct task_cputime *cputime)
1255 if (cputime_eq(cputime->utime, cputime_zero) &&
1256 cputime_eq(cputime->stime, cputime_zero) &&
1257 cputime->sum_exec_runtime == 0)
1263 * task_cputime_expired - Compare two task_cputime entities.
1265 * @sample: The task_cputime structure to be checked for expiration.
1266 * @expires: Expiration times, against which @sample will be checked.
1268 * Checks @sample against @expires to see if any field of @sample has expired.
1269 * Returns true if any field of the former is greater than the corresponding
1270 * field of the latter if the latter field is set. Otherwise returns false.
1272 static inline int task_cputime_expired(const struct task_cputime *sample,
1273 const struct task_cputime *expires)
1275 if (!cputime_eq(expires->utime, cputime_zero) &&
1276 cputime_ge(sample->utime, expires->utime))
1278 if (!cputime_eq(expires->stime, cputime_zero) &&
1279 cputime_ge(cputime_add(sample->utime, sample->stime),
1282 if (expires->sum_exec_runtime != 0 &&
1283 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1289 * fastpath_timer_check - POSIX CPU timers fast path.
1291 * @tsk: The task (thread) being checked.
1293 * Check the task and thread group timers. If both are zero (there are no
1294 * timers set) return false. Otherwise snapshot the task and thread group
1295 * timers and compare them with the corresponding expiration times. Return
1296 * true if a timer has expired, else return false.
1298 static inline int fastpath_timer_check(struct task_struct *tsk)
1300 struct signal_struct *sig;
1302 /* tsk == current, ensure it is safe to use ->signal/sighand */
1303 if (unlikely(tsk->exit_state))
1306 if (!task_cputime_zero(&tsk->cputime_expires)) {
1307 struct task_cputime task_sample = {
1308 .utime = tsk->utime,
1309 .stime = tsk->stime,
1310 .sum_exec_runtime = tsk->se.sum_exec_runtime
1313 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1318 if (!task_cputime_zero(&sig->cputime_expires)) {
1319 struct task_cputime group_sample;
1321 thread_group_cputimer(tsk, &group_sample);
1322 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1329 * This is called from the timer interrupt handler. The irq handler has
1330 * already updated our counts. We need to check if any timers fire now.
1331 * Interrupts are disabled.
1333 void run_posix_cpu_timers(struct task_struct *tsk)
1336 struct k_itimer *timer, *next;
1338 BUG_ON(!irqs_disabled());
1341 * The fast path checks that there are no expired thread or thread
1342 * group timers. If that's so, just return.
1344 if (!fastpath_timer_check(tsk))
1347 spin_lock(&tsk->sighand->siglock);
1349 * Here we take off tsk->signal->cpu_timers[N] and
1350 * tsk->cpu_timers[N] all the timers that are firing, and
1351 * put them on the firing list.
1353 check_thread_timers(tsk, &firing);
1354 check_process_timers(tsk, &firing);
1357 * We must release these locks before taking any timer's lock.
1358 * There is a potential race with timer deletion here, as the
1359 * siglock now protects our private firing list. We have set
1360 * the firing flag in each timer, so that a deletion attempt
1361 * that gets the timer lock before we do will give it up and
1362 * spin until we've taken care of that timer below.
1364 spin_unlock(&tsk->sighand->siglock);
1367 * Now that all the timers on our list have the firing flag,
1368 * noone will touch their list entries but us. We'll take
1369 * each timer's lock before clearing its firing flag, so no
1370 * timer call will interfere.
1372 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1374 spin_lock(&timer->it_lock);
1375 list_del_init(&timer->it.cpu.entry);
1376 firing = timer->it.cpu.firing;
1377 timer->it.cpu.firing = 0;
1379 * The firing flag is -1 if we collided with a reset
1380 * of the timer, which already reported this
1381 * almost-firing as an overrun. So don't generate an event.
1383 if (likely(firing >= 0)) {
1384 cpu_timer_fire(timer);
1386 spin_unlock(&timer->it_lock);
1391 * Sample a process (thread group) timer for the given group_leader task.
1392 * Must be called with tasklist_lock held for reading.
1394 static int cpu_timer_sample_group(const clockid_t which_clock,
1395 struct task_struct *p,
1396 union cpu_time_count *cpu)
1398 struct task_cputime cputime;
1400 thread_group_cputimer(p, &cputime);
1401 switch (CPUCLOCK_WHICH(which_clock)) {
1405 cpu->cpu = cputime_add(cputime.utime, cputime.stime);
1408 cpu->cpu = cputime.utime;
1410 case CPUCLOCK_SCHED:
1411 cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
1418 * Set one of the process-wide special case CPU timers.
1419 * The tsk->sighand->siglock must be held by the caller.
1420 * The *newval argument is relative and we update it to be absolute, *oldval
1421 * is absolute and we update it to be relative.
1423 void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1424 cputime_t *newval, cputime_t *oldval)
1426 union cpu_time_count now;
1427 struct list_head *head;
1429 BUG_ON(clock_idx == CPUCLOCK_SCHED);
1430 start_process_timers(tsk);
1431 cpu_timer_sample_group(clock_idx, tsk, &now);
1434 if (!cputime_eq(*oldval, cputime_zero)) {
1435 if (cputime_le(*oldval, now.cpu)) {
1436 /* Just about to fire. */
1437 *oldval = jiffies_to_cputime(1);
1439 *oldval = cputime_sub(*oldval, now.cpu);
1443 if (cputime_eq(*newval, cputime_zero))
1445 *newval = cputime_add(*newval, now.cpu);
1448 * If the RLIMIT_CPU timer will expire before the
1449 * ITIMER_PROF timer, we have nothing else to do.
1451 if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1452 < cputime_to_secs(*newval))
1457 * Check whether there are any process timers already set to fire
1458 * before this one. If so, we don't have anything more to do.
1460 head = &tsk->signal->cpu_timers[clock_idx];
1461 if (list_empty(head) ||
1462 cputime_ge(list_first_entry(head,
1463 struct cpu_timer_list, entry)->expires.cpu,
1465 switch (clock_idx) {
1467 tsk->signal->cputime_expires.prof_exp = *newval;
1470 tsk->signal->cputime_expires.virt_exp = *newval;
1476 static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1477 struct timespec *rqtp, struct itimerspec *it)
1479 struct k_itimer timer;
1483 * Set up a temporary timer and then wait for it to go off.
1485 memset(&timer, 0, sizeof timer);
1486 spin_lock_init(&timer.it_lock);
1487 timer.it_clock = which_clock;
1488 timer.it_overrun = -1;
1489 error = posix_cpu_timer_create(&timer);
1490 timer.it_process = current;
1492 static struct itimerspec zero_it;
1494 memset(it, 0, sizeof *it);
1495 it->it_value = *rqtp;
1497 spin_lock_irq(&timer.it_lock);
1498 error = posix_cpu_timer_set(&timer, flags, it, NULL);
1500 spin_unlock_irq(&timer.it_lock);
1504 while (!signal_pending(current)) {
1505 if (timer.it.cpu.expires.sched == 0) {
1507 * Our timer fired and was reset.
1509 spin_unlock_irq(&timer.it_lock);
1514 * Block until cpu_timer_fire (or a signal) wakes us.
1516 __set_current_state(TASK_INTERRUPTIBLE);
1517 spin_unlock_irq(&timer.it_lock);
1519 spin_lock_irq(&timer.it_lock);
1523 * We were interrupted by a signal.
1525 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
1526 posix_cpu_timer_set(&timer, 0, &zero_it, it);
1527 spin_unlock_irq(&timer.it_lock);
1529 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
1531 * It actually did fire already.
1536 error = -ERESTART_RESTARTBLOCK;
1542 int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1543 struct timespec *rqtp, struct timespec __user *rmtp)
1545 struct restart_block *restart_block =
1546 ¤t_thread_info()->restart_block;
1547 struct itimerspec it;
1551 * Diagnose required errors first.
1553 if (CPUCLOCK_PERTHREAD(which_clock) &&
1554 (CPUCLOCK_PID(which_clock) == 0 ||
1555 CPUCLOCK_PID(which_clock) == current->pid))
1558 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1560 if (error == -ERESTART_RESTARTBLOCK) {
1562 if (flags & TIMER_ABSTIME)
1563 return -ERESTARTNOHAND;
1565 * Report back to the user the time still remaining.
1567 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1570 restart_block->fn = posix_cpu_nsleep_restart;
1571 restart_block->arg0 = which_clock;
1572 restart_block->arg1 = (unsigned long) rmtp;
1573 restart_block->arg2 = rqtp->tv_sec;
1574 restart_block->arg3 = rqtp->tv_nsec;
1579 long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1581 clockid_t which_clock = restart_block->arg0;
1582 struct timespec __user *rmtp;
1584 struct itimerspec it;
1587 rmtp = (struct timespec __user *) restart_block->arg1;
1588 t.tv_sec = restart_block->arg2;
1589 t.tv_nsec = restart_block->arg3;
1591 restart_block->fn = do_no_restart_syscall;
1592 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1594 if (error == -ERESTART_RESTARTBLOCK) {
1596 * Report back to the user the time still remaining.
1598 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1601 restart_block->fn = posix_cpu_nsleep_restart;
1602 restart_block->arg0 = which_clock;
1603 restart_block->arg1 = (unsigned long) rmtp;
1604 restart_block->arg2 = t.tv_sec;
1605 restart_block->arg3 = t.tv_nsec;
1612 #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1613 #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1615 static int process_cpu_clock_getres(const clockid_t which_clock,
1616 struct timespec *tp)
1618 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1620 static int process_cpu_clock_get(const clockid_t which_clock,
1621 struct timespec *tp)
1623 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1625 static int process_cpu_timer_create(struct k_itimer *timer)
1627 timer->it_clock = PROCESS_CLOCK;
1628 return posix_cpu_timer_create(timer);
1630 static int process_cpu_nsleep(const clockid_t which_clock, int flags,
1631 struct timespec *rqtp,
1632 struct timespec __user *rmtp)
1634 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
1636 static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1640 static int thread_cpu_clock_getres(const clockid_t which_clock,
1641 struct timespec *tp)
1643 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1645 static int thread_cpu_clock_get(const clockid_t which_clock,
1646 struct timespec *tp)
1648 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1650 static int thread_cpu_timer_create(struct k_itimer *timer)
1652 timer->it_clock = THREAD_CLOCK;
1653 return posix_cpu_timer_create(timer);
1655 static int thread_cpu_nsleep(const clockid_t which_clock, int flags,
1656 struct timespec *rqtp, struct timespec __user *rmtp)
1660 static long thread_cpu_nsleep_restart(struct restart_block *restart_block)
1665 static __init int init_posix_cpu_timers(void)
1667 struct k_clock process = {
1668 .clock_getres = process_cpu_clock_getres,
1669 .clock_get = process_cpu_clock_get,
1670 .clock_set = do_posix_clock_nosettime,
1671 .timer_create = process_cpu_timer_create,
1672 .nsleep = process_cpu_nsleep,
1673 .nsleep_restart = process_cpu_nsleep_restart,
1675 struct k_clock thread = {
1676 .clock_getres = thread_cpu_clock_getres,
1677 .clock_get = thread_cpu_clock_get,
1678 .clock_set = do_posix_clock_nosettime,
1679 .timer_create = thread_cpu_timer_create,
1680 .nsleep = thread_cpu_nsleep,
1681 .nsleep_restart = thread_cpu_nsleep_restart,
1684 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1685 register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1689 __initcall(init_posix_cpu_timers);