aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/fork.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/fork.c')
-rw-r--r--kernel/fork.c97
1 files changed, 54 insertions, 43 deletions
diff --git a/kernel/fork.c b/kernel/fork.c
index 7ce2ebe84796..44e64d7ba29b 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -759,15 +759,44 @@ void __cleanup_sighand(struct sighand_struct *sighand)
759 kmem_cache_free(sighand_cachep, sighand); 759 kmem_cache_free(sighand_cachep, sighand);
760} 760}
761 761
762
763/*
764 * Initialize POSIX timer handling for a thread group.
765 */
766static void posix_cpu_timers_init_group(struct signal_struct *sig)
767{
768 /* Thread group counters. */
769 thread_group_cputime_init(sig);
770
771 /* Expiration times and increments. */
772 sig->it_virt_expires = cputime_zero;
773 sig->it_virt_incr = cputime_zero;
774 sig->it_prof_expires = cputime_zero;
775 sig->it_prof_incr = cputime_zero;
776
777 /* Cached expiration times. */
778 sig->cputime_expires.prof_exp = cputime_zero;
779 sig->cputime_expires.virt_exp = cputime_zero;
780 sig->cputime_expires.sched_exp = 0;
781
782 /* The timer lists. */
783 INIT_LIST_HEAD(&sig->cpu_timers[0]);
784 INIT_LIST_HEAD(&sig->cpu_timers[1]);
785 INIT_LIST_HEAD(&sig->cpu_timers[2]);
786}
787
762static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 788static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
763{ 789{
764 struct signal_struct *sig; 790 struct signal_struct *sig;
765 int ret; 791 int ret;
766 792
767 if (clone_flags & CLONE_THREAD) { 793 if (clone_flags & CLONE_THREAD) {
768 atomic_inc(&current->signal->count); 794 ret = thread_group_cputime_clone_thread(current);
769 atomic_inc(&current->signal->live); 795 if (likely(!ret)) {
770 return 0; 796 atomic_inc(&current->signal->count);
797 atomic_inc(&current->signal->live);
798 }
799 return ret;
771 } 800 }
772 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL); 801 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
773 tsk->signal = sig; 802 tsk->signal = sig;
@@ -795,39 +824,25 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
795 sig->it_real_incr.tv64 = 0; 824 sig->it_real_incr.tv64 = 0;
796 sig->real_timer.function = it_real_fn; 825 sig->real_timer.function = it_real_fn;
797 826
798 sig->it_virt_expires = cputime_zero;
799 sig->it_virt_incr = cputime_zero;
800 sig->it_prof_expires = cputime_zero;
801 sig->it_prof_incr = cputime_zero;
802
803 sig->leader = 0; /* session leadership doesn't inherit */ 827 sig->leader = 0; /* session leadership doesn't inherit */
804 sig->tty_old_pgrp = NULL; 828 sig->tty_old_pgrp = NULL;
829 sig->tty = NULL;
805 830
806 sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero; 831 sig->cutime = sig->cstime = cputime_zero;
807 sig->gtime = cputime_zero; 832 sig->gtime = cputime_zero;
808 sig->cgtime = cputime_zero; 833 sig->cgtime = cputime_zero;
809 sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0; 834 sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
810 sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0; 835 sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
811 sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0; 836 sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0;
812 task_io_accounting_init(&sig->ioac); 837 task_io_accounting_init(&sig->ioac);
813 sig->sum_sched_runtime = 0;
814 INIT_LIST_HEAD(&sig->cpu_timers[0]);
815 INIT_LIST_HEAD(&sig->cpu_timers[1]);
816 INIT_LIST_HEAD(&sig->cpu_timers[2]);
817 taskstats_tgid_init(sig); 838 taskstats_tgid_init(sig);
818 839
819 task_lock(current->group_leader); 840 task_lock(current->group_leader);
820 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 841 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
821 task_unlock(current->group_leader); 842 task_unlock(current->group_leader);
822 843
823 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 844 posix_cpu_timers_init_group(sig);
824 /* 845
825 * New sole thread in the process gets an expiry time
826 * of the whole CPU time limit.
827 */
828 tsk->it_prof_expires =
829 secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
830 }
831 acct_init_pacct(&sig->pacct); 846 acct_init_pacct(&sig->pacct);
832 847
833 tty_audit_fork(sig); 848 tty_audit_fork(sig);
@@ -837,7 +852,9 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
837 852
838void __cleanup_signal(struct signal_struct *sig) 853void __cleanup_signal(struct signal_struct *sig)
839{ 854{
855 thread_group_cputime_free(sig);
840 exit_thread_group_keys(sig); 856 exit_thread_group_keys(sig);
857 tty_kref_put(sig->tty);
841 kmem_cache_free(signal_cachep, sig); 858 kmem_cache_free(signal_cachep, sig);
842} 859}
843 860
@@ -886,6 +903,19 @@ void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
886#endif /* CONFIG_MM_OWNER */ 903#endif /* CONFIG_MM_OWNER */
887 904
888/* 905/*
906 * Initialize POSIX timer handling for a single task.
907 */
908static void posix_cpu_timers_init(struct task_struct *tsk)
909{
910 tsk->cputime_expires.prof_exp = cputime_zero;
911 tsk->cputime_expires.virt_exp = cputime_zero;
912 tsk->cputime_expires.sched_exp = 0;
913 INIT_LIST_HEAD(&tsk->cpu_timers[0]);
914 INIT_LIST_HEAD(&tsk->cpu_timers[1]);
915 INIT_LIST_HEAD(&tsk->cpu_timers[2]);
916}
917
918/*
889 * This creates a new process as a copy of the old one, 919 * This creates a new process as a copy of the old one,
890 * but does not actually start it yet. 920 * but does not actually start it yet.
891 * 921 *
@@ -995,12 +1025,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
995 task_io_accounting_init(&p->ioac); 1025 task_io_accounting_init(&p->ioac);
996 acct_clear_integrals(p); 1026 acct_clear_integrals(p);
997 1027
998 p->it_virt_expires = cputime_zero; 1028 posix_cpu_timers_init(p);
999 p->it_prof_expires = cputime_zero;
1000 p->it_sched_expires = 0;
1001 INIT_LIST_HEAD(&p->cpu_timers[0]);
1002 INIT_LIST_HEAD(&p->cpu_timers[1]);
1003 INIT_LIST_HEAD(&p->cpu_timers[2]);
1004 1029
1005 p->lock_depth = -1; /* -1 = no lock */ 1030 p->lock_depth = -1; /* -1 = no lock */
1006 do_posix_clock_monotonic_gettime(&p->start_time); 1031 do_posix_clock_monotonic_gettime(&p->start_time);
@@ -1201,21 +1226,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1201 if (clone_flags & CLONE_THREAD) { 1226 if (clone_flags & CLONE_THREAD) {
1202 p->group_leader = current->group_leader; 1227 p->group_leader = current->group_leader;
1203 list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group); 1228 list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
1204
1205 if (!cputime_eq(current->signal->it_virt_expires,
1206 cputime_zero) ||
1207 !cputime_eq(current->signal->it_prof_expires,
1208 cputime_zero) ||
1209 current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
1210 !list_empty(&current->signal->cpu_timers[0]) ||
1211 !list_empty(&current->signal->cpu_timers[1]) ||
1212 !list_empty(&current->signal->cpu_timers[2])) {
1213 /*
1214 * Have child wake up on its first tick to check
1215 * for process CPU timers.
1216 */
1217 p->it_prof_expires = jiffies_to_cputime(1);
1218 }
1219 } 1229 }
1220 1230
1221 if (likely(p->pid)) { 1231 if (likely(p->pid)) {
@@ -1227,7 +1237,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1227 p->nsproxy->pid_ns->child_reaper = p; 1237 p->nsproxy->pid_ns->child_reaper = p;
1228 1238
1229 p->signal->leader_pid = pid; 1239 p->signal->leader_pid = pid;
1230 p->signal->tty = current->signal->tty; 1240 tty_kref_put(p->signal->tty);
1241 p->signal->tty = tty_kref_get(current->signal->tty);
1231 set_task_pgrp(p, task_pgrp_nr(current)); 1242 set_task_pgrp(p, task_pgrp_nr(current));
1232 set_task_session(p, task_session_nr(current)); 1243 set_task_session(p, task_session_nr(current));
1233 attach_pid(p, PIDTYPE_PGID, task_pgrp(current)); 1244 attach_pid(p, PIDTYPE_PGID, task_pgrp(current));