2 * Implement CPU time clocks for the POSIX clock interface.
5 #include <linux/sched.h>
6 #include <linux/posix-timers.h>
7 #include <asm/uaccess.h>
8 #include <linux/errno.h>
10 static int check_clock(clockid_t which_clock)
13 struct task_struct *p;
14 const pid_t pid = CPUCLOCK_PID(which_clock);
16 if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
22 read_lock(&tasklist_lock);
23 p = find_task_by_pid(pid);
24 if (!p || (CPUCLOCK_PERTHREAD(which_clock) ?
25 p->tgid != current->tgid : p->tgid != pid)) {
28 read_unlock(&tasklist_lock);
33 static inline union cpu_time_count
34 timespec_to_sample(clockid_t which_clock, const struct timespec *tp)
36 union cpu_time_count ret;
37 ret.sched = 0; /* high half always zero when .cpu used */
38 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
39 ret.sched = tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
41 ret.cpu = timespec_to_cputime(tp);
46 static void sample_to_timespec(clockid_t which_clock,
47 union cpu_time_count cpu,
50 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
51 tp->tv_sec = div_long_long_rem(cpu.sched,
52 NSEC_PER_SEC, &tp->tv_nsec);
54 cputime_to_timespec(cpu.cpu, tp);
58 static inline int cpu_time_before(clockid_t which_clock,
59 union cpu_time_count now,
60 union cpu_time_count then)
62 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
63 return now.sched < then.sched;
65 return cputime_lt(now.cpu, then.cpu);
68 static inline void cpu_time_add(clockid_t which_clock,
69 union cpu_time_count *acc,
70 union cpu_time_count val)
72 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
73 acc->sched += val.sched;
75 acc->cpu = cputime_add(acc->cpu, val.cpu);
78 static inline union cpu_time_count cpu_time_sub(clockid_t which_clock,
79 union cpu_time_count a,
80 union cpu_time_count b)
82 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
85 a.cpu = cputime_sub(a.cpu, b.cpu);
91 * Update expiry time from increment, and increase overrun count,
92 * given the current clock sample.
94 static inline void bump_cpu_timer(struct k_itimer *timer,
95 union cpu_time_count now)
99 if (timer->it.cpu.incr.sched == 0)
102 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
103 unsigned long long delta, incr;
105 if (now.sched < timer->it.cpu.expires.sched)
107 incr = timer->it.cpu.incr.sched;
108 delta = now.sched + incr - timer->it.cpu.expires.sched;
109 /* Don't use (incr*2 < delta), incr*2 might overflow. */
110 for (i = 0; incr < delta - incr; i++)
112 for (; i >= 0; incr >>= 1, i--) {
115 timer->it.cpu.expires.sched += incr;
116 timer->it_overrun += 1 << i;
120 cputime_t delta, incr;
122 if (cputime_lt(now.cpu, timer->it.cpu.expires.cpu))
124 incr = timer->it.cpu.incr.cpu;
125 delta = cputime_sub(cputime_add(now.cpu, incr),
126 timer->it.cpu.expires.cpu);
127 /* Don't use (incr*2 < delta), incr*2 might overflow. */
128 for (i = 0; cputime_lt(incr, cputime_sub(delta, incr)); i++)
129 incr = cputime_add(incr, incr);
130 for (; i >= 0; incr = cputime_halve(incr), i--) {
131 if (cputime_le(delta, incr))
133 timer->it.cpu.expires.cpu =
134 cputime_add(timer->it.cpu.expires.cpu, incr);
135 timer->it_overrun += 1 << i;
136 delta = cputime_sub(delta, incr);
141 static inline cputime_t prof_ticks(struct task_struct *p)
143 return cputime_add(p->utime, p->stime);
145 static inline cputime_t virt_ticks(struct task_struct *p)
149 static inline unsigned long long sched_ns(struct task_struct *p)
151 return (p == current) ? current_sched_time(p) : p->sched_time;
154 int posix_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
156 int error = check_clock(which_clock);
159 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
160 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
162 * If sched_clock is using a cycle counter, we
163 * don't have any idea of its true resolution
164 * exported, but it is much more than 1s/HZ.
172 int posix_cpu_clock_set(clockid_t which_clock, const struct timespec *tp)
175 * You can never reset a CPU clock, but we check for other errors
176 * in the call before failing with EPERM.
178 int error = check_clock(which_clock);
187 * Sample a per-thread clock for the given task.
189 static int cpu_clock_sample(clockid_t which_clock, struct task_struct *p,
190 union cpu_time_count *cpu)
192 switch (CPUCLOCK_WHICH(which_clock)) {
196 cpu->cpu = prof_ticks(p);
199 cpu->cpu = virt_ticks(p);
202 cpu->sched = sched_ns(p);
209 * Sample a process (thread group) clock for the given group_leader task.
210 * Must be called with tasklist_lock held for reading.
211 * Must be called with tasklist_lock held for reading, and p->sighand->siglock.
213 static int cpu_clock_sample_group_locked(unsigned int clock_idx,
214 struct task_struct *p,
215 union cpu_time_count *cpu)
217 struct task_struct *t = p;
222 cpu->cpu = cputime_add(p->signal->utime, p->signal->stime);
224 cpu->cpu = cputime_add(cpu->cpu, prof_ticks(t));
229 cpu->cpu = p->signal->utime;
231 cpu->cpu = cputime_add(cpu->cpu, virt_ticks(t));
236 cpu->sched = p->signal->sched_time;
237 /* Add in each other live thread. */
238 while ((t = next_thread(t)) != p) {
239 cpu->sched += t->sched_time;
241 if (p->tgid == current->tgid) {
243 * We're sampling ourselves, so include the
244 * cycles not yet banked. We still omit
245 * other threads running on other CPUs,
246 * so the total can always be behind as
247 * much as max(nthreads-1,ncpus) * (NSEC_PER_SEC/HZ).
249 cpu->sched += current_sched_time(current);
251 cpu->sched += p->sched_time;
259 * Sample a process (thread group) clock for the given group_leader task.
260 * Must be called with tasklist_lock held for reading.
262 static int cpu_clock_sample_group(clockid_t which_clock,
263 struct task_struct *p,
264 union cpu_time_count *cpu)
268 spin_lock_irqsave(&p->sighand->siglock, flags);
269 ret = cpu_clock_sample_group_locked(CPUCLOCK_WHICH(which_clock), p,
271 spin_unlock_irqrestore(&p->sighand->siglock, flags);
276 int posix_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
278 const pid_t pid = CPUCLOCK_PID(which_clock);
280 union cpu_time_count rtn;
284 * Special case constant value for our own clocks.
285 * We don't have to do any lookup to find ourselves.
287 if (CPUCLOCK_PERTHREAD(which_clock)) {
289 * Sampling just ourselves we can do with no locking.
291 error = cpu_clock_sample(which_clock,
294 read_lock(&tasklist_lock);
295 error = cpu_clock_sample_group(which_clock,
297 read_unlock(&tasklist_lock);
301 * Find the given PID, and validate that the caller
302 * should be able to see it.
304 struct task_struct *p;
305 read_lock(&tasklist_lock);
306 p = find_task_by_pid(pid);
308 if (CPUCLOCK_PERTHREAD(which_clock)) {
309 if (p->tgid == current->tgid) {
310 error = cpu_clock_sample(which_clock,
313 } else if (p->tgid == pid && p->signal) {
314 error = cpu_clock_sample_group(which_clock,
318 read_unlock(&tasklist_lock);
323 sample_to_timespec(which_clock, rtn, tp);
329 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
330 * This is called from sys_timer_create with the new timer already locked.
332 int posix_cpu_timer_create(struct k_itimer *new_timer)
335 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
336 struct task_struct *p;
338 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
341 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
342 new_timer->it.cpu.incr.sched = 0;
343 new_timer->it.cpu.expires.sched = 0;
345 read_lock(&tasklist_lock);
346 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
350 p = find_task_by_pid(pid);
351 if (p && p->tgid != current->tgid)
356 p = current->group_leader;
358 p = find_task_by_pid(pid);
359 if (p && p->tgid != pid)
363 new_timer->it.cpu.task = p;
369 read_unlock(&tasklist_lock);
375 * Clean up a CPU-clock timer that is about to be destroyed.
376 * This is called from timer deletion with the timer already locked.
377 * If we return TIMER_RETRY, it's necessary to release the timer's lock
378 * and try again. (This happens when the timer is in the middle of firing.)
380 int posix_cpu_timer_del(struct k_itimer *timer)
382 struct task_struct *p = timer->it.cpu.task;
384 if (timer->it.cpu.firing)
387 if (unlikely(p == NULL))
390 spin_lock(&p->sighand->siglock);
391 if (!list_empty(&timer->it.cpu.entry)) {
393 * Take us off the task's timer list. We don't need to
394 * take tasklist_lock and check for the task being reaped.
395 * If it was reaped, it already called posix_cpu_timers_exit
396 * and posix_cpu_timers_exit_group to clear all the timers
397 * that pointed to it.
399 list_del(&timer->it.cpu.entry);
402 spin_unlock(&p->sighand->siglock);
408 * Clean out CPU timers still ticking when a thread exited. The task
409 * pointer is cleared, and the expiry time is replaced with the residual
410 * time for later timer_gettime calls to return.
411 * This must be called with the siglock held.
413 static void cleanup_timers(struct list_head *head,
414 cputime_t utime, cputime_t stime,
415 unsigned long long sched_time)
417 struct cpu_timer_list *timer, *next;
418 cputime_t ptime = cputime_add(utime, stime);
420 list_for_each_entry_safe(timer, next, head, entry) {
421 put_task_struct(timer->task);
423 list_del_init(&timer->entry);
424 if (cputime_lt(timer->expires.cpu, ptime)) {
425 timer->expires.cpu = cputime_zero;
427 timer->expires.cpu = cputime_sub(timer->expires.cpu,
433 list_for_each_entry_safe(timer, next, head, entry) {
434 put_task_struct(timer->task);
436 list_del_init(&timer->entry);
437 if (cputime_lt(timer->expires.cpu, utime)) {
438 timer->expires.cpu = cputime_zero;
440 timer->expires.cpu = cputime_sub(timer->expires.cpu,
446 list_for_each_entry_safe(timer, next, head, entry) {
447 put_task_struct(timer->task);
449 list_del_init(&timer->entry);
450 if (timer->expires.sched < sched_time) {
451 timer->expires.sched = 0;
453 timer->expires.sched -= sched_time;
459 * These are both called with the siglock held, when the current thread
460 * is being reaped. When the final (leader) thread in the group is reaped,
461 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
463 void posix_cpu_timers_exit(struct task_struct *tsk)
465 cleanup_timers(tsk->cpu_timers,
466 tsk->utime, tsk->stime, tsk->sched_time);
469 void posix_cpu_timers_exit_group(struct task_struct *tsk)
471 cleanup_timers(tsk->signal->cpu_timers,
472 cputime_add(tsk->utime, tsk->signal->utime),
473 cputime_add(tsk->stime, tsk->signal->stime),
474 tsk->sched_time + tsk->signal->sched_time);
479 * Set the expiry times of all the threads in the process so one of them
480 * will go off before the process cumulative expiry total is reached.
482 static void process_timer_rebalance(struct task_struct *p,
483 unsigned int clock_idx,
484 union cpu_time_count expires,
485 union cpu_time_count val)
487 cputime_t ticks, left;
488 unsigned long long ns, nsleft;
489 struct task_struct *t = p;
490 unsigned int nthreads = atomic_read(&p->signal->live);
497 left = cputime_div(cputime_sub(expires.cpu, val.cpu),
500 if (!unlikely(t->exit_state)) {
501 ticks = cputime_add(prof_ticks(t), left);
502 if (cputime_eq(t->it_prof_expires,
504 cputime_gt(t->it_prof_expires, ticks)) {
505 t->it_prof_expires = ticks;
512 left = cputime_div(cputime_sub(expires.cpu, val.cpu),
515 if (!unlikely(t->exit_state)) {
516 ticks = cputime_add(virt_ticks(t), left);
517 if (cputime_eq(t->it_virt_expires,
519 cputime_gt(t->it_virt_expires, ticks)) {
520 t->it_virt_expires = ticks;
527 nsleft = expires.sched - val.sched;
528 do_div(nsleft, nthreads);
530 if (!unlikely(t->exit_state)) {
531 ns = t->sched_time + nsleft;
532 if (t->it_sched_expires == 0 ||
533 t->it_sched_expires > ns) {
534 t->it_sched_expires = ns;
543 static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
546 * That's all for this thread or process.
547 * We leave our residual in expires to be reported.
549 put_task_struct(timer->it.cpu.task);
550 timer->it.cpu.task = NULL;
551 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
552 timer->it.cpu.expires,
557 * Insert the timer on the appropriate list before any timers that
558 * expire later. This must be called with the tasklist_lock held
559 * for reading, and interrupts disabled.
561 static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
563 struct task_struct *p = timer->it.cpu.task;
564 struct list_head *head, *listpos;
565 struct cpu_timer_list *const nt = &timer->it.cpu;
566 struct cpu_timer_list *next;
569 head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
570 p->cpu_timers : p->signal->cpu_timers);
571 head += CPUCLOCK_WHICH(timer->it_clock);
573 BUG_ON(!irqs_disabled());
574 spin_lock(&p->sighand->siglock);
577 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
578 list_for_each_entry(next, head, entry) {
579 if (next->expires.sched > nt->expires.sched) {
580 listpos = &next->entry;
585 list_for_each_entry(next, head, entry) {
586 if (cputime_gt(next->expires.cpu, nt->expires.cpu)) {
587 listpos = &next->entry;
592 list_add(&nt->entry, listpos);
594 if (listpos == head) {
596 * We are the new earliest-expiring timer.
597 * If we are a thread timer, there can always
598 * be a process timer telling us to stop earlier.
601 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
602 switch (CPUCLOCK_WHICH(timer->it_clock)) {
606 if (cputime_eq(p->it_prof_expires,
608 cputime_gt(p->it_prof_expires,
610 p->it_prof_expires = nt->expires.cpu;
613 if (cputime_eq(p->it_virt_expires,
615 cputime_gt(p->it_virt_expires,
617 p->it_virt_expires = nt->expires.cpu;
620 if (p->it_sched_expires == 0 ||
621 p->it_sched_expires > nt->expires.sched)
622 p->it_sched_expires = nt->expires.sched;
627 * For a process timer, we must balance
628 * all the live threads' expirations.
630 switch (CPUCLOCK_WHICH(timer->it_clock)) {
634 if (!cputime_eq(p->signal->it_virt_expires,
636 cputime_lt(p->signal->it_virt_expires,
637 timer->it.cpu.expires.cpu))
641 if (!cputime_eq(p->signal->it_prof_expires,
643 cputime_lt(p->signal->it_prof_expires,
644 timer->it.cpu.expires.cpu))
646 i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
647 if (i != RLIM_INFINITY &&
648 i <= cputime_to_secs(timer->it.cpu.expires.cpu))
653 process_timer_rebalance(
655 CPUCLOCK_WHICH(timer->it_clock),
656 timer->it.cpu.expires, now);
662 spin_unlock(&p->sighand->siglock);
666 * The timer is locked, fire it and arrange for its reload.
668 static void cpu_timer_fire(struct k_itimer *timer)
670 if (unlikely(timer->sigq == NULL)) {
672 * This a special case for clock_nanosleep,
673 * not a normal timer from sys_timer_create.
675 wake_up_process(timer->it_process);
676 timer->it.cpu.expires.sched = 0;
677 } else if (timer->it.cpu.incr.sched == 0) {
679 * One-shot timer. Clear it as soon as it's fired.
681 posix_timer_event(timer, 0);
682 timer->it.cpu.expires.sched = 0;
683 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
685 * The signal did not get queued because the signal
686 * was ignored, so we won't get any callback to
687 * reload the timer. But we need to keep it
688 * ticking in case the signal is deliverable next time.
690 posix_cpu_timer_schedule(timer);
695 * Guts of sys_timer_settime for CPU timers.
696 * This is called with the timer locked and interrupts disabled.
697 * If we return TIMER_RETRY, it's necessary to release the timer's lock
698 * and try again. (This happens when the timer is in the middle of firing.)
700 int posix_cpu_timer_set(struct k_itimer *timer, int flags,
701 struct itimerspec *new, struct itimerspec *old)
703 struct task_struct *p = timer->it.cpu.task;
704 union cpu_time_count old_expires, new_expires, val;
707 if (unlikely(p == NULL)) {
709 * Timer refers to a dead task's clock.
714 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
716 read_lock(&tasklist_lock);
718 * We need the tasklist_lock to protect against reaping that
719 * clears p->signal. If p has just been reaped, we can no
720 * longer get any information about it at all.
722 if (unlikely(p->signal == NULL)) {
723 read_unlock(&tasklist_lock);
725 timer->it.cpu.task = NULL;
730 * Disarm any old timer after extracting its expiry time.
732 BUG_ON(!irqs_disabled());
733 spin_lock(&p->sighand->siglock);
734 old_expires = timer->it.cpu.expires;
735 list_del_init(&timer->it.cpu.entry);
736 spin_unlock(&p->sighand->siglock);
739 * We need to sample the current value to convert the new
740 * value from to relative and absolute, and to convert the
741 * old value from absolute to relative. To set a process
742 * timer, we need a sample to balance the thread expiry
743 * times (in arm_timer). With an absolute time, we must
744 * check if it's already passed. In short, we need a sample.
746 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
747 cpu_clock_sample(timer->it_clock, p, &val);
749 cpu_clock_sample_group(timer->it_clock, p, &val);
753 if (old_expires.sched == 0) {
754 old->it_value.tv_sec = 0;
755 old->it_value.tv_nsec = 0;
758 * Update the timer in case it has
759 * overrun already. If it has,
760 * we'll report it as having overrun
761 * and with the next reloaded timer
762 * already ticking, though we are
763 * swallowing that pending
764 * notification here to install the
767 bump_cpu_timer(timer, val);
768 if (cpu_time_before(timer->it_clock, val,
769 timer->it.cpu.expires)) {
770 old_expires = cpu_time_sub(
772 timer->it.cpu.expires, val);
773 sample_to_timespec(timer->it_clock,
777 old->it_value.tv_nsec = 1;
778 old->it_value.tv_sec = 0;
783 if (unlikely(timer->it.cpu.firing)) {
785 * We are colliding with the timer actually firing.
786 * Punt after filling in the timer's old value, and
787 * disable this firing since we are already reporting
788 * it as an overrun (thanks to bump_cpu_timer above).
790 read_unlock(&tasklist_lock);
791 timer->it.cpu.firing = -1;
796 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
797 cpu_time_add(timer->it_clock, &new_expires, val);
801 * Install the new expiry time (or zero).
802 * For a timer with no notification action, we don't actually
803 * arm the timer (we'll just fake it for timer_gettime).
805 timer->it.cpu.expires = new_expires;
806 if (new_expires.sched != 0 &&
807 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
808 cpu_time_before(timer->it_clock, val, new_expires)) {
809 arm_timer(timer, val);
812 read_unlock(&tasklist_lock);
815 * Install the new reload setting, and
816 * set up the signal and overrun bookkeeping.
818 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
822 * This acts as a modification timestamp for the timer,
823 * so any automatic reload attempt will punt on seeing
824 * that we have reset the timer manually.
826 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
828 timer->it_overrun_last = 0;
829 timer->it_overrun = -1;
831 if (new_expires.sched != 0 &&
832 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
833 !cpu_time_before(timer->it_clock, val, new_expires)) {
835 * The designated time already passed, so we notify
836 * immediately, even if the thread never runs to
837 * accumulate more time on this clock.
839 cpu_timer_fire(timer);
845 sample_to_timespec(timer->it_clock,
846 timer->it.cpu.incr, &old->it_interval);
851 void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
853 union cpu_time_count now;
854 struct task_struct *p = timer->it.cpu.task;
858 * Easy part: convert the reload time.
860 sample_to_timespec(timer->it_clock,
861 timer->it.cpu.incr, &itp->it_interval);
863 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
864 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
868 if (unlikely(p == NULL)) {
870 * This task already died and the timer will never fire.
871 * In this case, expires is actually the dead value.
874 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
880 * Sample the clock to take the difference with the expiry time.
882 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
883 cpu_clock_sample(timer->it_clock, p, &now);
884 clear_dead = p->exit_state;
886 read_lock(&tasklist_lock);
887 if (unlikely(p->signal == NULL)) {
889 * The process has been reaped.
890 * We can't even collect a sample any more.
891 * Call the timer disarmed, nothing else to do.
894 timer->it.cpu.task = NULL;
895 timer->it.cpu.expires.sched = 0;
896 read_unlock(&tasklist_lock);
899 cpu_clock_sample_group(timer->it_clock, p, &now);
900 clear_dead = (unlikely(p->exit_state) &&
901 thread_group_empty(p));
903 read_unlock(&tasklist_lock);
906 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
907 if (timer->it.cpu.incr.sched == 0 &&
908 cpu_time_before(timer->it_clock,
909 timer->it.cpu.expires, now)) {
911 * Do-nothing timer expired and has no reload,
912 * so it's as if it was never set.
914 timer->it.cpu.expires.sched = 0;
915 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
919 * Account for any expirations and reloads that should
922 bump_cpu_timer(timer, now);
925 if (unlikely(clear_dead)) {
927 * We've noticed that the thread is dead, but
928 * not yet reaped. Take this opportunity to
931 clear_dead_task(timer, now);
935 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
936 sample_to_timespec(timer->it_clock,
937 cpu_time_sub(timer->it_clock,
938 timer->it.cpu.expires, now),
942 * The timer should have expired already, but the firing
943 * hasn't taken place yet. Say it's just about to expire.
945 itp->it_value.tv_nsec = 1;
946 itp->it_value.tv_sec = 0;
951 * Check for any per-thread CPU timers that have fired and move them off
952 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
953 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
955 static void check_thread_timers(struct task_struct *tsk,
956 struct list_head *firing)
958 struct list_head *timers = tsk->cpu_timers;
960 tsk->it_prof_expires = cputime_zero;
961 while (!list_empty(timers)) {
962 struct cpu_timer_list *t = list_entry(timers->next,
963 struct cpu_timer_list,
965 if (cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
966 tsk->it_prof_expires = t->expires.cpu;
970 list_move_tail(&t->entry, firing);
974 tsk->it_virt_expires = cputime_zero;
975 while (!list_empty(timers)) {
976 struct cpu_timer_list *t = list_entry(timers->next,
977 struct cpu_timer_list,
979 if (cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
980 tsk->it_virt_expires = t->expires.cpu;
984 list_move_tail(&t->entry, firing);
988 tsk->it_sched_expires = 0;
989 while (!list_empty(timers)) {
990 struct cpu_timer_list *t = list_entry(timers->next,
991 struct cpu_timer_list,
993 if (tsk->sched_time < t->expires.sched) {
994 tsk->it_sched_expires = t->expires.sched;
998 list_move_tail(&t->entry, firing);
1003 * Check for any per-thread CPU timers that have fired and move them
1004 * off the tsk->*_timers list onto the firing list. Per-thread timers
1005 * have already been taken off.
1007 static void check_process_timers(struct task_struct *tsk,
1008 struct list_head *firing)
1010 struct signal_struct *const sig = tsk->signal;
1011 cputime_t utime, stime, ptime, virt_expires, prof_expires;
1012 unsigned long long sched_time, sched_expires;
1013 struct task_struct *t;
1014 struct list_head *timers = sig->cpu_timers;
1017 * Don't sample the current process CPU clocks if there are no timers.
1019 if (list_empty(&timers[CPUCLOCK_PROF]) &&
1020 cputime_eq(sig->it_prof_expires, cputime_zero) &&
1021 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
1022 list_empty(&timers[CPUCLOCK_VIRT]) &&
1023 cputime_eq(sig->it_virt_expires, cputime_zero) &&
1024 list_empty(&timers[CPUCLOCK_SCHED]))
1028 * Collect the current process totals.
1032 sched_time = sig->sched_time;
1035 utime = cputime_add(utime, t->utime);
1036 stime = cputime_add(stime, t->stime);
1037 sched_time += t->sched_time;
1040 ptime = cputime_add(utime, stime);
1042 prof_expires = cputime_zero;
1043 while (!list_empty(timers)) {
1044 struct cpu_timer_list *t = list_entry(timers->next,
1045 struct cpu_timer_list,
1047 if (cputime_lt(ptime, t->expires.cpu)) {
1048 prof_expires = t->expires.cpu;
1052 list_move_tail(&t->entry, firing);
1056 virt_expires = cputime_zero;
1057 while (!list_empty(timers)) {
1058 struct cpu_timer_list *t = list_entry(timers->next,
1059 struct cpu_timer_list,
1061 if (cputime_lt(utime, t->expires.cpu)) {
1062 virt_expires = t->expires.cpu;
1066 list_move_tail(&t->entry, firing);
1071 while (!list_empty(timers)) {
1072 struct cpu_timer_list *t = list_entry(timers->next,
1073 struct cpu_timer_list,
1075 if (sched_time < t->expires.sched) {
1076 sched_expires = t->expires.sched;
1080 list_move_tail(&t->entry, firing);
1084 * Check for the special case process timers.
1086 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1087 if (cputime_ge(ptime, sig->it_prof_expires)) {
1088 /* ITIMER_PROF fires and reloads. */
1089 sig->it_prof_expires = sig->it_prof_incr;
1090 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1091 sig->it_prof_expires = cputime_add(
1092 sig->it_prof_expires, ptime);
1094 __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
1096 if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
1097 (cputime_eq(prof_expires, cputime_zero) ||
1098 cputime_lt(sig->it_prof_expires, prof_expires))) {
1099 prof_expires = sig->it_prof_expires;
1102 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1103 if (cputime_ge(utime, sig->it_virt_expires)) {
1104 /* ITIMER_VIRTUAL fires and reloads. */
1105 sig->it_virt_expires = sig->it_virt_incr;
1106 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1107 sig->it_virt_expires = cputime_add(
1108 sig->it_virt_expires, utime);
1110 __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
1112 if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
1113 (cputime_eq(virt_expires, cputime_zero) ||
1114 cputime_lt(sig->it_virt_expires, virt_expires))) {
1115 virt_expires = sig->it_virt_expires;
1118 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1119 unsigned long psecs = cputime_to_secs(ptime);
1121 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
1123 * At the hard limit, we just die.
1124 * No need to calculate anything else now.
1126 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1129 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
1131 * At the soft limit, send a SIGXCPU every second.
1133 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1134 if (sig->rlim[RLIMIT_CPU].rlim_cur
1135 < sig->rlim[RLIMIT_CPU].rlim_max) {
1136 sig->rlim[RLIMIT_CPU].rlim_cur++;
1139 x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
1140 if (cputime_eq(prof_expires, cputime_zero) ||
1141 cputime_lt(x, prof_expires)) {
1146 if (!cputime_eq(prof_expires, cputime_zero) ||
1147 !cputime_eq(virt_expires, cputime_zero) ||
1148 sched_expires != 0) {
1150 * Rebalance the threads' expiry times for the remaining
1151 * process CPU timers.
1154 cputime_t prof_left, virt_left, ticks;
1155 unsigned long long sched_left, sched;
1156 const unsigned int nthreads = atomic_read(&sig->live);
1158 prof_left = cputime_sub(prof_expires, utime);
1159 prof_left = cputime_sub(prof_left, stime);
1160 prof_left = cputime_div(prof_left, nthreads);
1161 virt_left = cputime_sub(virt_expires, utime);
1162 virt_left = cputime_div(virt_left, nthreads);
1163 if (sched_expires) {
1164 sched_left = sched_expires - sched_time;
1165 do_div(sched_left, nthreads);
1171 ticks = cputime_add(cputime_add(t->utime, t->stime),
1173 if (!cputime_eq(prof_expires, cputime_zero) &&
1174 (cputime_eq(t->it_prof_expires, cputime_zero) ||
1175 cputime_gt(t->it_prof_expires, ticks))) {
1176 t->it_prof_expires = ticks;
1179 ticks = cputime_add(t->utime, virt_left);
1180 if (!cputime_eq(virt_expires, cputime_zero) &&
1181 (cputime_eq(t->it_virt_expires, cputime_zero) ||
1182 cputime_gt(t->it_virt_expires, ticks))) {
1183 t->it_virt_expires = ticks;
1186 sched = t->sched_time + sched_left;
1187 if (sched_expires && (t->it_sched_expires == 0 ||
1188 t->it_sched_expires > sched)) {
1189 t->it_sched_expires = sched;
1194 } while (unlikely(t->exit_state));
1200 * This is called from the signal code (via do_schedule_next_timer)
1201 * when the last timer signal was delivered and we have to reload the timer.
1203 void posix_cpu_timer_schedule(struct k_itimer *timer)
1205 struct task_struct *p = timer->it.cpu.task;
1206 union cpu_time_count now;
1208 if (unlikely(p == NULL))
1210 * The task was cleaned up already, no future firings.
1215 * Fetch the current sample and update the timer's expiry time.
1217 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1218 cpu_clock_sample(timer->it_clock, p, &now);
1219 bump_cpu_timer(timer, now);
1220 if (unlikely(p->exit_state)) {
1221 clear_dead_task(timer, now);
1224 read_lock(&tasklist_lock); /* arm_timer needs it. */
1226 read_lock(&tasklist_lock);
1227 if (unlikely(p->signal == NULL)) {
1229 * The process has been reaped.
1230 * We can't even collect a sample any more.
1233 timer->it.cpu.task = p = NULL;
1234 timer->it.cpu.expires.sched = 0;
1235 read_unlock(&tasklist_lock);
1237 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1239 * We've noticed that the thread is dead, but
1240 * not yet reaped. Take this opportunity to
1241 * drop our task ref.
1243 clear_dead_task(timer, now);
1244 read_unlock(&tasklist_lock);
1247 cpu_clock_sample_group(timer->it_clock, p, &now);
1248 bump_cpu_timer(timer, now);
1249 /* Leave the tasklist_lock locked for the call below. */
1253 * Now re-arm for the new expiry time.
1255 arm_timer(timer, now);
1257 read_unlock(&tasklist_lock);
1261 * This is called from the timer interrupt handler. The irq handler has
1262 * already updated our counts. We need to check if any timers fire now.
1263 * Interrupts are disabled.
1265 void run_posix_cpu_timers(struct task_struct *tsk)
1268 struct k_itimer *timer, *next;
1270 BUG_ON(!irqs_disabled());
1272 #define UNEXPIRED(clock) \
1273 (cputime_eq(tsk->it_##clock##_expires, cputime_zero) || \
1274 cputime_lt(clock##_ticks(tsk), tsk->it_##clock##_expires))
1276 if (UNEXPIRED(prof) && UNEXPIRED(virt) &&
1277 (tsk->it_sched_expires == 0 ||
1278 tsk->sched_time < tsk->it_sched_expires))
1283 BUG_ON(tsk->exit_state);
1286 * Double-check with locks held.
1288 read_lock(&tasklist_lock);
1289 spin_lock(&tsk->sighand->siglock);
1292 * Here we take off tsk->cpu_timers[N] and tsk->signal->cpu_timers[N]
1293 * all the timers that are firing, and put them on the firing list.
1295 check_thread_timers(tsk, &firing);
1296 check_process_timers(tsk, &firing);
1299 * We must release these locks before taking any timer's lock.
1300 * There is a potential race with timer deletion here, as the
1301 * siglock now protects our private firing list. We have set
1302 * the firing flag in each timer, so that a deletion attempt
1303 * that gets the timer lock before we do will give it up and
1304 * spin until we've taken care of that timer below.
1306 spin_unlock(&tsk->sighand->siglock);
1307 read_unlock(&tasklist_lock);
1310 * Now that all the timers on our list have the firing flag,
1311 * noone will touch their list entries but us. We'll take
1312 * each timer's lock before clearing its firing flag, so no
1313 * timer call will interfere.
1315 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1317 spin_lock(&timer->it_lock);
1318 list_del_init(&timer->it.cpu.entry);
1319 firing = timer->it.cpu.firing;
1320 timer->it.cpu.firing = 0;
1322 * The firing flag is -1 if we collided with a reset
1323 * of the timer, which already reported this
1324 * almost-firing as an overrun. So don't generate an event.
1326 if (likely(firing >= 0)) {
1327 cpu_timer_fire(timer);
1329 spin_unlock(&timer->it_lock);
1334 * Set one of the process-wide special case CPU timers.
1335 * The tasklist_lock and tsk->sighand->siglock must be held by the caller.
1336 * The oldval argument is null for the RLIMIT_CPU timer, where *newval is
1337 * absolute; non-null for ITIMER_*, where *newval is relative and we update
1338 * it to be absolute, *oldval is absolute and we update it to be relative.
1340 void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1341 cputime_t *newval, cputime_t *oldval)
1343 union cpu_time_count now;
1344 struct list_head *head;
1346 BUG_ON(clock_idx == CPUCLOCK_SCHED);
1347 cpu_clock_sample_group_locked(clock_idx, tsk, &now);
1350 if (!cputime_eq(*oldval, cputime_zero)) {
1351 if (cputime_le(*oldval, now.cpu)) {
1352 /* Just about to fire. */
1353 *oldval = jiffies_to_cputime(1);
1355 *oldval = cputime_sub(*oldval, now.cpu);
1359 if (cputime_eq(*newval, cputime_zero))
1361 *newval = cputime_add(*newval, now.cpu);
1364 * If the RLIMIT_CPU timer will expire before the
1365 * ITIMER_PROF timer, we have nothing else to do.
1367 if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1368 < cputime_to_secs(*newval))
1373 * Check whether there are any process timers already set to fire
1374 * before this one. If so, we don't have anything more to do.
1376 head = &tsk->signal->cpu_timers[clock_idx];
1377 if (list_empty(head) ||
1378 cputime_ge(list_entry(head->next,
1379 struct cpu_timer_list, entry)->expires.cpu,
1382 * Rejigger each thread's expiry time so that one will
1383 * notice before we hit the process-cumulative expiry time.
1385 union cpu_time_count expires = { .sched = 0 };
1386 expires.cpu = *newval;
1387 process_timer_rebalance(tsk, clock_idx, expires, now);
1391 static long posix_cpu_clock_nanosleep_restart(struct restart_block *);
1393 int posix_cpu_nsleep(clockid_t which_clock, int flags,
1394 struct timespec *rqtp)
1396 struct restart_block *restart_block =
1397 ¤t_thread_info()->restart_block;
1398 struct k_itimer timer;
1402 * Diagnose required errors first.
1404 if (CPUCLOCK_PERTHREAD(which_clock) &&
1405 (CPUCLOCK_PID(which_clock) == 0 ||
1406 CPUCLOCK_PID(which_clock) == current->pid))
1410 * Set up a temporary timer and then wait for it to go off.
1412 memset(&timer, 0, sizeof timer);
1413 spin_lock_init(&timer.it_lock);
1414 timer.it_clock = which_clock;
1415 timer.it_overrun = -1;
1416 error = posix_cpu_timer_create(&timer);
1417 timer.it_process = current;
1419 struct timespec __user *rmtp;
1420 static struct itimerspec zero_it;
1421 struct itimerspec it = { .it_value = *rqtp,
1422 .it_interval = {} };
1424 spin_lock_irq(&timer.it_lock);
1425 error = posix_cpu_timer_set(&timer, flags, &it, NULL);
1427 spin_unlock_irq(&timer.it_lock);
1431 while (!signal_pending(current)) {
1432 if (timer.it.cpu.expires.sched == 0) {
1434 * Our timer fired and was reset.
1436 spin_unlock_irq(&timer.it_lock);
1441 * Block until cpu_timer_fire (or a signal) wakes us.
1443 __set_current_state(TASK_INTERRUPTIBLE);
1444 spin_unlock_irq(&timer.it_lock);
1446 spin_lock_irq(&timer.it_lock);
1450 * We were interrupted by a signal.
1452 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
1453 posix_cpu_timer_set(&timer, 0, &zero_it, &it);
1454 spin_unlock_irq(&timer.it_lock);
1456 if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
1458 * It actually did fire already.
1464 * Report back to the user the time still remaining.
1466 rmtp = (struct timespec __user *) restart_block->arg1;
1467 if (rmtp != NULL && !(flags & TIMER_ABSTIME) &&
1468 copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1471 restart_block->fn = posix_cpu_clock_nanosleep_restart;
1472 /* Caller already set restart_block->arg1 */
1473 restart_block->arg0 = which_clock;
1474 restart_block->arg2 = rqtp->tv_sec;
1475 restart_block->arg3 = rqtp->tv_nsec;
1477 error = -ERESTART_RESTARTBLOCK;
1484 posix_cpu_clock_nanosleep_restart(struct restart_block *restart_block)
1486 clockid_t which_clock = restart_block->arg0;
1487 struct timespec t = { .tv_sec = restart_block->arg2,
1488 .tv_nsec = restart_block->arg3 };
1489 restart_block->fn = do_no_restart_syscall;
1490 return posix_cpu_nsleep(which_clock, TIMER_ABSTIME, &t);
1494 #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1495 #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1497 static int process_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
1499 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1501 static int process_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
1503 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1505 static int process_cpu_timer_create(struct k_itimer *timer)
1507 timer->it_clock = PROCESS_CLOCK;
1508 return posix_cpu_timer_create(timer);
1510 static int process_cpu_nsleep(clockid_t which_clock, int flags,
1511 struct timespec *rqtp)
1513 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
1515 static int thread_cpu_clock_getres(clockid_t which_clock, struct timespec *tp)
1517 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1519 static int thread_cpu_clock_get(clockid_t which_clock, struct timespec *tp)
1521 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1523 static int thread_cpu_timer_create(struct k_itimer *timer)
1525 timer->it_clock = THREAD_CLOCK;
1526 return posix_cpu_timer_create(timer);
1528 static int thread_cpu_nsleep(clockid_t which_clock, int flags,
1529 struct timespec *rqtp)
1534 static __init int init_posix_cpu_timers(void)
1536 struct k_clock process = {
1537 .clock_getres = process_cpu_clock_getres,
1538 .clock_get = process_cpu_clock_get,
1539 .clock_set = do_posix_clock_nosettime,
1540 .timer_create = process_cpu_timer_create,
1541 .nsleep = process_cpu_nsleep,
1543 struct k_clock thread = {
1544 .clock_getres = thread_cpu_clock_getres,
1545 .clock_get = thread_cpu_clock_get,
1546 .clock_set = do_posix_clock_nosettime,
1547 .timer_create = thread_cpu_timer_create,
1548 .nsleep = thread_cpu_nsleep,
1551 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1552 register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1556 __initcall(init_posix_cpu_timers);