xref: /openbmc/linux/kernel/fork.c (revision a56704ef6b0c5796c9ff38cc78aa232dfb9644d7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/kernel/fork.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  *  'fork.c' contains the help-routines for the 'fork' system call
91da177e4SLinus Torvalds  * (see also entry.S and others).
101da177e4SLinus Torvalds  * Fork is rather simple, once you get the hang of it, but the memory
111da177e4SLinus Torvalds  * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/unistd.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/vmalloc.h>
191da177e4SLinus Torvalds #include <linux/completion.h>
201da177e4SLinus Torvalds #include <linux/personality.h>
211da177e4SLinus Torvalds #include <linux/mempolicy.h>
221da177e4SLinus Torvalds #include <linux/sem.h>
231da177e4SLinus Torvalds #include <linux/file.h>
249f3acc31SAl Viro #include <linux/fdtable.h>
25da9cbc87SJens Axboe #include <linux/iocontext.h>
261da177e4SLinus Torvalds #include <linux/key.h>
271da177e4SLinus Torvalds #include <linux/binfmts.h>
281da177e4SLinus Torvalds #include <linux/mman.h>
29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
301da177e4SLinus Torvalds #include <linux/fs.h>
31ab516013SSerge E. Hallyn #include <linux/nsproxy.h>
32c59ede7bSRandy.Dunlap #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/cpu.h>
34b4f48b63SPaul Menage #include <linux/cgroup.h>
351da177e4SLinus Torvalds #include <linux/security.h>
36a1e78772SMel Gorman #include <linux/hugetlb.h>
371da177e4SLinus Torvalds #include <linux/swap.h>
381da177e4SLinus Torvalds #include <linux/syscalls.h>
391da177e4SLinus Torvalds #include <linux/jiffies.h>
4009a05394SRoland McGrath #include <linux/tracehook.h>
411da177e4SLinus Torvalds #include <linux/futex.h>
428141c7f3SLinus Torvalds #include <linux/compat.h>
437c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h>
44ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
451da177e4SLinus Torvalds #include <linux/ptrace.h>
461da177e4SLinus Torvalds #include <linux/mount.h>
471da177e4SLinus Torvalds #include <linux/audit.h>
4878fb7466SPavel Emelianov #include <linux/memcontrol.h>
49f201ae23SFrederic Weisbecker #include <linux/ftrace.h>
501da177e4SLinus Torvalds #include <linux/profile.h>
511da177e4SLinus Torvalds #include <linux/rmap.h>
52f8af4da3SHugh Dickins #include <linux/ksm.h>
531da177e4SLinus Torvalds #include <linux/acct.h>
548f0ab514SJay Lan #include <linux/tsacct_kern.h>
559f46080cSMatt Helsley #include <linux/cn_proc.h>
56ba96a0c8SRafael J. Wysocki #include <linux/freezer.h>
57ca74e92bSShailabh Nagar #include <linux/delayacct.h>
58ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h>
590a425405SArjan van de Ven #include <linux/random.h>
60522ed776SMiloslav Trmac #include <linux/tty.h>
616f4e6433SPavel Emelyanov #include <linux/proc_fs.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>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <asm/pgtable.h>
701da177e4SLinus Torvalds #include <asm/pgalloc.h>
711da177e4SLinus Torvalds #include <asm/uaccess.h>
721da177e4SLinus Torvalds #include <asm/mmu_context.h>
731da177e4SLinus Torvalds #include <asm/cacheflush.h>
741da177e4SLinus Torvalds #include <asm/tlbflush.h>
751da177e4SLinus Torvalds 
76ad8d75ffSSteven Rostedt #include <trace/events/sched.h>
77ad8d75ffSSteven Rostedt 
781da177e4SLinus Torvalds /*
791da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
801da177e4SLinus Torvalds  */
811da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
821da177e4SLinus Torvalds int nr_threads; 		/* The idle threads do not count.. */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
89d11c563dSPaul E. McKenney EXPORT_SYMBOL_GPL(tasklist_lock);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds int nr_processes(void)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	int cpu;
941da177e4SLinus Torvalds 	int total = 0;
951da177e4SLinus Torvalds 
961d510750SIan Campbell 	for_each_possible_cpu(cpu)
971da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	return total;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
1031da177e4SLinus Torvalds # define alloc_task_struct()	kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
1041da177e4SLinus Torvalds # define free_task_struct(tsk)	kmem_cache_free(task_struct_cachep, (tsk))
105e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep;
1061da177e4SLinus Torvalds #endif
1071da177e4SLinus Torvalds 
108b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR
109b69c49b7SFUJITA Tomonori static inline struct thread_info *alloc_thread_info(struct task_struct *tsk)
110b69c49b7SFUJITA Tomonori {
111b69c49b7SFUJITA Tomonori #ifdef CONFIG_DEBUG_STACK_USAGE
112b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL | __GFP_ZERO;
113b69c49b7SFUJITA Tomonori #else
114b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL;
115b69c49b7SFUJITA Tomonori #endif
116b69c49b7SFUJITA Tomonori 	return (struct thread_info *)__get_free_pages(mask, THREAD_SIZE_ORDER);
117b69c49b7SFUJITA Tomonori }
118b69c49b7SFUJITA Tomonori 
119b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti)
120b69c49b7SFUJITA Tomonori {
121b69c49b7SFUJITA Tomonori 	free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
122b69c49b7SFUJITA Tomonori }
123b69c49b7SFUJITA Tomonori #endif
124b69c49b7SFUJITA Tomonori 
1251da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
126e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
129e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
132e18b890bSChristoph Lameter struct kmem_cache *files_cachep;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
135e18b890bSChristoph Lameter struct kmem_cache *fs_cachep;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
138e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
141e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep;
1421da177e4SLinus Torvalds 
143c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account)
144c6a7f572SKOSAKI Motohiro {
145c6a7f572SKOSAKI Motohiro 	struct zone *zone = page_zone(virt_to_page(ti));
146c6a7f572SKOSAKI Motohiro 
147c6a7f572SKOSAKI Motohiro 	mod_zone_page_state(zone, NR_KERNEL_STACK, account);
148c6a7f572SKOSAKI Motohiro }
149c6a7f572SKOSAKI Motohiro 
1501da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1511da177e4SLinus Torvalds {
1523e26c149SPeter Zijlstra 	prop_local_destroy_single(&tsk->dirties);
153c6a7f572SKOSAKI Motohiro 	account_kernel_stack(tsk->stack, -1);
154f7e4217bSRoman Zippel 	free_thread_info(tsk->stack);
15523f78d4aSIngo Molnar 	rt_mutex_debug_task_free(tsk);
156fb52607aSFrederic Weisbecker 	ftrace_graph_exit_task(tsk);
1571da177e4SLinus Torvalds 	free_task_struct(tsk);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1601da177e4SLinus Torvalds 
161158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
1621da177e4SLinus Torvalds {
163270f722dSEugene Teo 	WARN_ON(!tsk->exit_state);
1641da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1651da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1661da177e4SLinus Torvalds 
167e0e81739SDavid Howells 	exit_creds(tsk);
16835df17c5SShailabh Nagar 	delayacct_tsk_free(tsk);
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
1711da177e4SLinus Torvalds 		free_task(tsk);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1742adee9b3SSuresh Siddha /*
1752adee9b3SSuresh Siddha  * macro override instead of weak attribute alias, to workaround
1762adee9b3SSuresh Siddha  * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
1772adee9b3SSuresh Siddha  */
1782adee9b3SSuresh Siddha #ifndef arch_task_cache_init
1792adee9b3SSuresh Siddha #define arch_task_cache_init()
1802adee9b3SSuresh Siddha #endif
18161c4628bSSuresh Siddha 
1821da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
1851da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
1861da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
1871da177e4SLinus Torvalds #endif
1881da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
1891da177e4SLinus Torvalds 	task_struct_cachep =
1901da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
1912dff4405SVegard Nossum 			ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL);
1921da177e4SLinus Torvalds #endif
1931da177e4SLinus Torvalds 
19461c4628bSSuresh Siddha 	/* do the arch specific task caches init */
19561c4628bSSuresh Siddha 	arch_task_cache_init();
19661c4628bSSuresh Siddha 
1971da177e4SLinus Torvalds 	/*
1981da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
1991da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
2001da177e4SLinus Torvalds 	 * of memory.
2011da177e4SLinus Torvalds 	 */
2021da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	/*
2051da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
2061da177e4SLinus Torvalds 	 */
2071da177e4SLinus Torvalds 	if(max_threads < 20)
2081da177e4SLinus Torvalds 		max_threads = 20;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
2111da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
2121da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
2131da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
21661c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
21761c4628bSSuresh Siddha 					       struct task_struct *src)
21861c4628bSSuresh Siddha {
21961c4628bSSuresh Siddha 	*dst = *src;
22061c4628bSSuresh Siddha 	return 0;
22161c4628bSSuresh Siddha }
22261c4628bSSuresh Siddha 
2231da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	struct task_struct *tsk;
2261da177e4SLinus Torvalds 	struct thread_info *ti;
2277c9f8861SEric Sandeen 	unsigned long *stackend;
2287c9f8861SEric Sandeen 
2293e26c149SPeter Zijlstra 	int err;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	prepare_to_copy(orig);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	tsk = alloc_task_struct();
2341da177e4SLinus Torvalds 	if (!tsk)
2351da177e4SLinus Torvalds 		return NULL;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	ti = alloc_thread_info(tsk);
2381da177e4SLinus Torvalds 	if (!ti) {
2391da177e4SLinus Torvalds 		free_task_struct(tsk);
2401da177e4SLinus Torvalds 		return NULL;
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds 
24361c4628bSSuresh Siddha  	err = arch_dup_task_struct(tsk, orig);
24461c4628bSSuresh Siddha 	if (err)
24561c4628bSSuresh Siddha 		goto out;
24661c4628bSSuresh Siddha 
247f7e4217bSRoman Zippel 	tsk->stack = ti;
2483e26c149SPeter Zijlstra 
2493e26c149SPeter Zijlstra 	err = prop_local_init_single(&tsk->dirties);
25061c4628bSSuresh Siddha 	if (err)
25161c4628bSSuresh Siddha 		goto out;
2523e26c149SPeter Zijlstra 
25310ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
2548e7cac79SAvi Kivity 	clear_user_return_notifier(tsk);
2557c9f8861SEric Sandeen 	stackend = end_of_stack(tsk);
2567c9f8861SEric Sandeen 	*stackend = STACK_END_MAGIC;	/* for overflow detection */
2571da177e4SLinus Torvalds 
2580a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
2590a425405SArjan van de Ven 	tsk->stack_canary = get_random_int();
2600a425405SArjan van de Ven #endif
2610a425405SArjan van de Ven 
2621da177e4SLinus Torvalds 	/* One for us, one for whoever does the "release_task()" (usually parent) */
2631da177e4SLinus Torvalds 	atomic_set(&tsk->usage,2);
2644b5d37acSGiancarlo Formicuccia 	atomic_set(&tsk->fs_excl, 0);
2656c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
2662056a782SJens Axboe 	tsk->btrace_seq = 0;
2676c5c9341SAlexey Dobriyan #endif
268a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
269c6a7f572SKOSAKI Motohiro 
270c6a7f572SKOSAKI Motohiro 	account_kernel_stack(ti, 1);
271c6a7f572SKOSAKI Motohiro 
2721da177e4SLinus Torvalds 	return tsk;
27361c4628bSSuresh Siddha 
27461c4628bSSuresh Siddha out:
27561c4628bSSuresh Siddha 	free_thread_info(ti);
27661c4628bSSuresh Siddha 	free_task_struct(tsk);
27761c4628bSSuresh Siddha 	return NULL;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds #ifdef CONFIG_MMU
281a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, **pprev;
2841da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
2851da177e4SLinus Torvalds 	int retval;
2861da177e4SLinus Torvalds 	unsigned long charge;
2871da177e4SLinus Torvalds 	struct mempolicy *pol;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
290ec8c0446SRalf Baechle 	flush_cache_dup_mm(oldmm);
291ad339451SIngo Molnar 	/*
292ad339451SIngo Molnar 	 * Not linked in yet - no deadlock potential:
293ad339451SIngo Molnar 	 */
294ad339451SIngo Molnar 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
2957ee78232SHugh Dickins 
2961da177e4SLinus Torvalds 	mm->locked_vm = 0;
2971da177e4SLinus Torvalds 	mm->mmap = NULL;
2981da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
2991da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
3001363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
3011da177e4SLinus Torvalds 	mm->map_count = 0;
30294894244SRusty Russell 	cpumask_clear(mm_cpumask(mm));
3031da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
3041da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
3051da177e4SLinus Torvalds 	rb_parent = NULL;
3061da177e4SLinus Torvalds 	pprev = &mm->mmap;
307f8af4da3SHugh Dickins 	retval = ksm_fork(mm, oldmm);
308f8af4da3SHugh Dickins 	if (retval)
309f8af4da3SHugh Dickins 		goto out;
3101da177e4SLinus Torvalds 
311fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
3121da177e4SLinus Torvalds 		struct file *file;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
3153b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
3163b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
317ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
3183b6bfcdbSHugh Dickins 								-pages);
3191da177e4SLinus Torvalds 			continue;
3201da177e4SLinus Torvalds 		}
3211da177e4SLinus Torvalds 		charge = 0;
3221da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
3231da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
3241da177e4SLinus Torvalds 			if (security_vm_enough_memory(len))
3251da177e4SLinus Torvalds 				goto fail_nomem;
3261da177e4SLinus Torvalds 			charge = len;
3271da177e4SLinus Torvalds 		}
328e94b1766SChristoph Lameter 		tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
3291da177e4SLinus Torvalds 		if (!tmp)
3301da177e4SLinus Torvalds 			goto fail_nomem;
3311da177e4SLinus Torvalds 		*tmp = *mpnt;
3325beb4930SRik van Riel 		INIT_LIST_HEAD(&tmp->anon_vma_chain);
333846a16bfSLee Schermerhorn 		pol = mpol_dup(vma_policy(mpnt));
3341da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
3351da177e4SLinus Torvalds 		if (IS_ERR(pol))
3361da177e4SLinus Torvalds 			goto fail_nomem_policy;
3371da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
3385beb4930SRik van Riel 		if (anon_vma_fork(tmp, mpnt))
3395beb4930SRik van Riel 			goto fail_nomem_anon_vma_fork;
3401da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
3411da177e4SLinus Torvalds 		tmp->vm_mm = mm;
3421da177e4SLinus Torvalds 		tmp->vm_next = NULL;
3431da177e4SLinus Torvalds 		file = tmp->vm_file;
3441da177e4SLinus Torvalds 		if (file) {
345f3a43f3fSJosef "Jeff" Sipek 			struct inode *inode = file->f_path.dentry->d_inode;
346b88ed205SHugh Dickins 			struct address_space *mapping = file->f_mapping;
347b88ed205SHugh Dickins 
3481da177e4SLinus Torvalds 			get_file(file);
3491da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
3501da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
351b88ed205SHugh Dickins 			spin_lock(&mapping->i_mmap_lock);
352b88ed205SHugh Dickins 			if (tmp->vm_flags & VM_SHARED)
353b88ed205SHugh Dickins 				mapping->i_mmap_writable++;
3541da177e4SLinus Torvalds 			tmp->vm_truncate_count = mpnt->vm_truncate_count;
355b88ed205SHugh Dickins 			flush_dcache_mmap_lock(mapping);
356b88ed205SHugh Dickins 			/* insert tmp into the share list, just after mpnt */
3571da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
358b88ed205SHugh Dickins 			flush_dcache_mmap_unlock(mapping);
359b88ed205SHugh Dickins 			spin_unlock(&mapping->i_mmap_lock);
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		/*
363a1e78772SMel Gorman 		 * Clear hugetlb-related page reserves for children. This only
364a1e78772SMel Gorman 		 * affects MAP_PRIVATE mappings. Faults generated by the child
365a1e78772SMel Gorman 		 * are not guaranteed to succeed, even if read-only
366a1e78772SMel Gorman 		 */
367a1e78772SMel Gorman 		if (is_vm_hugetlb_page(tmp))
368a1e78772SMel Gorman 			reset_vma_resv_huge_pages(tmp);
369a1e78772SMel Gorman 
370a1e78772SMel Gorman 		/*
3717ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
3721da177e4SLinus Torvalds 		 */
3731da177e4SLinus Torvalds 		*pprev = tmp;
3741da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
3771da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
3781da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 		mm->map_count++;
3810b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
3841da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		if (retval)
3871da177e4SLinus Torvalds 			goto out;
3881da177e4SLinus Torvalds 	}
389d6dd61c8SJeremy Fitzhardinge 	/* a new mm has just been created */
390d6dd61c8SJeremy Fitzhardinge 	arch_dup_mmap(oldmm, mm);
3911da177e4SLinus Torvalds 	retval = 0;
3921da177e4SLinus Torvalds out:
3937ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
394fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
3951da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
3961da177e4SLinus Torvalds 	return retval;
3975beb4930SRik van Riel fail_nomem_anon_vma_fork:
3985beb4930SRik van Riel 	mpol_put(pol);
3991da177e4SLinus Torvalds fail_nomem_policy:
4001da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
4011da177e4SLinus Torvalds fail_nomem:
4021da177e4SLinus Torvalds 	retval = -ENOMEM;
4031da177e4SLinus Torvalds 	vm_unacct_memory(charge);
4041da177e4SLinus Torvalds 	goto out;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
4101da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
4111da177e4SLinus Torvalds 		return -ENOMEM;
4121da177e4SLinus Torvalds 	return 0;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm)
4161da177e4SLinus Torvalds {
4175e541973SBenjamin Herrenschmidt 	pgd_free(mm, mm->pgd);
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds #else
4201da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
4211da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
4221da177e4SLinus Torvalds #define mm_free_pgd(mm)
4231da177e4SLinus Torvalds #endif /* CONFIG_MMU */
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
4261da177e4SLinus Torvalds 
427e94b1766SChristoph Lameter #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
4281da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
4291da177e4SLinus Torvalds 
4304cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
4314cb0e11bSHidehiro Kawai 
4324cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s)
4334cb0e11bSHidehiro Kawai {
4344cb0e11bSHidehiro Kawai 	default_dump_filter =
4354cb0e11bSHidehiro Kawai 		(simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
4364cb0e11bSHidehiro Kawai 		MMF_DUMP_FILTER_MASK;
4374cb0e11bSHidehiro Kawai 	return 1;
4384cb0e11bSHidehiro Kawai }
4394cb0e11bSHidehiro Kawai 
4404cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup);
4414cb0e11bSHidehiro Kawai 
4421da177e4SLinus Torvalds #include <linux/init_task.h>
4431da177e4SLinus Torvalds 
444858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm)
445858f0993SAlexey Dobriyan {
446858f0993SAlexey Dobriyan #ifdef CONFIG_AIO
447858f0993SAlexey Dobriyan 	spin_lock_init(&mm->ioctx_lock);
448858f0993SAlexey Dobriyan 	INIT_HLIST_HEAD(&mm->ioctx_list);
449858f0993SAlexey Dobriyan #endif
450858f0993SAlexey Dobriyan }
451858f0993SAlexey Dobriyan 
45278fb7466SPavel Emelianov static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
4551da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
4561da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
4571da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
458f8af4da3SHugh Dickins 	mm->flags = (current->mm) ?
459f8af4da3SHugh Dickins 		(current->mm->flags & MMF_INIT_MASK) : default_dump_filter;
460999d9fc1SOleg Nesterov 	mm->core_state = NULL;
4611da177e4SLinus Torvalds 	mm->nr_ptes = 0;
462d559db08SKAMEZAWA Hiroyuki 	memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
4631da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
4641da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
4651363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
466858f0993SAlexey Dobriyan 	mm_init_aio(mm);
467cf475ad2SBalbir Singh 	mm_init_owner(mm, p);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
4701da177e4SLinus Torvalds 		mm->def_flags = 0;
471cddb8a5cSAndrea Arcangeli 		mmu_notifier_mm_init(mm);
4721da177e4SLinus Torvalds 		return mm;
4731da177e4SLinus Torvalds 	}
47478fb7466SPavel Emelianov 
4751da177e4SLinus Torvalds 	free_mm(mm);
4761da177e4SLinus Torvalds 	return NULL;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds /*
4801da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
4811da177e4SLinus Torvalds  */
4821da177e4SLinus Torvalds struct mm_struct * mm_alloc(void)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	struct mm_struct * mm;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	mm = allocate_mm();
4871da177e4SLinus Torvalds 	if (mm) {
4881da177e4SLinus Torvalds 		memset(mm, 0, sizeof(*mm));
48978fb7466SPavel Emelianov 		mm = mm_init(mm, current);
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 	return mm;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds /*
4951da177e4SLinus Torvalds  * Called when the last reference to the mm
4961da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
4971da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
4981da177e4SLinus Torvalds  */
4997ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
5021da177e4SLinus Torvalds 	mm_free_pgd(mm);
5031da177e4SLinus Torvalds 	destroy_context(mm);
504cddb8a5cSAndrea Arcangeli 	mmu_notifier_mm_destroy(mm);
5051da177e4SLinus Torvalds 	free_mm(mm);
5061da177e4SLinus Torvalds }
5076d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds /*
5101da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
5111da177e4SLinus Torvalds  */
5121da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
5131da177e4SLinus Torvalds {
5140ae26f1bSAndrew Morton 	might_sleep();
5150ae26f1bSAndrew Morton 
5161da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
5171da177e4SLinus Torvalds 		exit_aio(mm);
5181c2fb7a4SAndrea Arcangeli 		ksm_exit(mm);
5191da177e4SLinus Torvalds 		exit_mmap(mm);
520925d1c40SMatt Helsley 		set_mm_exe_file(mm, NULL);
5211da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
5221da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
5231da177e4SLinus Torvalds 			list_del(&mm->mmlist);
5241da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 		put_swap_token(mm);
527801460d0SHiroshi Shimamoto 		if (mm->binfmt)
528801460d0SHiroshi Shimamoto 			module_put(mm->binfmt->module);
5291da177e4SLinus Torvalds 		mmdrop(mm);
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds /**
5351da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
5361da177e4SLinus Torvalds  *
537246bb0b1SOleg Nesterov  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
5381da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
5391da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
5401da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
5411da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
5421da177e4SLinus Torvalds  */
5431da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	struct mm_struct *mm;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	task_lock(task);
5481da177e4SLinus Torvalds 	mm = task->mm;
5491da177e4SLinus Torvalds 	if (mm) {
550246bb0b1SOleg Nesterov 		if (task->flags & PF_KTHREAD)
5511da177e4SLinus Torvalds 			mm = NULL;
5521da177e4SLinus Torvalds 		else
5531da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 	task_unlock(task);
5561da177e4SLinus Torvalds 	return mm;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
5611da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
5621da177e4SLinus Torvalds  * error success whatever.
5631da177e4SLinus Torvalds  *
5641da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
5651da177e4SLinus Torvalds  * from the current process.
5661da177e4SLinus Torvalds  *
5671da177e4SLinus Torvalds  * This difference is important for error handling, when we
5681da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
5691da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
5701da177e4SLinus Torvalds  * restoring the old one. . .
5711da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
5721da177e4SLinus Torvalds  */
5731da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	struct completion *vfork_done = tsk->vfork_done;
5761da177e4SLinus Torvalds 
5778141c7f3SLinus Torvalds 	/* Get rid of any futexes when releasing the mm */
5788141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX
579fc6b177dSPeter Zijlstra 	if (unlikely(tsk->robust_list)) {
5808141c7f3SLinus Torvalds 		exit_robust_list(tsk);
581fc6b177dSPeter Zijlstra 		tsk->robust_list = NULL;
582fc6b177dSPeter Zijlstra 	}
5838141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT
584fc6b177dSPeter Zijlstra 	if (unlikely(tsk->compat_robust_list)) {
5858141c7f3SLinus Torvalds 		compat_exit_robust_list(tsk);
586fc6b177dSPeter Zijlstra 		tsk->compat_robust_list = NULL;
587fc6b177dSPeter Zijlstra 	}
5888141c7f3SLinus Torvalds #endif
589322a2c10SThomas Gleixner 	if (unlikely(!list_empty(&tsk->pi_state_list)))
590322a2c10SThomas Gleixner 		exit_pi_state_list(tsk);
5918141c7f3SLinus Torvalds #endif
5928141c7f3SLinus Torvalds 
5931da177e4SLinus Torvalds 	/* Get rid of any cached register state */
5941da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	/* notify parent sleeping on vfork() */
5971da177e4SLinus Torvalds 	if (vfork_done) {
5981da177e4SLinus Torvalds 		tsk->vfork_done = NULL;
5991da177e4SLinus Torvalds 		complete(vfork_done);
6001da177e4SLinus Torvalds 	}
601fec1d011SRoland McGrath 
602fec1d011SRoland McGrath 	/*
603fec1d011SRoland McGrath 	 * If we're exiting normally, clear a user-space tid field if
604fec1d011SRoland McGrath 	 * requested.  We leave this alone when dying by signal, to leave
605fec1d011SRoland McGrath 	 * the value intact in a core dump, and to save the unnecessary
606fec1d011SRoland McGrath 	 * trouble otherwise.  Userland only wants this done for a sys_exit.
607fec1d011SRoland McGrath 	 */
6089c8a8228SEric Dumazet 	if (tsk->clear_child_tid) {
6099c8a8228SEric Dumazet 		if (!(tsk->flags & PF_SIGNALED) &&
6109c8a8228SEric Dumazet 		    atomic_read(&mm->mm_users) > 1) {
6111da177e4SLinus Torvalds 			/*
6121da177e4SLinus Torvalds 			 * We don't check the error code - if userspace has
6131da177e4SLinus Torvalds 			 * not set up a proper pointer then tough luck.
6141da177e4SLinus Torvalds 			 */
6159c8a8228SEric Dumazet 			put_user(0, tsk->clear_child_tid);
6169c8a8228SEric Dumazet 			sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
6179c8a8228SEric Dumazet 					1, NULL, NULL, 0);
6189c8a8228SEric Dumazet 		}
6199c8a8228SEric Dumazet 		tsk->clear_child_tid = NULL;
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
623a0a7ec30SJANAK DESAI /*
624a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
625a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
626a0a7ec30SJANAK DESAI  */
627402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk)
628a0a7ec30SJANAK DESAI {
629a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
630a0a7ec30SJANAK DESAI 	int err;
631a0a7ec30SJANAK DESAI 
632a0a7ec30SJANAK DESAI 	if (!oldmm)
633a0a7ec30SJANAK DESAI 		return NULL;
634a0a7ec30SJANAK DESAI 
635a0a7ec30SJANAK DESAI 	mm = allocate_mm();
636a0a7ec30SJANAK DESAI 	if (!mm)
637a0a7ec30SJANAK DESAI 		goto fail_nomem;
638a0a7ec30SJANAK DESAI 
639a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
640a0a7ec30SJANAK DESAI 
6417602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
6427602bdf2SAshwin Chaugule 	mm->token_priority = 0;
6437602bdf2SAshwin Chaugule 	mm->last_interval = 0;
6447602bdf2SAshwin Chaugule 
64578fb7466SPavel Emelianov 	if (!mm_init(mm, tsk))
646a0a7ec30SJANAK DESAI 		goto fail_nomem;
647a0a7ec30SJANAK DESAI 
648a0a7ec30SJANAK DESAI 	if (init_new_context(tsk, mm))
649a0a7ec30SJANAK DESAI 		goto fail_nocontext;
650a0a7ec30SJANAK DESAI 
651925d1c40SMatt Helsley 	dup_mm_exe_file(oldmm, mm);
652925d1c40SMatt Helsley 
653a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
654a0a7ec30SJANAK DESAI 	if (err)
655a0a7ec30SJANAK DESAI 		goto free_pt;
656a0a7ec30SJANAK DESAI 
657a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
658a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
659a0a7ec30SJANAK DESAI 
660801460d0SHiroshi Shimamoto 	if (mm->binfmt && !try_module_get(mm->binfmt->module))
661801460d0SHiroshi Shimamoto 		goto free_pt;
662801460d0SHiroshi Shimamoto 
663a0a7ec30SJANAK DESAI 	return mm;
664a0a7ec30SJANAK DESAI 
665a0a7ec30SJANAK DESAI free_pt:
666801460d0SHiroshi Shimamoto 	/* don't put binfmt in mmput, we haven't got module yet */
667801460d0SHiroshi Shimamoto 	mm->binfmt = NULL;
668a0a7ec30SJANAK DESAI 	mmput(mm);
669a0a7ec30SJANAK DESAI 
670a0a7ec30SJANAK DESAI fail_nomem:
671a0a7ec30SJANAK DESAI 	return NULL;
672a0a7ec30SJANAK DESAI 
673a0a7ec30SJANAK DESAI fail_nocontext:
674a0a7ec30SJANAK DESAI 	/*
675a0a7ec30SJANAK DESAI 	 * If init_new_context() failed, we cannot use mmput() to free the mm
676a0a7ec30SJANAK DESAI 	 * because it calls destroy_context()
677a0a7ec30SJANAK DESAI 	 */
678a0a7ec30SJANAK DESAI 	mm_free_pgd(mm);
679a0a7ec30SJANAK DESAI 	free_mm(mm);
680a0a7ec30SJANAK DESAI 	return NULL;
681a0a7ec30SJANAK DESAI }
682a0a7ec30SJANAK DESAI 
6831da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	struct mm_struct * mm, *oldmm;
6861da177e4SLinus Torvalds 	int retval;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
6891da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
69017406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
69117406b82SMandeep Singh Baines 	tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
69217406b82SMandeep Singh Baines #endif
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	tsk->mm = NULL;
6951da177e4SLinus Torvalds 	tsk->active_mm = NULL;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	/*
6981da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
6991da177e4SLinus Torvalds 	 *
7001da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
7011da177e4SLinus Torvalds 	 */
7021da177e4SLinus Torvalds 	oldmm = current->mm;
7031da177e4SLinus Torvalds 	if (!oldmm)
7041da177e4SLinus Torvalds 		return 0;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
7071da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
7081da177e4SLinus Torvalds 		mm = oldmm;
7091da177e4SLinus Torvalds 		goto good_mm;
7101da177e4SLinus Torvalds 	}
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	retval = -ENOMEM;
713a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
7141da177e4SLinus Torvalds 	if (!mm)
7151da177e4SLinus Torvalds 		goto fail_nomem;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds good_mm:
7187602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
7197602bdf2SAshwin Chaugule 	mm->token_priority = 0;
7207602bdf2SAshwin Chaugule 	mm->last_interval = 0;
7217602bdf2SAshwin Chaugule 
7221da177e4SLinus Torvalds 	tsk->mm = mm;
7231da177e4SLinus Torvalds 	tsk->active_mm = mm;
7241da177e4SLinus Torvalds 	return 0;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds fail_nomem:
7271da177e4SLinus Torvalds 	return retval;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
730a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
7311da177e4SLinus Torvalds {
732498052bbSAl Viro 	struct fs_struct *fs = current->fs;
7331da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
734498052bbSAl Viro 		/* tsk->fs is already what we want */
735498052bbSAl Viro 		write_lock(&fs->lock);
736498052bbSAl Viro 		if (fs->in_exec) {
737498052bbSAl Viro 			write_unlock(&fs->lock);
738498052bbSAl Viro 			return -EAGAIN;
739498052bbSAl Viro 		}
740498052bbSAl Viro 		fs->users++;
741498052bbSAl Viro 		write_unlock(&fs->lock);
7421da177e4SLinus Torvalds 		return 0;
7431da177e4SLinus Torvalds 	}
744498052bbSAl Viro 	tsk->fs = copy_fs_struct(fs);
7451da177e4SLinus Torvalds 	if (!tsk->fs)
7461da177e4SLinus Torvalds 		return -ENOMEM;
7471da177e4SLinus Torvalds 	return 0;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
750a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
751a016f338SJANAK DESAI {
752a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
753a016f338SJANAK DESAI 	int error = 0;
754a016f338SJANAK DESAI 
755a016f338SJANAK DESAI 	/*
756a016f338SJANAK DESAI 	 * A background process may not have any files ...
757a016f338SJANAK DESAI 	 */
758a016f338SJANAK DESAI 	oldf = current->files;
759a016f338SJANAK DESAI 	if (!oldf)
760a016f338SJANAK DESAI 		goto out;
761a016f338SJANAK DESAI 
762a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
763a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
764a016f338SJANAK DESAI 		goto out;
765a016f338SJANAK DESAI 	}
766a016f338SJANAK DESAI 
767a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
768a016f338SJANAK DESAI 	if (!newf)
769a016f338SJANAK DESAI 		goto out;
770a016f338SJANAK DESAI 
771a016f338SJANAK DESAI 	tsk->files = newf;
772a016f338SJANAK DESAI 	error = 0;
773a016f338SJANAK DESAI out:
774a016f338SJANAK DESAI 	return error;
775a016f338SJANAK DESAI }
776a016f338SJANAK DESAI 
777fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
778fd0928dfSJens Axboe {
779fd0928dfSJens Axboe #ifdef CONFIG_BLOCK
780fd0928dfSJens Axboe 	struct io_context *ioc = current->io_context;
781fd0928dfSJens Axboe 
782fd0928dfSJens Axboe 	if (!ioc)
783fd0928dfSJens Axboe 		return 0;
784fadad878SJens Axboe 	/*
785fadad878SJens Axboe 	 * Share io context with parent, if CLONE_IO is set
786fadad878SJens Axboe 	 */
787fadad878SJens Axboe 	if (clone_flags & CLONE_IO) {
788fadad878SJens Axboe 		tsk->io_context = ioc_task_link(ioc);
789fadad878SJens Axboe 		if (unlikely(!tsk->io_context))
790fadad878SJens Axboe 			return -ENOMEM;
791fadad878SJens Axboe 	} else if (ioprio_valid(ioc->ioprio)) {
792fd0928dfSJens Axboe 		tsk->io_context = alloc_io_context(GFP_KERNEL, -1);
793fd0928dfSJens Axboe 		if (unlikely(!tsk->io_context))
794fd0928dfSJens Axboe 			return -ENOMEM;
795fd0928dfSJens Axboe 
796fd0928dfSJens Axboe 		tsk->io_context->ioprio = ioc->ioprio;
797fd0928dfSJens Axboe 	}
798fd0928dfSJens Axboe #endif
799fd0928dfSJens Axboe 	return 0;
800fd0928dfSJens Axboe }
801fd0928dfSJens Axboe 
802a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	struct sighand_struct *sig;
8051da177e4SLinus Torvalds 
80660348802SZhaolei 	if (clone_flags & CLONE_SIGHAND) {
8071da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
8081da177e4SLinus Torvalds 		return 0;
8091da177e4SLinus Torvalds 	}
8101da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
811e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
8121da177e4SLinus Torvalds 	if (!sig)
8131da177e4SLinus Torvalds 		return -ENOMEM;
8141da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
8151da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
8161da177e4SLinus Torvalds 	return 0;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
819a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
820c81addc9SOleg Nesterov {
821c81addc9SOleg Nesterov 	if (atomic_dec_and_test(&sighand->count))
822c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
823c81addc9SOleg Nesterov }
824c81addc9SOleg Nesterov 
825f06febc9SFrank Mayhar 
826f06febc9SFrank Mayhar /*
827f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a thread group.
828f06febc9SFrank Mayhar  */
829f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig)
830f06febc9SFrank Mayhar {
83178d7d407SJiri Slaby 	unsigned long cpu_limit;
83278d7d407SJiri Slaby 
833f06febc9SFrank Mayhar 	/* Thread group counters. */
834f06febc9SFrank Mayhar 	thread_group_cputime_init(sig);
835f06febc9SFrank Mayhar 
836f06febc9SFrank Mayhar 	/* Expiration times and increments. */
83742c4ab41SStanislaw Gruszka 	sig->it[CPUCLOCK_PROF].expires = cputime_zero;
83842c4ab41SStanislaw Gruszka 	sig->it[CPUCLOCK_PROF].incr = cputime_zero;
83942c4ab41SStanislaw Gruszka 	sig->it[CPUCLOCK_VIRT].expires = cputime_zero;
84042c4ab41SStanislaw Gruszka 	sig->it[CPUCLOCK_VIRT].incr = cputime_zero;
841f06febc9SFrank Mayhar 
842f06febc9SFrank Mayhar 	/* Cached expiration times. */
843f06febc9SFrank Mayhar 	sig->cputime_expires.prof_exp = cputime_zero;
844f06febc9SFrank Mayhar 	sig->cputime_expires.virt_exp = cputime_zero;
845f06febc9SFrank Mayhar 	sig->cputime_expires.sched_exp = 0;
846f06febc9SFrank Mayhar 
84778d7d407SJiri Slaby 	cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
84878d7d407SJiri Slaby 	if (cpu_limit != RLIM_INFINITY) {
84978d7d407SJiri Slaby 		sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
8506279a751SOleg Nesterov 		sig->cputimer.running = 1;
8516279a751SOleg Nesterov 	}
8526279a751SOleg Nesterov 
853f06febc9SFrank Mayhar 	/* The timer lists. */
854f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
855f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
856f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
857f06febc9SFrank Mayhar }
858f06febc9SFrank Mayhar 
859a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	struct signal_struct *sig;
8621da177e4SLinus Torvalds 
8634ab6c083SOleg Nesterov 	if (clone_flags & CLONE_THREAD)
864490dea45SPeter Zijlstra 		return 0;
8656279a751SOleg Nesterov 
866*a56704efSVeaceslav Falico 	sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
8671da177e4SLinus Torvalds 	tsk->signal = sig;
8681da177e4SLinus Torvalds 	if (!sig)
8691da177e4SLinus Torvalds 		return -ENOMEM;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
8721da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
8731da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
874b3bfa0cbSSukadev Bhattiprolu 	if (clone_flags & CLONE_NEWPID)
875b3bfa0cbSSukadev Bhattiprolu 		sig->flags |= SIGNAL_UNKILLABLE;
876db51aeccSOleg Nesterov 	sig->curr_target = tsk;
8771da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
8781da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
8791da177e4SLinus Torvalds 
880c9cb2e3dSThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8811da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	task_lock(current->group_leader);
8841da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
8851da177e4SLinus Torvalds 	task_unlock(current->group_leader);
8861da177e4SLinus Torvalds 
8876279a751SOleg Nesterov 	posix_cpu_timers_init_group(sig);
8886279a751SOleg Nesterov 
889522ed776SMiloslav Trmac 	tty_audit_fork(sig);
890522ed776SMiloslav Trmac 
89128b83c51SKOSAKI Motohiro 	sig->oom_adj = current->signal->oom_adj;
89228b83c51SKOSAKI Motohiro 
8931da177e4SLinus Torvalds 	return 0;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
8966b3934efSOleg Nesterov void __cleanup_signal(struct signal_struct *sig)
8976b3934efSOleg Nesterov {
898f06febc9SFrank Mayhar 	thread_group_cputime_free(sig);
8999c9f4dedSAlan Cox 	tty_kref_put(sig->tty);
9006b3934efSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
9016b3934efSOleg Nesterov }
9026b3934efSOleg Nesterov 
903a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
9061da177e4SLinus Torvalds 
90783144186SRafael J. Wysocki 	new_flags &= ~PF_SUPERPRIV;
9081da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
90909a05394SRoland McGrath 	new_flags |= PF_STARTING;
9101da177e4SLinus Torvalds 	p->flags = new_flags;
9112e131895SRafael J. Wysocki 	clear_freeze_flag(p);
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
91417da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
9151da177e4SLinus Torvalds {
9161da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
9171da177e4SLinus Torvalds 
918b488893aSPavel Emelyanov 	return task_pid_vnr(current);
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
921a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p)
92223f78d4aSIngo Molnar {
9231d615482SThomas Gleixner 	raw_spin_lock_init(&p->pi_lock);
924e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES
9251d615482SThomas Gleixner 	plist_head_init_raw(&p->pi_waiters, &p->pi_lock);
92623f78d4aSIngo Molnar 	p->pi_blocked_on = NULL;
92723f78d4aSIngo Molnar #endif
92823f78d4aSIngo Molnar }
92923f78d4aSIngo Molnar 
930cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER
931cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
932cf475ad2SBalbir Singh {
933cf475ad2SBalbir Singh 	mm->owner = p;
934cf475ad2SBalbir Singh }
935cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */
936cf475ad2SBalbir Singh 
9371da177e4SLinus Torvalds /*
938f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a single task.
939f06febc9SFrank Mayhar  */
940f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk)
941f06febc9SFrank Mayhar {
942f06febc9SFrank Mayhar 	tsk->cputime_expires.prof_exp = cputime_zero;
943f06febc9SFrank Mayhar 	tsk->cputime_expires.virt_exp = cputime_zero;
944f06febc9SFrank Mayhar 	tsk->cputime_expires.sched_exp = 0;
945f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[0]);
946f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[1]);
947f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[2]);
948f06febc9SFrank Mayhar }
949f06febc9SFrank Mayhar 
950f06febc9SFrank Mayhar /*
9511da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
9521da177e4SLinus Torvalds  * but does not actually start it yet.
9531da177e4SLinus Torvalds  *
9541da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
9551da177e4SLinus Torvalds  * parts of the process environment (as per the clone
9561da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
9571da177e4SLinus Torvalds  */
95836c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags,
9591da177e4SLinus Torvalds 					unsigned long stack_start,
9601da177e4SLinus Torvalds 					struct pt_regs *regs,
9611da177e4SLinus Torvalds 					unsigned long stack_size,
9621da177e4SLinus Torvalds 					int __user *child_tidptr,
96309a05394SRoland McGrath 					struct pid *pid,
96409a05394SRoland McGrath 					int trace)
9651da177e4SLinus Torvalds {
9661da177e4SLinus Torvalds 	int retval;
967a24efe62SMariusz Kozlowski 	struct task_struct *p;
968b4f48b63SPaul Menage 	int cgroup_callbacks_done = 0;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
9711da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/*
9741da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
9751da177e4SLinus Torvalds 	 * can only be started up within the thread group.
9761da177e4SLinus Torvalds 	 */
9771da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
9781da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	/*
9811da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
9821da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
9831da177e4SLinus Torvalds 	 * for various simplifications in other code.
9841da177e4SLinus Torvalds 	 */
9851da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
9861da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9871da177e4SLinus Torvalds 
988123be07bSSukadev Bhattiprolu 	/*
989123be07bSSukadev Bhattiprolu 	 * Siblings of global init remain as zombies on exit since they are
990123be07bSSukadev Bhattiprolu 	 * not reaped by their parent (swapper). To solve this and to avoid
991123be07bSSukadev Bhattiprolu 	 * multi-rooted process trees, prevent global and container-inits
992123be07bSSukadev Bhattiprolu 	 * from creating siblings.
993123be07bSSukadev Bhattiprolu 	 */
994123be07bSSukadev Bhattiprolu 	if ((clone_flags & CLONE_PARENT) &&
995123be07bSSukadev Bhattiprolu 				current->signal->flags & SIGNAL_UNKILLABLE)
996123be07bSSukadev Bhattiprolu 		return ERR_PTR(-EINVAL);
997123be07bSSukadev Bhattiprolu 
9981da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
9991da177e4SLinus Torvalds 	if (retval)
10001da177e4SLinus Torvalds 		goto fork_out;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	retval = -ENOMEM;
10031da177e4SLinus Torvalds 	p = dup_task_struct(current);
10041da177e4SLinus Torvalds 	if (!p)
10051da177e4SLinus Torvalds 		goto fork_out;
10061da177e4SLinus Torvalds 
1007f7e8b616SSteven Rostedt 	ftrace_graph_init_task(p);
1008f7e8b616SSteven Rostedt 
1009bea493a0SPeter Zijlstra 	rt_mutex_init_task(p);
1010bea493a0SPeter Zijlstra 
1011d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING
1012de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1013de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1014de30a2b3SIngo Molnar #endif
10151da177e4SLinus Torvalds 	retval = -EAGAIN;
10163b11a1deSDavid Howells 	if (atomic_read(&p->real_cred->user->processes) >=
101778d7d407SJiri Slaby 			task_rlimit(p, RLIMIT_NPROC)) {
10181da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
101918b6e041SSerge Hallyn 		    p->real_cred->user != INIT_USER)
10201da177e4SLinus Torvalds 			goto bad_fork_free;
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds 
1023f1752eecSDavid Howells 	retval = copy_creds(p, clone_flags);
1024f1752eecSDavid Howells 	if (retval < 0)
1025f1752eecSDavid Howells 		goto bad_fork_free;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	/*
10281da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
10291da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
10301da177e4SLinus Torvalds 	 * to stop root fork bombs.
10311da177e4SLinus Torvalds 	 */
103204ec93feSLi Zefan 	retval = -EAGAIN;
10331da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
10341da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
10351da177e4SLinus Torvalds 
1036a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
10371da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	p->did_exec = 0;
1040ca74e92bSShailabh Nagar 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
10411da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
10421da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
10431da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
1044f41d911fSPaul E. McKenney 	rcu_copy_process(p);
10451da177e4SLinus Torvalds 	p->vfork_done = NULL;
10461da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	init_sigpending(&p->pending);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	p->utime = cputime_zero;
10511da177e4SLinus Torvalds 	p->stime = cputime_zero;
10529ac52315SLaurent Vivier 	p->gtime = cputime_zero;
1053c66f08beSMichael Neuling 	p->utimescaled = cputime_zero;
1054c66f08beSMichael Neuling 	p->stimescaled = cputime_zero;
1055d99ca3b9SHidetoshi Seto #ifndef CONFIG_VIRT_CPU_ACCOUNTING
105673a2bcb0SPeter Zijlstra 	p->prev_utime = cputime_zero;
10579301899bSBalbir Singh 	p->prev_stime = cputime_zero;
1058d99ca3b9SHidetoshi Seto #endif
1059172ba844SBalbir Singh 
10606976675dSArjan van de Ven 	p->default_timer_slack_ns = current->timer_slack_ns;
10616976675dSArjan van de Ven 
10625995477aSAndrea Righi 	task_io_accounting_init(&p->ioac);
10631da177e4SLinus Torvalds 	acct_clear_integrals(p);
10641da177e4SLinus Torvalds 
1065f06febc9SFrank Mayhar 	posix_cpu_timers_init(p);
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	p->lock_depth = -1;		/* -1 = no lock */
10681da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
1069924b42d5STomas Janousek 	p->real_start_time = p->start_time;
1070924b42d5STomas Janousek 	monotonic_to_bootbased(&p->real_start_time);
10711da177e4SLinus Torvalds 	p->io_context = NULL;
10721da177e4SLinus Torvalds 	p->audit_context = NULL;
1073b4f48b63SPaul Menage 	cgroup_fork(p);
10741da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1075846a16bfSLee Schermerhorn 	p->mempolicy = mpol_dup(p->mempolicy);
10761da177e4SLinus Torvalds  	if (IS_ERR(p->mempolicy)) {
10771da177e4SLinus Torvalds  		retval = PTR_ERR(p->mempolicy);
10781da177e4SLinus Torvalds  		p->mempolicy = NULL;
1079b4f48b63SPaul Menage  		goto bad_fork_cleanup_cgroup;
10801da177e4SLinus Torvalds  	}
1081c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(p);
10821da177e4SLinus Torvalds #endif
1083de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1084de30a2b3SIngo Molnar 	p->irq_events = 0;
1085b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1086b36e4758SRussell King 	p->hardirqs_enabled = 1;
1087b36e4758SRussell King #else
1088de30a2b3SIngo Molnar 	p->hardirqs_enabled = 0;
1089b36e4758SRussell King #endif
1090de30a2b3SIngo Molnar 	p->hardirq_enable_ip = 0;
1091de30a2b3SIngo Molnar 	p->hardirq_enable_event = 0;
1092de30a2b3SIngo Molnar 	p->hardirq_disable_ip = _THIS_IP_;
1093de30a2b3SIngo Molnar 	p->hardirq_disable_event = 0;
1094de30a2b3SIngo Molnar 	p->softirqs_enabled = 1;
1095de30a2b3SIngo Molnar 	p->softirq_enable_ip = _THIS_IP_;
1096de30a2b3SIngo Molnar 	p->softirq_enable_event = 0;
1097de30a2b3SIngo Molnar 	p->softirq_disable_ip = 0;
1098de30a2b3SIngo Molnar 	p->softirq_disable_event = 0;
1099de30a2b3SIngo Molnar 	p->hardirq_context = 0;
1100de30a2b3SIngo Molnar 	p->softirq_context = 0;
1101de30a2b3SIngo Molnar #endif
1102fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1103fbb9ce95SIngo Molnar 	p->lockdep_depth = 0; /* no locks held yet */
1104fbb9ce95SIngo Molnar 	p->curr_chain_key = 0;
1105fbb9ce95SIngo Molnar 	p->lockdep_recursion = 0;
1106fbb9ce95SIngo Molnar #endif
11071da177e4SLinus Torvalds 
1108408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1109408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1110408894eeSIngo Molnar #endif
1111569b846dSKAMEZAWA Hiroyuki #ifdef CONFIG_CGROUP_MEM_RES_CTLR
1112569b846dSKAMEZAWA Hiroyuki 	p->memcg_batch.do_batch = 0;
1113569b846dSKAMEZAWA Hiroyuki 	p->memcg_batch.memcg = NULL;
1114569b846dSKAMEZAWA Hiroyuki #endif
11150f481406SMarkus Metzger 
11160f481406SMarkus Metzger 	p->bts = NULL;
1117408894eeSIngo Molnar 
1118d899bf7bSStefani Seibold 	p->stack_start = stack_start;
1119d899bf7bSStefani Seibold 
11203c90e6e9SSrivatsa Vaddagiri 	/* Perform scheduler related setup. Assign this task to a CPU. */
11213c90e6e9SSrivatsa Vaddagiri 	sched_fork(p, clone_flags);
11226ab423e0SPeter Zijlstra 
1123cdd6c482SIngo Molnar 	retval = perf_event_init_task(p);
11246ab423e0SPeter Zijlstra 	if (retval)
11256ab423e0SPeter Zijlstra 		goto bad_fork_cleanup_policy;
11263c90e6e9SSrivatsa Vaddagiri 
11271da177e4SLinus Torvalds 	if ((retval = audit_alloc(p)))
1128f1752eecSDavid Howells 		goto bad_fork_cleanup_policy;
11291da177e4SLinus Torvalds 	/* copy all the process information */
11301da177e4SLinus Torvalds 	if ((retval = copy_semundo(clone_flags, p)))
11311da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
11321da177e4SLinus Torvalds 	if ((retval = copy_files(clone_flags, p)))
11331da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
11341da177e4SLinus Torvalds 	if ((retval = copy_fs(clone_flags, p)))
11351da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
11361da177e4SLinus Torvalds 	if ((retval = copy_sighand(clone_flags, p)))
11371da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
11381da177e4SLinus Torvalds 	if ((retval = copy_signal(clone_flags, p)))
11391da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
11401da177e4SLinus Torvalds 	if ((retval = copy_mm(clone_flags, p)))
11411da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
1142ab516013SSerge E. Hallyn 	if ((retval = copy_namespaces(clone_flags, p)))
1143d84f4f99SDavid Howells 		goto bad_fork_cleanup_mm;
1144fadad878SJens Axboe 	if ((retval = copy_io(clone_flags, p)))
1145fd0928dfSJens Axboe 		goto bad_fork_cleanup_namespaces;
11466f2c55b8SAlexey Dobriyan 	retval = copy_thread(clone_flags, stack_start, stack_size, p, regs);
11471da177e4SLinus Torvalds 	if (retval)
1148fd0928dfSJens Axboe 		goto bad_fork_cleanup_io;
11491da177e4SLinus Torvalds 
1150425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid) {
1151425fb2b4SPavel Emelyanov 		retval = -ENOMEM;
115261bce0f1SEric W. Biederman 		pid = alloc_pid(p->nsproxy->pid_ns);
1153425fb2b4SPavel Emelyanov 		if (!pid)
1154fd0928dfSJens Axboe 			goto bad_fork_cleanup_io;
11556f4e6433SPavel Emelyanov 
11566f4e6433SPavel Emelyanov 		if (clone_flags & CLONE_NEWPID) {
115761bce0f1SEric W. Biederman 			retval = pid_ns_prepare_proc(p->nsproxy->pid_ns);
11586f4e6433SPavel Emelyanov 			if (retval < 0)
11596f4e6433SPavel Emelyanov 				goto bad_fork_free_pid;
11606f4e6433SPavel Emelyanov 		}
1161425fb2b4SPavel Emelyanov 	}
1162425fb2b4SPavel Emelyanov 
1163425fb2b4SPavel Emelyanov 	p->pid = pid_nr(pid);
1164425fb2b4SPavel Emelyanov 	p->tgid = p->pid;
1165425fb2b4SPavel Emelyanov 	if (clone_flags & CLONE_THREAD)
1166425fb2b4SPavel Emelyanov 		p->tgid = current->tgid;
1167425fb2b4SPavel Emelyanov 
1168e885dcdeSSerge E. Hallyn 	if (current->nsproxy != p->nsproxy) {
1169e885dcdeSSerge E. Hallyn 		retval = ns_cgroup_clone(p, pid);
1170e885dcdeSSerge E. Hallyn 		if (retval)
1171f7e8b616SSteven Rostedt 			goto bad_fork_free_pid;
1172e885dcdeSSerge E. Hallyn 	}
1173e885dcdeSSerge E. Hallyn 
11741da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
11751da177e4SLinus Torvalds 	/*
11761da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
11771da177e4SLinus Torvalds 	 */
11781da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
117942b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
11808f17d3a5SIngo Molnar 	p->robust_list = NULL;
11818f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
11828f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
11838f17d3a5SIngo Molnar #endif
1184c87e2837SIngo Molnar 	INIT_LIST_HEAD(&p->pi_state_list);
1185c87e2837SIngo Molnar 	p->pi_state_cache = NULL;
118642b2dd0aSAlexey Dobriyan #endif
11871da177e4SLinus Torvalds 	/*
1188f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1189f9a3879aSGOTO Masanori 	 */
1190f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1191f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1192f9a3879aSGOTO Masanori 
1193f9a3879aSGOTO Masanori 	/*
11946580807dSOleg Nesterov 	 * Syscall tracing and stepping should be turned off in the
11956580807dSOleg Nesterov 	 * child regardless of CLONE_PTRACE.
11961da177e4SLinus Torvalds 	 */
11976580807dSOleg Nesterov 	user_disable_single_step(p);
11981da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1199ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1200ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1201ed75e8d5SLaurent Vivier #endif
12029745512cSArjan van de Ven 	clear_all_latency_tracing(p);
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
12051da177e4SLinus Torvalds 	p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
12061da177e4SLinus Torvalds 	p->pdeath_signal = 0;
12071da177e4SLinus Torvalds 	p->exit_state = 0;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	/*
12101da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
12111da177e4SLinus Torvalds 	 * We dont wake it up yet.
12121da177e4SLinus Torvalds 	 */
12131da177e4SLinus Torvalds 	p->group_leader = p;
121447e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
12151da177e4SLinus Torvalds 
1216b4f48b63SPaul Menage 	/* Now that the task is set up, run cgroup callbacks if
1217b4f48b63SPaul Menage 	 * necessary. We need to run them before the task is visible
1218b4f48b63SPaul Menage 	 * on the tasklist. */
1219b4f48b63SPaul Menage 	cgroup_fork_callbacks(p);
1220b4f48b63SPaul Menage 	cgroup_callbacks_done = 1;
1221b4f48b63SPaul Menage 
12221da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
12231da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
12262d5516cbSOleg Nesterov 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
12271da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
12282d5516cbSOleg Nesterov 		p->parent_exec_id = current->parent_exec_id;
12292d5516cbSOleg Nesterov 	} else {
12301da177e4SLinus Torvalds 		p->real_parent = current;
12312d5516cbSOleg Nesterov 		p->parent_exec_id = current->self_exec_id;
12322d5516cbSOleg Nesterov 	}
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
12354a2c7a78SOleg Nesterov 
12364a2c7a78SOleg Nesterov 	/*
12374a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
12384a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
12394a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
12404a2c7a78SOleg Nesterov 	 * it's process group.
12414a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
12424a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
12434a2c7a78SOleg Nesterov  	 */
12444a2c7a78SOleg Nesterov 	recalc_sigpending();
12454a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
12464a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
12474a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
12484a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
1249f7e8b616SSteven Rostedt 		goto bad_fork_free_pid;
12504a2c7a78SOleg Nesterov 	}
12514a2c7a78SOleg Nesterov 
12523f17da69SOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
12534ab6c083SOleg Nesterov 		atomic_inc(&current->signal->count);
12544ab6c083SOleg Nesterov 		atomic_inc(&current->signal->live);
12551da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
125647e65328SOleg Nesterov 		list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds 
125973b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
126009a05394SRoland McGrath 		tracehook_finish_clone(p, clone_flags, trace);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
12635cd17569SEric W. Biederman 			if (clone_flags & CLONE_NEWPID)
126430e49c26SPavel Emelyanov 				p->nsproxy->pid_ns->child_reaper = p;
12655cd17569SEric W. Biederman 
1266fea9d175SOleg Nesterov 			p->signal->leader_pid = pid;
12679c9f4dedSAlan Cox 			tty_kref_put(p->signal->tty);
12689c9f4dedSAlan Cox 			p->signal->tty = tty_kref_get(current->signal->tty);
12695cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
12705cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_SID, task_session(current));
12719cd80bbbSOleg Nesterov 			list_add_tail(&p->sibling, &p->real_parent->children);
12725e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
12731da177e4SLinus Torvalds 			__get_cpu_var(process_counts)++;
12741da177e4SLinus Torvalds 		}
127585868995SSukadev Bhattiprolu 		attach_pid(p, PIDTYPE_PID, pid);
12761da177e4SLinus Torvalds 		nr_threads++;
127773b9ebfeSOleg Nesterov 	}
127873b9ebfeSOleg Nesterov 
12791da177e4SLinus Torvalds 	total_forks++;
12803f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
12811da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1282c13cf856SAndrew Morton 	proc_fork_connector(p);
1283817929ecSPaul Menage 	cgroup_post_fork(p);
1284cdd6c482SIngo Molnar 	perf_event_fork(p);
12851da177e4SLinus Torvalds 	return p;
12861da177e4SLinus Torvalds 
1287425fb2b4SPavel Emelyanov bad_fork_free_pid:
1288425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid)
1289425fb2b4SPavel Emelyanov 		free_pid(pid);
1290fd0928dfSJens Axboe bad_fork_cleanup_io:
1291b69f2292SLouis Rilling 	if (p->io_context)
1292b69f2292SLouis Rilling 		exit_io_context(p);
1293ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces:
1294444f378bSLinus Torvalds 	exit_task_namespaces(p);
12951da177e4SLinus Torvalds bad_fork_cleanup_mm:
12961da177e4SLinus Torvalds 	if (p->mm)
12971da177e4SLinus Torvalds 		mmput(p->mm);
12981da177e4SLinus Torvalds bad_fork_cleanup_signal:
12994ab6c083SOleg Nesterov 	if (!(clone_flags & CLONE_THREAD))
13004ab6c083SOleg Nesterov 		__cleanup_signal(p->signal);
13011da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1302a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
13031da177e4SLinus Torvalds bad_fork_cleanup_fs:
13041da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
13051da177e4SLinus Torvalds bad_fork_cleanup_files:
13061da177e4SLinus Torvalds 	exit_files(p); /* blocking */
13071da177e4SLinus Torvalds bad_fork_cleanup_semundo:
13081da177e4SLinus Torvalds 	exit_sem(p);
13091da177e4SLinus Torvalds bad_fork_cleanup_audit:
13101da177e4SLinus Torvalds 	audit_free(p);
13111da177e4SLinus Torvalds bad_fork_cleanup_policy:
1312cdd6c482SIngo Molnar 	perf_event_free_task(p);
13131da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1314f0be3d32SLee Schermerhorn 	mpol_put(p->mempolicy);
1315b4f48b63SPaul Menage bad_fork_cleanup_cgroup:
13161da177e4SLinus Torvalds #endif
1317b4f48b63SPaul Menage 	cgroup_exit(p, cgroup_callbacks_done);
131835df17c5SShailabh Nagar 	delayacct_tsk_free(p);
1319a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
13201da177e4SLinus Torvalds bad_fork_cleanup_count:
1321d84f4f99SDavid Howells 	atomic_dec(&p->cred->user->processes);
1322e0e81739SDavid Howells 	exit_creds(p);
13231da177e4SLinus Torvalds bad_fork_free:
13241da177e4SLinus Torvalds 	free_task(p);
1325fe7d37d1SOleg Nesterov fork_out:
1326fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
13296b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
13301da177e4SLinus Torvalds {
13311da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
13321da177e4SLinus Torvalds 	return regs;
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
13359abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu)
13361da177e4SLinus Torvalds {
133736c8b586SIngo Molnar 	struct task_struct *task;
13381da177e4SLinus Torvalds 	struct pt_regs regs;
13391da177e4SLinus Torvalds 
134030e49c26SPavel Emelyanov 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL,
134109a05394SRoland McGrath 			    &init_struct_pid, 0);
1342753ca4f3SAkinobu Mita 	if (!IS_ERR(task))
13431da177e4SLinus Torvalds 		init_idle(task, cpu);
134473b9ebfeSOleg Nesterov 
13451da177e4SLinus Torvalds 	return task;
13461da177e4SLinus Torvalds }
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds /*
13491da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
13501da177e4SLinus Torvalds  *
13511da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
13521da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
13531da177e4SLinus Torvalds  */
13541da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
13551da177e4SLinus Torvalds 	      unsigned long stack_start,
13561da177e4SLinus Torvalds 	      struct pt_regs *regs,
13571da177e4SLinus Torvalds 	      unsigned long stack_size,
13581da177e4SLinus Torvalds 	      int __user *parent_tidptr,
13591da177e4SLinus Torvalds 	      int __user *child_tidptr)
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds 	struct task_struct *p;
13621da177e4SLinus Torvalds 	int trace = 0;
136392476d7fSEric W. Biederman 	long nr;
13641da177e4SLinus Torvalds 
1365bdff746aSAndrew Morton 	/*
136618b6e041SSerge Hallyn 	 * Do some preliminary argument and permissions checking before we
136718b6e041SSerge Hallyn 	 * actually start allocating stuff
136818b6e041SSerge Hallyn 	 */
136918b6e041SSerge Hallyn 	if (clone_flags & CLONE_NEWUSER) {
137018b6e041SSerge Hallyn 		if (clone_flags & CLONE_THREAD)
137118b6e041SSerge Hallyn 			return -EINVAL;
137218b6e041SSerge Hallyn 		/* hopefully this check will go away when userns support is
137318b6e041SSerge Hallyn 		 * complete
137418b6e041SSerge Hallyn 		 */
13757657d904SSerge E. Hallyn 		if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) ||
13767657d904SSerge E. Hallyn 				!capable(CAP_SETGID))
137718b6e041SSerge Hallyn 			return -EPERM;
137818b6e041SSerge Hallyn 	}
137918b6e041SSerge Hallyn 
138018b6e041SSerge Hallyn 	/*
1381bdff746aSAndrew Morton 	 * We hope to recycle these flags after 2.6.26
1382bdff746aSAndrew Morton 	 */
1383bdff746aSAndrew Morton 	if (unlikely(clone_flags & CLONE_STOPPED)) {
1384bdff746aSAndrew Morton 		static int __read_mostly count = 100;
1385bdff746aSAndrew Morton 
1386bdff746aSAndrew Morton 		if (count > 0 && printk_ratelimit()) {
1387bdff746aSAndrew Morton 			char comm[TASK_COMM_LEN];
1388bdff746aSAndrew Morton 
1389bdff746aSAndrew Morton 			count--;
1390bdff746aSAndrew Morton 			printk(KERN_INFO "fork(): process `%s' used deprecated "
1391bdff746aSAndrew Morton 					"clone flags 0x%lx\n",
1392bdff746aSAndrew Morton 				get_task_comm(comm, current),
1393bdff746aSAndrew Morton 				clone_flags & CLONE_STOPPED);
1394bdff746aSAndrew Morton 		}
1395bdff746aSAndrew Morton 	}
1396bdff746aSAndrew Morton 
139709a05394SRoland McGrath 	/*
139809a05394SRoland McGrath 	 * When called from kernel_thread, don't do user tracing stuff.
139909a05394SRoland McGrath 	 */
140009a05394SRoland McGrath 	if (likely(user_mode(regs)))
140109a05394SRoland McGrath 		trace = tracehook_prepare_clone(clone_flags);
14021da177e4SLinus Torvalds 
1403a6f5e063SSukadev Bhattiprolu 	p = copy_process(clone_flags, stack_start, regs, stack_size,
140409a05394SRoland McGrath 			 child_tidptr, NULL, trace);
14051da177e4SLinus Torvalds 	/*
14061da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
14071da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
14081da177e4SLinus Torvalds 	 */
14091da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
14101da177e4SLinus Torvalds 		struct completion vfork;
14111da177e4SLinus Torvalds 
14120a16b607SMathieu Desnoyers 		trace_sched_process_fork(current, p);
14130a16b607SMathieu Desnoyers 
14146c5f3e7bSPavel Emelyanov 		nr = task_pid_vnr(p);
141530e49c26SPavel Emelyanov 
141630e49c26SPavel Emelyanov 		if (clone_flags & CLONE_PARENT_SETTID)
141730e49c26SPavel Emelyanov 			put_user(nr, parent_tidptr);
1418a6f5e063SSukadev Bhattiprolu 
14191da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
14201da177e4SLinus Torvalds 			p->vfork_done = &vfork;
14211da177e4SLinus Torvalds 			init_completion(&vfork);
14221da177e4SLinus Torvalds 		}
14231da177e4SLinus Torvalds 
1424a64e6494SAl Viro 		audit_finish_fork(p);
1425087eb437SOleg Nesterov 		tracehook_report_clone(regs, clone_flags, nr, p);
142609a05394SRoland McGrath 
142709a05394SRoland McGrath 		/*
142809a05394SRoland McGrath 		 * We set PF_STARTING at creation in case tracing wants to
142909a05394SRoland McGrath 		 * use this to distinguish a fully live task from one that
143009a05394SRoland McGrath 		 * hasn't gotten to tracehook_report_clone() yet.  Now we
143109a05394SRoland McGrath 		 * clear it and set the child going.
143209a05394SRoland McGrath 		 */
143309a05394SRoland McGrath 		p->flags &= ~PF_STARTING;
143409a05394SRoland McGrath 
143509a05394SRoland McGrath 		if (unlikely(clone_flags & CLONE_STOPPED)) {
14361da177e4SLinus Torvalds 			/*
14371da177e4SLinus Torvalds 			 * We'll start up with an immediate SIGSTOP.
14381da177e4SLinus Torvalds 			 */
14391da177e4SLinus Torvalds 			sigaddset(&p->pending.signal, SIGSTOP);
14401da177e4SLinus Torvalds 			set_tsk_thread_flag(p, TIF_SIGPENDING);
1441d9ae90acSOleg Nesterov 			__set_task_state(p, TASK_STOPPED);
144209a05394SRoland McGrath 		} else {
144309a05394SRoland McGrath 			wake_up_new_task(p, clone_flags);
14441da177e4SLinus Torvalds 		}
14451da177e4SLinus Torvalds 
144609a05394SRoland McGrath 		tracehook_report_clone_complete(trace, regs,
144709a05394SRoland McGrath 						clone_flags, nr, p);
144809a05394SRoland McGrath 
14491da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
1450ba96a0c8SRafael J. Wysocki 			freezer_do_not_count();
14511da177e4SLinus Torvalds 			wait_for_completion(&vfork);
1452ba96a0c8SRafael J. Wysocki 			freezer_count();
1453daded34bSRoland McGrath 			tracehook_report_vfork_done(p, nr);
14549f59ce5dSChuck Ebbert 		}
14551da177e4SLinus Torvalds 	} else {
145692476d7fSEric W. Biederman 		nr = PTR_ERR(p);
14571da177e4SLinus Torvalds 	}
145892476d7fSEric W. Biederman 	return nr;
14591da177e4SLinus Torvalds }
14601da177e4SLinus Torvalds 
14615fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN
14625fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0
14635fd63b30SRavikiran G Thirumalai #endif
14645fd63b30SRavikiran G Thirumalai 
146551cc5068SAlexey Dobriyan static void sighand_ctor(void *data)
1466aa1757f9SOleg Nesterov {
1467aa1757f9SOleg Nesterov 	struct sighand_struct *sighand = data;
1468aa1757f9SOleg Nesterov 
1469aa1757f9SOleg Nesterov 	spin_lock_init(&sighand->siglock);
1470b8fceee1SDavide Libenzi 	init_waitqueue_head(&sighand->signalfd_wqh);
1471fba2afaaSDavide Libenzi }
1472aa1757f9SOleg Nesterov 
14731da177e4SLinus Torvalds void __init proc_caches_init(void)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
14761da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
14772dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
14782dff4405SVegard Nossum 			SLAB_NOTRACK, sighand_ctor);
14791da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
14801da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
14812dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
14821da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
14831da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
14842dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
14851da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
14861da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
14872dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
14881da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
14895fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
14902dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
149133e5d769SDavid Howells 	vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC);
14928feae131SDavid Howells 	mmap_init();
14931da177e4SLinus Torvalds }
1494cf2e340fSJANAK DESAI 
1495cf2e340fSJANAK DESAI /*
1496cf2e340fSJANAK DESAI  * Check constraints on flags passed to the unshare system call and
1497cf2e340fSJANAK DESAI  * force unsharing of additional process context as appropriate.
1498cf2e340fSJANAK DESAI  */
1499a39bc516SAlexey Dobriyan static void check_unshare_flags(unsigned long *flags_ptr)
1500cf2e340fSJANAK DESAI {
1501cf2e340fSJANAK DESAI 	/*
1502cf2e340fSJANAK DESAI 	 * If unsharing a thread from a thread group, must also
1503cf2e340fSJANAK DESAI 	 * unshare vm.
1504cf2e340fSJANAK DESAI 	 */
1505cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_THREAD)
1506cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_VM;
1507cf2e340fSJANAK DESAI 
1508cf2e340fSJANAK DESAI 	/*
1509cf2e340fSJANAK DESAI 	 * If unsharing vm, must also unshare signal handlers.
1510cf2e340fSJANAK DESAI 	 */
1511cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_VM)
1512cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_SIGHAND;
1513cf2e340fSJANAK DESAI 
1514cf2e340fSJANAK DESAI 	/*
1515cf2e340fSJANAK DESAI 	 * If unsharing signal handlers and the task was created
1516cf2e340fSJANAK DESAI 	 * using CLONE_THREAD, then must unshare the thread
1517cf2e340fSJANAK DESAI 	 */
1518cf2e340fSJANAK DESAI 	if ((*flags_ptr & CLONE_SIGHAND) &&
1519cf2e340fSJANAK DESAI 	    (atomic_read(&current->signal->count) > 1))
1520cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_THREAD;
1521cf2e340fSJANAK DESAI 
1522cf2e340fSJANAK DESAI 	/*
1523cf2e340fSJANAK DESAI 	 * If unsharing namespace, must also unshare filesystem information.
1524cf2e340fSJANAK DESAI 	 */
1525cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_NEWNS)
1526cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_FS;
1527cf2e340fSJANAK DESAI }
1528cf2e340fSJANAK DESAI 
1529cf2e340fSJANAK DESAI /*
1530cf2e340fSJANAK DESAI  * Unsharing of tasks created with CLONE_THREAD is not supported yet
1531cf2e340fSJANAK DESAI  */
1532cf2e340fSJANAK DESAI static int unshare_thread(unsigned long unshare_flags)
1533cf2e340fSJANAK DESAI {
1534cf2e340fSJANAK DESAI 	if (unshare_flags & CLONE_THREAD)
1535cf2e340fSJANAK DESAI 		return -EINVAL;
1536cf2e340fSJANAK DESAI 
1537cf2e340fSJANAK DESAI 	return 0;
1538cf2e340fSJANAK DESAI }
1539cf2e340fSJANAK DESAI 
1540cf2e340fSJANAK DESAI /*
154199d1419dSJANAK DESAI  * Unshare the filesystem structure if it is being shared
1542cf2e340fSJANAK DESAI  */
1543cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1544cf2e340fSJANAK DESAI {
1545cf2e340fSJANAK DESAI 	struct fs_struct *fs = current->fs;
1546cf2e340fSJANAK DESAI 
1547498052bbSAl Viro 	if (!(unshare_flags & CLONE_FS) || !fs)
1548498052bbSAl Viro 		return 0;
1549498052bbSAl Viro 
1550498052bbSAl Viro 	/* don't need lock here; in the worst case we'll do useless copy */
1551498052bbSAl Viro 	if (fs->users == 1)
1552498052bbSAl Viro 		return 0;
1553498052bbSAl Viro 
1554498052bbSAl Viro 	*new_fsp = copy_fs_struct(fs);
155599d1419dSJANAK DESAI 	if (!*new_fsp)
155699d1419dSJANAK DESAI 		return -ENOMEM;
1557cf2e340fSJANAK DESAI 
1558cf2e340fSJANAK DESAI 	return 0;
1559cf2e340fSJANAK DESAI }
1560cf2e340fSJANAK DESAI 
1561cf2e340fSJANAK DESAI /*
1562dae3c5a0SOleg Nesterov  * Unsharing of sighand is not supported yet
1563cf2e340fSJANAK DESAI  */
1564cf2e340fSJANAK DESAI static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
1565cf2e340fSJANAK DESAI {
1566cf2e340fSJANAK DESAI 	struct sighand_struct *sigh = current->sighand;
1567cf2e340fSJANAK DESAI 
1568dae3c5a0SOleg Nesterov 	if ((unshare_flags & CLONE_SIGHAND) && atomic_read(&sigh->count) > 1)
1569cf2e340fSJANAK DESAI 		return -EINVAL;
1570cf2e340fSJANAK DESAI 	else
1571cf2e340fSJANAK DESAI 		return 0;
1572cf2e340fSJANAK DESAI }
1573cf2e340fSJANAK DESAI 
1574cf2e340fSJANAK DESAI /*
1575a0a7ec30SJANAK DESAI  * Unshare vm if it is being shared
1576cf2e340fSJANAK DESAI  */
1577cf2e340fSJANAK DESAI static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp)
1578cf2e340fSJANAK DESAI {
1579cf2e340fSJANAK DESAI 	struct mm_struct *mm = current->mm;
1580cf2e340fSJANAK DESAI 
1581cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_VM) &&
1582a0a7ec30SJANAK DESAI 	    (mm && atomic_read(&mm->mm_users) > 1)) {
15832d61b867SOleg Nesterov 		return -EINVAL;
1584a0a7ec30SJANAK DESAI 	}
1585cf2e340fSJANAK DESAI 
1586cf2e340fSJANAK DESAI 	return 0;
1587cf2e340fSJANAK DESAI }
1588cf2e340fSJANAK DESAI 
1589cf2e340fSJANAK DESAI /*
1590a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1591cf2e340fSJANAK DESAI  */
1592cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1593cf2e340fSJANAK DESAI {
1594cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1595a016f338SJANAK DESAI 	int error = 0;
1596cf2e340fSJANAK DESAI 
1597cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1598a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1599a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1600a016f338SJANAK DESAI 		if (!*new_fdp)
1601a016f338SJANAK DESAI 			return error;
1602a016f338SJANAK DESAI 	}
1603cf2e340fSJANAK DESAI 
1604cf2e340fSJANAK DESAI 	return 0;
1605cf2e340fSJANAK DESAI }
1606cf2e340fSJANAK DESAI 
1607cf2e340fSJANAK DESAI /*
1608cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1609cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1610cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1611cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1612cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1613cf2e340fSJANAK DESAI  * task_struct.
1614cf2e340fSJANAK DESAI  */
16156559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1616cf2e340fSJANAK DESAI {
1617cf2e340fSJANAK DESAI 	int err = 0;
1618cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1619dae3c5a0SOleg Nesterov 	struct sighand_struct *new_sigh = NULL;
1620cf2e340fSJANAK DESAI 	struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
1621cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1622cf7b708cSPavel Emelyanov 	struct nsproxy *new_nsproxy = NULL;
16239edff4abSManfred Spraul 	int do_sysvsem = 0;
1624cf2e340fSJANAK DESAI 
1625cf2e340fSJANAK DESAI 	check_unshare_flags(&unshare_flags);
1626cf2e340fSJANAK DESAI 
162706f9d4f9SEric W. Biederman 	/* Return -EINVAL for all unsupported flags */
162806f9d4f9SEric W. Biederman 	err = -EINVAL;
162906f9d4f9SEric W. Biederman 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
163025b21cb2SKirill Korotaev 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
163118b6e041SSerge Hallyn 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET))
163206f9d4f9SEric W. Biederman 		goto bad_unshare_out;
163306f9d4f9SEric W. Biederman 
16346013f67fSManfred Spraul 	/*
16356013f67fSManfred Spraul 	 * CLONE_NEWIPC must also detach from the undolist: after switching
16366013f67fSManfred Spraul 	 * to a new ipc namespace, the semaphore arrays from the old
16376013f67fSManfred Spraul 	 * namespace are unreachable.
16386013f67fSManfred Spraul 	 */
16396013f67fSManfred Spraul 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
16409edff4abSManfred Spraul 		do_sysvsem = 1;
1641cf2e340fSJANAK DESAI 	if ((err = unshare_thread(unshare_flags)))
1642cf2e340fSJANAK DESAI 		goto bad_unshare_out;
1643cf2e340fSJANAK DESAI 	if ((err = unshare_fs(unshare_flags, &new_fs)))
1644cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_thread;
1645cf2e340fSJANAK DESAI 	if ((err = unshare_sighand(unshare_flags, &new_sigh)))
1646e3222c4eSBadari Pulavarty 		goto bad_unshare_cleanup_fs;
1647cf2e340fSJANAK DESAI 	if ((err = unshare_vm(unshare_flags, &new_mm)))
1648cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_sigh;
1649cf2e340fSJANAK DESAI 	if ((err = unshare_fd(unshare_flags, &new_fd)))
1650cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_vm;
1651e3222c4eSBadari Pulavarty 	if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
1652e3222c4eSBadari Pulavarty 			new_fs)))
16539edff4abSManfred Spraul 		goto bad_unshare_cleanup_fd;
1654cf2e340fSJANAK DESAI 
16559edff4abSManfred Spraul 	if (new_fs ||  new_mm || new_fd || do_sysvsem || new_nsproxy) {
16569edff4abSManfred Spraul 		if (do_sysvsem) {
16579edff4abSManfred Spraul 			/*
16589edff4abSManfred Spraul 			 * CLONE_SYSVSEM is equivalent to sys_exit().
16599edff4abSManfred Spraul 			 */
16609edff4abSManfred Spraul 			exit_sem(current);
16619edff4abSManfred Spraul 		}
1662ab516013SSerge E. Hallyn 
1663c0b2fc31SSerge Hallyn 		if (new_nsproxy) {
1664cf7b708cSPavel Emelyanov 			switch_task_namespaces(current, new_nsproxy);
1665cf7b708cSPavel Emelyanov 			new_nsproxy = NULL;
1666c0b2fc31SSerge Hallyn 		}
1667cf2e340fSJANAK DESAI 
1668cf7b708cSPavel Emelyanov 		task_lock(current);
1669cf7b708cSPavel Emelyanov 
1670cf2e340fSJANAK DESAI 		if (new_fs) {
1671cf2e340fSJANAK DESAI 			fs = current->fs;
1672498052bbSAl Viro 			write_lock(&fs->lock);
1673cf2e340fSJANAK DESAI 			current->fs = new_fs;
1674498052bbSAl Viro 			if (--fs->users)
1675498052bbSAl Viro 				new_fs = NULL;
1676498052bbSAl Viro 			else
1677cf2e340fSJANAK DESAI 				new_fs = fs;
1678498052bbSAl Viro 			write_unlock(&fs->lock);
1679cf2e340fSJANAK DESAI 		}
1680cf2e340fSJANAK DESAI 
1681cf2e340fSJANAK DESAI 		if (new_mm) {
1682cf2e340fSJANAK DESAI 			mm = current->mm;
1683cf2e340fSJANAK DESAI 			active_mm = current->active_mm;
1684cf2e340fSJANAK DESAI 			current->mm = new_mm;
1685cf2e340fSJANAK DESAI 			current->active_mm = new_mm;
1686cf2e340fSJANAK DESAI 			activate_mm(active_mm, new_mm);
1687cf2e340fSJANAK DESAI 			new_mm = mm;
1688cf2e340fSJANAK DESAI 		}
1689cf2e340fSJANAK DESAI 
1690cf2e340fSJANAK DESAI 		if (new_fd) {
1691cf2e340fSJANAK DESAI 			fd = current->files;
1692cf2e340fSJANAK DESAI 			current->files = new_fd;
1693cf2e340fSJANAK DESAI 			new_fd = fd;
1694cf2e340fSJANAK DESAI 		}
1695cf2e340fSJANAK DESAI 
1696cf2e340fSJANAK DESAI 		task_unlock(current);
1697cf2e340fSJANAK DESAI 	}
1698cf2e340fSJANAK DESAI 
1699c0b2fc31SSerge Hallyn 	if (new_nsproxy)
1700444f378bSLinus Torvalds 		put_nsproxy(new_nsproxy);
1701c0b2fc31SSerge Hallyn 
1702cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
1703cf2e340fSJANAK DESAI 	if (new_fd)
1704cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
1705cf2e340fSJANAK DESAI 
1706cf2e340fSJANAK DESAI bad_unshare_cleanup_vm:
1707cf2e340fSJANAK DESAI 	if (new_mm)
1708cf2e340fSJANAK DESAI 		mmput(new_mm);
1709cf2e340fSJANAK DESAI 
1710cf2e340fSJANAK DESAI bad_unshare_cleanup_sigh:
1711cf2e340fSJANAK DESAI 	if (new_sigh)
1712cf2e340fSJANAK DESAI 		if (atomic_dec_and_test(&new_sigh->count))
1713cf2e340fSJANAK DESAI 			kmem_cache_free(sighand_cachep, new_sigh);
1714cf2e340fSJANAK DESAI 
1715cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
1716cf2e340fSJANAK DESAI 	if (new_fs)
1717498052bbSAl Viro 		free_fs_struct(new_fs);
1718cf2e340fSJANAK DESAI 
1719cf2e340fSJANAK DESAI bad_unshare_cleanup_thread:
1720cf2e340fSJANAK DESAI bad_unshare_out:
1721cf2e340fSJANAK DESAI 	return err;
1722cf2e340fSJANAK DESAI }
17233b125388SAl Viro 
17243b125388SAl Viro /*
17253b125388SAl Viro  *	Helper to unshare the files of the current task.
17263b125388SAl Viro  *	We don't want to expose copy_files internals to
17273b125388SAl Viro  *	the exec layer of the kernel.
17283b125388SAl Viro  */
17293b125388SAl Viro 
17303b125388SAl Viro int unshare_files(struct files_struct **displaced)
17313b125388SAl Viro {
17323b125388SAl Viro 	struct task_struct *task = current;
173350704516SAl Viro 	struct files_struct *copy = NULL;
17343b125388SAl Viro 	int error;
17353b125388SAl Viro 
17363b125388SAl Viro 	error = unshare_fd(CLONE_FILES, &copy);
17373b125388SAl Viro 	if (error || !copy) {
17383b125388SAl Viro 		*displaced = NULL;
17393b125388SAl Viro 		return error;
17403b125388SAl Viro 	}
17413b125388SAl Viro 	*displaced = task->files;
17423b125388SAl Viro 	task_lock(task);
17433b125388SAl Viro 	task->files = copy;
17443b125388SAl Viro 	task_unlock(task);
17453b125388SAl Viro 	return 0;
17463b125388SAl Viro }
1747