aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/posix-cpu-timers.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/posix-cpu-timers.c')
-rw-r--r--kernel/posix-cpu-timers.c387
1 files changed, 148 insertions, 239 deletions
diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index 5c9dc228747b..f66bdd33a6c6 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -11,19 +11,18 @@
11#include <trace/events/timer.h> 11#include <trace/events/timer.h>
12 12
13/* 13/*
14 * Called after updating RLIMIT_CPU to set timer expiration if necessary. 14 * Called after updating RLIMIT_CPU to run cpu timer and update
15 * tsk->signal->cputime_expires expiration cache if necessary. Needs
16 * siglock protection since other code may update expiration cache as
17 * well.
15 */ 18 */
16void update_rlimit_cpu(unsigned long rlim_new) 19void update_rlimit_cpu(unsigned long rlim_new)
17{ 20{
18 cputime_t cputime = secs_to_cputime(rlim_new); 21 cputime_t cputime = secs_to_cputime(rlim_new);
19 struct signal_struct *const sig = current->signal;
20 22
21 if (cputime_eq(sig->it[CPUCLOCK_PROF].expires, cputime_zero) || 23 spin_lock_irq(&current->sighand->siglock);
22 cputime_gt(sig->it[CPUCLOCK_PROF].expires, cputime)) { 24 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
23 spin_lock_irq(&current->sighand->siglock); 25 spin_unlock_irq(&current->sighand->siglock);
24 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
25 spin_unlock_irq(&current->sighand->siglock);
26 }
27} 26}
28 27
29static int check_clock(const clockid_t which_clock) 28static int check_clock(const clockid_t which_clock)
@@ -233,31 +232,24 @@ static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
233 232
234void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times) 233void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
235{ 234{
236 struct sighand_struct *sighand; 235 struct signal_struct *sig = tsk->signal;
237 struct signal_struct *sig;
238 struct task_struct *t; 236 struct task_struct *t;
239 237
240 *times = INIT_CPUTIME; 238 times->utime = sig->utime;
239 times->stime = sig->stime;
240 times->sum_exec_runtime = sig->sum_sched_runtime;
241 241
242 rcu_read_lock(); 242 rcu_read_lock();
243 sighand = rcu_dereference(tsk->sighand); 243 /* make sure we can trust tsk->thread_group list */
244 if (!sighand) 244 if (!likely(pid_alive(tsk)))
245 goto out; 245 goto out;
246 246
247 sig = tsk->signal;
248
249 t = tsk; 247 t = tsk;
250 do { 248 do {
251 times->utime = cputime_add(times->utime, t->utime); 249 times->utime = cputime_add(times->utime, t->utime);
252 times->stime = cputime_add(times->stime, t->stime); 250 times->stime = cputime_add(times->stime, t->stime);
253 times->sum_exec_runtime += t->se.sum_exec_runtime; 251 times->sum_exec_runtime += t->se.sum_exec_runtime;
254 252 } while_each_thread(tsk, t);
255 t = next_thread(t);
256 } while (t != tsk);
257
258 times->utime = cputime_add(times->utime, sig->utime);
259 times->stime = cputime_add(times->stime, sig->stime);
260 times->sum_exec_runtime += sig->sum_sched_runtime;
261out: 253out:
262 rcu_read_unlock(); 254 rcu_read_unlock();
263} 255}
@@ -364,7 +356,7 @@ int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
364 } 356 }
365 } else { 357 } else {
366 read_lock(&tasklist_lock); 358 read_lock(&tasklist_lock);
367 if (thread_group_leader(p) && p->signal) { 359 if (thread_group_leader(p) && p->sighand) {
368 error = 360 error =
369 cpu_clock_sample_group(which_clock, 361 cpu_clock_sample_group(which_clock,
370 p, &rtn); 362 p, &rtn);
@@ -384,7 +376,8 @@ int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
384 376
385/* 377/*
386 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer. 378 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
387 * This is called from sys_timer_create with the new timer already locked. 379 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
380 * new timer already all-zeros initialized.
388 */ 381 */
389int posix_cpu_timer_create(struct k_itimer *new_timer) 382int posix_cpu_timer_create(struct k_itimer *new_timer)
390{ 383{
@@ -396,8 +389,6 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
396 return -EINVAL; 389 return -EINVAL;
397 390
398 INIT_LIST_HEAD(&new_timer->it.cpu.entry); 391 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
399 new_timer->it.cpu.incr.sched = 0;
400 new_timer->it.cpu.expires.sched = 0;
401 392
402 read_lock(&tasklist_lock); 393 read_lock(&tasklist_lock);
403 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) { 394 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
@@ -441,7 +432,7 @@ int posix_cpu_timer_del(struct k_itimer *timer)
441 432
442 if (likely(p != NULL)) { 433 if (likely(p != NULL)) {
443 read_lock(&tasklist_lock); 434 read_lock(&tasklist_lock);
444 if (unlikely(p->signal == NULL)) { 435 if (unlikely(p->sighand == NULL)) {
445 /* 436 /*
446 * We raced with the reaping of the task. 437 * We raced with the reaping of the task.
447 * The deletion should have cleared us off the list. 438 * The deletion should have cleared us off the list.
@@ -549,111 +540,62 @@ static inline int expires_gt(cputime_t expires, cputime_t new_exp)
549 cputime_gt(expires, new_exp); 540 cputime_gt(expires, new_exp);
550} 541}
551 542
552static inline int expires_le(cputime_t expires, cputime_t new_exp)
553{
554 return !cputime_eq(expires, cputime_zero) &&
555 cputime_le(expires, new_exp);
556}
557/* 543/*
558 * Insert the timer on the appropriate list before any timers that 544 * Insert the timer on the appropriate list before any timers that
559 * expire later. This must be called with the tasklist_lock held 545 * expire later. This must be called with the tasklist_lock held
560 * for reading, and interrupts disabled. 546 * for reading, interrupts disabled and p->sighand->siglock taken.
561 */ 547 */
562static void arm_timer(struct k_itimer *timer, union cpu_time_count now) 548static void arm_timer(struct k_itimer *timer)
563{ 549{
564 struct task_struct *p = timer->it.cpu.task; 550 struct task_struct *p = timer->it.cpu.task;
565 struct list_head *head, *listpos; 551 struct list_head *head, *listpos;
552 struct task_cputime *cputime_expires;
566 struct cpu_timer_list *const nt = &timer->it.cpu; 553 struct cpu_timer_list *const nt = &timer->it.cpu;
567 struct cpu_timer_list *next; 554 struct cpu_timer_list *next;
568 unsigned long i;
569 555
570 head = (CPUCLOCK_PERTHREAD(timer->it_clock) ? 556 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
571 p->cpu_timers : p->signal->cpu_timers); 557 head = p->cpu_timers;
558 cputime_expires = &p->cputime_expires;
559 } else {
560 head = p->signal->cpu_timers;
561 cputime_expires = &p->signal->cputime_expires;
562 }
572 head += CPUCLOCK_WHICH(timer->it_clock); 563 head += CPUCLOCK_WHICH(timer->it_clock);
573 564
574 BUG_ON(!irqs_disabled());
575 spin_lock(&p->sighand->siglock);
576
577 listpos = head; 565 listpos = head;
578 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) { 566 list_for_each_entry(next, head, entry) {
579 list_for_each_entry(next, head, entry) { 567 if (cpu_time_before(timer->it_clock, nt->expires, next->expires))
580 if (next->expires.sched > nt->expires.sched) 568 break;
581 break; 569 listpos = &next->entry;
582 listpos = &next->entry;
583 }
584 } else {
585 list_for_each_entry(next, head, entry) {
586 if (cputime_gt(next->expires.cpu, nt->expires.cpu))
587 break;
588 listpos = &next->entry;
589 }
590 } 570 }
591 list_add(&nt->entry, listpos); 571 list_add(&nt->entry, listpos);
592 572
593 if (listpos == head) { 573 if (listpos == head) {
574 union cpu_time_count *exp = &nt->expires;
575
594 /* 576 /*
595 * We are the new earliest-expiring timer. 577 * We are the new earliest-expiring POSIX 1.b timer, hence
596 * If we are a thread timer, there can always 578 * need to update expiration cache. Take into account that
597 * be a process timer telling us to stop earlier. 579 * for process timers we share expiration cache with itimers
580 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
598 */ 581 */
599 582
600 if (CPUCLOCK_PERTHREAD(timer->it_clock)) { 583 switch (CPUCLOCK_WHICH(timer->it_clock)) {
601 union cpu_time_count *exp = &nt->expires; 584 case CPUCLOCK_PROF:
602 585 if (expires_gt(cputime_expires->prof_exp, exp->cpu))
603 switch (CPUCLOCK_WHICH(timer->it_clock)) { 586 cputime_expires->prof_exp = exp->cpu;
604 default: 587 break;
605 BUG(); 588 case CPUCLOCK_VIRT:
606 case CPUCLOCK_PROF: 589 if (expires_gt(cputime_expires->virt_exp, exp->cpu))
607 if (expires_gt(p->cputime_expires.prof_exp, 590 cputime_expires->virt_exp = exp->cpu;
608 exp->cpu)) 591 break;
609 p->cputime_expires.prof_exp = exp->cpu; 592 case CPUCLOCK_SCHED:
610 break; 593 if (cputime_expires->sched_exp == 0 ||
611 case CPUCLOCK_VIRT: 594 cputime_expires->sched_exp > exp->sched)
612 if (expires_gt(p->cputime_expires.virt_exp, 595 cputime_expires->sched_exp = exp->sched;
613 exp->cpu)) 596 break;
614 p->cputime_expires.virt_exp = exp->cpu;
615 break;
616 case CPUCLOCK_SCHED:
617 if (p->cputime_expires.sched_exp == 0 ||
618 p->cputime_expires.sched_exp > exp->sched)
619 p->cputime_expires.sched_exp =
620 exp->sched;
621 break;
622 }
623 } else {
624 struct signal_struct *const sig = p->signal;
625 union cpu_time_count *exp = &timer->it.cpu.expires;
626
627 /*
628 * For a process timer, set the cached expiration time.
629 */
630 switch (CPUCLOCK_WHICH(timer->it_clock)) {
631 default:
632 BUG();
633 case CPUCLOCK_VIRT:
634 if (expires_le(sig->it[CPUCLOCK_VIRT].expires,
635 exp->cpu))
636 break;
637 sig->cputime_expires.virt_exp = exp->cpu;
638 break;
639 case CPUCLOCK_PROF:
640 if (expires_le(sig->it[CPUCLOCK_PROF].expires,
641 exp->cpu))
642 break;
643 i = sig->rlim[RLIMIT_CPU].rlim_cur;
644 if (i != RLIM_INFINITY &&
645 i <= cputime_to_secs(exp->cpu))
646 break;
647 sig->cputime_expires.prof_exp = exp->cpu;
648 break;
649 case CPUCLOCK_SCHED:
650 sig->cputime_expires.sched_exp = exp->sched;
651 break;
652 }
653 } 597 }
654 } 598 }
655
656 spin_unlock(&p->sighand->siglock);
657} 599}
658 600
659/* 601/*
@@ -661,7 +603,12 @@ static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
661 */ 603 */
662static void cpu_timer_fire(struct k_itimer *timer) 604static void cpu_timer_fire(struct k_itimer *timer)
663{ 605{
664 if (unlikely(timer->sigq == NULL)) { 606 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
607 /*
608 * User don't want any signal.
609 */
610 timer->it.cpu.expires.sched = 0;
611 } else if (unlikely(timer->sigq == NULL)) {
665 /* 612 /*
666 * This a special case for clock_nanosleep, 613 * This a special case for clock_nanosleep,
667 * not a normal timer from sys_timer_create. 614 * not a normal timer from sys_timer_create.
@@ -722,7 +669,7 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags,
722 struct itimerspec *new, struct itimerspec *old) 669 struct itimerspec *new, struct itimerspec *old)
723{ 670{
724 struct task_struct *p = timer->it.cpu.task; 671 struct task_struct *p = timer->it.cpu.task;
725 union cpu_time_count old_expires, new_expires, val; 672 union cpu_time_count old_expires, new_expires, old_incr, val;
726 int ret; 673 int ret;
727 674
728 if (unlikely(p == NULL)) { 675 if (unlikely(p == NULL)) {
@@ -737,10 +684,10 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags,
737 read_lock(&tasklist_lock); 684 read_lock(&tasklist_lock);
738 /* 685 /*
739 * We need the tasklist_lock to protect against reaping that 686 * We need the tasklist_lock to protect against reaping that
740 * clears p->signal. If p has just been reaped, we can no 687 * clears p->sighand. If p has just been reaped, we can no
741 * longer get any information about it at all. 688 * longer get any information about it at all.
742 */ 689 */
743 if (unlikely(p->signal == NULL)) { 690 if (unlikely(p->sighand == NULL)) {
744 read_unlock(&tasklist_lock); 691 read_unlock(&tasklist_lock);
745 put_task_struct(p); 692 put_task_struct(p);
746 timer->it.cpu.task = NULL; 693 timer->it.cpu.task = NULL;
@@ -753,6 +700,7 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags,
753 BUG_ON(!irqs_disabled()); 700 BUG_ON(!irqs_disabled());
754 701
755 ret = 0; 702 ret = 0;
703 old_incr = timer->it.cpu.incr;
756 spin_lock(&p->sighand->siglock); 704 spin_lock(&p->sighand->siglock);
757 old_expires = timer->it.cpu.expires; 705 old_expires = timer->it.cpu.expires;
758 if (unlikely(timer->it.cpu.firing)) { 706 if (unlikely(timer->it.cpu.firing)) {
@@ -760,7 +708,6 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags,
760 ret = TIMER_RETRY; 708 ret = TIMER_RETRY;
761 } else 709 } else
762 list_del_init(&timer->it.cpu.entry); 710 list_del_init(&timer->it.cpu.entry);
763 spin_unlock(&p->sighand->siglock);
764 711
765 /* 712 /*
766 * We need to sample the current value to convert the new 713 * We need to sample the current value to convert the new
@@ -814,6 +761,7 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags,
814 * disable this firing since we are already reporting 761 * disable this firing since we are already reporting
815 * it as an overrun (thanks to bump_cpu_timer above). 762 * it as an overrun (thanks to bump_cpu_timer above).
816 */ 763 */
764 spin_unlock(&p->sighand->siglock);
817 read_unlock(&tasklist_lock); 765 read_unlock(&tasklist_lock);
818 goto out; 766 goto out;
819 } 767 }
@@ -829,11 +777,11 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags,
829 */ 777 */
830 timer->it.cpu.expires = new_expires; 778 timer->it.cpu.expires = new_expires;
831 if (new_expires.sched != 0 && 779 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)) { 780 cpu_time_before(timer->it_clock, val, new_expires)) {
834 arm_timer(timer, val); 781 arm_timer(timer);
835 } 782 }
836 783
784 spin_unlock(&p->sighand->siglock);
837 read_unlock(&tasklist_lock); 785 read_unlock(&tasklist_lock);
838 786
839 /* 787 /*
@@ -854,7 +802,6 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags,
854 timer->it_overrun = -1; 802 timer->it_overrun = -1;
855 803
856 if (new_expires.sched != 0 && 804 if (new_expires.sched != 0 &&
857 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
858 !cpu_time_before(timer->it_clock, val, new_expires)) { 805 !cpu_time_before(timer->it_clock, val, new_expires)) {
859 /* 806 /*
860 * The designated time already passed, so we notify 807 * The designated time already passed, so we notify
@@ -868,7 +815,7 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags,
868 out: 815 out:
869 if (old) { 816 if (old) {
870 sample_to_timespec(timer->it_clock, 817 sample_to_timespec(timer->it_clock,
871 timer->it.cpu.incr, &old->it_interval); 818 old_incr, &old->it_interval);
872 } 819 }
873 return ret; 820 return ret;
874} 821}
@@ -909,7 +856,7 @@ void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
909 clear_dead = p->exit_state; 856 clear_dead = p->exit_state;
910 } else { 857 } else {
911 read_lock(&tasklist_lock); 858 read_lock(&tasklist_lock);
912 if (unlikely(p->signal == NULL)) { 859 if (unlikely(p->sighand == NULL)) {
913 /* 860 /*
914 * The process has been reaped. 861 * The process has been reaped.
915 * We can't even collect a sample any more. 862 * We can't even collect a sample any more.
@@ -928,25 +875,6 @@ void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
928 read_unlock(&tasklist_lock); 875 read_unlock(&tasklist_lock);
929 } 876 }
930 877
931 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
932 if (timer->it.cpu.incr.sched == 0 &&
933 cpu_time_before(timer->it_clock,
934 timer->it.cpu.expires, now)) {
935 /*
936 * Do-nothing timer expired and has no reload,
937 * so it's as if it was never set.
938 */
939 timer->it.cpu.expires.sched = 0;
940 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
941 return;
942 }
943 /*
944 * Account for any expirations and reloads that should
945 * have happened.
946 */
947 bump_cpu_timer(timer, now);
948 }
949
950 if (unlikely(clear_dead)) { 878 if (unlikely(clear_dead)) {
951 /* 879 /*
952 * We've noticed that the thread is dead, but 880 * We've noticed that the thread is dead, but
@@ -983,6 +911,7 @@ static void check_thread_timers(struct task_struct *tsk,
983 int maxfire; 911 int maxfire;
984 struct list_head *timers = tsk->cpu_timers; 912 struct list_head *timers = tsk->cpu_timers;
985 struct signal_struct *const sig = tsk->signal; 913 struct signal_struct *const sig = tsk->signal;
914 unsigned long soft;
986 915
987 maxfire = 20; 916 maxfire = 20;
988 tsk->cputime_expires.prof_exp = cputime_zero; 917 tsk->cputime_expires.prof_exp = cputime_zero;
@@ -1031,9 +960,10 @@ static void check_thread_timers(struct task_struct *tsk,
1031 /* 960 /*
1032 * Check for the special case thread timers. 961 * Check for the special case thread timers.
1033 */ 962 */
1034 if (sig->rlim[RLIMIT_RTTIME].rlim_cur != RLIM_INFINITY) { 963 soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
1035 unsigned long hard = sig->rlim[RLIMIT_RTTIME].rlim_max; 964 if (soft != RLIM_INFINITY) {
1036 unsigned long *soft = &sig->rlim[RLIMIT_RTTIME].rlim_cur; 965 unsigned long hard =
966 ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
1037 967
1038 if (hard != RLIM_INFINITY && 968 if (hard != RLIM_INFINITY &&
1039 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) { 969 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
@@ -1044,14 +974,13 @@ static void check_thread_timers(struct task_struct *tsk,
1044 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); 974 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1045 return; 975 return;
1046 } 976 }
1047 if (tsk->rt.timeout > DIV_ROUND_UP(*soft, USEC_PER_SEC/HZ)) { 977 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
1048 /* 978 /*
1049 * At the soft limit, send a SIGXCPU every second. 979 * At the soft limit, send a SIGXCPU every second.
1050 */ 980 */
1051 if (sig->rlim[RLIMIT_RTTIME].rlim_cur 981 if (soft < hard) {
1052 < sig->rlim[RLIMIT_RTTIME].rlim_max) { 982 soft += USEC_PER_SEC;
1053 sig->rlim[RLIMIT_RTTIME].rlim_cur += 983 sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
1054 USEC_PER_SEC;
1055 } 984 }
1056 printk(KERN_INFO 985 printk(KERN_INFO
1057 "RT Watchdog Timeout: %s[%d]\n", 986 "RT Watchdog Timeout: %s[%d]\n",
@@ -1061,14 +990,11 @@ static void check_thread_timers(struct task_struct *tsk,
1061 } 990 }
1062} 991}
1063 992
1064static void stop_process_timers(struct task_struct *tsk) 993static void stop_process_timers(struct signal_struct *sig)
1065{ 994{
1066 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; 995 struct thread_group_cputimer *cputimer = &sig->cputimer;
1067 unsigned long flags; 996 unsigned long flags;
1068 997
1069 if (!cputimer->running)
1070 return;
1071
1072 spin_lock_irqsave(&cputimer->lock, flags); 998 spin_lock_irqsave(&cputimer->lock, flags);
1073 cputimer->running = 0; 999 cputimer->running = 0;
1074 spin_unlock_irqrestore(&cputimer->lock, flags); 1000 spin_unlock_irqrestore(&cputimer->lock, flags);
@@ -1108,6 +1034,23 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
1108 } 1034 }
1109} 1035}
1110 1036
1037/**
1038 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1039 *
1040 * @cputime: The struct to compare.
1041 *
1042 * Checks @cputime to see if all fields are zero. Returns true if all fields
1043 * are zero, false if any field is nonzero.
1044 */
1045static inline int task_cputime_zero(const struct task_cputime *cputime)
1046{
1047 if (cputime_eq(cputime->utime, cputime_zero) &&
1048 cputime_eq(cputime->stime, cputime_zero) &&
1049 cputime->sum_exec_runtime == 0)
1050 return 1;
1051 return 0;
1052}
1053
1111/* 1054/*
1112 * Check for any per-thread CPU timers that have fired and move them 1055 * Check for any per-thread CPU timers that have fired and move them
1113 * off the tsk->*_timers list onto the firing list. Per-thread timers 1056 * off the tsk->*_timers list onto the firing list. Per-thread timers
@@ -1122,19 +1065,7 @@ static void check_process_timers(struct task_struct *tsk,
1122 unsigned long long sum_sched_runtime, sched_expires; 1065 unsigned long long sum_sched_runtime, sched_expires;
1123 struct list_head *timers = sig->cpu_timers; 1066 struct list_head *timers = sig->cpu_timers;
1124 struct task_cputime cputime; 1067 struct task_cputime cputime;
1125 1068 unsigned long soft;
1126 /*
1127 * Don't sample the current process CPU clocks if there are no timers.
1128 */
1129 if (list_empty(&timers[CPUCLOCK_PROF]) &&
1130 cputime_eq(sig->it[CPUCLOCK_PROF].expires, cputime_zero) &&
1131 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
1132 list_empty(&timers[CPUCLOCK_VIRT]) &&
1133 cputime_eq(sig->it[CPUCLOCK_VIRT].expires, cputime_zero) &&
1134 list_empty(&timers[CPUCLOCK_SCHED])) {
1135 stop_process_timers(tsk);
1136 return;
1137 }
1138 1069
1139 /* 1070 /*
1140 * Collect the current process totals. 1071 * Collect the current process totals.
@@ -1194,11 +1125,13 @@ static void check_process_timers(struct task_struct *tsk,
1194 SIGPROF); 1125 SIGPROF);
1195 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime, 1126 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
1196 SIGVTALRM); 1127 SIGVTALRM);
1197 1128 soft = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
1198 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 1129 if (soft != RLIM_INFINITY) {
1199 unsigned long psecs = cputime_to_secs(ptime); 1130 unsigned long psecs = cputime_to_secs(ptime);
1131 unsigned long hard =
1132 ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
1200 cputime_t x; 1133 cputime_t x;
1201 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) { 1134 if (psecs >= hard) {
1202 /* 1135 /*
1203 * At the hard limit, we just die. 1136 * At the hard limit, we just die.
1204 * No need to calculate anything else now. 1137 * No need to calculate anything else now.
@@ -1206,35 +1139,28 @@ static void check_process_timers(struct task_struct *tsk,
1206 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); 1139 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1207 return; 1140 return;
1208 } 1141 }
1209 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) { 1142 if (psecs >= soft) {
1210 /* 1143 /*
1211 * At the soft limit, send a SIGXCPU every second. 1144 * At the soft limit, send a SIGXCPU every second.
1212 */ 1145 */
1213 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); 1146 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1214 if (sig->rlim[RLIMIT_CPU].rlim_cur 1147 if (soft < hard) {
1215 < sig->rlim[RLIMIT_CPU].rlim_max) { 1148 soft++;
1216 sig->rlim[RLIMIT_CPU].rlim_cur++; 1149 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
1217 } 1150 }
1218 } 1151 }
1219 x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur); 1152 x = secs_to_cputime(soft);
1220 if (cputime_eq(prof_expires, cputime_zero) || 1153 if (cputime_eq(prof_expires, cputime_zero) ||
1221 cputime_lt(x, prof_expires)) { 1154 cputime_lt(x, prof_expires)) {
1222 prof_expires = x; 1155 prof_expires = x;
1223 } 1156 }
1224 } 1157 }
1225 1158
1226 if (!cputime_eq(prof_expires, cputime_zero) && 1159 sig->cputime_expires.prof_exp = prof_expires;
1227 (cputime_eq(sig->cputime_expires.prof_exp, cputime_zero) || 1160 sig->cputime_expires.virt_exp = virt_expires;
1228 cputime_gt(sig->cputime_expires.prof_exp, prof_expires))) 1161 sig->cputime_expires.sched_exp = sched_expires;
1229 sig->cputime_expires.prof_exp = prof_expires; 1162 if (task_cputime_zero(&sig->cputime_expires))
1230 if (!cputime_eq(virt_expires, cputime_zero) && 1163 stop_process_timers(sig);
1231 (cputime_eq(sig->cputime_expires.virt_exp, cputime_zero) ||
1232 cputime_gt(sig->cputime_expires.virt_exp, virt_expires)))
1233 sig->cputime_expires.virt_exp = virt_expires;
1234 if (sched_expires != 0 &&
1235 (sig->cputime_expires.sched_exp == 0 ||
1236 sig->cputime_expires.sched_exp > sched_expires))
1237 sig->cputime_expires.sched_exp = sched_expires;
1238} 1164}
1239 1165
1240/* 1166/*
@@ -1263,9 +1189,10 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
1263 goto out; 1189 goto out;
1264 } 1190 }
1265 read_lock(&tasklist_lock); /* arm_timer needs it. */ 1191 read_lock(&tasklist_lock); /* arm_timer needs it. */
1192 spin_lock(&p->sighand->siglock);
1266 } else { 1193 } else {
1267 read_lock(&tasklist_lock); 1194 read_lock(&tasklist_lock);
1268 if (unlikely(p->signal == NULL)) { 1195 if (unlikely(p->sighand == NULL)) {
1269 /* 1196 /*
1270 * The process has been reaped. 1197 * The process has been reaped.
1271 * We can't even collect a sample any more. 1198 * We can't even collect a sample any more.
@@ -1283,6 +1210,7 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
1283 clear_dead_task(timer, now); 1210 clear_dead_task(timer, now);
1284 goto out_unlock; 1211 goto out_unlock;
1285 } 1212 }
1213 spin_lock(&p->sighand->siglock);
1286 cpu_timer_sample_group(timer->it_clock, p, &now); 1214 cpu_timer_sample_group(timer->it_clock, p, &now);
1287 bump_cpu_timer(timer, now); 1215 bump_cpu_timer(timer, now);
1288 /* Leave the tasklist_lock locked for the call below. */ 1216 /* Leave the tasklist_lock locked for the call below. */
@@ -1291,7 +1219,9 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
1291 /* 1219 /*
1292 * Now re-arm for the new expiry time. 1220 * Now re-arm for the new expiry time.
1293 */ 1221 */
1294 arm_timer(timer, now); 1222 BUG_ON(!irqs_disabled());
1223 arm_timer(timer);
1224 spin_unlock(&p->sighand->siglock);
1295 1225
1296out_unlock: 1226out_unlock:
1297 read_unlock(&tasklist_lock); 1227 read_unlock(&tasklist_lock);
@@ -1303,23 +1233,6 @@ out:
1303} 1233}
1304 1234
1305/** 1235/**
1306 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1307 *
1308 * @cputime: The struct to compare.
1309 *
1310 * Checks @cputime to see if all fields are zero. Returns true if all fields
1311 * are zero, false if any field is nonzero.
1312 */
1313static inline int task_cputime_zero(const struct task_cputime *cputime)
1314{
1315 if (cputime_eq(cputime->utime, cputime_zero) &&
1316 cputime_eq(cputime->stime, cputime_zero) &&
1317 cputime->sum_exec_runtime == 0)
1318 return 1;
1319 return 0;
1320}
1321
1322/**
1323 * task_cputime_expired - Compare two task_cputime entities. 1236 * task_cputime_expired - Compare two task_cputime entities.
1324 * 1237 *
1325 * @sample: The task_cputime structure to be checked for expiration. 1238 * @sample: The task_cputime structure to be checked for expiration.
@@ -1359,10 +1272,6 @@ static inline int fastpath_timer_check(struct task_struct *tsk)
1359{ 1272{
1360 struct signal_struct *sig; 1273 struct signal_struct *sig;
1361 1274
1362 /* tsk == current, ensure it is safe to use ->signal/sighand */
1363 if (unlikely(tsk->exit_state))
1364 return 0;
1365
1366 if (!task_cputime_zero(&tsk->cputime_expires)) { 1275 if (!task_cputime_zero(&tsk->cputime_expires)) {
1367 struct task_cputime task_sample = { 1276 struct task_cputime task_sample = {
1368 .utime = tsk->utime, 1277 .utime = tsk->utime,
@@ -1375,15 +1284,18 @@ static inline int fastpath_timer_check(struct task_struct *tsk)
1375 } 1284 }
1376 1285
1377 sig = tsk->signal; 1286 sig = tsk->signal;
1378 if (!task_cputime_zero(&sig->cputime_expires)) { 1287 if (sig->cputimer.running) {
1379 struct task_cputime group_sample; 1288 struct task_cputime group_sample;
1380 1289
1381 thread_group_cputimer(tsk, &group_sample); 1290 spin_lock(&sig->cputimer.lock);
1291 group_sample = sig->cputimer.cputime;
1292 spin_unlock(&sig->cputimer.lock);
1293
1382 if (task_cputime_expired(&group_sample, &sig->cputime_expires)) 1294 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1383 return 1; 1295 return 1;
1384 } 1296 }
1385 1297
1386 return sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY; 1298 return 0;
1387} 1299}
1388 1300
1389/* 1301/*
@@ -1395,6 +1307,7 @@ void run_posix_cpu_timers(struct task_struct *tsk)
1395{ 1307{
1396 LIST_HEAD(firing); 1308 LIST_HEAD(firing);
1397 struct k_itimer *timer, *next; 1309 struct k_itimer *timer, *next;
1310 unsigned long flags;
1398 1311
1399 BUG_ON(!irqs_disabled()); 1312 BUG_ON(!irqs_disabled());
1400 1313
@@ -1405,14 +1318,20 @@ void run_posix_cpu_timers(struct task_struct *tsk)
1405 if (!fastpath_timer_check(tsk)) 1318 if (!fastpath_timer_check(tsk))
1406 return; 1319 return;
1407 1320
1408 spin_lock(&tsk->sighand->siglock); 1321 if (!lock_task_sighand(tsk, &flags))
1322 return;
1409 /* 1323 /*
1410 * Here we take off tsk->signal->cpu_timers[N] and 1324 * Here we take off tsk->signal->cpu_timers[N] and
1411 * tsk->cpu_timers[N] all the timers that are firing, and 1325 * tsk->cpu_timers[N] all the timers that are firing, and
1412 * put them on the firing list. 1326 * put them on the firing list.
1413 */ 1327 */
1414 check_thread_timers(tsk, &firing); 1328 check_thread_timers(tsk, &firing);
1415 check_process_timers(tsk, &firing); 1329 /*
1330 * If there are any active process wide timers (POSIX 1.b, itimers,
1331 * RLIMIT_CPU) cputimer must be running.
1332 */
1333 if (tsk->signal->cputimer.running)
1334 check_process_timers(tsk, &firing);
1416 1335
1417 /* 1336 /*
1418 * We must release these locks before taking any timer's lock. 1337 * We must release these locks before taking any timer's lock.
@@ -1422,7 +1341,7 @@ void run_posix_cpu_timers(struct task_struct *tsk)
1422 * that gets the timer lock before we do will give it up and 1341 * that gets the timer lock before we do will give it up and
1423 * spin until we've taken care of that timer below. 1342 * spin until we've taken care of that timer below.
1424 */ 1343 */
1425 spin_unlock(&tsk->sighand->siglock); 1344 unlock_task_sighand(tsk, &flags);
1426 1345
1427 /* 1346 /*
1428 * Now that all the timers on our list have the firing flag, 1347 * Now that all the timers on our list have the firing flag,
@@ -1449,21 +1368,23 @@ void run_posix_cpu_timers(struct task_struct *tsk)
1449} 1368}
1450 1369
1451/* 1370/*
1452 * Set one of the process-wide special case CPU timers. 1371 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
1453 * The tsk->sighand->siglock must be held by the caller. 1372 * The tsk->sighand->siglock must be held by the caller.
1454 * The *newval argument is relative and we update it to be absolute, *oldval
1455 * is absolute and we update it to be relative.
1456 */ 1373 */
1457void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx, 1374void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1458 cputime_t *newval, cputime_t *oldval) 1375 cputime_t *newval, cputime_t *oldval)
1459{ 1376{
1460 union cpu_time_count now; 1377 union cpu_time_count now;
1461 struct list_head *head;
1462 1378
1463 BUG_ON(clock_idx == CPUCLOCK_SCHED); 1379 BUG_ON(clock_idx == CPUCLOCK_SCHED);
1464 cpu_timer_sample_group(clock_idx, tsk, &now); 1380 cpu_timer_sample_group(clock_idx, tsk, &now);
1465 1381
1466 if (oldval) { 1382 if (oldval) {
1383 /*
1384 * We are setting itimer. The *oldval is absolute and we update
1385 * it to be relative, *newval argument is relative and we update
1386 * it to be absolute.
1387 */
1467 if (!cputime_eq(*oldval, cputime_zero)) { 1388 if (!cputime_eq(*oldval, cputime_zero)) {
1468 if (cputime_le(*oldval, now.cpu)) { 1389 if (cputime_le(*oldval, now.cpu)) {
1469 /* Just about to fire. */ 1390 /* Just about to fire. */
@@ -1476,33 +1397,21 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1476 if (cputime_eq(*newval, cputime_zero)) 1397 if (cputime_eq(*newval, cputime_zero))
1477 return; 1398 return;
1478 *newval = cputime_add(*newval, now.cpu); 1399 *newval = cputime_add(*newval, now.cpu);
1479
1480 /*
1481 * If the RLIMIT_CPU timer will expire before the
1482 * ITIMER_PROF timer, we have nothing else to do.
1483 */
1484 if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1485 < cputime_to_secs(*newval))
1486 return;
1487 } 1400 }
1488 1401
1489 /* 1402 /*
1490 * Check whether there are any process timers already set to fire 1403 * Update expiration cache if we are the earliest timer, or eventually
1491 * before this one. If so, we don't have anything more to do. 1404 * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
1492 */ 1405 */
1493 head = &tsk->signal->cpu_timers[clock_idx]; 1406 switch (clock_idx) {
1494 if (list_empty(head) || 1407 case CPUCLOCK_PROF:
1495 cputime_ge(list_first_entry(head, 1408 if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
1496 struct cpu_timer_list, entry)->expires.cpu,
1497 *newval)) {
1498 switch (clock_idx) {
1499 case CPUCLOCK_PROF:
1500 tsk->signal->cputime_expires.prof_exp = *newval; 1409 tsk->signal->cputime_expires.prof_exp = *newval;
1501 break; 1410 break;
1502 case CPUCLOCK_VIRT: 1411 case CPUCLOCK_VIRT:
1412 if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
1503 tsk->signal->cputime_expires.virt_exp = *newval; 1413 tsk->signal->cputime_expires.virt_exp = *newval;
1504 break; 1414 break;
1505 }
1506 } 1415 }
1507} 1416}
1508 1417