aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/fork.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/fork.c')
-rw-r--r--kernel/fork.c111
1 files changed, 73 insertions, 38 deletions
diff --git a/kernel/fork.c b/kernel/fork.c
index 687a15d56243..ab5211b9e622 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;
@@ -356,7 +386,8 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
356 } 386 }
357 charge = 0; 387 charge = 0;
358 if (mpnt->vm_flags & VM_ACCOUNT) { 388 if (mpnt->vm_flags & VM_ACCOUNT) {
359 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT; 389 unsigned long len;
390 len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
360 if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 391 if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
361 goto fail_nomem; 392 goto fail_nomem;
362 charge = len; 393 charge = len;
@@ -422,6 +453,9 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
422 453
423 if (retval) 454 if (retval)
424 goto out; 455 goto out;
456
457 if (file && uprobe_mmap(tmp))
458 goto out;
425 } 459 }
426 /* a new mm has just been created */ 460 /* a new mm has just been created */
427 arch_dup_mmap(oldmm, mm); 461 arch_dup_mmap(oldmm, mm);
@@ -570,6 +604,7 @@ void mmput(struct mm_struct *mm)
570 might_sleep(); 604 might_sleep();
571 605
572 if (atomic_dec_and_test(&mm->mm_users)) { 606 if (atomic_dec_and_test(&mm->mm_users)) {
607 uprobe_clear_state(mm);
573 exit_aio(mm); 608 exit_aio(mm);
574 ksm_exit(mm); 609 ksm_exit(mm);
575 khugepaged_exit(mm); /* must run before exit_mmap */ 610 khugepaged_exit(mm); /* must run before exit_mmap */
@@ -580,7 +615,6 @@ void mmput(struct mm_struct *mm)
580 list_del(&mm->mmlist); 615 list_del(&mm->mmlist);
581 spin_unlock(&mmlist_lock); 616 spin_unlock(&mmlist_lock);
582 } 617 }
583 put_swap_token(mm);
584 if (mm->binfmt) 618 if (mm->binfmt)
585 module_put(mm->binfmt->module); 619 module_put(mm->binfmt->module);
586 mmdrop(mm); 620 mmdrop(mm);
@@ -748,12 +782,11 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm)
748 exit_pi_state_list(tsk); 782 exit_pi_state_list(tsk);
749#endif 783#endif
750 784
785 uprobe_free_utask(tsk);
786
751 /* Get rid of any cached register state */ 787 /* Get rid of any cached register state */
752 deactivate_mm(tsk, mm); 788 deactivate_mm(tsk, mm);
753 789
754 if (tsk->vfork_done)
755 complete_vfork_done(tsk);
756
757 /* 790 /*
758 * If we're exiting normally, clear a user-space tid field if 791 * If we're exiting normally, clear a user-space tid field if
759 * requested. We leave this alone when dying by signal, to leave 792 * requested. We leave this alone when dying by signal, to leave
@@ -774,6 +807,13 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm)
774 } 807 }
775 tsk->clear_child_tid = NULL; 808 tsk->clear_child_tid = NULL;
776 } 809 }
810
811 /*
812 * All done, finally we can wake up parent and return this mm to him.
813 * Also kthread_stop() uses this completion for synchronization.
814 */
815 if (tsk->vfork_done)
816 complete_vfork_done(tsk);
777} 817}
778 818
779/* 819/*
@@ -795,13 +835,10 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
795 memcpy(mm, oldmm, sizeof(*mm)); 835 memcpy(mm, oldmm, sizeof(*mm));
796 mm_init_cpumask(mm); 836 mm_init_cpumask(mm);
797 837
798 /* Initializing for Swap token stuff */
799 mm->token_priority = 0;
800 mm->last_interval = 0;
801
802#ifdef CONFIG_TRANSPARENT_HUGEPAGE 838#ifdef CONFIG_TRANSPARENT_HUGEPAGE
803 mm->pmd_huge_pte = NULL; 839 mm->pmd_huge_pte = NULL;
804#endif 840#endif
841 uprobe_reset_state(mm);
805 842
806 if (!mm_init(mm, tsk)) 843 if (!mm_init(mm, tsk))
807 goto fail_nomem; 844 goto fail_nomem;
@@ -876,10 +913,6 @@ static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
876 goto fail_nomem; 913 goto fail_nomem;
877 914
878good_mm: 915good_mm:
879 /* Initializing for Swap token stuff */
880 mm->token_priority = 0;
881 mm->last_interval = 0;
882
883 tsk->mm = mm; 916 tsk->mm = mm;
884 tsk->active_mm = mm; 917 tsk->active_mm = mm;
885 return 0; 918 return 0;
@@ -947,9 +980,8 @@ static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
947 * Share io context with parent, if CLONE_IO is set 980 * Share io context with parent, if CLONE_IO is set
948 */ 981 */
949 if (clone_flags & CLONE_IO) { 982 if (clone_flags & CLONE_IO) {
950 tsk->io_context = ioc_task_link(ioc); 983 ioc_task_link(ioc);
951 if (unlikely(!tsk->io_context)) 984 tsk->io_context = ioc;
952 return -ENOMEM;
953 } else if (ioprio_valid(ioc->ioprio)) { 985 } else if (ioprio_valid(ioc->ioprio)) {
954 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 986 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
955 if (unlikely(!new_ioc)) 987 if (unlikely(!new_ioc))
@@ -1163,6 +1195,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1163 goto fork_out; 1195 goto fork_out;
1164 1196
1165 ftrace_graph_init_task(p); 1197 ftrace_graph_init_task(p);
1198 get_seccomp_filter(p);
1166 1199
1167 rt_mutex_init_task(p); 1200 rt_mutex_init_task(p);
1168 1201
@@ -1343,6 +1376,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1343 INIT_LIST_HEAD(&p->pi_state_list); 1376 INIT_LIST_HEAD(&p->pi_state_list);
1344 p->pi_state_cache = NULL; 1377 p->pi_state_cache = NULL;
1345#endif 1378#endif
1379 uprobe_copy_process(p);
1346 /* 1380 /*
1347 * sigaltstack should be cleared when sharing the same VM 1381 * sigaltstack should be cleared when sharing the same VM
1348 */ 1382 */
@@ -1381,6 +1415,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1381 */ 1415 */
1382 p->group_leader = p; 1416 p->group_leader = p;
1383 INIT_LIST_HEAD(&p->thread_group); 1417 INIT_LIST_HEAD(&p->thread_group);
1418 INIT_HLIST_HEAD(&p->task_works);
1384 1419
1385 /* Now that the task is set up, run cgroup callbacks if 1420 /* Now that the task is set up, run cgroup callbacks if
1386 * necessary. We need to run them before the task is visible 1421 * necessary. We need to run them before the task is visible