xref: /openbmc/linux/kernel/fork.c (revision 5e2bf0142231194d36fdc9596b36a261ed2b9fe7)
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>
50*5e2bf014SMike Galbraith #include <linux/proc_fs.h>
511da177e4SLinus Torvalds #include <linux/profile.h>
521da177e4SLinus Torvalds #include <linux/rmap.h>
53f8af4da3SHugh Dickins #include <linux/ksm.h>
541da177e4SLinus Torvalds #include <linux/acct.h>
558f0ab514SJay Lan #include <linux/tsacct_kern.h>
569f46080cSMatt Helsley #include <linux/cn_proc.h>
57ba96a0c8SRafael J. Wysocki #include <linux/freezer.h>
58ca74e92bSShailabh Nagar #include <linux/delayacct.h>
59ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h>
600a425405SArjan van de Ven #include <linux/random.h>
61522ed776SMiloslav Trmac #include <linux/tty.h>
62fd0928dfSJens Axboe #include <linux/blkdev.h>
635ad4e53bSAl Viro #include <linux/fs_struct.h>
647c9f8861SEric Sandeen #include <linux/magic.h>
65cdd6c482SIngo Molnar #include <linux/perf_event.h>
6642c4ab41SStanislaw Gruszka #include <linux/posix-timers.h>
678e7cac79SAvi Kivity #include <linux/user-return-notifier.h>
683d5992d2SYing Han #include <linux/oom.h>
69ba76149fSAndrea Arcangeli #include <linux/khugepaged.h>
70d80e731eSOleg Nesterov #include <linux/signalfd.h>
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #include <asm/pgtable.h>
731da177e4SLinus Torvalds #include <asm/pgalloc.h>
741da177e4SLinus Torvalds #include <asm/uaccess.h>
751da177e4SLinus Torvalds #include <asm/mmu_context.h>
761da177e4SLinus Torvalds #include <asm/cacheflush.h>
771da177e4SLinus Torvalds #include <asm/tlbflush.h>
781da177e4SLinus Torvalds 
79ad8d75ffSSteven Rostedt #include <trace/events/sched.h>
80ad8d75ffSSteven Rostedt 
8143d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS
8243d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h>
8343d2b113SKAMEZAWA Hiroyuki 
841da177e4SLinus Torvalds /*
851da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
881da177e4SLinus Torvalds int nr_threads;			/* The idle threads do not count.. */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
95db1466b3SPaul E. McKenney 
96db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU
97db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void)
98db1466b3SPaul E. McKenney {
99db1466b3SPaul E. McKenney 	return lockdep_is_held(&tasklist_lock);
100db1466b3SPaul E. McKenney }
101db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
102db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds int nr_processes(void)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	int cpu;
1071da177e4SLinus Torvalds 	int total = 0;
1081da177e4SLinus Torvalds 
1091d510750SIan Campbell 	for_each_possible_cpu(cpu)
1101da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	return total;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
116504f52b5SEric Dumazet # define alloc_task_struct_node(node)		\
117504f52b5SEric Dumazet 		kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node)
118504f52b5SEric Dumazet # define free_task_struct(tsk)			\
119504f52b5SEric Dumazet 		kmem_cache_free(task_struct_cachep, (tsk))
120e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep;
1211da177e4SLinus Torvalds #endif
1221da177e4SLinus Torvalds 
123b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR
124b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
125b6a84016SEric Dumazet 						  int node)
126b69c49b7SFUJITA Tomonori {
127b69c49b7SFUJITA Tomonori #ifdef CONFIG_DEBUG_STACK_USAGE
128b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL | __GFP_ZERO;
129b69c49b7SFUJITA Tomonori #else
130b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL;
131b69c49b7SFUJITA Tomonori #endif
132b6a84016SEric Dumazet 	struct page *page = alloc_pages_node(node, mask, THREAD_SIZE_ORDER);
133b6a84016SEric Dumazet 
134b6a84016SEric Dumazet 	return page ? page_address(page) : NULL;
135b69c49b7SFUJITA Tomonori }
136b69c49b7SFUJITA Tomonori 
137b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti)
138b69c49b7SFUJITA Tomonori {
139b69c49b7SFUJITA Tomonori 	free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
140b69c49b7SFUJITA Tomonori }
141b69c49b7SFUJITA Tomonori #endif
142b69c49b7SFUJITA Tomonori 
1431da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
144e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
147e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
150e18b890bSChristoph Lameter struct kmem_cache *files_cachep;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
153e18b890bSChristoph Lameter struct kmem_cache *fs_cachep;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
156e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
159e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep;
1601da177e4SLinus Torvalds 
161c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account)
162c6a7f572SKOSAKI Motohiro {
163c6a7f572SKOSAKI Motohiro 	struct zone *zone = page_zone(virt_to_page(ti));
164c6a7f572SKOSAKI Motohiro 
165c6a7f572SKOSAKI Motohiro 	mod_zone_page_state(zone, NR_KERNEL_STACK, account);
166c6a7f572SKOSAKI Motohiro }
167c6a7f572SKOSAKI Motohiro 
1681da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1691da177e4SLinus Torvalds {
170c6a7f572SKOSAKI Motohiro 	account_kernel_stack(tsk->stack, -1);
171f7e4217bSRoman Zippel 	free_thread_info(tsk->stack);
17223f78d4aSIngo Molnar 	rt_mutex_debug_task_free(tsk);
173fb52607aSFrederic Weisbecker 	ftrace_graph_exit_task(tsk);
1741da177e4SLinus Torvalds 	free_task_struct(tsk);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1771da177e4SLinus Torvalds 
178ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig)
179ea6d290cSOleg Nesterov {
18097101eb4SOleg Nesterov 	taskstats_tgid_free(sig);
1811c5354deSMike Galbraith 	sched_autogroup_exit(sig);
182ea6d290cSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
183ea6d290cSOleg Nesterov }
184ea6d290cSOleg Nesterov 
185ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig)
186ea6d290cSOleg Nesterov {
1871c5354deSMike Galbraith 	if (atomic_dec_and_test(&sig->sigcnt))
188ea6d290cSOleg Nesterov 		free_signal_struct(sig);
189ea6d290cSOleg Nesterov }
190ea6d290cSOleg Nesterov 
191158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
1921da177e4SLinus Torvalds {
193270f722dSEugene Teo 	WARN_ON(!tsk->exit_state);
1941da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1951da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1961da177e4SLinus Torvalds 
1971a2a4d06SKees Cook 	security_task_free(tsk);
198e0e81739SDavid Howells 	exit_creds(tsk);
19935df17c5SShailabh Nagar 	delayacct_tsk_free(tsk);
200ea6d290cSOleg Nesterov 	put_signal_struct(tsk->signal);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
2031da177e4SLinus Torvalds 		free_task(tsk);
2041da177e4SLinus Torvalds }
20577c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct);
2061da177e4SLinus Torvalds 
2072adee9b3SSuresh Siddha /*
2082adee9b3SSuresh Siddha  * macro override instead of weak attribute alias, to workaround
2092adee9b3SSuresh Siddha  * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
2102adee9b3SSuresh Siddha  */
2112adee9b3SSuresh Siddha #ifndef arch_task_cache_init
2122adee9b3SSuresh Siddha #define arch_task_cache_init()
2132adee9b3SSuresh Siddha #endif
21461c4628bSSuresh Siddha 
2151da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
2181da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
2191da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
2201da177e4SLinus Torvalds #endif
2211da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
2221da177e4SLinus Torvalds 	task_struct_cachep =
2231da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
2242dff4405SVegard Nossum 			ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL);
2251da177e4SLinus Torvalds #endif
2261da177e4SLinus Torvalds 
22761c4628bSSuresh Siddha 	/* do the arch specific task caches init */
22861c4628bSSuresh Siddha 	arch_task_cache_init();
22961c4628bSSuresh Siddha 
2301da177e4SLinus Torvalds 	/*
2311da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
2321da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
2331da177e4SLinus Torvalds 	 * of memory.
2341da177e4SLinus Torvalds 	 */
2351da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	/*
2381da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
2391da177e4SLinus Torvalds 	 */
2401da177e4SLinus Torvalds 	if (max_threads < 20)
2411da177e4SLinus Torvalds 		max_threads = 20;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
2441da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
2451da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
2461da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
24961c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
25061c4628bSSuresh Siddha 					       struct task_struct *src)
25161c4628bSSuresh Siddha {
25261c4628bSSuresh Siddha 	*dst = *src;
25361c4628bSSuresh Siddha 	return 0;
25461c4628bSSuresh Siddha }
25561c4628bSSuresh Siddha 
2561da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	struct task_struct *tsk;
2591da177e4SLinus Torvalds 	struct thread_info *ti;
2607c9f8861SEric Sandeen 	unsigned long *stackend;
261207205a2SEric Dumazet 	int node = tsk_fork_get_node(orig);
2623e26c149SPeter Zijlstra 	int err;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	prepare_to_copy(orig);
2651da177e4SLinus Torvalds 
266504f52b5SEric Dumazet 	tsk = alloc_task_struct_node(node);
2671da177e4SLinus Torvalds 	if (!tsk)
2681da177e4SLinus Torvalds 		return NULL;
2691da177e4SLinus Torvalds 
270b6a84016SEric Dumazet 	ti = alloc_thread_info_node(tsk, node);
2711da177e4SLinus Torvalds 	if (!ti) {
2721da177e4SLinus Torvalds 		free_task_struct(tsk);
2731da177e4SLinus Torvalds 		return NULL;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
27661c4628bSSuresh Siddha 	err = arch_dup_task_struct(tsk, orig);
27761c4628bSSuresh Siddha 	if (err)
27861c4628bSSuresh Siddha 		goto out;
27961c4628bSSuresh Siddha 
280f7e4217bSRoman Zippel 	tsk->stack = ti;
2813e26c149SPeter Zijlstra 
28210ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
2838e7cac79SAvi Kivity 	clear_user_return_notifier(tsk);
284f26f9affSMike Galbraith 	clear_tsk_need_resched(tsk);
2857c9f8861SEric Sandeen 	stackend = end_of_stack(tsk);
2867c9f8861SEric Sandeen 	*stackend = STACK_END_MAGIC;	/* for overflow detection */
2871da177e4SLinus Torvalds 
2880a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
2890a425405SArjan van de Ven 	tsk->stack_canary = get_random_int();
2900a425405SArjan van de Ven #endif
2910a425405SArjan van de Ven 
292fb0a685cSDaniel Rebelo de Oliveira 	/*
293fb0a685cSDaniel Rebelo de Oliveira 	 * One for us, one for whoever does the "release_task()" (usually
294fb0a685cSDaniel Rebelo de Oliveira 	 * parent)
295fb0a685cSDaniel Rebelo de Oliveira 	 */
2961da177e4SLinus Torvalds 	atomic_set(&tsk->usage, 2);
2976c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
2982056a782SJens Axboe 	tsk->btrace_seq = 0;
2996c5c9341SAlexey Dobriyan #endif
300a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
301c6a7f572SKOSAKI Motohiro 
302c6a7f572SKOSAKI Motohiro 	account_kernel_stack(ti, 1);
303c6a7f572SKOSAKI Motohiro 
3041da177e4SLinus Torvalds 	return tsk;
30561c4628bSSuresh Siddha 
30661c4628bSSuresh Siddha out:
30761c4628bSSuresh Siddha 	free_thread_info(ti);
30861c4628bSSuresh Siddha 	free_task_struct(tsk);
30961c4628bSSuresh Siddha 	return NULL;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds #ifdef CONFIG_MMU
313a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
3141da177e4SLinus Torvalds {
315297c5eeeSLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
3161da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
3171da177e4SLinus Torvalds 	int retval;
3181da177e4SLinus Torvalds 	unsigned long charge;
3191da177e4SLinus Torvalds 	struct mempolicy *pol;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
322ec8c0446SRalf Baechle 	flush_cache_dup_mm(oldmm);
323ad339451SIngo Molnar 	/*
324ad339451SIngo Molnar 	 * Not linked in yet - no deadlock potential:
325ad339451SIngo Molnar 	 */
326ad339451SIngo Molnar 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
3277ee78232SHugh Dickins 
3281da177e4SLinus Torvalds 	mm->locked_vm = 0;
3291da177e4SLinus Torvalds 	mm->mmap = NULL;
3301da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
3311da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
3321363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
3331da177e4SLinus Torvalds 	mm->map_count = 0;
33494894244SRusty Russell 	cpumask_clear(mm_cpumask(mm));
3351da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
3361da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
3371da177e4SLinus Torvalds 	rb_parent = NULL;
3381da177e4SLinus Torvalds 	pprev = &mm->mmap;
339f8af4da3SHugh Dickins 	retval = ksm_fork(mm, oldmm);
340f8af4da3SHugh Dickins 	if (retval)
341f8af4da3SHugh Dickins 		goto out;
342ba76149fSAndrea Arcangeli 	retval = khugepaged_fork(mm, oldmm);
343ba76149fSAndrea Arcangeli 	if (retval)
344ba76149fSAndrea Arcangeli 		goto out;
3451da177e4SLinus Torvalds 
346297c5eeeSLinus Torvalds 	prev = NULL;
347fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
3481da177e4SLinus Torvalds 		struct file *file;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
3513b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
3523b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
353ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
3543b6bfcdbSHugh Dickins 								-pages);
3551da177e4SLinus Torvalds 			continue;
3561da177e4SLinus Torvalds 		}
3571da177e4SLinus Torvalds 		charge = 0;
3581da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
3591da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
360191c5424SAl Viro 			if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
3611da177e4SLinus Torvalds 				goto fail_nomem;
3621da177e4SLinus Torvalds 			charge = len;
3631da177e4SLinus Torvalds 		}
364e94b1766SChristoph Lameter 		tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
3651da177e4SLinus Torvalds 		if (!tmp)
3661da177e4SLinus Torvalds 			goto fail_nomem;
3671da177e4SLinus Torvalds 		*tmp = *mpnt;
3685beb4930SRik van Riel 		INIT_LIST_HEAD(&tmp->anon_vma_chain);
369846a16bfSLee Schermerhorn 		pol = mpol_dup(vma_policy(mpnt));
3701da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
3711da177e4SLinus Torvalds 		if (IS_ERR(pol))
3721da177e4SLinus Torvalds 			goto fail_nomem_policy;
3731da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
374a247c3a9SAndrea Arcangeli 		tmp->vm_mm = mm;
3755beb4930SRik van Riel 		if (anon_vma_fork(tmp, mpnt))
3765beb4930SRik van Riel 			goto fail_nomem_anon_vma_fork;
3771da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
378297c5eeeSLinus Torvalds 		tmp->vm_next = tmp->vm_prev = NULL;
3791da177e4SLinus Torvalds 		file = tmp->vm_file;
3801da177e4SLinus Torvalds 		if (file) {
381f3a43f3fSJosef "Jeff" Sipek 			struct inode *inode = file->f_path.dentry->d_inode;
382b88ed205SHugh Dickins 			struct address_space *mapping = file->f_mapping;
383b88ed205SHugh Dickins 
3841da177e4SLinus Torvalds 			get_file(file);
3851da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
3861da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
3873d48ae45SPeter Zijlstra 			mutex_lock(&mapping->i_mmap_mutex);
388b88ed205SHugh Dickins 			if (tmp->vm_flags & VM_SHARED)
389b88ed205SHugh Dickins 				mapping->i_mmap_writable++;
390b88ed205SHugh Dickins 			flush_dcache_mmap_lock(mapping);
391b88ed205SHugh Dickins 			/* insert tmp into the share list, just after mpnt */
3921da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
393b88ed205SHugh Dickins 			flush_dcache_mmap_unlock(mapping);
3943d48ae45SPeter Zijlstra 			mutex_unlock(&mapping->i_mmap_mutex);
3951da177e4SLinus Torvalds 		}
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 		/*
398a1e78772SMel Gorman 		 * Clear hugetlb-related page reserves for children. This only
399a1e78772SMel Gorman 		 * affects MAP_PRIVATE mappings. Faults generated by the child
400a1e78772SMel Gorman 		 * are not guaranteed to succeed, even if read-only
401a1e78772SMel Gorman 		 */
402a1e78772SMel Gorman 		if (is_vm_hugetlb_page(tmp))
403a1e78772SMel Gorman 			reset_vma_resv_huge_pages(tmp);
404a1e78772SMel Gorman 
405a1e78772SMel Gorman 		/*
4067ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
4071da177e4SLinus Torvalds 		 */
4081da177e4SLinus Torvalds 		*pprev = tmp;
4091da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
410297c5eeeSLinus Torvalds 		tmp->vm_prev = prev;
411297c5eeeSLinus Torvalds 		prev = tmp;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
4141da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
4151da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 		mm->map_count++;
4180b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
4211da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 		if (retval)
4241da177e4SLinus Torvalds 			goto out;
4251da177e4SLinus Torvalds 	}
426d6dd61c8SJeremy Fitzhardinge 	/* a new mm has just been created */
427d6dd61c8SJeremy Fitzhardinge 	arch_dup_mmap(oldmm, mm);
4281da177e4SLinus Torvalds 	retval = 0;
4291da177e4SLinus Torvalds out:
4307ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
431fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
4321da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
4331da177e4SLinus Torvalds 	return retval;
4345beb4930SRik van Riel fail_nomem_anon_vma_fork:
4355beb4930SRik van Riel 	mpol_put(pol);
4361da177e4SLinus Torvalds fail_nomem_policy:
4371da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
4381da177e4SLinus Torvalds fail_nomem:
4391da177e4SLinus Torvalds 	retval = -ENOMEM;
4401da177e4SLinus Torvalds 	vm_unacct_memory(charge);
4411da177e4SLinus Torvalds 	goto out;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
4471da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
4481da177e4SLinus Torvalds 		return -ENOMEM;
4491da177e4SLinus Torvalds 	return 0;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm)
4531da177e4SLinus Torvalds {
4545e541973SBenjamin Herrenschmidt 	pgd_free(mm, mm->pgd);
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds #else
4571da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
4581da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
4591da177e4SLinus Torvalds #define mm_free_pgd(mm)
4601da177e4SLinus Torvalds #endif /* CONFIG_MMU */
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
4631da177e4SLinus Torvalds 
464e94b1766SChristoph Lameter #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
4651da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
4661da177e4SLinus Torvalds 
4674cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
4684cb0e11bSHidehiro Kawai 
4694cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s)
4704cb0e11bSHidehiro Kawai {
4714cb0e11bSHidehiro Kawai 	default_dump_filter =
4724cb0e11bSHidehiro Kawai 		(simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
4734cb0e11bSHidehiro Kawai 		MMF_DUMP_FILTER_MASK;
4744cb0e11bSHidehiro Kawai 	return 1;
4754cb0e11bSHidehiro Kawai }
4764cb0e11bSHidehiro Kawai 
4774cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup);
4784cb0e11bSHidehiro Kawai 
4791da177e4SLinus Torvalds #include <linux/init_task.h>
4801da177e4SLinus Torvalds 
481858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm)
482858f0993SAlexey Dobriyan {
483858f0993SAlexey Dobriyan #ifdef CONFIG_AIO
484858f0993SAlexey Dobriyan 	spin_lock_init(&mm->ioctx_lock);
485858f0993SAlexey Dobriyan 	INIT_HLIST_HEAD(&mm->ioctx_list);
486858f0993SAlexey Dobriyan #endif
487858f0993SAlexey Dobriyan }
488858f0993SAlexey Dobriyan 
48978fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
4921da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
4931da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
4941da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
495f8af4da3SHugh Dickins 	mm->flags = (current->mm) ?
496f8af4da3SHugh Dickins 		(current->mm->flags & MMF_INIT_MASK) : default_dump_filter;
497999d9fc1SOleg Nesterov 	mm->core_state = NULL;
4981da177e4SLinus Torvalds 	mm->nr_ptes = 0;
499d559db08SKAMEZAWA Hiroyuki 	memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
5001da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
5011da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
5021363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
503858f0993SAlexey Dobriyan 	mm_init_aio(mm);
504cf475ad2SBalbir Singh 	mm_init_owner(mm, p);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
5071da177e4SLinus Torvalds 		mm->def_flags = 0;
508cddb8a5cSAndrea Arcangeli 		mmu_notifier_mm_init(mm);
5091da177e4SLinus Torvalds 		return mm;
5101da177e4SLinus Torvalds 	}
51178fb7466SPavel Emelianov 
5121da177e4SLinus Torvalds 	free_mm(mm);
5131da177e4SLinus Torvalds 	return NULL;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
516c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm)
517c3f0327fSKonstantin Khlebnikov {
518c3f0327fSKonstantin Khlebnikov 	int i;
519c3f0327fSKonstantin Khlebnikov 
520c3f0327fSKonstantin Khlebnikov 	for (i = 0; i < NR_MM_COUNTERS; i++) {
521c3f0327fSKonstantin Khlebnikov 		long x = atomic_long_read(&mm->rss_stat.count[i]);
522c3f0327fSKonstantin Khlebnikov 
523c3f0327fSKonstantin Khlebnikov 		if (unlikely(x))
524c3f0327fSKonstantin Khlebnikov 			printk(KERN_ALERT "BUG: Bad rss-counter state "
525c3f0327fSKonstantin Khlebnikov 					  "mm:%p idx:%d val:%ld\n", mm, i, x);
526c3f0327fSKonstantin Khlebnikov 	}
527c3f0327fSKonstantin Khlebnikov 
528c3f0327fSKonstantin Khlebnikov #ifdef CONFIG_TRANSPARENT_HUGEPAGE
529c3f0327fSKonstantin Khlebnikov 	VM_BUG_ON(mm->pmd_huge_pte);
530c3f0327fSKonstantin Khlebnikov #endif
531c3f0327fSKonstantin Khlebnikov }
532c3f0327fSKonstantin Khlebnikov 
5331da177e4SLinus Torvalds /*
5341da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
5351da177e4SLinus Torvalds  */
5361da177e4SLinus Torvalds struct mm_struct *mm_alloc(void)
5371da177e4SLinus Torvalds {
5381da177e4SLinus Torvalds 	struct mm_struct *mm;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	mm = allocate_mm();
541de03c72cSKOSAKI Motohiro 	if (!mm)
542de03c72cSKOSAKI Motohiro 		return NULL;
543de03c72cSKOSAKI Motohiro 
5441da177e4SLinus Torvalds 	memset(mm, 0, sizeof(*mm));
5456345d24dSLinus Torvalds 	mm_init_cpumask(mm);
5466345d24dSLinus Torvalds 	return mm_init(mm, current);
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /*
5501da177e4SLinus Torvalds  * Called when the last reference to the mm
5511da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
5521da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
5531da177e4SLinus Torvalds  */
5547ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
5571da177e4SLinus Torvalds 	mm_free_pgd(mm);
5581da177e4SLinus Torvalds 	destroy_context(mm);
559cddb8a5cSAndrea Arcangeli 	mmu_notifier_mm_destroy(mm);
560c3f0327fSKonstantin Khlebnikov 	check_mm(mm);
5611da177e4SLinus Torvalds 	free_mm(mm);
5621da177e4SLinus Torvalds }
5636d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds /*
5661da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
5671da177e4SLinus Torvalds  */
5681da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
5691da177e4SLinus Torvalds {
5700ae26f1bSAndrew Morton 	might_sleep();
5710ae26f1bSAndrew Morton 
5721da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
5731da177e4SLinus Torvalds 		exit_aio(mm);
5741c2fb7a4SAndrea Arcangeli 		ksm_exit(mm);
575ba76149fSAndrea Arcangeli 		khugepaged_exit(mm); /* must run before exit_mmap */
5761da177e4SLinus Torvalds 		exit_mmap(mm);
577925d1c40SMatt Helsley 		set_mm_exe_file(mm, NULL);
5781da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
5791da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
5801da177e4SLinus Torvalds 			list_del(&mm->mmlist);
5811da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
5821da177e4SLinus Torvalds 		}
5831da177e4SLinus Torvalds 		put_swap_token(mm);
584801460d0SHiroshi Shimamoto 		if (mm->binfmt)
585801460d0SHiroshi Shimamoto 			module_put(mm->binfmt->module);
5861da177e4SLinus Torvalds 		mmdrop(mm);
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
5901da177e4SLinus Torvalds 
59138646013SJiri Slaby /*
59238646013SJiri Slaby  * We added or removed a vma mapping the executable. The vmas are only mapped
59338646013SJiri Slaby  * during exec and are not mapped with the mmap system call.
59438646013SJiri Slaby  * Callers must hold down_write() on the mm's mmap_sem for these
59538646013SJiri Slaby  */
59638646013SJiri Slaby void added_exe_file_vma(struct mm_struct *mm)
59738646013SJiri Slaby {
59838646013SJiri Slaby 	mm->num_exe_file_vmas++;
59938646013SJiri Slaby }
60038646013SJiri Slaby 
60138646013SJiri Slaby void removed_exe_file_vma(struct mm_struct *mm)
60238646013SJiri Slaby {
60338646013SJiri Slaby 	mm->num_exe_file_vmas--;
60438646013SJiri Slaby 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file) {
60538646013SJiri Slaby 		fput(mm->exe_file);
60638646013SJiri Slaby 		mm->exe_file = NULL;
60738646013SJiri Slaby 	}
60838646013SJiri Slaby 
60938646013SJiri Slaby }
61038646013SJiri Slaby 
61138646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
61238646013SJiri Slaby {
61338646013SJiri Slaby 	if (new_exe_file)
61438646013SJiri Slaby 		get_file(new_exe_file);
61538646013SJiri Slaby 	if (mm->exe_file)
61638646013SJiri Slaby 		fput(mm->exe_file);
61738646013SJiri Slaby 	mm->exe_file = new_exe_file;
61838646013SJiri Slaby 	mm->num_exe_file_vmas = 0;
61938646013SJiri Slaby }
62038646013SJiri Slaby 
62138646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm)
62238646013SJiri Slaby {
62338646013SJiri Slaby 	struct file *exe_file;
62438646013SJiri Slaby 
62538646013SJiri Slaby 	/* We need mmap_sem to protect against races with removal of
62638646013SJiri Slaby 	 * VM_EXECUTABLE vmas */
62738646013SJiri Slaby 	down_read(&mm->mmap_sem);
62838646013SJiri Slaby 	exe_file = mm->exe_file;
62938646013SJiri Slaby 	if (exe_file)
63038646013SJiri Slaby 		get_file(exe_file);
63138646013SJiri Slaby 	up_read(&mm->mmap_sem);
63238646013SJiri Slaby 	return exe_file;
63338646013SJiri Slaby }
63438646013SJiri Slaby 
63538646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
63638646013SJiri Slaby {
63738646013SJiri Slaby 	/* It's safe to write the exe_file pointer without exe_file_lock because
63838646013SJiri Slaby 	 * this is called during fork when the task is not yet in /proc */
63938646013SJiri Slaby 	newmm->exe_file = get_mm_exe_file(oldmm);
64038646013SJiri Slaby }
64138646013SJiri Slaby 
6421da177e4SLinus Torvalds /**
6431da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
6441da177e4SLinus Torvalds  *
645246bb0b1SOleg Nesterov  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
6461da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
6471da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
6481da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
6491da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
6501da177e4SLinus Torvalds  */
6511da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	struct mm_struct *mm;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	task_lock(task);
6561da177e4SLinus Torvalds 	mm = task->mm;
6571da177e4SLinus Torvalds 	if (mm) {
658246bb0b1SOleg Nesterov 		if (task->flags & PF_KTHREAD)
6591da177e4SLinus Torvalds 			mm = NULL;
6601da177e4SLinus Torvalds 		else
6611da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 	task_unlock(task);
6641da177e4SLinus Torvalds 	return mm;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
6671da177e4SLinus Torvalds 
6688cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
6698cdb878dSChristopher Yeoh {
6708cdb878dSChristopher Yeoh 	struct mm_struct *mm;
6718cdb878dSChristopher Yeoh 	int err;
6728cdb878dSChristopher Yeoh 
6738cdb878dSChristopher Yeoh 	err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
6748cdb878dSChristopher Yeoh 	if (err)
6758cdb878dSChristopher Yeoh 		return ERR_PTR(err);
6768cdb878dSChristopher Yeoh 
6778cdb878dSChristopher Yeoh 	mm = get_task_mm(task);
6788cdb878dSChristopher Yeoh 	if (mm && mm != current->mm &&
6798cdb878dSChristopher Yeoh 			!ptrace_may_access(task, mode)) {
6808cdb878dSChristopher Yeoh 		mmput(mm);
6818cdb878dSChristopher Yeoh 		mm = ERR_PTR(-EACCES);
6828cdb878dSChristopher Yeoh 	}
6838cdb878dSChristopher Yeoh 	mutex_unlock(&task->signal->cred_guard_mutex);
6848cdb878dSChristopher Yeoh 
6858cdb878dSChristopher Yeoh 	return mm;
6868cdb878dSChristopher Yeoh }
6878cdb878dSChristopher Yeoh 
68857b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk)
689c415c3b4SOleg Nesterov {
690d68b46feSOleg Nesterov 	struct completion *vfork;
691c415c3b4SOleg Nesterov 
692d68b46feSOleg Nesterov 	task_lock(tsk);
693d68b46feSOleg Nesterov 	vfork = tsk->vfork_done;
694d68b46feSOleg Nesterov 	if (likely(vfork)) {
695c415c3b4SOleg Nesterov 		tsk->vfork_done = NULL;
696d68b46feSOleg Nesterov 		complete(vfork);
697d68b46feSOleg Nesterov 	}
698d68b46feSOleg Nesterov 	task_unlock(tsk);
699d68b46feSOleg Nesterov }
700d68b46feSOleg Nesterov 
701d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child,
702d68b46feSOleg Nesterov 				struct completion *vfork)
703d68b46feSOleg Nesterov {
704d68b46feSOleg Nesterov 	int killed;
705d68b46feSOleg Nesterov 
706d68b46feSOleg Nesterov 	freezer_do_not_count();
707d68b46feSOleg Nesterov 	killed = wait_for_completion_killable(vfork);
708d68b46feSOleg Nesterov 	freezer_count();
709d68b46feSOleg Nesterov 
710d68b46feSOleg Nesterov 	if (killed) {
711d68b46feSOleg Nesterov 		task_lock(child);
712d68b46feSOleg Nesterov 		child->vfork_done = NULL;
713d68b46feSOleg Nesterov 		task_unlock(child);
714d68b46feSOleg Nesterov 	}
715d68b46feSOleg Nesterov 
716d68b46feSOleg Nesterov 	put_task_struct(child);
717d68b46feSOleg Nesterov 	return killed;
718c415c3b4SOleg Nesterov }
719c415c3b4SOleg Nesterov 
7201da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
7211da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
7221da177e4SLinus Torvalds  * error success whatever.
7231da177e4SLinus Torvalds  *
7241da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
7251da177e4SLinus Torvalds  * from the current process.
7261da177e4SLinus Torvalds  *
7271da177e4SLinus Torvalds  * This difference is important for error handling, when we
7281da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
7291da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
7301da177e4SLinus Torvalds  * restoring the old one. . .
7311da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
7321da177e4SLinus Torvalds  */
7331da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
7341da177e4SLinus Torvalds {
7358141c7f3SLinus Torvalds 	/* Get rid of any futexes when releasing the mm */
7368141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX
737fc6b177dSPeter Zijlstra 	if (unlikely(tsk->robust_list)) {
7388141c7f3SLinus Torvalds 		exit_robust_list(tsk);
739fc6b177dSPeter Zijlstra 		tsk->robust_list = NULL;
740fc6b177dSPeter Zijlstra 	}
7418141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT
742fc6b177dSPeter Zijlstra 	if (unlikely(tsk->compat_robust_list)) {
7438141c7f3SLinus Torvalds 		compat_exit_robust_list(tsk);
744fc6b177dSPeter Zijlstra 		tsk->compat_robust_list = NULL;
745fc6b177dSPeter Zijlstra 	}
7468141c7f3SLinus Torvalds #endif
747322a2c10SThomas Gleixner 	if (unlikely(!list_empty(&tsk->pi_state_list)))
748322a2c10SThomas Gleixner 		exit_pi_state_list(tsk);
7498141c7f3SLinus Torvalds #endif
7508141c7f3SLinus Torvalds 
7511da177e4SLinus Torvalds 	/* Get rid of any cached register state */
7521da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
7531da177e4SLinus Torvalds 
754c415c3b4SOleg Nesterov 	if (tsk->vfork_done)
755c415c3b4SOleg Nesterov 		complete_vfork_done(tsk);
756fec1d011SRoland McGrath 
757fec1d011SRoland McGrath 	/*
758fec1d011SRoland McGrath 	 * If we're exiting normally, clear a user-space tid field if
759fec1d011SRoland McGrath 	 * requested.  We leave this alone when dying by signal, to leave
760fec1d011SRoland McGrath 	 * the value intact in a core dump, and to save the unnecessary
761d68b46feSOleg Nesterov 	 * trouble, say, a killed vfork parent shouldn't touch this mm.
762d68b46feSOleg Nesterov 	 * Userland only wants this done for a sys_exit.
763fec1d011SRoland McGrath 	 */
7649c8a8228SEric Dumazet 	if (tsk->clear_child_tid) {
7659c8a8228SEric Dumazet 		if (!(tsk->flags & PF_SIGNALED) &&
7669c8a8228SEric Dumazet 		    atomic_read(&mm->mm_users) > 1) {
7671da177e4SLinus Torvalds 			/*
7681da177e4SLinus Torvalds 			 * We don't check the error code - if userspace has
7691da177e4SLinus Torvalds 			 * not set up a proper pointer then tough luck.
7701da177e4SLinus Torvalds 			 */
7719c8a8228SEric Dumazet 			put_user(0, tsk->clear_child_tid);
7729c8a8228SEric Dumazet 			sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
7739c8a8228SEric Dumazet 					1, NULL, NULL, 0);
7749c8a8228SEric Dumazet 		}
7759c8a8228SEric Dumazet 		tsk->clear_child_tid = NULL;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
779a0a7ec30SJANAK DESAI /*
780a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
781a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
782a0a7ec30SJANAK DESAI  */
783402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk)
784a0a7ec30SJANAK DESAI {
785a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
786a0a7ec30SJANAK DESAI 	int err;
787a0a7ec30SJANAK DESAI 
788a0a7ec30SJANAK DESAI 	if (!oldmm)
789a0a7ec30SJANAK DESAI 		return NULL;
790a0a7ec30SJANAK DESAI 
791a0a7ec30SJANAK DESAI 	mm = allocate_mm();
792a0a7ec30SJANAK DESAI 	if (!mm)
793a0a7ec30SJANAK DESAI 		goto fail_nomem;
794a0a7ec30SJANAK DESAI 
795a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
7966345d24dSLinus Torvalds 	mm_init_cpumask(mm);
797a0a7ec30SJANAK DESAI 
7987602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
7997602bdf2SAshwin Chaugule 	mm->token_priority = 0;
8007602bdf2SAshwin Chaugule 	mm->last_interval = 0;
8017602bdf2SAshwin Chaugule 
802e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE
803e7a00c45SAndrea Arcangeli 	mm->pmd_huge_pte = NULL;
804e7a00c45SAndrea Arcangeli #endif
805e7a00c45SAndrea Arcangeli 
80678fb7466SPavel Emelianov 	if (!mm_init(mm, tsk))
807a0a7ec30SJANAK DESAI 		goto fail_nomem;
808a0a7ec30SJANAK DESAI 
809a0a7ec30SJANAK DESAI 	if (init_new_context(tsk, mm))
810a0a7ec30SJANAK DESAI 		goto fail_nocontext;
811a0a7ec30SJANAK DESAI 
812925d1c40SMatt Helsley 	dup_mm_exe_file(oldmm, mm);
813925d1c40SMatt Helsley 
814a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
815a0a7ec30SJANAK DESAI 	if (err)
816a0a7ec30SJANAK DESAI 		goto free_pt;
817a0a7ec30SJANAK DESAI 
818a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
819a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
820a0a7ec30SJANAK DESAI 
821801460d0SHiroshi Shimamoto 	if (mm->binfmt && !try_module_get(mm->binfmt->module))
822801460d0SHiroshi Shimamoto 		goto free_pt;
823801460d0SHiroshi Shimamoto 
824a0a7ec30SJANAK DESAI 	return mm;
825a0a7ec30SJANAK DESAI 
826a0a7ec30SJANAK DESAI free_pt:
827801460d0SHiroshi Shimamoto 	/* don't put binfmt in mmput, we haven't got module yet */
828801460d0SHiroshi Shimamoto 	mm->binfmt = NULL;
829a0a7ec30SJANAK DESAI 	mmput(mm);
830a0a7ec30SJANAK DESAI 
831a0a7ec30SJANAK DESAI fail_nomem:
832a0a7ec30SJANAK DESAI 	return NULL;
833a0a7ec30SJANAK DESAI 
834a0a7ec30SJANAK DESAI fail_nocontext:
835a0a7ec30SJANAK DESAI 	/*
836a0a7ec30SJANAK DESAI 	 * If init_new_context() failed, we cannot use mmput() to free the mm
837a0a7ec30SJANAK DESAI 	 * because it calls destroy_context()
838a0a7ec30SJANAK DESAI 	 */
839a0a7ec30SJANAK DESAI 	mm_free_pgd(mm);
840a0a7ec30SJANAK DESAI 	free_mm(mm);
841a0a7ec30SJANAK DESAI 	return NULL;
842a0a7ec30SJANAK DESAI }
843a0a7ec30SJANAK DESAI 
8441da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	struct mm_struct *mm, *oldmm;
8471da177e4SLinus Torvalds 	int retval;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
8501da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
85117406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
85217406b82SMandeep Singh Baines 	tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
85317406b82SMandeep Singh Baines #endif
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	tsk->mm = NULL;
8561da177e4SLinus Torvalds 	tsk->active_mm = NULL;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	/*
8591da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
8601da177e4SLinus Torvalds 	 *
8611da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
8621da177e4SLinus Torvalds 	 */
8631da177e4SLinus Torvalds 	oldmm = current->mm;
8641da177e4SLinus Torvalds 	if (!oldmm)
8651da177e4SLinus Torvalds 		return 0;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
8681da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
8691da177e4SLinus Torvalds 		mm = oldmm;
8701da177e4SLinus Torvalds 		goto good_mm;
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	retval = -ENOMEM;
874a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
8751da177e4SLinus Torvalds 	if (!mm)
8761da177e4SLinus Torvalds 		goto fail_nomem;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds good_mm:
8797602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
8807602bdf2SAshwin Chaugule 	mm->token_priority = 0;
8817602bdf2SAshwin Chaugule 	mm->last_interval = 0;
8827602bdf2SAshwin Chaugule 
8831da177e4SLinus Torvalds 	tsk->mm = mm;
8841da177e4SLinus Torvalds 	tsk->active_mm = mm;
8851da177e4SLinus Torvalds 	return 0;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds fail_nomem:
8881da177e4SLinus Torvalds 	return retval;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
891a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
8921da177e4SLinus Torvalds {
893498052bbSAl Viro 	struct fs_struct *fs = current->fs;
8941da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
895498052bbSAl Viro 		/* tsk->fs is already what we want */
8962a4419b5SNick Piggin 		spin_lock(&fs->lock);
897498052bbSAl Viro 		if (fs->in_exec) {
8982a4419b5SNick Piggin 			spin_unlock(&fs->lock);
899498052bbSAl Viro 			return -EAGAIN;
900498052bbSAl Viro 		}
901498052bbSAl Viro 		fs->users++;
9022a4419b5SNick Piggin 		spin_unlock(&fs->lock);
9031da177e4SLinus Torvalds 		return 0;
9041da177e4SLinus Torvalds 	}
905498052bbSAl Viro 	tsk->fs = copy_fs_struct(fs);
9061da177e4SLinus Torvalds 	if (!tsk->fs)
9071da177e4SLinus Torvalds 		return -ENOMEM;
9081da177e4SLinus Torvalds 	return 0;
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
911a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
912a016f338SJANAK DESAI {
913a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
914a016f338SJANAK DESAI 	int error = 0;
915a016f338SJANAK DESAI 
916a016f338SJANAK DESAI 	/*
917a016f338SJANAK DESAI 	 * A background process may not have any files ...
918a016f338SJANAK DESAI 	 */
919a016f338SJANAK DESAI 	oldf = current->files;
920a016f338SJANAK DESAI 	if (!oldf)
921a016f338SJANAK DESAI 		goto out;
922a016f338SJANAK DESAI 
923a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
924a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
925a016f338SJANAK DESAI 		goto out;
926a016f338SJANAK DESAI 	}
927a016f338SJANAK DESAI 
928a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
929a016f338SJANAK DESAI 	if (!newf)
930a016f338SJANAK DESAI 		goto out;
931a016f338SJANAK DESAI 
932a016f338SJANAK DESAI 	tsk->files = newf;
933a016f338SJANAK DESAI 	error = 0;
934a016f338SJANAK DESAI out:
935a016f338SJANAK DESAI 	return error;
936a016f338SJANAK DESAI }
937a016f338SJANAK DESAI 
938fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
939fd0928dfSJens Axboe {
940fd0928dfSJens Axboe #ifdef CONFIG_BLOCK
941fd0928dfSJens Axboe 	struct io_context *ioc = current->io_context;
9426e736be7STejun Heo 	struct io_context *new_ioc;
943fd0928dfSJens Axboe 
944fd0928dfSJens Axboe 	if (!ioc)
945fd0928dfSJens Axboe 		return 0;
946fadad878SJens Axboe 	/*
947fadad878SJens Axboe 	 * Share io context with parent, if CLONE_IO is set
948fadad878SJens Axboe 	 */
949fadad878SJens Axboe 	if (clone_flags & CLONE_IO) {
950fadad878SJens Axboe 		tsk->io_context = ioc_task_link(ioc);
951fadad878SJens Axboe 		if (unlikely(!tsk->io_context))
952fadad878SJens Axboe 			return -ENOMEM;
953fadad878SJens Axboe 	} else if (ioprio_valid(ioc->ioprio)) {
9546e736be7STejun Heo 		new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
9556e736be7STejun Heo 		if (unlikely(!new_ioc))
956fd0928dfSJens Axboe 			return -ENOMEM;
957fd0928dfSJens Axboe 
9586e736be7STejun Heo 		new_ioc->ioprio = ioc->ioprio;
95911a3122fSTejun Heo 		put_io_context(new_ioc);
960fd0928dfSJens Axboe 	}
961fd0928dfSJens Axboe #endif
962fd0928dfSJens Axboe 	return 0;
963fd0928dfSJens Axboe }
964fd0928dfSJens Axboe 
965a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
9661da177e4SLinus Torvalds {
9671da177e4SLinus Torvalds 	struct sighand_struct *sig;
9681da177e4SLinus Torvalds 
96960348802SZhaolei 	if (clone_flags & CLONE_SIGHAND) {
9701da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
9711da177e4SLinus Torvalds 		return 0;
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
974e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
9751da177e4SLinus Torvalds 	if (!sig)
9761da177e4SLinus Torvalds 		return -ENOMEM;
9771da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
9781da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
9791da177e4SLinus Torvalds 	return 0;
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
982a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
983c81addc9SOleg Nesterov {
984d80e731eSOleg Nesterov 	if (atomic_dec_and_test(&sighand->count)) {
985d80e731eSOleg Nesterov 		signalfd_cleanup(sighand);
986c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
987c81addc9SOleg Nesterov 	}
988d80e731eSOleg Nesterov }
989c81addc9SOleg Nesterov 
990f06febc9SFrank Mayhar 
991f06febc9SFrank Mayhar /*
992f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a thread group.
993f06febc9SFrank Mayhar  */
994f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig)
995f06febc9SFrank Mayhar {
99678d7d407SJiri Slaby 	unsigned long cpu_limit;
99778d7d407SJiri Slaby 
998f06febc9SFrank Mayhar 	/* Thread group counters. */
999f06febc9SFrank Mayhar 	thread_group_cputime_init(sig);
1000f06febc9SFrank Mayhar 
100178d7d407SJiri Slaby 	cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
100278d7d407SJiri Slaby 	if (cpu_limit != RLIM_INFINITY) {
100378d7d407SJiri Slaby 		sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
10046279a751SOleg Nesterov 		sig->cputimer.running = 1;
10056279a751SOleg Nesterov 	}
10066279a751SOleg Nesterov 
1007f06febc9SFrank Mayhar 	/* The timer lists. */
1008f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
1009f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
1010f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
1011f06febc9SFrank Mayhar }
1012f06febc9SFrank Mayhar 
1013a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	struct signal_struct *sig;
10161da177e4SLinus Torvalds 
10174ab6c083SOleg Nesterov 	if (clone_flags & CLONE_THREAD)
1018490dea45SPeter Zijlstra 		return 0;
10196279a751SOleg Nesterov 
1020a56704efSVeaceslav Falico 	sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
10211da177e4SLinus Torvalds 	tsk->signal = sig;
10221da177e4SLinus Torvalds 	if (!sig)
10231da177e4SLinus Torvalds 		return -ENOMEM;
10241da177e4SLinus Torvalds 
1025b3ac022cSOleg Nesterov 	sig->nr_threads = 1;
10261da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
1027b3ac022cSOleg Nesterov 	atomic_set(&sig->sigcnt, 1);
10281da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
1029b3bfa0cbSSukadev Bhattiprolu 	if (clone_flags & CLONE_NEWPID)
1030b3bfa0cbSSukadev Bhattiprolu 		sig->flags |= SIGNAL_UNKILLABLE;
1031db51aeccSOleg Nesterov 	sig->curr_target = tsk;
10321da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
10331da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
10341da177e4SLinus Torvalds 
1035c9cb2e3dSThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
10361da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	task_lock(current->group_leader);
10391da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
10401da177e4SLinus Torvalds 	task_unlock(current->group_leader);
10411da177e4SLinus Torvalds 
10426279a751SOleg Nesterov 	posix_cpu_timers_init_group(sig);
10436279a751SOleg Nesterov 
1044522ed776SMiloslav Trmac 	tty_audit_fork(sig);
10455091faa4SMike Galbraith 	sched_autogroup_fork(sig);
1046522ed776SMiloslav Trmac 
10474714d1d3SBen Blum #ifdef CONFIG_CGROUPS
1048257058aeSTejun Heo 	init_rwsem(&sig->group_rwsem);
10494714d1d3SBen Blum #endif
10504714d1d3SBen Blum 
105128b83c51SKOSAKI Motohiro 	sig->oom_adj = current->signal->oom_adj;
1052a63d83f4SDavid Rientjes 	sig->oom_score_adj = current->signal->oom_score_adj;
1053dabb16f6SMandeep Singh Baines 	sig->oom_score_adj_min = current->signal->oom_score_adj_min;
105428b83c51SKOSAKI Motohiro 
1055ebec18a6SLennart Poettering 	sig->has_child_subreaper = current->signal->has_child_subreaper ||
1056ebec18a6SLennart Poettering 				   current->signal->is_child_subreaper;
1057ebec18a6SLennart Poettering 
10589b1bf12dSKOSAKI Motohiro 	mutex_init(&sig->cred_guard_mutex);
10599b1bf12dSKOSAKI Motohiro 
10601da177e4SLinus Torvalds 	return 0;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
1063a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p)
10641da177e4SLinus Torvalds {
10651da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
10661da177e4SLinus Torvalds 
106721aa9af0STejun Heo 	new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
10681da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
10691da177e4SLinus Torvalds 	p->flags = new_flags;
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
107217da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
10751da177e4SLinus Torvalds 
1076b488893aSPavel Emelyanov 	return task_pid_vnr(current);
10771da177e4SLinus Torvalds }
10781da177e4SLinus Torvalds 
1079a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p)
108023f78d4aSIngo Molnar {
10811d615482SThomas Gleixner 	raw_spin_lock_init(&p->pi_lock);
1082e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES
1083732375c6SDima Zavin 	plist_head_init(&p->pi_waiters);
108423f78d4aSIngo Molnar 	p->pi_blocked_on = NULL;
108523f78d4aSIngo Molnar #endif
108623f78d4aSIngo Molnar }
108723f78d4aSIngo Molnar 
1088cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER
1089cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
1090cf475ad2SBalbir Singh {
1091cf475ad2SBalbir Singh 	mm->owner = p;
1092cf475ad2SBalbir Singh }
1093cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */
1094cf475ad2SBalbir Singh 
10951da177e4SLinus Torvalds /*
1096f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a single task.
1097f06febc9SFrank Mayhar  */
1098f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk)
1099f06febc9SFrank Mayhar {
110064861634SMartin Schwidefsky 	tsk->cputime_expires.prof_exp = 0;
110164861634SMartin Schwidefsky 	tsk->cputime_expires.virt_exp = 0;
1102f06febc9SFrank Mayhar 	tsk->cputime_expires.sched_exp = 0;
1103f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[0]);
1104f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[1]);
1105f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[2]);
1106f06febc9SFrank Mayhar }
1107f06febc9SFrank Mayhar 
1108f06febc9SFrank Mayhar /*
11091da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
11101da177e4SLinus Torvalds  * but does not actually start it yet.
11111da177e4SLinus Torvalds  *
11121da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
11131da177e4SLinus Torvalds  * parts of the process environment (as per the clone
11141da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
11151da177e4SLinus Torvalds  */
111636c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags,
11171da177e4SLinus Torvalds 					unsigned long stack_start,
11181da177e4SLinus Torvalds 					struct pt_regs *regs,
11191da177e4SLinus Torvalds 					unsigned long stack_size,
11201da177e4SLinus Torvalds 					int __user *child_tidptr,
112109a05394SRoland McGrath 					struct pid *pid,
112209a05394SRoland McGrath 					int trace)
11231da177e4SLinus Torvalds {
11241da177e4SLinus Torvalds 	int retval;
1125a24efe62SMariusz Kozlowski 	struct task_struct *p;
1126b4f48b63SPaul Menage 	int cgroup_callbacks_done = 0;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
11291da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	/*
11321da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
11331da177e4SLinus Torvalds 	 * can only be started up within the thread group.
11341da177e4SLinus Torvalds 	 */
11351da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
11361da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	/*
11391da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
11401da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
11411da177e4SLinus Torvalds 	 * for various simplifications in other code.
11421da177e4SLinus Torvalds 	 */
11431da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
11441da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
11451da177e4SLinus Torvalds 
1146123be07bSSukadev Bhattiprolu 	/*
1147123be07bSSukadev Bhattiprolu 	 * Siblings of global init remain as zombies on exit since they are
1148123be07bSSukadev Bhattiprolu 	 * not reaped by their parent (swapper). To solve this and to avoid
1149123be07bSSukadev Bhattiprolu 	 * multi-rooted process trees, prevent global and container-inits
1150123be07bSSukadev Bhattiprolu 	 * from creating siblings.
1151123be07bSSukadev Bhattiprolu 	 */
1152123be07bSSukadev Bhattiprolu 	if ((clone_flags & CLONE_PARENT) &&
1153123be07bSSukadev Bhattiprolu 				current->signal->flags & SIGNAL_UNKILLABLE)
1154123be07bSSukadev Bhattiprolu 		return ERR_PTR(-EINVAL);
1155123be07bSSukadev Bhattiprolu 
11561da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
11571da177e4SLinus Torvalds 	if (retval)
11581da177e4SLinus Torvalds 		goto fork_out;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	retval = -ENOMEM;
11611da177e4SLinus Torvalds 	p = dup_task_struct(current);
11621da177e4SLinus Torvalds 	if (!p)
11631da177e4SLinus Torvalds 		goto fork_out;
11641da177e4SLinus Torvalds 
1165f7e8b616SSteven Rostedt 	ftrace_graph_init_task(p);
1166f7e8b616SSteven Rostedt 
1167bea493a0SPeter Zijlstra 	rt_mutex_init_task(p);
1168bea493a0SPeter Zijlstra 
1169d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING
1170de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1171de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1172de30a2b3SIngo Molnar #endif
11731da177e4SLinus Torvalds 	retval = -EAGAIN;
11743b11a1deSDavid Howells 	if (atomic_read(&p->real_cred->user->processes) >=
117578d7d407SJiri Slaby 			task_rlimit(p, RLIMIT_NPROC)) {
11761da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
117718b6e041SSerge Hallyn 		    p->real_cred->user != INIT_USER)
11781da177e4SLinus Torvalds 			goto bad_fork_free;
11791da177e4SLinus Torvalds 	}
118072fa5997SVasiliy Kulikov 	current->flags &= ~PF_NPROC_EXCEEDED;
11811da177e4SLinus Torvalds 
1182f1752eecSDavid Howells 	retval = copy_creds(p, clone_flags);
1183f1752eecSDavid Howells 	if (retval < 0)
1184f1752eecSDavid Howells 		goto bad_fork_free;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	/*
11871da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
11881da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
11891da177e4SLinus Torvalds 	 * to stop root fork bombs.
11901da177e4SLinus Torvalds 	 */
119104ec93feSLi Zefan 	retval = -EAGAIN;
11921da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
11931da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
11941da177e4SLinus Torvalds 
1195a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
11961da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	p->did_exec = 0;
1199ca74e92bSShailabh Nagar 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
12001da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
12011da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
12021da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
1203f41d911fSPaul E. McKenney 	rcu_copy_process(p);
12041da177e4SLinus Torvalds 	p->vfork_done = NULL;
12051da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	init_sigpending(&p->pending);
12081da177e4SLinus Torvalds 
120964861634SMartin Schwidefsky 	p->utime = p->stime = p->gtime = 0;
121064861634SMartin Schwidefsky 	p->utimescaled = p->stimescaled = 0;
1211d99ca3b9SHidetoshi Seto #ifndef CONFIG_VIRT_CPU_ACCOUNTING
121264861634SMartin Schwidefsky 	p->prev_utime = p->prev_stime = 0;
1213d99ca3b9SHidetoshi Seto #endif
1214a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
1215a3a2e76cSKAMEZAWA Hiroyuki 	memset(&p->rss_stat, 0, sizeof(p->rss_stat));
1216a3a2e76cSKAMEZAWA Hiroyuki #endif
1217172ba844SBalbir Singh 
12186976675dSArjan van de Ven 	p->default_timer_slack_ns = current->timer_slack_ns;
12196976675dSArjan van de Ven 
12205995477aSAndrea Righi 	task_io_accounting_init(&p->ioac);
12211da177e4SLinus Torvalds 	acct_clear_integrals(p);
12221da177e4SLinus Torvalds 
1223f06febc9SFrank Mayhar 	posix_cpu_timers_init(p);
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
1226924b42d5STomas Janousek 	p->real_start_time = p->start_time;
1227924b42d5STomas Janousek 	monotonic_to_bootbased(&p->real_start_time);
12281da177e4SLinus Torvalds 	p->io_context = NULL;
12291da177e4SLinus Torvalds 	p->audit_context = NULL;
12304714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
1231257058aeSTejun Heo 		threadgroup_change_begin(current);
1232b4f48b63SPaul Menage 	cgroup_fork(p);
12331da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1234846a16bfSLee Schermerhorn 	p->mempolicy = mpol_dup(p->mempolicy);
12351da177e4SLinus Torvalds 	if (IS_ERR(p->mempolicy)) {
12361da177e4SLinus Torvalds 		retval = PTR_ERR(p->mempolicy);
12371da177e4SLinus Torvalds 		p->mempolicy = NULL;
1238b4f48b63SPaul Menage 		goto bad_fork_cleanup_cgroup;
12391da177e4SLinus Torvalds 	}
1240c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(p);
12411da177e4SLinus Torvalds #endif
1242778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS
1243778d3b0fSMichal Hocko 	p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
1244778d3b0fSMichal Hocko 	p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
1245cc9a6c87SMel Gorman 	seqcount_init(&p->mems_allowed_seq);
1246778d3b0fSMichal Hocko #endif
1247de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1248de30a2b3SIngo Molnar 	p->irq_events = 0;
1249b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1250b36e4758SRussell King 	p->hardirqs_enabled = 1;
1251b36e4758SRussell King #else
1252de30a2b3SIngo Molnar 	p->hardirqs_enabled = 0;
1253b36e4758SRussell King #endif
1254de30a2b3SIngo Molnar 	p->hardirq_enable_ip = 0;
1255de30a2b3SIngo Molnar 	p->hardirq_enable_event = 0;
1256de30a2b3SIngo Molnar 	p->hardirq_disable_ip = _THIS_IP_;
1257de30a2b3SIngo Molnar 	p->hardirq_disable_event = 0;
1258de30a2b3SIngo Molnar 	p->softirqs_enabled = 1;
1259de30a2b3SIngo Molnar 	p->softirq_enable_ip = _THIS_IP_;
1260de30a2b3SIngo Molnar 	p->softirq_enable_event = 0;
1261de30a2b3SIngo Molnar 	p->softirq_disable_ip = 0;
1262de30a2b3SIngo Molnar 	p->softirq_disable_event = 0;
1263de30a2b3SIngo Molnar 	p->hardirq_context = 0;
1264de30a2b3SIngo Molnar 	p->softirq_context = 0;
1265de30a2b3SIngo Molnar #endif
1266fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1267fbb9ce95SIngo Molnar 	p->lockdep_depth = 0; /* no locks held yet */
1268fbb9ce95SIngo Molnar 	p->curr_chain_key = 0;
1269fbb9ce95SIngo Molnar 	p->lockdep_recursion = 0;
1270fbb9ce95SIngo Molnar #endif
12711da177e4SLinus Torvalds 
1272408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1273408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1274408894eeSIngo Molnar #endif
1275569b846dSKAMEZAWA Hiroyuki #ifdef CONFIG_CGROUP_MEM_RES_CTLR
1276569b846dSKAMEZAWA Hiroyuki 	p->memcg_batch.do_batch = 0;
1277569b846dSKAMEZAWA Hiroyuki 	p->memcg_batch.memcg = NULL;
1278569b846dSKAMEZAWA Hiroyuki #endif
12790f481406SMarkus Metzger 
12803c90e6e9SSrivatsa Vaddagiri 	/* Perform scheduler related setup. Assign this task to a CPU. */
12813e51e3edSSamir Bellabes 	sched_fork(p);
12826ab423e0SPeter Zijlstra 
1283cdd6c482SIngo Molnar 	retval = perf_event_init_task(p);
12846ab423e0SPeter Zijlstra 	if (retval)
12856ab423e0SPeter Zijlstra 		goto bad_fork_cleanup_policy;
1286fb0a685cSDaniel Rebelo de Oliveira 	retval = audit_alloc(p);
1287fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
1288f1752eecSDavid Howells 		goto bad_fork_cleanup_policy;
12891da177e4SLinus Torvalds 	/* copy all the process information */
1290fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_semundo(clone_flags, p);
1291fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12921da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
1293fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_files(clone_flags, p);
1294fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12951da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
1296fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_fs(clone_flags, p);
1297fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
12981da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
1299fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_sighand(clone_flags, p);
1300fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
13011da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
1302fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_signal(clone_flags, p);
1303fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
13041da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
1305fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_mm(clone_flags, p);
1306fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
13071da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
1308fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_namespaces(clone_flags, p);
1309fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
1310d84f4f99SDavid Howells 		goto bad_fork_cleanup_mm;
1311fb0a685cSDaniel Rebelo de Oliveira 	retval = copy_io(clone_flags, p);
1312fb0a685cSDaniel Rebelo de Oliveira 	if (retval)
1313fd0928dfSJens Axboe 		goto bad_fork_cleanup_namespaces;
13146f2c55b8SAlexey Dobriyan 	retval = copy_thread(clone_flags, stack_start, stack_size, p, regs);
13151da177e4SLinus Torvalds 	if (retval)
1316fd0928dfSJens Axboe 		goto bad_fork_cleanup_io;
13171da177e4SLinus Torvalds 
1318425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid) {
1319425fb2b4SPavel Emelyanov 		retval = -ENOMEM;
132061bce0f1SEric W. Biederman 		pid = alloc_pid(p->nsproxy->pid_ns);
1321425fb2b4SPavel Emelyanov 		if (!pid)
1322fd0928dfSJens Axboe 			goto bad_fork_cleanup_io;
1323425fb2b4SPavel Emelyanov 	}
1324425fb2b4SPavel Emelyanov 
1325425fb2b4SPavel Emelyanov 	p->pid = pid_nr(pid);
1326425fb2b4SPavel Emelyanov 	p->tgid = p->pid;
1327425fb2b4SPavel Emelyanov 	if (clone_flags & CLONE_THREAD)
1328425fb2b4SPavel Emelyanov 		p->tgid = current->tgid;
1329425fb2b4SPavel Emelyanov 
13301da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
13311da177e4SLinus Torvalds 	/*
13321da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
13331da177e4SLinus Torvalds 	 */
13341da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
133573c10101SJens Axboe #ifdef CONFIG_BLOCK
133673c10101SJens Axboe 	p->plug = NULL;
133773c10101SJens Axboe #endif
133842b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
13398f17d3a5SIngo Molnar 	p->robust_list = NULL;
13408f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
13418f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
13428f17d3a5SIngo Molnar #endif
1343c87e2837SIngo Molnar 	INIT_LIST_HEAD(&p->pi_state_list);
1344c87e2837SIngo Molnar 	p->pi_state_cache = NULL;
134542b2dd0aSAlexey Dobriyan #endif
13461da177e4SLinus Torvalds 	/*
1347f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1348f9a3879aSGOTO Masanori 	 */
1349f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1350f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1351f9a3879aSGOTO Masanori 
1352f9a3879aSGOTO Masanori 	/*
13536580807dSOleg Nesterov 	 * Syscall tracing and stepping should be turned off in the
13546580807dSOleg Nesterov 	 * child regardless of CLONE_PTRACE.
13551da177e4SLinus Torvalds 	 */
13566580807dSOleg Nesterov 	user_disable_single_step(p);
13571da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1358ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1359ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1360ed75e8d5SLaurent Vivier #endif
13619745512cSArjan van de Ven 	clear_all_latency_tracing(p);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
13645f8aadd8SOleg Nesterov 	if (clone_flags & CLONE_THREAD)
13655f8aadd8SOleg Nesterov 		p->exit_signal = -1;
13665f8aadd8SOleg Nesterov 	else if (clone_flags & CLONE_PARENT)
13675f8aadd8SOleg Nesterov 		p->exit_signal = current->group_leader->exit_signal;
13685f8aadd8SOleg Nesterov 	else
13695f8aadd8SOleg Nesterov 		p->exit_signal = (clone_flags & CSIGNAL);
13705f8aadd8SOleg Nesterov 
13711da177e4SLinus Torvalds 	p->pdeath_signal = 0;
13721da177e4SLinus Torvalds 	p->exit_state = 0;
13731da177e4SLinus Torvalds 
13749d823e8fSWu Fengguang 	p->nr_dirtied = 0;
13759d823e8fSWu Fengguang 	p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
137683712358SWu Fengguang 	p->dirty_paused_when = 0;
13779d823e8fSWu Fengguang 
13781da177e4SLinus Torvalds 	/*
13791da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
13801da177e4SLinus Torvalds 	 * We dont wake it up yet.
13811da177e4SLinus Torvalds 	 */
13821da177e4SLinus Torvalds 	p->group_leader = p;
138347e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
13841da177e4SLinus Torvalds 
1385b4f48b63SPaul Menage 	/* Now that the task is set up, run cgroup callbacks if
1386b4f48b63SPaul Menage 	 * necessary. We need to run them before the task is visible
1387b4f48b63SPaul Menage 	 * on the tasklist. */
1388b4f48b63SPaul Menage 	cgroup_fork_callbacks(p);
1389b4f48b63SPaul Menage 	cgroup_callbacks_done = 1;
1390b4f48b63SPaul Menage 
13911da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
13921da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
13952d5516cbSOleg Nesterov 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
13961da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
13972d5516cbSOleg Nesterov 		p->parent_exec_id = current->parent_exec_id;
13982d5516cbSOleg Nesterov 	} else {
13991da177e4SLinus Torvalds 		p->real_parent = current;
14002d5516cbSOleg Nesterov 		p->parent_exec_id = current->self_exec_id;
14012d5516cbSOleg Nesterov 	}
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
14044a2c7a78SOleg Nesterov 
14054a2c7a78SOleg Nesterov 	/*
14064a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
14074a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
14084a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
14094a2c7a78SOleg Nesterov 	 * it's process group.
14104a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
14114a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
14124a2c7a78SOleg Nesterov 	*/
14134a2c7a78SOleg Nesterov 	recalc_sigpending();
14144a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
14154a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
14164a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
14174a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
1418f7e8b616SSteven Rostedt 		goto bad_fork_free_pid;
14194a2c7a78SOleg Nesterov 	}
14204a2c7a78SOleg Nesterov 
14213f17da69SOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
1422b3ac022cSOleg Nesterov 		current->signal->nr_threads++;
14234ab6c083SOleg Nesterov 		atomic_inc(&current->signal->live);
1424b3ac022cSOleg Nesterov 		atomic_inc(&current->signal->sigcnt);
14251da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
142647e65328SOleg Nesterov 		list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 
142973b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
14304b9d33e6STejun Heo 		ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
143345a68628SEric W. Biederman 			if (is_child_reaper(pid))
143430e49c26SPavel Emelyanov 				p->nsproxy->pid_ns->child_reaper = p;
14355cd17569SEric W. Biederman 
1436fea9d175SOleg Nesterov 			p->signal->leader_pid = pid;
14379c9f4dedSAlan Cox 			p->signal->tty = tty_kref_get(current->signal->tty);
14385cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
14395cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_SID, task_session(current));
14409cd80bbbSOleg Nesterov 			list_add_tail(&p->sibling, &p->real_parent->children);
14415e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
1442909ea964SChristoph Lameter 			__this_cpu_inc(process_counts);
14431da177e4SLinus Torvalds 		}
144485868995SSukadev Bhattiprolu 		attach_pid(p, PIDTYPE_PID, pid);
14451da177e4SLinus Torvalds 		nr_threads++;
144673b9ebfeSOleg Nesterov 	}
144773b9ebfeSOleg Nesterov 
14481da177e4SLinus Torvalds 	total_forks++;
14493f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
14501da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1451c13cf856SAndrew Morton 	proc_fork_connector(p);
1452817929ecSPaul Menage 	cgroup_post_fork(p);
14534714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
1454257058aeSTejun Heo 		threadgroup_change_end(current);
1455cdd6c482SIngo Molnar 	perf_event_fork(p);
145643d2b113SKAMEZAWA Hiroyuki 
145743d2b113SKAMEZAWA Hiroyuki 	trace_task_newtask(p, clone_flags);
145843d2b113SKAMEZAWA Hiroyuki 
14591da177e4SLinus Torvalds 	return p;
14601da177e4SLinus Torvalds 
1461425fb2b4SPavel Emelyanov bad_fork_free_pid:
1462425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid)
1463425fb2b4SPavel Emelyanov 		free_pid(pid);
1464fd0928dfSJens Axboe bad_fork_cleanup_io:
1465b69f2292SLouis Rilling 	if (p->io_context)
1466b69f2292SLouis Rilling 		exit_io_context(p);
1467ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces:
1468*5e2bf014SMike Galbraith 	if (unlikely(clone_flags & CLONE_NEWPID))
1469*5e2bf014SMike Galbraith 		pid_ns_release_proc(p->nsproxy->pid_ns);
1470444f378bSLinus Torvalds 	exit_task_namespaces(p);
14711da177e4SLinus Torvalds bad_fork_cleanup_mm:
1472c9f01245SDavid Rientjes 	if (p->mm)
14731da177e4SLinus Torvalds 		mmput(p->mm);
14741da177e4SLinus Torvalds bad_fork_cleanup_signal:
14754ab6c083SOleg Nesterov 	if (!(clone_flags & CLONE_THREAD))
14761c5354deSMike Galbraith 		free_signal_struct(p->signal);
14771da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1478a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
14791da177e4SLinus Torvalds bad_fork_cleanup_fs:
14801da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
14811da177e4SLinus Torvalds bad_fork_cleanup_files:
14821da177e4SLinus Torvalds 	exit_files(p); /* blocking */
14831da177e4SLinus Torvalds bad_fork_cleanup_semundo:
14841da177e4SLinus Torvalds 	exit_sem(p);
14851da177e4SLinus Torvalds bad_fork_cleanup_audit:
14861da177e4SLinus Torvalds 	audit_free(p);
14871da177e4SLinus Torvalds bad_fork_cleanup_policy:
1488cdd6c482SIngo Molnar 	perf_event_free_task(p);
14891da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1490f0be3d32SLee Schermerhorn 	mpol_put(p->mempolicy);
1491b4f48b63SPaul Menage bad_fork_cleanup_cgroup:
14921da177e4SLinus Torvalds #endif
14934714d1d3SBen Blum 	if (clone_flags & CLONE_THREAD)
1494257058aeSTejun Heo 		threadgroup_change_end(current);
1495b4f48b63SPaul Menage 	cgroup_exit(p, cgroup_callbacks_done);
149635df17c5SShailabh Nagar 	delayacct_tsk_free(p);
1497a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
14981da177e4SLinus Torvalds bad_fork_cleanup_count:
1499d84f4f99SDavid Howells 	atomic_dec(&p->cred->user->processes);
1500e0e81739SDavid Howells 	exit_creds(p);
15011da177e4SLinus Torvalds bad_fork_free:
15021da177e4SLinus Torvalds 	free_task(p);
1503fe7d37d1SOleg Nesterov fork_out:
1504fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
15051da177e4SLinus Torvalds }
15061da177e4SLinus Torvalds 
15076b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
15101da177e4SLinus Torvalds 	return regs;
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
1513f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links)
1514f106eee1SOleg Nesterov {
1515f106eee1SOleg Nesterov 	enum pid_type type;
1516f106eee1SOleg Nesterov 
1517f106eee1SOleg Nesterov 	for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
1518f106eee1SOleg Nesterov 		INIT_HLIST_NODE(&links[type].node); /* not really needed */
1519f106eee1SOleg Nesterov 		links[type].pid = &init_struct_pid;
1520f106eee1SOleg Nesterov 	}
1521f106eee1SOleg Nesterov }
1522f106eee1SOleg Nesterov 
15239abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu)
15241da177e4SLinus Torvalds {
152536c8b586SIngo Molnar 	struct task_struct *task;
15261da177e4SLinus Torvalds 	struct pt_regs regs;
15271da177e4SLinus Torvalds 
152830e49c26SPavel Emelyanov 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL,
152909a05394SRoland McGrath 			    &init_struct_pid, 0);
1530f106eee1SOleg Nesterov 	if (!IS_ERR(task)) {
1531f106eee1SOleg Nesterov 		init_idle_pids(task->pids);
15321da177e4SLinus Torvalds 		init_idle(task, cpu);
1533f106eee1SOleg Nesterov 	}
153473b9ebfeSOleg Nesterov 
15351da177e4SLinus Torvalds 	return task;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds /*
15391da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
15401da177e4SLinus Torvalds  *
15411da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
15421da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
15431da177e4SLinus Torvalds  */
15441da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
15451da177e4SLinus Torvalds 	      unsigned long stack_start,
15461da177e4SLinus Torvalds 	      struct pt_regs *regs,
15471da177e4SLinus Torvalds 	      unsigned long stack_size,
15481da177e4SLinus Torvalds 	      int __user *parent_tidptr,
15491da177e4SLinus Torvalds 	      int __user *child_tidptr)
15501da177e4SLinus Torvalds {
15511da177e4SLinus Torvalds 	struct task_struct *p;
15521da177e4SLinus Torvalds 	int trace = 0;
155392476d7fSEric W. Biederman 	long nr;
15541da177e4SLinus Torvalds 
1555bdff746aSAndrew Morton 	/*
155618b6e041SSerge Hallyn 	 * Do some preliminary argument and permissions checking before we
155718b6e041SSerge Hallyn 	 * actually start allocating stuff
155818b6e041SSerge Hallyn 	 */
155918b6e041SSerge Hallyn 	if (clone_flags & CLONE_NEWUSER) {
156018b6e041SSerge Hallyn 		if (clone_flags & CLONE_THREAD)
156118b6e041SSerge Hallyn 			return -EINVAL;
156218b6e041SSerge Hallyn 		/* hopefully this check will go away when userns support is
156318b6e041SSerge Hallyn 		 * complete
156418b6e041SSerge Hallyn 		 */
15657657d904SSerge E. Hallyn 		if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) ||
15667657d904SSerge E. Hallyn 				!capable(CAP_SETGID))
156718b6e041SSerge Hallyn 			return -EPERM;
156818b6e041SSerge Hallyn 	}
156918b6e041SSerge Hallyn 
157018b6e041SSerge Hallyn 	/*
15714b9d33e6STejun Heo 	 * Determine whether and which event to report to ptracer.  When
15724b9d33e6STejun Heo 	 * called from kernel_thread or CLONE_UNTRACED is explicitly
15734b9d33e6STejun Heo 	 * requested, no event is reported; otherwise, report if the event
15744b9d33e6STejun Heo 	 * for the type of forking is enabled.
157509a05394SRoland McGrath 	 */
15764b9d33e6STejun Heo 	if (likely(user_mode(regs)) && !(clone_flags & CLONE_UNTRACED)) {
15774b9d33e6STejun Heo 		if (clone_flags & CLONE_VFORK)
15784b9d33e6STejun Heo 			trace = PTRACE_EVENT_VFORK;
15794b9d33e6STejun Heo 		else if ((clone_flags & CSIGNAL) != SIGCHLD)
15804b9d33e6STejun Heo 			trace = PTRACE_EVENT_CLONE;
15814b9d33e6STejun Heo 		else
15824b9d33e6STejun Heo 			trace = PTRACE_EVENT_FORK;
15834b9d33e6STejun Heo 
15844b9d33e6STejun Heo 		if (likely(!ptrace_event_enabled(current, trace)))
15854b9d33e6STejun Heo 			trace = 0;
15864b9d33e6STejun Heo 	}
15871da177e4SLinus Torvalds 
1588a6f5e063SSukadev Bhattiprolu 	p = copy_process(clone_flags, stack_start, regs, stack_size,
158909a05394SRoland McGrath 			 child_tidptr, NULL, trace);
15901da177e4SLinus Torvalds 	/*
15911da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
15921da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
15931da177e4SLinus Torvalds 	 */
15941da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
15951da177e4SLinus Torvalds 		struct completion vfork;
15961da177e4SLinus Torvalds 
15970a16b607SMathieu Desnoyers 		trace_sched_process_fork(current, p);
15980a16b607SMathieu Desnoyers 
15996c5f3e7bSPavel Emelyanov 		nr = task_pid_vnr(p);
160030e49c26SPavel Emelyanov 
160130e49c26SPavel Emelyanov 		if (clone_flags & CLONE_PARENT_SETTID)
160230e49c26SPavel Emelyanov 			put_user(nr, parent_tidptr);
1603a6f5e063SSukadev Bhattiprolu 
16041da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
16051da177e4SLinus Torvalds 			p->vfork_done = &vfork;
16061da177e4SLinus Torvalds 			init_completion(&vfork);
1607d68b46feSOleg Nesterov 			get_task_struct(p);
16081da177e4SLinus Torvalds 		}
16091da177e4SLinus Torvalds 
16103e51e3edSSamir Bellabes 		wake_up_new_task(p);
16111da177e4SLinus Torvalds 
16124b9d33e6STejun Heo 		/* forking complete and child started to run, tell ptracer */
16134b9d33e6STejun Heo 		if (unlikely(trace))
16144b9d33e6STejun Heo 			ptrace_event(trace, nr);
161509a05394SRoland McGrath 
16161da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
1617d68b46feSOleg Nesterov 			if (!wait_for_vfork_done(p, &vfork))
1618a288eeccSTejun Heo 				ptrace_event(PTRACE_EVENT_VFORK_DONE, nr);
16199f59ce5dSChuck Ebbert 		}
16201da177e4SLinus Torvalds 	} else {
162192476d7fSEric W. Biederman 		nr = PTR_ERR(p);
16221da177e4SLinus Torvalds 	}
162392476d7fSEric W. Biederman 	return nr;
16241da177e4SLinus Torvalds }
16251da177e4SLinus Torvalds 
16265fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN
16275fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0
16285fd63b30SRavikiran G Thirumalai #endif
16295fd63b30SRavikiran G Thirumalai 
163051cc5068SAlexey Dobriyan static void sighand_ctor(void *data)
1631aa1757f9SOleg Nesterov {
1632aa1757f9SOleg Nesterov 	struct sighand_struct *sighand = data;
1633aa1757f9SOleg Nesterov 
1634aa1757f9SOleg Nesterov 	spin_lock_init(&sighand->siglock);
1635b8fceee1SDavide Libenzi 	init_waitqueue_head(&sighand->signalfd_wqh);
1636fba2afaaSDavide Libenzi }
1637aa1757f9SOleg Nesterov 
16381da177e4SLinus Torvalds void __init proc_caches_init(void)
16391da177e4SLinus Torvalds {
16401da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
16411da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
16422dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
16432dff4405SVegard Nossum 			SLAB_NOTRACK, sighand_ctor);
16441da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
16451da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
16462dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
16471da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
16481da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
16492dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
16501da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
16511da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
16522dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
16536345d24dSLinus Torvalds 	/*
16546345d24dSLinus Torvalds 	 * FIXME! The "sizeof(struct mm_struct)" currently includes the
16556345d24dSLinus Torvalds 	 * whole struct cpumask for the OFFSTACK case. We could change
16566345d24dSLinus Torvalds 	 * this to *only* allocate as much of it as required by the
16576345d24dSLinus Torvalds 	 * maximum number of CPU's we can ever have.  The cpumask_allocation
16586345d24dSLinus Torvalds 	 * is at the end of the structure, exactly for that reason.
16596345d24dSLinus Torvalds 	 */
16601da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
16615fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
16622dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
166333e5d769SDavid Howells 	vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC);
16648feae131SDavid Howells 	mmap_init();
166566577193SAl Viro 	nsproxy_cache_init();
16661da177e4SLinus Torvalds }
1667cf2e340fSJANAK DESAI 
1668cf2e340fSJANAK DESAI /*
16699bfb23fcSOleg Nesterov  * Check constraints on flags passed to the unshare system call.
1670cf2e340fSJANAK DESAI  */
16719bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags)
1672cf2e340fSJANAK DESAI {
16739bfb23fcSOleg Nesterov 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
16749bfb23fcSOleg Nesterov 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
16759bfb23fcSOleg Nesterov 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET))
1676cf2e340fSJANAK DESAI 		return -EINVAL;
16779bfb23fcSOleg Nesterov 	/*
16789bfb23fcSOleg Nesterov 	 * Not implemented, but pretend it works if there is nothing to
16799bfb23fcSOleg Nesterov 	 * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND
16809bfb23fcSOleg Nesterov 	 * needs to unshare vm.
16819bfb23fcSOleg Nesterov 	 */
16829bfb23fcSOleg Nesterov 	if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
16839bfb23fcSOleg Nesterov 		/* FIXME: get_task_mm() increments ->mm_users */
16849bfb23fcSOleg Nesterov 		if (atomic_read(&current->mm->mm_users) > 1)
16859bfb23fcSOleg Nesterov 			return -EINVAL;
16869bfb23fcSOleg Nesterov 	}
1687cf2e340fSJANAK DESAI 
1688cf2e340fSJANAK DESAI 	return 0;
1689cf2e340fSJANAK DESAI }
1690cf2e340fSJANAK DESAI 
1691cf2e340fSJANAK DESAI /*
169299d1419dSJANAK DESAI  * Unshare the filesystem structure if it is being shared
1693cf2e340fSJANAK DESAI  */
1694cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1695cf2e340fSJANAK DESAI {
1696cf2e340fSJANAK DESAI 	struct fs_struct *fs = current->fs;
1697cf2e340fSJANAK DESAI 
1698498052bbSAl Viro 	if (!(unshare_flags & CLONE_FS) || !fs)
1699498052bbSAl Viro 		return 0;
1700498052bbSAl Viro 
1701498052bbSAl Viro 	/* don't need lock here; in the worst case we'll do useless copy */
1702498052bbSAl Viro 	if (fs->users == 1)
1703498052bbSAl Viro 		return 0;
1704498052bbSAl Viro 
1705498052bbSAl Viro 	*new_fsp = copy_fs_struct(fs);
170699d1419dSJANAK DESAI 	if (!*new_fsp)
170799d1419dSJANAK DESAI 		return -ENOMEM;
1708cf2e340fSJANAK DESAI 
1709cf2e340fSJANAK DESAI 	return 0;
1710cf2e340fSJANAK DESAI }
1711cf2e340fSJANAK DESAI 
1712cf2e340fSJANAK DESAI /*
1713a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1714cf2e340fSJANAK DESAI  */
1715cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1716cf2e340fSJANAK DESAI {
1717cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1718a016f338SJANAK DESAI 	int error = 0;
1719cf2e340fSJANAK DESAI 
1720cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1721a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1722a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1723a016f338SJANAK DESAI 		if (!*new_fdp)
1724a016f338SJANAK DESAI 			return error;
1725a016f338SJANAK DESAI 	}
1726cf2e340fSJANAK DESAI 
1727cf2e340fSJANAK DESAI 	return 0;
1728cf2e340fSJANAK DESAI }
1729cf2e340fSJANAK DESAI 
1730cf2e340fSJANAK DESAI /*
1731cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1732cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1733cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1734cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1735cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1736cf2e340fSJANAK DESAI  * task_struct.
1737cf2e340fSJANAK DESAI  */
17386559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1739cf2e340fSJANAK DESAI {
1740cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1741cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1742cf7b708cSPavel Emelyanov 	struct nsproxy *new_nsproxy = NULL;
17439edff4abSManfred Spraul 	int do_sysvsem = 0;
17449bfb23fcSOleg Nesterov 	int err;
1745cf2e340fSJANAK DESAI 
17469bfb23fcSOleg Nesterov 	err = check_unshare_flags(unshare_flags);
17479bfb23fcSOleg Nesterov 	if (err)
174806f9d4f9SEric W. Biederman 		goto bad_unshare_out;
174906f9d4f9SEric W. Biederman 
17506013f67fSManfred Spraul 	/*
17519bfb23fcSOleg Nesterov 	 * If unsharing namespace, must also unshare filesystem information.
17529bfb23fcSOleg Nesterov 	 */
17539bfb23fcSOleg Nesterov 	if (unshare_flags & CLONE_NEWNS)
17549bfb23fcSOleg Nesterov 		unshare_flags |= CLONE_FS;
17559bfb23fcSOleg Nesterov 	/*
17566013f67fSManfred Spraul 	 * CLONE_NEWIPC must also detach from the undolist: after switching
17576013f67fSManfred Spraul 	 * to a new ipc namespace, the semaphore arrays from the old
17586013f67fSManfred Spraul 	 * namespace are unreachable.
17596013f67fSManfred Spraul 	 */
17606013f67fSManfred Spraul 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
17619edff4abSManfred Spraul 		do_sysvsem = 1;
1762fb0a685cSDaniel Rebelo de Oliveira 	err = unshare_fs(unshare_flags, &new_fs);
1763fb0a685cSDaniel Rebelo de Oliveira 	if (err)
17649bfb23fcSOleg Nesterov 		goto bad_unshare_out;
1765fb0a685cSDaniel Rebelo de Oliveira 	err = unshare_fd(unshare_flags, &new_fd);
1766fb0a685cSDaniel Rebelo de Oliveira 	if (err)
17679bfb23fcSOleg Nesterov 		goto bad_unshare_cleanup_fs;
1768fb0a685cSDaniel Rebelo de Oliveira 	err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, new_fs);
1769fb0a685cSDaniel Rebelo de Oliveira 	if (err)
17709edff4abSManfred Spraul 		goto bad_unshare_cleanup_fd;
1771cf2e340fSJANAK DESAI 
17729bfb23fcSOleg Nesterov 	if (new_fs || new_fd || do_sysvsem || new_nsproxy) {
17739edff4abSManfred Spraul 		if (do_sysvsem) {
17749edff4abSManfred Spraul 			/*
17759edff4abSManfred Spraul 			 * CLONE_SYSVSEM is equivalent to sys_exit().
17769edff4abSManfred Spraul 			 */
17779edff4abSManfred Spraul 			exit_sem(current);
17789edff4abSManfred Spraul 		}
1779ab516013SSerge E. Hallyn 
1780c0b2fc31SSerge Hallyn 		if (new_nsproxy) {
1781cf7b708cSPavel Emelyanov 			switch_task_namespaces(current, new_nsproxy);
1782cf7b708cSPavel Emelyanov 			new_nsproxy = NULL;
1783c0b2fc31SSerge Hallyn 		}
1784cf2e340fSJANAK DESAI 
1785cf7b708cSPavel Emelyanov 		task_lock(current);
1786cf7b708cSPavel Emelyanov 
1787cf2e340fSJANAK DESAI 		if (new_fs) {
1788cf2e340fSJANAK DESAI 			fs = current->fs;
17892a4419b5SNick Piggin 			spin_lock(&fs->lock);
1790cf2e340fSJANAK DESAI 			current->fs = new_fs;
1791498052bbSAl Viro 			if (--fs->users)
1792498052bbSAl Viro 				new_fs = NULL;
1793498052bbSAl Viro 			else
1794cf2e340fSJANAK DESAI 				new_fs = fs;
17952a4419b5SNick Piggin 			spin_unlock(&fs->lock);
1796cf2e340fSJANAK DESAI 		}
1797cf2e340fSJANAK DESAI 
1798cf2e340fSJANAK DESAI 		if (new_fd) {
1799cf2e340fSJANAK DESAI 			fd = current->files;
1800cf2e340fSJANAK DESAI 			current->files = new_fd;
1801cf2e340fSJANAK DESAI 			new_fd = fd;
1802cf2e340fSJANAK DESAI 		}
1803cf2e340fSJANAK DESAI 
1804cf2e340fSJANAK DESAI 		task_unlock(current);
1805cf2e340fSJANAK DESAI 	}
1806cf2e340fSJANAK DESAI 
1807c0b2fc31SSerge Hallyn 	if (new_nsproxy)
1808444f378bSLinus Torvalds 		put_nsproxy(new_nsproxy);
1809c0b2fc31SSerge Hallyn 
1810cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
1811cf2e340fSJANAK DESAI 	if (new_fd)
1812cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
1813cf2e340fSJANAK DESAI 
1814cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
1815cf2e340fSJANAK DESAI 	if (new_fs)
1816498052bbSAl Viro 		free_fs_struct(new_fs);
1817cf2e340fSJANAK DESAI 
1818cf2e340fSJANAK DESAI bad_unshare_out:
1819cf2e340fSJANAK DESAI 	return err;
1820cf2e340fSJANAK DESAI }
18213b125388SAl Viro 
18223b125388SAl Viro /*
18233b125388SAl Viro  *	Helper to unshare the files of the current task.
18243b125388SAl Viro  *	We don't want to expose copy_files internals to
18253b125388SAl Viro  *	the exec layer of the kernel.
18263b125388SAl Viro  */
18273b125388SAl Viro 
18283b125388SAl Viro int unshare_files(struct files_struct **displaced)
18293b125388SAl Viro {
18303b125388SAl Viro 	struct task_struct *task = current;
183150704516SAl Viro 	struct files_struct *copy = NULL;
18323b125388SAl Viro 	int error;
18333b125388SAl Viro 
18343b125388SAl Viro 	error = unshare_fd(CLONE_FILES, &copy);
18353b125388SAl Viro 	if (error || !copy) {
18363b125388SAl Viro 		*displaced = NULL;
18373b125388SAl Viro 		return error;
18383b125388SAl Viro 	}
18393b125388SAl Viro 	*displaced = task->files;
18403b125388SAl Viro 	task_lock(task);
18413b125388SAl Viro 	task->files = copy;
18423b125388SAl Viro 	task_unlock(task);
18433b125388SAl Viro 	return 0;
18443b125388SAl Viro }
1845