5 #include <linux/sched.h>
7 #include <linux/errno.h>
9 #include <asm/uaccess.h>
23 spin_lock_irq(&task->
sighand->siglock);
25 spin_unlock_irq(&task->
sighand->siglock);
28 static int check_clock(
const clockid_t which_clock)
43 same_thread_group(p,
current) : has_group_leader_pid(p))) {
64 static void sample_to_timespec(
const clockid_t which_clock,
74 static inline int cpu_time_before(
const clockid_t which_clock,
84 static inline void cpu_time_add(
const clockid_t which_clock,
115 if (timer->
it.
cpu.incr.sched == 0)
119 unsigned long long delta, incr;
123 incr = timer->
it.
cpu.incr.sched;
124 delta = now.
sched + incr - timer->
it.
cpu.expires.sched;
126 for (i = 0; incr < delta - incr; i++)
128 for (; i >= 0; incr >>= 1, i--) {
131 timer->
it.
cpu.expires.sched += incr;
138 if (now.
cpu < timer->
it.
cpu.expires.cpu)
140 incr = timer->
it.
cpu.incr.cpu;
141 delta = now.
cpu + incr - timer->
it.
cpu.expires.cpu;
143 for (i = 0; incr < delta - incr; i++)
145 for (; i >= 0; incr = incr >> 1, i--) {
148 timer->
it.
cpu.expires.cpu += incr;
167 int error = check_clock(which_clock);
190 int error = check_clock(which_clock);
208 cpu->
cpu = prof_ticks(p);
211 cpu->
cpu = virt_ticks(p);
231 if (!
likely(pid_alive(tsk)))
272 update_gt_cputime(&cputimer->
cputime, &sum);
283 static int cpu_clock_sample_group(
const clockid_t which_clock,
294 cpu->
cpu = cputime.utime + cputime.stime;
298 cpu->
cpu = cputime.utime;
302 cpu->
sched = cputime.sum_exec_runtime;
324 error = cpu_clock_sample(which_clock,
328 error = cpu_clock_sample_group(which_clock,
342 if (same_thread_group(p,
current)) {
343 error = cpu_clock_sample(which_clock,
348 if (thread_group_leader(p) && p->
sighand) {
350 cpu_clock_sample_group(which_clock,
361 sample_to_timespec(which_clock, rtn, tp);
371 static int posix_cpu_timer_create(
struct k_itimer *new_timer)
380 INIT_LIST_HEAD(&new_timer->
it.
cpu.entry);
388 if (p && !same_thread_group(p,
current))
396 if (p && !has_group_leader_pid(p))
400 new_timer->
it.
cpu.task =
p;
417 static int posix_cpu_timer_del(
struct k_itimer *timer)
431 spin_lock(&p->
sighand->siglock);
432 if (timer->
it.
cpu.firing)
436 spin_unlock(&p->
sighand->siglock);
455 unsigned long long sum_exec_runtime)
461 list_del_init(&timer->
entry);
462 if (timer->expires.cpu < ptime) {
463 timer->expires.cpu = 0;
465 timer->expires.cpu -= ptime;
471 list_del_init(&timer->
entry);
472 if (timer->expires.cpu < utime) {
473 timer->expires.cpu = 0;
475 timer->expires.cpu -=
utime;
481 list_del_init(&timer->
entry);
482 if (timer->expires.sched < sum_exec_runtime) {
483 timer->expires.sched = 0;
485 timer->expires.sched -= sum_exec_runtime;
505 cleanup_timers(tsk->
signal->cpu_timers,
516 put_task_struct(timer->
it.
cpu.task);
519 timer->
it.
cpu.expires,
525 return expires == 0 || expires > new_exp;
533 static void arm_timer(
struct k_itimer *timer)
545 head = p->
signal->cpu_timers;
546 cputime_expires = &p->
signal->cputime_expires;
552 if (cpu_time_before(timer->
it_clock, nt->expires, next->expires))
554 listpos = &next->
entry;
556 list_add(&nt->
entry, listpos);
558 if (listpos == head) {
570 if (expires_gt(cputime_expires->prof_exp, exp->
cpu))
571 cputime_expires->prof_exp = exp->
cpu;
574 if (expires_gt(cputime_expires->virt_exp, exp->
cpu))
575 cputime_expires->virt_exp = exp->
cpu;
578 if (cputime_expires->sched_exp == 0 ||
579 cputime_expires->sched_exp > exp->
sched)
580 cputime_expires->sched_exp = exp->
sched;
589 static void cpu_timer_fire(
struct k_itimer *timer)
595 timer->
it.
cpu.expires.sched = 0;
602 timer->
it.
cpu.expires.sched = 0;
603 }
else if (timer->
it.
cpu.incr.sched == 0) {
608 timer->
it.
cpu.expires.sched = 0;
624 static int cpu_timer_sample_group(
const clockid_t which_clock,
635 cpu->
cpu = cputime.utime + cputime.stime;
638 cpu->
cpu = cputime.utime;
653 static int posix_cpu_timer_set(
struct k_itimer *timer,
int flags,
667 new_expires = timespec_to_sample(timer->
it_clock, &new->it_value);
688 old_incr = timer->
it.
cpu.incr;
689 spin_lock(&p->
sighand->siglock);
690 old_expires = timer->
it.
cpu.expires;
692 timer->
it.
cpu.firing = -1;
695 list_del_init(&timer->
it.
cpu.entry);
706 cpu_clock_sample(timer->
it_clock, p, &val);
708 cpu_timer_sample_group(timer->
it_clock, p, &val);
712 if (old_expires.sched == 0) {
726 bump_cpu_timer(timer, val);
727 if (cpu_time_before(timer->
it_clock, val,
728 timer->
it.
cpu.expires)) {
729 old_expires = cpu_time_sub(
731 timer->
it.
cpu.expires, val);
749 spin_unlock(&p->
sighand->siglock);
755 cpu_time_add(timer->
it_clock, &new_expires, val);
763 timer->
it.
cpu.expires = new_expires;
764 if (new_expires.sched != 0 &&
765 cpu_time_before(timer->
it_clock, val, new_expires)) {
769 spin_unlock(&p->
sighand->siglock);
789 if (new_expires.sched != 0 &&
790 !cpu_time_before(timer->
it_clock, val, new_expires)) {
796 cpu_timer_fire(timer);
820 if (timer->
it.
cpu.expires.sched == 0) {
840 cpu_clock_sample(timer->
it_clock, p, &now);
852 timer->
it.
cpu.expires.sched = 0;
856 cpu_timer_sample_group(timer->
it_clock, p, &now);
858 thread_group_empty(p));
869 clear_dead_task(timer, now);
873 if (cpu_time_before(timer->
it_clock, now, timer->
it.
cpu.expires)) {
876 timer->
it.
cpu.expires, now),
893 static void check_thread_timers(
struct task_struct *tsk,
903 while (!list_empty(timers)) {
907 if (!--maxfire || prof_ticks(tsk) < t->expires.cpu) {
912 list_move_tail(&t->
entry, firing);
918 while (!list_empty(timers)) {
922 if (!--maxfire || virt_ticks(tsk) < t->expires.cpu) {
927 list_move_tail(&t->
entry, firing);
933 while (!list_empty(timers)) {
937 if (!--maxfire || tsk->
se.sum_exec_runtime < t->expires.sched) {
942 list_move_tail(&t->
entry, firing);
971 "RT Watchdog Timeout: %s[%d]\n",
972 tsk->
comm, task_pid_nr(tsk));
988 static u32 onecputick;
1000 if (it->
error >= onecputick) {
1002 it->
error -= onecputick;
1008 trace_itimer_expire(signo ==
SIGPROF ?
1010 tsk->
signal->leader_pid, cur_time);
1039 static void check_process_timers(
struct task_struct *tsk,
1054 utime = cputime.
utime;
1055 ptime = utime + cputime.
stime;
1059 while (!list_empty(timers)) {
1063 if (!--maxfire || ptime < tl->expires.cpu) {
1064 prof_expires = tl->expires.cpu;
1068 list_move_tail(&tl->
entry, firing);
1074 while (!list_empty(timers)) {
1078 if (!--maxfire || utime < tl->expires.cpu) {
1079 virt_expires = tl->expires.cpu;
1083 list_move_tail(&tl->
entry, firing);
1089 while (!list_empty(timers)) {
1093 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1094 sched_expires = tl->expires.sched;
1098 list_move_tail(&tl->
entry, firing);
1111 unsigned long hard =
1114 if (psecs >= hard) {
1122 if (psecs >= soft) {
1133 if (!prof_expires || x < prof_expires) {
1142 stop_process_timers(sig);
1164 cpu_clock_sample(timer->
it_clock, p, &now);
1165 bump_cpu_timer(timer, now);
1167 clear_dead_task(timer, now);
1171 spin_lock(&p->
sighand->siglock);
1181 timer->
it.
cpu.expires.sched = 0;
1189 clear_dead_task(timer, now);
1192 spin_lock(&p->
sighand->siglock);
1193 cpu_timer_sample_group(timer->
it_clock, p, &now);
1194 bump_cpu_timer(timer, now);
1203 spin_unlock(&p->
sighand->siglock);
1247 static inline int fastpath_timer_check(
struct task_struct *tsk)
1254 .stime = tsk->
stime,
1255 .sum_exec_runtime = tsk->
se.sum_exec_runtime
1267 group_sample = sig->
cputimer.cputime;
1286 unsigned long flags;
1294 if (!fastpath_timer_check(tsk))
1297 if (!lock_task_sighand(tsk, &flags))
1304 check_thread_timers(tsk, &firing);
1309 if (tsk->
signal->cputimer.running)
1310 check_process_timers(tsk, &firing);
1320 unlock_task_sighand(tsk, &flags);
1332 list_del_init(&timer->
it.
cpu.entry);
1333 cpu_firing = timer->
it.
cpu.firing;
1334 timer->
it.
cpu.firing = 0;
1340 if (
likely(cpu_firing >= 0))
1341 cpu_timer_fire(timer);
1356 cpu_timer_sample_group(clock_idx, tsk, &now);
1365 if (*oldval <= now.
cpu) {
1382 switch (clock_idx) {
1384 if (expires_gt(tsk->
signal->cputime_expires.prof_exp, *newval))
1385 tsk->
signal->cputime_expires.prof_exp = *newval;
1388 if (expires_gt(tsk->
signal->cputime_expires.virt_exp, *newval))
1389 tsk->
signal->cputime_expires.virt_exp = *newval;
1394 static int do_cpu_nanosleep(
const clockid_t which_clock,
int flags,
1403 memset(&timer, 0,
sizeof timer);
1407 error = posix_cpu_timer_create(&timer);
1412 memset(it, 0,
sizeof *it);
1415 spin_lock_irq(&timer.
it_lock);
1416 error = posix_cpu_timer_set(&timer, flags, it,
NULL);
1418 spin_unlock_irq(&timer.
it_lock);
1422 while (!signal_pending(
current)) {
1423 if (timer.
it.
cpu.expires.sched == 0) {
1427 spin_unlock_irq(&timer.
it_lock);
1435 spin_unlock_irq(&timer.
it_lock);
1437 spin_lock_irq(&timer.
it_lock);
1443 sample_to_timespec(which_clock, timer.
it.
cpu.expires, rqtp);
1444 posix_cpu_timer_set(&timer, 0, &zero_it, it);
1445 spin_unlock_irq(&timer.
it_lock);
1462 static int posix_cpu_nsleep(
const clockid_t which_clock,
int flags,
1478 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1482 if (flags & TIMER_ABSTIME)
1490 restart_block->
fn = posix_cpu_nsleep_restart;
1491 restart_block->
nanosleep.clockid = which_clock;
1493 restart_block->
nanosleep.expires = timespec_to_ns(rqtp);
1498 static long posix_cpu_nsleep_restart(
struct restart_block *restart_block)
1507 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1517 restart_block->
nanosleep.expires = timespec_to_ns(&t);
1523 #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1524 #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1526 static int process_cpu_clock_getres(
const clockid_t which_clock,
1531 static int process_cpu_clock_get(
const clockid_t which_clock,
1536 static int process_cpu_timer_create(
struct k_itimer *timer)
1539 return posix_cpu_timer_create(timer);
1541 static int process_cpu_nsleep(
const clockid_t which_clock,
int flags,
1547 static long process_cpu_nsleep_restart(
struct restart_block *restart_block)
1551 static int thread_cpu_clock_getres(
const clockid_t which_clock,
1556 static int thread_cpu_clock_get(
const clockid_t which_clock,
1561 static int thread_cpu_timer_create(
struct k_itimer *timer)
1564 return posix_cpu_timer_create(timer);
1568 .clock_getres = posix_cpu_clock_getres,
1569 .clock_set = posix_cpu_clock_set,
1570 .clock_get = posix_cpu_clock_get,
1571 .timer_create = posix_cpu_timer_create,
1572 .nsleep = posix_cpu_nsleep,
1573 .nsleep_restart = posix_cpu_nsleep_restart,
1574 .timer_set = posix_cpu_timer_set,
1575 .timer_del = posix_cpu_timer_del,
1576 .timer_get = posix_cpu_timer_get,
1579 static __init int init_posix_cpu_timers(
void)
1583 .clock_get = process_cpu_clock_get,
1584 .timer_create = process_cpu_timer_create,
1585 .nsleep = process_cpu_nsleep,
1586 .nsleep_restart = process_cpu_nsleep_restart,
1589 .clock_getres = thread_cpu_clock_getres,
1590 .clock_get = thread_cpu_clock_get,
1591 .timer_create = thread_cpu_timer_create,
1599 onecputick =
ts.tv_nsec;