aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/fork.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/fork.c')
-rw-r--r--kernel/fork.c119
1 files changed, 81 insertions, 38 deletions
diff --git a/kernel/fork.c b/kernel/fork.c
index 49adc0e8d47c..1c999f3e0b47 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -11,7 +11,6 @@
11 * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' 11 * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
12 */ 12 */
13 13
14#include <linux/config.h>
15#include <linux/slab.h> 14#include <linux/slab.h>
16#include <linux/init.h> 15#include <linux/init.h>
17#include <linux/unistd.h> 16#include <linux/unistd.h>
@@ -44,6 +43,9 @@
44#include <linux/rmap.h> 43#include <linux/rmap.h>
45#include <linux/acct.h> 44#include <linux/acct.h>
46#include <linux/cn_proc.h> 45#include <linux/cn_proc.h>
46#include <linux/delayacct.h>
47#include <linux/taskstats_kern.h>
48#include <linux/random.h>
47 49
48#include <asm/pgtable.h> 50#include <asm/pgtable.h>
49#include <asm/pgalloc.h> 51#include <asm/pgalloc.h>
@@ -62,9 +64,7 @@ int max_threads; /* tunable limit on nr_threads */
62 64
63DEFINE_PER_CPU(unsigned long, process_counts) = 0; 65DEFINE_PER_CPU(unsigned long, process_counts) = 0;
64 66
65 __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 67__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
66
67EXPORT_SYMBOL(tasklist_lock);
68 68
69int nr_processes(void) 69int nr_processes(void)
70{ 70{
@@ -104,6 +104,7 @@ static kmem_cache_t *mm_cachep;
104void free_task(struct task_struct *tsk) 104void free_task(struct task_struct *tsk)
105{ 105{
106 free_thread_info(tsk->thread_info); 106 free_thread_info(tsk->thread_info);
107 rt_mutex_debug_task_free(tsk);
107 free_task_struct(tsk); 108 free_task_struct(tsk);
108} 109}
109EXPORT_SYMBOL(free_task); 110EXPORT_SYMBOL(free_task);
@@ -117,6 +118,7 @@ void __put_task_struct(struct task_struct *tsk)
117 security_task_free(tsk); 118 security_task_free(tsk);
118 free_uid(tsk->user); 119 free_uid(tsk->user);
119 put_group_info(tsk->group_info); 120 put_group_info(tsk->group_info);
121 delayacct_tsk_free(tsk);
120 122
121 if (!profile_handoff_task(tsk)) 123 if (!profile_handoff_task(tsk))
122 free_task(tsk); 124 free_task(tsk);
@@ -174,10 +176,16 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
174 tsk->thread_info = ti; 176 tsk->thread_info = ti;
175 setup_thread_stack(tsk, orig); 177 setup_thread_stack(tsk, orig);
176 178
179#ifdef CONFIG_CC_STACKPROTECTOR
180 tsk->stack_canary = get_random_int();
181#endif
182
177 /* One for us, one for whoever does the "release_task()" (usually parent) */ 183 /* One for us, one for whoever does the "release_task()" (usually parent) */
178 atomic_set(&tsk->usage,2); 184 atomic_set(&tsk->usage,2);
179 atomic_set(&tsk->fs_excl, 0); 185 atomic_set(&tsk->fs_excl, 0);
186#ifdef CONFIG_BLK_DEV_IO_TRACE
180 tsk->btrace_seq = 0; 187 tsk->btrace_seq = 0;
188#endif
181 tsk->splice_pipe = NULL; 189 tsk->splice_pipe = NULL;
182 return tsk; 190 return tsk;
183} 191}
@@ -193,7 +201,10 @@ static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
193 201
194 down_write(&oldmm->mmap_sem); 202 down_write(&oldmm->mmap_sem);
195 flush_cache_mm(oldmm); 203 flush_cache_mm(oldmm);
196 down_write(&mm->mmap_sem); 204 /*
205 * Not linked in yet - no deadlock potential:
206 */
207 down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
197 208
198 mm->locked_vm = 0; 209 mm->locked_vm = 0;
199 mm->mmap = NULL; 210 mm->mmap = NULL;
@@ -817,6 +828,7 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
817 if (clone_flags & CLONE_THREAD) { 828 if (clone_flags & CLONE_THREAD) {
818 atomic_inc(&current->signal->count); 829 atomic_inc(&current->signal->count);
819 atomic_inc(&current->signal->live); 830 atomic_inc(&current->signal->live);
831 taskstats_tgid_alloc(current->signal);
820 return 0; 832 return 0;
821 } 833 }
822 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL); 834 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
@@ -861,6 +873,7 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
861 INIT_LIST_HEAD(&sig->cpu_timers[0]); 873 INIT_LIST_HEAD(&sig->cpu_timers[0]);
862 INIT_LIST_HEAD(&sig->cpu_timers[1]); 874 INIT_LIST_HEAD(&sig->cpu_timers[1]);
863 INIT_LIST_HEAD(&sig->cpu_timers[2]); 875 INIT_LIST_HEAD(&sig->cpu_timers[2]);
876 taskstats_tgid_init(sig);
864 877
865 task_lock(current->group_leader); 878 task_lock(current->group_leader);
866 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 879 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
@@ -874,6 +887,7 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
874 tsk->it_prof_expires = 887 tsk->it_prof_expires =
875 secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur); 888 secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
876 } 889 }
890 acct_init_pacct(&sig->pacct);
877 891
878 return 0; 892 return 0;
879} 893}
@@ -881,6 +895,7 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
881void __cleanup_signal(struct signal_struct *sig) 895void __cleanup_signal(struct signal_struct *sig)
882{ 896{
883 exit_thread_group_keys(sig); 897 exit_thread_group_keys(sig);
898 taskstats_tgid_free(sig);
884 kmem_cache_free(signal_cachep, sig); 899 kmem_cache_free(signal_cachep, sig);
885} 900}
886 901
@@ -912,6 +927,15 @@ asmlinkage long sys_set_tid_address(int __user *tidptr)
912 return current->pid; 927 return current->pid;
913} 928}
914 929
930static inline void rt_mutex_init_task(struct task_struct *p)
931{
932#ifdef CONFIG_RT_MUTEXES
933 spin_lock_init(&p->pi_lock);
934 plist_head_init(&p->pi_waiters, &p->pi_lock);
935 p->pi_blocked_on = NULL;
936#endif
937}
938
915/* 939/*
916 * This creates a new process as a copy of the old one, 940 * This creates a new process as a copy of the old one,
917 * but does not actually start it yet. 941 * but does not actually start it yet.
@@ -920,13 +944,13 @@ asmlinkage long sys_set_tid_address(int __user *tidptr)
920 * parts of the process environment (as per the clone 944 * parts of the process environment (as per the clone
921 * flags). The actual kick-off is left to the caller. 945 * flags). The actual kick-off is left to the caller.
922 */ 946 */
923static task_t *copy_process(unsigned long clone_flags, 947static struct task_struct *copy_process(unsigned long clone_flags,
924 unsigned long stack_start, 948 unsigned long stack_start,
925 struct pt_regs *regs, 949 struct pt_regs *regs,
926 unsigned long stack_size, 950 unsigned long stack_size,
927 int __user *parent_tidptr, 951 int __user *parent_tidptr,
928 int __user *child_tidptr, 952 int __user *child_tidptr,
929 int pid) 953 int pid)
930{ 954{
931 int retval; 955 int retval;
932 struct task_struct *p = NULL; 956 struct task_struct *p = NULL;
@@ -958,6 +982,10 @@ static task_t *copy_process(unsigned long clone_flags,
958 if (!p) 982 if (!p)
959 goto fork_out; 983 goto fork_out;
960 984
985#ifdef CONFIG_TRACE_IRQFLAGS
986 DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
987 DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
988#endif
961 retval = -EAGAIN; 989 retval = -EAGAIN;
962 if (atomic_read(&p->user->processes) >= 990 if (atomic_read(&p->user->processes) >=
963 p->signal->rlim[RLIMIT_NPROC].rlim_cur) { 991 p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
@@ -985,20 +1013,18 @@ static task_t *copy_process(unsigned long clone_flags,
985 goto bad_fork_cleanup_put_domain; 1013 goto bad_fork_cleanup_put_domain;
986 1014
987 p->did_exec = 0; 1015 p->did_exec = 0;
1016 delayacct_tsk_init(p); /* Must remain after dup_task_struct() */
988 copy_flags(clone_flags, p); 1017 copy_flags(clone_flags, p);
989 p->pid = pid; 1018 p->pid = pid;
990 retval = -EFAULT; 1019 retval = -EFAULT;
991 if (clone_flags & CLONE_PARENT_SETTID) 1020 if (clone_flags & CLONE_PARENT_SETTID)
992 if (put_user(p->pid, parent_tidptr)) 1021 if (put_user(p->pid, parent_tidptr))
993 goto bad_fork_cleanup; 1022 goto bad_fork_cleanup_delays_binfmt;
994
995 p->proc_dentry = NULL;
996 1023
997 INIT_LIST_HEAD(&p->children); 1024 INIT_LIST_HEAD(&p->children);
998 INIT_LIST_HEAD(&p->sibling); 1025 INIT_LIST_HEAD(&p->sibling);
999 p->vfork_done = NULL; 1026 p->vfork_done = NULL;
1000 spin_lock_init(&p->alloc_lock); 1027 spin_lock_init(&p->alloc_lock);
1001 spin_lock_init(&p->proc_lock);
1002 1028
1003 clear_tsk_thread_flag(p, TIF_SIGPENDING); 1029 clear_tsk_thread_flag(p, TIF_SIGPENDING);
1004 init_sigpending(&p->pending); 1030 init_sigpending(&p->pending);
@@ -1035,6 +1061,32 @@ static task_t *copy_process(unsigned long clone_flags,
1035 } 1061 }
1036 mpol_fix_fork_child_flag(p); 1062 mpol_fix_fork_child_flag(p);
1037#endif 1063#endif
1064#ifdef CONFIG_TRACE_IRQFLAGS
1065 p->irq_events = 0;
1066#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1067 p->hardirqs_enabled = 1;
1068#else
1069 p->hardirqs_enabled = 0;
1070#endif
1071 p->hardirq_enable_ip = 0;
1072 p->hardirq_enable_event = 0;
1073 p->hardirq_disable_ip = _THIS_IP_;
1074 p->hardirq_disable_event = 0;
1075 p->softirqs_enabled = 1;
1076 p->softirq_enable_ip = _THIS_IP_;
1077 p->softirq_enable_event = 0;
1078 p->softirq_disable_ip = 0;
1079 p->softirq_disable_event = 0;
1080 p->hardirq_context = 0;
1081 p->softirq_context = 0;
1082#endif
1083#ifdef CONFIG_LOCKDEP
1084 p->lockdep_depth = 0; /* no locks held yet */
1085 p->curr_chain_key = 0;
1086 p->lockdep_recursion = 0;
1087#endif
1088
1089 rt_mutex_init_task(p);
1038 1090
1039#ifdef CONFIG_DEBUG_MUTEXES 1091#ifdef CONFIG_DEBUG_MUTEXES
1040 p->blocked_on = NULL; /* not blocked yet */ 1092 p->blocked_on = NULL; /* not blocked yet */
@@ -1078,6 +1130,9 @@ static task_t *copy_process(unsigned long clone_flags,
1078#ifdef CONFIG_COMPAT 1130#ifdef CONFIG_COMPAT
1079 p->compat_robust_list = NULL; 1131 p->compat_robust_list = NULL;
1080#endif 1132#endif
1133 INIT_LIST_HEAD(&p->pi_state_list);
1134 p->pi_state_cache = NULL;
1135
1081 /* 1136 /*
1082 * sigaltstack should be cleared when sharing the same VM 1137 * sigaltstack should be cleared when sharing the same VM
1083 */ 1138 */
@@ -1095,7 +1150,6 @@ static task_t *copy_process(unsigned long clone_flags,
1095 1150
1096 /* Our parent execution domain becomes current domain 1151 /* Our parent execution domain becomes current domain
1097 These must match for thread signalling to apply */ 1152 These must match for thread signalling to apply */
1098
1099 p->parent_exec_id = p->self_exec_id; 1153 p->parent_exec_id = p->self_exec_id;
1100 1154
1101 /* ok, now we should be set up.. */ 1155 /* ok, now we should be set up.. */
@@ -1118,6 +1172,9 @@ static task_t *copy_process(unsigned long clone_flags,
1118 /* Need tasklist lock for parent etc handling! */ 1172 /* Need tasklist lock for parent etc handling! */
1119 write_lock_irq(&tasklist_lock); 1173 write_lock_irq(&tasklist_lock);
1120 1174
1175 /* for sys_ioprio_set(IOPRIO_WHO_PGRP) */
1176 p->ioprio = current->ioprio;
1177
1121 /* 1178 /*
1122 * The task hasn't been attached yet, so its cpus_allowed mask will 1179 * The task hasn't been attached yet, so its cpus_allowed mask will
1123 * not be changed, nor will its assigned CPU. 1180 * not be changed, nor will its assigned CPU.
@@ -1158,18 +1215,6 @@ static task_t *copy_process(unsigned long clone_flags,
1158 } 1215 }
1159 1216
1160 if (clone_flags & CLONE_THREAD) { 1217 if (clone_flags & CLONE_THREAD) {
1161 /*
1162 * Important: if an exit-all has been started then
1163 * do not create this new thread - the whole thread
1164 * group is supposed to exit anyway.
1165 */
1166 if (current->signal->flags & SIGNAL_GROUP_EXIT) {
1167 spin_unlock(&current->sighand->siglock);
1168 write_unlock_irq(&tasklist_lock);
1169 retval = -EAGAIN;
1170 goto bad_fork_cleanup_namespace;
1171 }
1172
1173 p->group_leader = current->group_leader; 1218 p->group_leader = current->group_leader;
1174 list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group); 1219 list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
1175 1220
@@ -1189,11 +1234,6 @@ static task_t *copy_process(unsigned long clone_flags,
1189 } 1234 }
1190 } 1235 }
1191 1236
1192 /*
1193 * inherit ioprio
1194 */
1195 p->ioprio = current->ioprio;
1196
1197 if (likely(p->pid)) { 1237 if (likely(p->pid)) {
1198 add_parent(p); 1238 add_parent(p);
1199 if (unlikely(p->ptrace & PT_PTRACED)) 1239 if (unlikely(p->ptrace & PT_PTRACED))
@@ -1246,7 +1286,8 @@ bad_fork_cleanup_policy:
1246bad_fork_cleanup_cpuset: 1286bad_fork_cleanup_cpuset:
1247#endif 1287#endif
1248 cpuset_exit(p); 1288 cpuset_exit(p);
1249bad_fork_cleanup: 1289bad_fork_cleanup_delays_binfmt:
1290 delayacct_tsk_free(p);
1250 if (p->binfmt) 1291 if (p->binfmt)
1251 module_put(p->binfmt->module); 1292 module_put(p->binfmt->module);
1252bad_fork_cleanup_put_domain: 1293bad_fork_cleanup_put_domain:
@@ -1267,9 +1308,9 @@ struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
1267 return regs; 1308 return regs;
1268} 1309}
1269 1310
1270task_t * __devinit fork_idle(int cpu) 1311struct task_struct * __devinit fork_idle(int cpu)
1271{ 1312{
1272 task_t *task; 1313 struct task_struct *task;
1273 struct pt_regs regs; 1314 struct pt_regs regs;
1274 1315
1275 task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0); 1316 task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0);
@@ -1356,8 +1397,10 @@ long do_fork(unsigned long clone_flags,
1356 1397
1357 if (clone_flags & CLONE_VFORK) { 1398 if (clone_flags & CLONE_VFORK) {
1358 wait_for_completion(&vfork); 1399 wait_for_completion(&vfork);
1359 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) 1400 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) {
1401 current->ptrace_message = nr;
1360 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP); 1402 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1403 }
1361 } 1404 }
1362 } else { 1405 } else {
1363 free_pid(pid); 1406 free_pid(pid);