xref: /openbmc/linux/kernel/fork.c (revision 3864601387cf4196371e3c1897fdffa5228296f9)
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>
31ab516013SSerge E. Hallyn #include <linux/nsproxy.h>
32c59ede7bSRandy.Dunlap #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/cpu.h>
34b4f48b63SPaul Menage #include <linux/cgroup.h>
351da177e4SLinus Torvalds #include <linux/security.h>
36a1e78772SMel Gorman #include <linux/hugetlb.h>
371da177e4SLinus Torvalds #include <linux/swap.h>
381da177e4SLinus Torvalds #include <linux/syscalls.h>
391da177e4SLinus Torvalds #include <linux/jiffies.h>
4009a05394SRoland McGrath #include <linux/tracehook.h>
411da177e4SLinus Torvalds #include <linux/futex.h>
428141c7f3SLinus Torvalds #include <linux/compat.h>
43207205a2SEric Dumazet #include <linux/kthread.h>
447c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h>
45ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
461da177e4SLinus Torvalds #include <linux/ptrace.h>
471da177e4SLinus Torvalds #include <linux/mount.h>
481da177e4SLinus Torvalds #include <linux/audit.h>
4978fb7466SPavel Emelianov #include <linux/memcontrol.h>
50f201ae23SFrederic Weisbecker #include <linux/ftrace.h>
511da177e4SLinus Torvalds #include <linux/profile.h>
521da177e4SLinus Torvalds #include <linux/rmap.h>
53f8af4da3SHugh Dickins #include <linux/ksm.h>
541da177e4SLinus Torvalds #include <linux/acct.h>
558f0ab514SJay Lan #include <linux/tsacct_kern.h>
569f46080cSMatt Helsley #include <linux/cn_proc.h>
57ba96a0c8SRafael J. Wysocki #include <linux/freezer.h>
58ca74e92bSShailabh Nagar #include <linux/delayacct.h>
59ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h>
600a425405SArjan van de Ven #include <linux/random.h>
61522ed776SMiloslav Trmac #include <linux/tty.h>
62fd0928dfSJens Axboe #include <linux/blkdev.h>
635ad4e53bSAl Viro #include <linux/fs_struct.h>
647c9f8861SEric Sandeen #include <linux/magic.h>
65cdd6c482SIngo Molnar #include <linux/perf_event.h>
6642c4ab41SStanislaw Gruszka #include <linux/posix-timers.h>
678e7cac79SAvi Kivity #include <linux/user-return-notifier.h>
683d5992d2SYing Han #include <linux/oom.h>
69ba76149fSAndrea Arcangeli #include <linux/khugepaged.h>
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds #include <asm/pgtable.h>
721da177e4SLinus Torvalds #include <asm/pgalloc.h>
731da177e4SLinus Torvalds #include <asm/uaccess.h>
741da177e4SLinus Torvalds #include <asm/mmu_context.h>
751da177e4SLinus Torvalds #include <asm/cacheflush.h>
761da177e4SLinus Torvalds #include <asm/tlbflush.h>
771da177e4SLinus Torvalds 
78ad8d75ffSSteven Rostedt #include <trace/events/sched.h>
79ad8d75ffSSteven Rostedt 
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
841da177e4SLinus Torvalds int nr_threads; 		/* The idle threads do not count.. */
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
91db1466b3SPaul E. McKenney 
92db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU
93db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void)
94db1466b3SPaul E. McKenney {
95db1466b3SPaul E. McKenney 	return lockdep_is_held(&tasklist_lock);
96db1466b3SPaul E. McKenney }
97db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
98db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds int nr_processes(void)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	int cpu;
1031da177e4SLinus Torvalds 	int total = 0;
1041da177e4SLinus Torvalds 
1051d510750SIan Campbell 	for_each_possible_cpu(cpu)
1061da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	return total;
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
112504f52b5SEric Dumazet # define alloc_task_struct_node(node)		\
113504f52b5SEric Dumazet 		kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node)
114504f52b5SEric Dumazet # define free_task_struct(tsk)			\
115504f52b5SEric Dumazet 		kmem_cache_free(task_struct_cachep, (tsk))
116e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep;
1171da177e4SLinus Torvalds #endif
1181da177e4SLinus Torvalds 
119b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR
120b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
121b6a84016SEric Dumazet 						  int node)
122b69c49b7SFUJITA Tomonori {
123b69c49b7SFUJITA Tomonori #ifdef CONFIG_DEBUG_STACK_USAGE
124b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL | __GFP_ZERO;
125b69c49b7SFUJITA Tomonori #else
126b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL;
127b69c49b7SFUJITA Tomonori #endif
128b6a84016SEric Dumazet 	struct page *page = alloc_pages_node(node, mask, THREAD_SIZE_ORDER);
129b6a84016SEric Dumazet 
130b6a84016SEric Dumazet 	return page ? page_address(page) : NULL;
131b69c49b7SFUJITA Tomonori }
132b69c49b7SFUJITA Tomonori 
133b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti)
134b69c49b7SFUJITA Tomonori {
135b69c49b7SFUJITA Tomonori 	free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
136b69c49b7SFUJITA Tomonori }
137b69c49b7SFUJITA Tomonori #endif
138b69c49b7SFUJITA Tomonori 
1391da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
140e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
143e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
146e18b890bSChristoph Lameter struct kmem_cache *files_cachep;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
149e18b890bSChristoph Lameter struct kmem_cache *fs_cachep;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
152e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
155e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep;
1561da177e4SLinus Torvalds 
157c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account)
158c6a7f572SKOSAKI Motohiro {
159c6a7f572SKOSAKI Motohiro 	struct zone *zone = page_zone(virt_to_page(ti));
160c6a7f572SKOSAKI Motohiro 
161c6a7f572SKOSAKI Motohiro 	mod_zone_page_state(zone, NR_KERNEL_STACK, account);
162c6a7f572SKOSAKI Motohiro }
163c6a7f572SKOSAKI Motohiro 
1641da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1651da177e4SLinus Torvalds {
1663e26c149SPeter Zijlstra 	prop_local_destroy_single(&tsk->dirties);
167c6a7f572SKOSAKI Motohiro 	account_kernel_stack(tsk->stack, -1);
168f7e4217bSRoman Zippel 	free_thread_info(tsk->stack);
16923f78d4aSIngo Molnar 	rt_mutex_debug_task_free(tsk);
170fb52607aSFrederic Weisbecker 	ftrace_graph_exit_task(tsk);
1711da177e4SLinus Torvalds 	free_task_struct(tsk);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1741da177e4SLinus Torvalds 
175ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig)
176ea6d290cSOleg Nesterov {
17797101eb4SOleg Nesterov 	taskstats_tgid_free(sig);
1781c5354deSMike Galbraith 	sched_autogroup_exit(sig);
179ea6d290cSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
180ea6d290cSOleg Nesterov }
181ea6d290cSOleg Nesterov 
182ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig)
183ea6d290cSOleg Nesterov {
1841c5354deSMike Galbraith 	if (atomic_dec_and_test(&sig->sigcnt))
185ea6d290cSOleg Nesterov 		free_signal_struct(sig);
186ea6d290cSOleg Nesterov }
187ea6d290cSOleg Nesterov 
188158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
1891da177e4SLinus Torvalds {
190270f722dSEugene Teo 	WARN_ON(!tsk->exit_state);
1911da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1921da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1931da177e4SLinus Torvalds 
194e0e81739SDavid Howells 	exit_creds(tsk);
19535df17c5SShailabh Nagar 	delayacct_tsk_free(tsk);
196ea6d290cSOleg Nesterov 	put_signal_struct(tsk->signal);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
1991da177e4SLinus Torvalds 		free_task(tsk);
2001da177e4SLinus Torvalds }
20177c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct);
2021da177e4SLinus Torvalds 
2032adee9b3SSuresh Siddha /*
2042adee9b3SSuresh Siddha  * macro override instead of weak attribute alias, to workaround
2052adee9b3SSuresh Siddha  * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
2062adee9b3SSuresh Siddha  */
2072adee9b3SSuresh Siddha #ifndef arch_task_cache_init
2082adee9b3SSuresh Siddha #define arch_task_cache_init()
2092adee9b3SSuresh Siddha #endif
21061c4628bSSuresh Siddha 
2111da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
2141da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
2151da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
2161da177e4SLinus Torvalds #endif
2171da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
2181da177e4SLinus Torvalds 	task_struct_cachep =
2191da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
2202dff4405SVegard Nossum 			ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL);
2211da177e4SLinus Torvalds #endif
2221da177e4SLinus Torvalds 
22361c4628bSSuresh Siddha 	/* do the arch specific task caches init */
22461c4628bSSuresh Siddha 	arch_task_cache_init();
22561c4628bSSuresh Siddha 
2261da177e4SLinus Torvalds 	/*
2271da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
2281da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
2291da177e4SLinus Torvalds 	 * of memory.
2301da177e4SLinus Torvalds 	 */
2311da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	/*
2341da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
2351da177e4SLinus Torvalds 	 */
2361da177e4SLinus Torvalds 	if(max_threads < 20)
2371da177e4SLinus Torvalds 		max_threads = 20;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
2401da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
2411da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
2421da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
24561c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
24661c4628bSSuresh Siddha 					       struct task_struct *src)
24761c4628bSSuresh Siddha {
24861c4628bSSuresh Siddha 	*dst = *src;
24961c4628bSSuresh Siddha 	return 0;
25061c4628bSSuresh Siddha }
25161c4628bSSuresh Siddha 
2521da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
2531da177e4SLinus Torvalds {
2541da177e4SLinus Torvalds 	struct task_struct *tsk;
2551da177e4SLinus Torvalds 	struct thread_info *ti;
2567c9f8861SEric Sandeen 	unsigned long *stackend;
257207205a2SEric Dumazet 	int node = tsk_fork_get_node(orig);
2583e26c149SPeter Zijlstra 	int err;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	prepare_to_copy(orig);
2611da177e4SLinus Torvalds 
262504f52b5SEric Dumazet 	tsk = alloc_task_struct_node(node);
2631da177e4SLinus Torvalds 	if (!tsk)
2641da177e4SLinus Torvalds 		return NULL;
2651da177e4SLinus Torvalds 
266b6a84016SEric Dumazet 	ti = alloc_thread_info_node(tsk, node);
2671da177e4SLinus Torvalds 	if (!ti) {
2681da177e4SLinus Torvalds 		free_task_struct(tsk);
2691da177e4SLinus Torvalds 		return NULL;
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds 
27261c4628bSSuresh Siddha  	err = arch_dup_task_struct(tsk, orig);
27361c4628bSSuresh Siddha 	if (err)
27461c4628bSSuresh Siddha 		goto out;
27561c4628bSSuresh Siddha 
276f7e4217bSRoman Zippel 	tsk->stack = ti;
2773e26c149SPeter Zijlstra 
2783e26c149SPeter Zijlstra 	err = prop_local_init_single(&tsk->dirties);
27961c4628bSSuresh Siddha 	if (err)
28061c4628bSSuresh Siddha 		goto out;
2813e26c149SPeter Zijlstra 
28210ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
2838e7cac79SAvi Kivity 	clear_user_return_notifier(tsk);
284f26f9affSMike Galbraith 	clear_tsk_need_resched(tsk);
2857c9f8861SEric Sandeen 	stackend = end_of_stack(tsk);
2867c9f8861SEric Sandeen 	*stackend = STACK_END_MAGIC;	/* for overflow detection */
2871da177e4SLinus Torvalds 
2880a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
2890a425405SArjan van de Ven 	tsk->stack_canary = get_random_int();
2900a425405SArjan van de Ven #endif
2910a425405SArjan van de Ven 
2921da177e4SLinus Torvalds 	/* One for us, one for whoever does the "release_task()" (usually parent) */
2931da177e4SLinus Torvalds 	atomic_set(&tsk->usage,2);
2944b5d37acSGiancarlo Formicuccia 	atomic_set(&tsk->fs_excl, 0);
2956c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
2962056a782SJens Axboe 	tsk->btrace_seq = 0;
2976c5c9341SAlexey Dobriyan #endif
298a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
299c6a7f572SKOSAKI Motohiro 
300c6a7f572SKOSAKI Motohiro 	account_kernel_stack(ti, 1);
301c6a7f572SKOSAKI Motohiro 
3021da177e4SLinus Torvalds 	return tsk;
30361c4628bSSuresh Siddha 
30461c4628bSSuresh Siddha out:
30561c4628bSSuresh Siddha 	free_thread_info(ti);
30661c4628bSSuresh Siddha 	free_task_struct(tsk);
30761c4628bSSuresh Siddha 	return NULL;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds #ifdef CONFIG_MMU
311a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
3121da177e4SLinus Torvalds {
313297c5eeeSLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
3141da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
3151da177e4SLinus Torvalds 	int retval;
3161da177e4SLinus Torvalds 	unsigned long charge;
3171da177e4SLinus Torvalds 	struct mempolicy *pol;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
320ec8c0446SRalf Baechle 	flush_cache_dup_mm(oldmm);
321ad339451SIngo Molnar 	/*
322ad339451SIngo Molnar 	 * Not linked in yet - no deadlock potential:
323ad339451SIngo Molnar 	 */
324ad339451SIngo Molnar 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
3257ee78232SHugh Dickins 
3261da177e4SLinus Torvalds 	mm->locked_vm = 0;
3271da177e4SLinus Torvalds 	mm->mmap = NULL;
3281da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
3291da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
3301363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
3311da177e4SLinus Torvalds 	mm->map_count = 0;
33294894244SRusty Russell 	cpumask_clear(mm_cpumask(mm));
3331da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
3341da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
3351da177e4SLinus Torvalds 	rb_parent = NULL;
3361da177e4SLinus Torvalds 	pprev = &mm->mmap;
337f8af4da3SHugh Dickins 	retval = ksm_fork(mm, oldmm);
338f8af4da3SHugh Dickins 	if (retval)
339f8af4da3SHugh Dickins 		goto out;
340ba76149fSAndrea Arcangeli 	retval = khugepaged_fork(mm, oldmm);
341ba76149fSAndrea Arcangeli 	if (retval)
342ba76149fSAndrea Arcangeli 		goto out;
3431da177e4SLinus Torvalds 
344297c5eeeSLinus Torvalds 	prev = NULL;
345fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
3461da177e4SLinus Torvalds 		struct file *file;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
3493b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
3503b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
351ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
3523b6bfcdbSHugh Dickins 								-pages);
3531da177e4SLinus Torvalds 			continue;
3541da177e4SLinus Torvalds 		}
3551da177e4SLinus Torvalds 		charge = 0;
3561da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
3571da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
3581da177e4SLinus Torvalds 			if (security_vm_enough_memory(len))
3591da177e4SLinus Torvalds 				goto fail_nomem;
3601da177e4SLinus Torvalds 			charge = len;
3611da177e4SLinus Torvalds 		}
362e94b1766SChristoph Lameter 		tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
3631da177e4SLinus Torvalds 		if (!tmp)
3641da177e4SLinus Torvalds 			goto fail_nomem;
3651da177e4SLinus Torvalds 		*tmp = *mpnt;
3665beb4930SRik van Riel 		INIT_LIST_HEAD(&tmp->anon_vma_chain);
367846a16bfSLee Schermerhorn 		pol = mpol_dup(vma_policy(mpnt));
3681da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
3691da177e4SLinus Torvalds 		if (IS_ERR(pol))
3701da177e4SLinus Torvalds 			goto fail_nomem_policy;
3711da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
372a247c3a9SAndrea Arcangeli 		tmp->vm_mm = mm;
3735beb4930SRik van Riel 		if (anon_vma_fork(tmp, mpnt))
3745beb4930SRik van Riel 			goto fail_nomem_anon_vma_fork;
3751da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
376297c5eeeSLinus Torvalds 		tmp->vm_next = tmp->vm_prev = NULL;
3771da177e4SLinus Torvalds 		file = tmp->vm_file;
3781da177e4SLinus Torvalds 		if (file) {
379f3a43f3fSJosef "Jeff" Sipek 			struct inode *inode = file->f_path.dentry->d_inode;
380b88ed205SHugh Dickins 			struct address_space *mapping = file->f_mapping;
381b88ed205SHugh Dickins 
3821da177e4SLinus Torvalds 			get_file(file);
3831da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
3841da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
3853d48ae45SPeter Zijlstra 			mutex_lock(&mapping->i_mmap_mutex);
386b88ed205SHugh Dickins 			if (tmp->vm_flags & VM_SHARED)
387b88ed205SHugh Dickins 				mapping->i_mmap_writable++;
388b88ed205SHugh Dickins 			flush_dcache_mmap_lock(mapping);
389b88ed205SHugh Dickins 			/* insert tmp into the share list, just after mpnt */
3901da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
391b88ed205SHugh Dickins 			flush_dcache_mmap_unlock(mapping);
3923d48ae45SPeter Zijlstra 			mutex_unlock(&mapping->i_mmap_mutex);
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 		/*
396a1e78772SMel Gorman 		 * Clear hugetlb-related page reserves for children. This only
397a1e78772SMel Gorman 		 * affects MAP_PRIVATE mappings. Faults generated by the child
398a1e78772SMel Gorman 		 * are not guaranteed to succeed, even if read-only
399a1e78772SMel Gorman 		 */
400a1e78772SMel Gorman 		if (is_vm_hugetlb_page(tmp))
401a1e78772SMel Gorman 			reset_vma_resv_huge_pages(tmp);
402a1e78772SMel Gorman 
403a1e78772SMel Gorman 		/*
4047ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
4051da177e4SLinus Torvalds 		 */
4061da177e4SLinus Torvalds 		*pprev = tmp;
4071da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
408297c5eeeSLinus Torvalds 		tmp->vm_prev = prev;
409297c5eeeSLinus Torvalds 		prev = tmp;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
4121da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
4131da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 		mm->map_count++;
4160b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
4191da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 		if (retval)
4221da177e4SLinus Torvalds 			goto out;
4231da177e4SLinus Torvalds 	}
424d6dd61c8SJeremy Fitzhardinge 	/* a new mm has just been created */
425d6dd61c8SJeremy Fitzhardinge 	arch_dup_mmap(oldmm, mm);
4261da177e4SLinus Torvalds 	retval = 0;
4271da177e4SLinus Torvalds out:
4287ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
429fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
4301da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
4311da177e4SLinus Torvalds 	return retval;
4325beb4930SRik van Riel fail_nomem_anon_vma_fork:
4335beb4930SRik van Riel 	mpol_put(pol);
4341da177e4SLinus Torvalds fail_nomem_policy:
4351da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
4361da177e4SLinus Torvalds fail_nomem:
4371da177e4SLinus Torvalds 	retval = -ENOMEM;
4381da177e4SLinus Torvalds 	vm_unacct_memory(charge);
4391da177e4SLinus Torvalds 	goto out;
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
4451da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
4461da177e4SLinus Torvalds 		return -ENOMEM;
4471da177e4SLinus Torvalds 	return 0;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm)
4511da177e4SLinus Torvalds {
4525e541973SBenjamin Herrenschmidt 	pgd_free(mm, mm->pgd);
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds #else
4551da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
4561da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
4571da177e4SLinus Torvalds #define mm_free_pgd(mm)
4581da177e4SLinus Torvalds #endif /* CONFIG_MMU */
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
4611da177e4SLinus Torvalds 
462e94b1766SChristoph Lameter #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
4631da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
4641da177e4SLinus Torvalds 
4654cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
4664cb0e11bSHidehiro Kawai 
4674cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s)
4684cb0e11bSHidehiro Kawai {
4694cb0e11bSHidehiro Kawai 	default_dump_filter =
4704cb0e11bSHidehiro Kawai 		(simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
4714cb0e11bSHidehiro Kawai 		MMF_DUMP_FILTER_MASK;
4724cb0e11bSHidehiro Kawai 	return 1;
4734cb0e11bSHidehiro Kawai }
4744cb0e11bSHidehiro Kawai 
4754cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup);
4764cb0e11bSHidehiro Kawai 
4771da177e4SLinus Torvalds #include <linux/init_task.h>
4781da177e4SLinus Torvalds 
479858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm)
480858f0993SAlexey Dobriyan {
481858f0993SAlexey Dobriyan #ifdef CONFIG_AIO
482858f0993SAlexey Dobriyan 	spin_lock_init(&mm->ioctx_lock);
483858f0993SAlexey Dobriyan 	INIT_HLIST_HEAD(&mm->ioctx_list);
484858f0993SAlexey Dobriyan #endif
485858f0993SAlexey Dobriyan }
486858f0993SAlexey Dobriyan 
487de03c72cSKOSAKI Motohiro int mm_init_cpumask(struct mm_struct *mm, struct mm_struct *oldmm)
488de03c72cSKOSAKI Motohiro {
489de03c72cSKOSAKI Motohiro #ifdef CONFIG_CPUMASK_OFFSTACK
490de03c72cSKOSAKI Motohiro 	if (!alloc_cpumask_var(&mm->cpu_vm_mask_var, GFP_KERNEL))
491de03c72cSKOSAKI Motohiro 		return -ENOMEM;
492de03c72cSKOSAKI Motohiro 
493de03c72cSKOSAKI Motohiro 	if (oldmm)
494de03c72cSKOSAKI Motohiro 		cpumask_copy(mm_cpumask(mm), mm_cpumask(oldmm));
495de03c72cSKOSAKI Motohiro 	else
496de03c72cSKOSAKI Motohiro 		memset(mm_cpumask(mm), 0, cpumask_size());
497de03c72cSKOSAKI Motohiro #endif
498de03c72cSKOSAKI Motohiro 	return 0;
499de03c72cSKOSAKI Motohiro }
500de03c72cSKOSAKI Motohiro 
50178fb7466SPavel Emelianov static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
5041da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
5051da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
5061da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
507f8af4da3SHugh Dickins 	mm->flags = (current->mm) ?
508f8af4da3SHugh Dickins 		(current->mm->flags & MMF_INIT_MASK) : default_dump_filter;
509999d9fc1SOleg Nesterov 	mm->core_state = NULL;
5101da177e4SLinus Torvalds 	mm->nr_ptes = 0;
511d559db08SKAMEZAWA Hiroyuki 	memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
5121da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
5131da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
5141363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
515858f0993SAlexey Dobriyan 	mm_init_aio(mm);
516cf475ad2SBalbir Singh 	mm_init_owner(mm, p);
5173d5992d2SYing Han 	atomic_set(&mm->oom_disable_count, 0);
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
5201da177e4SLinus Torvalds 		mm->def_flags = 0;
521cddb8a5cSAndrea Arcangeli 		mmu_notifier_mm_init(mm);
5221da177e4SLinus Torvalds 		return mm;
5231da177e4SLinus Torvalds 	}
52478fb7466SPavel Emelianov 
5251da177e4SLinus Torvalds 	free_mm(mm);
5261da177e4SLinus Torvalds 	return NULL;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds /*
5301da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
5311da177e4SLinus Torvalds  */
5321da177e4SLinus Torvalds struct mm_struct * mm_alloc(void)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	struct mm_struct * mm;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	mm = allocate_mm();
537de03c72cSKOSAKI Motohiro 	if (!mm)
538de03c72cSKOSAKI Motohiro 		return NULL;
539de03c72cSKOSAKI Motohiro 
5401da177e4SLinus Torvalds 	memset(mm, 0, sizeof(*mm));
54178fb7466SPavel Emelianov 	mm = mm_init(mm, current);
542de03c72cSKOSAKI Motohiro 	if (!mm)
543de03c72cSKOSAKI Motohiro 		return NULL;
544de03c72cSKOSAKI Motohiro 
545de03c72cSKOSAKI Motohiro 	if (mm_init_cpumask(mm, NULL)) {
546de03c72cSKOSAKI Motohiro 		mm_free_pgd(mm);
547de03c72cSKOSAKI Motohiro 		free_mm(mm);
548de03c72cSKOSAKI Motohiro 		return NULL;
5491da177e4SLinus Torvalds 	}
550de03c72cSKOSAKI Motohiro 
5511da177e4SLinus Torvalds 	return mm;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /*
5551da177e4SLinus Torvalds  * Called when the last reference to the mm
5561da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
5571da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
5581da177e4SLinus Torvalds  */
5597ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm)
5601da177e4SLinus Torvalds {
5611da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
562de03c72cSKOSAKI Motohiro 	free_cpumask_var(mm->cpu_vm_mask_var);
5631da177e4SLinus Torvalds 	mm_free_pgd(mm);
5641da177e4SLinus Torvalds 	destroy_context(mm);
565cddb8a5cSAndrea Arcangeli 	mmu_notifier_mm_destroy(mm);
566e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE
567e7a00c45SAndrea Arcangeli 	VM_BUG_ON(mm->pmd_huge_pte);
568e7a00c45SAndrea Arcangeli #endif
5691da177e4SLinus Torvalds 	free_mm(mm);
5701da177e4SLinus Torvalds }
5716d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /*
5741da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
5751da177e4SLinus Torvalds  */
5761da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
5771da177e4SLinus Torvalds {
5780ae26f1bSAndrew Morton 	might_sleep();
5790ae26f1bSAndrew Morton 
5801da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
5811da177e4SLinus Torvalds 		exit_aio(mm);
5821c2fb7a4SAndrea Arcangeli 		ksm_exit(mm);
583ba76149fSAndrea Arcangeli 		khugepaged_exit(mm); /* must run before exit_mmap */
5841da177e4SLinus Torvalds 		exit_mmap(mm);
585925d1c40SMatt Helsley 		set_mm_exe_file(mm, NULL);
5861da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
5871da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
5881da177e4SLinus Torvalds 			list_del(&mm->mmlist);
5891da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
5901da177e4SLinus Torvalds 		}
5911da177e4SLinus Torvalds 		put_swap_token(mm);
592801460d0SHiroshi Shimamoto 		if (mm->binfmt)
593801460d0SHiroshi Shimamoto 			module_put(mm->binfmt->module);
5941da177e4SLinus Torvalds 		mmdrop(mm);
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
5981da177e4SLinus Torvalds 
599*38646013SJiri Slaby /*
600*38646013SJiri Slaby  * We added or removed a vma mapping the executable. The vmas are only mapped
601*38646013SJiri Slaby  * during exec and are not mapped with the mmap system call.
602*38646013SJiri Slaby  * Callers must hold down_write() on the mm's mmap_sem for these
603*38646013SJiri Slaby  */
604*38646013SJiri Slaby void added_exe_file_vma(struct mm_struct *mm)
605*38646013SJiri Slaby {
606*38646013SJiri Slaby 	mm->num_exe_file_vmas++;
607*38646013SJiri Slaby }
608*38646013SJiri Slaby 
609*38646013SJiri Slaby void removed_exe_file_vma(struct mm_struct *mm)
610*38646013SJiri Slaby {
611*38646013SJiri Slaby 	mm->num_exe_file_vmas--;
612*38646013SJiri Slaby 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
613*38646013SJiri Slaby 		fput(mm->exe_file);
614*38646013SJiri Slaby 		mm->exe_file = NULL;
615*38646013SJiri Slaby 	}
616*38646013SJiri Slaby 
617*38646013SJiri Slaby }
618*38646013SJiri Slaby 
619*38646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
620*38646013SJiri Slaby {
621*38646013SJiri Slaby 	if (new_exe_file)
622*38646013SJiri Slaby 		get_file(new_exe_file);
623*38646013SJiri Slaby 	if (mm->exe_file)
624*38646013SJiri Slaby 		fput(mm->exe_file);
625*38646013SJiri Slaby 	mm->exe_file = new_exe_file;
626*38646013SJiri Slaby 	mm->num_exe_file_vmas = 0;
627*38646013SJiri Slaby }
628*38646013SJiri Slaby 
629*38646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm)
630*38646013SJiri Slaby {
631*38646013SJiri Slaby 	struct file *exe_file;
632*38646013SJiri Slaby 
633*38646013SJiri Slaby 	/* We need mmap_sem to protect against races with removal of
634*38646013SJiri Slaby 	 * VM_EXECUTABLE vmas */
635*38646013SJiri Slaby 	down_read(&mm->mmap_sem);
636*38646013SJiri Slaby 	exe_file = mm->exe_file;
637*38646013SJiri Slaby 	if (exe_file)
638*38646013SJiri Slaby 		get_file(exe_file);
639*38646013SJiri Slaby 	up_read(&mm->mmap_sem);
640*38646013SJiri Slaby 	return exe_file;
641*38646013SJiri Slaby }
642*38646013SJiri Slaby 
643*38646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
644*38646013SJiri Slaby {
645*38646013SJiri Slaby 	/* It's safe to write the exe_file pointer without exe_file_lock because
646*38646013SJiri Slaby 	 * this is called during fork when the task is not yet in /proc */
647*38646013SJiri Slaby 	newmm->exe_file = get_mm_exe_file(oldmm);
648*38646013SJiri Slaby }
649*38646013SJiri Slaby 
6501da177e4SLinus Torvalds /**
6511da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
6521da177e4SLinus Torvalds  *
653246bb0b1SOleg Nesterov  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
6541da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
6551da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
6561da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
6571da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
6581da177e4SLinus Torvalds  */
6591da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	struct mm_struct *mm;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	task_lock(task);
6641da177e4SLinus Torvalds 	mm = task->mm;
6651da177e4SLinus Torvalds 	if (mm) {
666246bb0b1SOleg Nesterov 		if (task->flags & PF_KTHREAD)
6671da177e4SLinus Torvalds 			mm = NULL;
6681da177e4SLinus Torvalds 		else
6691da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 	task_unlock(task);
6721da177e4SLinus Torvalds 	return mm;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
6771da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
6781da177e4SLinus Torvalds  * error success whatever.
6791da177e4SLinus Torvalds  *
6801da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
6811da177e4SLinus Torvalds  * from the current process.
6821da177e4SLinus Torvalds  *
6831da177e4SLinus Torvalds  * This difference is important for error handling, when we
6841da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
6851da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
6861da177e4SLinus Torvalds  * restoring the old one. . .
6871da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
6881da177e4SLinus Torvalds  */
6891da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
6901da177e4SLinus Torvalds {
6911da177e4SLinus Torvalds 	struct completion *vfork_done = tsk->vfork_done;
6921da177e4SLinus Torvalds 
6938141c7f3SLinus Torvalds 	/* Get rid of any futexes when releasing the mm */
6948141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX
695fc6b177dSPeter Zijlstra 	if (unlikely(tsk->robust_list)) {
6968141c7f3SLinus Torvalds 		exit_robust_list(tsk);
697fc6b177dSPeter Zijlstra 		tsk->robust_list = NULL;
698fc6b177dSPeter Zijlstra 	}
6998141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT
700fc6b177dSPeter Zijlstra 	if (unlikely(tsk->compat_robust_list)) {
7018141c7f3SLinus Torvalds 		compat_exit_robust_list(tsk);
702fc6b177dSPeter Zijlstra 		tsk->compat_robust_list = NULL;
703fc6b177dSPeter Zijlstra 	}
7048141c7f3SLinus Torvalds #endif
705322a2c10SThomas Gleixner 	if (unlikely(!list_empty(&tsk->pi_state_list)))
706322a2c10SThomas Gleixner 		exit_pi_state_list(tsk);
7078141c7f3SLinus Torvalds #endif
7088141c7f3SLinus Torvalds 
7091da177e4SLinus Torvalds 	/* Get rid of any cached register state */
7101da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/* notify parent sleeping on vfork() */
7131da177e4SLinus Torvalds 	if (vfork_done) {
7141da177e4SLinus Torvalds 		tsk->vfork_done = NULL;
7151da177e4SLinus Torvalds 		complete(vfork_done);
7161da177e4SLinus Torvalds 	}
717fec1d011SRoland McGrath 
718fec1d011SRoland McGrath 	/*
719fec1d011SRoland McGrath 	 * If we're exiting normally, clear a user-space tid field if
720fec1d011SRoland McGrath 	 * requested.  We leave this alone when dying by signal, to leave
721fec1d011SRoland McGrath 	 * the value intact in a core dump, and to save the unnecessary
722fec1d011SRoland McGrath 	 * trouble otherwise.  Userland only wants this done for a sys_exit.
723fec1d011SRoland McGrath 	 */
7249c8a8228SEric Dumazet 	if (tsk->clear_child_tid) {
7259c8a8228SEric Dumazet 		if (!(tsk->flags & PF_SIGNALED) &&
7269c8a8228SEric Dumazet 		    atomic_read(&mm->mm_users) > 1) {
7271da177e4SLinus Torvalds 			/*
7281da177e4SLinus Torvalds 			 * We don't check the error code - if userspace has
7291da177e4SLinus Torvalds 			 * not set up a proper pointer then tough luck.
7301da177e4SLinus Torvalds 			 */
7319c8a8228SEric Dumazet 			put_user(0, tsk->clear_child_tid);
7329c8a8228SEric Dumazet 			sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
7339c8a8228SEric Dumazet 					1, NULL, NULL, 0);
7349c8a8228SEric Dumazet 		}
7359c8a8228SEric Dumazet 		tsk->clear_child_tid = NULL;
7361da177e4SLinus Torvalds 	}
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
739a0a7ec30SJANAK DESAI /*
740a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
741a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
742a0a7ec30SJANAK DESAI  */
743402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk)
744a0a7ec30SJANAK DESAI {
745a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
746a0a7ec30SJANAK DESAI 	int err;
747a0a7ec30SJANAK DESAI 
748a0a7ec30SJANAK DESAI 	if (!oldmm)
749a0a7ec30SJANAK DESAI 		return NULL;
750a0a7ec30SJANAK DESAI 
751a0a7ec30SJANAK DESAI 	mm = allocate_mm();
752a0a7ec30SJANAK DESAI 	if (!mm)
753a0a7ec30SJANAK DESAI 		goto fail_nomem;
754a0a7ec30SJANAK DESAI 
755a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
756a0a7ec30SJANAK DESAI 
7577602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
7587602bdf2SAshwin Chaugule 	mm->token_priority = 0;
7597602bdf2SAshwin Chaugule 	mm->last_interval = 0;
7607602bdf2SAshwin Chaugule 
761e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE
762e7a00c45SAndrea Arcangeli 	mm->pmd_huge_pte = NULL;
763e7a00c45SAndrea Arcangeli #endif
764e7a00c45SAndrea Arcangeli 
76578fb7466SPavel Emelianov 	if (!mm_init(mm, tsk))
766a0a7ec30SJANAK DESAI 		goto fail_nomem;
767a0a7ec30SJANAK DESAI 
768de03c72cSKOSAKI Motohiro 	if (mm_init_cpumask(mm, oldmm))
769de03c72cSKOSAKI Motohiro 		goto fail_nocpumask;
770de03c72cSKOSAKI Motohiro 
771a0a7ec30SJANAK DESAI 	if (init_new_context(tsk, mm))
772a0a7ec30SJANAK DESAI 		goto fail_nocontext;
773a0a7ec30SJANAK DESAI 
774925d1c40SMatt Helsley 	dup_mm_exe_file(oldmm, mm);
775925d1c40SMatt Helsley 
776a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
777a0a7ec30SJANAK DESAI 	if (err)
778a0a7ec30SJANAK DESAI 		goto free_pt;
779a0a7ec30SJANAK DESAI 
780a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
781a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
782a0a7ec30SJANAK DESAI 
783801460d0SHiroshi Shimamoto 	if (mm->binfmt && !try_module_get(mm->binfmt->module))
784801460d0SHiroshi Shimamoto 		goto free_pt;
785801460d0SHiroshi Shimamoto 
786a0a7ec30SJANAK DESAI 	return mm;
787a0a7ec30SJANAK DESAI 
788a0a7ec30SJANAK DESAI free_pt:
789801460d0SHiroshi Shimamoto 	/* don't put binfmt in mmput, we haven't got module yet */
790801460d0SHiroshi Shimamoto 	mm->binfmt = NULL;
791a0a7ec30SJANAK DESAI 	mmput(mm);
792a0a7ec30SJANAK DESAI 
793a0a7ec30SJANAK DESAI fail_nomem:
794a0a7ec30SJANAK DESAI 	return NULL;
795a0a7ec30SJANAK DESAI 
796a0a7ec30SJANAK DESAI fail_nocontext:
797de03c72cSKOSAKI Motohiro 	free_cpumask_var(mm->cpu_vm_mask_var);
798de03c72cSKOSAKI Motohiro 
799de03c72cSKOSAKI Motohiro fail_nocpumask:
800a0a7ec30SJANAK DESAI 	/*
801a0a7ec30SJANAK DESAI 	 * If init_new_context() failed, we cannot use mmput() to free the mm
802a0a7ec30SJANAK DESAI 	 * because it calls destroy_context()
803a0a7ec30SJANAK DESAI 	 */
804a0a7ec30SJANAK DESAI 	mm_free_pgd(mm);
805a0a7ec30SJANAK DESAI 	free_mm(mm);
806a0a7ec30SJANAK DESAI 	return NULL;
807a0a7ec30SJANAK DESAI }
808a0a7ec30SJANAK DESAI 
8091da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	struct mm_struct * mm, *oldmm;
8121da177e4SLinus Torvalds 	int retval;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
8151da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
81617406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
81717406b82SMandeep Singh Baines 	tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
81817406b82SMandeep Singh Baines #endif
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	tsk->mm = NULL;
8211da177e4SLinus Torvalds 	tsk->active_mm = NULL;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	/*
8241da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
8251da177e4SLinus Torvalds 	 *
8261da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
8271da177e4SLinus Torvalds 	 */
8281da177e4SLinus Torvalds 	oldmm = current->mm;
8291da177e4SLinus Torvalds 	if (!oldmm)
8301da177e4SLinus Torvalds 		return 0;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
8331da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
8341da177e4SLinus Torvalds 		mm = oldmm;
8351da177e4SLinus Torvalds 		goto good_mm;
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	retval = -ENOMEM;
839a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
8401da177e4SLinus Torvalds 	if (!mm)
8411da177e4SLinus Torvalds 		goto fail_nomem;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds good_mm:
8447602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
8457602bdf2SAshwin Chaugule 	mm->token_priority = 0;
8467602bdf2SAshwin Chaugule 	mm->last_interval = 0;
8473d5992d2SYing Han 	if (tsk->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
8483d5992d2SYing Han 		atomic_inc(&mm->oom_disable_count);
8497602bdf2SAshwin Chaugule 
8501da177e4SLinus Torvalds 	tsk->mm = mm;
8511da177e4SLinus Torvalds 	tsk->active_mm = mm;
8521da177e4SLinus Torvalds 	return 0;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds fail_nomem:
8551da177e4SLinus Torvalds 	return retval;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
858a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
8591da177e4SLinus Torvalds {
860498052bbSAl Viro 	struct fs_struct *fs = current->fs;
8611da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
862498052bbSAl Viro 		/* tsk->fs is already what we want */
8632a4419b5SNick Piggin 		spin_lock(&fs->lock);
864498052bbSAl Viro 		if (fs->in_exec) {
8652a4419b5SNick Piggin 			spin_unlock(&fs->lock);
866498052bbSAl Viro 			return -EAGAIN;
867498052bbSAl Viro 		}
868498052bbSAl Viro 		fs->users++;
8692a4419b5SNick Piggin 		spin_unlock(&fs->lock);
8701da177e4SLinus Torvalds 		return 0;
8711da177e4SLinus Torvalds 	}
872498052bbSAl Viro 	tsk->fs = copy_fs_struct(fs);
8731da177e4SLinus Torvalds 	if (!tsk->fs)
8741da177e4SLinus Torvalds 		return -ENOMEM;
8751da177e4SLinus Torvalds 	return 0;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
878a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
879a016f338SJANAK DESAI {
880a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
881a016f338SJANAK DESAI 	int error = 0;
882a016f338SJANAK DESAI 
883a016f338SJANAK DESAI 	/*
884a016f338SJANAK DESAI 	 * A background process may not have any files ...
885a016f338SJANAK DESAI 	 */
886a016f338SJANAK DESAI 	oldf = current->files;
887a016f338SJANAK DESAI 	if (!oldf)
888a016f338SJANAK DESAI 		goto out;
889a016f338SJANAK DESAI 
890a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
891a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
892a016f338SJANAK DESAI 		goto out;
893a016f338SJANAK DESAI 	}
894a016f338SJANAK DESAI 
895a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
896a016f338SJANAK DESAI 	if (!newf)
897a016f338SJANAK DESAI 		goto out;
898a016f338SJANAK DESAI 
899a016f338SJANAK DESAI 	tsk->files = newf;
900a016f338SJANAK DESAI 	error = 0;
901a016f338SJANAK DESAI out:
902a016f338SJANAK DESAI 	return error;
903a016f338SJANAK DESAI }
904a016f338SJANAK DESAI 
905fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
906fd0928dfSJens Axboe {
907fd0928dfSJens Axboe #ifdef CONFIG_BLOCK
908fd0928dfSJens Axboe 	struct io_context *ioc = current->io_context;
909fd0928dfSJens Axboe 
910fd0928dfSJens Axboe 	if (!ioc)
911fd0928dfSJens Axboe 		return 0;
912fadad878SJens Axboe 	/*
913fadad878SJens Axboe 	 * Share io context with parent, if CLONE_IO is set
914fadad878SJens Axboe 	 */
915fadad878SJens Axboe 	if (clone_flags & CLONE_IO) {
916fadad878SJens Axboe 		tsk->io_context = ioc_task_link(ioc);
917fadad878SJens Axboe 		if (unlikely(!tsk->io_context))
918fadad878SJens Axboe 			return -ENOMEM;
919fadad878SJens Axboe 	} else if (ioprio_valid(ioc->ioprio)) {
920fd0928dfSJens Axboe 		tsk->io_context = alloc_io_context(GFP_KERNEL, -1);
921fd0928dfSJens Axboe 		if (unlikely(!tsk->io_context))
922fd0928dfSJens Axboe 			return -ENOMEM;
923fd0928dfSJens Axboe 
924fd0928dfSJens Axboe 		tsk->io_context->ioprio = ioc->ioprio;
925fd0928dfSJens Axboe 	}
926fd0928dfSJens Axboe #endif
927fd0928dfSJens Axboe 	return 0;
928fd0928dfSJens Axboe }
929fd0928dfSJens Axboe 
930a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
9311da177e4SLinus Torvalds {
9321da177e4SLinus Torvalds 	struct sighand_struct *sig;
9331da177e4SLinus Torvalds 
93460348802SZhaolei 	if (clone_flags & CLONE_SIGHAND) {
9351da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
9361da177e4SLinus Torvalds 		return 0;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
939e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
9401da177e4SLinus Torvalds 	if (!sig)
9411da177e4SLinus Torvalds 		return -ENOMEM;
9421da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
9431da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
9441da177e4SLinus Torvalds 	return 0;
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
947a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
948c81addc9SOleg Nesterov {
949c81addc9SOleg Nesterov 	if (atomic_dec_and_test(&sighand->count))
950c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
951c81addc9SOleg Nesterov }
952c81addc9SOleg Nesterov 
953f06febc9SFrank Mayhar 
954f06febc9SFrank Mayhar /*
955f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a thread group.
956f06febc9SFrank Mayhar  */
957f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig)
958f06febc9SFrank Mayhar {
95978d7d407SJiri Slaby 	unsigned long cpu_limit;
96078d7d407SJiri Slaby 
961f06febc9SFrank Mayhar 	/* Thread group counters. */
962f06febc9SFrank Mayhar 	thread_group_cputime_init(sig);
963f06febc9SFrank Mayhar 
96478d7d407SJiri Slaby 	cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
96578d7d407SJiri Slaby 	if (cpu_limit != RLIM_INFINITY) {
96678d7d407SJiri Slaby 		sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
9676279a751SOleg Nesterov 		sig->cputimer.running = 1;
9686279a751SOleg Nesterov 	}
9696279a751SOleg Nesterov 
970f06febc9SFrank Mayhar 	/* The timer lists. */
971f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
972f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
973f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
974f06febc9SFrank Mayhar }
975f06febc9SFrank Mayhar 
976a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
9771da177e4SLinus Torvalds {
9781da177e4SLinus Torvalds 	struct signal_struct *sig;
9791da177e4SLinus Torvalds 
9804ab6c083SOleg Nesterov 	if (clone_flags & CLONE_THREAD)
981490dea45SPeter Zijlstra 		return 0;
9826279a751SOleg Nesterov 
983a56704efSVeaceslav Falico 	sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
9841da177e4SLinus Torvalds 	tsk->signal = sig;
9851da177e4SLinus Torvalds 	if (!sig)
9861da177e4SLinus Torvalds 		return -ENOMEM;
9871da177e4SLinus Torvalds 
988b3ac022cSOleg Nesterov 	sig->nr_threads = 1;
9891da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
990b3ac022cSOleg Nesterov 	atomic_set(&sig->sigcnt, 1);
9911da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
992b3bfa0cbSSukadev Bhattiprolu 	if (clone_flags & CLONE_NEWPID)
993b3bfa0cbSSukadev Bhattiprolu 		sig->flags |= SIGNAL_UNKILLABLE;
994db51aeccSOleg Nesterov 	sig->curr_target = tsk;
9951da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
9961da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
9971da177e4SLinus Torvalds 
998c9cb2e3dSThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
9991da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	task_lock(current->group_leader);
10021da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
10031da177e4SLinus Torvalds 	task_unlock(current->group_leader);
10041da177e4SLinus Torvalds 
10056279a751SOleg Nesterov 	posix_cpu_timers_init_group(sig);
10066279a751SOleg Nesterov 
1007522ed776SMiloslav Trmac 	tty_audit_fork(sig);
10085091faa4SMike Galbraith 	sched_autogroup_fork(sig);
1009522ed776SMiloslav Trmac 
10104714d1d3SBen Blum #ifdef CONFIG_CGROUPS
10114714d1d3SBen Blum 	init_rwsem(&sig->threadgroup_fork_lock);
10124714d1d3SBen Blum #endif
10134714d1d3SBen Blum 
101428b83c51SKOSAKI Motohiro 	sig->oom_adj = current->signal->oom_adj;
1015a63d83f4SDavid Rientjes 	sig->oom_score_adj = current->signal->oom_score_adj;
1016dabb16f6SMandeep Singh Baines 	sig->oom_score_adj_min = current->signal->oom_score_adj_min;
101728b83c51SKOSAKI Motohiro 
10189b1bf12dSKOSAKI Motohiro 	mutex_init(&sig->cred_guard_mutex);
10199b1bf12dSKOSAKI Motohiro 
10201da177e4SLinus Torvalds 	return 0;
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
1023a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
10261da177e4SLinus Torvalds 
102721aa9af0STejun Heo 	new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
10281da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
102909a05394SRoland McGrath 	new_flags |= PF_STARTING;
10301da177e4SLinus Torvalds 	p->flags = new_flags;
10312e131895SRafael J. Wysocki 	clear_freeze_flag(p);
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds 
103417da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
10351da177e4SLinus Torvalds {
10361da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
10371da177e4SLinus Torvalds 
1038b488893aSPavel Emelyanov 	return task_pid_vnr(current);
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
1041a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p)
104223f78d4aSIngo Molnar {
10431d615482SThomas Gleixner 	raw_spin_lock_init(&p->pi_lock);
1044e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES
10451d615482SThomas Gleixner 	plist_head_init_raw(&p->pi_waiters, &p->pi_lock);
104623f78d4aSIngo Molnar 	p->pi_blocked_on = NULL;
104723f78d4aSIngo Molnar #endif
104823f78d4aSIngo Molnar }
104923f78d4aSIngo Molnar 
1050cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER
1051cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
1052cf475ad2SBalbir Singh {
1053cf475ad2SBalbir Singh 	mm->owner = p;
1054cf475ad2SBalbir Singh }
1055cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */
1056cf475ad2SBalbir Singh 
10571da177e4SLinus Torvalds /*
1058f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a single task.
1059f06febc9SFrank Mayhar  */
1060f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk)
1061f06febc9SFrank Mayhar {
1062f06febc9SFrank Mayhar 	tsk->cputime_expires.prof_exp = cputime_zero;
1063f06febc9SFrank Mayhar 	tsk->cputime_expires.virt_exp = cputime_zero;
1064f06febc9SFrank Mayhar 	tsk->cputime_expires.sched_exp = 0;
1065f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[0]);
1066f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[1]);
1067f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[2]);
1068f06febc9SFrank Mayhar }
1069f06febc9SFrank Mayhar 
1070f06febc9SFrank Mayhar /*
10711da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
10721da177e4SLinus Torvalds  * but does not actually start it yet.
10731da177e4SLinus Torvalds  *
10741da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
10751da177e4SLinus Torvalds  * parts of the process environment (as per the clone
10761da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
10771da177e4SLinus Torvalds  */
107836c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags,
10791da177e4SLinus Torvalds 					unsigned long stack_start,
10801da177e4SLinus Torvalds 					struct pt_regs *regs,
10811da177e4SLinus Torvalds 					unsigned long stack_size,
10821da177e4SLinus Torvalds 					int __user *child_tidptr,
108309a05394SRoland McGrath 					struct pid *pid,
108409a05394SRoland McGrath 					int trace)
10851da177e4SLinus Torvalds {
10861da177e4SLinus Torvalds 	int retval;
1087a24efe62SMariusz Kozlowski 	struct task_struct *p;
1088b4f48b63SPaul Menage 	int cgroup_callbacks_done = 0;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
10911da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	/*
10941da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
10951da177e4SLinus Torvalds 	 * can only be started up within the thread group.
10961da177e4SLinus Torvalds 	 */
10971da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
10981da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	/*
11011da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
11021da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
11031da177e4SLinus Torvalds 	 * for various simplifications in other code.
11041da177e4SLinus Torvalds 	 */
11051da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
11061da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
11071da177e4SLinus Torvalds 
1108123be07bSSukadev Bhattiprolu 	/*
1109123be07bSSukadev Bhattiprolu 	 * Siblings of global init remain as zombies on exit since they are
1110123be07bSSukadev Bhattiprolu 	 * not reaped by their parent (swapper). To solve this and to avoid
1111123be07bSSukadev Bhattiprolu 	 * multi-rooted process trees, prevent global and container-inits
1112123be07bSSukadev Bhattiprolu 	 * from creating siblings.
1113123be07bSSukadev Bhattiprolu 	 */
1114123be07bSSukadev Bhattiprolu 	if ((clone_flags & CLONE_PARENT) &&
1115123be07bSSukadev Bhattiprolu 				current->signal->flags & SIGNAL_UNKILLABLE)
1116123be07bSSukadev Bhattiprolu 		return ERR_PTR(-EINVAL);
1117123be07bSSukadev Bhattiprolu 
11181da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
11191da177e4SLinus Torvalds 	if (retval)
11201da177e4SLinus Torvalds 		goto fork_out;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	retval = -ENOMEM;
11231da177e4SLinus Torvalds 	p = dup_task_struct(current);
11241da177e4SLinus Torvalds 	if (!p)
11251da177e4SLinus Torvalds 		goto fork_out;
11261da177e4SLinus Torvalds 
1127f7e8b616SSteven Rostedt 	ftrace_graph_init_task(p);
1128f7e8b616SSteven Rostedt 
1129bea493a0SPeter Zijlstra 	rt_mutex_init_task(p);
1130bea493a0SPeter Zijlstra 
1131d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING
1132de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1133de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1134de30a2b3SIngo Molnar #endif
11351da177e4SLinus Torvalds 	retval = -EAGAIN;
11363b11a1deSDavid Howells 	if (atomic_read(&p->real_cred->user->processes) >=
113778d7d407SJiri Slaby 			task_rlimit(p, RLIMIT_NPROC)) {
11381da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
113918b6e041SSerge Hallyn 		    p->real_cred->user != INIT_USER)
11401da177e4SLinus Torvalds 			goto bad_fork_free;
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 
1143f1752eecSDavid Howells 	retval = copy_creds(p, clone_flags);
1144f1752eecSDavid Howells 	if (retval < 0)
1145f1752eecSDavid Howells 		goto bad_fork_free;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	/*
11481da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
11491da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
11501da177e4SLinus Torvalds 	 * to stop root fork bombs.
11511da177e4SLinus Torvalds 	 */
115204ec93feSLi Zefan 	retval = -EAGAIN;
11531da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
11541da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
11551da177e4SLinus Torvalds 
1156a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
11571da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	p->did_exec = 0;
1160ca74e92bSShailabh Nagar 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
11611da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
11621da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
11631da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
1164f41d911fSPaul E. McKenney 	rcu_copy_process(p);
11651da177e4SLinus Torvalds 	p->vfork_done = NULL;
11661da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	init_sigpending(&p->pending);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	p->utime = cputime_zero;
11711da177e4SLinus Torvalds 	p->stime = cputime_zero;
11729ac52315SLaurent Vivier 	p->gtime = cputime_zero;
1173c66f08beSMichael Neuling 	p->utimescaled = cputime_zero;
1174c66f08beSMichael Neuling 	p->stimescaled = cputime_zero;
1175d99ca3b9SHidetoshi Seto #ifndef CONFIG_VIRT_CPU_ACCOUNTING
117673a2bcb0SPeter Zijlstra 	p->prev_utime = cputime_zero;
11779301899bSBalbir Singh 	p->prev_stime = cputime_zero;
1178d99ca3b9SHidetoshi Seto #endif
1179a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
1180a3a2e76cSKAMEZAWA Hiroyuki 	memset(&p->rss_stat, 0, sizeof(p->rss_stat));
1181a3a2e76cSKAMEZAWA Hiroyuki #endif
1182172ba844SBalbir Singh 
11836976675dSArjan van de Ven 	p->default_timer_slack_ns = current->timer_slack_ns;
11846976675dSArjan van de Ven 
11855995477aSAndrea Righi 	task_io_accounting_init(&p->ioac);
11861da177e4SLinus Torvalds 	acct_clear_integrals(p);
11871da177e4SLinus Torvalds 
1188f06febc9SFrank Mayhar 	posix_cpu_timers_init(p);
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
1191924b42d5STomas Janousek 	p->real_start_time = p->start_time;
1192924b42d5STomas Janousek 	monotonic_to_bootbased(&p->real_start_time);
11931da177e4SLinus Torvalds 	p->io_context = NULL;
11941da177e4SLinus Torvalds 	p->audit_context = NULL;
11954714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
11964714d1d3SBen Blum 		threadgroup_fork_read_lock(current);
1197b4f48b63SPaul Menage 	cgroup_fork(p);
11981da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1199846a16bfSLee Schermerhorn 	p->mempolicy = mpol_dup(p->mempolicy);
12001da177e4SLinus Torvalds  	if (IS_ERR(p->mempolicy)) {
12011da177e4SLinus Torvalds  		retval = PTR_ERR(p->mempolicy);
12021da177e4SLinus Torvalds  		p->mempolicy = NULL;
1203b4f48b63SPaul Menage  		goto bad_fork_cleanup_cgroup;
12041da177e4SLinus Torvalds  	}
1205c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(p);
12061da177e4SLinus Torvalds #endif
1207de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1208de30a2b3SIngo Molnar 	p->irq_events = 0;
1209b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1210b36e4758SRussell King 	p->hardirqs_enabled = 1;
1211b36e4758SRussell King #else
1212de30a2b3SIngo Molnar 	p->hardirqs_enabled = 0;
1213b36e4758SRussell King #endif
1214de30a2b3SIngo Molnar 	p->hardirq_enable_ip = 0;
1215de30a2b3SIngo Molnar 	p->hardirq_enable_event = 0;
1216de30a2b3SIngo Molnar 	p->hardirq_disable_ip = _THIS_IP_;
1217de30a2b3SIngo Molnar 	p->hardirq_disable_event = 0;
1218de30a2b3SIngo Molnar 	p->softirqs_enabled = 1;
1219de30a2b3SIngo Molnar 	p->softirq_enable_ip = _THIS_IP_;
1220de30a2b3SIngo Molnar 	p->softirq_enable_event = 0;
1221de30a2b3SIngo Molnar 	p->softirq_disable_ip = 0;
1222de30a2b3SIngo Molnar 	p->softirq_disable_event = 0;
1223de30a2b3SIngo Molnar 	p->hardirq_context = 0;
1224de30a2b3SIngo Molnar 	p->softirq_context = 0;
1225de30a2b3SIngo Molnar #endif
1226fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1227fbb9ce95SIngo Molnar 	p->lockdep_depth = 0; /* no locks held yet */
1228fbb9ce95SIngo Molnar 	p->curr_chain_key = 0;
1229fbb9ce95SIngo Molnar 	p->lockdep_recursion = 0;
1230fbb9ce95SIngo Molnar #endif
12311da177e4SLinus Torvalds 
1232408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1233408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1234408894eeSIngo Molnar #endif
1235569b846dSKAMEZAWA Hiroyuki #ifdef CONFIG_CGROUP_MEM_RES_CTLR
1236569b846dSKAMEZAWA Hiroyuki 	p->memcg_batch.do_batch = 0;
1237569b846dSKAMEZAWA Hiroyuki 	p->memcg_batch.memcg = NULL;
1238569b846dSKAMEZAWA Hiroyuki #endif
12390f481406SMarkus Metzger 
12403c90e6e9SSrivatsa Vaddagiri 	/* Perform scheduler related setup. Assign this task to a CPU. */
12413e51e3edSSamir Bellabes 	sched_fork(p);
12426ab423e0SPeter Zijlstra 
1243cdd6c482SIngo Molnar 	retval = perf_event_init_task(p);
12446ab423e0SPeter Zijlstra 	if (retval)
12456ab423e0SPeter Zijlstra 		goto bad_fork_cleanup_policy;
12463c90e6e9SSrivatsa Vaddagiri 
12471da177e4SLinus Torvalds 	if ((retval = audit_alloc(p)))
1248f1752eecSDavid Howells 		goto bad_fork_cleanup_policy;
12491da177e4SLinus Torvalds 	/* copy all the process information */
12501da177e4SLinus Torvalds 	if ((retval = copy_semundo(clone_flags, p)))
12511da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
12521da177e4SLinus Torvalds 	if ((retval = copy_files(clone_flags, p)))
12531da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
12541da177e4SLinus Torvalds 	if ((retval = copy_fs(clone_flags, p)))
12551da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
12561da177e4SLinus Torvalds 	if ((retval = copy_sighand(clone_flags, p)))
12571da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
12581da177e4SLinus Torvalds 	if ((retval = copy_signal(clone_flags, p)))
12591da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
12601da177e4SLinus Torvalds 	if ((retval = copy_mm(clone_flags, p)))
12611da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
1262ab516013SSerge E. Hallyn 	if ((retval = copy_namespaces(clone_flags, p)))
1263d84f4f99SDavid Howells 		goto bad_fork_cleanup_mm;
1264fadad878SJens Axboe 	if ((retval = copy_io(clone_flags, p)))
1265fd0928dfSJens Axboe 		goto bad_fork_cleanup_namespaces;
12666f2c55b8SAlexey Dobriyan 	retval = copy_thread(clone_flags, stack_start, stack_size, p, regs);
12671da177e4SLinus Torvalds 	if (retval)
1268fd0928dfSJens Axboe 		goto bad_fork_cleanup_io;
12691da177e4SLinus Torvalds 
1270425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid) {
1271425fb2b4SPavel Emelyanov 		retval = -ENOMEM;
127261bce0f1SEric W. Biederman 		pid = alloc_pid(p->nsproxy->pid_ns);
1273425fb2b4SPavel Emelyanov 		if (!pid)
1274fd0928dfSJens Axboe 			goto bad_fork_cleanup_io;
1275425fb2b4SPavel Emelyanov 	}
1276425fb2b4SPavel Emelyanov 
1277425fb2b4SPavel Emelyanov 	p->pid = pid_nr(pid);
1278425fb2b4SPavel Emelyanov 	p->tgid = p->pid;
1279425fb2b4SPavel Emelyanov 	if (clone_flags & CLONE_THREAD)
1280425fb2b4SPavel Emelyanov 		p->tgid = current->tgid;
1281425fb2b4SPavel Emelyanov 
12821da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
12831da177e4SLinus Torvalds 	/*
12841da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
12851da177e4SLinus Torvalds 	 */
12861da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
128773c10101SJens Axboe #ifdef CONFIG_BLOCK
128873c10101SJens Axboe 	p->plug = NULL;
128973c10101SJens Axboe #endif
129042b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
12918f17d3a5SIngo Molnar 	p->robust_list = NULL;
12928f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
12938f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
12948f17d3a5SIngo Molnar #endif
1295c87e2837SIngo Molnar 	INIT_LIST_HEAD(&p->pi_state_list);
1296c87e2837SIngo Molnar 	p->pi_state_cache = NULL;
129742b2dd0aSAlexey Dobriyan #endif
12981da177e4SLinus Torvalds 	/*
1299f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1300f9a3879aSGOTO Masanori 	 */
1301f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1302f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1303f9a3879aSGOTO Masanori 
1304f9a3879aSGOTO Masanori 	/*
13056580807dSOleg Nesterov 	 * Syscall tracing and stepping should be turned off in the
13066580807dSOleg Nesterov 	 * child regardless of CLONE_PTRACE.
13071da177e4SLinus Torvalds 	 */
13086580807dSOleg Nesterov 	user_disable_single_step(p);
13091da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1310ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1311ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1312ed75e8d5SLaurent Vivier #endif
13139745512cSArjan van de Ven 	clear_all_latency_tracing(p);
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
13161da177e4SLinus Torvalds 	p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
13171da177e4SLinus Torvalds 	p->pdeath_signal = 0;
13181da177e4SLinus Torvalds 	p->exit_state = 0;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	/*
13211da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
13221da177e4SLinus Torvalds 	 * We dont wake it up yet.
13231da177e4SLinus Torvalds 	 */
13241da177e4SLinus Torvalds 	p->group_leader = p;
132547e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
13261da177e4SLinus Torvalds 
1327b4f48b63SPaul Menage 	/* Now that the task is set up, run cgroup callbacks if
1328b4f48b63SPaul Menage 	 * necessary. We need to run them before the task is visible
1329b4f48b63SPaul Menage 	 * on the tasklist. */
1330b4f48b63SPaul Menage 	cgroup_fork_callbacks(p);
1331b4f48b63SPaul Menage 	cgroup_callbacks_done = 1;
1332b4f48b63SPaul Menage 
13331da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
13341da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
13372d5516cbSOleg Nesterov 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
13381da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
13392d5516cbSOleg Nesterov 		p->parent_exec_id = current->parent_exec_id;
13402d5516cbSOleg Nesterov 	} else {
13411da177e4SLinus Torvalds 		p->real_parent = current;
13422d5516cbSOleg Nesterov 		p->parent_exec_id = current->self_exec_id;
13432d5516cbSOleg Nesterov 	}
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
13464a2c7a78SOleg Nesterov 
13474a2c7a78SOleg Nesterov 	/*
13484a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
13494a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
13504a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
13514a2c7a78SOleg Nesterov 	 * it's process group.
13524a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
13534a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
13544a2c7a78SOleg Nesterov  	 */
13554a2c7a78SOleg Nesterov 	recalc_sigpending();
13564a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
13574a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
13584a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
13594a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
1360f7e8b616SSteven Rostedt 		goto bad_fork_free_pid;
13614a2c7a78SOleg Nesterov 	}
13624a2c7a78SOleg Nesterov 
13633f17da69SOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
1364b3ac022cSOleg Nesterov 		current->signal->nr_threads++;
13654ab6c083SOleg Nesterov 		atomic_inc(&current->signal->live);
1366b3ac022cSOleg Nesterov 		atomic_inc(&current->signal->sigcnt);
13671da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
136847e65328SOleg Nesterov 		list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
13691da177e4SLinus Torvalds 	}
13701da177e4SLinus Torvalds 
137173b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
137209a05394SRoland McGrath 		tracehook_finish_clone(p, clone_flags, trace);
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
137545a68628SEric W. Biederman 			if (is_child_reaper(pid))
137630e49c26SPavel Emelyanov 				p->nsproxy->pid_ns->child_reaper = p;
13775cd17569SEric W. Biederman 
1378fea9d175SOleg Nesterov 			p->signal->leader_pid = pid;
13799c9f4dedSAlan Cox 			p->signal->tty = tty_kref_get(current->signal->tty);
13805cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
13815cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_SID, task_session(current));
13829cd80bbbSOleg Nesterov 			list_add_tail(&p->sibling, &p->real_parent->children);
13835e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
1384909ea964SChristoph Lameter 			__this_cpu_inc(process_counts);
13851da177e4SLinus Torvalds 		}
138685868995SSukadev Bhattiprolu 		attach_pid(p, PIDTYPE_PID, pid);
13871da177e4SLinus Torvalds 		nr_threads++;
138873b9ebfeSOleg Nesterov 	}
138973b9ebfeSOleg Nesterov 
13901da177e4SLinus Torvalds 	total_forks++;
13913f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
13921da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1393c13cf856SAndrew Morton 	proc_fork_connector(p);
1394817929ecSPaul Menage 	cgroup_post_fork(p);
13954714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
13964714d1d3SBen Blum 		threadgroup_fork_read_unlock(current);
1397cdd6c482SIngo Molnar 	perf_event_fork(p);
13981da177e4SLinus Torvalds 	return p;
13991da177e4SLinus Torvalds 
1400425fb2b4SPavel Emelyanov bad_fork_free_pid:
1401425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid)
1402425fb2b4SPavel Emelyanov 		free_pid(pid);
1403fd0928dfSJens Axboe bad_fork_cleanup_io:
1404b69f2292SLouis Rilling 	if (p->io_context)
1405b69f2292SLouis Rilling 		exit_io_context(p);
1406ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces:
1407444f378bSLinus Torvalds 	exit_task_namespaces(p);
14081da177e4SLinus Torvalds bad_fork_cleanup_mm:
14093d5992d2SYing Han 	if (p->mm) {
14103d5992d2SYing Han 		task_lock(p);
14113d5992d2SYing Han 		if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
14123d5992d2SYing Han 			atomic_dec(&p->mm->oom_disable_count);
14133d5992d2SYing Han 		task_unlock(p);
14141da177e4SLinus Torvalds 		mmput(p->mm);
14153d5992d2SYing Han 	}
14161da177e4SLinus Torvalds bad_fork_cleanup_signal:
14174ab6c083SOleg Nesterov 	if (!(clone_flags & CLONE_THREAD))
14181c5354deSMike Galbraith 		free_signal_struct(p->signal);
14191da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1420a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
14211da177e4SLinus Torvalds bad_fork_cleanup_fs:
14221da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
14231da177e4SLinus Torvalds bad_fork_cleanup_files:
14241da177e4SLinus Torvalds 	exit_files(p); /* blocking */
14251da177e4SLinus Torvalds bad_fork_cleanup_semundo:
14261da177e4SLinus Torvalds 	exit_sem(p);
14271da177e4SLinus Torvalds bad_fork_cleanup_audit:
14281da177e4SLinus Torvalds 	audit_free(p);
14291da177e4SLinus Torvalds bad_fork_cleanup_policy:
1430cdd6c482SIngo Molnar 	perf_event_free_task(p);
14311da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1432f0be3d32SLee Schermerhorn 	mpol_put(p->mempolicy);
1433b4f48b63SPaul Menage bad_fork_cleanup_cgroup:
14341da177e4SLinus Torvalds #endif
14354714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
14364714d1d3SBen Blum 		threadgroup_fork_read_unlock(current);
1437b4f48b63SPaul Menage 	cgroup_exit(p, cgroup_callbacks_done);
143835df17c5SShailabh Nagar 	delayacct_tsk_free(p);
1439a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
14401da177e4SLinus Torvalds bad_fork_cleanup_count:
1441d84f4f99SDavid Howells 	atomic_dec(&p->cred->user->processes);
1442e0e81739SDavid Howells 	exit_creds(p);
14431da177e4SLinus Torvalds bad_fork_free:
14441da177e4SLinus Torvalds 	free_task(p);
1445fe7d37d1SOleg Nesterov fork_out:
1446fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14496b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
14501da177e4SLinus Torvalds {
14511da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
14521da177e4SLinus Torvalds 	return regs;
14531da177e4SLinus Torvalds }
14541da177e4SLinus Torvalds 
1455f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links)
1456f106eee1SOleg Nesterov {
1457f106eee1SOleg Nesterov 	enum pid_type type;
1458f106eee1SOleg Nesterov 
1459f106eee1SOleg Nesterov 	for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
1460f106eee1SOleg Nesterov 		INIT_HLIST_NODE(&links[type].node); /* not really needed */
1461f106eee1SOleg Nesterov 		links[type].pid = &init_struct_pid;
1462f106eee1SOleg Nesterov 	}
1463f106eee1SOleg Nesterov }
1464f106eee1SOleg Nesterov 
14659abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu)
14661da177e4SLinus Torvalds {
146736c8b586SIngo Molnar 	struct task_struct *task;
14681da177e4SLinus Torvalds 	struct pt_regs regs;
14691da177e4SLinus Torvalds 
147030e49c26SPavel Emelyanov 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL,
147109a05394SRoland McGrath 			    &init_struct_pid, 0);
1472f106eee1SOleg Nesterov 	if (!IS_ERR(task)) {
1473f106eee1SOleg Nesterov 		init_idle_pids(task->pids);
14741da177e4SLinus Torvalds 		init_idle(task, cpu);
1475f106eee1SOleg Nesterov 	}
147673b9ebfeSOleg Nesterov 
14771da177e4SLinus Torvalds 	return task;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds /*
14811da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
14821da177e4SLinus Torvalds  *
14831da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
14841da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
14851da177e4SLinus Torvalds  */
14861da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
14871da177e4SLinus Torvalds 	      unsigned long stack_start,
14881da177e4SLinus Torvalds 	      struct pt_regs *regs,
14891da177e4SLinus Torvalds 	      unsigned long stack_size,
14901da177e4SLinus Torvalds 	      int __user *parent_tidptr,
14911da177e4SLinus Torvalds 	      int __user *child_tidptr)
14921da177e4SLinus Torvalds {
14931da177e4SLinus Torvalds 	struct task_struct *p;
14941da177e4SLinus Torvalds 	int trace = 0;
149592476d7fSEric W. Biederman 	long nr;
14961da177e4SLinus Torvalds 
1497bdff746aSAndrew Morton 	/*
149818b6e041SSerge Hallyn 	 * Do some preliminary argument and permissions checking before we
149918b6e041SSerge Hallyn 	 * actually start allocating stuff
150018b6e041SSerge Hallyn 	 */
150118b6e041SSerge Hallyn 	if (clone_flags & CLONE_NEWUSER) {
150218b6e041SSerge Hallyn 		if (clone_flags & CLONE_THREAD)
150318b6e041SSerge Hallyn 			return -EINVAL;
150418b6e041SSerge Hallyn 		/* hopefully this check will go away when userns support is
150518b6e041SSerge Hallyn 		 * complete
150618b6e041SSerge Hallyn 		 */
15077657d904SSerge E. Hallyn 		if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) ||
15087657d904SSerge E. Hallyn 				!capable(CAP_SETGID))
150918b6e041SSerge Hallyn 			return -EPERM;
151018b6e041SSerge Hallyn 	}
151118b6e041SSerge Hallyn 
151218b6e041SSerge Hallyn 	/*
151309a05394SRoland McGrath 	 * When called from kernel_thread, don't do user tracing stuff.
151409a05394SRoland McGrath 	 */
151509a05394SRoland McGrath 	if (likely(user_mode(regs)))
151609a05394SRoland McGrath 		trace = tracehook_prepare_clone(clone_flags);
15171da177e4SLinus Torvalds 
1518a6f5e063SSukadev Bhattiprolu 	p = copy_process(clone_flags, stack_start, regs, stack_size,
151909a05394SRoland McGrath 			 child_tidptr, NULL, trace);
15201da177e4SLinus Torvalds 	/*
15211da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
15221da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
15231da177e4SLinus Torvalds 	 */
15241da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
15251da177e4SLinus Torvalds 		struct completion vfork;
15261da177e4SLinus Torvalds 
15270a16b607SMathieu Desnoyers 		trace_sched_process_fork(current, p);
15280a16b607SMathieu Desnoyers 
15296c5f3e7bSPavel Emelyanov 		nr = task_pid_vnr(p);
153030e49c26SPavel Emelyanov 
153130e49c26SPavel Emelyanov 		if (clone_flags & CLONE_PARENT_SETTID)
153230e49c26SPavel Emelyanov 			put_user(nr, parent_tidptr);
1533a6f5e063SSukadev Bhattiprolu 
15341da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
15351da177e4SLinus Torvalds 			p->vfork_done = &vfork;
15361da177e4SLinus Torvalds 			init_completion(&vfork);
15371da177e4SLinus Torvalds 		}
15381da177e4SLinus Torvalds 
1539a64e6494SAl Viro 		audit_finish_fork(p);
1540087eb437SOleg Nesterov 		tracehook_report_clone(regs, clone_flags, nr, p);
154109a05394SRoland McGrath 
154209a05394SRoland McGrath 		/*
154309a05394SRoland McGrath 		 * We set PF_STARTING at creation in case tracing wants to
154409a05394SRoland McGrath 		 * use this to distinguish a fully live task from one that
154509a05394SRoland McGrath 		 * hasn't gotten to tracehook_report_clone() yet.  Now we
154609a05394SRoland McGrath 		 * clear it and set the child going.
154709a05394SRoland McGrath 		 */
154809a05394SRoland McGrath 		p->flags &= ~PF_STARTING;
154909a05394SRoland McGrath 
15503e51e3edSSamir Bellabes 		wake_up_new_task(p);
15511da177e4SLinus Torvalds 
155209a05394SRoland McGrath 		tracehook_report_clone_complete(trace, regs,
155309a05394SRoland McGrath 						clone_flags, nr, p);
155409a05394SRoland McGrath 
15551da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
1556ba96a0c8SRafael J. Wysocki 			freezer_do_not_count();
15571da177e4SLinus Torvalds 			wait_for_completion(&vfork);
1558ba96a0c8SRafael J. Wysocki 			freezer_count();
1559daded34bSRoland McGrath 			tracehook_report_vfork_done(p, nr);
15609f59ce5dSChuck Ebbert 		}
15611da177e4SLinus Torvalds 	} else {
156292476d7fSEric W. Biederman 		nr = PTR_ERR(p);
15631da177e4SLinus Torvalds 	}
156492476d7fSEric W. Biederman 	return nr;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15675fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN
15685fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0
15695fd63b30SRavikiran G Thirumalai #endif
15705fd63b30SRavikiran G Thirumalai 
157151cc5068SAlexey Dobriyan static void sighand_ctor(void *data)
1572aa1757f9SOleg Nesterov {
1573aa1757f9SOleg Nesterov 	struct sighand_struct *sighand = data;
1574aa1757f9SOleg Nesterov 
1575aa1757f9SOleg Nesterov 	spin_lock_init(&sighand->siglock);
1576b8fceee1SDavide Libenzi 	init_waitqueue_head(&sighand->signalfd_wqh);
1577fba2afaaSDavide Libenzi }
1578aa1757f9SOleg Nesterov 
15791da177e4SLinus Torvalds void __init proc_caches_init(void)
15801da177e4SLinus Torvalds {
15811da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
15821da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
15832dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
15842dff4405SVegard Nossum 			SLAB_NOTRACK, sighand_ctor);
15851da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
15861da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
15872dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
15881da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
15891da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
15902dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
15911da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
15921da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
15932dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
15941da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
15955fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
15962dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
159733e5d769SDavid Howells 	vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC);
15988feae131SDavid Howells 	mmap_init();
15991da177e4SLinus Torvalds }
1600cf2e340fSJANAK DESAI 
1601cf2e340fSJANAK DESAI /*
16029bfb23fcSOleg Nesterov  * Check constraints on flags passed to the unshare system call.
1603cf2e340fSJANAK DESAI  */
16049bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags)
1605cf2e340fSJANAK DESAI {
16069bfb23fcSOleg Nesterov 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
16079bfb23fcSOleg Nesterov 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
16089bfb23fcSOleg Nesterov 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET))
1609cf2e340fSJANAK DESAI 		return -EINVAL;
16109bfb23fcSOleg Nesterov 	/*
16119bfb23fcSOleg Nesterov 	 * Not implemented, but pretend it works if there is nothing to
16129bfb23fcSOleg Nesterov 	 * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND
16139bfb23fcSOleg Nesterov 	 * needs to unshare vm.
16149bfb23fcSOleg Nesterov 	 */
16159bfb23fcSOleg Nesterov 	if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
16169bfb23fcSOleg Nesterov 		/* FIXME: get_task_mm() increments ->mm_users */
16179bfb23fcSOleg Nesterov 		if (atomic_read(&current->mm->mm_users) > 1)
16189bfb23fcSOleg Nesterov 			return -EINVAL;
16199bfb23fcSOleg Nesterov 	}
1620cf2e340fSJANAK DESAI 
1621cf2e340fSJANAK DESAI 	return 0;
1622cf2e340fSJANAK DESAI }
1623cf2e340fSJANAK DESAI 
1624cf2e340fSJANAK DESAI /*
162599d1419dSJANAK DESAI  * Unshare the filesystem structure if it is being shared
1626cf2e340fSJANAK DESAI  */
1627cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1628cf2e340fSJANAK DESAI {
1629cf2e340fSJANAK DESAI 	struct fs_struct *fs = current->fs;
1630cf2e340fSJANAK DESAI 
1631498052bbSAl Viro 	if (!(unshare_flags & CLONE_FS) || !fs)
1632498052bbSAl Viro 		return 0;
1633498052bbSAl Viro 
1634498052bbSAl Viro 	/* don't need lock here; in the worst case we'll do useless copy */
1635498052bbSAl Viro 	if (fs->users == 1)
1636498052bbSAl Viro 		return 0;
1637498052bbSAl Viro 
1638498052bbSAl Viro 	*new_fsp = copy_fs_struct(fs);
163999d1419dSJANAK DESAI 	if (!*new_fsp)
164099d1419dSJANAK DESAI 		return -ENOMEM;
1641cf2e340fSJANAK DESAI 
1642cf2e340fSJANAK DESAI 	return 0;
1643cf2e340fSJANAK DESAI }
1644cf2e340fSJANAK DESAI 
1645cf2e340fSJANAK DESAI /*
1646a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1647cf2e340fSJANAK DESAI  */
1648cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1649cf2e340fSJANAK DESAI {
1650cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1651a016f338SJANAK DESAI 	int error = 0;
1652cf2e340fSJANAK DESAI 
1653cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1654a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1655a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1656a016f338SJANAK DESAI 		if (!*new_fdp)
1657a016f338SJANAK DESAI 			return error;
1658a016f338SJANAK DESAI 	}
1659cf2e340fSJANAK DESAI 
1660cf2e340fSJANAK DESAI 	return 0;
1661cf2e340fSJANAK DESAI }
1662cf2e340fSJANAK DESAI 
1663cf2e340fSJANAK DESAI /*
1664cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1665cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1666cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1667cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1668cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1669cf2e340fSJANAK DESAI  * task_struct.
1670cf2e340fSJANAK DESAI  */
16716559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1672cf2e340fSJANAK DESAI {
1673cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1674cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1675cf7b708cSPavel Emelyanov 	struct nsproxy *new_nsproxy = NULL;
16769edff4abSManfred Spraul 	int do_sysvsem = 0;
16779bfb23fcSOleg Nesterov 	int err;
1678cf2e340fSJANAK DESAI 
16799bfb23fcSOleg Nesterov 	err = check_unshare_flags(unshare_flags);
16809bfb23fcSOleg Nesterov 	if (err)
168106f9d4f9SEric W. Biederman 		goto bad_unshare_out;
168206f9d4f9SEric W. Biederman 
16836013f67fSManfred Spraul 	/*
16849bfb23fcSOleg Nesterov 	 * If unsharing namespace, must also unshare filesystem information.
16859bfb23fcSOleg Nesterov 	 */
16869bfb23fcSOleg Nesterov 	if (unshare_flags & CLONE_NEWNS)
16879bfb23fcSOleg Nesterov 		unshare_flags |= CLONE_FS;
16889bfb23fcSOleg Nesterov 	/*
16896013f67fSManfred Spraul 	 * CLONE_NEWIPC must also detach from the undolist: after switching
16906013f67fSManfred Spraul 	 * to a new ipc namespace, the semaphore arrays from the old
16916013f67fSManfred Spraul 	 * namespace are unreachable.
16926013f67fSManfred Spraul 	 */
16936013f67fSManfred Spraul 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
16949edff4abSManfred Spraul 		do_sysvsem = 1;
1695cf2e340fSJANAK DESAI 	if ((err = unshare_fs(unshare_flags, &new_fs)))
16969bfb23fcSOleg Nesterov 		goto bad_unshare_out;
1697cf2e340fSJANAK DESAI 	if ((err = unshare_fd(unshare_flags, &new_fd)))
16989bfb23fcSOleg Nesterov 		goto bad_unshare_cleanup_fs;
1699e3222c4eSBadari Pulavarty 	if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
1700e3222c4eSBadari Pulavarty 			new_fs)))
17019edff4abSManfred Spraul 		goto bad_unshare_cleanup_fd;
1702cf2e340fSJANAK DESAI 
17039bfb23fcSOleg Nesterov 	if (new_fs || new_fd || do_sysvsem || new_nsproxy) {
17049edff4abSManfred Spraul 		if (do_sysvsem) {
17059edff4abSManfred Spraul 			/*
17069edff4abSManfred Spraul 			 * CLONE_SYSVSEM is equivalent to sys_exit().
17079edff4abSManfred Spraul 			 */
17089edff4abSManfred Spraul 			exit_sem(current);
17099edff4abSManfred Spraul 		}
1710ab516013SSerge E. Hallyn 
1711c0b2fc31SSerge Hallyn 		if (new_nsproxy) {
1712cf7b708cSPavel Emelyanov 			switch_task_namespaces(current, new_nsproxy);
1713cf7b708cSPavel Emelyanov 			new_nsproxy = NULL;
1714c0b2fc31SSerge Hallyn 		}
1715cf2e340fSJANAK DESAI 
1716cf7b708cSPavel Emelyanov 		task_lock(current);
1717cf7b708cSPavel Emelyanov 
1718cf2e340fSJANAK DESAI 		if (new_fs) {
1719cf2e340fSJANAK DESAI 			fs = current->fs;
17202a4419b5SNick Piggin 			spin_lock(&fs->lock);
1721cf2e340fSJANAK DESAI 			current->fs = new_fs;
1722498052bbSAl Viro 			if (--fs->users)
1723498052bbSAl Viro 				new_fs = NULL;
1724498052bbSAl Viro 			else
1725cf2e340fSJANAK DESAI 				new_fs = fs;
17262a4419b5SNick Piggin 			spin_unlock(&fs->lock);
1727cf2e340fSJANAK DESAI 		}
1728cf2e340fSJANAK DESAI 
1729cf2e340fSJANAK DESAI 		if (new_fd) {
1730cf2e340fSJANAK DESAI 			fd = current->files;
1731cf2e340fSJANAK DESAI 			current->files = new_fd;
1732cf2e340fSJANAK DESAI 			new_fd = fd;
1733cf2e340fSJANAK DESAI 		}
1734cf2e340fSJANAK DESAI 
1735cf2e340fSJANAK DESAI 		task_unlock(current);
1736cf2e340fSJANAK DESAI 	}
1737cf2e340fSJANAK DESAI 
1738c0b2fc31SSerge Hallyn 	if (new_nsproxy)
1739444f378bSLinus Torvalds 		put_nsproxy(new_nsproxy);
1740c0b2fc31SSerge Hallyn 
1741cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
1742cf2e340fSJANAK DESAI 	if (new_fd)
1743cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
1744cf2e340fSJANAK DESAI 
1745cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
1746cf2e340fSJANAK DESAI 	if (new_fs)
1747498052bbSAl Viro 		free_fs_struct(new_fs);
1748cf2e340fSJANAK DESAI 
1749cf2e340fSJANAK DESAI bad_unshare_out:
1750cf2e340fSJANAK DESAI 	return err;
1751cf2e340fSJANAK DESAI }
17523b125388SAl Viro 
17533b125388SAl Viro /*
17543b125388SAl Viro  *	Helper to unshare the files of the current task.
17553b125388SAl Viro  *	We don't want to expose copy_files internals to
17563b125388SAl Viro  *	the exec layer of the kernel.
17573b125388SAl Viro  */
17583b125388SAl Viro 
17593b125388SAl Viro int unshare_files(struct files_struct **displaced)
17603b125388SAl Viro {
17613b125388SAl Viro 	struct task_struct *task = current;
176250704516SAl Viro 	struct files_struct *copy = NULL;
17633b125388SAl Viro 	int error;
17643b125388SAl Viro 
17653b125388SAl Viro 	error = unshare_fd(CLONE_FILES, &copy);
17663b125388SAl Viro 	if (error || !copy) {
17673b125388SAl Viro 		*displaced = NULL;
17683b125388SAl Viro 		return error;
17693b125388SAl Viro 	}
17703b125388SAl Viro 	*displaced = task->files;
17713b125388SAl Viro 	task_lock(task);
17723b125388SAl Viro 	task->files = copy;
17733b125388SAl Viro 	task_unlock(task);
17743b125388SAl Viro 	return 0;
17753b125388SAl Viro }
1776