xref: /openbmc/linux/kernel/fork.c (revision 2889f60814e15dea644782597d897cdba943564f)
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>
401da177e4SLinus Torvalds #include <linux/futex.h>
418141c7f3SLinus Torvalds #include <linux/compat.h>
42207205a2SEric Dumazet #include <linux/kthread.h>
437c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h>
44ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
451da177e4SLinus Torvalds #include <linux/ptrace.h>
461da177e4SLinus Torvalds #include <linux/mount.h>
471da177e4SLinus Torvalds #include <linux/audit.h>
4878fb7466SPavel Emelianov #include <linux/memcontrol.h>
49f201ae23SFrederic Weisbecker #include <linux/ftrace.h>
501da177e4SLinus Torvalds #include <linux/profile.h>
511da177e4SLinus Torvalds #include <linux/rmap.h>
52f8af4da3SHugh Dickins #include <linux/ksm.h>
531da177e4SLinus Torvalds #include <linux/acct.h>
548f0ab514SJay Lan #include <linux/tsacct_kern.h>
559f46080cSMatt Helsley #include <linux/cn_proc.h>
56ba96a0c8SRafael J. Wysocki #include <linux/freezer.h>
57ca74e92bSShailabh Nagar #include <linux/delayacct.h>
58ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h>
590a425405SArjan van de Ven #include <linux/random.h>
60522ed776SMiloslav Trmac #include <linux/tty.h>
61fd0928dfSJens Axboe #include <linux/blkdev.h>
625ad4e53bSAl Viro #include <linux/fs_struct.h>
637c9f8861SEric Sandeen #include <linux/magic.h>
64cdd6c482SIngo Molnar #include <linux/perf_event.h>
6542c4ab41SStanislaw Gruszka #include <linux/posix-timers.h>
668e7cac79SAvi Kivity #include <linux/user-return-notifier.h>
673d5992d2SYing Han #include <linux/oom.h>
68ba76149fSAndrea Arcangeli #include <linux/khugepaged.h>
69d80e731eSOleg Nesterov #include <linux/signalfd.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 
8043d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS
8143d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h>
8243d2b113SKAMEZAWA Hiroyuki 
831da177e4SLinus Torvalds /*
841da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
871da177e4SLinus Torvalds int nr_threads;			/* The idle threads do not count.. */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
94db1466b3SPaul E. McKenney 
95db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU
96db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void)
97db1466b3SPaul E. McKenney {
98db1466b3SPaul E. McKenney 	return lockdep_is_held(&tasklist_lock);
99db1466b3SPaul E. McKenney }
100db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
101db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds int nr_processes(void)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	int cpu;
1061da177e4SLinus Torvalds 	int total = 0;
1071da177e4SLinus Torvalds 
1081d510750SIan Campbell 	for_each_possible_cpu(cpu)
1091da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	return total;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
115504f52b5SEric Dumazet # define alloc_task_struct_node(node)		\
116504f52b5SEric Dumazet 		kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node)
117504f52b5SEric Dumazet # define free_task_struct(tsk)			\
118504f52b5SEric Dumazet 		kmem_cache_free(task_struct_cachep, (tsk))
119e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep;
1201da177e4SLinus Torvalds #endif
1211da177e4SLinus Torvalds 
122b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR
123b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
124b6a84016SEric Dumazet 						  int node)
125b69c49b7SFUJITA Tomonori {
126*2889f608SThomas Gleixner 	struct page *page = alloc_pages_node(node, THREADINFO_GFP,
127*2889f608SThomas Gleixner 					     THREAD_SIZE_ORDER);
128b6a84016SEric Dumazet 
129b6a84016SEric Dumazet 	return page ? page_address(page) : NULL;
130b69c49b7SFUJITA Tomonori }
131b69c49b7SFUJITA Tomonori 
132b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti)
133b69c49b7SFUJITA Tomonori {
134b69c49b7SFUJITA Tomonori 	free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
135b69c49b7SFUJITA Tomonori }
136b69c49b7SFUJITA Tomonori #endif
137b69c49b7SFUJITA Tomonori 
1381da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
139e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
142e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
145e18b890bSChristoph Lameter struct kmem_cache *files_cachep;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
148e18b890bSChristoph Lameter struct kmem_cache *fs_cachep;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
151e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
154e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep;
1551da177e4SLinus Torvalds 
156c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account)
157c6a7f572SKOSAKI Motohiro {
158c6a7f572SKOSAKI Motohiro 	struct zone *zone = page_zone(virt_to_page(ti));
159c6a7f572SKOSAKI Motohiro 
160c6a7f572SKOSAKI Motohiro 	mod_zone_page_state(zone, NR_KERNEL_STACK, account);
161c6a7f572SKOSAKI Motohiro }
162c6a7f572SKOSAKI Motohiro 
1631da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1641da177e4SLinus Torvalds {
165c6a7f572SKOSAKI Motohiro 	account_kernel_stack(tsk->stack, -1);
166f7e4217bSRoman Zippel 	free_thread_info(tsk->stack);
16723f78d4aSIngo Molnar 	rt_mutex_debug_task_free(tsk);
168fb52607aSFrederic Weisbecker 	ftrace_graph_exit_task(tsk);
1691da177e4SLinus Torvalds 	free_task_struct(tsk);
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1721da177e4SLinus Torvalds 
173ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig)
174ea6d290cSOleg Nesterov {
17597101eb4SOleg Nesterov 	taskstats_tgid_free(sig);
1761c5354deSMike Galbraith 	sched_autogroup_exit(sig);
177ea6d290cSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
178ea6d290cSOleg Nesterov }
179ea6d290cSOleg Nesterov 
180ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig)
181ea6d290cSOleg Nesterov {
1821c5354deSMike Galbraith 	if (atomic_dec_and_test(&sig->sigcnt))
183ea6d290cSOleg Nesterov 		free_signal_struct(sig);
184ea6d290cSOleg Nesterov }
185ea6d290cSOleg Nesterov 
186158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
1871da177e4SLinus Torvalds {
188270f722dSEugene Teo 	WARN_ON(!tsk->exit_state);
1891da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1901da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1911da177e4SLinus Torvalds 
1921a2a4d06SKees Cook 	security_task_free(tsk);
193e0e81739SDavid Howells 	exit_creds(tsk);
19435df17c5SShailabh Nagar 	delayacct_tsk_free(tsk);
195ea6d290cSOleg Nesterov 	put_signal_struct(tsk->signal);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
1981da177e4SLinus Torvalds 		free_task(tsk);
1991da177e4SLinus Torvalds }
20077c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct);
2011da177e4SLinus Torvalds 
2026c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { }
20361c4628bSSuresh Siddha 
2041da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
2071da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
2081da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
2091da177e4SLinus Torvalds #endif
2101da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
2111da177e4SLinus Torvalds 	task_struct_cachep =
2121da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
2132dff4405SVegard Nossum 			ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL);
2141da177e4SLinus Torvalds #endif
2151da177e4SLinus Torvalds 
21661c4628bSSuresh Siddha 	/* do the arch specific task caches init */
21761c4628bSSuresh Siddha 	arch_task_cache_init();
21861c4628bSSuresh Siddha 
2191da177e4SLinus Torvalds 	/*
2201da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
2211da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
2221da177e4SLinus Torvalds 	 * of memory.
2231da177e4SLinus Torvalds 	 */
2241da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	/*
2271da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
2281da177e4SLinus Torvalds 	 */
2291da177e4SLinus Torvalds 	if (max_threads < 20)
2301da177e4SLinus Torvalds 		max_threads = 20;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
2331da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
2341da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
2351da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
23861c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
23961c4628bSSuresh Siddha 					       struct task_struct *src)
24061c4628bSSuresh Siddha {
24161c4628bSSuresh Siddha 	*dst = *src;
24261c4628bSSuresh Siddha 	return 0;
24361c4628bSSuresh Siddha }
24461c4628bSSuresh Siddha 
2451da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	struct task_struct *tsk;
2481da177e4SLinus Torvalds 	struct thread_info *ti;
2497c9f8861SEric Sandeen 	unsigned long *stackend;
250207205a2SEric Dumazet 	int node = tsk_fork_get_node(orig);
2513e26c149SPeter Zijlstra 	int err;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	prepare_to_copy(orig);
2541da177e4SLinus Torvalds 
255504f52b5SEric Dumazet 	tsk = alloc_task_struct_node(node);
2561da177e4SLinus Torvalds 	if (!tsk)
2571da177e4SLinus Torvalds 		return NULL;
2581da177e4SLinus Torvalds 
259b6a84016SEric Dumazet 	ti = alloc_thread_info_node(tsk, node);
2601da177e4SLinus Torvalds 	if (!ti) {
2611da177e4SLinus Torvalds 		free_task_struct(tsk);
2621da177e4SLinus Torvalds 		return NULL;
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
26561c4628bSSuresh Siddha 	err = arch_dup_task_struct(tsk, orig);
26661c4628bSSuresh Siddha 	if (err)
26761c4628bSSuresh Siddha 		goto out;
26861c4628bSSuresh Siddha 
269f7e4217bSRoman Zippel 	tsk->stack = ti;
2703e26c149SPeter Zijlstra 
27110ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
2728e7cac79SAvi Kivity 	clear_user_return_notifier(tsk);
273f26f9affSMike Galbraith 	clear_tsk_need_resched(tsk);
2747c9f8861SEric Sandeen 	stackend = end_of_stack(tsk);
2757c9f8861SEric Sandeen 	*stackend = STACK_END_MAGIC;	/* for overflow detection */
2761da177e4SLinus Torvalds 
2770a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
2780a425405SArjan van de Ven 	tsk->stack_canary = get_random_int();
2790a425405SArjan van de Ven #endif
2800a425405SArjan van de Ven 
281fb0a685cSDaniel Rebelo de Oliveira 	/*
282fb0a685cSDaniel Rebelo de Oliveira 	 * One for us, one for whoever does the "release_task()" (usually
283fb0a685cSDaniel Rebelo de Oliveira 	 * parent)
284fb0a685cSDaniel Rebelo de Oliveira 	 */
2851da177e4SLinus Torvalds 	atomic_set(&tsk->usage, 2);
2866c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
2872056a782SJens Axboe 	tsk->btrace_seq = 0;
2886c5c9341SAlexey Dobriyan #endif
289a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
290c6a7f572SKOSAKI Motohiro 
291c6a7f572SKOSAKI Motohiro 	account_kernel_stack(ti, 1);
292c6a7f572SKOSAKI Motohiro 
2931da177e4SLinus Torvalds 	return tsk;
29461c4628bSSuresh Siddha 
29561c4628bSSuresh Siddha out:
29661c4628bSSuresh Siddha 	free_thread_info(ti);
29761c4628bSSuresh Siddha 	free_task_struct(tsk);
29861c4628bSSuresh Siddha 	return NULL;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds #ifdef CONFIG_MMU
302a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
3031da177e4SLinus Torvalds {
304297c5eeeSLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
3051da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
3061da177e4SLinus Torvalds 	int retval;
3071da177e4SLinus Torvalds 	unsigned long charge;
3081da177e4SLinus Torvalds 	struct mempolicy *pol;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
311ec8c0446SRalf Baechle 	flush_cache_dup_mm(oldmm);
312ad339451SIngo Molnar 	/*
313ad339451SIngo Molnar 	 * Not linked in yet - no deadlock potential:
314ad339451SIngo Molnar 	 */
315ad339451SIngo Molnar 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
3167ee78232SHugh Dickins 
3171da177e4SLinus Torvalds 	mm->locked_vm = 0;
3181da177e4SLinus Torvalds 	mm->mmap = NULL;
3191da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
3201da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
3211363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
3221da177e4SLinus Torvalds 	mm->map_count = 0;
32394894244SRusty Russell 	cpumask_clear(mm_cpumask(mm));
3241da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
3251da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
3261da177e4SLinus Torvalds 	rb_parent = NULL;
3271da177e4SLinus Torvalds 	pprev = &mm->mmap;
328f8af4da3SHugh Dickins 	retval = ksm_fork(mm, oldmm);
329f8af4da3SHugh Dickins 	if (retval)
330f8af4da3SHugh Dickins 		goto out;
331ba76149fSAndrea Arcangeli 	retval = khugepaged_fork(mm, oldmm);
332ba76149fSAndrea Arcangeli 	if (retval)
333ba76149fSAndrea Arcangeli 		goto out;
3341da177e4SLinus Torvalds 
335297c5eeeSLinus Torvalds 	prev = NULL;
336fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
3371da177e4SLinus Torvalds 		struct file *file;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
3403b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
3413b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
342ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
3433b6bfcdbSHugh Dickins 								-pages);
3441da177e4SLinus Torvalds 			continue;
3451da177e4SLinus Torvalds 		}
3461da177e4SLinus Torvalds 		charge = 0;
3471da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
3481da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
349191c5424SAl Viro 			if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
3501da177e4SLinus Torvalds 				goto fail_nomem;
3511da177e4SLinus Torvalds 			charge = len;
3521da177e4SLinus Torvalds 		}
353e94b1766SChristoph Lameter 		tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
3541da177e4SLinus Torvalds 		if (!tmp)
3551da177e4SLinus Torvalds 			goto fail_nomem;
3561da177e4SLinus Torvalds 		*tmp = *mpnt;
3575beb4930SRik van Riel 		INIT_LIST_HEAD(&tmp->anon_vma_chain);
358846a16bfSLee Schermerhorn 		pol = mpol_dup(vma_policy(mpnt));
3591da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
3601da177e4SLinus Torvalds 		if (IS_ERR(pol))
3611da177e4SLinus Torvalds 			goto fail_nomem_policy;
3621da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
363a247c3a9SAndrea Arcangeli 		tmp->vm_mm = mm;
3645beb4930SRik van Riel 		if (anon_vma_fork(tmp, mpnt))
3655beb4930SRik van Riel 			goto fail_nomem_anon_vma_fork;
3661da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
367297c5eeeSLinus Torvalds 		tmp->vm_next = tmp->vm_prev = NULL;
3681da177e4SLinus Torvalds 		file = tmp->vm_file;
3691da177e4SLinus Torvalds 		if (file) {
370f3a43f3fSJosef "Jeff" Sipek 			struct inode *inode = file->f_path.dentry->d_inode;
371b88ed205SHugh Dickins 			struct address_space *mapping = file->f_mapping;
372b88ed205SHugh Dickins 
3731da177e4SLinus Torvalds 			get_file(file);
3741da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
3751da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
3763d48ae45SPeter Zijlstra 			mutex_lock(&mapping->i_mmap_mutex);
377b88ed205SHugh Dickins 			if (tmp->vm_flags & VM_SHARED)
378b88ed205SHugh Dickins 				mapping->i_mmap_writable++;
379b88ed205SHugh Dickins 			flush_dcache_mmap_lock(mapping);
380b88ed205SHugh Dickins 			/* insert tmp into the share list, just after mpnt */
3811da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
382b88ed205SHugh Dickins 			flush_dcache_mmap_unlock(mapping);
3833d48ae45SPeter Zijlstra 			mutex_unlock(&mapping->i_mmap_mutex);
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		/*
387a1e78772SMel Gorman 		 * Clear hugetlb-related page reserves for children. This only
388a1e78772SMel Gorman 		 * affects MAP_PRIVATE mappings. Faults generated by the child
389a1e78772SMel Gorman 		 * are not guaranteed to succeed, even if read-only
390a1e78772SMel Gorman 		 */
391a1e78772SMel Gorman 		if (is_vm_hugetlb_page(tmp))
392a1e78772SMel Gorman 			reset_vma_resv_huge_pages(tmp);
393a1e78772SMel Gorman 
394a1e78772SMel Gorman 		/*
3957ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
3961da177e4SLinus Torvalds 		 */
3971da177e4SLinus Torvalds 		*pprev = tmp;
3981da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
399297c5eeeSLinus Torvalds 		tmp->vm_prev = prev;
400297c5eeeSLinus Torvalds 		prev = tmp;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
4031da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
4041da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 		mm->map_count++;
4070b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
4101da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 		if (retval)
4131da177e4SLinus Torvalds 			goto out;
4141da177e4SLinus Torvalds 	}
415d6dd61c8SJeremy Fitzhardinge 	/* a new mm has just been created */
416d6dd61c8SJeremy Fitzhardinge 	arch_dup_mmap(oldmm, mm);
4171da177e4SLinus Torvalds 	retval = 0;
4181da177e4SLinus Torvalds out:
4197ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
420fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
4211da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
4221da177e4SLinus Torvalds 	return retval;
4235beb4930SRik van Riel fail_nomem_anon_vma_fork:
4245beb4930SRik van Riel 	mpol_put(pol);
4251da177e4SLinus Torvalds fail_nomem_policy:
4261da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
4271da177e4SLinus Torvalds fail_nomem:
4281da177e4SLinus Torvalds 	retval = -ENOMEM;
4291da177e4SLinus Torvalds 	vm_unacct_memory(charge);
4301da177e4SLinus Torvalds 	goto out;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
4361da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
4371da177e4SLinus Torvalds 		return -ENOMEM;
4381da177e4SLinus Torvalds 	return 0;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm)
4421da177e4SLinus Torvalds {
4435e541973SBenjamin Herrenschmidt 	pgd_free(mm, mm->pgd);
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds #else
4461da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
4471da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
4481da177e4SLinus Torvalds #define mm_free_pgd(mm)
4491da177e4SLinus Torvalds #endif /* CONFIG_MMU */
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
4521da177e4SLinus Torvalds 
453e94b1766SChristoph Lameter #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
4541da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
4551da177e4SLinus Torvalds 
4564cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
4574cb0e11bSHidehiro Kawai 
4584cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s)
4594cb0e11bSHidehiro Kawai {
4604cb0e11bSHidehiro Kawai 	default_dump_filter =
4614cb0e11bSHidehiro Kawai 		(simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
4624cb0e11bSHidehiro Kawai 		MMF_DUMP_FILTER_MASK;
4634cb0e11bSHidehiro Kawai 	return 1;
4644cb0e11bSHidehiro Kawai }
4654cb0e11bSHidehiro Kawai 
4664cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup);
4674cb0e11bSHidehiro Kawai 
4681da177e4SLinus Torvalds #include <linux/init_task.h>
4691da177e4SLinus Torvalds 
470858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm)
471858f0993SAlexey Dobriyan {
472858f0993SAlexey Dobriyan #ifdef CONFIG_AIO
473858f0993SAlexey Dobriyan 	spin_lock_init(&mm->ioctx_lock);
474858f0993SAlexey Dobriyan 	INIT_HLIST_HEAD(&mm->ioctx_list);
475858f0993SAlexey Dobriyan #endif
476858f0993SAlexey Dobriyan }
477858f0993SAlexey Dobriyan 
47878fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
4811da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
4821da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
4831da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
484f8af4da3SHugh Dickins 	mm->flags = (current->mm) ?
485f8af4da3SHugh Dickins 		(current->mm->flags & MMF_INIT_MASK) : default_dump_filter;
486999d9fc1SOleg Nesterov 	mm->core_state = NULL;
4871da177e4SLinus Torvalds 	mm->nr_ptes = 0;
488d559db08SKAMEZAWA Hiroyuki 	memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
4891da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
4901da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
4911363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
492858f0993SAlexey Dobriyan 	mm_init_aio(mm);
493cf475ad2SBalbir Singh 	mm_init_owner(mm, p);
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
4961da177e4SLinus Torvalds 		mm->def_flags = 0;
497cddb8a5cSAndrea Arcangeli 		mmu_notifier_mm_init(mm);
4981da177e4SLinus Torvalds 		return mm;
4991da177e4SLinus Torvalds 	}
50078fb7466SPavel Emelianov 
5011da177e4SLinus Torvalds 	free_mm(mm);
5021da177e4SLinus Torvalds 	return NULL;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
505c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm)
506c3f0327fSKonstantin Khlebnikov {
507c3f0327fSKonstantin Khlebnikov 	int i;
508c3f0327fSKonstantin Khlebnikov 
509c3f0327fSKonstantin Khlebnikov 	for (i = 0; i < NR_MM_COUNTERS; i++) {
510c3f0327fSKonstantin Khlebnikov 		long x = atomic_long_read(&mm->rss_stat.count[i]);
511c3f0327fSKonstantin Khlebnikov 
512c3f0327fSKonstantin Khlebnikov 		if (unlikely(x))
513c3f0327fSKonstantin Khlebnikov 			printk(KERN_ALERT "BUG: Bad rss-counter state "
514c3f0327fSKonstantin Khlebnikov 					  "mm:%p idx:%d val:%ld\n", mm, i, x);
515c3f0327fSKonstantin Khlebnikov 	}
516c3f0327fSKonstantin Khlebnikov 
517c3f0327fSKonstantin Khlebnikov #ifdef CONFIG_TRANSPARENT_HUGEPAGE
518c3f0327fSKonstantin Khlebnikov 	VM_BUG_ON(mm->pmd_huge_pte);
519c3f0327fSKonstantin Khlebnikov #endif
520c3f0327fSKonstantin Khlebnikov }
521c3f0327fSKonstantin Khlebnikov 
5221da177e4SLinus Torvalds /*
5231da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
5241da177e4SLinus Torvalds  */
5251da177e4SLinus Torvalds struct mm_struct *mm_alloc(void)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	struct mm_struct *mm;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	mm = allocate_mm();
530de03c72cSKOSAKI Motohiro 	if (!mm)
531de03c72cSKOSAKI Motohiro 		return NULL;
532de03c72cSKOSAKI Motohiro 
5331da177e4SLinus Torvalds 	memset(mm, 0, sizeof(*mm));
5346345d24dSLinus Torvalds 	mm_init_cpumask(mm);
5356345d24dSLinus Torvalds 	return mm_init(mm, current);
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds /*
5391da177e4SLinus Torvalds  * Called when the last reference to the mm
5401da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
5411da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
5421da177e4SLinus Torvalds  */
5437ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
5461da177e4SLinus Torvalds 	mm_free_pgd(mm);
5471da177e4SLinus Torvalds 	destroy_context(mm);
548cddb8a5cSAndrea Arcangeli 	mmu_notifier_mm_destroy(mm);
549c3f0327fSKonstantin Khlebnikov 	check_mm(mm);
5501da177e4SLinus Torvalds 	free_mm(mm);
5511da177e4SLinus Torvalds }
5526d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop);
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /*
5551da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
5561da177e4SLinus Torvalds  */
5571da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
5581da177e4SLinus Torvalds {
5590ae26f1bSAndrew Morton 	might_sleep();
5600ae26f1bSAndrew Morton 
5611da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
5621da177e4SLinus Torvalds 		exit_aio(mm);
5631c2fb7a4SAndrea Arcangeli 		ksm_exit(mm);
564ba76149fSAndrea Arcangeli 		khugepaged_exit(mm); /* must run before exit_mmap */
5651da177e4SLinus Torvalds 		exit_mmap(mm);
566925d1c40SMatt Helsley 		set_mm_exe_file(mm, NULL);
5671da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
5681da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
5691da177e4SLinus Torvalds 			list_del(&mm->mmlist);
5701da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
5711da177e4SLinus Torvalds 		}
5721da177e4SLinus Torvalds 		put_swap_token(mm);
573801460d0SHiroshi Shimamoto 		if (mm->binfmt)
574801460d0SHiroshi Shimamoto 			module_put(mm->binfmt->module);
5751da177e4SLinus Torvalds 		mmdrop(mm);
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
5791da177e4SLinus Torvalds 
58038646013SJiri Slaby /*
58138646013SJiri Slaby  * We added or removed a vma mapping the executable. The vmas are only mapped
58238646013SJiri Slaby  * during exec and are not mapped with the mmap system call.
58338646013SJiri Slaby  * Callers must hold down_write() on the mm's mmap_sem for these
58438646013SJiri Slaby  */
58538646013SJiri Slaby void added_exe_file_vma(struct mm_struct *mm)
58638646013SJiri Slaby {
58738646013SJiri Slaby 	mm->num_exe_file_vmas++;
58838646013SJiri Slaby }
58938646013SJiri Slaby 
59038646013SJiri Slaby void removed_exe_file_vma(struct mm_struct *mm)
59138646013SJiri Slaby {
59238646013SJiri Slaby 	mm->num_exe_file_vmas--;
59338646013SJiri Slaby 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file) {
59438646013SJiri Slaby 		fput(mm->exe_file);
59538646013SJiri Slaby 		mm->exe_file = NULL;
59638646013SJiri Slaby 	}
59738646013SJiri Slaby 
59838646013SJiri Slaby }
59938646013SJiri Slaby 
60038646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
60138646013SJiri Slaby {
60238646013SJiri Slaby 	if (new_exe_file)
60338646013SJiri Slaby 		get_file(new_exe_file);
60438646013SJiri Slaby 	if (mm->exe_file)
60538646013SJiri Slaby 		fput(mm->exe_file);
60638646013SJiri Slaby 	mm->exe_file = new_exe_file;
60738646013SJiri Slaby 	mm->num_exe_file_vmas = 0;
60838646013SJiri Slaby }
60938646013SJiri Slaby 
61038646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm)
61138646013SJiri Slaby {
61238646013SJiri Slaby 	struct file *exe_file;
61338646013SJiri Slaby 
61438646013SJiri Slaby 	/* We need mmap_sem to protect against races with removal of
61538646013SJiri Slaby 	 * VM_EXECUTABLE vmas */
61638646013SJiri Slaby 	down_read(&mm->mmap_sem);
61738646013SJiri Slaby 	exe_file = mm->exe_file;
61838646013SJiri Slaby 	if (exe_file)
61938646013SJiri Slaby 		get_file(exe_file);
62038646013SJiri Slaby 	up_read(&mm->mmap_sem);
62138646013SJiri Slaby 	return exe_file;
62238646013SJiri Slaby }
62338646013SJiri Slaby 
62438646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
62538646013SJiri Slaby {
62638646013SJiri Slaby 	/* It's safe to write the exe_file pointer without exe_file_lock because
62738646013SJiri Slaby 	 * this is called during fork when the task is not yet in /proc */
62838646013SJiri Slaby 	newmm->exe_file = get_mm_exe_file(oldmm);
62938646013SJiri Slaby }
63038646013SJiri Slaby 
6311da177e4SLinus Torvalds /**
6321da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
6331da177e4SLinus Torvalds  *
634246bb0b1SOleg Nesterov  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
6351da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
6361da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
6371da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
6381da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
6391da177e4SLinus Torvalds  */
6401da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	struct mm_struct *mm;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	task_lock(task);
6451da177e4SLinus Torvalds 	mm = task->mm;
6461da177e4SLinus Torvalds 	if (mm) {
647246bb0b1SOleg Nesterov 		if (task->flags & PF_KTHREAD)
6481da177e4SLinus Torvalds 			mm = NULL;
6491da177e4SLinus Torvalds 		else
6501da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
6511da177e4SLinus Torvalds 	}
6521da177e4SLinus Torvalds 	task_unlock(task);
6531da177e4SLinus Torvalds 	return mm;
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
6561da177e4SLinus Torvalds 
6578cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
6588cdb878dSChristopher Yeoh {
6598cdb878dSChristopher Yeoh 	struct mm_struct *mm;
6608cdb878dSChristopher Yeoh 	int err;
6618cdb878dSChristopher Yeoh 
6628cdb878dSChristopher Yeoh 	err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
6638cdb878dSChristopher Yeoh 	if (err)
6648cdb878dSChristopher Yeoh 		return ERR_PTR(err);
6658cdb878dSChristopher Yeoh 
6668cdb878dSChristopher Yeoh 	mm = get_task_mm(task);
6678cdb878dSChristopher Yeoh 	if (mm && mm != current->mm &&
6688cdb878dSChristopher Yeoh 			!ptrace_may_access(task, mode)) {
6698cdb878dSChristopher Yeoh 		mmput(mm);
6708cdb878dSChristopher Yeoh 		mm = ERR_PTR(-EACCES);
6718cdb878dSChristopher Yeoh 	}
6728cdb878dSChristopher Yeoh 	mutex_unlock(&task->signal->cred_guard_mutex);
6738cdb878dSChristopher Yeoh 
6748cdb878dSChristopher Yeoh 	return mm;
6758cdb878dSChristopher Yeoh }
6768cdb878dSChristopher Yeoh 
67757b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk)
678c415c3b4SOleg Nesterov {
679d68b46feSOleg Nesterov 	struct completion *vfork;
680c415c3b4SOleg Nesterov 
681d68b46feSOleg Nesterov 	task_lock(tsk);
682d68b46feSOleg Nesterov 	vfork = tsk->vfork_done;
683d68b46feSOleg Nesterov 	if (likely(vfork)) {
684c415c3b4SOleg Nesterov 		tsk->vfork_done = NULL;
685d68b46feSOleg Nesterov 		complete(vfork);
686d68b46feSOleg Nesterov 	}
687d68b46feSOleg Nesterov 	task_unlock(tsk);
688d68b46feSOleg Nesterov }
689d68b46feSOleg Nesterov 
690d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child,
691d68b46feSOleg Nesterov 				struct completion *vfork)
692d68b46feSOleg Nesterov {
693d68b46feSOleg Nesterov 	int killed;
694d68b46feSOleg Nesterov 
695d68b46feSOleg Nesterov 	freezer_do_not_count();
696d68b46feSOleg Nesterov 	killed = wait_for_completion_killable(vfork);
697d68b46feSOleg Nesterov 	freezer_count();
698d68b46feSOleg Nesterov 
699d68b46feSOleg Nesterov 	if (killed) {
700d68b46feSOleg Nesterov 		task_lock(child);
701d68b46feSOleg Nesterov 		child->vfork_done = NULL;
702d68b46feSOleg Nesterov 		task_unlock(child);
703d68b46feSOleg Nesterov 	}
704d68b46feSOleg Nesterov 
705d68b46feSOleg Nesterov 	put_task_struct(child);
706d68b46feSOleg Nesterov 	return killed;
707c415c3b4SOleg Nesterov }
708c415c3b4SOleg Nesterov 
7091da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
7101da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
7111da177e4SLinus Torvalds  * error success whatever.
7121da177e4SLinus Torvalds  *
7131da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
7141da177e4SLinus Torvalds  * from the current process.
7151da177e4SLinus Torvalds  *
7161da177e4SLinus Torvalds  * This difference is important for error handling, when we
7171da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
7181da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
7191da177e4SLinus Torvalds  * restoring the old one. . .
7201da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
7211da177e4SLinus Torvalds  */
7221da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
7231da177e4SLinus Torvalds {
7248141c7f3SLinus Torvalds 	/* Get rid of any futexes when releasing the mm */
7258141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX
726fc6b177dSPeter Zijlstra 	if (unlikely(tsk->robust_list)) {
7278141c7f3SLinus Torvalds 		exit_robust_list(tsk);
728fc6b177dSPeter Zijlstra 		tsk->robust_list = NULL;
729fc6b177dSPeter Zijlstra 	}
7308141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT
731fc6b177dSPeter Zijlstra 	if (unlikely(tsk->compat_robust_list)) {
7328141c7f3SLinus Torvalds 		compat_exit_robust_list(tsk);
733fc6b177dSPeter Zijlstra 		tsk->compat_robust_list = NULL;
734fc6b177dSPeter Zijlstra 	}
7358141c7f3SLinus Torvalds #endif
736322a2c10SThomas Gleixner 	if (unlikely(!list_empty(&tsk->pi_state_list)))
737322a2c10SThomas Gleixner 		exit_pi_state_list(tsk);
7388141c7f3SLinus Torvalds #endif
7398141c7f3SLinus Torvalds 
7401da177e4SLinus Torvalds 	/* Get rid of any cached register state */
7411da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
7421da177e4SLinus Torvalds 
743c415c3b4SOleg Nesterov 	if (tsk->vfork_done)
744c415c3b4SOleg Nesterov 		complete_vfork_done(tsk);
745fec1d011SRoland McGrath 
746fec1d011SRoland McGrath 	/*
747fec1d011SRoland McGrath 	 * If we're exiting normally, clear a user-space tid field if
748fec1d011SRoland McGrath 	 * requested.  We leave this alone when dying by signal, to leave
749fec1d011SRoland McGrath 	 * the value intact in a core dump, and to save the unnecessary
750d68b46feSOleg Nesterov 	 * trouble, say, a killed vfork parent shouldn't touch this mm.
751d68b46feSOleg Nesterov 	 * Userland only wants this done for a sys_exit.
752fec1d011SRoland McGrath 	 */
7539c8a8228SEric Dumazet 	if (tsk->clear_child_tid) {
7549c8a8228SEric Dumazet 		if (!(tsk->flags & PF_SIGNALED) &&
7559c8a8228SEric Dumazet 		    atomic_read(&mm->mm_users) > 1) {
7561da177e4SLinus Torvalds 			/*
7571da177e4SLinus Torvalds 			 * We don't check the error code - if userspace has
7581da177e4SLinus Torvalds 			 * not set up a proper pointer then tough luck.
7591da177e4SLinus Torvalds 			 */
7609c8a8228SEric Dumazet 			put_user(0, tsk->clear_child_tid);
7619c8a8228SEric Dumazet 			sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
7629c8a8228SEric Dumazet 					1, NULL, NULL, 0);
7639c8a8228SEric Dumazet 		}
7649c8a8228SEric Dumazet 		tsk->clear_child_tid = NULL;
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
768a0a7ec30SJANAK DESAI /*
769a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
770a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
771a0a7ec30SJANAK DESAI  */
772402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk)
773a0a7ec30SJANAK DESAI {
774a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
775a0a7ec30SJANAK DESAI 	int err;
776a0a7ec30SJANAK DESAI 
777a0a7ec30SJANAK DESAI 	if (!oldmm)
778a0a7ec30SJANAK DESAI 		return NULL;
779a0a7ec30SJANAK DESAI 
780a0a7ec30SJANAK DESAI 	mm = allocate_mm();
781a0a7ec30SJANAK DESAI 	if (!mm)
782a0a7ec30SJANAK DESAI 		goto fail_nomem;
783a0a7ec30SJANAK DESAI 
784a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
7856345d24dSLinus Torvalds 	mm_init_cpumask(mm);
786a0a7ec30SJANAK DESAI 
7877602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
7887602bdf2SAshwin Chaugule 	mm->token_priority = 0;
7897602bdf2SAshwin Chaugule 	mm->last_interval = 0;
7907602bdf2SAshwin Chaugule 
791e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE
792e7a00c45SAndrea Arcangeli 	mm->pmd_huge_pte = NULL;
793e7a00c45SAndrea Arcangeli #endif
794e7a00c45SAndrea Arcangeli 
79578fb7466SPavel Emelianov 	if (!mm_init(mm, tsk))
796a0a7ec30SJANAK DESAI 		goto fail_nomem;
797a0a7ec30SJANAK DESAI 
798a0a7ec30SJANAK DESAI 	if (init_new_context(tsk, mm))
799a0a7ec30SJANAK DESAI 		goto fail_nocontext;
800a0a7ec30SJANAK DESAI 
801925d1c40SMatt Helsley 	dup_mm_exe_file(oldmm, mm);
802925d1c40SMatt Helsley 
803a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
804a0a7ec30SJANAK DESAI 	if (err)
805a0a7ec30SJANAK DESAI 		goto free_pt;
806a0a7ec30SJANAK DESAI 
807a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
808a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
809a0a7ec30SJANAK DESAI 
810801460d0SHiroshi Shimamoto 	if (mm->binfmt && !try_module_get(mm->binfmt->module))
811801460d0SHiroshi Shimamoto 		goto free_pt;
812801460d0SHiroshi Shimamoto 
813a0a7ec30SJANAK DESAI 	return mm;
814a0a7ec30SJANAK DESAI 
815a0a7ec30SJANAK DESAI free_pt:
816801460d0SHiroshi Shimamoto 	/* don't put binfmt in mmput, we haven't got module yet */
817801460d0SHiroshi Shimamoto 	mm->binfmt = NULL;
818a0a7ec30SJANAK DESAI 	mmput(mm);
819a0a7ec30SJANAK DESAI 
820a0a7ec30SJANAK DESAI fail_nomem:
821a0a7ec30SJANAK DESAI 	return NULL;
822a0a7ec30SJANAK DESAI 
823a0a7ec30SJANAK DESAI fail_nocontext:
824a0a7ec30SJANAK DESAI 	/*
825a0a7ec30SJANAK DESAI 	 * If init_new_context() failed, we cannot use mmput() to free the mm
826a0a7ec30SJANAK DESAI 	 * because it calls destroy_context()
827a0a7ec30SJANAK DESAI 	 */
828a0a7ec30SJANAK DESAI 	mm_free_pgd(mm);
829a0a7ec30SJANAK DESAI 	free_mm(mm);
830a0a7ec30SJANAK DESAI 	return NULL;
831a0a7ec30SJANAK DESAI }
832a0a7ec30SJANAK DESAI 
8331da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
8341da177e4SLinus Torvalds {
8351da177e4SLinus Torvalds 	struct mm_struct *mm, *oldmm;
8361da177e4SLinus Torvalds 	int retval;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
8391da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
84017406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
84117406b82SMandeep Singh Baines 	tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
84217406b82SMandeep Singh Baines #endif
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	tsk->mm = NULL;
8451da177e4SLinus Torvalds 	tsk->active_mm = NULL;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/*
8481da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
8491da177e4SLinus Torvalds 	 *
8501da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
8511da177e4SLinus Torvalds 	 */
8521da177e4SLinus Torvalds 	oldmm = current->mm;
8531da177e4SLinus Torvalds 	if (!oldmm)
8541da177e4SLinus Torvalds 		return 0;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
8571da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
8581da177e4SLinus Torvalds 		mm = oldmm;
8591da177e4SLinus Torvalds 		goto good_mm;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	retval = -ENOMEM;
863a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
8641da177e4SLinus Torvalds 	if (!mm)
8651da177e4SLinus Torvalds 		goto fail_nomem;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds good_mm:
8687602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
8697602bdf2SAshwin Chaugule 	mm->token_priority = 0;
8707602bdf2SAshwin Chaugule 	mm->last_interval = 0;
8717602bdf2SAshwin Chaugule 
8721da177e4SLinus Torvalds 	tsk->mm = mm;
8731da177e4SLinus Torvalds 	tsk->active_mm = mm;
8741da177e4SLinus Torvalds 	return 0;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds fail_nomem:
8771da177e4SLinus Torvalds 	return retval;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
880a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
8811da177e4SLinus Torvalds {
882498052bbSAl Viro 	struct fs_struct *fs = current->fs;
8831da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
884498052bbSAl Viro 		/* tsk->fs is already what we want */
8852a4419b5SNick Piggin 		spin_lock(&fs->lock);
886498052bbSAl Viro 		if (fs->in_exec) {
8872a4419b5SNick Piggin 			spin_unlock(&fs->lock);
888498052bbSAl Viro 			return -EAGAIN;
889498052bbSAl Viro 		}
890498052bbSAl Viro 		fs->users++;
8912a4419b5SNick Piggin 		spin_unlock(&fs->lock);
8921da177e4SLinus Torvalds 		return 0;
8931da177e4SLinus Torvalds 	}
894498052bbSAl Viro 	tsk->fs = copy_fs_struct(fs);
8951da177e4SLinus Torvalds 	if (!tsk->fs)
8961da177e4SLinus Torvalds 		return -ENOMEM;
8971da177e4SLinus Torvalds 	return 0;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
900a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
901a016f338SJANAK DESAI {
902a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
903a016f338SJANAK DESAI 	int error = 0;
904a016f338SJANAK DESAI 
905a016f338SJANAK DESAI 	/*
906a016f338SJANAK DESAI 	 * A background process may not have any files ...
907a016f338SJANAK DESAI 	 */
908a016f338SJANAK DESAI 	oldf = current->files;
909a016f338SJANAK DESAI 	if (!oldf)
910a016f338SJANAK DESAI 		goto out;
911a016f338SJANAK DESAI 
912a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
913a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
914a016f338SJANAK DESAI 		goto out;
915a016f338SJANAK DESAI 	}
916a016f338SJANAK DESAI 
917a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
918a016f338SJANAK DESAI 	if (!newf)
919a016f338SJANAK DESAI 		goto out;
920a016f338SJANAK DESAI 
921a016f338SJANAK DESAI 	tsk->files = newf;
922a016f338SJANAK DESAI 	error = 0;
923a016f338SJANAK DESAI out:
924a016f338SJANAK DESAI 	return error;
925a016f338SJANAK DESAI }
926a016f338SJANAK DESAI 
927fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
928fd0928dfSJens Axboe {
929fd0928dfSJens Axboe #ifdef CONFIG_BLOCK
930fd0928dfSJens Axboe 	struct io_context *ioc = current->io_context;
9316e736be7STejun Heo 	struct io_context *new_ioc;
932fd0928dfSJens Axboe 
933fd0928dfSJens Axboe 	if (!ioc)
934fd0928dfSJens Axboe 		return 0;
935fadad878SJens Axboe 	/*
936fadad878SJens Axboe 	 * Share io context with parent, if CLONE_IO is set
937fadad878SJens Axboe 	 */
938fadad878SJens Axboe 	if (clone_flags & CLONE_IO) {
939fadad878SJens Axboe 		tsk->io_context = ioc_task_link(ioc);
940fadad878SJens Axboe 		if (unlikely(!tsk->io_context))
941fadad878SJens Axboe 			return -ENOMEM;
942fadad878SJens Axboe 	} else if (ioprio_valid(ioc->ioprio)) {
9436e736be7STejun Heo 		new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
9446e736be7STejun Heo 		if (unlikely(!new_ioc))
945fd0928dfSJens Axboe 			return -ENOMEM;
946fd0928dfSJens Axboe 
9476e736be7STejun Heo 		new_ioc->ioprio = ioc->ioprio;
94811a3122fSTejun Heo 		put_io_context(new_ioc);
949fd0928dfSJens Axboe 	}
950fd0928dfSJens Axboe #endif
951fd0928dfSJens Axboe 	return 0;
952fd0928dfSJens Axboe }
953fd0928dfSJens Axboe 
954a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
9551da177e4SLinus Torvalds {
9561da177e4SLinus Torvalds 	struct sighand_struct *sig;
9571da177e4SLinus Torvalds 
95860348802SZhaolei 	if (clone_flags & CLONE_SIGHAND) {
9591da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
9601da177e4SLinus Torvalds 		return 0;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
963e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
9641da177e4SLinus Torvalds 	if (!sig)
9651da177e4SLinus Torvalds 		return -ENOMEM;
9661da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
9671da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
9681da177e4SLinus Torvalds 	return 0;
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
971a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
972c81addc9SOleg Nesterov {
973d80e731eSOleg Nesterov 	if (atomic_dec_and_test(&sighand->count)) {
974d80e731eSOleg Nesterov 		signalfd_cleanup(sighand);
975c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
976c81addc9SOleg Nesterov 	}
977d80e731eSOleg Nesterov }
978c81addc9SOleg Nesterov 
979f06febc9SFrank Mayhar 
980f06febc9SFrank Mayhar /*
981f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a thread group.
982f06febc9SFrank Mayhar  */
983f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig)
984f06febc9SFrank Mayhar {
98578d7d407SJiri Slaby 	unsigned long cpu_limit;
98678d7d407SJiri Slaby 
987f06febc9SFrank Mayhar 	/* Thread group counters. */
988f06febc9SFrank Mayhar 	thread_group_cputime_init(sig);
989f06febc9SFrank Mayhar 
99078d7d407SJiri Slaby 	cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
99178d7d407SJiri Slaby 	if (cpu_limit != RLIM_INFINITY) {
99278d7d407SJiri Slaby 		sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
9936279a751SOleg Nesterov 		sig->cputimer.running = 1;
9946279a751SOleg Nesterov 	}
9956279a751SOleg Nesterov 
996f06febc9SFrank Mayhar 	/* The timer lists. */
997f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
998f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
999f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
1000f06febc9SFrank Mayhar }
1001f06febc9SFrank Mayhar 
1002a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
10031da177e4SLinus Torvalds {
10041da177e4SLinus Torvalds 	struct signal_struct *sig;
10051da177e4SLinus Torvalds 
10064ab6c083SOleg Nesterov 	if (clone_flags & CLONE_THREAD)
1007490dea45SPeter Zijlstra 		return 0;
10086279a751SOleg Nesterov 
1009a56704efSVeaceslav Falico 	sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
10101da177e4SLinus Torvalds 	tsk->signal = sig;
10111da177e4SLinus Torvalds 	if (!sig)
10121da177e4SLinus Torvalds 		return -ENOMEM;
10131da177e4SLinus Torvalds 
1014b3ac022cSOleg Nesterov 	sig->nr_threads = 1;
10151da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
1016b3ac022cSOleg Nesterov 	atomic_set(&sig->sigcnt, 1);
10171da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
1018b3bfa0cbSSukadev Bhattiprolu 	if (clone_flags & CLONE_NEWPID)
1019b3bfa0cbSSukadev Bhattiprolu 		sig->flags |= SIGNAL_UNKILLABLE;
1020db51aeccSOleg Nesterov 	sig->curr_target = tsk;
10211da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
10221da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
10231da177e4SLinus Torvalds 
1024c9cb2e3dSThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
10251da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	task_lock(current->group_leader);
10281da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
10291da177e4SLinus Torvalds 	task_unlock(current->group_leader);
10301da177e4SLinus Torvalds 
10316279a751SOleg Nesterov 	posix_cpu_timers_init_group(sig);
10326279a751SOleg Nesterov 
1033522ed776SMiloslav Trmac 	tty_audit_fork(sig);
10345091faa4SMike Galbraith 	sched_autogroup_fork(sig);
1035522ed776SMiloslav Trmac 
10364714d1d3SBen Blum #ifdef CONFIG_CGROUPS
1037257058aeSTejun Heo 	init_rwsem(&sig->group_rwsem);
10384714d1d3SBen Blum #endif
10394714d1d3SBen Blum 
104028b83c51SKOSAKI Motohiro 	sig->oom_adj = current->signal->oom_adj;
1041a63d83f4SDavid Rientjes 	sig->oom_score_adj = current->signal->oom_score_adj;
1042dabb16f6SMandeep Singh Baines 	sig->oom_score_adj_min = current->signal->oom_score_adj_min;
104328b83c51SKOSAKI Motohiro 
1044ebec18a6SLennart Poettering 	sig->has_child_subreaper = current->signal->has_child_subreaper ||
1045ebec18a6SLennart Poettering 				   current->signal->is_child_subreaper;
1046ebec18a6SLennart Poettering 
10479b1bf12dSKOSAKI Motohiro 	mutex_init(&sig->cred_guard_mutex);
10489b1bf12dSKOSAKI Motohiro 
10491da177e4SLinus Torvalds 	return 0;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
1052a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
10551da177e4SLinus Torvalds 
105621aa9af0STejun Heo 	new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
10571da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
10581da177e4SLinus Torvalds 	p->flags = new_flags;
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds 
106117da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
10621da177e4SLinus Torvalds {
10631da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
10641da177e4SLinus Torvalds 
1065b488893aSPavel Emelyanov 	return task_pid_vnr(current);
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
1068a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p)
106923f78d4aSIngo Molnar {
10701d615482SThomas Gleixner 	raw_spin_lock_init(&p->pi_lock);
1071e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES
1072732375c6SDima Zavin 	plist_head_init(&p->pi_waiters);
107323f78d4aSIngo Molnar 	p->pi_blocked_on = NULL;
107423f78d4aSIngo Molnar #endif
107523f78d4aSIngo Molnar }
107623f78d4aSIngo Molnar 
1077cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER
1078cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
1079cf475ad2SBalbir Singh {
1080cf475ad2SBalbir Singh 	mm->owner = p;
1081cf475ad2SBalbir Singh }
1082cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */
1083cf475ad2SBalbir Singh 
10841da177e4SLinus Torvalds /*
1085f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a single task.
1086f06febc9SFrank Mayhar  */
1087f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk)
1088f06febc9SFrank Mayhar {
108964861634SMartin Schwidefsky 	tsk->cputime_expires.prof_exp = 0;
109064861634SMartin Schwidefsky 	tsk->cputime_expires.virt_exp = 0;
1091f06febc9SFrank Mayhar 	tsk->cputime_expires.sched_exp = 0;
1092f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[0]);
1093f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[1]);
1094f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[2]);
1095f06febc9SFrank Mayhar }
1096f06febc9SFrank Mayhar 
1097f06febc9SFrank Mayhar /*
10981da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
10991da177e4SLinus Torvalds  * but does not actually start it yet.
11001da177e4SLinus Torvalds  *
11011da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
11021da177e4SLinus Torvalds  * parts of the process environment (as per the clone
11031da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
11041da177e4SLinus Torvalds  */
110536c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags,
11061da177e4SLinus Torvalds 					unsigned long stack_start,
11071da177e4SLinus Torvalds 					struct pt_regs *regs,
11081da177e4SLinus Torvalds 					unsigned long stack_size,
11091da177e4SLinus Torvalds 					int __user *child_tidptr,
111009a05394SRoland McGrath 					struct pid *pid,
111109a05394SRoland McGrath 					int trace)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	int retval;
1114a24efe62SMariusz Kozlowski 	struct task_struct *p;
1115b4f48b63SPaul Menage 	int cgroup_callbacks_done = 0;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
11181da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	/*
11211da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
11221da177e4SLinus Torvalds 	 * can only be started up within the thread group.
11231da177e4SLinus Torvalds 	 */
11241da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
11251da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	/*
11281da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
11291da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
11301da177e4SLinus Torvalds 	 * for various simplifications in other code.
11311da177e4SLinus Torvalds 	 */
11321da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
11331da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
11341da177e4SLinus Torvalds 
1135123be07bSSukadev Bhattiprolu 	/*
1136123be07bSSukadev Bhattiprolu 	 * Siblings of global init remain as zombies on exit since they are
1137123be07bSSukadev Bhattiprolu 	 * not reaped by their parent (swapper). To solve this and to avoid
1138123be07bSSukadev Bhattiprolu 	 * multi-rooted process trees, prevent global and container-inits
1139123be07bSSukadev Bhattiprolu 	 * from creating siblings.
1140123be07bSSukadev Bhattiprolu 	 */
1141123be07bSSukadev Bhattiprolu 	if ((clone_flags & CLONE_PARENT) &&
1142123be07bSSukadev Bhattiprolu 				current->signal->flags & SIGNAL_UNKILLABLE)
1143123be07bSSukadev Bhattiprolu 		return ERR_PTR(-EINVAL);
1144123be07bSSukadev Bhattiprolu 
11451da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
11461da177e4SLinus Torvalds 	if (retval)
11471da177e4SLinus Torvalds 		goto fork_out;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	retval = -ENOMEM;
11501da177e4SLinus Torvalds 	p = dup_task_struct(current);
11511da177e4SLinus Torvalds 	if (!p)
11521da177e4SLinus Torvalds 		goto fork_out;
11531da177e4SLinus Torvalds 
1154f7e8b616SSteven Rostedt 	ftrace_graph_init_task(p);
1155f7e8b616SSteven Rostedt 
1156bea493a0SPeter Zijlstra 	rt_mutex_init_task(p);
1157bea493a0SPeter Zijlstra 
1158d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING
1159de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1160de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1161de30a2b3SIngo Molnar #endif
11621da177e4SLinus Torvalds 	retval = -EAGAIN;
11633b11a1deSDavid Howells 	if (atomic_read(&p->real_cred->user->processes) >=
116478d7d407SJiri Slaby 			task_rlimit(p, RLIMIT_NPROC)) {
11651da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
116618b6e041SSerge Hallyn 		    p->real_cred->user != INIT_USER)
11671da177e4SLinus Torvalds 			goto bad_fork_free;
11681da177e4SLinus Torvalds 	}
116972fa5997SVasiliy Kulikov 	current->flags &= ~PF_NPROC_EXCEEDED;
11701da177e4SLinus Torvalds 
1171f1752eecSDavid Howells 	retval = copy_creds(p, clone_flags);
1172f1752eecSDavid Howells 	if (retval < 0)
1173f1752eecSDavid Howells 		goto bad_fork_free;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	/*
11761da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
11771da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
11781da177e4SLinus Torvalds 	 * to stop root fork bombs.
11791da177e4SLinus Torvalds 	 */
118004ec93feSLi Zefan 	retval = -EAGAIN;
11811da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
11821da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
11831da177e4SLinus Torvalds 
1184a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
11851da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	p->did_exec = 0;
1188ca74e92bSShailabh Nagar 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
11891da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
11901da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
11911da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
1192f41d911fSPaul E. McKenney 	rcu_copy_process(p);
11931da177e4SLinus Torvalds 	p->vfork_done = NULL;
11941da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	init_sigpending(&p->pending);
11971da177e4SLinus Torvalds 
119864861634SMartin Schwidefsky 	p->utime = p->stime = p->gtime = 0;
119964861634SMartin Schwidefsky 	p->utimescaled = p->stimescaled = 0;
1200d99ca3b9SHidetoshi Seto #ifndef CONFIG_VIRT_CPU_ACCOUNTING
120164861634SMartin Schwidefsky 	p->prev_utime = p->prev_stime = 0;
1202d99ca3b9SHidetoshi Seto #endif
1203a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
1204a3a2e76cSKAMEZAWA Hiroyuki 	memset(&p->rss_stat, 0, sizeof(p->rss_stat));
1205a3a2e76cSKAMEZAWA Hiroyuki #endif
1206172ba844SBalbir Singh 
12076976675dSArjan van de Ven 	p->default_timer_slack_ns = current->timer_slack_ns;
12086976675dSArjan van de Ven 
12095995477aSAndrea Righi 	task_io_accounting_init(&p->ioac);
12101da177e4SLinus Torvalds 	acct_clear_integrals(p);
12111da177e4SLinus Torvalds 
1212f06febc9SFrank Mayhar 	posix_cpu_timers_init(p);
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
1215924b42d5STomas Janousek 	p->real_start_time = p->start_time;
1216924b42d5STomas Janousek 	monotonic_to_bootbased(&p->real_start_time);
12171da177e4SLinus Torvalds 	p->io_context = NULL;
12181da177e4SLinus Torvalds 	p->audit_context = NULL;
12194714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
1220257058aeSTejun Heo 		threadgroup_change_begin(current);
1221b4f48b63SPaul Menage 	cgroup_fork(p);
12221da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1223846a16bfSLee Schermerhorn 	p->mempolicy = mpol_dup(p->mempolicy);
12241da177e4SLinus Torvalds 	if (IS_ERR(p->mempolicy)) {
12251da177e4SLinus Torvalds 		retval = PTR_ERR(p->mempolicy);
12261da177e4SLinus Torvalds 		p->mempolicy = NULL;
1227b4f48b63SPaul Menage 		goto bad_fork_cleanup_cgroup;
12281da177e4SLinus Torvalds 	}
1229c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(p);
12301da177e4SLinus Torvalds #endif
1231778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS
1232778d3b0fSMichal Hocko 	p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
1233778d3b0fSMichal Hocko 	p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
1234cc9a6c87SMel Gorman 	seqcount_init(&p->mems_allowed_seq);
1235778d3b0fSMichal Hocko #endif
1236de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1237de30a2b3SIngo Molnar 	p->irq_events = 0;
1238b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1239b36e4758SRussell King 	p->hardirqs_enabled = 1;
1240b36e4758SRussell King #else
1241de30a2b3SIngo Molnar 	p->hardirqs_enabled = 0;
1242b36e4758SRussell King #endif
1243de30a2b3SIngo Molnar 	p->hardirq_enable_ip = 0;
1244de30a2b3SIngo Molnar 	p->hardirq_enable_event = 0;
1245de30a2b3SIngo Molnar 	p->hardirq_disable_ip = _THIS_IP_;
1246de30a2b3SIngo Molnar 	p->hardirq_disable_event = 0;
1247de30a2b3SIngo Molnar 	p->softirqs_enabled = 1;
1248de30a2b3SIngo Molnar 	p->softirq_enable_ip = _THIS_IP_;
1249de30a2b3SIngo Molnar 	p->softirq_enable_event = 0;
1250de30a2b3SIngo Molnar 	p->softirq_disable_ip = 0;
1251de30a2b3SIngo Molnar 	p->softirq_disable_event = 0;
1252de30a2b3SIngo Molnar 	p->hardirq_context = 0;
1253de30a2b3SIngo Molnar 	p->softirq_context = 0;
1254de30a2b3SIngo Molnar #endif
1255fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1256fbb9ce95SIngo Molnar 	p->lockdep_depth = 0; /* no locks held yet */
1257fbb9ce95SIngo Molnar 	p->curr_chain_key = 0;
1258fbb9ce95SIngo Molnar 	p->lockdep_recursion = 0;
1259fbb9ce95SIngo Molnar #endif
12601da177e4SLinus Torvalds 
1261408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1262408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1263408894eeSIngo Molnar #endif
1264569b846dSKAMEZAWA Hiroyuki #ifdef CONFIG_CGROUP_MEM_RES_CTLR
1265569b846dSKAMEZAWA Hiroyuki 	p->memcg_batch.do_batch = 0;
1266569b846dSKAMEZAWA Hiroyuki 	p->memcg_batch.memcg = NULL;
1267569b846dSKAMEZAWA Hiroyuki #endif
12680f481406SMarkus Metzger 
12693c90e6e9SSrivatsa Vaddagiri 	/* Perform scheduler related setup. Assign this task to a CPU. */
12703e51e3edSSamir Bellabes 	sched_fork(p);
12716ab423e0SPeter Zijlstra 
1272cdd6c482SIngo Molnar 	retval = perf_event_init_task(p);
12736ab423e0SPeter Zijlstra 	if (retval)
12746ab423e0SPeter Zijlstra 		goto bad_fork_cleanup_policy;
1275fb0a685cSDaniel Rebelo de Oliveira 	retval = audit_alloc(p);
1276fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
1277f1752eecSDavid Howells 		goto bad_fork_cleanup_policy;
12781da177e4SLinus Torvalds 	/* copy all the process information */
1279fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_semundo(clone_flags, p);
1280fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12811da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
1282fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_files(clone_flags, p);
1283fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12841da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
1285fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_fs(clone_flags, p);
1286fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12871da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
1288fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_sighand(clone_flags, p);
1289fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12901da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
1291fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_signal(clone_flags, p);
1292fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12931da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
1294fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_mm(clone_flags, p);
1295fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12961da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
1297fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_namespaces(clone_flags, p);
1298fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
1299d84f4f99SDavid Howells 		goto bad_fork_cleanup_mm;
1300fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_io(clone_flags, p);
1301fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
1302fd0928dfSJens Axboe 		goto bad_fork_cleanup_namespaces;
13036f2c55b8SAlexey Dobriyan 	retval = copy_thread(clone_flags, stack_start, stack_size, p, regs);
13041da177e4SLinus Torvalds 	if (retval)
1305fd0928dfSJens Axboe 		goto bad_fork_cleanup_io;
13061da177e4SLinus Torvalds 
1307425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid) {
1308425fb2b4SPavel Emelyanov 		retval = -ENOMEM;
130961bce0f1SEric W. Biederman 		pid = alloc_pid(p->nsproxy->pid_ns);
1310425fb2b4SPavel Emelyanov 		if (!pid)
1311fd0928dfSJens Axboe 			goto bad_fork_cleanup_io;
1312425fb2b4SPavel Emelyanov 	}
1313425fb2b4SPavel Emelyanov 
1314425fb2b4SPavel Emelyanov 	p->pid = pid_nr(pid);
1315425fb2b4SPavel Emelyanov 	p->tgid = p->pid;
1316425fb2b4SPavel Emelyanov 	if (clone_flags & CLONE_THREAD)
1317425fb2b4SPavel Emelyanov 		p->tgid = current->tgid;
1318425fb2b4SPavel Emelyanov 
13191da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
13201da177e4SLinus Torvalds 	/*
13211da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
13221da177e4SLinus Torvalds 	 */
13231da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
132473c10101SJens Axboe #ifdef CONFIG_BLOCK
132573c10101SJens Axboe 	p->plug = NULL;
132673c10101SJens Axboe #endif
132742b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
13288f17d3a5SIngo Molnar 	p->robust_list = NULL;
13298f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
13308f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
13318f17d3a5SIngo Molnar #endif
1332c87e2837SIngo Molnar 	INIT_LIST_HEAD(&p->pi_state_list);
1333c87e2837SIngo Molnar 	p->pi_state_cache = NULL;
133442b2dd0aSAlexey Dobriyan #endif
13351da177e4SLinus Torvalds 	/*
1336f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1337f9a3879aSGOTO Masanori 	 */
1338f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1339f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1340f9a3879aSGOTO Masanori 
1341f9a3879aSGOTO Masanori 	/*
13426580807dSOleg Nesterov 	 * Syscall tracing and stepping should be turned off in the
13436580807dSOleg Nesterov 	 * child regardless of CLONE_PTRACE.
13441da177e4SLinus Torvalds 	 */
13456580807dSOleg Nesterov 	user_disable_single_step(p);
13461da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1347ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1348ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1349ed75e8d5SLaurent Vivier #endif
13509745512cSArjan van de Ven 	clear_all_latency_tracing(p);
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
13535f8aadd8SOleg Nesterov 	if (clone_flags & CLONE_THREAD)
13545f8aadd8SOleg Nesterov 		p->exit_signal = -1;
13555f8aadd8SOleg Nesterov 	else if (clone_flags & CLONE_PARENT)
13565f8aadd8SOleg Nesterov 		p->exit_signal = current->group_leader->exit_signal;
13575f8aadd8SOleg Nesterov 	else
13585f8aadd8SOleg Nesterov 		p->exit_signal = (clone_flags & CSIGNAL);
13595f8aadd8SOleg Nesterov 
13601da177e4SLinus Torvalds 	p->pdeath_signal = 0;
13611da177e4SLinus Torvalds 	p->exit_state = 0;
13621da177e4SLinus Torvalds 
13639d823e8fSWu Fengguang 	p->nr_dirtied = 0;
13649d823e8fSWu Fengguang 	p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
136583712358SWu Fengguang 	p->dirty_paused_when = 0;
13669d823e8fSWu Fengguang 
13671da177e4SLinus Torvalds 	/*
13681da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
13691da177e4SLinus Torvalds 	 * We dont wake it up yet.
13701da177e4SLinus Torvalds 	 */
13711da177e4SLinus Torvalds 	p->group_leader = p;
137247e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
13731da177e4SLinus Torvalds 
1374b4f48b63SPaul Menage 	/* Now that the task is set up, run cgroup callbacks if
1375b4f48b63SPaul Menage 	 * necessary. We need to run them before the task is visible
1376b4f48b63SPaul Menage 	 * on the tasklist. */
1377b4f48b63SPaul Menage 	cgroup_fork_callbacks(p);
1378b4f48b63SPaul Menage 	cgroup_callbacks_done = 1;
1379b4f48b63SPaul Menage 
13801da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
13811da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
13842d5516cbSOleg Nesterov 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
13851da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
13862d5516cbSOleg Nesterov 		p->parent_exec_id = current->parent_exec_id;
13872d5516cbSOleg Nesterov 	} else {
13881da177e4SLinus Torvalds 		p->real_parent = current;
13892d5516cbSOleg Nesterov 		p->parent_exec_id = current->self_exec_id;
13902d5516cbSOleg Nesterov 	}
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
13934a2c7a78SOleg Nesterov 
13944a2c7a78SOleg Nesterov 	/*
13954a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
13964a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
13974a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
13984a2c7a78SOleg Nesterov 	 * it's process group.
13994a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
14004a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
14014a2c7a78SOleg Nesterov 	*/
14024a2c7a78SOleg Nesterov 	recalc_sigpending();
14034a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
14044a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
14054a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
14064a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
1407f7e8b616SSteven Rostedt 		goto bad_fork_free_pid;
14084a2c7a78SOleg Nesterov 	}
14094a2c7a78SOleg Nesterov 
14103f17da69SOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
1411b3ac022cSOleg Nesterov 		current->signal->nr_threads++;
14124ab6c083SOleg Nesterov 		atomic_inc(&current->signal->live);
1413b3ac022cSOleg Nesterov 		atomic_inc(&current->signal->sigcnt);
14141da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
141547e65328SOleg Nesterov 		list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds 
141873b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
14194b9d33e6STejun Heo 		ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
142245a68628SEric W. Biederman 			if (is_child_reaper(pid))
142330e49c26SPavel Emelyanov 				p->nsproxy->pid_ns->child_reaper = p;
14245cd17569SEric W. Biederman 
1425fea9d175SOleg Nesterov 			p->signal->leader_pid = pid;
14269c9f4dedSAlan Cox 			p->signal->tty = tty_kref_get(current->signal->tty);
14275cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
14285cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_SID, task_session(current));
14299cd80bbbSOleg Nesterov 			list_add_tail(&p->sibling, &p->real_parent->children);
14305e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
1431909ea964SChristoph Lameter 			__this_cpu_inc(process_counts);
14321da177e4SLinus Torvalds 		}
143385868995SSukadev Bhattiprolu 		attach_pid(p, PIDTYPE_PID, pid);
14341da177e4SLinus Torvalds 		nr_threads++;
143573b9ebfeSOleg Nesterov 	}
143673b9ebfeSOleg Nesterov 
14371da177e4SLinus Torvalds 	total_forks++;
14383f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
14391da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1440c13cf856SAndrew Morton 	proc_fork_connector(p);
1441817929ecSPaul Menage 	cgroup_post_fork(p);
14424714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
1443257058aeSTejun Heo 		threadgroup_change_end(current);
1444cdd6c482SIngo Molnar 	perf_event_fork(p);
144543d2b113SKAMEZAWA Hiroyuki 
144643d2b113SKAMEZAWA Hiroyuki 	trace_task_newtask(p, clone_flags);
144743d2b113SKAMEZAWA Hiroyuki 
14481da177e4SLinus Torvalds 	return p;
14491da177e4SLinus Torvalds 
1450425fb2b4SPavel Emelyanov bad_fork_free_pid:
1451425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid)
1452425fb2b4SPavel Emelyanov 		free_pid(pid);
1453fd0928dfSJens Axboe bad_fork_cleanup_io:
1454b69f2292SLouis Rilling 	if (p->io_context)
1455b69f2292SLouis Rilling 		exit_io_context(p);
1456ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces:
1457444f378bSLinus Torvalds 	exit_task_namespaces(p);
14581da177e4SLinus Torvalds bad_fork_cleanup_mm:
1459c9f01245SDavid Rientjes 	if (p->mm)
14601da177e4SLinus Torvalds 		mmput(p->mm);
14611da177e4SLinus Torvalds bad_fork_cleanup_signal:
14624ab6c083SOleg Nesterov 	if (!(clone_flags & CLONE_THREAD))
14631c5354deSMike Galbraith 		free_signal_struct(p->signal);
14641da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1465a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
14661da177e4SLinus Torvalds bad_fork_cleanup_fs:
14671da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
14681da177e4SLinus Torvalds bad_fork_cleanup_files:
14691da177e4SLinus Torvalds 	exit_files(p); /* blocking */
14701da177e4SLinus Torvalds bad_fork_cleanup_semundo:
14711da177e4SLinus Torvalds 	exit_sem(p);
14721da177e4SLinus Torvalds bad_fork_cleanup_audit:
14731da177e4SLinus Torvalds 	audit_free(p);
14741da177e4SLinus Torvalds bad_fork_cleanup_policy:
1475cdd6c482SIngo Molnar 	perf_event_free_task(p);
14761da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1477f0be3d32SLee Schermerhorn 	mpol_put(p->mempolicy);
1478b4f48b63SPaul Menage bad_fork_cleanup_cgroup:
14791da177e4SLinus Torvalds #endif
14804714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
1481257058aeSTejun Heo 		threadgroup_change_end(current);
1482b4f48b63SPaul Menage 	cgroup_exit(p, cgroup_callbacks_done);
148335df17c5SShailabh Nagar 	delayacct_tsk_free(p);
1484a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
14851da177e4SLinus Torvalds bad_fork_cleanup_count:
1486d84f4f99SDavid Howells 	atomic_dec(&p->cred->user->processes);
1487e0e81739SDavid Howells 	exit_creds(p);
14881da177e4SLinus Torvalds bad_fork_free:
14891da177e4SLinus Torvalds 	free_task(p);
1490fe7d37d1SOleg Nesterov fork_out:
1491fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
14946b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
14951da177e4SLinus Torvalds {
14961da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
14971da177e4SLinus Torvalds 	return regs;
14981da177e4SLinus Torvalds }
14991da177e4SLinus Torvalds 
1500f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links)
1501f106eee1SOleg Nesterov {
1502f106eee1SOleg Nesterov 	enum pid_type type;
1503f106eee1SOleg Nesterov 
1504f106eee1SOleg Nesterov 	for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
1505f106eee1SOleg Nesterov 		INIT_HLIST_NODE(&links[type].node); /* not really needed */
1506f106eee1SOleg Nesterov 		links[type].pid = &init_struct_pid;
1507f106eee1SOleg Nesterov 	}
1508f106eee1SOleg Nesterov }
1509f106eee1SOleg Nesterov 
15109abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu)
15111da177e4SLinus Torvalds {
151236c8b586SIngo Molnar 	struct task_struct *task;
15131da177e4SLinus Torvalds 	struct pt_regs regs;
15141da177e4SLinus Torvalds 
151530e49c26SPavel Emelyanov 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL,
151609a05394SRoland McGrath 			    &init_struct_pid, 0);
1517f106eee1SOleg Nesterov 	if (!IS_ERR(task)) {
1518f106eee1SOleg Nesterov 		init_idle_pids(task->pids);
15191da177e4SLinus Torvalds 		init_idle(task, cpu);
1520f106eee1SOleg Nesterov 	}
152173b9ebfeSOleg Nesterov 
15221da177e4SLinus Torvalds 	return task;
15231da177e4SLinus Torvalds }
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds /*
15261da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
15271da177e4SLinus Torvalds  *
15281da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
15291da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
15301da177e4SLinus Torvalds  */
15311da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
15321da177e4SLinus Torvalds 	      unsigned long stack_start,
15331da177e4SLinus Torvalds 	      struct pt_regs *regs,
15341da177e4SLinus Torvalds 	      unsigned long stack_size,
15351da177e4SLinus Torvalds 	      int __user *parent_tidptr,
15361da177e4SLinus Torvalds 	      int __user *child_tidptr)
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds 	struct task_struct *p;
15391da177e4SLinus Torvalds 	int trace = 0;
154092476d7fSEric W. Biederman 	long nr;
15411da177e4SLinus Torvalds 
1542bdff746aSAndrew Morton 	/*
154318b6e041SSerge Hallyn 	 * Do some preliminary argument and permissions checking before we
154418b6e041SSerge Hallyn 	 * actually start allocating stuff
154518b6e041SSerge Hallyn 	 */
154618b6e041SSerge Hallyn 	if (clone_flags & CLONE_NEWUSER) {
154718b6e041SSerge Hallyn 		if (clone_flags & CLONE_THREAD)
154818b6e041SSerge Hallyn 			return -EINVAL;
154918b6e041SSerge Hallyn 		/* hopefully this check will go away when userns support is
155018b6e041SSerge Hallyn 		 * complete
155118b6e041SSerge Hallyn 		 */
15527657d904SSerge E. Hallyn 		if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) ||
15537657d904SSerge E. Hallyn 				!capable(CAP_SETGID))
155418b6e041SSerge Hallyn 			return -EPERM;
155518b6e041SSerge Hallyn 	}
155618b6e041SSerge Hallyn 
155718b6e041SSerge Hallyn 	/*
15584b9d33e6STejun Heo 	 * Determine whether and which event to report to ptracer.  When
15594b9d33e6STejun Heo 	 * called from kernel_thread or CLONE_UNTRACED is explicitly
15604b9d33e6STejun Heo 	 * requested, no event is reported; otherwise, report if the event
15614b9d33e6STejun Heo 	 * for the type of forking is enabled.
156209a05394SRoland McGrath 	 */
15634b9d33e6STejun Heo 	if (likely(user_mode(regs)) && !(clone_flags & CLONE_UNTRACED)) {
15644b9d33e6STejun Heo 		if (clone_flags & CLONE_VFORK)
15654b9d33e6STejun Heo 			trace = PTRACE_EVENT_VFORK;
15664b9d33e6STejun Heo 		else if ((clone_flags & CSIGNAL) != SIGCHLD)
15674b9d33e6STejun Heo 			trace = PTRACE_EVENT_CLONE;
15684b9d33e6STejun Heo 		else
15694b9d33e6STejun Heo 			trace = PTRACE_EVENT_FORK;
15704b9d33e6STejun Heo 
15714b9d33e6STejun Heo 		if (likely(!ptrace_event_enabled(current, trace)))
15724b9d33e6STejun Heo 			trace = 0;
15734b9d33e6STejun Heo 	}
15741da177e4SLinus Torvalds 
1575a6f5e063SSukadev Bhattiprolu 	p = copy_process(clone_flags, stack_start, regs, stack_size,
157609a05394SRoland McGrath 			 child_tidptr, NULL, trace);
15771da177e4SLinus Torvalds 	/*
15781da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
15791da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
15801da177e4SLinus Torvalds 	 */
15811da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
15821da177e4SLinus Torvalds 		struct completion vfork;
15831da177e4SLinus Torvalds 
15840a16b607SMathieu Desnoyers 		trace_sched_process_fork(current, p);
15850a16b607SMathieu Desnoyers 
15866c5f3e7bSPavel Emelyanov 		nr = task_pid_vnr(p);
158730e49c26SPavel Emelyanov 
158830e49c26SPavel Emelyanov 		if (clone_flags & CLONE_PARENT_SETTID)
158930e49c26SPavel Emelyanov 			put_user(nr, parent_tidptr);
1590a6f5e063SSukadev Bhattiprolu 
15911da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
15921da177e4SLinus Torvalds 			p->vfork_done = &vfork;
15931da177e4SLinus Torvalds 			init_completion(&vfork);
1594d68b46feSOleg Nesterov 			get_task_struct(p);
15951da177e4SLinus Torvalds 		}
15961da177e4SLinus Torvalds 
15973e51e3edSSamir Bellabes 		wake_up_new_task(p);
15981da177e4SLinus Torvalds 
15994b9d33e6STejun Heo 		/* forking complete and child started to run, tell ptracer */
16004b9d33e6STejun Heo 		if (unlikely(trace))
16014b9d33e6STejun Heo 			ptrace_event(trace, nr);
160209a05394SRoland McGrath 
16031da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
1604d68b46feSOleg Nesterov 			if (!wait_for_vfork_done(p, &vfork))
1605a288eeccSTejun Heo 				ptrace_event(PTRACE_EVENT_VFORK_DONE, nr);
16069f59ce5dSChuck Ebbert 		}
16071da177e4SLinus Torvalds 	} else {
160892476d7fSEric W. Biederman 		nr = PTR_ERR(p);
16091da177e4SLinus Torvalds 	}
161092476d7fSEric W. Biederman 	return nr;
16111da177e4SLinus Torvalds }
16121da177e4SLinus Torvalds 
16135fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN
16145fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0
16155fd63b30SRavikiran G Thirumalai #endif
16165fd63b30SRavikiran G Thirumalai 
161751cc5068SAlexey Dobriyan static void sighand_ctor(void *data)
1618aa1757f9SOleg Nesterov {
1619aa1757f9SOleg Nesterov 	struct sighand_struct *sighand = data;
1620aa1757f9SOleg Nesterov 
1621aa1757f9SOleg Nesterov 	spin_lock_init(&sighand->siglock);
1622b8fceee1SDavide Libenzi 	init_waitqueue_head(&sighand->signalfd_wqh);
1623fba2afaaSDavide Libenzi }
1624aa1757f9SOleg Nesterov 
16251da177e4SLinus Torvalds void __init proc_caches_init(void)
16261da177e4SLinus Torvalds {
16271da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
16281da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
16292dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
16302dff4405SVegard Nossum 			SLAB_NOTRACK, sighand_ctor);
16311da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
16321da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
16332dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
16341da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
16351da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
16362dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
16371da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
16381da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
16392dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
16406345d24dSLinus Torvalds 	/*
16416345d24dSLinus Torvalds 	 * FIXME! The "sizeof(struct mm_struct)" currently includes the
16426345d24dSLinus Torvalds 	 * whole struct cpumask for the OFFSTACK case. We could change
16436345d24dSLinus Torvalds 	 * this to *only* allocate as much of it as required by the
16446345d24dSLinus Torvalds 	 * maximum number of CPU's we can ever have.  The cpumask_allocation
16456345d24dSLinus Torvalds 	 * is at the end of the structure, exactly for that reason.
16466345d24dSLinus Torvalds 	 */
16471da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
16485fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
16492dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
165033e5d769SDavid Howells 	vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC);
16518feae131SDavid Howells 	mmap_init();
165266577193SAl Viro 	nsproxy_cache_init();
16531da177e4SLinus Torvalds }
1654cf2e340fSJANAK DESAI 
1655cf2e340fSJANAK DESAI /*
16569bfb23fcSOleg Nesterov  * Check constraints on flags passed to the unshare system call.
1657cf2e340fSJANAK DESAI  */
16589bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags)
1659cf2e340fSJANAK DESAI {
16609bfb23fcSOleg Nesterov 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
16619bfb23fcSOleg Nesterov 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
16629bfb23fcSOleg Nesterov 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET))
1663cf2e340fSJANAK DESAI 		return -EINVAL;
16649bfb23fcSOleg Nesterov 	/*
16659bfb23fcSOleg Nesterov 	 * Not implemented, but pretend it works if there is nothing to
16669bfb23fcSOleg Nesterov 	 * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND
16679bfb23fcSOleg Nesterov 	 * needs to unshare vm.
16689bfb23fcSOleg Nesterov 	 */
16699bfb23fcSOleg Nesterov 	if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
16709bfb23fcSOleg Nesterov 		/* FIXME: get_task_mm() increments ->mm_users */
16719bfb23fcSOleg Nesterov 		if (atomic_read(&current->mm->mm_users) > 1)
16729bfb23fcSOleg Nesterov 			return -EINVAL;
16739bfb23fcSOleg Nesterov 	}
1674cf2e340fSJANAK DESAI 
1675cf2e340fSJANAK DESAI 	return 0;
1676cf2e340fSJANAK DESAI }
1677cf2e340fSJANAK DESAI 
1678cf2e340fSJANAK DESAI /*
167999d1419dSJANAK DESAI  * Unshare the filesystem structure if it is being shared
1680cf2e340fSJANAK DESAI  */
1681cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1682cf2e340fSJANAK DESAI {
1683cf2e340fSJANAK DESAI 	struct fs_struct *fs = current->fs;
1684cf2e340fSJANAK DESAI 
1685498052bbSAl Viro 	if (!(unshare_flags & CLONE_FS) || !fs)
1686498052bbSAl Viro 		return 0;
1687498052bbSAl Viro 
1688498052bbSAl Viro 	/* don't need lock here; in the worst case we'll do useless copy */
1689498052bbSAl Viro 	if (fs->users == 1)
1690498052bbSAl Viro 		return 0;
1691498052bbSAl Viro 
1692498052bbSAl Viro 	*new_fsp = copy_fs_struct(fs);
169399d1419dSJANAK DESAI 	if (!*new_fsp)
169499d1419dSJANAK DESAI 		return -ENOMEM;
1695cf2e340fSJANAK DESAI 
1696cf2e340fSJANAK DESAI 	return 0;
1697cf2e340fSJANAK DESAI }
1698cf2e340fSJANAK DESAI 
1699cf2e340fSJANAK DESAI /*
1700a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1701cf2e340fSJANAK DESAI  */
1702cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1703cf2e340fSJANAK DESAI {
1704cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1705a016f338SJANAK DESAI 	int error = 0;
1706cf2e340fSJANAK DESAI 
1707cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1708a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1709a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1710a016f338SJANAK DESAI 		if (!*new_fdp)
1711a016f338SJANAK DESAI 			return error;
1712a016f338SJANAK DESAI 	}
1713cf2e340fSJANAK DESAI 
1714cf2e340fSJANAK DESAI 	return 0;
1715cf2e340fSJANAK DESAI }
1716cf2e340fSJANAK DESAI 
1717cf2e340fSJANAK DESAI /*
1718cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1719cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1720cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1721cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1722cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1723cf2e340fSJANAK DESAI  * task_struct.
1724cf2e340fSJANAK DESAI  */
17256559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1726cf2e340fSJANAK DESAI {
1727cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1728cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1729cf7b708cSPavel Emelyanov 	struct nsproxy *new_nsproxy = NULL;
17309edff4abSManfred Spraul 	int do_sysvsem = 0;
17319bfb23fcSOleg Nesterov 	int err;
1732cf2e340fSJANAK DESAI 
17339bfb23fcSOleg Nesterov 	err = check_unshare_flags(unshare_flags);
17349bfb23fcSOleg Nesterov 	if (err)
173506f9d4f9SEric W. Biederman 		goto bad_unshare_out;
173606f9d4f9SEric W. Biederman 
17376013f67fSManfred Spraul 	/*
17389bfb23fcSOleg Nesterov 	 * If unsharing namespace, must also unshare filesystem information.
17399bfb23fcSOleg Nesterov 	 */
17409bfb23fcSOleg Nesterov 	if (unshare_flags & CLONE_NEWNS)
17419bfb23fcSOleg Nesterov 		unshare_flags |= CLONE_FS;
17429bfb23fcSOleg Nesterov 	/*
17436013f67fSManfred Spraul 	 * CLONE_NEWIPC must also detach from the undolist: after switching
17446013f67fSManfred Spraul 	 * to a new ipc namespace, the semaphore arrays from the old
17456013f67fSManfred Spraul 	 * namespace are unreachable.
17466013f67fSManfred Spraul 	 */
17476013f67fSManfred Spraul 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
17489edff4abSManfred Spraul 		do_sysvsem = 1;
1749fb0a685cSDaniel Rebelo de Oliveira 	err = unshare_fs(unshare_flags, &new_fs);
1750fb0a685cSDaniel Rebelo de Oliveira 	if (err)
17519bfb23fcSOleg Nesterov 		goto bad_unshare_out;
1752fb0a685cSDaniel Rebelo de Oliveira 	err = unshare_fd(unshare_flags, &new_fd);
1753fb0a685cSDaniel Rebelo de Oliveira 	if (err)
17549bfb23fcSOleg Nesterov 		goto bad_unshare_cleanup_fs;
1755fb0a685cSDaniel Rebelo de Oliveira 	err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, new_fs);
1756fb0a685cSDaniel Rebelo de Oliveira 	if (err)
17579edff4abSManfred Spraul 		goto bad_unshare_cleanup_fd;
1758cf2e340fSJANAK DESAI 
17599bfb23fcSOleg Nesterov 	if (new_fs || new_fd || do_sysvsem || new_nsproxy) {
17609edff4abSManfred Spraul 		if (do_sysvsem) {
17619edff4abSManfred Spraul 			/*
17629edff4abSManfred Spraul 			 * CLONE_SYSVSEM is equivalent to sys_exit().
17639edff4abSManfred Spraul 			 */
17649edff4abSManfred Spraul 			exit_sem(current);
17659edff4abSManfred Spraul 		}
1766ab516013SSerge E. Hallyn 
1767c0b2fc31SSerge Hallyn 		if (new_nsproxy) {
1768cf7b708cSPavel Emelyanov 			switch_task_namespaces(current, new_nsproxy);
1769cf7b708cSPavel Emelyanov 			new_nsproxy = NULL;
1770c0b2fc31SSerge Hallyn 		}
1771cf2e340fSJANAK DESAI 
1772cf7b708cSPavel Emelyanov 		task_lock(current);
1773cf7b708cSPavel Emelyanov 
1774cf2e340fSJANAK DESAI 		if (new_fs) {
1775cf2e340fSJANAK DESAI 			fs = current->fs;
17762a4419b5SNick Piggin 			spin_lock(&fs->lock);
1777cf2e340fSJANAK DESAI 			current->fs = new_fs;
1778498052bbSAl Viro 			if (--fs->users)
1779498052bbSAl Viro 				new_fs = NULL;
1780498052bbSAl Viro 			else
1781cf2e340fSJANAK DESAI 				new_fs = fs;
17822a4419b5SNick Piggin 			spin_unlock(&fs->lock);
1783cf2e340fSJANAK DESAI 		}
1784cf2e340fSJANAK DESAI 
1785cf2e340fSJANAK DESAI 		if (new_fd) {
1786cf2e340fSJANAK DESAI 			fd = current->files;
1787cf2e340fSJANAK DESAI 			current->files = new_fd;
1788cf2e340fSJANAK DESAI 			new_fd = fd;
1789cf2e340fSJANAK DESAI 		}
1790cf2e340fSJANAK DESAI 
1791cf2e340fSJANAK DESAI 		task_unlock(current);
1792cf2e340fSJANAK DESAI 	}
1793cf2e340fSJANAK DESAI 
1794c0b2fc31SSerge Hallyn 	if (new_nsproxy)
1795444f378bSLinus Torvalds 		put_nsproxy(new_nsproxy);
1796c0b2fc31SSerge Hallyn 
1797cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
1798cf2e340fSJANAK DESAI 	if (new_fd)
1799cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
1800cf2e340fSJANAK DESAI 
1801cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
1802cf2e340fSJANAK DESAI 	if (new_fs)
1803498052bbSAl Viro 		free_fs_struct(new_fs);
1804cf2e340fSJANAK DESAI 
1805cf2e340fSJANAK DESAI bad_unshare_out:
1806cf2e340fSJANAK DESAI 	return err;
1807cf2e340fSJANAK DESAI }
18083b125388SAl Viro 
18093b125388SAl Viro /*
18103b125388SAl Viro  *	Helper to unshare the files of the current task.
18113b125388SAl Viro  *	We don't want to expose copy_files internals to
18123b125388SAl Viro  *	the exec layer of the kernel.
18133b125388SAl Viro  */
18143b125388SAl Viro 
18153b125388SAl Viro int unshare_files(struct files_struct **displaced)
18163b125388SAl Viro {
18173b125388SAl Viro 	struct task_struct *task = current;
181850704516SAl Viro 	struct files_struct *copy = NULL;
18193b125388SAl Viro 	int error;
18203b125388SAl Viro 
18213b125388SAl Viro 	error = unshare_fd(CLONE_FILES, &copy);
18223b125388SAl Viro 	if (error || !copy) {
18233b125388SAl Viro 		*displaced = NULL;
18243b125388SAl Viro 		return error;
18253b125388SAl Viro 	}
18263b125388SAl Viro 	*displaced = task->files;
18273b125388SAl Viro 	task_lock(task);
18283b125388SAl Viro 	task->files = copy;
18293b125388SAl Viro 	task_unlock(task);
18303b125388SAl Viro 	return 0;
18313b125388SAl Viro }
1832