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>
9 #include <linux/vs_cvirt.h>
11 static int check_clock(clockid_t which_clock)
14 struct task_struct *p;
15 const pid_t pid = CPUCLOCK_PID(which_clock);
17 if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
23 read_lock(&tasklist_lock);
24 p = find_task_by_pid(pid);
25 if (!p || (CPUCLOCK_PERTHREAD(which_clock) ?
26 p->tgid != current->tgid : p->tgid != pid)) {
29 read_unlock(&tasklist_lock);
34 static inline union cpu_time_count
35 timespec_to_sample(clockid_t which_clock, const struct timespec *tp)
37 union cpu_time_count ret;
38 ret.sched = 0; /* high half always zero when .cpu used */
39 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
40 ret.sched = tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
42 ret.cpu = timespec_to_cputime(tp);
47 static void sample_to_timespec(clockid_t which_clock,
48 union cpu_time_count cpu,
51 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
52 tp->tv_sec = div_long_long_rem(cpu.sched,
53 NSEC_PER_SEC, &tp->tv_nsec);
55 cputime_to_timespec(cpu.cpu, tp);
59 static inline int cpu_time_before(clockid_t which_clock,
60 union cpu_time_count now,
61 union cpu_time_count then)
63 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
64 return now.sched < then.sched;
66 return cputime_lt(now.cpu, then.cpu);
69 static inline void cpu_time_add(clockid_t which_clock,
70 union cpu_time_count *acc,
71 union cpu_time_count val)
73 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
74 acc->sched += val.sched;
76 acc->cpu = cputime_add(acc->cpu, val.cpu);
79 static inline union cpu_time_count cpu_time_sub(clockid_t which_clock,
80 union cpu_time_count a,
81 union cpu_time_count b)
83 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
86 a.cpu = cputime_sub(a.cpu, b.cpu);
92 * Update expiry time from increment, and increase overrun count,
93 * given the current clock sample.
95 static inline void bump_cpu_timer(struct k_itimer *timer,
96 union cpu_time_count now)
100 if (timer->it.cpu.incr.sched == 0)
103 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
104 unsigned long long delta, incr;
106 if (now.sched < timer->it.cpu.expires.sched)
108 incr = timer->it.cpu.incr.sched;
109 delta = now.sched + incr - timer->it.cpu.expires.sched;
110 /* Don't use (incr*2 < delta), incr*2 might overflow. */
111 for (i = 0; incr < delta - incr; i++)
113 for (; i >= 0; incr >>= 1, i--) {
116 timer->it.cpu.expires.sched += incr;
117 timer->it_overrun += 1 << i;
121 cputime_t delta, incr;
123 if (cputime_lt(now.cpu, timer->it.cpu.expires.cpu))
125 incr = timer->it.cpu.incr.cpu;
126 delta = cputime_sub(cputime_add(now.cpu, incr),
127 timer->it.cpu.expires.cpu);
128 /* Don't use (incr*2 < delta), incr*2 might overflow. */
129 for (i = 0; cputime_lt(incr, cputime_sub(delta, incr)); i++)
130 incr = cputime_add(incr, incr);
131 for (; i >= 0; incr = cputime_halve(incr), i--) {
132 if (cputime_le(delta, incr))
134 timer->it.cpu.expires.cpu =
135 cputime_add(timer->it.cpu.expires.cpu, incr);
136 timer->it_overrun += 1 << i;
137 delta = cputime_sub(delta, incr);
142 static inline cputime_t prof_ticks(struct task_struct *p)
144 return cputime_add(p->utime, p->stime);
146 static inline cputime_t virt_ticks(struct task_struct *p)
150 static inline unsigned long long sched_ns(struct task_struct *p)
152 return (p == current) ? current_sched_time(p) : p->sched_time;
155 int posix_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
157 int error = check_clock(which_clock);
160 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
161 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
163 * If sched_clock is using a cycle counter, we
164 * don't have any idea of its true resolution
165 * exported, but it is much more than 1s/HZ.
173 int posix_cpu_clock_set(clockid_t which_clock, const struct timespec *tp)
176 * You can never reset a CPU clock, but we check for other errors
177 * in the call before failing with EPERM.
179 int error = check_clock(which_clock);
188 * Sample a per-thread clock for the given task.
190 static int cpu_clock_sample(clockid_t which_clock, struct task_struct *p,
191 union cpu_time_count *cpu)
193 switch (CPUCLOCK_WHICH(which_clock)) {
197 cpu->cpu = prof_ticks(p);
200 cpu->cpu = virt_ticks(p);
203 cpu->sched = sched_ns(p);
210 * Sample a process (thread group) clock for the given group_leader task.
211 * Must be called with tasklist_lock held for reading.
212 * Must be called with tasklist_lock held for reading, and p->sighand->siglock.
214 static int cpu_clock_sample_group_locked(unsigned int clock_idx,
215 struct task_struct *p,
216 union cpu_time_count *cpu)
218 struct task_struct *t = p;
223 cpu->cpu = cputime_add(p->signal->utime, p->signal->stime);
225 cpu->cpu = cputime_add(cpu->cpu, prof_ticks(t));
230 cpu->cpu = p->signal->utime;
232 cpu->cpu = cputime_add(cpu->cpu, virt_ticks(t));
237 cpu->sched = p->signal->sched_time;
238 /* Add in each other live thread. */
239 while ((t = next_thread(t)) != p) {
240 cpu->sched += t->sched_time;
242 if (p->tgid == current->tgid) {
244 * We're sampling ourselves, so include the
245 * cycles not yet banked. We still omit
246 * other threads running on other CPUs,
247 * so the total can always be behind as
248 * much as max(nthreads-1,ncpus) * (NSEC_PER_SEC/HZ).
250 cpu->sched += current_sched_time(current);
252 cpu->sched += p->sched_time;
260 * Sample a process (thread group) clock for the given group_leader task.
261 * Must be called with tasklist_lock held for reading.
263 static int cpu_clock_sample_group(clockid_t which_clock,
264 struct task_struct *p,
265 union cpu_time_count *cpu)
269 spin_lock_irqsave(&p->sighand->siglock, flags);
270 ret = cpu_clock_sample_group_locked(CPUCLOCK_WHICH(which_clock), p,
272 spin_unlock_irqrestore(&p->sighand->siglock, flags);
277 int posix_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
279 const pid_t pid = CPUCLOCK_PID(which_clock);
281 union cpu_time_count rtn;
285 * Special case constant value for our own clocks.
286 * We don't have to do any lookup to find ourselves.
288 if (CPUCLOCK_PERTHREAD(which_clock)) {
290 * Sampling just ourselves we can do with no locking.
292 error = cpu_clock_sample(which_clock,
295 read_lock(&tasklist_lock);
296 error = cpu_clock_sample_group(which_clock,
298 read_unlock(&tasklist_lock);
302 * Find the given PID, and validate that the caller
303 * should be able to see it.
305 struct task_struct *p;
306 read_lock(&tasklist_lock);
307 p = find_task_by_pid(pid);
309 if (CPUCLOCK_PERTHREAD(which_clock)) {
310 if (p->tgid == current->tgid) {
311 error = cpu_clock_sample(which_clock,
314 } else if (p->tgid == pid && p->signal) {
315 error = cpu_clock_sample_group(which_clock,
319 read_unlock(&tasklist_lock);
324 sample_to_timespec(which_clock, rtn, tp);
330 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
331 * This is called from sys_timer_create with the new timer already locked.
333 int posix_cpu_timer_create(struct k_itimer *new_timer)
336 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
337 struct task_struct *p;
339 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
342 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
343 new_timer->it.cpu.incr.sched = 0;
344 new_timer->it.cpu.expires.sched = 0;
346 read_lock(&tasklist_lock);
347 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
351 p = find_task_by_pid(pid);
352 if (p && p->tgid != current->tgid)
357 p = current->group_leader;
359 p = find_task_by_pid(pid);
360 if (p && p->tgid != pid)
364 new_timer->it.cpu.task = p;
370 read_unlock(&tasklist_lock);
376 * Clean up a CPU-clock timer that is about to be destroyed.
377 * This is called from timer deletion with the timer already locked.
378 * If we return TIMER_RETRY, it's necessary to release the timer's lock
379 * and try again. (This happens when the timer is in the middle of firing.)
381 int posix_cpu_timer_del(struct k_itimer *timer)
383 struct task_struct *p = timer->it.cpu.task;
385 if (timer->it.cpu.firing)
388 if (unlikely(p == NULL))
391 if (!list_empty(&timer->it.cpu.entry)) {
392 read_lock(&tasklist_lock);
393 if (unlikely(p->signal == NULL)) {
395 * We raced with the reaping of the task.
396 * The deletion should have cleared us off the list.
398 BUG_ON(!list_empty(&timer->it.cpu.entry));
401 * Take us off the task's timer list.
403 spin_lock(&p->sighand->siglock);
404 list_del(&timer->it.cpu.entry);
405 spin_unlock(&p->sighand->siglock);
407 read_unlock(&tasklist_lock);
415 * Clean out CPU timers still ticking when a thread exited. The task
416 * pointer is cleared, and the expiry time is replaced with the residual
417 * time for later timer_gettime calls to return.
418 * This must be called with the siglock held.
420 static void cleanup_timers(struct list_head *head,
421 cputime_t utime, cputime_t stime,
422 unsigned long long sched_time)
424 struct cpu_timer_list *timer, *next;
425 cputime_t ptime = cputime_add(utime, stime);
427 list_for_each_entry_safe(timer, next, head, entry) {
429 list_del_init(&timer->entry);
430 if (cputime_lt(timer->expires.cpu, ptime)) {
431 timer->expires.cpu = cputime_zero;
433 timer->expires.cpu = cputime_sub(timer->expires.cpu,
439 list_for_each_entry_safe(timer, next, head, entry) {
441 list_del_init(&timer->entry);
442 if (cputime_lt(timer->expires.cpu, utime)) {
443 timer->expires.cpu = cputime_zero;
445 timer->expires.cpu = cputime_sub(timer->expires.cpu,
451 list_for_each_entry_safe(timer, next, head, entry) {
453 list_del_init(&timer->entry);
454 if (timer->expires.sched < sched_time) {
455 timer->expires.sched = 0;
457 timer->expires.sched -= sched_time;
463 * These are both called with the siglock held, when the current thread
464 * is being reaped. When the final (leader) thread in the group is reaped,
465 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
467 void posix_cpu_timers_exit(struct task_struct *tsk)
469 cleanup_timers(tsk->cpu_timers,
470 tsk->utime, tsk->stime, tsk->sched_time);
473 void posix_cpu_timers_exit_group(struct task_struct *tsk)
475 cleanup_timers(tsk->signal->cpu_timers,
476 cputime_add(tsk->utime, tsk->signal->utime),
477 cputime_add(tsk->stime, tsk->signal->stime),
478 tsk->sched_time + tsk->signal->sched_time);
483 * Set the expiry times of all the threads in the process so one of them
484 * will go off before the process cumulative expiry total is reached.
486 static void process_timer_rebalance(struct task_struct *p,
487 unsigned int clock_idx,
488 union cpu_time_count expires,
489 union cpu_time_count val)
491 cputime_t ticks, left;
492 unsigned long long ns, nsleft;
493 struct task_struct *t = p;
494 unsigned int nthreads = atomic_read(&p->signal->live);
501 left = cputime_div(cputime_sub(expires.cpu, val.cpu),
504 if (!unlikely(t->exit_state)) {
505 ticks = cputime_add(prof_ticks(t), left);
506 if (cputime_eq(t->it_prof_expires,
508 cputime_gt(t->it_prof_expires, ticks)) {
509 t->it_prof_expires = ticks;
516 left = cputime_div(cputime_sub(expires.cpu, val.cpu),
519 if (!unlikely(t->exit_state)) {
520 ticks = cputime_add(virt_ticks(t), left);
521 if (cputime_eq(t->it_virt_expires,
523 cputime_gt(t->it_virt_expires, ticks)) {
524 t->it_virt_expires = ticks;
531 nsleft = expires.sched - val.sched;
532 do_div(nsleft, nthreads);
534 if (!unlikely(t->exit_state)) {
535 ns = t->sched_time + nsleft;
536 if (t->it_sched_expires == 0 ||
537 t->it_sched_expires > ns) {
538 t->it_sched_expires = ns;
547 static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
550 * That's all for this thread or process.
551 * We leave our residual in expires to be reported.
553 put_task_struct(timer->it.cpu.task);
554 timer->it.cpu.task = NULL;
555 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
556 timer->it.cpu.expires,
561 * Insert the timer on the appropriate list before any timers that
562 * expire later. This must be called with the tasklist_lock held
563 * for reading, and interrupts disabled.
565 static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
567 struct task_struct *p = timer->it.cpu.task;
568 struct list_head *head, *listpos;
569 struct cpu_timer_list *const nt = &timer->it.cpu;
570 struct cpu_timer_list *next;
573 head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
574 p->cpu_timers : p->signal->cpu_timers);
575 head += CPUCLOCK_WHICH(timer->it_clock);
577 BUG_ON(!irqs_disabled());
578 spin_lock(&p->sighand->siglock);
581 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
582 list_for_each_entry(next, head, entry) {
583 if (next->expires.sched > nt->expires.sched) {
584 listpos = &next->entry;
589 list_for_each_entry(next, head, entry) {
590 if (cputime_gt(next->expires.cpu, nt->expires.cpu)) {
591 listpos = &next->entry;
596 list_add(&nt->entry, listpos);
598 if (listpos == head) {
600 * We are the new earliest-expiring timer.
601 * If we are a thread timer, there can always
602 * be a process timer telling us to stop earlier.
605 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
606 switch (CPUCLOCK_WHICH(timer->it_clock)) {
610 if (cputime_eq(p->it_prof_expires,
612 cputime_gt(p->it_prof_expires,
614 p->it_prof_expires = nt->expires.cpu;
617 if (cputime_eq(p->it_virt_expires,
619 cputime_gt(p->it_virt_expires,
621 p->it_virt_expires = nt->expires.cpu;
624 if (p->it_sched_expires == 0 ||
625 p->it_sched_expires > nt->expires.sched)
626 p->it_sched_expires = nt->expires.sched;
631 * For a process timer, we must balance
632 * all the live threads' expirations.
634 switch (CPUCLOCK_WHICH(timer->it_clock)) {
638 if (!cputime_eq(p->signal->it_virt_expires,
640 cputime_lt(p->signal->it_virt_expires,
641 timer->it.cpu.expires.cpu))
645 if (!cputime_eq(p->signal->it_prof_expires,
647 cputime_lt(p->signal->it_prof_expires,
648 timer->it.cpu.expires.cpu))
650 i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
651 if (i != RLIM_INFINITY &&
652 i <= cputime_to_secs(timer->it.cpu.expires.cpu))
657 process_timer_rebalance(
659 CPUCLOCK_WHICH(timer->it_clock),
660 timer->it.cpu.expires, now);
666 spin_unlock(&p->sighand->siglock);
670 * The timer is locked, fire it and arrange for its reload.
672 static void cpu_timer_fire(struct k_itimer *timer)
674 if (unlikely(timer->sigq == NULL)) {
676 * This a special case for clock_nanosleep,
677 * not a normal timer from sys_timer_create.
679 wake_up_process(timer->it_process);
680 timer->it.cpu.expires.sched = 0;
681 } else if (timer->it.cpu.incr.sched == 0) {
683 * One-shot timer. Clear it as soon as it's fired.
685 posix_timer_event(timer, 0);
686 timer->it.cpu.expires.sched = 0;
687 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
689 * The signal did not get queued because the signal
690 * was ignored, so we won't get any callback to
691 * reload the timer. But we need to keep it
692 * ticking in case the signal is deliverable next time.
694 posix_cpu_timer_schedule(timer);
699 * Guts of sys_timer_settime for CPU timers.
700 * This is called with the timer locked and interrupts disabled.
701 * If we return TIMER_RETRY, it's necessary to release the timer's lock
702 * and try again. (This happens when the timer is in the middle of firing.)
704 int posix_cpu_timer_set(struct k_itimer *timer, int flags,
705 struct itimerspec *new, struct itimerspec *old)
707 struct task_struct *p = timer->it.cpu.task;
708 union cpu_time_count old_expires, new_expires, val;
711 if (unlikely(p == NULL)) {
713 * Timer refers to a dead task's clock.
718 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
720 read_lock(&tasklist_lock);
722 * We need the tasklist_lock to protect against reaping that
723 * clears p->signal. If p has just been reaped, we can no
724 * longer get any information about it at all.
726 if (unlikely(p->signal == NULL)) {
727 read_unlock(&tasklist_lock);
729 timer->it.cpu.task = NULL;
734 * Disarm any old timer after extracting its expiry time.
736 BUG_ON(!irqs_disabled());
737 spin_lock(&p->sighand->siglock);
738 old_expires = timer->it.cpu.expires;
739 list_del_init(&timer->it.cpu.entry);
740 spin_unlock(&p->sighand->siglock);
743 * We need to sample the current value to convert the new
744 * value from to relative and absolute, and to convert the
745 * old value from absolute to relative. To set a process
746 * timer, we need a sample to balance the thread expiry
747 * times (in arm_timer). With an absolute time, we must
748 * check if it's already passed. In short, we need a sample.
750 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
751 cpu_clock_sample(timer->it_clock, p, &val);
753 cpu_clock_sample_group(timer->it_clock, p, &val);
757 if (old_expires.sched == 0) {
758 old->it_value.tv_sec = 0;
759 old->it_value.tv_nsec = 0;
762 * Update the timer in case it has
763 * overrun already. If it has,
764 * we'll report it as having overrun
765 * and with the next reloaded timer
766 * already ticking, though we are
767 * swallowing that pending
768 * notification here to install the
771 bump_cpu_timer(timer, val);
772 if (cpu_time_before(timer->it_clock, val,
773 timer->it.cpu.expires)) {
774 old_expires = cpu_time_sub(
776 timer->it.cpu.expires, val);
777 sample_to_timespec(timer->it_clock,
781 old->it_value.tv_nsec = 1;
782 old->it_value.tv_sec = 0;
787 if (unlikely(timer->it.cpu.firing)) {
789 * We are colliding with the timer actually firing.
790 * Punt after filling in the timer's old value, and
791 * disable this firing since we are already reporting
792 * it as an overrun (thanks to bump_cpu_timer above).
794 read_unlock(&tasklist_lock);
795 timer->it.cpu.firing = -1;
800 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
801 cpu_time_add(timer->it_clock, &new_expires, val);
805 * Install the new expiry time (or zero).
806 * For a timer with no notification action, we don't actually
807 * arm the timer (we'll just fake it for timer_gettime).
809 timer->it.cpu.expires = new_expires;
810 if (new_expires.sched != 0 &&
811 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
812 cpu_time_before(timer->it_clock, val, new_expires)) {
813 arm_timer(timer, val);
816 read_unlock(&tasklist_lock);
819 * Install the new reload setting, and
820 * set up the signal and overrun bookkeeping.
822 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
826 * This acts as a modification timestamp for the timer,
827 * so any automatic reload attempt will punt on seeing
828 * that we have reset the timer manually.
830 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
832 timer->it_overrun_last = 0;
833 timer->it_overrun = -1;
835 if (new_expires.sched != 0 &&
836 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
837 !cpu_time_before(timer->it_clock, val, new_expires)) {
839 * The designated time already passed, so we notify
840 * immediately, even if the thread never runs to
841 * accumulate more time on this clock.
843 cpu_timer_fire(timer);
849 sample_to_timespec(timer->it_clock,
850 timer->it.cpu.incr, &old->it_interval);
855 void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
857 union cpu_time_count now;
858 struct task_struct *p = timer->it.cpu.task;
862 * Easy part: convert the reload time.
864 sample_to_timespec(timer->it_clock,
865 timer->it.cpu.incr, &itp->it_interval);
867 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
868 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
872 if (unlikely(p == NULL)) {
874 * This task already died and the timer will never fire.
875 * In this case, expires is actually the dead value.
878 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
884 * Sample the clock to take the difference with the expiry time.
886 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
887 cpu_clock_sample(timer->it_clock, p, &now);
888 clear_dead = p->exit_state;
890 read_lock(&tasklist_lock);
891 if (unlikely(p->signal == NULL)) {
893 * The process has been reaped.
894 * We can't even collect a sample any more.
895 * Call the timer disarmed, nothing else to do.
898 timer->it.cpu.task = NULL;
899 timer->it.cpu.expires.sched = 0;
900 read_unlock(&tasklist_lock);
903 cpu_clock_sample_group(timer->it_clock, p, &now);
904 clear_dead = (unlikely(p->exit_state) &&
905 thread_group_empty(p));
907 read_unlock(&tasklist_lock);
910 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
911 if (timer->it.cpu.incr.sched == 0 &&
912 cpu_time_before(timer->it_clock,
913 timer->it.cpu.expires, now)) {
915 * Do-nothing timer expired and has no reload,
916 * so it's as if it was never set.
918 timer->it.cpu.expires.sched = 0;
919 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
923 * Account for any expirations and reloads that should
926 bump_cpu_timer(timer, now);
929 if (unlikely(clear_dead)) {
931 * We've noticed that the thread is dead, but
932 * not yet reaped. Take this opportunity to
935 clear_dead_task(timer, now);
939 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
940 sample_to_timespec(timer->it_clock,
941 cpu_time_sub(timer->it_clock,
942 timer->it.cpu.expires, now),
946 * The timer should have expired already, but the firing
947 * hasn't taken place yet. Say it's just about to expire.
949 itp->it_value.tv_nsec = 1;
950 itp->it_value.tv_sec = 0;
955 * Check for any per-thread CPU timers that have fired and move them off
956 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
957 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
959 static void check_thread_timers(struct task_struct *tsk,
960 struct list_head *firing)
962 struct list_head *timers = tsk->cpu_timers;
964 tsk->it_prof_expires = cputime_zero;
965 while (!list_empty(timers)) {
966 struct cpu_timer_list *t = list_entry(timers->next,
967 struct cpu_timer_list,
969 if (cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
970 tsk->it_prof_expires = t->expires.cpu;
974 list_move_tail(&t->entry, firing);
978 tsk->it_virt_expires = cputime_zero;
979 while (!list_empty(timers)) {
980 struct cpu_timer_list *t = list_entry(timers->next,
981 struct cpu_timer_list,
983 if (cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
984 tsk->it_virt_expires = t->expires.cpu;
988 list_move_tail(&t->entry, firing);
992 tsk->it_sched_expires = 0;
993 while (!list_empty(timers)) {
994 struct cpu_timer_list *t = list_entry(timers->next,
995 struct cpu_timer_list,
997 if (tsk->sched_time < t->expires.sched) {
998 tsk->it_sched_expires = t->expires.sched;
1002 list_move_tail(&t->entry, firing);
1007 * Check for any per-thread CPU timers that have fired and move them
1008 * off the tsk->*_timers list onto the firing list. Per-thread timers
1009 * have already been taken off.
1011 static void check_process_timers(struct task_struct *tsk,
1012 struct list_head *firing)
1014 struct signal_struct *const sig = tsk->signal;
1015 cputime_t utime, stime, ptime, virt_expires, prof_expires;
1016 unsigned long long sched_time, sched_expires;
1017 struct task_struct *t;
1018 struct list_head *timers = sig->cpu_timers;
1021 * Don't sample the current process CPU clocks if there are no timers.
1023 if (list_empty(&timers[CPUCLOCK_PROF]) &&
1024 cputime_eq(sig->it_prof_expires, cputime_zero) &&
1025 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
1026 list_empty(&timers[CPUCLOCK_VIRT]) &&
1027 cputime_eq(sig->it_virt_expires, cputime_zero) &&
1028 list_empty(&timers[CPUCLOCK_SCHED]))
1032 * Collect the current process totals.
1036 sched_time = sig->sched_time;
1039 utime = cputime_add(utime, t->utime);
1040 stime = cputime_add(stime, t->stime);
1041 sched_time += t->sched_time;
1044 ptime = cputime_add(utime, stime);
1046 prof_expires = cputime_zero;
1047 while (!list_empty(timers)) {
1048 struct cpu_timer_list *t = list_entry(timers->next,
1049 struct cpu_timer_list,
1051 if (cputime_lt(ptime, t->expires.cpu)) {
1052 prof_expires = t->expires.cpu;
1056 list_move_tail(&t->entry, firing);
1060 virt_expires = cputime_zero;
1061 while (!list_empty(timers)) {
1062 struct cpu_timer_list *t = list_entry(timers->next,
1063 struct cpu_timer_list,
1065 if (cputime_lt(utime, t->expires.cpu)) {
1066 virt_expires = t->expires.cpu;
1070 list_move_tail(&t->entry, firing);
1075 while (!list_empty(timers)) {
1076 struct cpu_timer_list *t = list_entry(timers->next,
1077 struct cpu_timer_list,
1079 if (sched_time < t->expires.sched) {
1080 sched_expires = t->expires.sched;
1084 list_move_tail(&t->entry, firing);
1088 * Check for the special case process timers.
1090 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1091 if (cputime_ge(ptime, sig->it_prof_expires)) {
1092 /* ITIMER_PROF fires and reloads. */
1093 sig->it_prof_expires = sig->it_prof_incr;
1094 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1095 sig->it_prof_expires = cputime_add(
1096 sig->it_prof_expires, ptime);
1098 __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
1100 if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
1101 (cputime_eq(prof_expires, cputime_zero) ||
1102 cputime_lt(sig->it_prof_expires, prof_expires))) {
1103 prof_expires = sig->it_prof_expires;
1106 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1107 if (cputime_ge(utime, sig->it_virt_expires)) {
1108 /* ITIMER_VIRTUAL fires and reloads. */
1109 sig->it_virt_expires = sig->it_virt_incr;
1110 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1111 sig->it_virt_expires = cputime_add(
1112 sig->it_virt_expires, utime);
1114 __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
1116 if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
1117 (cputime_eq(virt_expires, cputime_zero) ||
1118 cputime_lt(sig->it_virt_expires, virt_expires))) {
1119 virt_expires = sig->it_virt_expires;
1122 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1123 unsigned long psecs = cputime_to_secs(ptime);
1125 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
1127 * At the hard limit, we just die.
1128 * No need to calculate anything else now.
1130 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1133 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
1135 * At the soft limit, send a SIGXCPU every second.
1137 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1138 if (sig->rlim[RLIMIT_CPU].rlim_cur
1139 < sig->rlim[RLIMIT_CPU].rlim_max) {
1140 sig->rlim[RLIMIT_CPU].rlim_cur++;
1143 x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
1144 if (cputime_eq(prof_expires, cputime_zero) ||
1145 cputime_lt(x, prof_expires)) {
1150 if (!cputime_eq(prof_expires, cputime_zero) ||
1151 !cputime_eq(virt_expires, cputime_zero) ||
1152 sched_expires != 0) {
1154 * Rebalance the threads' expiry times for the remaining
1155 * process CPU timers.
1158 cputime_t prof_left, virt_left, ticks;
1159 unsigned long long sched_left, sched;
1160 const unsigned int nthreads = atomic_read(&sig->live);
1162 prof_left = cputime_sub(prof_expires, utime);
1163 prof_left = cputime_sub(prof_left, stime);
1164 prof_left = cputime_div(prof_left, nthreads);
1165 virt_left = cputime_sub(virt_expires, utime);
1166 virt_left = cputime_div(virt_left, nthreads);
1167 if (sched_expires) {
1168 sched_left = sched_expires - sched_time;
1169 do_div(sched_left, nthreads);
1175 ticks = cputime_add(cputime_add(t->utime, t->stime),
1177 if (!cputime_eq(prof_expires, cputime_zero) &&
1178 (cputime_eq(t->it_prof_expires, cputime_zero) ||
1179 cputime_gt(t->it_prof_expires, ticks))) {
1180 t->it_prof_expires = ticks;
1183 ticks = cputime_add(t->utime, virt_left);
1184 if (!cputime_eq(virt_expires, cputime_zero) &&
1185 (cputime_eq(t->it_virt_expires, cputime_zero) ||
1186 cputime_gt(t->it_virt_expires, ticks))) {
1187 t->it_virt_expires = ticks;
1190 sched = t->sched_time + sched_left;
1191 if (sched_expires && (t->it_sched_expires == 0 ||
1192 t->it_sched_expires > sched)) {
1193 t->it_sched_expires = sched;
1198 } while (unlikely(t->exit_state));
1204 * This is called from the signal code (via do_schedule_next_timer)
1205 * when the last timer signal was delivered and we have to reload the timer.
1207 void posix_cpu_timer_schedule(struct k_itimer *timer)
1209 struct task_struct *p = timer->it.cpu.task;
1210 union cpu_time_count now;
1212 if (unlikely(p == NULL))
1214 * The task was cleaned up already, no future firings.
1219 * Fetch the current sample and update the timer's expiry time.
1221 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1222 cpu_clock_sample(timer->it_clock, p, &now);
1223 bump_cpu_timer(timer, now);
1224 if (unlikely(p->exit_state)) {
1225 clear_dead_task(timer, now);
1228 read_lock(&tasklist_lock); /* arm_timer needs it. */
1230 read_lock(&tasklist_lock);
1231 if (unlikely(p->signal == NULL)) {
1233 * The process has been reaped.
1234 * We can't even collect a sample any more.
1237 timer->it.cpu.task = p = NULL;
1238 timer->it.cpu.expires.sched = 0;
1239 read_unlock(&tasklist_lock);
1241 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1243 * We've noticed that the thread is dead, but
1244 * not yet reaped. Take this opportunity to
1245 * drop our task ref.
1247 clear_dead_task(timer, now);
1248 read_unlock(&tasklist_lock);
1251 cpu_clock_sample_group(timer->it_clock, p, &now);
1252 bump_cpu_timer(timer, now);
1253 /* Leave the tasklist_lock locked for the call below. */
1257 * Now re-arm for the new expiry time.
1259 arm_timer(timer, now);
1261 read_unlock(&tasklist_lock);
1265 * This is called from the timer interrupt handler. The irq handler has
1266 * already updated our counts. We need to check if any timers fire now.
1267 * Interrupts are disabled.
1269 void run_posix_cpu_timers(struct task_struct *tsk)
1272 struct k_itimer *timer, *next;
1274 BUG_ON(!irqs_disabled());
1276 #define UNEXPIRED(clock) \
1277 (cputime_eq(tsk->it_##clock##_expires, cputime_zero) || \
1278 cputime_lt(clock##_ticks(tsk), tsk->it_##clock##_expires))
1280 if (UNEXPIRED(prof) && UNEXPIRED(virt) &&
1281 (tsk->it_sched_expires == 0 ||
1282 tsk->sched_time < tsk->it_sched_expires))
1287 BUG_ON(tsk->exit_state);
1290 * Double-check with locks held.
1292 read_lock(&tasklist_lock);
1293 spin_lock(&tsk->sighand->siglock);
1296 * Here we take off tsk->cpu_timers[N] and tsk->signal->cpu_timers[N]
1297 * all the timers that are firing, and put them on the firing list.
1299 check_thread_timers(tsk, &firing);
1300 check_process_timers(tsk, &firing);
1303 * We must release these locks before taking any timer's lock.
1304 * There is a potential race with timer deletion here, as the
1305 * siglock now protects our private firing list. We have set
1306 * the firing flag in each timer, so that a deletion attempt
1307 * that gets the timer lock before we do will give it up and
1308 * spin until we've taken care of that timer below.
1310 spin_unlock(&tsk->sighand->siglock);
1311 read_unlock(&tasklist_lock);
1314 * Now that all the timers on our list have the firing flag,
1315 * noone will touch their list entries but us. We'll take
1316 * each timer's lock before clearing its firing flag, so no
1317 * timer call will interfere.
1319 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1321 spin_lock(&timer->it_lock);
1322 list_del_init(&timer->it.cpu.entry);
1323 firing = timer->it.cpu.firing;
1324 timer->it.cpu.firing = 0;
1326 * The firing flag is -1 if we collided with a reset
1327 * of the timer, which already reported this
1328 * almost-firing as an overrun. So don't generate an event.
1330 if (likely(firing >= 0)) {
1331 cpu_timer_fire(timer);
1333 spin_unlock(&timer->it_lock);
1338 * Set one of the process-wide special case CPU timers.
1339 * The tasklist_lock and tsk->sighand->siglock must be held by the caller.
1340 * The oldval argument is null for the RLIMIT_CPU timer, where *newval is
1341 * absolute; non-null for ITIMER_*, where *newval is relative and we update
1342 * it to be absolute, *oldval is absolute and we update it to be relative.
1344 void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1345 cputime_t *newval, cputime_t *oldval)
1347 union cpu_time_count now;
1348 struct list_head *head;
1350 BUG_ON(clock_idx == CPUCLOCK_SCHED);
1351 cpu_clock_sample_group_locked(clock_idx, tsk, &now);
1354 if (!cputime_eq(*oldval, cputime_zero)) {
1355 if (cputime_le(*oldval, now.cpu)) {
1356 /* Just about to fire. */
1357 *oldval = jiffies_to_cputime(1);
1359 *oldval = cputime_sub(*oldval, now.cpu);
1363 if (cputime_eq(*newval, cputime_zero))
1365 *newval = cputime_add(*newval, now.cpu);
1368 * If the RLIMIT_CPU timer will expire before the
1369 * ITIMER_PROF timer, we have nothing else to do.
1371 if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1372 < cputime_to_secs(*newval))
1377 * Check whether there are any process timers already set to fire
1378 * before this one. If so, we don't have anything more to do.
1380 head = &tsk->signal->cpu_timers[clock_idx];
1381 if (list_empty(head) ||
1382 cputime_ge(list_entry(head->next,
1383 struct cpu_timer_list, entry)->expires.cpu,
1386 * Rejigger each thread's expiry time so that one will
1387 * notice before we hit the process-cumulative expiry time.
1389 union cpu_time_count expires = { .sched = 0 };
1390 expires.cpu = *newval;
1391 process_timer_rebalance(tsk, clock_idx, expires, now);
1395 static long posix_cpu_clock_nanosleep_restart(struct restart_block *);
1397 int posix_cpu_nsleep(clockid_t which_clock, int flags,
1398 struct timespec *rqtp)
1400 struct restart_block *restart_block =
1401 ¤t_thread_info()->restart_block;
1402 struct k_itimer timer;
1406 * Diagnose required errors first.
1408 if (CPUCLOCK_PERTHREAD(which_clock) &&
1409 (CPUCLOCK_PID(which_clock) == 0 ||
1410 CPUCLOCK_PID(which_clock) == current->pid))
1414 * Set up a temporary timer and then wait for it to go off.
1416 memset(&timer, 0, sizeof timer);
1417 spin_lock_init(&timer.it_lock);
1418 timer.it_clock = which_clock;
1419 timer.it_overrun = -1;
1420 error = posix_cpu_timer_create(&timer);
1421 timer.it_process = current;
1423 struct timespec __user *rmtp;
1424 static struct itimerspec zero_it;
1425 struct itimerspec it = { .it_value = *rqtp,
1426 .it_interval = {} };
1428 spin_lock_irq(&timer.it_lock);
1429 error = posix_cpu_timer_set(&timer, flags, &it, NULL);
1431 spin_unlock_irq(&timer.it_lock);
1435 while (!signal_pending(current)) {
1436 if (timer.it.cpu.expires.sched == 0) {
1438 * Our timer fired and was reset.
1440 spin_unlock_irq(&timer.it_lock);
1445 * Block until cpu_timer_fire (or a signal) wakes us.
1447 __set_current_state(TASK_INTERRUPTIBLE);
1448 spin_unlock_irq(&timer.it_lock);
1450 spin_lock_irq(&timer.it_lock);
1454 * We were interrupted by a signal.
1456 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
1457 posix_cpu_timer_set(&timer, 0, &zero_it, &it);
1458 spin_unlock_irq(&timer.it_lock);
1460 if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
1462 * It actually did fire already.
1468 * Report back to the user the time still remaining.
1470 rmtp = (struct timespec __user *) restart_block->arg1;
1471 if (rmtp != NULL && !(flags & TIMER_ABSTIME) &&
1472 copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1475 restart_block->fn = posix_cpu_clock_nanosleep_restart;
1476 /* Caller already set restart_block->arg1 */
1477 restart_block->arg0 = which_clock;
1478 restart_block->arg2 = rqtp->tv_sec;
1479 restart_block->arg3 = rqtp->tv_nsec;
1481 error = -ERESTART_RESTARTBLOCK;
1488 posix_cpu_clock_nanosleep_restart(struct restart_block *restart_block)
1490 clockid_t which_clock = restart_block->arg0;
1491 struct timespec t = { .tv_sec = restart_block->arg2,
1492 .tv_nsec = restart_block->arg3 };
1493 restart_block->fn = do_no_restart_syscall;
1494 return posix_cpu_nsleep(which_clock, TIMER_ABSTIME, &t);
1498 #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1499 #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1501 static int process_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
1503 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1505 static int process_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
1507 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1509 static int process_cpu_timer_create(struct k_itimer *timer)
1511 timer->it_clock = PROCESS_CLOCK;
1512 return posix_cpu_timer_create(timer);
1514 static int process_cpu_nsleep(clockid_t which_clock, int flags,
1515 struct timespec *rqtp)
1517 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
1519 static int thread_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
1521 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1523 static int thread_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
1525 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1527 static int thread_cpu_timer_create(struct k_itimer *timer)
1529 timer->it_clock = THREAD_CLOCK;
1530 return posix_cpu_timer_create(timer);
1532 static int thread_cpu_nsleep(clockid_t which_clock, int flags,
1533 struct timespec *rqtp)
1538 static __init int init_posix_cpu_timers(void)
1540 struct k_clock process = {
1541 .clock_getres = process_cpu_clock_getres,
1542 .clock_get = process_cpu_clock_get,
1543 .clock_set = do_posix_clock_nosettime,
1544 .timer_create = process_cpu_timer_create,
1545 .nsleep = process_cpu_nsleep,
1547 struct k_clock thread = {
1548 .clock_getres = thread_cpu_clock_getres,
1549 .clock_get = thread_cpu_clock_get,
1550 .clock_set = do_posix_clock_nosettime,
1551 .timer_create = thread_cpu_timer_create,
1552 .nsleep = thread_cpu_nsleep,
1555 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1556 register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1560 __initcall(init_posix_cpu_timers);