aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/fork.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/fork.c')
-rw-r--r--kernel/fork.c122
1 files changed, 81 insertions, 41 deletions
diff --git a/kernel/fork.c b/kernel/fork.c
index 687a15d56243..f00e319d8376 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -34,6 +34,7 @@
34#include <linux/cgroup.h> 34#include <linux/cgroup.h>
35#include <linux/security.h> 35#include <linux/security.h>
36#include <linux/hugetlb.h> 36#include <linux/hugetlb.h>
37#include <linux/seccomp.h>
37#include <linux/swap.h> 38#include <linux/swap.h>
38#include <linux/syscalls.h> 39#include <linux/syscalls.h>
39#include <linux/jiffies.h> 40#include <linux/jiffies.h>
@@ -68,6 +69,7 @@
68#include <linux/oom.h> 69#include <linux/oom.h>
69#include <linux/khugepaged.h> 70#include <linux/khugepaged.h>
70#include <linux/signalfd.h> 71#include <linux/signalfd.h>
72#include <linux/uprobes.h>
71 73
72#include <asm/pgtable.h> 74#include <asm/pgtable.h>
73#include <asm/pgalloc.h> 75#include <asm/pgalloc.h>
@@ -112,32 +114,67 @@ int nr_processes(void)
112 return total; 114 return total;
113} 115}
114 116
115#ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 117#ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
116# define alloc_task_struct_node(node) \
117 kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node)
118# define free_task_struct(tsk) \
119 kmem_cache_free(task_struct_cachep, (tsk))
120static struct kmem_cache *task_struct_cachep; 118static struct kmem_cache *task_struct_cachep;
119
120static inline struct task_struct *alloc_task_struct_node(int node)
121{
122 return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node);
123}
124
125void __weak arch_release_task_struct(struct task_struct *tsk) { }
126
127static inline void free_task_struct(struct task_struct *tsk)
128{
129 arch_release_task_struct(tsk);
130 kmem_cache_free(task_struct_cachep, tsk);
131}
121#endif 132#endif
122 133
123#ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR 134#ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR
135void __weak arch_release_thread_info(struct thread_info *ti) { }
136
137/*
138 * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
139 * kmemcache based allocator.
140 */
141# if THREAD_SIZE >= PAGE_SIZE
124static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 142static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
125 int node) 143 int node)
126{ 144{
127#ifdef CONFIG_DEBUG_STACK_USAGE 145 struct page *page = alloc_pages_node(node, THREADINFO_GFP,
128 gfp_t mask = GFP_KERNEL | __GFP_ZERO; 146 THREAD_SIZE_ORDER);
129#else
130 gfp_t mask = GFP_KERNEL;
131#endif
132 struct page *page = alloc_pages_node(node, mask, THREAD_SIZE_ORDER);
133 147
134 return page ? page_address(page) : NULL; 148 return page ? page_address(page) : NULL;
135} 149}
136 150
137static inline void free_thread_info(struct thread_info *ti) 151static inline void free_thread_info(struct thread_info *ti)
138{ 152{
153 arch_release_thread_info(ti);
139 free_pages((unsigned long)ti, THREAD_SIZE_ORDER); 154 free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
140} 155}
156# else
157static struct kmem_cache *thread_info_cache;
158
159static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
160 int node)
161{
162 return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node);
163}
164
165static void free_thread_info(struct thread_info *ti)
166{
167 arch_release_thread_info(ti);
168 kmem_cache_free(thread_info_cache, ti);
169}
170
171void thread_info_cache_init(void)
172{
173 thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
174 THREAD_SIZE, 0, NULL);
175 BUG_ON(thread_info_cache == NULL);
176}
177# endif
141#endif 178#endif
142 179
143/* SLAB cache for signal_struct structures (tsk->signal) */ 180/* SLAB cache for signal_struct structures (tsk->signal) */
@@ -171,6 +208,7 @@ void free_task(struct task_struct *tsk)
171 free_thread_info(tsk->stack); 208 free_thread_info(tsk->stack);
172 rt_mutex_debug_task_free(tsk); 209 rt_mutex_debug_task_free(tsk);
173 ftrace_graph_exit_task(tsk); 210 ftrace_graph_exit_task(tsk);
211 put_seccomp_filter(tsk);
174 free_task_struct(tsk); 212 free_task_struct(tsk);
175} 213}
176EXPORT_SYMBOL(free_task); 214EXPORT_SYMBOL(free_task);
@@ -204,17 +242,11 @@ void __put_task_struct(struct task_struct *tsk)
204} 242}
205EXPORT_SYMBOL_GPL(__put_task_struct); 243EXPORT_SYMBOL_GPL(__put_task_struct);
206 244
207/* 245void __init __weak arch_task_cache_init(void) { }
208 * macro override instead of weak attribute alias, to workaround
209 * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
210 */
211#ifndef arch_task_cache_init
212#define arch_task_cache_init()
213#endif
214 246
215void __init fork_init(unsigned long mempages) 247void __init fork_init(unsigned long mempages)
216{ 248{
217#ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 249#ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
218#ifndef ARCH_MIN_TASKALIGN 250#ifndef ARCH_MIN_TASKALIGN
219#define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 251#define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
220#endif 252#endif
@@ -261,8 +293,6 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
261 int node = tsk_fork_get_node(orig); 293 int node = tsk_fork_get_node(orig);
262 int err; 294 int err;
263 295
264 prepare_to_copy(orig);
265
266 tsk = alloc_task_struct_node(node); 296 tsk = alloc_task_struct_node(node);
267 if (!tsk) 297 if (!tsk)
268 return NULL; 298 return NULL;
@@ -274,12 +304,17 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
274 } 304 }
275 305
276 err = arch_dup_task_struct(tsk, orig); 306 err = arch_dup_task_struct(tsk, orig);
277 if (err)
278 goto out;
279 307
308 /*
309 * We defer looking at err, because we will need this setup
310 * for the clean up path to work correctly.
311 */
280 tsk->stack = ti; 312 tsk->stack = ti;
281
282 setup_thread_stack(tsk, orig); 313 setup_thread_stack(tsk, orig);
314
315 if (err)
316 goto out;
317
283 clear_user_return_notifier(tsk); 318 clear_user_return_notifier(tsk);
284 clear_tsk_need_resched(tsk); 319 clear_tsk_need_resched(tsk);
285 stackend = end_of_stack(tsk); 320 stackend = end_of_stack(tsk);
@@ -356,7 +391,8 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
356 } 391 }
357 charge = 0; 392 charge = 0;
358 if (mpnt->vm_flags & VM_ACCOUNT) { 393 if (mpnt->vm_flags & VM_ACCOUNT) {
359 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT; 394 unsigned long len;
395 len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
360 if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 396 if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
361 goto fail_nomem; 397 goto fail_nomem;
362 charge = len; 398 charge = len;
@@ -422,6 +458,9 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
422 458
423 if (retval) 459 if (retval)
424 goto out; 460 goto out;
461
462 if (file && uprobe_mmap(tmp))
463 goto out;
425 } 464 }
426 /* a new mm has just been created */ 465 /* a new mm has just been created */
427 arch_dup_mmap(oldmm, mm); 466 arch_dup_mmap(oldmm, mm);
@@ -570,6 +609,7 @@ void mmput(struct mm_struct *mm)
570 might_sleep(); 609 might_sleep();
571 610
572 if (atomic_dec_and_test(&mm->mm_users)) { 611 if (atomic_dec_and_test(&mm->mm_users)) {
612 uprobe_clear_state(mm);
573 exit_aio(mm); 613 exit_aio(mm);
574 ksm_exit(mm); 614 ksm_exit(mm);
575 khugepaged_exit(mm); /* must run before exit_mmap */ 615 khugepaged_exit(mm); /* must run before exit_mmap */
@@ -580,7 +620,6 @@ void mmput(struct mm_struct *mm)
580 list_del(&mm->mmlist); 620 list_del(&mm->mmlist);
581 spin_unlock(&mmlist_lock); 621 spin_unlock(&mmlist_lock);
582 } 622 }
583 put_swap_token(mm);
584 if (mm->binfmt) 623 if (mm->binfmt)
585 module_put(mm->binfmt->module); 624 module_put(mm->binfmt->module);
586 mmdrop(mm); 625 mmdrop(mm);
@@ -748,12 +787,11 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm)
748 exit_pi_state_list(tsk); 787 exit_pi_state_list(tsk);
749#endif 788#endif
750 789
790 uprobe_free_utask(tsk);
791
751 /* Get rid of any cached register state */ 792 /* Get rid of any cached register state */
752 deactivate_mm(tsk, mm); 793 deactivate_mm(tsk, mm);
753 794
754 if (tsk->vfork_done)
755 complete_vfork_done(tsk);
756
757 /* 795 /*
758 * If we're exiting normally, clear a user-space tid field if 796 * If we're exiting normally, clear a user-space tid field if
759 * requested. We leave this alone when dying by signal, to leave 797 * requested. We leave this alone when dying by signal, to leave
@@ -774,6 +812,13 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm)
774 } 812 }
775 tsk->clear_child_tid = NULL; 813 tsk->clear_child_tid = NULL;
776 } 814 }
815
816 /*
817 * All done, finally we can wake up parent and return this mm to him.
818 * Also kthread_stop() uses this completion for synchronization.
819 */
820 if (tsk->vfork_done)
821 complete_vfork_done(tsk);
777} 822}
778 823
779/* 824/*
@@ -795,13 +840,10 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
795 memcpy(mm, oldmm, sizeof(*mm)); 840 memcpy(mm, oldmm, sizeof(*mm));
796 mm_init_cpumask(mm); 841 mm_init_cpumask(mm);
797 842
798 /* Initializing for Swap token stuff */
799 mm->token_priority = 0;
800 mm->last_interval = 0;
801
802#ifdef CONFIG_TRANSPARENT_HUGEPAGE 843#ifdef CONFIG_TRANSPARENT_HUGEPAGE
803 mm->pmd_huge_pte = NULL; 844 mm->pmd_huge_pte = NULL;
804#endif 845#endif
846 uprobe_reset_state(mm);
805 847
806 if (!mm_init(mm, tsk)) 848 if (!mm_init(mm, tsk))
807 goto fail_nomem; 849 goto fail_nomem;
@@ -876,10 +918,6 @@ static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
876 goto fail_nomem; 918 goto fail_nomem;
877 919
878good_mm: 920good_mm:
879 /* Initializing for Swap token stuff */
880 mm->token_priority = 0;
881 mm->last_interval = 0;
882
883 tsk->mm = mm; 921 tsk->mm = mm;
884 tsk->active_mm = mm; 922 tsk->active_mm = mm;
885 return 0; 923 return 0;
@@ -947,9 +985,8 @@ static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
947 * Share io context with parent, if CLONE_IO is set 985 * Share io context with parent, if CLONE_IO is set
948 */ 986 */
949 if (clone_flags & CLONE_IO) { 987 if (clone_flags & CLONE_IO) {
950 tsk->io_context = ioc_task_link(ioc); 988 ioc_task_link(ioc);
951 if (unlikely(!tsk->io_context)) 989 tsk->io_context = ioc;
952 return -ENOMEM;
953 } else if (ioprio_valid(ioc->ioprio)) { 990 } else if (ioprio_valid(ioc->ioprio)) {
954 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 991 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
955 if (unlikely(!new_ioc)) 992 if (unlikely(!new_ioc))
@@ -1163,6 +1200,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1163 goto fork_out; 1200 goto fork_out;
1164 1201
1165 ftrace_graph_init_task(p); 1202 ftrace_graph_init_task(p);
1203 get_seccomp_filter(p);
1166 1204
1167 rt_mutex_init_task(p); 1205 rt_mutex_init_task(p);
1168 1206
@@ -1343,6 +1381,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1343 INIT_LIST_HEAD(&p->pi_state_list); 1381 INIT_LIST_HEAD(&p->pi_state_list);
1344 p->pi_state_cache = NULL; 1382 p->pi_state_cache = NULL;
1345#endif 1383#endif
1384 uprobe_copy_process(p);
1346 /* 1385 /*
1347 * sigaltstack should be cleared when sharing the same VM 1386 * sigaltstack should be cleared when sharing the same VM
1348 */ 1387 */
@@ -1381,6 +1420,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1381 */ 1420 */
1382 p->group_leader = p; 1421 p->group_leader = p;
1383 INIT_LIST_HEAD(&p->thread_group); 1422 INIT_LIST_HEAD(&p->thread_group);
1423 INIT_HLIST_HEAD(&p->task_works);
1384 1424
1385 /* Now that the task is set up, run cgroup callbacks if 1425 /* Now that the task is set up, run cgroup callbacks if
1386 * necessary. We need to run them before the task is visible 1426 * necessary. We need to run them before the task is visible