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);
234 * Sample a process (thread group) clock for the given group_leader task.
235 * Must be called with tasklist_lock held for reading.
237 static int cpu_clock_sample_group(const clockid_t which_clock,
238 struct task_struct *p,
239 union cpu_time_count *cpu)
241 struct task_cputime cputime;
243 thread_group_cputime(p, &cputime);
244 switch (CPUCLOCK_WHICH(which_clock)) {
248 cpu->cpu = cputime_add(cputime.utime, cputime.stime);
251 cpu->cpu = cputime.utime;
254 cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
261 int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
263 const pid_t pid = CPUCLOCK_PID(which_clock);
265 union cpu_time_count rtn;
269 * Special case constant value for our own clocks.
270 * We don't have to do any lookup to find ourselves.
272 if (CPUCLOCK_PERTHREAD(which_clock)) {
274 * Sampling just ourselves we can do with no locking.
276 error = cpu_clock_sample(which_clock,
279 read_lock(&tasklist_lock);
280 error = cpu_clock_sample_group(which_clock,
282 read_unlock(&tasklist_lock);
286 * Find the given PID, and validate that the caller
287 * should be able to see it.
289 struct task_struct *p;
291 p = find_task_by_vpid(pid);
293 if (CPUCLOCK_PERTHREAD(which_clock)) {
294 if (same_thread_group(p, current)) {
295 error = cpu_clock_sample(which_clock,
299 read_lock(&tasklist_lock);
300 if (thread_group_leader(p) && p->signal) {
302 cpu_clock_sample_group(which_clock,
305 read_unlock(&tasklist_lock);
313 sample_to_timespec(which_clock, rtn, tp);
319 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
320 * This is called from sys_timer_create with the new timer already locked.
322 int posix_cpu_timer_create(struct k_itimer *new_timer)
325 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
326 struct task_struct *p;
328 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
331 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
332 new_timer->it.cpu.incr.sched = 0;
333 new_timer->it.cpu.expires.sched = 0;
335 read_lock(&tasklist_lock);
336 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
340 p = find_task_by_vpid(pid);
341 if (p && !same_thread_group(p, current))
346 p = current->group_leader;
348 p = find_task_by_vpid(pid);
349 if (p && !thread_group_leader(p))
353 new_timer->it.cpu.task = p;
359 read_unlock(&tasklist_lock);
365 * Clean up a CPU-clock timer that is about to be destroyed.
366 * This is called from timer deletion with the timer already locked.
367 * If we return TIMER_RETRY, it's necessary to release the timer's lock
368 * and try again. (This happens when the timer is in the middle of firing.)
370 int posix_cpu_timer_del(struct k_itimer *timer)
372 struct task_struct *p = timer->it.cpu.task;
375 if (likely(p != NULL)) {
376 read_lock(&tasklist_lock);
377 if (unlikely(p->signal == NULL)) {
379 * We raced with the reaping of the task.
380 * The deletion should have cleared us off the list.
382 BUG_ON(!list_empty(&timer->it.cpu.entry));
384 spin_lock(&p->sighand->siglock);
385 if (timer->it.cpu.firing)
388 list_del(&timer->it.cpu.entry);
389 spin_unlock(&p->sighand->siglock);
391 read_unlock(&tasklist_lock);
401 * Clean out CPU timers still ticking when a thread exited. The task
402 * pointer is cleared, and the expiry time is replaced with the residual
403 * time for later timer_gettime calls to return.
404 * This must be called with the siglock held.
406 static void cleanup_timers(struct list_head *head,
407 cputime_t utime, cputime_t stime,
408 unsigned long long sum_exec_runtime)
410 struct cpu_timer_list *timer, *next;
411 cputime_t ptime = cputime_add(utime, stime);
413 list_for_each_entry_safe(timer, next, head, entry) {
414 list_del_init(&timer->entry);
415 if (cputime_lt(timer->expires.cpu, ptime)) {
416 timer->expires.cpu = cputime_zero;
418 timer->expires.cpu = cputime_sub(timer->expires.cpu,
424 list_for_each_entry_safe(timer, next, head, entry) {
425 list_del_init(&timer->entry);
426 if (cputime_lt(timer->expires.cpu, utime)) {
427 timer->expires.cpu = cputime_zero;
429 timer->expires.cpu = cputime_sub(timer->expires.cpu,
435 list_for_each_entry_safe(timer, next, head, entry) {
436 list_del_init(&timer->entry);
437 if (timer->expires.sched < sum_exec_runtime) {
438 timer->expires.sched = 0;
440 timer->expires.sched -= sum_exec_runtime;
446 * These are both called with the siglock held, when the current thread
447 * is being reaped. When the final (leader) thread in the group is reaped,
448 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
450 void posix_cpu_timers_exit(struct task_struct *tsk)
452 cleanup_timers(tsk->cpu_timers,
453 tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
456 void posix_cpu_timers_exit_group(struct task_struct *tsk)
458 struct task_cputime cputime;
460 thread_group_cputime(tsk, &cputime);
461 cleanup_timers(tsk->signal->cpu_timers,
462 cputime.utime, cputime.stime, cputime.sum_exec_runtime);
465 static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
468 * That's all for this thread or process.
469 * We leave our residual in expires to be reported.
471 put_task_struct(timer->it.cpu.task);
472 timer->it.cpu.task = NULL;
473 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
474 timer->it.cpu.expires,
479 * Insert the timer on the appropriate list before any timers that
480 * expire later. This must be called with the tasklist_lock held
481 * for reading, and interrupts disabled.
483 static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
485 struct task_struct *p = timer->it.cpu.task;
486 struct list_head *head, *listpos;
487 struct cpu_timer_list *const nt = &timer->it.cpu;
488 struct cpu_timer_list *next;
491 head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
492 p->cpu_timers : p->signal->cpu_timers);
493 head += CPUCLOCK_WHICH(timer->it_clock);
495 BUG_ON(!irqs_disabled());
496 spin_lock(&p->sighand->siglock);
499 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
500 list_for_each_entry(next, head, entry) {
501 if (next->expires.sched > nt->expires.sched)
503 listpos = &next->entry;
506 list_for_each_entry(next, head, entry) {
507 if (cputime_gt(next->expires.cpu, nt->expires.cpu))
509 listpos = &next->entry;
512 list_add(&nt->entry, listpos);
514 if (listpos == head) {
516 * We are the new earliest-expiring timer.
517 * If we are a thread timer, there can always
518 * be a process timer telling us to stop earlier.
521 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
522 switch (CPUCLOCK_WHICH(timer->it_clock)) {
526 if (cputime_eq(p->cputime_expires.prof_exp,
528 cputime_gt(p->cputime_expires.prof_exp,
530 p->cputime_expires.prof_exp =
534 if (cputime_eq(p->cputime_expires.virt_exp,
536 cputime_gt(p->cputime_expires.virt_exp,
538 p->cputime_expires.virt_exp =
542 if (p->cputime_expires.sched_exp == 0 ||
543 p->cputime_expires.sched_exp >
545 p->cputime_expires.sched_exp =
551 * For a process timer, set the cached expiration time.
553 switch (CPUCLOCK_WHICH(timer->it_clock)) {
557 if (!cputime_eq(p->signal->it_virt_expires,
559 cputime_lt(p->signal->it_virt_expires,
560 timer->it.cpu.expires.cpu))
562 p->signal->cputime_expires.virt_exp =
563 timer->it.cpu.expires.cpu;
566 if (!cputime_eq(p->signal->it_prof_expires,
568 cputime_lt(p->signal->it_prof_expires,
569 timer->it.cpu.expires.cpu))
571 i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
572 if (i != RLIM_INFINITY &&
573 i <= cputime_to_secs(timer->it.cpu.expires.cpu))
575 p->signal->cputime_expires.prof_exp =
576 timer->it.cpu.expires.cpu;
579 p->signal->cputime_expires.sched_exp =
580 timer->it.cpu.expires.sched;
586 spin_unlock(&p->sighand->siglock);
590 * The timer is locked, fire it and arrange for its reload.
592 static void cpu_timer_fire(struct k_itimer *timer)
594 if (unlikely(timer->sigq == NULL)) {
596 * This a special case for clock_nanosleep,
597 * not a normal timer from sys_timer_create.
599 wake_up_process(timer->it_process);
600 timer->it.cpu.expires.sched = 0;
601 } else if (timer->it.cpu.incr.sched == 0) {
603 * One-shot timer. Clear it as soon as it's fired.
605 posix_timer_event(timer, 0);
606 timer->it.cpu.expires.sched = 0;
607 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
609 * The signal did not get queued because the signal
610 * was ignored, so we won't get any callback to
611 * reload the timer. But we need to keep it
612 * ticking in case the signal is deliverable next time.
614 posix_cpu_timer_schedule(timer);
619 * Guts of sys_timer_settime for CPU timers.
620 * This is called with the timer locked and interrupts disabled.
621 * If we return TIMER_RETRY, it's necessary to release the timer's lock
622 * and try again. (This happens when the timer is in the middle of firing.)
624 int posix_cpu_timer_set(struct k_itimer *timer, int flags,
625 struct itimerspec *new, struct itimerspec *old)
627 struct task_struct *p = timer->it.cpu.task;
628 union cpu_time_count old_expires, new_expires, val;
631 if (unlikely(p == NULL)) {
633 * Timer refers to a dead task's clock.
638 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
640 read_lock(&tasklist_lock);
642 * We need the tasklist_lock to protect against reaping that
643 * clears p->signal. If p has just been reaped, we can no
644 * longer get any information about it at all.
646 if (unlikely(p->signal == NULL)) {
647 read_unlock(&tasklist_lock);
649 timer->it.cpu.task = NULL;
654 * Disarm any old timer after extracting its expiry time.
656 BUG_ON(!irqs_disabled());
659 spin_lock(&p->sighand->siglock);
660 old_expires = timer->it.cpu.expires;
661 if (unlikely(timer->it.cpu.firing)) {
662 timer->it.cpu.firing = -1;
665 list_del_init(&timer->it.cpu.entry);
666 spin_unlock(&p->sighand->siglock);
669 * We need to sample the current value to convert the new
670 * value from to relative and absolute, and to convert the
671 * old value from absolute to relative. To set a process
672 * timer, we need a sample to balance the thread expiry
673 * times (in arm_timer). With an absolute time, we must
674 * check if it's already passed. In short, we need a sample.
676 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
677 cpu_clock_sample(timer->it_clock, p, &val);
679 cpu_clock_sample_group(timer->it_clock, p, &val);
683 if (old_expires.sched == 0) {
684 old->it_value.tv_sec = 0;
685 old->it_value.tv_nsec = 0;
688 * Update the timer in case it has
689 * overrun already. If it has,
690 * we'll report it as having overrun
691 * and with the next reloaded timer
692 * already ticking, though we are
693 * swallowing that pending
694 * notification here to install the
697 bump_cpu_timer(timer, val);
698 if (cpu_time_before(timer->it_clock, val,
699 timer->it.cpu.expires)) {
700 old_expires = cpu_time_sub(
702 timer->it.cpu.expires, val);
703 sample_to_timespec(timer->it_clock,
707 old->it_value.tv_nsec = 1;
708 old->it_value.tv_sec = 0;
715 * We are colliding with the timer actually firing.
716 * Punt after filling in the timer's old value, and
717 * disable this firing since we are already reporting
718 * it as an overrun (thanks to bump_cpu_timer above).
720 read_unlock(&tasklist_lock);
724 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
725 cpu_time_add(timer->it_clock, &new_expires, val);
729 * Install the new expiry time (or zero).
730 * For a timer with no notification action, we don't actually
731 * arm the timer (we'll just fake it for timer_gettime).
733 timer->it.cpu.expires = new_expires;
734 if (new_expires.sched != 0 &&
735 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
736 cpu_time_before(timer->it_clock, val, new_expires)) {
737 arm_timer(timer, val);
740 read_unlock(&tasklist_lock);
743 * Install the new reload setting, and
744 * set up the signal and overrun bookkeeping.
746 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
750 * This acts as a modification timestamp for the timer,
751 * so any automatic reload attempt will punt on seeing
752 * that we have reset the timer manually.
754 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
756 timer->it_overrun_last = 0;
757 timer->it_overrun = -1;
759 if (new_expires.sched != 0 &&
760 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
761 !cpu_time_before(timer->it_clock, val, new_expires)) {
763 * The designated time already passed, so we notify
764 * immediately, even if the thread never runs to
765 * accumulate more time on this clock.
767 cpu_timer_fire(timer);
773 sample_to_timespec(timer->it_clock,
774 timer->it.cpu.incr, &old->it_interval);
779 void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
781 union cpu_time_count now;
782 struct task_struct *p = timer->it.cpu.task;
786 * Easy part: convert the reload time.
788 sample_to_timespec(timer->it_clock,
789 timer->it.cpu.incr, &itp->it_interval);
791 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
792 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
796 if (unlikely(p == NULL)) {
798 * This task already died and the timer will never fire.
799 * In this case, expires is actually the dead value.
802 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
808 * Sample the clock to take the difference with the expiry time.
810 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
811 cpu_clock_sample(timer->it_clock, p, &now);
812 clear_dead = p->exit_state;
814 read_lock(&tasklist_lock);
815 if (unlikely(p->signal == NULL)) {
817 * The process has been reaped.
818 * We can't even collect a sample any more.
819 * Call the timer disarmed, nothing else to do.
822 timer->it.cpu.task = NULL;
823 timer->it.cpu.expires.sched = 0;
824 read_unlock(&tasklist_lock);
827 cpu_clock_sample_group(timer->it_clock, p, &now);
828 clear_dead = (unlikely(p->exit_state) &&
829 thread_group_empty(p));
831 read_unlock(&tasklist_lock);
834 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
835 if (timer->it.cpu.incr.sched == 0 &&
836 cpu_time_before(timer->it_clock,
837 timer->it.cpu.expires, now)) {
839 * Do-nothing timer expired and has no reload,
840 * so it's as if it was never set.
842 timer->it.cpu.expires.sched = 0;
843 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
847 * Account for any expirations and reloads that should
850 bump_cpu_timer(timer, now);
853 if (unlikely(clear_dead)) {
855 * We've noticed that the thread is dead, but
856 * not yet reaped. Take this opportunity to
859 clear_dead_task(timer, now);
863 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
864 sample_to_timespec(timer->it_clock,
865 cpu_time_sub(timer->it_clock,
866 timer->it.cpu.expires, now),
870 * The timer should have expired already, but the firing
871 * hasn't taken place yet. Say it's just about to expire.
873 itp->it_value.tv_nsec = 1;
874 itp->it_value.tv_sec = 0;
879 * Check for any per-thread CPU timers that have fired and move them off
880 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
881 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
883 static void check_thread_timers(struct task_struct *tsk,
884 struct list_head *firing)
887 struct list_head *timers = tsk->cpu_timers;
888 struct signal_struct *const sig = tsk->signal;
891 tsk->cputime_expires.prof_exp = cputime_zero;
892 while (!list_empty(timers)) {
893 struct cpu_timer_list *t = list_first_entry(timers,
894 struct cpu_timer_list,
896 if (!--maxfire || cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
897 tsk->cputime_expires.prof_exp = t->expires.cpu;
901 list_move_tail(&t->entry, firing);
906 tsk->cputime_expires.virt_exp = cputime_zero;
907 while (!list_empty(timers)) {
908 struct cpu_timer_list *t = list_first_entry(timers,
909 struct cpu_timer_list,
911 if (!--maxfire || cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
912 tsk->cputime_expires.virt_exp = t->expires.cpu;
916 list_move_tail(&t->entry, firing);
921 tsk->cputime_expires.sched_exp = 0;
922 while (!list_empty(timers)) {
923 struct cpu_timer_list *t = list_first_entry(timers,
924 struct cpu_timer_list,
926 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
927 tsk->cputime_expires.sched_exp = t->expires.sched;
931 list_move_tail(&t->entry, firing);
935 * Check for the special case thread timers.
937 if (sig->rlim[RLIMIT_RTTIME].rlim_cur != RLIM_INFINITY) {
938 unsigned long hard = sig->rlim[RLIMIT_RTTIME].rlim_max;
939 unsigned long *soft = &sig->rlim[RLIMIT_RTTIME].rlim_cur;
941 if (hard != RLIM_INFINITY &&
942 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
944 * At the hard limit, we just die.
945 * No need to calculate anything else now.
947 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
950 if (tsk->rt.timeout > DIV_ROUND_UP(*soft, USEC_PER_SEC/HZ)) {
952 * At the soft limit, send a SIGXCPU every second.
954 if (sig->rlim[RLIMIT_RTTIME].rlim_cur
955 < sig->rlim[RLIMIT_RTTIME].rlim_max) {
956 sig->rlim[RLIMIT_RTTIME].rlim_cur +=
960 "RT Watchdog Timeout: %s[%d]\n",
961 tsk->comm, task_pid_nr(tsk));
962 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
968 * Check for any per-thread CPU timers that have fired and move them
969 * off the tsk->*_timers list onto the firing list. Per-thread timers
970 * have already been taken off.
972 static void check_process_timers(struct task_struct *tsk,
973 struct list_head *firing)
976 struct signal_struct *const sig = tsk->signal;
977 cputime_t utime, ptime, virt_expires, prof_expires;
978 unsigned long long sum_sched_runtime, sched_expires;
979 struct list_head *timers = sig->cpu_timers;
980 struct task_cputime cputime;
983 * Don't sample the current process CPU clocks if there are no timers.
985 if (list_empty(&timers[CPUCLOCK_PROF]) &&
986 cputime_eq(sig->it_prof_expires, cputime_zero) &&
987 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
988 list_empty(&timers[CPUCLOCK_VIRT]) &&
989 cputime_eq(sig->it_virt_expires, cputime_zero) &&
990 list_empty(&timers[CPUCLOCK_SCHED]))
994 * Collect the current process totals.
996 thread_group_cputime(tsk, &cputime);
997 utime = cputime.utime;
998 ptime = cputime_add(utime, cputime.stime);
999 sum_sched_runtime = cputime.sum_exec_runtime;
1001 prof_expires = cputime_zero;
1002 while (!list_empty(timers)) {
1003 struct cpu_timer_list *tl = list_first_entry(timers,
1004 struct cpu_timer_list,
1006 if (!--maxfire || cputime_lt(ptime, tl->expires.cpu)) {
1007 prof_expires = tl->expires.cpu;
1011 list_move_tail(&tl->entry, firing);
1016 virt_expires = cputime_zero;
1017 while (!list_empty(timers)) {
1018 struct cpu_timer_list *tl = list_first_entry(timers,
1019 struct cpu_timer_list,
1021 if (!--maxfire || cputime_lt(utime, tl->expires.cpu)) {
1022 virt_expires = tl->expires.cpu;
1026 list_move_tail(&tl->entry, firing);
1032 while (!list_empty(timers)) {
1033 struct cpu_timer_list *tl = list_first_entry(timers,
1034 struct cpu_timer_list,
1036 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1037 sched_expires = tl->expires.sched;
1041 list_move_tail(&tl->entry, firing);
1045 * Check for the special case process timers.
1047 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1048 if (cputime_ge(ptime, sig->it_prof_expires)) {
1049 /* ITIMER_PROF fires and reloads. */
1050 sig->it_prof_expires = sig->it_prof_incr;
1051 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1052 sig->it_prof_expires = cputime_add(
1053 sig->it_prof_expires, ptime);
1055 __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
1057 if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
1058 (cputime_eq(prof_expires, cputime_zero) ||
1059 cputime_lt(sig->it_prof_expires, prof_expires))) {
1060 prof_expires = sig->it_prof_expires;
1063 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1064 if (cputime_ge(utime, sig->it_virt_expires)) {
1065 /* ITIMER_VIRTUAL fires and reloads. */
1066 sig->it_virt_expires = sig->it_virt_incr;
1067 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1068 sig->it_virt_expires = cputime_add(
1069 sig->it_virt_expires, utime);
1071 __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
1073 if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
1074 (cputime_eq(virt_expires, cputime_zero) ||
1075 cputime_lt(sig->it_virt_expires, virt_expires))) {
1076 virt_expires = sig->it_virt_expires;
1079 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1080 unsigned long psecs = cputime_to_secs(ptime);
1082 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
1084 * At the hard limit, we just die.
1085 * No need to calculate anything else now.
1087 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1090 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
1092 * At the soft limit, send a SIGXCPU every second.
1094 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1095 if (sig->rlim[RLIMIT_CPU].rlim_cur
1096 < sig->rlim[RLIMIT_CPU].rlim_max) {
1097 sig->rlim[RLIMIT_CPU].rlim_cur++;
1100 x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
1101 if (cputime_eq(prof_expires, cputime_zero) ||
1102 cputime_lt(x, prof_expires)) {
1107 if (!cputime_eq(prof_expires, cputime_zero) &&
1108 (cputime_eq(sig->cputime_expires.prof_exp, cputime_zero) ||
1109 cputime_gt(sig->cputime_expires.prof_exp, prof_expires)))
1110 sig->cputime_expires.prof_exp = prof_expires;
1111 if (!cputime_eq(virt_expires, cputime_zero) &&
1112 (cputime_eq(sig->cputime_expires.virt_exp, cputime_zero) ||
1113 cputime_gt(sig->cputime_expires.virt_exp, virt_expires)))
1114 sig->cputime_expires.virt_exp = virt_expires;
1115 if (sched_expires != 0 &&
1116 (sig->cputime_expires.sched_exp == 0 ||
1117 sig->cputime_expires.sched_exp > sched_expires))
1118 sig->cputime_expires.sched_exp = sched_expires;
1122 * This is called from the signal code (via do_schedule_next_timer)
1123 * when the last timer signal was delivered and we have to reload the timer.
1125 void posix_cpu_timer_schedule(struct k_itimer *timer)
1127 struct task_struct *p = timer->it.cpu.task;
1128 union cpu_time_count now;
1130 if (unlikely(p == NULL))
1132 * The task was cleaned up already, no future firings.
1137 * Fetch the current sample and update the timer's expiry time.
1139 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1140 cpu_clock_sample(timer->it_clock, p, &now);
1141 bump_cpu_timer(timer, now);
1142 if (unlikely(p->exit_state)) {
1143 clear_dead_task(timer, now);
1146 read_lock(&tasklist_lock); /* arm_timer needs it. */
1148 read_lock(&tasklist_lock);
1149 if (unlikely(p->signal == NULL)) {
1151 * The process has been reaped.
1152 * We can't even collect a sample any more.
1155 timer->it.cpu.task = p = NULL;
1156 timer->it.cpu.expires.sched = 0;
1158 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1160 * We've noticed that the thread is dead, but
1161 * not yet reaped. Take this opportunity to
1162 * drop our task ref.
1164 clear_dead_task(timer, now);
1167 cpu_clock_sample_group(timer->it_clock, p, &now);
1168 bump_cpu_timer(timer, now);
1169 /* Leave the tasklist_lock locked for the call below. */
1173 * Now re-arm for the new expiry time.
1175 arm_timer(timer, now);
1178 read_unlock(&tasklist_lock);
1181 timer->it_overrun_last = timer->it_overrun;
1182 timer->it_overrun = -1;
1183 ++timer->it_requeue_pending;
1187 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1189 * @cputime: The struct to compare.
1191 * Checks @cputime to see if all fields are zero. Returns true if all fields
1192 * are zero, false if any field is nonzero.
1194 static inline int task_cputime_zero(const struct task_cputime *cputime)
1196 if (cputime_eq(cputime->utime, cputime_zero) &&
1197 cputime_eq(cputime->stime, cputime_zero) &&
1198 cputime->sum_exec_runtime == 0)
1204 * task_cputime_expired - Compare two task_cputime entities.
1206 * @sample: The task_cputime structure to be checked for expiration.
1207 * @expires: Expiration times, against which @sample will be checked.
1209 * Checks @sample against @expires to see if any field of @sample has expired.
1210 * Returns true if any field of the former is greater than the corresponding
1211 * field of the latter if the latter field is set. Otherwise returns false.
1213 static inline int task_cputime_expired(const struct task_cputime *sample,
1214 const struct task_cputime *expires)
1216 if (!cputime_eq(expires->utime, cputime_zero) &&
1217 cputime_ge(sample->utime, expires->utime))
1219 if (!cputime_eq(expires->stime, cputime_zero) &&
1220 cputime_ge(cputime_add(sample->utime, sample->stime),
1223 if (expires->sum_exec_runtime != 0 &&
1224 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1230 * fastpath_timer_check - POSIX CPU timers fast path.
1232 * @tsk: The task (thread) being checked.
1234 * Check the task and thread group timers. If both are zero (there are no
1235 * timers set) return false. Otherwise snapshot the task and thread group
1236 * timers and compare them with the corresponding expiration times. Return
1237 * true if a timer has expired, else return false.
1239 static inline int fastpath_timer_check(struct task_struct *tsk)
1241 struct signal_struct *sig;
1243 /* tsk == current, ensure it is safe to use ->signal/sighand */
1244 if (unlikely(tsk->exit_state))
1247 if (!task_cputime_zero(&tsk->cputime_expires)) {
1248 struct task_cputime task_sample = {
1249 .utime = tsk->utime,
1250 .stime = tsk->stime,
1251 .sum_exec_runtime = tsk->se.sum_exec_runtime
1254 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1259 if (!task_cputime_zero(&sig->cputime_expires)) {
1260 struct task_cputime group_sample;
1262 thread_group_cputime(tsk, &group_sample);
1263 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1270 * This is called from the timer interrupt handler. The irq handler has
1271 * already updated our counts. We need to check if any timers fire now.
1272 * Interrupts are disabled.
1274 void run_posix_cpu_timers(struct task_struct *tsk)
1277 struct k_itimer *timer, *next;
1279 BUG_ON(!irqs_disabled());
1282 * The fast path checks that there are no expired thread or thread
1283 * group timers. If that's so, just return.
1285 if (!fastpath_timer_check(tsk))
1288 spin_lock(&tsk->sighand->siglock);
1290 * Here we take off tsk->signal->cpu_timers[N] and
1291 * tsk->cpu_timers[N] all the timers that are firing, and
1292 * put them on the firing list.
1294 check_thread_timers(tsk, &firing);
1295 check_process_timers(tsk, &firing);
1298 * We must release these locks before taking any timer's lock.
1299 * There is a potential race with timer deletion here, as the
1300 * siglock now protects our private firing list. We have set
1301 * the firing flag in each timer, so that a deletion attempt
1302 * that gets the timer lock before we do will give it up and
1303 * spin until we've taken care of that timer below.
1305 spin_unlock(&tsk->sighand->siglock);
1308 * Now that all the timers on our list have the firing flag,
1309 * noone will touch their list entries but us. We'll take
1310 * each timer's lock before clearing its firing flag, so no
1311 * timer call will interfere.
1313 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1315 spin_lock(&timer->it_lock);
1316 list_del_init(&timer->it.cpu.entry);
1317 firing = timer->it.cpu.firing;
1318 timer->it.cpu.firing = 0;
1320 * The firing flag is -1 if we collided with a reset
1321 * of the timer, which already reported this
1322 * almost-firing as an overrun. So don't generate an event.
1324 if (likely(firing >= 0)) {
1325 cpu_timer_fire(timer);
1327 spin_unlock(&timer->it_lock);
1332 * Set one of the process-wide special case CPU timers.
1333 * The tsk->sighand->siglock must be held by the caller.
1334 * The *newval argument is relative and we update it to be absolute, *oldval
1335 * is absolute and we update it to be relative.
1337 void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1338 cputime_t *newval, cputime_t *oldval)
1340 union cpu_time_count now;
1341 struct list_head *head;
1343 BUG_ON(clock_idx == CPUCLOCK_SCHED);
1344 cpu_clock_sample_group(clock_idx, tsk, &now);
1347 if (!cputime_eq(*oldval, cputime_zero)) {
1348 if (cputime_le(*oldval, now.cpu)) {
1349 /* Just about to fire. */
1350 *oldval = jiffies_to_cputime(1);
1352 *oldval = cputime_sub(*oldval, now.cpu);
1356 if (cputime_eq(*newval, cputime_zero))
1358 *newval = cputime_add(*newval, now.cpu);
1361 * If the RLIMIT_CPU timer will expire before the
1362 * ITIMER_PROF timer, we have nothing else to do.
1364 if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1365 < cputime_to_secs(*newval))
1370 * Check whether there are any process timers already set to fire
1371 * before this one. If so, we don't have anything more to do.
1373 head = &tsk->signal->cpu_timers[clock_idx];
1374 if (list_empty(head) ||
1375 cputime_ge(list_first_entry(head,
1376 struct cpu_timer_list, entry)->expires.cpu,
1378 switch (clock_idx) {
1380 tsk->signal->cputime_expires.prof_exp = *newval;
1383 tsk->signal->cputime_expires.virt_exp = *newval;
1389 static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1390 struct timespec *rqtp, struct itimerspec *it)
1392 struct k_itimer timer;
1396 * Set up a temporary timer and then wait for it to go off.
1398 memset(&timer, 0, sizeof timer);
1399 spin_lock_init(&timer.it_lock);
1400 timer.it_clock = which_clock;
1401 timer.it_overrun = -1;
1402 error = posix_cpu_timer_create(&timer);
1403 timer.it_process = current;
1405 static struct itimerspec zero_it;
1407 memset(it, 0, sizeof *it);
1408 it->it_value = *rqtp;
1410 spin_lock_irq(&timer.it_lock);
1411 error = posix_cpu_timer_set(&timer, flags, it, NULL);
1413 spin_unlock_irq(&timer.it_lock);
1417 while (!signal_pending(current)) {
1418 if (timer.it.cpu.expires.sched == 0) {
1420 * Our timer fired and was reset.
1422 spin_unlock_irq(&timer.it_lock);
1427 * Block until cpu_timer_fire (or a signal) wakes us.
1429 __set_current_state(TASK_INTERRUPTIBLE);
1430 spin_unlock_irq(&timer.it_lock);
1432 spin_lock_irq(&timer.it_lock);
1436 * We were interrupted by a signal.
1438 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
1439 posix_cpu_timer_set(&timer, 0, &zero_it, it);
1440 spin_unlock_irq(&timer.it_lock);
1442 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
1444 * It actually did fire already.
1449 error = -ERESTART_RESTARTBLOCK;
1455 int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1456 struct timespec *rqtp, struct timespec __user *rmtp)
1458 struct restart_block *restart_block =
1459 ¤t_thread_info()->restart_block;
1460 struct itimerspec it;
1464 * Diagnose required errors first.
1466 if (CPUCLOCK_PERTHREAD(which_clock) &&
1467 (CPUCLOCK_PID(which_clock) == 0 ||
1468 CPUCLOCK_PID(which_clock) == current->pid))
1471 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1473 if (error == -ERESTART_RESTARTBLOCK) {
1475 if (flags & TIMER_ABSTIME)
1476 return -ERESTARTNOHAND;
1478 * Report back to the user the time still remaining.
1480 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1483 restart_block->fn = posix_cpu_nsleep_restart;
1484 restart_block->arg0 = which_clock;
1485 restart_block->arg1 = (unsigned long) rmtp;
1486 restart_block->arg2 = rqtp->tv_sec;
1487 restart_block->arg3 = rqtp->tv_nsec;
1492 long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1494 clockid_t which_clock = restart_block->arg0;
1495 struct timespec __user *rmtp;
1497 struct itimerspec it;
1500 rmtp = (struct timespec __user *) restart_block->arg1;
1501 t.tv_sec = restart_block->arg2;
1502 t.tv_nsec = restart_block->arg3;
1504 restart_block->fn = do_no_restart_syscall;
1505 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1507 if (error == -ERESTART_RESTARTBLOCK) {
1509 * Report back to the user the time still remaining.
1511 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1514 restart_block->fn = posix_cpu_nsleep_restart;
1515 restart_block->arg0 = which_clock;
1516 restart_block->arg1 = (unsigned long) rmtp;
1517 restart_block->arg2 = t.tv_sec;
1518 restart_block->arg3 = t.tv_nsec;
1525 #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1526 #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1528 static int process_cpu_clock_getres(const clockid_t which_clock,
1529 struct timespec *tp)
1531 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1533 static int process_cpu_clock_get(const clockid_t which_clock,
1534 struct timespec *tp)
1536 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1538 static int process_cpu_timer_create(struct k_itimer *timer)
1540 timer->it_clock = PROCESS_CLOCK;
1541 return posix_cpu_timer_create(timer);
1543 static int process_cpu_nsleep(const clockid_t which_clock, int flags,
1544 struct timespec *rqtp,
1545 struct timespec __user *rmtp)
1547 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
1549 static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1553 static int thread_cpu_clock_getres(const clockid_t which_clock,
1554 struct timespec *tp)
1556 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1558 static int thread_cpu_clock_get(const clockid_t which_clock,
1559 struct timespec *tp)
1561 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1563 static int thread_cpu_timer_create(struct k_itimer *timer)
1565 timer->it_clock = THREAD_CLOCK;
1566 return posix_cpu_timer_create(timer);
1568 static int thread_cpu_nsleep(const clockid_t which_clock, int flags,
1569 struct timespec *rqtp, struct timespec __user *rmtp)
1573 static long thread_cpu_nsleep_restart(struct restart_block *restart_block)
1578 static __init int init_posix_cpu_timers(void)
1580 struct k_clock process = {
1581 .clock_getres = process_cpu_clock_getres,
1582 .clock_get = process_cpu_clock_get,
1583 .clock_set = do_posix_clock_nosettime,
1584 .timer_create = process_cpu_timer_create,
1585 .nsleep = process_cpu_nsleep,
1586 .nsleep_restart = process_cpu_nsleep_restart,
1588 struct k_clock thread = {
1589 .clock_getres = thread_cpu_clock_getres,
1590 .clock_get = thread_cpu_clock_get,
1591 .clock_set = do_posix_clock_nosettime,
1592 .timer_create = thread_cpu_timer_create,
1593 .nsleep = thread_cpu_nsleep,
1594 .nsleep_restart = thread_cpu_nsleep_restart,
1597 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1598 register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1602 __initcall(init_posix_cpu_timers);