xref: /openbmc/linux/kernel/fork.c (revision 12580e4b54ba8a1b22ec977c200be0174ca42348)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/kernel/fork.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  *  'fork.c' contains the help-routines for the 'fork' system call
91da177e4SLinus Torvalds  * (see also entry.S and others).
101da177e4SLinus Torvalds  * Fork is rather simple, once you get the hang of it, but the memory
111da177e4SLinus Torvalds  * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/unistd.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/vmalloc.h>
191da177e4SLinus Torvalds #include <linux/completion.h>
201da177e4SLinus Torvalds #include <linux/personality.h>
211da177e4SLinus Torvalds #include <linux/mempolicy.h>
221da177e4SLinus Torvalds #include <linux/sem.h>
231da177e4SLinus Torvalds #include <linux/file.h>
249f3acc31SAl Viro #include <linux/fdtable.h>
25da9cbc87SJens Axboe #include <linux/iocontext.h>
261da177e4SLinus Torvalds #include <linux/key.h>
271da177e4SLinus Torvalds #include <linux/binfmts.h>
281da177e4SLinus Torvalds #include <linux/mman.h>
29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
301da177e4SLinus Torvalds #include <linux/fs.h>
31615d6e87SDavidlohr Bueso #include <linux/mm.h>
32615d6e87SDavidlohr Bueso #include <linux/vmacache.h>
33ab516013SSerge E. Hallyn #include <linux/nsproxy.h>
34c59ede7bSRandy.Dunlap #include <linux/capability.h>
351da177e4SLinus Torvalds #include <linux/cpu.h>
36b4f48b63SPaul Menage #include <linux/cgroup.h>
371da177e4SLinus Torvalds #include <linux/security.h>
38a1e78772SMel Gorman #include <linux/hugetlb.h>
39e2cfabdfSWill Drewry #include <linux/seccomp.h>
401da177e4SLinus Torvalds #include <linux/swap.h>
411da177e4SLinus Torvalds #include <linux/syscalls.h>
421da177e4SLinus Torvalds #include <linux/jiffies.h>
431da177e4SLinus Torvalds #include <linux/futex.h>
448141c7f3SLinus Torvalds #include <linux/compat.h>
45207205a2SEric Dumazet #include <linux/kthread.h>
467c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h>
47ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
481da177e4SLinus Torvalds #include <linux/ptrace.h>
491da177e4SLinus Torvalds #include <linux/mount.h>
501da177e4SLinus Torvalds #include <linux/audit.h>
5178fb7466SPavel Emelianov #include <linux/memcontrol.h>
52f201ae23SFrederic Weisbecker #include <linux/ftrace.h>
535e2bf014SMike Galbraith #include <linux/proc_fs.h>
541da177e4SLinus Torvalds #include <linux/profile.h>
551da177e4SLinus Torvalds #include <linux/rmap.h>
56f8af4da3SHugh Dickins #include <linux/ksm.h>
571da177e4SLinus Torvalds #include <linux/acct.h>
588f0ab514SJay Lan #include <linux/tsacct_kern.h>
599f46080cSMatt Helsley #include <linux/cn_proc.h>
60ba96a0c8SRafael J. Wysocki #include <linux/freezer.h>
61ca74e92bSShailabh Nagar #include <linux/delayacct.h>
62ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h>
630a425405SArjan van de Ven #include <linux/random.h>
64522ed776SMiloslav Trmac #include <linux/tty.h>
65fd0928dfSJens Axboe #include <linux/blkdev.h>
665ad4e53bSAl Viro #include <linux/fs_struct.h>
677c9f8861SEric Sandeen #include <linux/magic.h>
68cdd6c482SIngo Molnar #include <linux/perf_event.h>
6942c4ab41SStanislaw Gruszka #include <linux/posix-timers.h>
708e7cac79SAvi Kivity #include <linux/user-return-notifier.h>
713d5992d2SYing Han #include <linux/oom.h>
72ba76149fSAndrea Arcangeli #include <linux/khugepaged.h>
73d80e731eSOleg Nesterov #include <linux/signalfd.h>
740326f5a9SSrikar Dronamraju #include <linux/uprobes.h>
75a27bb332SKent Overstreet #include <linux/aio.h>
7652f5684cSGideon Israel Dsouza #include <linux/compiler.h>
7716db3d3fSHeinrich Schuchardt #include <linux/sysctl.h>
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #include <asm/pgtable.h>
801da177e4SLinus Torvalds #include <asm/pgalloc.h>
811da177e4SLinus Torvalds #include <asm/uaccess.h>
821da177e4SLinus Torvalds #include <asm/mmu_context.h>
831da177e4SLinus Torvalds #include <asm/cacheflush.h>
841da177e4SLinus Torvalds #include <asm/tlbflush.h>
851da177e4SLinus Torvalds 
86ad8d75ffSSteven Rostedt #include <trace/events/sched.h>
87ad8d75ffSSteven Rostedt 
8843d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS
8943d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h>
9043d2b113SKAMEZAWA Hiroyuki 
911da177e4SLinus Torvalds /*
92ac1b398dSHeinrich Schuchardt  * Minimum number of threads to boot the kernel
93ac1b398dSHeinrich Schuchardt  */
94ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20
95ac1b398dSHeinrich Schuchardt 
96ac1b398dSHeinrich Schuchardt /*
97ac1b398dSHeinrich Schuchardt  * Maximum number of threads
98ac1b398dSHeinrich Schuchardt  */
99ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK
100ac1b398dSHeinrich Schuchardt 
101ac1b398dSHeinrich Schuchardt /*
1021da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
1051da177e4SLinus Torvalds int nr_threads;			/* The idle threads do not count.. */
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
112db1466b3SPaul E. McKenney 
113db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU
114db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void)
115db1466b3SPaul E. McKenney {
116db1466b3SPaul E. McKenney 	return lockdep_is_held(&tasklist_lock);
117db1466b3SPaul E. McKenney }
118db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
119db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds int nr_processes(void)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	int cpu;
1241da177e4SLinus Torvalds 	int total = 0;
1251da177e4SLinus Torvalds 
1261d510750SIan Campbell 	for_each_possible_cpu(cpu)
1271da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	return total;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
132f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk)
133f19b9f74SAkinobu Mita {
134f19b9f74SAkinobu Mita }
135f19b9f74SAkinobu Mita 
136f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
137e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep;
13841101809SThomas Gleixner 
13941101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node)
14041101809SThomas Gleixner {
14141101809SThomas Gleixner 	return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node);
14241101809SThomas Gleixner }
14341101809SThomas Gleixner 
14441101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk)
14541101809SThomas Gleixner {
14641101809SThomas Gleixner 	kmem_cache_free(task_struct_cachep, tsk);
14741101809SThomas Gleixner }
1481da177e4SLinus Torvalds #endif
1491da177e4SLinus Torvalds 
150f19b9f74SAkinobu Mita void __weak arch_release_thread_info(struct thread_info *ti)
151f19b9f74SAkinobu Mita {
152f19b9f74SAkinobu Mita }
153f19b9f74SAkinobu Mita 
154f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR
15541101809SThomas Gleixner 
1560d15d74aSThomas Gleixner /*
1570d15d74aSThomas Gleixner  * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
1580d15d74aSThomas Gleixner  * kmemcache based allocator.
1590d15d74aSThomas Gleixner  */
1600d15d74aSThomas Gleixner # if THREAD_SIZE >= PAGE_SIZE
161b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
162b6a84016SEric Dumazet 						  int node)
163b69c49b7SFUJITA Tomonori {
16452383431SVladimir Davydov 	struct page *page = alloc_kmem_pages_node(node, THREADINFO_GFP,
1652889f608SThomas Gleixner 						  THREAD_SIZE_ORDER);
166b6a84016SEric Dumazet 
167*12580e4bSVladimir Davydov 	if (page)
168*12580e4bSVladimir Davydov 		memcg_kmem_update_page_stat(page, MEMCG_KERNEL_STACK,
169*12580e4bSVladimir Davydov 					    1 << THREAD_SIZE_ORDER);
170*12580e4bSVladimir Davydov 
171b6a84016SEric Dumazet 	return page ? page_address(page) : NULL;
172b69c49b7SFUJITA Tomonori }
173b69c49b7SFUJITA Tomonori 
174b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti)
175b69c49b7SFUJITA Tomonori {
176*12580e4bSVladimir Davydov 	struct page *page = virt_to_page(ti);
177*12580e4bSVladimir Davydov 
178*12580e4bSVladimir Davydov 	memcg_kmem_update_page_stat(page, MEMCG_KERNEL_STACK,
179*12580e4bSVladimir Davydov 				    -(1 << THREAD_SIZE_ORDER));
180*12580e4bSVladimir Davydov 	__free_kmem_pages(page, THREAD_SIZE_ORDER);
181b69c49b7SFUJITA Tomonori }
1820d15d74aSThomas Gleixner # else
1830d15d74aSThomas Gleixner static struct kmem_cache *thread_info_cache;
1840d15d74aSThomas Gleixner 
1850d15d74aSThomas Gleixner static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
1860d15d74aSThomas Gleixner 						  int node)
1870d15d74aSThomas Gleixner {
1880d15d74aSThomas Gleixner 	return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node);
1890d15d74aSThomas Gleixner }
1900d15d74aSThomas Gleixner 
1910d15d74aSThomas Gleixner static void free_thread_info(struct thread_info *ti)
1920d15d74aSThomas Gleixner {
1930d15d74aSThomas Gleixner 	kmem_cache_free(thread_info_cache, ti);
1940d15d74aSThomas Gleixner }
1950d15d74aSThomas Gleixner 
1960d15d74aSThomas Gleixner void thread_info_cache_init(void)
1970d15d74aSThomas Gleixner {
1980d15d74aSThomas Gleixner 	thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
1990d15d74aSThomas Gleixner 					      THREAD_SIZE, 0, NULL);
2000d15d74aSThomas Gleixner 	BUG_ON(thread_info_cache == NULL);
2010d15d74aSThomas Gleixner }
2020d15d74aSThomas Gleixner # endif
203b69c49b7SFUJITA Tomonori #endif
204b69c49b7SFUJITA Tomonori 
2051da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
206e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
209e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
212e18b890bSChristoph Lameter struct kmem_cache *files_cachep;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
215e18b890bSChristoph Lameter struct kmem_cache *fs_cachep;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
218e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
221e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep;
2221da177e4SLinus Torvalds 
223c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account)
224c6a7f572SKOSAKI Motohiro {
225c6a7f572SKOSAKI Motohiro 	struct zone *zone = page_zone(virt_to_page(ti));
226c6a7f572SKOSAKI Motohiro 
227c6a7f572SKOSAKI Motohiro 	mod_zone_page_state(zone, NR_KERNEL_STACK, account);
228c6a7f572SKOSAKI Motohiro }
229c6a7f572SKOSAKI Motohiro 
2301da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
2311da177e4SLinus Torvalds {
232c6a7f572SKOSAKI Motohiro 	account_kernel_stack(tsk->stack, -1);
233f19b9f74SAkinobu Mita 	arch_release_thread_info(tsk->stack);
234f7e4217bSRoman Zippel 	free_thread_info(tsk->stack);
23523f78d4aSIngo Molnar 	rt_mutex_debug_task_free(tsk);
236fb52607aSFrederic Weisbecker 	ftrace_graph_exit_task(tsk);
237e2cfabdfSWill Drewry 	put_seccomp_filter(tsk);
238f19b9f74SAkinobu Mita 	arch_release_task_struct(tsk);
2391da177e4SLinus Torvalds 	free_task_struct(tsk);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
2421da177e4SLinus Torvalds 
243ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig)
244ea6d290cSOleg Nesterov {
24597101eb4SOleg Nesterov 	taskstats_tgid_free(sig);
2461c5354deSMike Galbraith 	sched_autogroup_exit(sig);
247ea6d290cSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
248ea6d290cSOleg Nesterov }
249ea6d290cSOleg Nesterov 
250ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig)
251ea6d290cSOleg Nesterov {
2521c5354deSMike Galbraith 	if (atomic_dec_and_test(&sig->sigcnt))
253ea6d290cSOleg Nesterov 		free_signal_struct(sig);
254ea6d290cSOleg Nesterov }
255ea6d290cSOleg Nesterov 
256158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
2571da177e4SLinus Torvalds {
258270f722dSEugene Teo 	WARN_ON(!tsk->exit_state);
2591da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
2601da177e4SLinus Torvalds 	WARN_ON(tsk == current);
2611da177e4SLinus Torvalds 
2622e91fa7fSTejun Heo 	cgroup_free(tsk);
263156654f4SMike Galbraith 	task_numa_free(tsk);
2641a2a4d06SKees Cook 	security_task_free(tsk);
265e0e81739SDavid Howells 	exit_creds(tsk);
26635df17c5SShailabh Nagar 	delayacct_tsk_free(tsk);
267ea6d290cSOleg Nesterov 	put_signal_struct(tsk->signal);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
2701da177e4SLinus Torvalds 		free_task(tsk);
2711da177e4SLinus Torvalds }
27277c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct);
2731da177e4SLinus Torvalds 
2746c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { }
27561c4628bSSuresh Siddha 
276ff691f6eSHeinrich Schuchardt /*
277ff691f6eSHeinrich Schuchardt  * set_max_threads
278ff691f6eSHeinrich Schuchardt  */
27916db3d3fSHeinrich Schuchardt static void set_max_threads(unsigned int max_threads_suggested)
280ff691f6eSHeinrich Schuchardt {
281ac1b398dSHeinrich Schuchardt 	u64 threads;
282ff691f6eSHeinrich Schuchardt 
283ff691f6eSHeinrich Schuchardt 	/*
284ac1b398dSHeinrich Schuchardt 	 * The number of threads shall be limited such that the thread
285ac1b398dSHeinrich Schuchardt 	 * structures may only consume a small part of the available memory.
286ff691f6eSHeinrich Schuchardt 	 */
287ac1b398dSHeinrich Schuchardt 	if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64)
288ac1b398dSHeinrich Schuchardt 		threads = MAX_THREADS;
289ac1b398dSHeinrich Schuchardt 	else
290ac1b398dSHeinrich Schuchardt 		threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
291ac1b398dSHeinrich Schuchardt 				    (u64) THREAD_SIZE * 8UL);
292ac1b398dSHeinrich Schuchardt 
29316db3d3fSHeinrich Schuchardt 	if (threads > max_threads_suggested)
29416db3d3fSHeinrich Schuchardt 		threads = max_threads_suggested;
29516db3d3fSHeinrich Schuchardt 
296ac1b398dSHeinrich Schuchardt 	max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS);
297ff691f6eSHeinrich Schuchardt }
298ff691f6eSHeinrich Schuchardt 
2995aaeb5c0SIngo Molnar #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
3005aaeb5c0SIngo Molnar /* Initialized by the architecture: */
3015aaeb5c0SIngo Molnar int arch_task_struct_size __read_mostly;
3025aaeb5c0SIngo Molnar #endif
3030c8c0f03SDave Hansen 
304ff691f6eSHeinrich Schuchardt void __init fork_init(void)
3051da177e4SLinus Torvalds {
306f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
3071da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
3081da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
3091da177e4SLinus Torvalds #endif
3101da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
3115d097056SVladimir Davydov 	task_struct_cachep = kmem_cache_create("task_struct",
3125d097056SVladimir Davydov 			arch_task_struct_size, ARCH_MIN_TASKALIGN,
3135d097056SVladimir Davydov 			SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, NULL);
3141da177e4SLinus Torvalds #endif
3151da177e4SLinus Torvalds 
31661c4628bSSuresh Siddha 	/* do the arch specific task caches init */
31761c4628bSSuresh Siddha 	arch_task_cache_init();
31861c4628bSSuresh Siddha 
31916db3d3fSHeinrich Schuchardt 	set_max_threads(MAX_THREADS);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
3221da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
3231da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
3241da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
32752f5684cSGideon Israel Dsouza int __weak arch_dup_task_struct(struct task_struct *dst,
32861c4628bSSuresh Siddha 					       struct task_struct *src)
32961c4628bSSuresh Siddha {
33061c4628bSSuresh Siddha 	*dst = *src;
33161c4628bSSuresh Siddha 	return 0;
33261c4628bSSuresh Siddha }
33361c4628bSSuresh Siddha 
334d4311ff1SAaron Tomlin void set_task_stack_end_magic(struct task_struct *tsk)
335d4311ff1SAaron Tomlin {
336d4311ff1SAaron Tomlin 	unsigned long *stackend;
337d4311ff1SAaron Tomlin 
338d4311ff1SAaron Tomlin 	stackend = end_of_stack(tsk);
339d4311ff1SAaron Tomlin 	*stackend = STACK_END_MAGIC;	/* for overflow detection */
340d4311ff1SAaron Tomlin }
341d4311ff1SAaron Tomlin 
3421da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct task_struct *tsk;
3451da177e4SLinus Torvalds 	struct thread_info *ti;
346207205a2SEric Dumazet 	int node = tsk_fork_get_node(orig);
3473e26c149SPeter Zijlstra 	int err;
3481da177e4SLinus Torvalds 
349504f52b5SEric Dumazet 	tsk = alloc_task_struct_node(node);
3501da177e4SLinus Torvalds 	if (!tsk)
3511da177e4SLinus Torvalds 		return NULL;
3521da177e4SLinus Torvalds 
353b6a84016SEric Dumazet 	ti = alloc_thread_info_node(tsk, node);
354f19b9f74SAkinobu Mita 	if (!ti)
355f19b9f74SAkinobu Mita 		goto free_tsk;
3561da177e4SLinus Torvalds 
35761c4628bSSuresh Siddha 	err = arch_dup_task_struct(tsk, orig);
35861c4628bSSuresh Siddha 	if (err)
359f19b9f74SAkinobu Mita 		goto free_ti;
36061c4628bSSuresh Siddha 
36187bec58aSAndrew Morton 	tsk->stack = ti;
362dbd95212SKees Cook #ifdef CONFIG_SECCOMP
363dbd95212SKees Cook 	/*
364dbd95212SKees Cook 	 * We must handle setting up seccomp filters once we're under
365dbd95212SKees Cook 	 * the sighand lock in case orig has changed between now and
366dbd95212SKees Cook 	 * then. Until then, filter must be NULL to avoid messing up
367dbd95212SKees Cook 	 * the usage counts on the error path calling free_task.
368dbd95212SKees Cook 	 */
369dbd95212SKees Cook 	tsk->seccomp.filter = NULL;
370dbd95212SKees Cook #endif
37187bec58aSAndrew Morton 
37287bec58aSAndrew Morton 	setup_thread_stack(tsk, orig);
3738e7cac79SAvi Kivity 	clear_user_return_notifier(tsk);
374f26f9affSMike Galbraith 	clear_tsk_need_resched(tsk);
375d4311ff1SAaron Tomlin 	set_task_stack_end_magic(tsk);
3761da177e4SLinus Torvalds 
3770a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
3780a425405SArjan van de Ven 	tsk->stack_canary = get_random_int();
3790a425405SArjan van de Ven #endif
3800a425405SArjan van de Ven 
381fb0a685cSDaniel Rebelo de Oliveira 	/*
382fb0a685cSDaniel Rebelo de Oliveira 	 * One for us, one for whoever does the "release_task()" (usually
383fb0a685cSDaniel Rebelo de Oliveira 	 * parent)
384fb0a685cSDaniel Rebelo de Oliveira 	 */
3851da177e4SLinus Torvalds 	atomic_set(&tsk->usage, 2);
3866c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
3872056a782SJens Axboe 	tsk->btrace_seq = 0;
3886c5c9341SAlexey Dobriyan #endif
389a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
3905640f768SEric Dumazet 	tsk->task_frag.page = NULL;
391093e5840SSebastian Andrzej Siewior 	tsk->wake_q.next = NULL;
392c6a7f572SKOSAKI Motohiro 
393c6a7f572SKOSAKI Motohiro 	account_kernel_stack(ti, 1);
394c6a7f572SKOSAKI Motohiro 
3951da177e4SLinus Torvalds 	return tsk;
39661c4628bSSuresh Siddha 
397f19b9f74SAkinobu Mita free_ti:
39861c4628bSSuresh Siddha 	free_thread_info(ti);
399f19b9f74SAkinobu Mita free_tsk:
40061c4628bSSuresh Siddha 	free_task_struct(tsk);
40161c4628bSSuresh Siddha 	return NULL;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds #ifdef CONFIG_MMU
405a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
4061da177e4SLinus Torvalds {
407297c5eeeSLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
4081da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
4091da177e4SLinus Torvalds 	int retval;
4101da177e4SLinus Torvalds 	unsigned long charge;
4111da177e4SLinus Torvalds 
41232cdba1eSOleg Nesterov 	uprobe_start_dup_mmap();
4131da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
414ec8c0446SRalf Baechle 	flush_cache_dup_mm(oldmm);
415f8ac4ec9SOleg Nesterov 	uprobe_dup_mmap(oldmm, mm);
416ad339451SIngo Molnar 	/*
417ad339451SIngo Molnar 	 * Not linked in yet - no deadlock potential:
418ad339451SIngo Molnar 	 */
419ad339451SIngo Molnar 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
4207ee78232SHugh Dickins 
42190f31d0eSKonstantin Khlebnikov 	/* No ordering required: file already has been exposed. */
42290f31d0eSKonstantin Khlebnikov 	RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
42390f31d0eSKonstantin Khlebnikov 
4244f7d4614SVladimir Davydov 	mm->total_vm = oldmm->total_vm;
42584638335SKonstantin Khlebnikov 	mm->data_vm = oldmm->data_vm;
4264f7d4614SVladimir Davydov 	mm->exec_vm = oldmm->exec_vm;
4274f7d4614SVladimir Davydov 	mm->stack_vm = oldmm->stack_vm;
4284f7d4614SVladimir Davydov 
4291da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
4301da177e4SLinus Torvalds 	rb_parent = NULL;
4311da177e4SLinus Torvalds 	pprev = &mm->mmap;
432f8af4da3SHugh Dickins 	retval = ksm_fork(mm, oldmm);
433f8af4da3SHugh Dickins 	if (retval)
434f8af4da3SHugh Dickins 		goto out;
435ba76149fSAndrea Arcangeli 	retval = khugepaged_fork(mm, oldmm);
436ba76149fSAndrea Arcangeli 	if (retval)
437ba76149fSAndrea Arcangeli 		goto out;
4381da177e4SLinus Torvalds 
439297c5eeeSLinus Torvalds 	prev = NULL;
440fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
4411da177e4SLinus Torvalds 		struct file *file;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
44484638335SKonstantin Khlebnikov 			vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt));
4451da177e4SLinus Torvalds 			continue;
4461da177e4SLinus Torvalds 		}
4471da177e4SLinus Torvalds 		charge = 0;
4481da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
449b2412b7fSHuang Shijie 			unsigned long len = vma_pages(mpnt);
450b2412b7fSHuang Shijie 
451191c5424SAl Viro 			if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
4521da177e4SLinus Torvalds 				goto fail_nomem;
4531da177e4SLinus Torvalds 			charge = len;
4541da177e4SLinus Torvalds 		}
455e94b1766SChristoph Lameter 		tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
4561da177e4SLinus Torvalds 		if (!tmp)
4571da177e4SLinus Torvalds 			goto fail_nomem;
4581da177e4SLinus Torvalds 		*tmp = *mpnt;
4595beb4930SRik van Riel 		INIT_LIST_HEAD(&tmp->anon_vma_chain);
460ef0855d3SOleg Nesterov 		retval = vma_dup_policy(mpnt, tmp);
461ef0855d3SOleg Nesterov 		if (retval)
4621da177e4SLinus Torvalds 			goto fail_nomem_policy;
463a247c3a9SAndrea Arcangeli 		tmp->vm_mm = mm;
4645beb4930SRik van Riel 		if (anon_vma_fork(tmp, mpnt))
4655beb4930SRik van Riel 			goto fail_nomem_anon_vma_fork;
466de60f5f1SEric B Munson 		tmp->vm_flags &=
467de60f5f1SEric B Munson 			~(VM_LOCKED|VM_LOCKONFAULT|VM_UFFD_MISSING|VM_UFFD_WP);
468297c5eeeSLinus Torvalds 		tmp->vm_next = tmp->vm_prev = NULL;
469745f234bSAndrea Arcangeli 		tmp->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
4701da177e4SLinus Torvalds 		file = tmp->vm_file;
4711da177e4SLinus Torvalds 		if (file) {
472496ad9aaSAl Viro 			struct inode *inode = file_inode(file);
473b88ed205SHugh Dickins 			struct address_space *mapping = file->f_mapping;
474b88ed205SHugh Dickins 
4751da177e4SLinus Torvalds 			get_file(file);
4761da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
4771da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
47883cde9e8SDavidlohr Bueso 			i_mmap_lock_write(mapping);
479b88ed205SHugh Dickins 			if (tmp->vm_flags & VM_SHARED)
4804bb5f5d9SDavid Herrmann 				atomic_inc(&mapping->i_mmap_writable);
481b88ed205SHugh Dickins 			flush_dcache_mmap_lock(mapping);
482b88ed205SHugh Dickins 			/* insert tmp into the share list, just after mpnt */
4839826a516SMichel Lespinasse 			vma_interval_tree_insert_after(tmp, mpnt,
4849826a516SMichel Lespinasse 					&mapping->i_mmap);
485b88ed205SHugh Dickins 			flush_dcache_mmap_unlock(mapping);
48683cde9e8SDavidlohr Bueso 			i_mmap_unlock_write(mapping);
4871da177e4SLinus Torvalds 		}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 		/*
490a1e78772SMel Gorman 		 * Clear hugetlb-related page reserves for children. This only
491a1e78772SMel Gorman 		 * affects MAP_PRIVATE mappings. Faults generated by the child
492a1e78772SMel Gorman 		 * are not guaranteed to succeed, even if read-only
493a1e78772SMel Gorman 		 */
494a1e78772SMel Gorman 		if (is_vm_hugetlb_page(tmp))
495a1e78772SMel Gorman 			reset_vma_resv_huge_pages(tmp);
496a1e78772SMel Gorman 
497a1e78772SMel Gorman 		/*
4987ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
4991da177e4SLinus Torvalds 		 */
5001da177e4SLinus Torvalds 		*pprev = tmp;
5011da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
502297c5eeeSLinus Torvalds 		tmp->vm_prev = prev;
503297c5eeeSLinus Torvalds 		prev = tmp;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
5061da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
5071da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 		mm->map_count++;
5100b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
5131da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 		if (retval)
5161da177e4SLinus Torvalds 			goto out;
5171da177e4SLinus Torvalds 	}
518d6dd61c8SJeremy Fitzhardinge 	/* a new mm has just been created */
519d6dd61c8SJeremy Fitzhardinge 	arch_dup_mmap(oldmm, mm);
5201da177e4SLinus Torvalds 	retval = 0;
5211da177e4SLinus Torvalds out:
5227ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
523fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
5241da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
52532cdba1eSOleg Nesterov 	uprobe_end_dup_mmap();
5261da177e4SLinus Torvalds 	return retval;
5275beb4930SRik van Riel fail_nomem_anon_vma_fork:
528ef0855d3SOleg Nesterov 	mpol_put(vma_policy(tmp));
5291da177e4SLinus Torvalds fail_nomem_policy:
5301da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
5311da177e4SLinus Torvalds fail_nomem:
5321da177e4SLinus Torvalds 	retval = -ENOMEM;
5331da177e4SLinus Torvalds 	vm_unacct_memory(charge);
5341da177e4SLinus Torvalds 	goto out;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
5401da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
5411da177e4SLinus Torvalds 		return -ENOMEM;
5421da177e4SLinus Torvalds 	return 0;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm)
5461da177e4SLinus Torvalds {
5475e541973SBenjamin Herrenschmidt 	pgd_free(mm, mm->pgd);
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds #else
55090f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
55190f31d0eSKonstantin Khlebnikov {
55290f31d0eSKonstantin Khlebnikov 	down_write(&oldmm->mmap_sem);
55390f31d0eSKonstantin Khlebnikov 	RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
55490f31d0eSKonstantin Khlebnikov 	up_write(&oldmm->mmap_sem);
55590f31d0eSKonstantin Khlebnikov 	return 0;
55690f31d0eSKonstantin Khlebnikov }
5571da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
5581da177e4SLinus Torvalds #define mm_free_pgd(mm)
5591da177e4SLinus Torvalds #endif /* CONFIG_MMU */
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
5621da177e4SLinus Torvalds 
563e94b1766SChristoph Lameter #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
5641da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
5651da177e4SLinus Torvalds 
5664cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
5674cb0e11bSHidehiro Kawai 
5684cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s)
5694cb0e11bSHidehiro Kawai {
5704cb0e11bSHidehiro Kawai 	default_dump_filter =
5714cb0e11bSHidehiro Kawai 		(simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
5724cb0e11bSHidehiro Kawai 		MMF_DUMP_FILTER_MASK;
5734cb0e11bSHidehiro Kawai 	return 1;
5744cb0e11bSHidehiro Kawai }
5754cb0e11bSHidehiro Kawai 
5764cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup);
5774cb0e11bSHidehiro Kawai 
5781da177e4SLinus Torvalds #include <linux/init_task.h>
5791da177e4SLinus Torvalds 
580858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm)
581858f0993SAlexey Dobriyan {
582858f0993SAlexey Dobriyan #ifdef CONFIG_AIO
583858f0993SAlexey Dobriyan 	spin_lock_init(&mm->ioctx_lock);
584db446a08SBenjamin LaHaise 	mm->ioctx_table = NULL;
585858f0993SAlexey Dobriyan #endif
586858f0993SAlexey Dobriyan }
587858f0993SAlexey Dobriyan 
58833144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
58933144e84SVladimir Davydov {
59033144e84SVladimir Davydov #ifdef CONFIG_MEMCG
59133144e84SVladimir Davydov 	mm->owner = p;
59233144e84SVladimir Davydov #endif
59333144e84SVladimir Davydov }
59433144e84SVladimir Davydov 
59578fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
5961da177e4SLinus Torvalds {
59741f727fdSVladimir Davydov 	mm->mmap = NULL;
59841f727fdSVladimir Davydov 	mm->mm_rb = RB_ROOT;
59941f727fdSVladimir Davydov 	mm->vmacache_seqnum = 0;
6001da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
6011da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
6021da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
6031da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
604999d9fc1SOleg Nesterov 	mm->core_state = NULL;
605e1f56c89SKirill A. Shutemov 	atomic_long_set(&mm->nr_ptes, 0);
6062d2f5119SKirill A. Shutemov 	mm_nr_pmds_init(mm);
60741f727fdSVladimir Davydov 	mm->map_count = 0;
60841f727fdSVladimir Davydov 	mm->locked_vm = 0;
609ce65cefaSVladimir Davydov 	mm->pinned_vm = 0;
610d559db08SKAMEZAWA Hiroyuki 	memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
6111da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
61241f727fdSVladimir Davydov 	mm_init_cpumask(mm);
613858f0993SAlexey Dobriyan 	mm_init_aio(mm);
614cf475ad2SBalbir Singh 	mm_init_owner(mm, p);
61541f727fdSVladimir Davydov 	mmu_notifier_mm_init(mm);
61620841405SRik van Riel 	clear_tlb_flush_pending(mm);
61741f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
61841f727fdSVladimir Davydov 	mm->pmd_huge_pte = NULL;
61941f727fdSVladimir Davydov #endif
6201da177e4SLinus Torvalds 
621a0715cc2SAlex Thorlton 	if (current->mm) {
622a0715cc2SAlex Thorlton 		mm->flags = current->mm->flags & MMF_INIT_MASK;
623a0715cc2SAlex Thorlton 		mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
624a0715cc2SAlex Thorlton 	} else {
625a0715cc2SAlex Thorlton 		mm->flags = default_dump_filter;
6261da177e4SLinus Torvalds 		mm->def_flags = 0;
627a0715cc2SAlex Thorlton 	}
628a0715cc2SAlex Thorlton 
62941f727fdSVladimir Davydov 	if (mm_alloc_pgd(mm))
63041f727fdSVladimir Davydov 		goto fail_nopgd;
63178fb7466SPavel Emelianov 
63241f727fdSVladimir Davydov 	if (init_new_context(p, mm))
63341f727fdSVladimir Davydov 		goto fail_nocontext;
63441f727fdSVladimir Davydov 
63541f727fdSVladimir Davydov 	return mm;
63641f727fdSVladimir Davydov 
63741f727fdSVladimir Davydov fail_nocontext:
63841f727fdSVladimir Davydov 	mm_free_pgd(mm);
63941f727fdSVladimir Davydov fail_nopgd:
6401da177e4SLinus Torvalds 	free_mm(mm);
6411da177e4SLinus Torvalds 	return NULL;
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
644c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm)
645c3f0327fSKonstantin Khlebnikov {
646c3f0327fSKonstantin Khlebnikov 	int i;
647c3f0327fSKonstantin Khlebnikov 
648c3f0327fSKonstantin Khlebnikov 	for (i = 0; i < NR_MM_COUNTERS; i++) {
649c3f0327fSKonstantin Khlebnikov 		long x = atomic_long_read(&mm->rss_stat.count[i]);
650c3f0327fSKonstantin Khlebnikov 
651c3f0327fSKonstantin Khlebnikov 		if (unlikely(x))
652c3f0327fSKonstantin Khlebnikov 			printk(KERN_ALERT "BUG: Bad rss-counter state "
653c3f0327fSKonstantin Khlebnikov 					  "mm:%p idx:%d val:%ld\n", mm, i, x);
654c3f0327fSKonstantin Khlebnikov 	}
655b30fe6c7SKirill A. Shutemov 
656b30fe6c7SKirill A. Shutemov 	if (atomic_long_read(&mm->nr_ptes))
657b30fe6c7SKirill A. Shutemov 		pr_alert("BUG: non-zero nr_ptes on freeing mm: %ld\n",
658b30fe6c7SKirill A. Shutemov 				atomic_long_read(&mm->nr_ptes));
659b30fe6c7SKirill A. Shutemov 	if (mm_nr_pmds(mm))
660b30fe6c7SKirill A. Shutemov 		pr_alert("BUG: non-zero nr_pmds on freeing mm: %ld\n",
661b30fe6c7SKirill A. Shutemov 				mm_nr_pmds(mm));
662b30fe6c7SKirill A. Shutemov 
663e009bb30SKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
66496dad67fSSasha Levin 	VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
665c3f0327fSKonstantin Khlebnikov #endif
666c3f0327fSKonstantin Khlebnikov }
667c3f0327fSKonstantin Khlebnikov 
6681da177e4SLinus Torvalds /*
6691da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
6701da177e4SLinus Torvalds  */
6711da177e4SLinus Torvalds struct mm_struct *mm_alloc(void)
6721da177e4SLinus Torvalds {
6731da177e4SLinus Torvalds 	struct mm_struct *mm;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	mm = allocate_mm();
676de03c72cSKOSAKI Motohiro 	if (!mm)
677de03c72cSKOSAKI Motohiro 		return NULL;
678de03c72cSKOSAKI Motohiro 
6791da177e4SLinus Torvalds 	memset(mm, 0, sizeof(*mm));
6806345d24dSLinus Torvalds 	return mm_init(mm, current);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds /*
6841da177e4SLinus Torvalds  * Called when the last reference to the mm
6851da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
6861da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
6871da177e4SLinus Torvalds  */
6887ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
6911da177e4SLinus Torvalds 	mm_free_pgd(mm);
6921da177e4SLinus Torvalds 	destroy_context(mm);
693cddb8a5cSAndrea Arcangeli 	mmu_notifier_mm_destroy(mm);
694c3f0327fSKonstantin Khlebnikov 	check_mm(mm);
6951da177e4SLinus Torvalds 	free_mm(mm);
6961da177e4SLinus Torvalds }
6976d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop);
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds /*
7001da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
7011da177e4SLinus Torvalds  */
7021da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
7031da177e4SLinus Torvalds {
7040ae26f1bSAndrew Morton 	might_sleep();
7050ae26f1bSAndrew Morton 
7061da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
707d4b3b638SSrikar Dronamraju 		uprobe_clear_state(mm);
7081da177e4SLinus Torvalds 		exit_aio(mm);
7091c2fb7a4SAndrea Arcangeli 		ksm_exit(mm);
710ba76149fSAndrea Arcangeli 		khugepaged_exit(mm); /* must run before exit_mmap */
7111da177e4SLinus Torvalds 		exit_mmap(mm);
712925d1c40SMatt Helsley 		set_mm_exe_file(mm, NULL);
7131da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
7141da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
7151da177e4SLinus Torvalds 			list_del(&mm->mmlist);
7161da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
7171da177e4SLinus Torvalds 		}
718801460d0SHiroshi Shimamoto 		if (mm->binfmt)
719801460d0SHiroshi Shimamoto 			module_put(mm->binfmt->module);
7201da177e4SLinus Torvalds 		mmdrop(mm);
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
7241da177e4SLinus Torvalds 
72590f31d0eSKonstantin Khlebnikov /**
72690f31d0eSKonstantin Khlebnikov  * set_mm_exe_file - change a reference to the mm's executable file
72790f31d0eSKonstantin Khlebnikov  *
72890f31d0eSKonstantin Khlebnikov  * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
72990f31d0eSKonstantin Khlebnikov  *
7306e399cd1SDavidlohr Bueso  * Main users are mmput() and sys_execve(). Callers prevent concurrent
7316e399cd1SDavidlohr Bueso  * invocations: in mmput() nobody alive left, in execve task is single
7326e399cd1SDavidlohr Bueso  * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the
7336e399cd1SDavidlohr Bueso  * mm->exe_file, but does so without using set_mm_exe_file() in order
7346e399cd1SDavidlohr Bueso  * to do avoid the need for any locks.
73590f31d0eSKonstantin Khlebnikov  */
73638646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
73738646013SJiri Slaby {
7386e399cd1SDavidlohr Bueso 	struct file *old_exe_file;
7396e399cd1SDavidlohr Bueso 
7406e399cd1SDavidlohr Bueso 	/*
7416e399cd1SDavidlohr Bueso 	 * It is safe to dereference the exe_file without RCU as
7426e399cd1SDavidlohr Bueso 	 * this function is only called if nobody else can access
7436e399cd1SDavidlohr Bueso 	 * this mm -- see comment above for justification.
7446e399cd1SDavidlohr Bueso 	 */
7456e399cd1SDavidlohr Bueso 	old_exe_file = rcu_dereference_raw(mm->exe_file);
74690f31d0eSKonstantin Khlebnikov 
74738646013SJiri Slaby 	if (new_exe_file)
74838646013SJiri Slaby 		get_file(new_exe_file);
74990f31d0eSKonstantin Khlebnikov 	rcu_assign_pointer(mm->exe_file, new_exe_file);
75090f31d0eSKonstantin Khlebnikov 	if (old_exe_file)
75190f31d0eSKonstantin Khlebnikov 		fput(old_exe_file);
75238646013SJiri Slaby }
75338646013SJiri Slaby 
75490f31d0eSKonstantin Khlebnikov /**
75590f31d0eSKonstantin Khlebnikov  * get_mm_exe_file - acquire a reference to the mm's executable file
75690f31d0eSKonstantin Khlebnikov  *
75790f31d0eSKonstantin Khlebnikov  * Returns %NULL if mm has no associated executable file.
75890f31d0eSKonstantin Khlebnikov  * User must release file via fput().
75990f31d0eSKonstantin Khlebnikov  */
76038646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm)
76138646013SJiri Slaby {
76238646013SJiri Slaby 	struct file *exe_file;
76338646013SJiri Slaby 
76490f31d0eSKonstantin Khlebnikov 	rcu_read_lock();
76590f31d0eSKonstantin Khlebnikov 	exe_file = rcu_dereference(mm->exe_file);
76690f31d0eSKonstantin Khlebnikov 	if (exe_file && !get_file_rcu(exe_file))
76790f31d0eSKonstantin Khlebnikov 		exe_file = NULL;
76890f31d0eSKonstantin Khlebnikov 	rcu_read_unlock();
76938646013SJiri Slaby 	return exe_file;
77038646013SJiri Slaby }
77111163348SDavidlohr Bueso EXPORT_SYMBOL(get_mm_exe_file);
77238646013SJiri Slaby 
7731da177e4SLinus Torvalds /**
7741da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
7751da177e4SLinus Torvalds  *
776246bb0b1SOleg Nesterov  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
7771da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
7781da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
7791da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
7801da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
7811da177e4SLinus Torvalds  */
7821da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
7831da177e4SLinus Torvalds {
7841da177e4SLinus Torvalds 	struct mm_struct *mm;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	task_lock(task);
7871da177e4SLinus Torvalds 	mm = task->mm;
7881da177e4SLinus Torvalds 	if (mm) {
789246bb0b1SOleg Nesterov 		if (task->flags & PF_KTHREAD)
7901da177e4SLinus Torvalds 			mm = NULL;
7911da177e4SLinus Torvalds 		else
7921da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 	task_unlock(task);
7951da177e4SLinus Torvalds 	return mm;
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
7981da177e4SLinus Torvalds 
7998cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
8008cdb878dSChristopher Yeoh {
8018cdb878dSChristopher Yeoh 	struct mm_struct *mm;
8028cdb878dSChristopher Yeoh 	int err;
8038cdb878dSChristopher Yeoh 
8048cdb878dSChristopher Yeoh 	err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
8058cdb878dSChristopher Yeoh 	if (err)
8068cdb878dSChristopher Yeoh 		return ERR_PTR(err);
8078cdb878dSChristopher Yeoh 
8088cdb878dSChristopher Yeoh 	mm = get_task_mm(task);
8098cdb878dSChristopher Yeoh 	if (mm && mm != current->mm &&
8108cdb878dSChristopher Yeoh 			!ptrace_may_access(task, mode)) {
8118cdb878dSChristopher Yeoh 		mmput(mm);
8128cdb878dSChristopher Yeoh 		mm = ERR_PTR(-EACCES);
8138cdb878dSChristopher Yeoh 	}
8148cdb878dSChristopher Yeoh 	mutex_unlock(&task->signal->cred_guard_mutex);
8158cdb878dSChristopher Yeoh 
8168cdb878dSChristopher Yeoh 	return mm;
8178cdb878dSChristopher Yeoh }
8188cdb878dSChristopher Yeoh 
81957b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk)
820c415c3b4SOleg Nesterov {
821d68b46feSOleg Nesterov 	struct completion *vfork;
822c415c3b4SOleg Nesterov 
823d68b46feSOleg Nesterov 	task_lock(tsk);
824d68b46feSOleg Nesterov 	vfork = tsk->vfork_done;
825d68b46feSOleg Nesterov 	if (likely(vfork)) {
826c415c3b4SOleg Nesterov 		tsk->vfork_done = NULL;
827d68b46feSOleg Nesterov 		complete(vfork);
828d68b46feSOleg Nesterov 	}
829d68b46feSOleg Nesterov 	task_unlock(tsk);
830d68b46feSOleg Nesterov }
831d68b46feSOleg Nesterov 
832d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child,
833d68b46feSOleg Nesterov 				struct completion *vfork)
834d68b46feSOleg Nesterov {
835d68b46feSOleg Nesterov 	int killed;
836d68b46feSOleg Nesterov 
837d68b46feSOleg Nesterov 	freezer_do_not_count();
838d68b46feSOleg Nesterov 	killed = wait_for_completion_killable(vfork);
839d68b46feSOleg Nesterov 	freezer_count();
840d68b46feSOleg Nesterov 
841d68b46feSOleg Nesterov 	if (killed) {
842d68b46feSOleg Nesterov 		task_lock(child);
843d68b46feSOleg Nesterov 		child->vfork_done = NULL;
844d68b46feSOleg Nesterov 		task_unlock(child);
845d68b46feSOleg Nesterov 	}
846d68b46feSOleg Nesterov 
847d68b46feSOleg Nesterov 	put_task_struct(child);
848d68b46feSOleg Nesterov 	return killed;
849c415c3b4SOleg Nesterov }
850c415c3b4SOleg Nesterov 
8511da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
8521da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
8531da177e4SLinus Torvalds  * error success whatever.
8541da177e4SLinus Torvalds  *
8551da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
8561da177e4SLinus Torvalds  * from the current process.
8571da177e4SLinus Torvalds  *
8581da177e4SLinus Torvalds  * This difference is important for error handling, when we
8591da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
8601da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
8611da177e4SLinus Torvalds  * restoring the old one. . .
8621da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
8631da177e4SLinus Torvalds  */
8641da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
8651da177e4SLinus Torvalds {
8668141c7f3SLinus Torvalds 	/* Get rid of any futexes when releasing the mm */
8678141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX
868fc6b177dSPeter Zijlstra 	if (unlikely(tsk->robust_list)) {
8698141c7f3SLinus Torvalds 		exit_robust_list(tsk);
870fc6b177dSPeter Zijlstra 		tsk->robust_list = NULL;
871fc6b177dSPeter Zijlstra 	}
8728141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT
873fc6b177dSPeter Zijlstra 	if (unlikely(tsk->compat_robust_list)) {
8748141c7f3SLinus Torvalds 		compat_exit_robust_list(tsk);
875fc6b177dSPeter Zijlstra 		tsk->compat_robust_list = NULL;
876fc6b177dSPeter Zijlstra 	}
8778141c7f3SLinus Torvalds #endif
878322a2c10SThomas Gleixner 	if (unlikely(!list_empty(&tsk->pi_state_list)))
879322a2c10SThomas Gleixner 		exit_pi_state_list(tsk);
8808141c7f3SLinus Torvalds #endif
8818141c7f3SLinus Torvalds 
8820326f5a9SSrikar Dronamraju 	uprobe_free_utask(tsk);
8830326f5a9SSrikar Dronamraju 
8841da177e4SLinus Torvalds 	/* Get rid of any cached register state */
8851da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
8861da177e4SLinus Torvalds 
887fec1d011SRoland McGrath 	/*
888fec1d011SRoland McGrath 	 * If we're exiting normally, clear a user-space tid field if
889fec1d011SRoland McGrath 	 * requested.  We leave this alone when dying by signal, to leave
890fec1d011SRoland McGrath 	 * the value intact in a core dump, and to save the unnecessary
891d68b46feSOleg Nesterov 	 * trouble, say, a killed vfork parent shouldn't touch this mm.
892d68b46feSOleg Nesterov 	 * Userland only wants this done for a sys_exit.
893fec1d011SRoland McGrath 	 */
8949c8a8228SEric Dumazet 	if (tsk->clear_child_tid) {
8959c8a8228SEric Dumazet 		if (!(tsk->flags & PF_SIGNALED) &&
8969c8a8228SEric Dumazet 		    atomic_read(&mm->mm_users) > 1) {
8971da177e4SLinus Torvalds 			/*
8981da177e4SLinus Torvalds 			 * We don't check the error code - if userspace has
8991da177e4SLinus Torvalds 			 * not set up a proper pointer then tough luck.
9001da177e4SLinus Torvalds 			 */
9019c8a8228SEric Dumazet 			put_user(0, tsk->clear_child_tid);
9029c8a8228SEric Dumazet 			sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
9039c8a8228SEric Dumazet 					1, NULL, NULL, 0);
9049c8a8228SEric Dumazet 		}
9059c8a8228SEric Dumazet 		tsk->clear_child_tid = NULL;
9061da177e4SLinus Torvalds 	}
907f7505d64SKonstantin Khlebnikov 
908f7505d64SKonstantin Khlebnikov 	/*
909f7505d64SKonstantin Khlebnikov 	 * All done, finally we can wake up parent and return this mm to him.
910f7505d64SKonstantin Khlebnikov 	 * Also kthread_stop() uses this completion for synchronization.
911f7505d64SKonstantin Khlebnikov 	 */
912f7505d64SKonstantin Khlebnikov 	if (tsk->vfork_done)
913f7505d64SKonstantin Khlebnikov 		complete_vfork_done(tsk);
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
916a0a7ec30SJANAK DESAI /*
917a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
918a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
919a0a7ec30SJANAK DESAI  */
920ff252c1fSDaeSeok Youn static struct mm_struct *dup_mm(struct task_struct *tsk)
921a0a7ec30SJANAK DESAI {
922a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
923a0a7ec30SJANAK DESAI 	int err;
924a0a7ec30SJANAK DESAI 
925a0a7ec30SJANAK DESAI 	mm = allocate_mm();
926a0a7ec30SJANAK DESAI 	if (!mm)
927a0a7ec30SJANAK DESAI 		goto fail_nomem;
928a0a7ec30SJANAK DESAI 
929a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
930a0a7ec30SJANAK DESAI 
93178fb7466SPavel Emelianov 	if (!mm_init(mm, tsk))
932a0a7ec30SJANAK DESAI 		goto fail_nomem;
933a0a7ec30SJANAK DESAI 
934a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
935a0a7ec30SJANAK DESAI 	if (err)
936a0a7ec30SJANAK DESAI 		goto free_pt;
937a0a7ec30SJANAK DESAI 
938a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
939a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
940a0a7ec30SJANAK DESAI 
941801460d0SHiroshi Shimamoto 	if (mm->binfmt && !try_module_get(mm->binfmt->module))
942801460d0SHiroshi Shimamoto 		goto free_pt;
943801460d0SHiroshi Shimamoto 
944a0a7ec30SJANAK DESAI 	return mm;
945a0a7ec30SJANAK DESAI 
946a0a7ec30SJANAK DESAI free_pt:
947801460d0SHiroshi Shimamoto 	/* don't put binfmt in mmput, we haven't got module yet */
948801460d0SHiroshi Shimamoto 	mm->binfmt = NULL;
949a0a7ec30SJANAK DESAI 	mmput(mm);
950a0a7ec30SJANAK DESAI 
951a0a7ec30SJANAK DESAI fail_nomem:
952a0a7ec30SJANAK DESAI 	return NULL;
953a0a7ec30SJANAK DESAI }
954a0a7ec30SJANAK DESAI 
9551da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	struct mm_struct *mm, *oldmm;
9581da177e4SLinus Torvalds 	int retval;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
9611da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
96217406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
96317406b82SMandeep Singh Baines 	tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
96417406b82SMandeep Singh Baines #endif
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	tsk->mm = NULL;
9671da177e4SLinus Torvalds 	tsk->active_mm = NULL;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	/*
9701da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
9711da177e4SLinus Torvalds 	 *
9721da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
9731da177e4SLinus Torvalds 	 */
9741da177e4SLinus Torvalds 	oldmm = current->mm;
9751da177e4SLinus Torvalds 	if (!oldmm)
9761da177e4SLinus Torvalds 		return 0;
9771da177e4SLinus Torvalds 
978615d6e87SDavidlohr Bueso 	/* initialize the new vmacache entries */
979615d6e87SDavidlohr Bueso 	vmacache_flush(tsk);
980615d6e87SDavidlohr Bueso 
9811da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
9821da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
9831da177e4SLinus Torvalds 		mm = oldmm;
9841da177e4SLinus Torvalds 		goto good_mm;
9851da177e4SLinus Torvalds 	}
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	retval = -ENOMEM;
988a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
9891da177e4SLinus Torvalds 	if (!mm)
9901da177e4SLinus Torvalds 		goto fail_nomem;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds good_mm:
9931da177e4SLinus Torvalds 	tsk->mm = mm;
9941da177e4SLinus Torvalds 	tsk->active_mm = mm;
9951da177e4SLinus Torvalds 	return 0;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds fail_nomem:
9981da177e4SLinus Torvalds 	return retval;
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
1001a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
10021da177e4SLinus Torvalds {
1003498052bbSAl Viro 	struct fs_struct *fs = current->fs;
10041da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
1005498052bbSAl Viro 		/* tsk->fs is already what we want */
10062a4419b5SNick Piggin 		spin_lock(&fs->lock);
1007498052bbSAl Viro 		if (fs->in_exec) {
10082a4419b5SNick Piggin 			spin_unlock(&fs->lock);
1009498052bbSAl Viro 			return -EAGAIN;
1010498052bbSAl Viro 		}
1011498052bbSAl Viro 		fs->users++;
10122a4419b5SNick Piggin 		spin_unlock(&fs->lock);
10131da177e4SLinus Torvalds 		return 0;
10141da177e4SLinus Torvalds 	}
1015498052bbSAl Viro 	tsk->fs = copy_fs_struct(fs);
10161da177e4SLinus Torvalds 	if (!tsk->fs)
10171da177e4SLinus Torvalds 		return -ENOMEM;
10181da177e4SLinus Torvalds 	return 0;
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
1021a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
1022a016f338SJANAK DESAI {
1023a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
1024a016f338SJANAK DESAI 	int error = 0;
1025a016f338SJANAK DESAI 
1026a016f338SJANAK DESAI 	/*
1027a016f338SJANAK DESAI 	 * A background process may not have any files ...
1028a016f338SJANAK DESAI 	 */
1029a016f338SJANAK DESAI 	oldf = current->files;
1030a016f338SJANAK DESAI 	if (!oldf)
1031a016f338SJANAK DESAI 		goto out;
1032a016f338SJANAK DESAI 
1033a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
1034a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
1035a016f338SJANAK DESAI 		goto out;
1036a016f338SJANAK DESAI 	}
1037a016f338SJANAK DESAI 
1038a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
1039a016f338SJANAK DESAI 	if (!newf)
1040a016f338SJANAK DESAI 		goto out;
1041a016f338SJANAK DESAI 
1042a016f338SJANAK DESAI 	tsk->files = newf;
1043a016f338SJANAK DESAI 	error = 0;
1044a016f338SJANAK DESAI out:
1045a016f338SJANAK DESAI 	return error;
1046a016f338SJANAK DESAI }
1047a016f338SJANAK DESAI 
1048fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
1049fd0928dfSJens Axboe {
1050fd0928dfSJens Axboe #ifdef CONFIG_BLOCK
1051fd0928dfSJens Axboe 	struct io_context *ioc = current->io_context;
10526e736be7STejun Heo 	struct io_context *new_ioc;
1053fd0928dfSJens Axboe 
1054fd0928dfSJens Axboe 	if (!ioc)
1055fd0928dfSJens Axboe 		return 0;
1056fadad878SJens Axboe 	/*
1057fadad878SJens Axboe 	 * Share io context with parent, if CLONE_IO is set
1058fadad878SJens Axboe 	 */
1059fadad878SJens Axboe 	if (clone_flags & CLONE_IO) {
10603d48749dSTejun Heo 		ioc_task_link(ioc);
10613d48749dSTejun Heo 		tsk->io_context = ioc;
1062fadad878SJens Axboe 	} else if (ioprio_valid(ioc->ioprio)) {
10636e736be7STejun Heo 		new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
10646e736be7STejun Heo 		if (unlikely(!new_ioc))
1065fd0928dfSJens Axboe 			return -ENOMEM;
1066fd0928dfSJens Axboe 
10676e736be7STejun Heo 		new_ioc->ioprio = ioc->ioprio;
106811a3122fSTejun Heo 		put_io_context(new_ioc);
1069fd0928dfSJens Axboe 	}
1070fd0928dfSJens Axboe #endif
1071fd0928dfSJens Axboe 	return 0;
1072fd0928dfSJens Axboe }
1073fd0928dfSJens Axboe 
1074a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct sighand_struct *sig;
10771da177e4SLinus Torvalds 
107860348802SZhaolei 	if (clone_flags & CLONE_SIGHAND) {
10791da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
10801da177e4SLinus Torvalds 		return 0;
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1083e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
10841da177e4SLinus Torvalds 	if (!sig)
10851da177e4SLinus Torvalds 		return -ENOMEM;
10869d7fb042SPeter Zijlstra 
10871da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
10881da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
10891da177e4SLinus Torvalds 	return 0;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
1092a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
1093c81addc9SOleg Nesterov {
1094d80e731eSOleg Nesterov 	if (atomic_dec_and_test(&sighand->count)) {
1095d80e731eSOleg Nesterov 		signalfd_cleanup(sighand);
1096392809b2SOleg Nesterov 		/*
1097392809b2SOleg Nesterov 		 * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it
1098392809b2SOleg Nesterov 		 * without an RCU grace period, see __lock_task_sighand().
1099392809b2SOleg Nesterov 		 */
1100c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
1101c81addc9SOleg Nesterov 	}
1102d80e731eSOleg Nesterov }
1103c81addc9SOleg Nesterov 
1104f06febc9SFrank Mayhar /*
1105f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a thread group.
1106f06febc9SFrank Mayhar  */
1107f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig)
1108f06febc9SFrank Mayhar {
110978d7d407SJiri Slaby 	unsigned long cpu_limit;
111078d7d407SJiri Slaby 
1111316c1608SJason Low 	cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
111278d7d407SJiri Slaby 	if (cpu_limit != RLIM_INFINITY) {
111378d7d407SJiri Slaby 		sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
1114d5c373ebSJason Low 		sig->cputimer.running = true;
11156279a751SOleg Nesterov 	}
11166279a751SOleg Nesterov 
1117f06febc9SFrank Mayhar 	/* The timer lists. */
1118f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
1119f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
1120f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
1121f06febc9SFrank Mayhar }
1122f06febc9SFrank Mayhar 
1123a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
11241da177e4SLinus Torvalds {
11251da177e4SLinus Torvalds 	struct signal_struct *sig;
11261da177e4SLinus Torvalds 
11274ab6c083SOleg Nesterov 	if (clone_flags & CLONE_THREAD)
1128490dea45SPeter Zijlstra 		return 0;
11296279a751SOleg Nesterov 
1130a56704efSVeaceslav Falico 	sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
11311da177e4SLinus Torvalds 	tsk->signal = sig;
11321da177e4SLinus Torvalds 	if (!sig)
11331da177e4SLinus Torvalds 		return -ENOMEM;
11341da177e4SLinus Torvalds 
1135b3ac022cSOleg Nesterov 	sig->nr_threads = 1;
11361da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
1137b3ac022cSOleg Nesterov 	atomic_set(&sig->sigcnt, 1);
11380c740d0aSOleg Nesterov 
11390c740d0aSOleg Nesterov 	/* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
11400c740d0aSOleg Nesterov 	sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node);
11410c740d0aSOleg Nesterov 	tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head);
11420c740d0aSOleg Nesterov 
11431da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
1144db51aeccSOleg Nesterov 	sig->curr_target = tsk;
11451da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
11461da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
1147e78c3496SRik van Riel 	seqlock_init(&sig->stats_lock);
11489d7fb042SPeter Zijlstra 	prev_cputime_init(&sig->prev_cputime);
11491da177e4SLinus Torvalds 
1150c9cb2e3dSThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
11511da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	task_lock(current->group_leader);
11541da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
11551da177e4SLinus Torvalds 	task_unlock(current->group_leader);
11561da177e4SLinus Torvalds 
11576279a751SOleg Nesterov 	posix_cpu_timers_init_group(sig);
11586279a751SOleg Nesterov 
1159522ed776SMiloslav Trmac 	tty_audit_fork(sig);
11605091faa4SMike Galbraith 	sched_autogroup_fork(sig);
1161522ed776SMiloslav Trmac 
1162a63d83f4SDavid Rientjes 	sig->oom_score_adj = current->signal->oom_score_adj;
1163dabb16f6SMandeep Singh Baines 	sig->oom_score_adj_min = current->signal->oom_score_adj_min;
116428b83c51SKOSAKI Motohiro 
1165ebec18a6SLennart Poettering 	sig->has_child_subreaper = current->signal->has_child_subreaper ||
1166ebec18a6SLennart Poettering 				   current->signal->is_child_subreaper;
1167ebec18a6SLennart Poettering 
11689b1bf12dSKOSAKI Motohiro 	mutex_init(&sig->cred_guard_mutex);
11699b1bf12dSKOSAKI Motohiro 
11701da177e4SLinus Torvalds 	return 0;
11711da177e4SLinus Torvalds }
11721da177e4SLinus Torvalds 
1173dbd95212SKees Cook static void copy_seccomp(struct task_struct *p)
1174dbd95212SKees Cook {
1175dbd95212SKees Cook #ifdef CONFIG_SECCOMP
1176dbd95212SKees Cook 	/*
1177dbd95212SKees Cook 	 * Must be called with sighand->lock held, which is common to
1178dbd95212SKees Cook 	 * all threads in the group. Holding cred_guard_mutex is not
1179dbd95212SKees Cook 	 * needed because this new task is not yet running and cannot
1180dbd95212SKees Cook 	 * be racing exec.
1181dbd95212SKees Cook 	 */
118269f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
1183dbd95212SKees Cook 
1184dbd95212SKees Cook 	/* Ref-count the new filter user, and assign it. */
1185dbd95212SKees Cook 	get_seccomp_filter(current);
1186dbd95212SKees Cook 	p->seccomp = current->seccomp;
1187dbd95212SKees Cook 
1188dbd95212SKees Cook 	/*
1189dbd95212SKees Cook 	 * Explicitly enable no_new_privs here in case it got set
1190dbd95212SKees Cook 	 * between the task_struct being duplicated and holding the
1191dbd95212SKees Cook 	 * sighand lock. The seccomp state and nnp must be in sync.
1192dbd95212SKees Cook 	 */
1193dbd95212SKees Cook 	if (task_no_new_privs(current))
1194dbd95212SKees Cook 		task_set_no_new_privs(p);
1195dbd95212SKees Cook 
1196dbd95212SKees Cook 	/*
1197dbd95212SKees Cook 	 * If the parent gained a seccomp mode after copying thread
1198dbd95212SKees Cook 	 * flags and between before we held the sighand lock, we have
1199dbd95212SKees Cook 	 * to manually enable the seccomp thread flag here.
1200dbd95212SKees Cook 	 */
1201dbd95212SKees Cook 	if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
1202dbd95212SKees Cook 		set_tsk_thread_flag(p, TIF_SECCOMP);
1203dbd95212SKees Cook #endif
1204dbd95212SKees Cook }
1205dbd95212SKees Cook 
120617da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
12091da177e4SLinus Torvalds 
1210b488893aSPavel Emelyanov 	return task_pid_vnr(current);
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds 
1213a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p)
121423f78d4aSIngo Molnar {
12151d615482SThomas Gleixner 	raw_spin_lock_init(&p->pi_lock);
1216e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES
1217fb00aca4SPeter Zijlstra 	p->pi_waiters = RB_ROOT;
1218fb00aca4SPeter Zijlstra 	p->pi_waiters_leftmost = NULL;
121923f78d4aSIngo Molnar 	p->pi_blocked_on = NULL;
122023f78d4aSIngo Molnar #endif
122123f78d4aSIngo Molnar }
122223f78d4aSIngo Molnar 
12231da177e4SLinus Torvalds /*
1224f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a single task.
1225f06febc9SFrank Mayhar  */
1226f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk)
1227f06febc9SFrank Mayhar {
122864861634SMartin Schwidefsky 	tsk->cputime_expires.prof_exp = 0;
122964861634SMartin Schwidefsky 	tsk->cputime_expires.virt_exp = 0;
1230f06febc9SFrank Mayhar 	tsk->cputime_expires.sched_exp = 0;
1231f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[0]);
1232f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[1]);
1233f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[2]);
1234f06febc9SFrank Mayhar }
1235f06febc9SFrank Mayhar 
123681907739SOleg Nesterov static inline void
123781907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
123881907739SOleg Nesterov {
123981907739SOleg Nesterov 	 task->pids[type].pid = pid;
124081907739SOleg Nesterov }
124181907739SOleg Nesterov 
1242f06febc9SFrank Mayhar /*
12431da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
12441da177e4SLinus Torvalds  * but does not actually start it yet.
12451da177e4SLinus Torvalds  *
12461da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
12471da177e4SLinus Torvalds  * parts of the process environment (as per the clone
12481da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
12491da177e4SLinus Torvalds  */
125036c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags,
12511da177e4SLinus Torvalds 					unsigned long stack_start,
12521da177e4SLinus Torvalds 					unsigned long stack_size,
12531da177e4SLinus Torvalds 					int __user *child_tidptr,
125409a05394SRoland McGrath 					struct pid *pid,
12553033f14aSJosh Triplett 					int trace,
12563033f14aSJosh Triplett 					unsigned long tls)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	int retval;
1259a24efe62SMariusz Kozlowski 	struct task_struct *p;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
12621da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
12631da177e4SLinus Torvalds 
1264e66eded8SEric W. Biederman 	if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
1265e66eded8SEric W. Biederman 		return ERR_PTR(-EINVAL);
1266e66eded8SEric W. Biederman 
12671da177e4SLinus Torvalds 	/*
12681da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
12691da177e4SLinus Torvalds 	 * can only be started up within the thread group.
12701da177e4SLinus Torvalds 	 */
12711da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
12721da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	/*
12751da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
12761da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
12771da177e4SLinus Torvalds 	 * for various simplifications in other code.
12781da177e4SLinus Torvalds 	 */
12791da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
12801da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
12811da177e4SLinus Torvalds 
1282123be07bSSukadev Bhattiprolu 	/*
1283123be07bSSukadev Bhattiprolu 	 * Siblings of global init remain as zombies on exit since they are
1284123be07bSSukadev Bhattiprolu 	 * not reaped by their parent (swapper). To solve this and to avoid
1285123be07bSSukadev Bhattiprolu 	 * multi-rooted process trees, prevent global and container-inits
1286123be07bSSukadev Bhattiprolu 	 * from creating siblings.
1287123be07bSSukadev Bhattiprolu 	 */
1288123be07bSSukadev Bhattiprolu 	if ((clone_flags & CLONE_PARENT) &&
1289123be07bSSukadev Bhattiprolu 				current->signal->flags & SIGNAL_UNKILLABLE)
1290123be07bSSukadev Bhattiprolu 		return ERR_PTR(-EINVAL);
1291123be07bSSukadev Bhattiprolu 
12928382fcacSEric W. Biederman 	/*
129340a0d32dSOleg Nesterov 	 * If the new process will be in a different pid or user namespace
1294faf00da5SEric W. Biederman 	 * do not allow it to share a thread group with the forking task.
12958382fcacSEric W. Biederman 	 */
1296faf00da5SEric W. Biederman 	if (clone_flags & CLONE_THREAD) {
129740a0d32dSOleg Nesterov 		if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
129840a0d32dSOleg Nesterov 		    (task_active_pid_ns(current) !=
1299c2b1df2eSAndy Lutomirski 				current->nsproxy->pid_ns_for_children))
13008382fcacSEric W. Biederman 			return ERR_PTR(-EINVAL);
130140a0d32dSOleg Nesterov 	}
13028382fcacSEric W. Biederman 
13031da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
13041da177e4SLinus Torvalds 	if (retval)
13051da177e4SLinus Torvalds 		goto fork_out;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	retval = -ENOMEM;
13081da177e4SLinus Torvalds 	p = dup_task_struct(current);
13091da177e4SLinus Torvalds 	if (!p)
13101da177e4SLinus Torvalds 		goto fork_out;
13111da177e4SLinus Torvalds 
1312f7e8b616SSteven Rostedt 	ftrace_graph_init_task(p);
1313f7e8b616SSteven Rostedt 
1314bea493a0SPeter Zijlstra 	rt_mutex_init_task(p);
1315bea493a0SPeter Zijlstra 
1316d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING
1317de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1318de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1319de30a2b3SIngo Molnar #endif
13201da177e4SLinus Torvalds 	retval = -EAGAIN;
13213b11a1deSDavid Howells 	if (atomic_read(&p->real_cred->user->processes) >=
132278d7d407SJiri Slaby 			task_rlimit(p, RLIMIT_NPROC)) {
1323b57922b6SEric Paris 		if (p->real_cred->user != INIT_USER &&
1324b57922b6SEric Paris 		    !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
13251da177e4SLinus Torvalds 			goto bad_fork_free;
13261da177e4SLinus Torvalds 	}
132772fa5997SVasiliy Kulikov 	current->flags &= ~PF_NPROC_EXCEEDED;
13281da177e4SLinus Torvalds 
1329f1752eecSDavid Howells 	retval = copy_creds(p, clone_flags);
1330f1752eecSDavid Howells 	if (retval < 0)
1331f1752eecSDavid Howells 		goto bad_fork_free;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	/*
13341da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
13351da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
13361da177e4SLinus Torvalds 	 * to stop root fork bombs.
13371da177e4SLinus Torvalds 	 */
133804ec93feSLi Zefan 	retval = -EAGAIN;
13391da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
13401da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
13411da177e4SLinus Torvalds 
1342ca74e92bSShailabh Nagar 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
1343514ddb44SDavid Rientjes 	p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
1344514ddb44SDavid Rientjes 	p->flags |= PF_FORKNOEXEC;
13451da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
13461da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
1347f41d911fSPaul E. McKenney 	rcu_copy_process(p);
13481da177e4SLinus Torvalds 	p->vfork_done = NULL;
13491da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	init_sigpending(&p->pending);
13521da177e4SLinus Torvalds 
135364861634SMartin Schwidefsky 	p->utime = p->stime = p->gtime = 0;
135464861634SMartin Schwidefsky 	p->utimescaled = p->stimescaled = 0;
13559d7fb042SPeter Zijlstra 	prev_cputime_init(&p->prev_cputime);
13569d7fb042SPeter Zijlstra 
13576a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1358b7ce2277SFrederic Weisbecker 	seqcount_init(&p->vtime_seqcount);
13596a61671bSFrederic Weisbecker 	p->vtime_snap = 0;
13607098c1eaSFrederic Weisbecker 	p->vtime_snap_whence = VTIME_INACTIVE;
13616a61671bSFrederic Weisbecker #endif
13626a61671bSFrederic Weisbecker 
1363a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
1364a3a2e76cSKAMEZAWA Hiroyuki 	memset(&p->rss_stat, 0, sizeof(p->rss_stat));
1365a3a2e76cSKAMEZAWA Hiroyuki #endif
1366172ba844SBalbir Singh 
13676976675dSArjan van de Ven 	p->default_timer_slack_ns = current->timer_slack_ns;
13686976675dSArjan van de Ven 
13695995477aSAndrea Righi 	task_io_accounting_init(&p->ioac);
13701da177e4SLinus Torvalds 	acct_clear_integrals(p);
13711da177e4SLinus Torvalds 
1372f06febc9SFrank Mayhar 	posix_cpu_timers_init(p);
13731da177e4SLinus Torvalds 
1374ccbf62d8SThomas Gleixner 	p->start_time = ktime_get_ns();
137557e0be04SThomas Gleixner 	p->real_start_time = ktime_get_boot_ns();
13761da177e4SLinus Torvalds 	p->io_context = NULL;
13771da177e4SLinus Torvalds 	p->audit_context = NULL;
1378257058aeSTejun Heo 	threadgroup_change_begin(current);
1379b4f48b63SPaul Menage 	cgroup_fork(p);
13801da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1381846a16bfSLee Schermerhorn 	p->mempolicy = mpol_dup(p->mempolicy);
13821da177e4SLinus Torvalds 	if (IS_ERR(p->mempolicy)) {
13831da177e4SLinus Torvalds 		retval = PTR_ERR(p->mempolicy);
13841da177e4SLinus Torvalds 		p->mempolicy = NULL;
1385e8604cb4SLi Zefan 		goto bad_fork_cleanup_threadgroup_lock;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds #endif
1388778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS
1389778d3b0fSMichal Hocko 	p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
1390778d3b0fSMichal Hocko 	p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
1391cc9a6c87SMel Gorman 	seqcount_init(&p->mems_allowed_seq);
1392778d3b0fSMichal Hocko #endif
1393de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1394de30a2b3SIngo Molnar 	p->irq_events = 0;
1395de30a2b3SIngo Molnar 	p->hardirqs_enabled = 0;
1396de30a2b3SIngo Molnar 	p->hardirq_enable_ip = 0;
1397de30a2b3SIngo Molnar 	p->hardirq_enable_event = 0;
1398de30a2b3SIngo Molnar 	p->hardirq_disable_ip = _THIS_IP_;
1399de30a2b3SIngo Molnar 	p->hardirq_disable_event = 0;
1400de30a2b3SIngo Molnar 	p->softirqs_enabled = 1;
1401de30a2b3SIngo Molnar 	p->softirq_enable_ip = _THIS_IP_;
1402de30a2b3SIngo Molnar 	p->softirq_enable_event = 0;
1403de30a2b3SIngo Molnar 	p->softirq_disable_ip = 0;
1404de30a2b3SIngo Molnar 	p->softirq_disable_event = 0;
1405de30a2b3SIngo Molnar 	p->hardirq_context = 0;
1406de30a2b3SIngo Molnar 	p->softirq_context = 0;
1407de30a2b3SIngo Molnar #endif
14088bcbde54SDavid Hildenbrand 
14098bcbde54SDavid Hildenbrand 	p->pagefault_disabled = 0;
14108bcbde54SDavid Hildenbrand 
1411fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1412fbb9ce95SIngo Molnar 	p->lockdep_depth = 0; /* no locks held yet */
1413fbb9ce95SIngo Molnar 	p->curr_chain_key = 0;
1414fbb9ce95SIngo Molnar 	p->lockdep_recursion = 0;
1415fbb9ce95SIngo Molnar #endif
14161da177e4SLinus Torvalds 
1417408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1418408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1419408894eeSIngo Molnar #endif
1420cafe5635SKent Overstreet #ifdef CONFIG_BCACHE
1421cafe5635SKent Overstreet 	p->sequential_io	= 0;
1422cafe5635SKent Overstreet 	p->sequential_io_avg	= 0;
1423cafe5635SKent Overstreet #endif
14240f481406SMarkus Metzger 
14253c90e6e9SSrivatsa Vaddagiri 	/* Perform scheduler related setup. Assign this task to a CPU. */
1426aab03e05SDario Faggioli 	retval = sched_fork(clone_flags, p);
1427aab03e05SDario Faggioli 	if (retval)
1428aab03e05SDario Faggioli 		goto bad_fork_cleanup_policy;
14296ab423e0SPeter Zijlstra 
1430cdd6c482SIngo Molnar 	retval = perf_event_init_task(p);
14316ab423e0SPeter Zijlstra 	if (retval)
14326ab423e0SPeter Zijlstra 		goto bad_fork_cleanup_policy;
1433fb0a685cSDaniel Rebelo de Oliveira 	retval = audit_alloc(p);
1434fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
14356c72e350SPeter Zijlstra 		goto bad_fork_cleanup_perf;
14361da177e4SLinus Torvalds 	/* copy all the process information */
1437ab602f79SJack Miller 	shm_init_task(p);
1438fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_semundo(clone_flags, p);
1439fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
14401da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
1441fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_files(clone_flags, p);
1442fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
14431da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
1444fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_fs(clone_flags, p);
1445fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
14461da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
1447fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_sighand(clone_flags, p);
1448fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
14491da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
1450fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_signal(clone_flags, p);
1451fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
14521da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
1453fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_mm(clone_flags, p);
1454fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
14551da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
1456fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_namespaces(clone_flags, p);
1457fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
1458d84f4f99SDavid Howells 		goto bad_fork_cleanup_mm;
1459fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_io(clone_flags, p);
1460fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
1461fd0928dfSJens Axboe 		goto bad_fork_cleanup_namespaces;
14623033f14aSJosh Triplett 	retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls);
14631da177e4SLinus Torvalds 	if (retval)
1464fd0928dfSJens Axboe 		goto bad_fork_cleanup_io;
14651da177e4SLinus Torvalds 
1466425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid) {
1467c2b1df2eSAndy Lutomirski 		pid = alloc_pid(p->nsproxy->pid_ns_for_children);
146835f71bc0SMichal Hocko 		if (IS_ERR(pid)) {
146935f71bc0SMichal Hocko 			retval = PTR_ERR(pid);
1470fd0928dfSJens Axboe 			goto bad_fork_cleanup_io;
1471425fb2b4SPavel Emelyanov 		}
147235f71bc0SMichal Hocko 	}
1473425fb2b4SPavel Emelyanov 
14741da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
14751da177e4SLinus Torvalds 	/*
14761da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
14771da177e4SLinus Torvalds 	 */
14781da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
147973c10101SJens Axboe #ifdef CONFIG_BLOCK
148073c10101SJens Axboe 	p->plug = NULL;
148173c10101SJens Axboe #endif
148242b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
14838f17d3a5SIngo Molnar 	p->robust_list = NULL;
14848f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
14858f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
14868f17d3a5SIngo Molnar #endif
1487c87e2837SIngo Molnar 	INIT_LIST_HEAD(&p->pi_state_list);
1488c87e2837SIngo Molnar 	p->pi_state_cache = NULL;
148942b2dd0aSAlexey Dobriyan #endif
14901da177e4SLinus Torvalds 	/*
1491f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1492f9a3879aSGOTO Masanori 	 */
1493f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1494f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1495f9a3879aSGOTO Masanori 
1496f9a3879aSGOTO Masanori 	/*
14976580807dSOleg Nesterov 	 * Syscall tracing and stepping should be turned off in the
14986580807dSOleg Nesterov 	 * child regardless of CLONE_PTRACE.
14991da177e4SLinus Torvalds 	 */
15006580807dSOleg Nesterov 	user_disable_single_step(p);
15011da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1502ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1503ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1504ed75e8d5SLaurent Vivier #endif
15059745512cSArjan van de Ven 	clear_all_latency_tracing(p);
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
150818c830dfSOleg Nesterov 	p->pid = pid_nr(pid);
150918c830dfSOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
15105f8aadd8SOleg Nesterov 		p->exit_signal = -1;
151118c830dfSOleg Nesterov 		p->group_leader = current->group_leader;
151218c830dfSOleg Nesterov 		p->tgid = current->tgid;
151318c830dfSOleg Nesterov 	} else {
151418c830dfSOleg Nesterov 		if (clone_flags & CLONE_PARENT)
15155f8aadd8SOleg Nesterov 			p->exit_signal = current->group_leader->exit_signal;
15165f8aadd8SOleg Nesterov 		else
15175f8aadd8SOleg Nesterov 			p->exit_signal = (clone_flags & CSIGNAL);
151818c830dfSOleg Nesterov 		p->group_leader = p;
151918c830dfSOleg Nesterov 		p->tgid = p->pid;
152018c830dfSOleg Nesterov 	}
15215f8aadd8SOleg Nesterov 
15229d823e8fSWu Fengguang 	p->nr_dirtied = 0;
15239d823e8fSWu Fengguang 	p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
152483712358SWu Fengguang 	p->dirty_paused_when = 0;
15259d823e8fSWu Fengguang 
1526bb8cbbfeSOleg Nesterov 	p->pdeath_signal = 0;
152747e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
1528158e1645SAl Viro 	p->task_works = NULL;
15291da177e4SLinus Torvalds 
153018c830dfSOleg Nesterov 	/*
15317e47682eSAleksa Sarai 	 * Ensure that the cgroup subsystem policies allow the new process to be
15327e47682eSAleksa Sarai 	 * forked. It should be noted the the new process's css_set can be changed
15337e47682eSAleksa Sarai 	 * between here and cgroup_post_fork() if an organisation operation is in
15347e47682eSAleksa Sarai 	 * progress.
15357e47682eSAleksa Sarai 	 */
1536b53202e6SOleg Nesterov 	retval = cgroup_can_fork(p);
15377e47682eSAleksa Sarai 	if (retval)
15387e47682eSAleksa Sarai 		goto bad_fork_free_pid;
15397e47682eSAleksa Sarai 
15407e47682eSAleksa Sarai 	/*
154118c830dfSOleg Nesterov 	 * Make it visible to the rest of the system, but dont wake it up yet.
154218c830dfSOleg Nesterov 	 * Need tasklist lock for parent etc handling!
154318c830dfSOleg Nesterov 	 */
15441da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
15472d5516cbSOleg Nesterov 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
15481da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
15492d5516cbSOleg Nesterov 		p->parent_exec_id = current->parent_exec_id;
15502d5516cbSOleg Nesterov 	} else {
15511da177e4SLinus Torvalds 		p->real_parent = current;
15522d5516cbSOleg Nesterov 		p->parent_exec_id = current->self_exec_id;
15532d5516cbSOleg Nesterov 	}
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
15564a2c7a78SOleg Nesterov 
15574a2c7a78SOleg Nesterov 	/*
1558dbd95212SKees Cook 	 * Copy seccomp details explicitly here, in case they were changed
1559dbd95212SKees Cook 	 * before holding sighand lock.
1560dbd95212SKees Cook 	 */
1561dbd95212SKees Cook 	copy_seccomp(p);
1562dbd95212SKees Cook 
1563dbd95212SKees Cook 	/*
15644a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
15654a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
15664a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
15674a2c7a78SOleg Nesterov 	 * it's process group.
15684a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
15694a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
15704a2c7a78SOleg Nesterov 	*/
15714a2c7a78SOleg Nesterov 	recalc_sigpending();
15724a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
15734a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
15744a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
15754a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
15767e47682eSAleksa Sarai 		goto bad_fork_cancel_cgroup;
15774a2c7a78SOleg Nesterov 	}
15784a2c7a78SOleg Nesterov 
157973b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
15804b9d33e6STejun Heo 		ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
15811da177e4SLinus Torvalds 
158281907739SOleg Nesterov 		init_task_pid(p, PIDTYPE_PID, pid);
15831da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
158481907739SOleg Nesterov 			init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
158581907739SOleg Nesterov 			init_task_pid(p, PIDTYPE_SID, task_session(current));
158681907739SOleg Nesterov 
15871c4042c2SEric W. Biederman 			if (is_child_reaper(pid)) {
158817cf22c3SEric W. Biederman 				ns_of_pid(pid)->child_reaper = p;
15891c4042c2SEric W. Biederman 				p->signal->flags |= SIGNAL_UNKILLABLE;
15901c4042c2SEric W. Biederman 			}
15915cd17569SEric W. Biederman 
1592fea9d175SOleg Nesterov 			p->signal->leader_pid = pid;
15939c9f4dedSAlan Cox 			p->signal->tty = tty_kref_get(current->signal->tty);
15949cd80bbbSOleg Nesterov 			list_add_tail(&p->sibling, &p->real_parent->children);
15955e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
159681907739SOleg Nesterov 			attach_pid(p, PIDTYPE_PGID);
159781907739SOleg Nesterov 			attach_pid(p, PIDTYPE_SID);
1598909ea964SChristoph Lameter 			__this_cpu_inc(process_counts);
159980628ca0SOleg Nesterov 		} else {
160080628ca0SOleg Nesterov 			current->signal->nr_threads++;
160180628ca0SOleg Nesterov 			atomic_inc(&current->signal->live);
160280628ca0SOleg Nesterov 			atomic_inc(&current->signal->sigcnt);
160380628ca0SOleg Nesterov 			list_add_tail_rcu(&p->thread_group,
160480628ca0SOleg Nesterov 					  &p->group_leader->thread_group);
16050c740d0aSOleg Nesterov 			list_add_tail_rcu(&p->thread_node,
16060c740d0aSOleg Nesterov 					  &p->signal->thread_head);
16071da177e4SLinus Torvalds 		}
160881907739SOleg Nesterov 		attach_pid(p, PIDTYPE_PID);
16091da177e4SLinus Torvalds 		nr_threads++;
161073b9ebfeSOleg Nesterov 	}
161173b9ebfeSOleg Nesterov 
16121da177e4SLinus Torvalds 	total_forks++;
16133f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
16144af4206bSOleg Nesterov 	syscall_tracepoint_update(p);
16151da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
16164af4206bSOleg Nesterov 
1617c13cf856SAndrew Morton 	proc_fork_connector(p);
1618b53202e6SOleg Nesterov 	cgroup_post_fork(p);
1619257058aeSTejun Heo 	threadgroup_change_end(current);
1620cdd6c482SIngo Molnar 	perf_event_fork(p);
162143d2b113SKAMEZAWA Hiroyuki 
162243d2b113SKAMEZAWA Hiroyuki 	trace_task_newtask(p, clone_flags);
16233ab67966SOleg Nesterov 	uprobe_copy_process(p, clone_flags);
162443d2b113SKAMEZAWA Hiroyuki 
16251da177e4SLinus Torvalds 	return p;
16261da177e4SLinus Torvalds 
16277e47682eSAleksa Sarai bad_fork_cancel_cgroup:
1628b53202e6SOleg Nesterov 	cgroup_cancel_fork(p);
1629425fb2b4SPavel Emelyanov bad_fork_free_pid:
1630425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid)
1631425fb2b4SPavel Emelyanov 		free_pid(pid);
1632fd0928dfSJens Axboe bad_fork_cleanup_io:
1633b69f2292SLouis Rilling 	if (p->io_context)
1634b69f2292SLouis Rilling 		exit_io_context(p);
1635ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces:
1636444f378bSLinus Torvalds 	exit_task_namespaces(p);
16371da177e4SLinus Torvalds bad_fork_cleanup_mm:
1638c9f01245SDavid Rientjes 	if (p->mm)
16391da177e4SLinus Torvalds 		mmput(p->mm);
16401da177e4SLinus Torvalds bad_fork_cleanup_signal:
16414ab6c083SOleg Nesterov 	if (!(clone_flags & CLONE_THREAD))
16421c5354deSMike Galbraith 		free_signal_struct(p->signal);
16431da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1644a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
16451da177e4SLinus Torvalds bad_fork_cleanup_fs:
16461da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
16471da177e4SLinus Torvalds bad_fork_cleanup_files:
16481da177e4SLinus Torvalds 	exit_files(p); /* blocking */
16491da177e4SLinus Torvalds bad_fork_cleanup_semundo:
16501da177e4SLinus Torvalds 	exit_sem(p);
16511da177e4SLinus Torvalds bad_fork_cleanup_audit:
16521da177e4SLinus Torvalds 	audit_free(p);
16536c72e350SPeter Zijlstra bad_fork_cleanup_perf:
1654cdd6c482SIngo Molnar 	perf_event_free_task(p);
16556c72e350SPeter Zijlstra bad_fork_cleanup_policy:
16561da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1657f0be3d32SLee Schermerhorn 	mpol_put(p->mempolicy);
1658e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock:
16591da177e4SLinus Torvalds #endif
1660257058aeSTejun Heo 	threadgroup_change_end(current);
166135df17c5SShailabh Nagar 	delayacct_tsk_free(p);
16621da177e4SLinus Torvalds bad_fork_cleanup_count:
1663d84f4f99SDavid Howells 	atomic_dec(&p->cred->user->processes);
1664e0e81739SDavid Howells 	exit_creds(p);
16651da177e4SLinus Torvalds bad_fork_free:
16661da177e4SLinus Torvalds 	free_task(p);
1667fe7d37d1SOleg Nesterov fork_out:
1668fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
16691da177e4SLinus Torvalds }
16701da177e4SLinus Torvalds 
1671f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links)
1672f106eee1SOleg Nesterov {
1673f106eee1SOleg Nesterov 	enum pid_type type;
1674f106eee1SOleg Nesterov 
1675f106eee1SOleg Nesterov 	for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
1676f106eee1SOleg Nesterov 		INIT_HLIST_NODE(&links[type].node); /* not really needed */
1677f106eee1SOleg Nesterov 		links[type].pid = &init_struct_pid;
1678f106eee1SOleg Nesterov 	}
1679f106eee1SOleg Nesterov }
1680f106eee1SOleg Nesterov 
16810db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu)
16821da177e4SLinus Torvalds {
168336c8b586SIngo Molnar 	struct task_struct *task;
16843033f14aSJosh Triplett 	task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0);
1685f106eee1SOleg Nesterov 	if (!IS_ERR(task)) {
1686f106eee1SOleg Nesterov 		init_idle_pids(task->pids);
16871da177e4SLinus Torvalds 		init_idle(task, cpu);
1688f106eee1SOleg Nesterov 	}
168973b9ebfeSOleg Nesterov 
16901da177e4SLinus Torvalds 	return task;
16911da177e4SLinus Torvalds }
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds /*
16941da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
16951da177e4SLinus Torvalds  *
16961da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
16971da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
16981da177e4SLinus Torvalds  */
16993033f14aSJosh Triplett long _do_fork(unsigned long clone_flags,
17001da177e4SLinus Torvalds 	      unsigned long stack_start,
17011da177e4SLinus Torvalds 	      unsigned long stack_size,
17021da177e4SLinus Torvalds 	      int __user *parent_tidptr,
17033033f14aSJosh Triplett 	      int __user *child_tidptr,
17043033f14aSJosh Triplett 	      unsigned long tls)
17051da177e4SLinus Torvalds {
17061da177e4SLinus Torvalds 	struct task_struct *p;
17071da177e4SLinus Torvalds 	int trace = 0;
170892476d7fSEric W. Biederman 	long nr;
17091da177e4SLinus Torvalds 
1710bdff746aSAndrew Morton 	/*
17114b9d33e6STejun Heo 	 * Determine whether and which event to report to ptracer.  When
17124b9d33e6STejun Heo 	 * called from kernel_thread or CLONE_UNTRACED is explicitly
17134b9d33e6STejun Heo 	 * requested, no event is reported; otherwise, report if the event
17144b9d33e6STejun Heo 	 * for the type of forking is enabled.
171509a05394SRoland McGrath 	 */
1716e80d6661SAl Viro 	if (!(clone_flags & CLONE_UNTRACED)) {
17174b9d33e6STejun Heo 		if (clone_flags & CLONE_VFORK)
17184b9d33e6STejun Heo 			trace = PTRACE_EVENT_VFORK;
17194b9d33e6STejun Heo 		else if ((clone_flags & CSIGNAL) != SIGCHLD)
17204b9d33e6STejun Heo 			trace = PTRACE_EVENT_CLONE;
17214b9d33e6STejun Heo 		else
17224b9d33e6STejun Heo 			trace = PTRACE_EVENT_FORK;
17234b9d33e6STejun Heo 
17244b9d33e6STejun Heo 		if (likely(!ptrace_event_enabled(current, trace)))
17254b9d33e6STejun Heo 			trace = 0;
17264b9d33e6STejun Heo 	}
17271da177e4SLinus Torvalds 
172862e791c1SAl Viro 	p = copy_process(clone_flags, stack_start, stack_size,
17293033f14aSJosh Triplett 			 child_tidptr, NULL, trace, tls);
17301da177e4SLinus Torvalds 	/*
17311da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
17321da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
17331da177e4SLinus Torvalds 	 */
17341da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
17351da177e4SLinus Torvalds 		struct completion vfork;
17364e52365fSMatthew Dempsky 		struct pid *pid;
17371da177e4SLinus Torvalds 
17380a16b607SMathieu Desnoyers 		trace_sched_process_fork(current, p);
17390a16b607SMathieu Desnoyers 
17404e52365fSMatthew Dempsky 		pid = get_task_pid(p, PIDTYPE_PID);
17414e52365fSMatthew Dempsky 		nr = pid_vnr(pid);
174230e49c26SPavel Emelyanov 
174330e49c26SPavel Emelyanov 		if (clone_flags & CLONE_PARENT_SETTID)
174430e49c26SPavel Emelyanov 			put_user(nr, parent_tidptr);
1745a6f5e063SSukadev Bhattiprolu 
17461da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
17471da177e4SLinus Torvalds 			p->vfork_done = &vfork;
17481da177e4SLinus Torvalds 			init_completion(&vfork);
1749d68b46feSOleg Nesterov 			get_task_struct(p);
17501da177e4SLinus Torvalds 		}
17511da177e4SLinus Torvalds 
17523e51e3edSSamir Bellabes 		wake_up_new_task(p);
17531da177e4SLinus Torvalds 
17544b9d33e6STejun Heo 		/* forking complete and child started to run, tell ptracer */
17554b9d33e6STejun Heo 		if (unlikely(trace))
17564e52365fSMatthew Dempsky 			ptrace_event_pid(trace, pid);
175709a05394SRoland McGrath 
17581da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
1759d68b46feSOleg Nesterov 			if (!wait_for_vfork_done(p, &vfork))
17604e52365fSMatthew Dempsky 				ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);
17619f59ce5dSChuck Ebbert 		}
17624e52365fSMatthew Dempsky 
17634e52365fSMatthew Dempsky 		put_pid(pid);
17641da177e4SLinus Torvalds 	} else {
176592476d7fSEric W. Biederman 		nr = PTR_ERR(p);
17661da177e4SLinus Torvalds 	}
176792476d7fSEric W. Biederman 	return nr;
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds 
17703033f14aSJosh Triplett #ifndef CONFIG_HAVE_COPY_THREAD_TLS
17713033f14aSJosh Triplett /* For compatibility with architectures that call do_fork directly rather than
17723033f14aSJosh Triplett  * using the syscall entry points below. */
17733033f14aSJosh Triplett long do_fork(unsigned long clone_flags,
17743033f14aSJosh Triplett 	      unsigned long stack_start,
17753033f14aSJosh Triplett 	      unsigned long stack_size,
17763033f14aSJosh Triplett 	      int __user *parent_tidptr,
17773033f14aSJosh Triplett 	      int __user *child_tidptr)
17783033f14aSJosh Triplett {
17793033f14aSJosh Triplett 	return _do_fork(clone_flags, stack_start, stack_size,
17803033f14aSJosh Triplett 			parent_tidptr, child_tidptr, 0);
17813033f14aSJosh Triplett }
17823033f14aSJosh Triplett #endif
17833033f14aSJosh Triplett 
17842aa3a7f8SAl Viro /*
17852aa3a7f8SAl Viro  * Create a kernel thread.
17862aa3a7f8SAl Viro  */
17872aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
17882aa3a7f8SAl Viro {
17893033f14aSJosh Triplett 	return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
17903033f14aSJosh Triplett 		(unsigned long)arg, NULL, NULL, 0);
17912aa3a7f8SAl Viro }
17922aa3a7f8SAl Viro 
1793d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK
1794d2125043SAl Viro SYSCALL_DEFINE0(fork)
1795d2125043SAl Viro {
1796d2125043SAl Viro #ifdef CONFIG_MMU
17973033f14aSJosh Triplett 	return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0);
1798d2125043SAl Viro #else
1799d2125043SAl Viro 	/* can not support in nommu mode */
18005d59e182SDaeseok Youn 	return -EINVAL;
1801d2125043SAl Viro #endif
1802d2125043SAl Viro }
1803d2125043SAl Viro #endif
1804d2125043SAl Viro 
1805d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK
1806d2125043SAl Viro SYSCALL_DEFINE0(vfork)
1807d2125043SAl Viro {
18083033f14aSJosh Triplett 	return _do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0,
18093033f14aSJosh Triplett 			0, NULL, NULL, 0);
1810d2125043SAl Viro }
1811d2125043SAl Viro #endif
1812d2125043SAl Viro 
1813d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE
1814d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS
1815d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
1816d2125043SAl Viro 		 int __user *, parent_tidptr,
18173033f14aSJosh Triplett 		 unsigned long, tls,
1818d2125043SAl Viro 		 int __user *, child_tidptr)
1819d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2)
1820d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
1821d2125043SAl Viro 		 int __user *, parent_tidptr,
1822d2125043SAl Viro 		 int __user *, child_tidptr,
18233033f14aSJosh Triplett 		 unsigned long, tls)
1824dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3)
1825dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
1826dfa9771aSMichal Simek 		int, stack_size,
1827dfa9771aSMichal Simek 		int __user *, parent_tidptr,
1828dfa9771aSMichal Simek 		int __user *, child_tidptr,
18293033f14aSJosh Triplett 		unsigned long, tls)
1830d2125043SAl Viro #else
1831d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
1832d2125043SAl Viro 		 int __user *, parent_tidptr,
1833d2125043SAl Viro 		 int __user *, child_tidptr,
18343033f14aSJosh Triplett 		 unsigned long, tls)
1835d2125043SAl Viro #endif
1836d2125043SAl Viro {
18373033f14aSJosh Triplett 	return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls);
1838d2125043SAl Viro }
1839d2125043SAl Viro #endif
1840d2125043SAl Viro 
18415fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN
18425fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0
18435fd63b30SRavikiran G Thirumalai #endif
18445fd63b30SRavikiran G Thirumalai 
184551cc5068SAlexey Dobriyan static void sighand_ctor(void *data)
1846aa1757f9SOleg Nesterov {
1847aa1757f9SOleg Nesterov 	struct sighand_struct *sighand = data;
1848aa1757f9SOleg Nesterov 
1849aa1757f9SOleg Nesterov 	spin_lock_init(&sighand->siglock);
1850b8fceee1SDavide Libenzi 	init_waitqueue_head(&sighand->signalfd_wqh);
1851fba2afaaSDavide Libenzi }
1852aa1757f9SOleg Nesterov 
18531da177e4SLinus Torvalds void __init proc_caches_init(void)
18541da177e4SLinus Torvalds {
18551da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
18561da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
18572dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
18585d097056SVladimir Davydov 			SLAB_NOTRACK|SLAB_ACCOUNT, sighand_ctor);
18591da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
18601da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
18615d097056SVladimir Davydov 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT,
18625d097056SVladimir Davydov 			NULL);
18631da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
18641da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
18655d097056SVladimir Davydov 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT,
18665d097056SVladimir Davydov 			NULL);
18671da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
18681da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
18695d097056SVladimir Davydov 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT,
18705d097056SVladimir Davydov 			NULL);
18716345d24dSLinus Torvalds 	/*
18726345d24dSLinus Torvalds 	 * FIXME! The "sizeof(struct mm_struct)" currently includes the
18736345d24dSLinus Torvalds 	 * whole struct cpumask for the OFFSTACK case. We could change
18746345d24dSLinus Torvalds 	 * this to *only* allocate as much of it as required by the
18756345d24dSLinus Torvalds 	 * maximum number of CPU's we can ever have.  The cpumask_allocation
18766345d24dSLinus Torvalds 	 * is at the end of the structure, exactly for that reason.
18776345d24dSLinus Torvalds 	 */
18781da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
18795fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
18805d097056SVladimir Davydov 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT,
18815d097056SVladimir Davydov 			NULL);
18825d097056SVladimir Davydov 	vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT);
18838feae131SDavid Howells 	mmap_init();
188466577193SAl Viro 	nsproxy_cache_init();
18851da177e4SLinus Torvalds }
1886cf2e340fSJANAK DESAI 
1887cf2e340fSJANAK DESAI /*
18889bfb23fcSOleg Nesterov  * Check constraints on flags passed to the unshare system call.
1889cf2e340fSJANAK DESAI  */
18909bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags)
1891cf2e340fSJANAK DESAI {
18929bfb23fcSOleg Nesterov 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
18939bfb23fcSOleg Nesterov 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
189450804fe3SEric W. Biederman 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
1895b2e0d987SEric W. Biederman 				CLONE_NEWUSER|CLONE_NEWPID))
1896cf2e340fSJANAK DESAI 		return -EINVAL;
18979bfb23fcSOleg Nesterov 	/*
189812c641abSEric W. Biederman 	 * Not implemented, but pretend it works if there is nothing
189912c641abSEric W. Biederman 	 * to unshare.  Note that unsharing the address space or the
190012c641abSEric W. Biederman 	 * signal handlers also need to unshare the signal queues (aka
190112c641abSEric W. Biederman 	 * CLONE_THREAD).
19029bfb23fcSOleg Nesterov 	 */
19039bfb23fcSOleg Nesterov 	if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
190412c641abSEric W. Biederman 		if (!thread_group_empty(current))
190512c641abSEric W. Biederman 			return -EINVAL;
190612c641abSEric W. Biederman 	}
190712c641abSEric W. Biederman 	if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {
190812c641abSEric W. Biederman 		if (atomic_read(&current->sighand->count) > 1)
190912c641abSEric W. Biederman 			return -EINVAL;
191012c641abSEric W. Biederman 	}
191112c641abSEric W. Biederman 	if (unshare_flags & CLONE_VM) {
191212c641abSEric W. Biederman 		if (!current_is_single_threaded())
19139bfb23fcSOleg Nesterov 			return -EINVAL;
19149bfb23fcSOleg Nesterov 	}
1915cf2e340fSJANAK DESAI 
1916cf2e340fSJANAK DESAI 	return 0;
1917cf2e340fSJANAK DESAI }
1918cf2e340fSJANAK DESAI 
1919cf2e340fSJANAK DESAI /*
192099d1419dSJANAK DESAI  * Unshare the filesystem structure if it is being shared
1921cf2e340fSJANAK DESAI  */
1922cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1923cf2e340fSJANAK DESAI {
1924cf2e340fSJANAK DESAI 	struct fs_struct *fs = current->fs;
1925cf2e340fSJANAK DESAI 
1926498052bbSAl Viro 	if (!(unshare_flags & CLONE_FS) || !fs)
1927498052bbSAl Viro 		return 0;
1928498052bbSAl Viro 
1929498052bbSAl Viro 	/* don't need lock here; in the worst case we'll do useless copy */
1930498052bbSAl Viro 	if (fs->users == 1)
1931498052bbSAl Viro 		return 0;
1932498052bbSAl Viro 
1933498052bbSAl Viro 	*new_fsp = copy_fs_struct(fs);
193499d1419dSJANAK DESAI 	if (!*new_fsp)
193599d1419dSJANAK DESAI 		return -ENOMEM;
1936cf2e340fSJANAK DESAI 
1937cf2e340fSJANAK DESAI 	return 0;
1938cf2e340fSJANAK DESAI }
1939cf2e340fSJANAK DESAI 
1940cf2e340fSJANAK DESAI /*
1941a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1942cf2e340fSJANAK DESAI  */
1943cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1944cf2e340fSJANAK DESAI {
1945cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1946a016f338SJANAK DESAI 	int error = 0;
1947cf2e340fSJANAK DESAI 
1948cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1949a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1950a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1951a016f338SJANAK DESAI 		if (!*new_fdp)
1952a016f338SJANAK DESAI 			return error;
1953a016f338SJANAK DESAI 	}
1954cf2e340fSJANAK DESAI 
1955cf2e340fSJANAK DESAI 	return 0;
1956cf2e340fSJANAK DESAI }
1957cf2e340fSJANAK DESAI 
1958cf2e340fSJANAK DESAI /*
1959cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1960cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1961cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1962cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1963cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1964cf2e340fSJANAK DESAI  * task_struct.
1965cf2e340fSJANAK DESAI  */
19666559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1967cf2e340fSJANAK DESAI {
1968cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1969cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1970b2e0d987SEric W. Biederman 	struct cred *new_cred = NULL;
1971cf7b708cSPavel Emelyanov 	struct nsproxy *new_nsproxy = NULL;
19729edff4abSManfred Spraul 	int do_sysvsem = 0;
19739bfb23fcSOleg Nesterov 	int err;
1974cf2e340fSJANAK DESAI 
197550804fe3SEric W. Biederman 	/*
1976faf00da5SEric W. Biederman 	 * If unsharing a user namespace must also unshare the thread group
1977faf00da5SEric W. Biederman 	 * and unshare the filesystem root and working directories.
1978b2e0d987SEric W. Biederman 	 */
1979b2e0d987SEric W. Biederman 	if (unshare_flags & CLONE_NEWUSER)
1980e66eded8SEric W. Biederman 		unshare_flags |= CLONE_THREAD | CLONE_FS;
1981b2e0d987SEric W. Biederman 	/*
198250804fe3SEric W. Biederman 	 * If unsharing vm, must also unshare signal handlers.
198350804fe3SEric W. Biederman 	 */
198450804fe3SEric W. Biederman 	if (unshare_flags & CLONE_VM)
198550804fe3SEric W. Biederman 		unshare_flags |= CLONE_SIGHAND;
19866013f67fSManfred Spraul 	/*
198712c641abSEric W. Biederman 	 * If unsharing a signal handlers, must also unshare the signal queues.
198812c641abSEric W. Biederman 	 */
198912c641abSEric W. Biederman 	if (unshare_flags & CLONE_SIGHAND)
199012c641abSEric W. Biederman 		unshare_flags |= CLONE_THREAD;
199112c641abSEric W. Biederman 	/*
19929bfb23fcSOleg Nesterov 	 * If unsharing namespace, must also unshare filesystem information.
19939bfb23fcSOleg Nesterov 	 */
19949bfb23fcSOleg Nesterov 	if (unshare_flags & CLONE_NEWNS)
19959bfb23fcSOleg Nesterov 		unshare_flags |= CLONE_FS;
199650804fe3SEric W. Biederman 
199750804fe3SEric W. Biederman 	err = check_unshare_flags(unshare_flags);
199850804fe3SEric W. Biederman 	if (err)
199950804fe3SEric W. Biederman 		goto bad_unshare_out;
20009bfb23fcSOleg Nesterov 	/*
20016013f67fSManfred Spraul 	 * CLONE_NEWIPC must also detach from the undolist: after switching
20026013f67fSManfred Spraul 	 * to a new ipc namespace, the semaphore arrays from the old
20036013f67fSManfred Spraul 	 * namespace are unreachable.
20046013f67fSManfred Spraul 	 */
20056013f67fSManfred Spraul 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
20069edff4abSManfred Spraul 		do_sysvsem = 1;
2007fb0a685cSDaniel Rebelo de Oliveira 	err = unshare_fs(unshare_flags, &new_fs);
2008fb0a685cSDaniel Rebelo de Oliveira 	if (err)
20099bfb23fcSOleg Nesterov 		goto bad_unshare_out;
2010fb0a685cSDaniel Rebelo de Oliveira 	err = unshare_fd(unshare_flags, &new_fd);
2011fb0a685cSDaniel Rebelo de Oliveira 	if (err)
20129bfb23fcSOleg Nesterov 		goto bad_unshare_cleanup_fs;
2013b2e0d987SEric W. Biederman 	err = unshare_userns(unshare_flags, &new_cred);
2014fb0a685cSDaniel Rebelo de Oliveira 	if (err)
20159edff4abSManfred Spraul 		goto bad_unshare_cleanup_fd;
2016b2e0d987SEric W. Biederman 	err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
2017b2e0d987SEric W. Biederman 					 new_cred, new_fs);
2018b2e0d987SEric W. Biederman 	if (err)
2019b2e0d987SEric W. Biederman 		goto bad_unshare_cleanup_cred;
2020cf2e340fSJANAK DESAI 
2021b2e0d987SEric W. Biederman 	if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
20229edff4abSManfred Spraul 		if (do_sysvsem) {
20239edff4abSManfred Spraul 			/*
20249edff4abSManfred Spraul 			 * CLONE_SYSVSEM is equivalent to sys_exit().
20259edff4abSManfred Spraul 			 */
20269edff4abSManfred Spraul 			exit_sem(current);
20279edff4abSManfred Spraul 		}
2028ab602f79SJack Miller 		if (unshare_flags & CLONE_NEWIPC) {
2029ab602f79SJack Miller 			/* Orphan segments in old ns (see sem above). */
2030ab602f79SJack Miller 			exit_shm(current);
2031ab602f79SJack Miller 			shm_init_task(current);
2032ab602f79SJack Miller 		}
2033ab516013SSerge E. Hallyn 
20346f977e6bSAlan Cox 		if (new_nsproxy)
2035cf7b708cSPavel Emelyanov 			switch_task_namespaces(current, new_nsproxy);
2036cf2e340fSJANAK DESAI 
2037cf7b708cSPavel Emelyanov 		task_lock(current);
2038cf7b708cSPavel Emelyanov 
2039cf2e340fSJANAK DESAI 		if (new_fs) {
2040cf2e340fSJANAK DESAI 			fs = current->fs;
20412a4419b5SNick Piggin 			spin_lock(&fs->lock);
2042cf2e340fSJANAK DESAI 			current->fs = new_fs;
2043498052bbSAl Viro 			if (--fs->users)
2044498052bbSAl Viro 				new_fs = NULL;
2045498052bbSAl Viro 			else
2046cf2e340fSJANAK DESAI 				new_fs = fs;
20472a4419b5SNick Piggin 			spin_unlock(&fs->lock);
2048cf2e340fSJANAK DESAI 		}
2049cf2e340fSJANAK DESAI 
2050cf2e340fSJANAK DESAI 		if (new_fd) {
2051cf2e340fSJANAK DESAI 			fd = current->files;
2052cf2e340fSJANAK DESAI 			current->files = new_fd;
2053cf2e340fSJANAK DESAI 			new_fd = fd;
2054cf2e340fSJANAK DESAI 		}
2055cf2e340fSJANAK DESAI 
2056cf2e340fSJANAK DESAI 		task_unlock(current);
2057b2e0d987SEric W. Biederman 
2058b2e0d987SEric W. Biederman 		if (new_cred) {
2059b2e0d987SEric W. Biederman 			/* Install the new user namespace */
2060b2e0d987SEric W. Biederman 			commit_creds(new_cred);
2061b2e0d987SEric W. Biederman 			new_cred = NULL;
2062b2e0d987SEric W. Biederman 		}
2063cf2e340fSJANAK DESAI 	}
2064cf2e340fSJANAK DESAI 
2065b2e0d987SEric W. Biederman bad_unshare_cleanup_cred:
2066b2e0d987SEric W. Biederman 	if (new_cred)
2067b2e0d987SEric W. Biederman 		put_cred(new_cred);
2068cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
2069cf2e340fSJANAK DESAI 	if (new_fd)
2070cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
2071cf2e340fSJANAK DESAI 
2072cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
2073cf2e340fSJANAK DESAI 	if (new_fs)
2074498052bbSAl Viro 		free_fs_struct(new_fs);
2075cf2e340fSJANAK DESAI 
2076cf2e340fSJANAK DESAI bad_unshare_out:
2077cf2e340fSJANAK DESAI 	return err;
2078cf2e340fSJANAK DESAI }
20793b125388SAl Viro 
20803b125388SAl Viro /*
20813b125388SAl Viro  *	Helper to unshare the files of the current task.
20823b125388SAl Viro  *	We don't want to expose copy_files internals to
20833b125388SAl Viro  *	the exec layer of the kernel.
20843b125388SAl Viro  */
20853b125388SAl Viro 
20863b125388SAl Viro int unshare_files(struct files_struct **displaced)
20873b125388SAl Viro {
20883b125388SAl Viro 	struct task_struct *task = current;
208950704516SAl Viro 	struct files_struct *copy = NULL;
20903b125388SAl Viro 	int error;
20913b125388SAl Viro 
20923b125388SAl Viro 	error = unshare_fd(CLONE_FILES, &copy);
20933b125388SAl Viro 	if (error || !copy) {
20943b125388SAl Viro 		*displaced = NULL;
20953b125388SAl Viro 		return error;
20963b125388SAl Viro 	}
20973b125388SAl Viro 	*displaced = task->files;
20983b125388SAl Viro 	task_lock(task);
20993b125388SAl Viro 	task->files = copy;
21003b125388SAl Viro 	task_unlock(task);
21013b125388SAl Viro 	return 0;
21023b125388SAl Viro }
210316db3d3fSHeinrich Schuchardt 
210416db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write,
210516db3d3fSHeinrich Schuchardt 		       void __user *buffer, size_t *lenp, loff_t *ppos)
210616db3d3fSHeinrich Schuchardt {
210716db3d3fSHeinrich Schuchardt 	struct ctl_table t;
210816db3d3fSHeinrich Schuchardt 	int ret;
210916db3d3fSHeinrich Schuchardt 	int threads = max_threads;
211016db3d3fSHeinrich Schuchardt 	int min = MIN_THREADS;
211116db3d3fSHeinrich Schuchardt 	int max = MAX_THREADS;
211216db3d3fSHeinrich Schuchardt 
211316db3d3fSHeinrich Schuchardt 	t = *table;
211416db3d3fSHeinrich Schuchardt 	t.data = &threads;
211516db3d3fSHeinrich Schuchardt 	t.extra1 = &min;
211616db3d3fSHeinrich Schuchardt 	t.extra2 = &max;
211716db3d3fSHeinrich Schuchardt 
211816db3d3fSHeinrich Schuchardt 	ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
211916db3d3fSHeinrich Schuchardt 	if (ret || !write)
212016db3d3fSHeinrich Schuchardt 		return ret;
212116db3d3fSHeinrich Schuchardt 
212216db3d3fSHeinrich Schuchardt 	set_max_threads(threads);
212316db3d3fSHeinrich Schuchardt 
212416db3d3fSHeinrich Schuchardt 	return 0;
212516db3d3fSHeinrich Schuchardt }
2126