xref: /openbmc/linux/kernel/fork.c (revision 6f2c55b843836d26528c56a0968689accaedbc67)
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>
206b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
211da177e4SLinus Torvalds #include <linux/personality.h>
221da177e4SLinus Torvalds #include <linux/mempolicy.h>
231da177e4SLinus Torvalds #include <linux/sem.h>
241da177e4SLinus Torvalds #include <linux/file.h>
259f3acc31SAl Viro #include <linux/fdtable.h>
26da9cbc87SJens Axboe #include <linux/iocontext.h>
271da177e4SLinus Torvalds #include <linux/key.h>
281da177e4SLinus Torvalds #include <linux/binfmts.h>
291da177e4SLinus Torvalds #include <linux/mman.h>
30cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
311da177e4SLinus Torvalds #include <linux/fs.h>
32ab516013SSerge E. Hallyn #include <linux/nsproxy.h>
33c59ede7bSRandy.Dunlap #include <linux/capability.h>
341da177e4SLinus Torvalds #include <linux/cpu.h>
35b4f48b63SPaul Menage #include <linux/cgroup.h>
361da177e4SLinus Torvalds #include <linux/security.h>
37a1e78772SMel Gorman #include <linux/hugetlb.h>
381da177e4SLinus Torvalds #include <linux/swap.h>
391da177e4SLinus Torvalds #include <linux/syscalls.h>
401da177e4SLinus Torvalds #include <linux/jiffies.h>
4109a05394SRoland McGrath #include <linux/tracehook.h>
421da177e4SLinus Torvalds #include <linux/futex.h>
438141c7f3SLinus Torvalds #include <linux/compat.h>
447c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h>
45ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
461da177e4SLinus Torvalds #include <linux/ptrace.h>
471da177e4SLinus Torvalds #include <linux/mount.h>
481da177e4SLinus Torvalds #include <linux/audit.h>
4978fb7466SPavel Emelianov #include <linux/memcontrol.h>
50f201ae23SFrederic Weisbecker #include <linux/ftrace.h>
511da177e4SLinus Torvalds #include <linux/profile.h>
521da177e4SLinus Torvalds #include <linux/rmap.h>
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>
630a16b607SMathieu Desnoyers #include <trace/sched.h>
647c9f8861SEric Sandeen #include <linux/magic.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include <asm/pgtable.h>
671da177e4SLinus Torvalds #include <asm/pgalloc.h>
681da177e4SLinus Torvalds #include <asm/uaccess.h>
691da177e4SLinus Torvalds #include <asm/mmu_context.h>
701da177e4SLinus Torvalds #include <asm/cacheflush.h>
711da177e4SLinus Torvalds #include <asm/tlbflush.h>
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
771da177e4SLinus Torvalds int nr_threads; 		/* The idle threads do not count.. */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
841da177e4SLinus Torvalds 
857e066fb8SMathieu Desnoyers DEFINE_TRACE(sched_process_fork);
867e066fb8SMathieu Desnoyers 
871da177e4SLinus Torvalds int nr_processes(void)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	int cpu;
901da177e4SLinus Torvalds 	int total = 0;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	for_each_online_cpu(cpu)
931da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	return total;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
991da177e4SLinus Torvalds # define alloc_task_struct()	kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
1001da177e4SLinus Torvalds # define free_task_struct(tsk)	kmem_cache_free(task_struct_cachep, (tsk))
101e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep;
1021da177e4SLinus Torvalds #endif
1031da177e4SLinus Torvalds 
104b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR
105b69c49b7SFUJITA Tomonori static inline struct thread_info *alloc_thread_info(struct task_struct *tsk)
106b69c49b7SFUJITA Tomonori {
107b69c49b7SFUJITA Tomonori #ifdef CONFIG_DEBUG_STACK_USAGE
108b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL | __GFP_ZERO;
109b69c49b7SFUJITA Tomonori #else
110b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL;
111b69c49b7SFUJITA Tomonori #endif
112b69c49b7SFUJITA Tomonori 	return (struct thread_info *)__get_free_pages(mask, THREAD_SIZE_ORDER);
113b69c49b7SFUJITA Tomonori }
114b69c49b7SFUJITA Tomonori 
115b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti)
116b69c49b7SFUJITA Tomonori {
117b69c49b7SFUJITA Tomonori 	free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
118b69c49b7SFUJITA Tomonori }
119b69c49b7SFUJITA Tomonori #endif
120b69c49b7SFUJITA Tomonori 
1211da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
122e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
125e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
128e18b890bSChristoph Lameter struct kmem_cache *files_cachep;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
131e18b890bSChristoph Lameter struct kmem_cache *fs_cachep;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
134e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
137e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1401da177e4SLinus Torvalds {
1413e26c149SPeter Zijlstra 	prop_local_destroy_single(&tsk->dirties);
142f7e4217bSRoman Zippel 	free_thread_info(tsk->stack);
14323f78d4aSIngo Molnar 	rt_mutex_debug_task_free(tsk);
144fb52607aSFrederic Weisbecker 	ftrace_graph_exit_task(tsk);
1451da177e4SLinus Torvalds 	free_task_struct(tsk);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1481da177e4SLinus Torvalds 
149158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
1501da177e4SLinus Torvalds {
151270f722dSEugene Teo 	WARN_ON(!tsk->exit_state);
1521da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1531da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1541da177e4SLinus Torvalds 
1553b11a1deSDavid Howells 	put_cred(tsk->real_cred);
156f1752eecSDavid Howells 	put_cred(tsk->cred);
15735df17c5SShailabh Nagar 	delayacct_tsk_free(tsk);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
1601da177e4SLinus Torvalds 		free_task(tsk);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1632adee9b3SSuresh Siddha /*
1642adee9b3SSuresh Siddha  * macro override instead of weak attribute alias, to workaround
1652adee9b3SSuresh Siddha  * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
1662adee9b3SSuresh Siddha  */
1672adee9b3SSuresh Siddha #ifndef arch_task_cache_init
1682adee9b3SSuresh Siddha #define arch_task_cache_init()
1692adee9b3SSuresh Siddha #endif
17061c4628bSSuresh Siddha 
1711da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
1741da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
1751da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
1761da177e4SLinus Torvalds #endif
1771da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
1781da177e4SLinus Torvalds 	task_struct_cachep =
1791da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
18020c2df83SPaul Mundt 			ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL);
1811da177e4SLinus Torvalds #endif
1821da177e4SLinus Torvalds 
18361c4628bSSuresh Siddha 	/* do the arch specific task caches init */
18461c4628bSSuresh Siddha 	arch_task_cache_init();
18561c4628bSSuresh Siddha 
1861da177e4SLinus Torvalds 	/*
1871da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
1881da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
1891da177e4SLinus Torvalds 	 * of memory.
1901da177e4SLinus Torvalds 	 */
1911da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	/*
1941da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
1951da177e4SLinus Torvalds 	 */
1961da177e4SLinus Torvalds 	if(max_threads < 20)
1971da177e4SLinus Torvalds 		max_threads = 20;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
2001da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
2011da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
2021da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
20561c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
20661c4628bSSuresh Siddha 					       struct task_struct *src)
20761c4628bSSuresh Siddha {
20861c4628bSSuresh Siddha 	*dst = *src;
20961c4628bSSuresh Siddha 	return 0;
21061c4628bSSuresh Siddha }
21161c4628bSSuresh Siddha 
2121da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	struct task_struct *tsk;
2151da177e4SLinus Torvalds 	struct thread_info *ti;
2167c9f8861SEric Sandeen 	unsigned long *stackend;
2177c9f8861SEric Sandeen 
2183e26c149SPeter Zijlstra 	int err;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	prepare_to_copy(orig);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	tsk = alloc_task_struct();
2231da177e4SLinus Torvalds 	if (!tsk)
2241da177e4SLinus Torvalds 		return NULL;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	ti = alloc_thread_info(tsk);
2271da177e4SLinus Torvalds 	if (!ti) {
2281da177e4SLinus Torvalds 		free_task_struct(tsk);
2291da177e4SLinus Torvalds 		return NULL;
2301da177e4SLinus Torvalds 	}
2311da177e4SLinus Torvalds 
23261c4628bSSuresh Siddha  	err = arch_dup_task_struct(tsk, orig);
23361c4628bSSuresh Siddha 	if (err)
23461c4628bSSuresh Siddha 		goto out;
23561c4628bSSuresh Siddha 
236f7e4217bSRoman Zippel 	tsk->stack = ti;
2373e26c149SPeter Zijlstra 
2383e26c149SPeter Zijlstra 	err = prop_local_init_single(&tsk->dirties);
23961c4628bSSuresh Siddha 	if (err)
24061c4628bSSuresh Siddha 		goto out;
2413e26c149SPeter Zijlstra 
24210ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
2437c9f8861SEric Sandeen 	stackend = end_of_stack(tsk);
2447c9f8861SEric Sandeen 	*stackend = STACK_END_MAGIC;	/* for overflow detection */
2451da177e4SLinus Torvalds 
2460a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
2470a425405SArjan van de Ven 	tsk->stack_canary = get_random_int();
2480a425405SArjan van de Ven #endif
2490a425405SArjan van de Ven 
2501da177e4SLinus Torvalds 	/* One for us, one for whoever does the "release_task()" (usually parent) */
2511da177e4SLinus Torvalds 	atomic_set(&tsk->usage,2);
2524b5d37acSGiancarlo Formicuccia 	atomic_set(&tsk->fs_excl, 0);
2536c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
2542056a782SJens Axboe 	tsk->btrace_seq = 0;
2556c5c9341SAlexey Dobriyan #endif
256a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
2571da177e4SLinus Torvalds 	return tsk;
25861c4628bSSuresh Siddha 
25961c4628bSSuresh Siddha out:
26061c4628bSSuresh Siddha 	free_thread_info(ti);
26161c4628bSSuresh Siddha 	free_task_struct(tsk);
26261c4628bSSuresh Siddha 	return NULL;
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds #ifdef CONFIG_MMU
266a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, **pprev;
2691da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
2701da177e4SLinus Torvalds 	int retval;
2711da177e4SLinus Torvalds 	unsigned long charge;
2721da177e4SLinus Torvalds 	struct mempolicy *pol;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
275ec8c0446SRalf Baechle 	flush_cache_dup_mm(oldmm);
276ad339451SIngo Molnar 	/*
277ad339451SIngo Molnar 	 * Not linked in yet - no deadlock potential:
278ad339451SIngo Molnar 	 */
279ad339451SIngo Molnar 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
2807ee78232SHugh Dickins 
2811da177e4SLinus Torvalds 	mm->locked_vm = 0;
2821da177e4SLinus Torvalds 	mm->mmap = NULL;
2831da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
2841da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
2851363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
2861da177e4SLinus Torvalds 	mm->map_count = 0;
28794894244SRusty Russell 	cpumask_clear(mm_cpumask(mm));
2881da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
2891da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
2901da177e4SLinus Torvalds 	rb_parent = NULL;
2911da177e4SLinus Torvalds 	pprev = &mm->mmap;
2921da177e4SLinus Torvalds 
293fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
2941da177e4SLinus Torvalds 		struct file *file;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
2973b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
2983b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
299ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
3003b6bfcdbSHugh Dickins 								-pages);
3011da177e4SLinus Torvalds 			continue;
3021da177e4SLinus Torvalds 		}
3031da177e4SLinus Torvalds 		charge = 0;
3041da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
3051da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
3061da177e4SLinus Torvalds 			if (security_vm_enough_memory(len))
3071da177e4SLinus Torvalds 				goto fail_nomem;
3081da177e4SLinus Torvalds 			charge = len;
3091da177e4SLinus Torvalds 		}
310e94b1766SChristoph Lameter 		tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
3111da177e4SLinus Torvalds 		if (!tmp)
3121da177e4SLinus Torvalds 			goto fail_nomem;
3131da177e4SLinus Torvalds 		*tmp = *mpnt;
314846a16bfSLee Schermerhorn 		pol = mpol_dup(vma_policy(mpnt));
3151da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
3161da177e4SLinus Torvalds 		if (IS_ERR(pol))
3171da177e4SLinus Torvalds 			goto fail_nomem_policy;
3181da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
3191da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
3201da177e4SLinus Torvalds 		tmp->vm_mm = mm;
3211da177e4SLinus Torvalds 		tmp->vm_next = NULL;
3221da177e4SLinus Torvalds 		anon_vma_link(tmp);
3231da177e4SLinus Torvalds 		file = tmp->vm_file;
3241da177e4SLinus Torvalds 		if (file) {
325f3a43f3fSJosef "Jeff" Sipek 			struct inode *inode = file->f_path.dentry->d_inode;
326b88ed205SHugh Dickins 			struct address_space *mapping = file->f_mapping;
327b88ed205SHugh Dickins 
3281da177e4SLinus Torvalds 			get_file(file);
3291da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
3301da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
331b88ed205SHugh Dickins 			spin_lock(&mapping->i_mmap_lock);
332b88ed205SHugh Dickins 			if (tmp->vm_flags & VM_SHARED)
333b88ed205SHugh Dickins 				mapping->i_mmap_writable++;
3341da177e4SLinus Torvalds 			tmp->vm_truncate_count = mpnt->vm_truncate_count;
335b88ed205SHugh Dickins 			flush_dcache_mmap_lock(mapping);
336b88ed205SHugh Dickins 			/* insert tmp into the share list, just after mpnt */
3371da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
338b88ed205SHugh Dickins 			flush_dcache_mmap_unlock(mapping);
339b88ed205SHugh Dickins 			spin_unlock(&mapping->i_mmap_lock);
3401da177e4SLinus Torvalds 		}
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 		/*
343a1e78772SMel Gorman 		 * Clear hugetlb-related page reserves for children. This only
344a1e78772SMel Gorman 		 * affects MAP_PRIVATE mappings. Faults generated by the child
345a1e78772SMel Gorman 		 * are not guaranteed to succeed, even if read-only
346a1e78772SMel Gorman 		 */
347a1e78772SMel Gorman 		if (is_vm_hugetlb_page(tmp))
348a1e78772SMel Gorman 			reset_vma_resv_huge_pages(tmp);
349a1e78772SMel Gorman 
350a1e78772SMel Gorman 		/*
3517ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
3521da177e4SLinus Torvalds 		 */
3531da177e4SLinus Torvalds 		*pprev = tmp;
3541da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
3571da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
3581da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 		mm->map_count++;
3610b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
3641da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 		if (retval)
3671da177e4SLinus Torvalds 			goto out;
3681da177e4SLinus Torvalds 	}
369d6dd61c8SJeremy Fitzhardinge 	/* a new mm has just been created */
370d6dd61c8SJeremy Fitzhardinge 	arch_dup_mmap(oldmm, mm);
3711da177e4SLinus Torvalds 	retval = 0;
3721da177e4SLinus Torvalds out:
3737ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
374fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
3751da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
3761da177e4SLinus Torvalds 	return retval;
3771da177e4SLinus Torvalds fail_nomem_policy:
3781da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
3791da177e4SLinus Torvalds fail_nomem:
3801da177e4SLinus Torvalds 	retval = -ENOMEM;
3811da177e4SLinus Torvalds 	vm_unacct_memory(charge);
3821da177e4SLinus Torvalds 	goto out;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
3881da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
3891da177e4SLinus Torvalds 		return -ENOMEM;
3901da177e4SLinus Torvalds 	return 0;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm)
3941da177e4SLinus Torvalds {
3955e541973SBenjamin Herrenschmidt 	pgd_free(mm, mm->pgd);
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds #else
3981da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
3991da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
4001da177e4SLinus Torvalds #define mm_free_pgd(mm)
4011da177e4SLinus Torvalds #endif /* CONFIG_MMU */
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
4041da177e4SLinus Torvalds 
405e94b1766SChristoph Lameter #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
4061da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
4071da177e4SLinus Torvalds 
4084cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
4094cb0e11bSHidehiro Kawai 
4104cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s)
4114cb0e11bSHidehiro Kawai {
4124cb0e11bSHidehiro Kawai 	default_dump_filter =
4134cb0e11bSHidehiro Kawai 		(simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
4144cb0e11bSHidehiro Kawai 		MMF_DUMP_FILTER_MASK;
4154cb0e11bSHidehiro Kawai 	return 1;
4164cb0e11bSHidehiro Kawai }
4174cb0e11bSHidehiro Kawai 
4184cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup);
4194cb0e11bSHidehiro Kawai 
4201da177e4SLinus Torvalds #include <linux/init_task.h>
4211da177e4SLinus Torvalds 
42278fb7466SPavel Emelianov static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
4251da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
4261da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
4271da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
4284cb0e11bSHidehiro Kawai 	mm->flags = (current->mm) ? current->mm->flags : default_dump_filter;
429999d9fc1SOleg Nesterov 	mm->core_state = NULL;
4301da177e4SLinus Torvalds 	mm->nr_ptes = 0;
4314294621fSHugh Dickins 	set_mm_counter(mm, file_rss, 0);
432404351e6SHugh Dickins 	set_mm_counter(mm, anon_rss, 0);
4331da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
434abf137ddSJens Axboe 	spin_lock_init(&mm->ioctx_lock);
435abf137ddSJens Axboe 	INIT_HLIST_HEAD(&mm->ioctx_list);
4361da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
4371363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
438cf475ad2SBalbir Singh 	mm_init_owner(mm, p);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
4411da177e4SLinus Torvalds 		mm->def_flags = 0;
442cddb8a5cSAndrea Arcangeli 		mmu_notifier_mm_init(mm);
4431da177e4SLinus Torvalds 		return mm;
4441da177e4SLinus Torvalds 	}
44578fb7466SPavel Emelianov 
4461da177e4SLinus Torvalds 	free_mm(mm);
4471da177e4SLinus Torvalds 	return NULL;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds /*
4511da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
4521da177e4SLinus Torvalds  */
4531da177e4SLinus Torvalds struct mm_struct * mm_alloc(void)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct mm_struct * mm;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	mm = allocate_mm();
4581da177e4SLinus Torvalds 	if (mm) {
4591da177e4SLinus Torvalds 		memset(mm, 0, sizeof(*mm));
46078fb7466SPavel Emelianov 		mm = mm_init(mm, current);
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 	return mm;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds /*
4661da177e4SLinus Torvalds  * Called when the last reference to the mm
4671da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
4681da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
4691da177e4SLinus Torvalds  */
4707ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
4731da177e4SLinus Torvalds 	mm_free_pgd(mm);
4741da177e4SLinus Torvalds 	destroy_context(mm);
475cddb8a5cSAndrea Arcangeli 	mmu_notifier_mm_destroy(mm);
4761da177e4SLinus Torvalds 	free_mm(mm);
4771da177e4SLinus Torvalds }
4786d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds /*
4811da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
4821da177e4SLinus Torvalds  */
4831da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
4841da177e4SLinus Torvalds {
4850ae26f1bSAndrew Morton 	might_sleep();
4860ae26f1bSAndrew Morton 
4871da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
4881da177e4SLinus Torvalds 		exit_aio(mm);
4891da177e4SLinus Torvalds 		exit_mmap(mm);
490925d1c40SMatt Helsley 		set_mm_exe_file(mm, NULL);
4911da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
4921da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
4931da177e4SLinus Torvalds 			list_del(&mm->mmlist);
4941da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
4951da177e4SLinus Torvalds 		}
4961da177e4SLinus Torvalds 		put_swap_token(mm);
4971da177e4SLinus Torvalds 		mmdrop(mm);
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds /**
5031da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
5041da177e4SLinus Torvalds  *
505246bb0b1SOleg Nesterov  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
5061da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
5071da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
5081da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
5091da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
5101da177e4SLinus Torvalds  */
5111da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	struct mm_struct *mm;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	task_lock(task);
5161da177e4SLinus Torvalds 	mm = task->mm;
5171da177e4SLinus Torvalds 	if (mm) {
518246bb0b1SOleg Nesterov 		if (task->flags & PF_KTHREAD)
5191da177e4SLinus Torvalds 			mm = NULL;
5201da177e4SLinus Torvalds 		else
5211da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 	task_unlock(task);
5241da177e4SLinus Torvalds 	return mm;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
5291da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
5301da177e4SLinus Torvalds  * error success whatever.
5311da177e4SLinus Torvalds  *
5321da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
5331da177e4SLinus Torvalds  * from the current process.
5341da177e4SLinus Torvalds  *
5351da177e4SLinus Torvalds  * This difference is important for error handling, when we
5361da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
5371da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
5381da177e4SLinus Torvalds  * restoring the old one. . .
5391da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
5401da177e4SLinus Torvalds  */
5411da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	struct completion *vfork_done = tsk->vfork_done;
5441da177e4SLinus Torvalds 
5458141c7f3SLinus Torvalds 	/* Get rid of any futexes when releasing the mm */
5468141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX
5478141c7f3SLinus Torvalds 	if (unlikely(tsk->robust_list))
5488141c7f3SLinus Torvalds 		exit_robust_list(tsk);
5498141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT
5508141c7f3SLinus Torvalds 	if (unlikely(tsk->compat_robust_list))
5518141c7f3SLinus Torvalds 		compat_exit_robust_list(tsk);
5528141c7f3SLinus Torvalds #endif
5538141c7f3SLinus Torvalds #endif
5548141c7f3SLinus Torvalds 
5551da177e4SLinus Torvalds 	/* Get rid of any cached register state */
5561da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	/* notify parent sleeping on vfork() */
5591da177e4SLinus Torvalds 	if (vfork_done) {
5601da177e4SLinus Torvalds 		tsk->vfork_done = NULL;
5611da177e4SLinus Torvalds 		complete(vfork_done);
5621da177e4SLinus Torvalds 	}
563fec1d011SRoland McGrath 
564fec1d011SRoland McGrath 	/*
565fec1d011SRoland McGrath 	 * If we're exiting normally, clear a user-space tid field if
566fec1d011SRoland McGrath 	 * requested.  We leave this alone when dying by signal, to leave
567fec1d011SRoland McGrath 	 * the value intact in a core dump, and to save the unnecessary
568fec1d011SRoland McGrath 	 * trouble otherwise.  Userland only wants this done for a sys_exit.
569fec1d011SRoland McGrath 	 */
570fec1d011SRoland McGrath 	if (tsk->clear_child_tid
571fec1d011SRoland McGrath 	    && !(tsk->flags & PF_SIGNALED)
572fec1d011SRoland McGrath 	    && atomic_read(&mm->mm_users) > 1) {
5731da177e4SLinus Torvalds 		u32 __user * tidptr = tsk->clear_child_tid;
5741da177e4SLinus Torvalds 		tsk->clear_child_tid = NULL;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 		/*
5771da177e4SLinus Torvalds 		 * We don't check the error code - if userspace has
5781da177e4SLinus Torvalds 		 * not set up a proper pointer then tough luck.
5791da177e4SLinus Torvalds 		 */
5801da177e4SLinus Torvalds 		put_user(0, tidptr);
5811da177e4SLinus Torvalds 		sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
585a0a7ec30SJANAK DESAI /*
586a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
587a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
588a0a7ec30SJANAK DESAI  */
589402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk)
590a0a7ec30SJANAK DESAI {
591a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
592a0a7ec30SJANAK DESAI 	int err;
593a0a7ec30SJANAK DESAI 
594a0a7ec30SJANAK DESAI 	if (!oldmm)
595a0a7ec30SJANAK DESAI 		return NULL;
596a0a7ec30SJANAK DESAI 
597a0a7ec30SJANAK DESAI 	mm = allocate_mm();
598a0a7ec30SJANAK DESAI 	if (!mm)
599a0a7ec30SJANAK DESAI 		goto fail_nomem;
600a0a7ec30SJANAK DESAI 
601a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
602a0a7ec30SJANAK DESAI 
6037602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
6047602bdf2SAshwin Chaugule 	mm->token_priority = 0;
6057602bdf2SAshwin Chaugule 	mm->last_interval = 0;
6067602bdf2SAshwin Chaugule 
60778fb7466SPavel Emelianov 	if (!mm_init(mm, tsk))
608a0a7ec30SJANAK DESAI 		goto fail_nomem;
609a0a7ec30SJANAK DESAI 
610a0a7ec30SJANAK DESAI 	if (init_new_context(tsk, mm))
611a0a7ec30SJANAK DESAI 		goto fail_nocontext;
612a0a7ec30SJANAK DESAI 
613925d1c40SMatt Helsley 	dup_mm_exe_file(oldmm, mm);
614925d1c40SMatt Helsley 
615a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
616a0a7ec30SJANAK DESAI 	if (err)
617a0a7ec30SJANAK DESAI 		goto free_pt;
618a0a7ec30SJANAK DESAI 
619a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
620a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
621a0a7ec30SJANAK DESAI 
622a0a7ec30SJANAK DESAI 	return mm;
623a0a7ec30SJANAK DESAI 
624a0a7ec30SJANAK DESAI free_pt:
625a0a7ec30SJANAK DESAI 	mmput(mm);
626a0a7ec30SJANAK DESAI 
627a0a7ec30SJANAK DESAI fail_nomem:
628a0a7ec30SJANAK DESAI 	return NULL;
629a0a7ec30SJANAK DESAI 
630a0a7ec30SJANAK DESAI fail_nocontext:
631a0a7ec30SJANAK DESAI 	/*
632a0a7ec30SJANAK DESAI 	 * If init_new_context() failed, we cannot use mmput() to free the mm
633a0a7ec30SJANAK DESAI 	 * because it calls destroy_context()
634a0a7ec30SJANAK DESAI 	 */
635a0a7ec30SJANAK DESAI 	mm_free_pgd(mm);
636a0a7ec30SJANAK DESAI 	free_mm(mm);
637a0a7ec30SJANAK DESAI 	return NULL;
638a0a7ec30SJANAK DESAI }
639a0a7ec30SJANAK DESAI 
6401da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	struct mm_struct * mm, *oldmm;
6431da177e4SLinus Torvalds 	int retval;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
6461da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	tsk->mm = NULL;
6491da177e4SLinus Torvalds 	tsk->active_mm = NULL;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	/*
6521da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
6531da177e4SLinus Torvalds 	 *
6541da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
6551da177e4SLinus Torvalds 	 */
6561da177e4SLinus Torvalds 	oldmm = current->mm;
6571da177e4SLinus Torvalds 	if (!oldmm)
6581da177e4SLinus Torvalds 		return 0;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
6611da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
6621da177e4SLinus Torvalds 		mm = oldmm;
6631da177e4SLinus Torvalds 		goto good_mm;
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	retval = -ENOMEM;
667a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
6681da177e4SLinus Torvalds 	if (!mm)
6691da177e4SLinus Torvalds 		goto fail_nomem;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds good_mm:
6727602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
6737602bdf2SAshwin Chaugule 	mm->token_priority = 0;
6747602bdf2SAshwin Chaugule 	mm->last_interval = 0;
6757602bdf2SAshwin Chaugule 
6761da177e4SLinus Torvalds 	tsk->mm = mm;
6771da177e4SLinus Torvalds 	tsk->active_mm = mm;
6781da177e4SLinus Torvalds 	return 0;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds fail_nomem:
6811da177e4SLinus Torvalds 	return retval;
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
684a39bc516SAlexey Dobriyan static struct fs_struct *__copy_fs_struct(struct fs_struct *old)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
6871da177e4SLinus Torvalds 	/* We don't need to lock fs - think why ;-) */
6881da177e4SLinus Torvalds 	if (fs) {
6891da177e4SLinus Torvalds 		atomic_set(&fs->count, 1);
6901da177e4SLinus Torvalds 		rwlock_init(&fs->lock);
6911da177e4SLinus Torvalds 		fs->umask = old->umask;
6921da177e4SLinus Torvalds 		read_lock(&old->lock);
6936ac08c39SJan Blunck 		fs->root = old->root;
6946ac08c39SJan Blunck 		path_get(&old->root);
6956ac08c39SJan Blunck 		fs->pwd = old->pwd;
6966ac08c39SJan Blunck 		path_get(&old->pwd);
6971da177e4SLinus Torvalds 		read_unlock(&old->lock);
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 	return fs;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds struct fs_struct *copy_fs_struct(struct fs_struct *old)
7031da177e4SLinus Torvalds {
7041da177e4SLinus Torvalds 	return __copy_fs_struct(old);
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(copy_fs_struct);
7081da177e4SLinus Torvalds 
709a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
7121da177e4SLinus Torvalds 		atomic_inc(&current->fs->count);
7131da177e4SLinus Torvalds 		return 0;
7141da177e4SLinus Torvalds 	}
7151da177e4SLinus Torvalds 	tsk->fs = __copy_fs_struct(current->fs);
7161da177e4SLinus Torvalds 	if (!tsk->fs)
7171da177e4SLinus Torvalds 		return -ENOMEM;
7181da177e4SLinus Torvalds 	return 0;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
721a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
722a016f338SJANAK DESAI {
723a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
724a016f338SJANAK DESAI 	int error = 0;
725a016f338SJANAK DESAI 
726a016f338SJANAK DESAI 	/*
727a016f338SJANAK DESAI 	 * A background process may not have any files ...
728a016f338SJANAK DESAI 	 */
729a016f338SJANAK DESAI 	oldf = current->files;
730a016f338SJANAK DESAI 	if (!oldf)
731a016f338SJANAK DESAI 		goto out;
732a016f338SJANAK DESAI 
733a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
734a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
735a016f338SJANAK DESAI 		goto out;
736a016f338SJANAK DESAI 	}
737a016f338SJANAK DESAI 
738a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
739a016f338SJANAK DESAI 	if (!newf)
740a016f338SJANAK DESAI 		goto out;
741a016f338SJANAK DESAI 
742a016f338SJANAK DESAI 	tsk->files = newf;
743a016f338SJANAK DESAI 	error = 0;
744a016f338SJANAK DESAI out:
745a016f338SJANAK DESAI 	return error;
746a016f338SJANAK DESAI }
747a016f338SJANAK DESAI 
748fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
749fd0928dfSJens Axboe {
750fd0928dfSJens Axboe #ifdef CONFIG_BLOCK
751fd0928dfSJens Axboe 	struct io_context *ioc = current->io_context;
752fd0928dfSJens Axboe 
753fd0928dfSJens Axboe 	if (!ioc)
754fd0928dfSJens Axboe 		return 0;
755fadad878SJens Axboe 	/*
756fadad878SJens Axboe 	 * Share io context with parent, if CLONE_IO is set
757fadad878SJens Axboe 	 */
758fadad878SJens Axboe 	if (clone_flags & CLONE_IO) {
759fadad878SJens Axboe 		tsk->io_context = ioc_task_link(ioc);
760fadad878SJens Axboe 		if (unlikely(!tsk->io_context))
761fadad878SJens Axboe 			return -ENOMEM;
762fadad878SJens Axboe 	} else if (ioprio_valid(ioc->ioprio)) {
763fd0928dfSJens Axboe 		tsk->io_context = alloc_io_context(GFP_KERNEL, -1);
764fd0928dfSJens Axboe 		if (unlikely(!tsk->io_context))
765fd0928dfSJens Axboe 			return -ENOMEM;
766fd0928dfSJens Axboe 
767fd0928dfSJens Axboe 		tsk->io_context->ioprio = ioc->ioprio;
768fd0928dfSJens Axboe 	}
769fd0928dfSJens Axboe #endif
770fd0928dfSJens Axboe 	return 0;
771fd0928dfSJens Axboe }
772fd0928dfSJens Axboe 
773a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	struct sighand_struct *sig;
7761da177e4SLinus Torvalds 
77760348802SZhaolei 	if (clone_flags & CLONE_SIGHAND) {
7781da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
7791da177e4SLinus Torvalds 		return 0;
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
782e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
7831da177e4SLinus Torvalds 	if (!sig)
7841da177e4SLinus Torvalds 		return -ENOMEM;
7851da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
7861da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
7871da177e4SLinus Torvalds 	return 0;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
790a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
791c81addc9SOleg Nesterov {
792c81addc9SOleg Nesterov 	if (atomic_dec_and_test(&sighand->count))
793c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
794c81addc9SOleg Nesterov }
795c81addc9SOleg Nesterov 
796f06febc9SFrank Mayhar 
797f06febc9SFrank Mayhar /*
798f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a thread group.
799f06febc9SFrank Mayhar  */
800f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig)
801f06febc9SFrank Mayhar {
802f06febc9SFrank Mayhar 	/* Thread group counters. */
803f06febc9SFrank Mayhar 	thread_group_cputime_init(sig);
804f06febc9SFrank Mayhar 
805f06febc9SFrank Mayhar 	/* Expiration times and increments. */
806f06febc9SFrank Mayhar 	sig->it_virt_expires = cputime_zero;
807f06febc9SFrank Mayhar 	sig->it_virt_incr = cputime_zero;
808f06febc9SFrank Mayhar 	sig->it_prof_expires = cputime_zero;
809f06febc9SFrank Mayhar 	sig->it_prof_incr = cputime_zero;
810f06febc9SFrank Mayhar 
811f06febc9SFrank Mayhar 	/* Cached expiration times. */
812f06febc9SFrank Mayhar 	sig->cputime_expires.prof_exp = cputime_zero;
813f06febc9SFrank Mayhar 	sig->cputime_expires.virt_exp = cputime_zero;
814f06febc9SFrank Mayhar 	sig->cputime_expires.sched_exp = 0;
815f06febc9SFrank Mayhar 
816f06febc9SFrank Mayhar 	/* The timer lists. */
817f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
818f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
819f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
820f06febc9SFrank Mayhar }
821f06febc9SFrank Mayhar 
822a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	struct signal_struct *sig;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	if (clone_flags & CLONE_THREAD) {
8271da177e4SLinus Torvalds 		atomic_inc(&current->signal->count);
8281da177e4SLinus Torvalds 		atomic_inc(&current->signal->live);
829490dea45SPeter Zijlstra 		return 0;
8301da177e4SLinus Torvalds 	}
8311da177e4SLinus Torvalds 	sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
832490dea45SPeter Zijlstra 
833490dea45SPeter Zijlstra 	if (sig)
834490dea45SPeter Zijlstra 		posix_cpu_timers_init_group(sig);
835490dea45SPeter Zijlstra 
8361da177e4SLinus Torvalds 	tsk->signal = sig;
8371da177e4SLinus Torvalds 	if (!sig)
8381da177e4SLinus Torvalds 		return -ENOMEM;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
8411da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
8421da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
8431da177e4SLinus Torvalds 	sig->flags = 0;
8441da177e4SLinus Torvalds 	sig->group_exit_code = 0;
8451da177e4SLinus Torvalds 	sig->group_exit_task = NULL;
8461da177e4SLinus Torvalds 	sig->group_stop_count = 0;
847db51aeccSOleg Nesterov 	sig->curr_target = tsk;
8481da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
8491da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
8501da177e4SLinus Torvalds 
851c9cb2e3dSThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8522ff678b8SThomas Gleixner 	sig->it_real_incr.tv64 = 0;
8531da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	sig->leader = 0;	/* session leadership doesn't inherit */
856ab521dc0SEric W. Biederman 	sig->tty_old_pgrp = NULL;
8579c9f4dedSAlan Cox 	sig->tty = NULL;
8581da177e4SLinus Torvalds 
85932bd671dSPeter Zijlstra 	sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
8609ac52315SLaurent Vivier 	sig->gtime = cputime_zero;
8619ac52315SLaurent Vivier 	sig->cgtime = cputime_zero;
8621da177e4SLinus Torvalds 	sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
8631da177e4SLinus Torvalds 	sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
8646eaeeabaSEric Dumazet 	sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0;
8655995477aSAndrea Righi 	task_io_accounting_init(&sig->ioac);
86632bd671dSPeter Zijlstra 	sig->sum_sched_runtime = 0;
867ad4ecbcbSShailabh Nagar 	taskstats_tgid_init(sig);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	task_lock(current->group_leader);
8701da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
8711da177e4SLinus Torvalds 	task_unlock(current->group_leader);
8721da177e4SLinus Torvalds 
8730e464814SKaiGai Kohei 	acct_init_pacct(&sig->pacct);
8741da177e4SLinus Torvalds 
875522ed776SMiloslav Trmac 	tty_audit_fork(sig);
876522ed776SMiloslav Trmac 
8771da177e4SLinus Torvalds 	return 0;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8806b3934efSOleg Nesterov void __cleanup_signal(struct signal_struct *sig)
8816b3934efSOleg Nesterov {
882f06febc9SFrank Mayhar 	thread_group_cputime_free(sig);
8839c9f4dedSAlan Cox 	tty_kref_put(sig->tty);
8846b3934efSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
8856b3934efSOleg Nesterov }
8866b3934efSOleg Nesterov 
887a39bc516SAlexey Dobriyan static void cleanup_signal(struct task_struct *tsk)
8886b3934efSOleg Nesterov {
8896b3934efSOleg Nesterov 	struct signal_struct *sig = tsk->signal;
8906b3934efSOleg Nesterov 
8916b3934efSOleg Nesterov 	atomic_dec(&sig->live);
8926b3934efSOleg Nesterov 
8936b3934efSOleg Nesterov 	if (atomic_dec_and_test(&sig->count))
8946b3934efSOleg Nesterov 		__cleanup_signal(sig);
8956b3934efSOleg Nesterov }
8966b3934efSOleg Nesterov 
897a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
9001da177e4SLinus Torvalds 
90183144186SRafael J. Wysocki 	new_flags &= ~PF_SUPERPRIV;
9021da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
90309a05394SRoland McGrath 	new_flags |= PF_STARTING;
9041da177e4SLinus Torvalds 	p->flags = new_flags;
9052e131895SRafael J. Wysocki 	clear_freeze_flag(p);
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
90817da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
9111da177e4SLinus Torvalds 
912b488893aSPavel Emelyanov 	return task_pid_vnr(current);
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
915a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p)
91623f78d4aSIngo Molnar {
91723f78d4aSIngo Molnar 	spin_lock_init(&p->pi_lock);
918e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES
91923f78d4aSIngo Molnar 	plist_head_init(&p->pi_waiters, &p->pi_lock);
92023f78d4aSIngo Molnar 	p->pi_blocked_on = NULL;
92123f78d4aSIngo Molnar #endif
92223f78d4aSIngo Molnar }
92323f78d4aSIngo Molnar 
924cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER
925cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
926cf475ad2SBalbir Singh {
927cf475ad2SBalbir Singh 	mm->owner = p;
928cf475ad2SBalbir Singh }
929cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */
930cf475ad2SBalbir Singh 
9311da177e4SLinus Torvalds /*
932f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a single task.
933f06febc9SFrank Mayhar  */
934f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk)
935f06febc9SFrank Mayhar {
936f06febc9SFrank Mayhar 	tsk->cputime_expires.prof_exp = cputime_zero;
937f06febc9SFrank Mayhar 	tsk->cputime_expires.virt_exp = cputime_zero;
938f06febc9SFrank Mayhar 	tsk->cputime_expires.sched_exp = 0;
939f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[0]);
940f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[1]);
941f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[2]);
942f06febc9SFrank Mayhar }
943f06febc9SFrank Mayhar 
944f06febc9SFrank Mayhar /*
9451da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
9461da177e4SLinus Torvalds  * but does not actually start it yet.
9471da177e4SLinus Torvalds  *
9481da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
9491da177e4SLinus Torvalds  * parts of the process environment (as per the clone
9501da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
9511da177e4SLinus Torvalds  */
95236c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags,
9531da177e4SLinus Torvalds 					unsigned long stack_start,
9541da177e4SLinus Torvalds 					struct pt_regs *regs,
9551da177e4SLinus Torvalds 					unsigned long stack_size,
9561da177e4SLinus Torvalds 					int __user *child_tidptr,
95709a05394SRoland McGrath 					struct pid *pid,
95809a05394SRoland McGrath 					int trace)
9591da177e4SLinus Torvalds {
9601da177e4SLinus Torvalds 	int retval;
961a24efe62SMariusz Kozlowski 	struct task_struct *p;
962b4f48b63SPaul Menage 	int cgroup_callbacks_done = 0;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
9651da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	/*
9681da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
9691da177e4SLinus Torvalds 	 * can only be started up within the thread group.
9701da177e4SLinus Torvalds 	 */
9711da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
9721da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	/*
9751da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
9761da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
9771da177e4SLinus Torvalds 	 * for various simplifications in other code.
9781da177e4SLinus Torvalds 	 */
9791da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
9801da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
9831da177e4SLinus Torvalds 	if (retval)
9841da177e4SLinus Torvalds 		goto fork_out;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	retval = -ENOMEM;
9871da177e4SLinus Torvalds 	p = dup_task_struct(current);
9881da177e4SLinus Torvalds 	if (!p)
9891da177e4SLinus Torvalds 		goto fork_out;
9901da177e4SLinus Torvalds 
991bea493a0SPeter Zijlstra 	rt_mutex_init_task(p);
992bea493a0SPeter Zijlstra 
993d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING
994de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
995de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
996de30a2b3SIngo Molnar #endif
9971da177e4SLinus Torvalds 	retval = -EAGAIN;
9983b11a1deSDavid Howells 	if (atomic_read(&p->real_cred->user->processes) >=
9991da177e4SLinus Torvalds 			p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
10001da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
100118b6e041SSerge Hallyn 		    p->real_cred->user != INIT_USER)
10021da177e4SLinus Torvalds 			goto bad_fork_free;
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
1005f1752eecSDavid Howells 	retval = copy_creds(p, clone_flags);
1006f1752eecSDavid Howells 	if (retval < 0)
1007f1752eecSDavid Howells 		goto bad_fork_free;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	/*
10101da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
10111da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
10121da177e4SLinus Torvalds 	 * to stop root fork bombs.
10131da177e4SLinus Torvalds 	 */
101404ec93feSLi Zefan 	retval = -EAGAIN;
10151da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
10161da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
10171da177e4SLinus Torvalds 
1018a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
10191da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	if (p->binfmt && !try_module_get(p->binfmt->module))
10221da177e4SLinus Torvalds 		goto bad_fork_cleanup_put_domain;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	p->did_exec = 0;
1025ca74e92bSShailabh Nagar 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
10261da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
10271da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
10281da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
1029e260be67SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
1030e260be67SPaul E. McKenney 	p->rcu_read_lock_nesting = 0;
1031e260be67SPaul E. McKenney 	p->rcu_flipctr_idx = 0;
1032e260be67SPaul E. McKenney #endif /* #ifdef CONFIG_PREEMPT_RCU */
10331da177e4SLinus Torvalds 	p->vfork_done = NULL;
10341da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SIGPENDING);
10371da177e4SLinus Torvalds 	init_sigpending(&p->pending);
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	p->utime = cputime_zero;
10401da177e4SLinus Torvalds 	p->stime = cputime_zero;
10419ac52315SLaurent Vivier 	p->gtime = cputime_zero;
1042c66f08beSMichael Neuling 	p->utimescaled = cputime_zero;
1043c66f08beSMichael Neuling 	p->stimescaled = cputime_zero;
104473a2bcb0SPeter Zijlstra 	p->prev_utime = cputime_zero;
10459301899bSBalbir Singh 	p->prev_stime = cputime_zero;
1046172ba844SBalbir Singh 
10476976675dSArjan van de Ven 	p->default_timer_slack_ns = current->timer_slack_ns;
10486976675dSArjan van de Ven 
104982a1fcb9SIngo Molnar #ifdef CONFIG_DETECT_SOFTLOCKUP
105082a1fcb9SIngo Molnar 	p->last_switch_count = 0;
105182a1fcb9SIngo Molnar 	p->last_switch_timestamp = 0;
105282a1fcb9SIngo Molnar #endif
105382a1fcb9SIngo Molnar 
10545995477aSAndrea Righi 	task_io_accounting_init(&p->ioac);
10551da177e4SLinus Torvalds 	acct_clear_integrals(p);
10561da177e4SLinus Torvalds 
1057f06febc9SFrank Mayhar 	posix_cpu_timers_init(p);
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	p->lock_depth = -1;		/* -1 = no lock */
10601da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
1061924b42d5STomas Janousek 	p->real_start_time = p->start_time;
1062924b42d5STomas Janousek 	monotonic_to_bootbased(&p->real_start_time);
10631da177e4SLinus Torvalds 	p->io_context = NULL;
10641da177e4SLinus Torvalds 	p->audit_context = NULL;
1065b4f48b63SPaul Menage 	cgroup_fork(p);
10661da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1067846a16bfSLee Schermerhorn 	p->mempolicy = mpol_dup(p->mempolicy);
10681da177e4SLinus Torvalds  	if (IS_ERR(p->mempolicy)) {
10691da177e4SLinus Torvalds  		retval = PTR_ERR(p->mempolicy);
10701da177e4SLinus Torvalds  		p->mempolicy = NULL;
1071b4f48b63SPaul Menage  		goto bad_fork_cleanup_cgroup;
10721da177e4SLinus Torvalds  	}
1073c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(p);
10741da177e4SLinus Torvalds #endif
1075de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1076de30a2b3SIngo Molnar 	p->irq_events = 0;
1077b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1078b36e4758SRussell King 	p->hardirqs_enabled = 1;
1079b36e4758SRussell King #else
1080de30a2b3SIngo Molnar 	p->hardirqs_enabled = 0;
1081b36e4758SRussell King #endif
1082de30a2b3SIngo Molnar 	p->hardirq_enable_ip = 0;
1083de30a2b3SIngo Molnar 	p->hardirq_enable_event = 0;
1084de30a2b3SIngo Molnar 	p->hardirq_disable_ip = _THIS_IP_;
1085de30a2b3SIngo Molnar 	p->hardirq_disable_event = 0;
1086de30a2b3SIngo Molnar 	p->softirqs_enabled = 1;
1087de30a2b3SIngo Molnar 	p->softirq_enable_ip = _THIS_IP_;
1088de30a2b3SIngo Molnar 	p->softirq_enable_event = 0;
1089de30a2b3SIngo Molnar 	p->softirq_disable_ip = 0;
1090de30a2b3SIngo Molnar 	p->softirq_disable_event = 0;
1091de30a2b3SIngo Molnar 	p->hardirq_context = 0;
1092de30a2b3SIngo Molnar 	p->softirq_context = 0;
1093de30a2b3SIngo Molnar #endif
1094fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1095fbb9ce95SIngo Molnar 	p->lockdep_depth = 0; /* no locks held yet */
1096fbb9ce95SIngo Molnar 	p->curr_chain_key = 0;
1097fbb9ce95SIngo Molnar 	p->lockdep_recursion = 0;
1098fbb9ce95SIngo Molnar #endif
10991da177e4SLinus Torvalds 
1100408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1101408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1102408894eeSIngo Molnar #endif
110306eb23b1SOleg Nesterov 	if (unlikely(current->ptrace))
1104bf53de90SMarkus Metzger 		ptrace_fork(p, clone_flags);
1105408894eeSIngo Molnar 
11063c90e6e9SSrivatsa Vaddagiri 	/* Perform scheduler related setup. Assign this task to a CPU. */
11073c90e6e9SSrivatsa Vaddagiri 	sched_fork(p, clone_flags);
11083c90e6e9SSrivatsa Vaddagiri 
11091da177e4SLinus Torvalds 	if ((retval = audit_alloc(p)))
1110f1752eecSDavid Howells 		goto bad_fork_cleanup_policy;
11111da177e4SLinus Torvalds 	/* copy all the process information */
11121da177e4SLinus Torvalds 	if ((retval = copy_semundo(clone_flags, p)))
11131da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
11141da177e4SLinus Torvalds 	if ((retval = copy_files(clone_flags, p)))
11151da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
11161da177e4SLinus Torvalds 	if ((retval = copy_fs(clone_flags, p)))
11171da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
11181da177e4SLinus Torvalds 	if ((retval = copy_sighand(clone_flags, p)))
11191da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
11201da177e4SLinus Torvalds 	if ((retval = copy_signal(clone_flags, p)))
11211da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
11221da177e4SLinus Torvalds 	if ((retval = copy_mm(clone_flags, p)))
11231da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
1124ab516013SSerge E. Hallyn 	if ((retval = copy_namespaces(clone_flags, p)))
1125d84f4f99SDavid Howells 		goto bad_fork_cleanup_mm;
1126fadad878SJens Axboe 	if ((retval = copy_io(clone_flags, p)))
1127fd0928dfSJens Axboe 		goto bad_fork_cleanup_namespaces;
1128*6f2c55b8SAlexey Dobriyan 	retval = copy_thread(clone_flags, stack_start, stack_size, p, regs);
11291da177e4SLinus Torvalds 	if (retval)
1130fd0928dfSJens Axboe 		goto bad_fork_cleanup_io;
11311da177e4SLinus Torvalds 
1132425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid) {
1133425fb2b4SPavel Emelyanov 		retval = -ENOMEM;
113461bce0f1SEric W. Biederman 		pid = alloc_pid(p->nsproxy->pid_ns);
1135425fb2b4SPavel Emelyanov 		if (!pid)
1136fd0928dfSJens Axboe 			goto bad_fork_cleanup_io;
11376f4e6433SPavel Emelyanov 
11386f4e6433SPavel Emelyanov 		if (clone_flags & CLONE_NEWPID) {
113961bce0f1SEric W. Biederman 			retval = pid_ns_prepare_proc(p->nsproxy->pid_ns);
11406f4e6433SPavel Emelyanov 			if (retval < 0)
11416f4e6433SPavel Emelyanov 				goto bad_fork_free_pid;
11426f4e6433SPavel Emelyanov 		}
1143425fb2b4SPavel Emelyanov 	}
1144425fb2b4SPavel Emelyanov 
1145e8e1abe9SSteven Rostedt 	ftrace_graph_init_task(p);
1146e8e1abe9SSteven Rostedt 
1147425fb2b4SPavel Emelyanov 	p->pid = pid_nr(pid);
1148425fb2b4SPavel Emelyanov 	p->tgid = p->pid;
1149425fb2b4SPavel Emelyanov 	if (clone_flags & CLONE_THREAD)
1150425fb2b4SPavel Emelyanov 		p->tgid = current->tgid;
1151425fb2b4SPavel Emelyanov 
1152e885dcdeSSerge E. Hallyn 	if (current->nsproxy != p->nsproxy) {
1153e885dcdeSSerge E. Hallyn 		retval = ns_cgroup_clone(p, pid);
1154e885dcdeSSerge E. Hallyn 		if (retval)
1155e8e1abe9SSteven Rostedt 			goto bad_fork_free_graph;
1156e885dcdeSSerge E. Hallyn 	}
1157e885dcdeSSerge E. Hallyn 
11581da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
11591da177e4SLinus Torvalds 	/*
11601da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
11611da177e4SLinus Torvalds 	 */
11621da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
116342b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
11648f17d3a5SIngo Molnar 	p->robust_list = NULL;
11658f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
11668f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
11678f17d3a5SIngo Molnar #endif
1168c87e2837SIngo Molnar 	INIT_LIST_HEAD(&p->pi_state_list);
1169c87e2837SIngo Molnar 	p->pi_state_cache = NULL;
117042b2dd0aSAlexey Dobriyan #endif
11711da177e4SLinus Torvalds 	/*
1172f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1173f9a3879aSGOTO Masanori 	 */
1174f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1175f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1176f9a3879aSGOTO Masanori 
1177f9a3879aSGOTO Masanori 	/*
11781da177e4SLinus Torvalds 	 * Syscall tracing should be turned off in the child regardless
11791da177e4SLinus Torvalds 	 * of CLONE_PTRACE.
11801da177e4SLinus Torvalds 	 */
11811da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1182ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1183ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1184ed75e8d5SLaurent Vivier #endif
11859745512cSArjan van de Ven 	clear_all_latency_tracing(p);
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
11881da177e4SLinus Torvalds 	p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
11891da177e4SLinus Torvalds 	p->pdeath_signal = 0;
11901da177e4SLinus Torvalds 	p->exit_state = 0;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	/*
11931da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
11941da177e4SLinus Torvalds 	 * We dont wake it up yet.
11951da177e4SLinus Torvalds 	 */
11961da177e4SLinus Torvalds 	p->group_leader = p;
119747e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
11981da177e4SLinus Torvalds 
1199b4f48b63SPaul Menage 	/* Now that the task is set up, run cgroup callbacks if
1200b4f48b63SPaul Menage 	 * necessary. We need to run them before the task is visible
1201b4f48b63SPaul Menage 	 * on the tasklist. */
1202b4f48b63SPaul Menage 	cgroup_fork_callbacks(p);
1203b4f48b63SPaul Menage 	cgroup_callbacks_done = 1;
1204b4f48b63SPaul Menage 
12051da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
12061da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	/*
1209476d139cSNick Piggin 	 * The task hasn't been attached yet, so its cpus_allowed mask will
1210476d139cSNick Piggin 	 * not be changed, nor will its assigned CPU.
1211476d139cSNick Piggin 	 *
1212476d139cSNick Piggin 	 * The cpus_allowed mask of the parent may have changed after it was
1213476d139cSNick Piggin 	 * copied first time - so re-copy it here, then check the child's CPU
1214476d139cSNick Piggin 	 * to ensure it is on a valid CPU (and if not, just force it back to
1215476d139cSNick Piggin 	 * parent's CPU). This avoids alot of nasty races.
12161da177e4SLinus Torvalds 	 */
12171da177e4SLinus Torvalds 	p->cpus_allowed = current->cpus_allowed;
12186f505b16SPeter Zijlstra 	p->rt.nr_cpus_allowed = current->rt.nr_cpus_allowed;
121926ff6ad9SSrivatsa Vaddagiri 	if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
122026ff6ad9SSrivatsa Vaddagiri 			!cpu_online(task_cpu(p))))
12211da177e4SLinus Torvalds 		set_task_cpu(p, smp_processor_id());
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
12242d5516cbSOleg Nesterov 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
12251da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
12262d5516cbSOleg Nesterov 		p->parent_exec_id = current->parent_exec_id;
12272d5516cbSOleg Nesterov 	} else {
12281da177e4SLinus Torvalds 		p->real_parent = current;
12292d5516cbSOleg Nesterov 		p->parent_exec_id = current->self_exec_id;
12302d5516cbSOleg Nesterov 	}
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
12334a2c7a78SOleg Nesterov 
12344a2c7a78SOleg Nesterov 	/*
12354a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
12364a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
12374a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
12384a2c7a78SOleg Nesterov 	 * it's process group.
12394a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
12404a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
12414a2c7a78SOleg Nesterov  	 */
12424a2c7a78SOleg Nesterov 	recalc_sigpending();
12434a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
12444a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
12454a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
12464a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
1247e8e1abe9SSteven Rostedt 		goto bad_fork_free_graph;
12484a2c7a78SOleg Nesterov 	}
12494a2c7a78SOleg Nesterov 
12503f17da69SOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
12511da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
125247e65328SOleg Nesterov 		list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
12531da177e4SLinus Torvalds 	}
12541da177e4SLinus Torvalds 
125573b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
1256f470021aSRoland McGrath 		list_add_tail(&p->sibling, &p->real_parent->children);
125709a05394SRoland McGrath 		tracehook_finish_clone(p, clone_flags, trace);
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
12605cd17569SEric W. Biederman 			if (clone_flags & CLONE_NEWPID)
126130e49c26SPavel Emelyanov 				p->nsproxy->pid_ns->child_reaper = p;
12625cd17569SEric W. Biederman 
1263fea9d175SOleg Nesterov 			p->signal->leader_pid = pid;
12649c9f4dedSAlan Cox 			tty_kref_put(p->signal->tty);
12659c9f4dedSAlan Cox 			p->signal->tty = tty_kref_get(current->signal->tty);
12669a2e7057SPavel Emelyanov 			set_task_pgrp(p, task_pgrp_nr(current));
1267a47afb0fSPavel Emelianov 			set_task_session(p, task_session_nr(current));
12685cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
12695cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_SID, task_session(current));
12705e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
12711da177e4SLinus Torvalds 			__get_cpu_var(process_counts)++;
12721da177e4SLinus Torvalds 		}
127385868995SSukadev Bhattiprolu 		attach_pid(p, PIDTYPE_PID, pid);
12741da177e4SLinus Torvalds 		nr_threads++;
127573b9ebfeSOleg Nesterov 	}
127673b9ebfeSOleg Nesterov 
12771da177e4SLinus Torvalds 	total_forks++;
12783f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
12791da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1280c13cf856SAndrew Morton 	proc_fork_connector(p);
1281817929ecSPaul Menage 	cgroup_post_fork(p);
12821da177e4SLinus Torvalds 	return p;
12831da177e4SLinus Torvalds 
1284e8e1abe9SSteven Rostedt bad_fork_free_graph:
1285e8e1abe9SSteven Rostedt 	ftrace_graph_exit_task(p);
1286425fb2b4SPavel Emelyanov bad_fork_free_pid:
1287425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid)
1288425fb2b4SPavel Emelyanov 		free_pid(pid);
1289fd0928dfSJens Axboe bad_fork_cleanup_io:
1290fd0928dfSJens Axboe 	put_io_context(p->io_context);
1291ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces:
1292444f378bSLinus Torvalds 	exit_task_namespaces(p);
12931da177e4SLinus Torvalds bad_fork_cleanup_mm:
12941da177e4SLinus Torvalds 	if (p->mm)
12951da177e4SLinus Torvalds 		mmput(p->mm);
12961da177e4SLinus Torvalds bad_fork_cleanup_signal:
12976b3934efSOleg Nesterov 	cleanup_signal(p);
12981da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1299a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
13001da177e4SLinus Torvalds bad_fork_cleanup_fs:
13011da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
13021da177e4SLinus Torvalds bad_fork_cleanup_files:
13031da177e4SLinus Torvalds 	exit_files(p); /* blocking */
13041da177e4SLinus Torvalds bad_fork_cleanup_semundo:
13051da177e4SLinus Torvalds 	exit_sem(p);
13061da177e4SLinus Torvalds bad_fork_cleanup_audit:
13071da177e4SLinus Torvalds 	audit_free(p);
13081da177e4SLinus Torvalds bad_fork_cleanup_policy:
13091da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1310f0be3d32SLee Schermerhorn 	mpol_put(p->mempolicy);
1311b4f48b63SPaul Menage bad_fork_cleanup_cgroup:
13121da177e4SLinus Torvalds #endif
1313b4f48b63SPaul Menage 	cgroup_exit(p, cgroup_callbacks_done);
131435df17c5SShailabh Nagar 	delayacct_tsk_free(p);
13151da177e4SLinus Torvalds 	if (p->binfmt)
13161da177e4SLinus Torvalds 		module_put(p->binfmt->module);
13171da177e4SLinus Torvalds bad_fork_cleanup_put_domain:
1318a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
13191da177e4SLinus Torvalds bad_fork_cleanup_count:
1320d84f4f99SDavid Howells 	atomic_dec(&p->cred->user->processes);
13213b11a1deSDavid Howells 	put_cred(p->real_cred);
1322f1752eecSDavid Howells 	put_cred(p->cred);
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);
142509a05394SRoland McGrath 		tracehook_report_clone(trace, 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,
1477aa1757f9SOleg Nesterov 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU,
147820c2df83SPaul Mundt 			sighand_ctor);
14791da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
14801da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
148120c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
14821da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
14831da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
148420c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
14851da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
14861da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
148720c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
14881da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
14895fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
149020c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, 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 
1547cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FS) &&
154899d1419dSJANAK DESAI 	    (fs && atomic_read(&fs->count) > 1)) {
154999d1419dSJANAK DESAI 		*new_fsp = __copy_fs_struct(current->fs);
155099d1419dSJANAK DESAI 		if (!*new_fsp)
155199d1419dSJANAK DESAI 			return -ENOMEM;
155299d1419dSJANAK DESAI 	}
1553cf2e340fSJANAK DESAI 
1554cf2e340fSJANAK DESAI 	return 0;
1555cf2e340fSJANAK DESAI }
1556cf2e340fSJANAK DESAI 
1557cf2e340fSJANAK DESAI /*
1558dae3c5a0SOleg Nesterov  * Unsharing of sighand is not supported yet
1559cf2e340fSJANAK DESAI  */
1560cf2e340fSJANAK DESAI static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
1561cf2e340fSJANAK DESAI {
1562cf2e340fSJANAK DESAI 	struct sighand_struct *sigh = current->sighand;
1563cf2e340fSJANAK DESAI 
1564dae3c5a0SOleg Nesterov 	if ((unshare_flags & CLONE_SIGHAND) && atomic_read(&sigh->count) > 1)
1565cf2e340fSJANAK DESAI 		return -EINVAL;
1566cf2e340fSJANAK DESAI 	else
1567cf2e340fSJANAK DESAI 		return 0;
1568cf2e340fSJANAK DESAI }
1569cf2e340fSJANAK DESAI 
1570cf2e340fSJANAK DESAI /*
1571a0a7ec30SJANAK DESAI  * Unshare vm if it is being shared
1572cf2e340fSJANAK DESAI  */
1573cf2e340fSJANAK DESAI static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp)
1574cf2e340fSJANAK DESAI {
1575cf2e340fSJANAK DESAI 	struct mm_struct *mm = current->mm;
1576cf2e340fSJANAK DESAI 
1577cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_VM) &&
1578a0a7ec30SJANAK DESAI 	    (mm && atomic_read(&mm->mm_users) > 1)) {
15792d61b867SOleg Nesterov 		return -EINVAL;
1580a0a7ec30SJANAK DESAI 	}
1581cf2e340fSJANAK DESAI 
1582cf2e340fSJANAK DESAI 	return 0;
1583cf2e340fSJANAK DESAI }
1584cf2e340fSJANAK DESAI 
1585cf2e340fSJANAK DESAI /*
1586a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1587cf2e340fSJANAK DESAI  */
1588cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1589cf2e340fSJANAK DESAI {
1590cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1591a016f338SJANAK DESAI 	int error = 0;
1592cf2e340fSJANAK DESAI 
1593cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1594a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1595a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1596a016f338SJANAK DESAI 		if (!*new_fdp)
1597a016f338SJANAK DESAI 			return error;
1598a016f338SJANAK DESAI 	}
1599cf2e340fSJANAK DESAI 
1600cf2e340fSJANAK DESAI 	return 0;
1601cf2e340fSJANAK DESAI }
1602cf2e340fSJANAK DESAI 
1603cf2e340fSJANAK DESAI /*
1604cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1605cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1606cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1607cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1608cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1609cf2e340fSJANAK DESAI  * task_struct.
1610cf2e340fSJANAK DESAI  */
16116559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1612cf2e340fSJANAK DESAI {
1613cf2e340fSJANAK DESAI 	int err = 0;
1614cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1615dae3c5a0SOleg Nesterov 	struct sighand_struct *new_sigh = NULL;
1616cf2e340fSJANAK DESAI 	struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
1617cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1618cf7b708cSPavel Emelyanov 	struct nsproxy *new_nsproxy = NULL;
16199edff4abSManfred Spraul 	int do_sysvsem = 0;
1620cf2e340fSJANAK DESAI 
1621cf2e340fSJANAK DESAI 	check_unshare_flags(&unshare_flags);
1622cf2e340fSJANAK DESAI 
162306f9d4f9SEric W. Biederman 	/* Return -EINVAL for all unsupported flags */
162406f9d4f9SEric W. Biederman 	err = -EINVAL;
162506f9d4f9SEric W. Biederman 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
162625b21cb2SKirill Korotaev 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
162718b6e041SSerge Hallyn 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET))
162806f9d4f9SEric W. Biederman 		goto bad_unshare_out;
162906f9d4f9SEric W. Biederman 
16306013f67fSManfred Spraul 	/*
16316013f67fSManfred Spraul 	 * CLONE_NEWIPC must also detach from the undolist: after switching
16326013f67fSManfred Spraul 	 * to a new ipc namespace, the semaphore arrays from the old
16336013f67fSManfred Spraul 	 * namespace are unreachable.
16346013f67fSManfred Spraul 	 */
16356013f67fSManfred Spraul 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
16369edff4abSManfred Spraul 		do_sysvsem = 1;
1637cf2e340fSJANAK DESAI 	if ((err = unshare_thread(unshare_flags)))
1638cf2e340fSJANAK DESAI 		goto bad_unshare_out;
1639cf2e340fSJANAK DESAI 	if ((err = unshare_fs(unshare_flags, &new_fs)))
1640cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_thread;
1641cf2e340fSJANAK DESAI 	if ((err = unshare_sighand(unshare_flags, &new_sigh)))
1642e3222c4eSBadari Pulavarty 		goto bad_unshare_cleanup_fs;
1643cf2e340fSJANAK DESAI 	if ((err = unshare_vm(unshare_flags, &new_mm)))
1644cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_sigh;
1645cf2e340fSJANAK DESAI 	if ((err = unshare_fd(unshare_flags, &new_fd)))
1646cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_vm;
1647e3222c4eSBadari Pulavarty 	if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
1648e3222c4eSBadari Pulavarty 			new_fs)))
16499edff4abSManfred Spraul 		goto bad_unshare_cleanup_fd;
1650cf2e340fSJANAK DESAI 
16519edff4abSManfred Spraul 	if (new_fs ||  new_mm || new_fd || do_sysvsem || new_nsproxy) {
16529edff4abSManfred Spraul 		if (do_sysvsem) {
16539edff4abSManfred Spraul 			/*
16549edff4abSManfred Spraul 			 * CLONE_SYSVSEM is equivalent to sys_exit().
16559edff4abSManfred Spraul 			 */
16569edff4abSManfred Spraul 			exit_sem(current);
16579edff4abSManfred Spraul 		}
1658ab516013SSerge E. Hallyn 
1659c0b2fc31SSerge Hallyn 		if (new_nsproxy) {
1660cf7b708cSPavel Emelyanov 			switch_task_namespaces(current, new_nsproxy);
1661cf7b708cSPavel Emelyanov 			new_nsproxy = NULL;
1662c0b2fc31SSerge Hallyn 		}
1663cf2e340fSJANAK DESAI 
1664cf7b708cSPavel Emelyanov 		task_lock(current);
1665cf7b708cSPavel Emelyanov 
1666cf2e340fSJANAK DESAI 		if (new_fs) {
1667cf2e340fSJANAK DESAI 			fs = current->fs;
1668cf2e340fSJANAK DESAI 			current->fs = new_fs;
1669cf2e340fSJANAK DESAI 			new_fs = fs;
1670cf2e340fSJANAK DESAI 		}
1671cf2e340fSJANAK DESAI 
1672cf2e340fSJANAK DESAI 		if (new_mm) {
1673cf2e340fSJANAK DESAI 			mm = current->mm;
1674cf2e340fSJANAK DESAI 			active_mm = current->active_mm;
1675cf2e340fSJANAK DESAI 			current->mm = new_mm;
1676cf2e340fSJANAK DESAI 			current->active_mm = new_mm;
1677cf2e340fSJANAK DESAI 			activate_mm(active_mm, new_mm);
1678cf2e340fSJANAK DESAI 			new_mm = mm;
1679cf2e340fSJANAK DESAI 		}
1680cf2e340fSJANAK DESAI 
1681cf2e340fSJANAK DESAI 		if (new_fd) {
1682cf2e340fSJANAK DESAI 			fd = current->files;
1683cf2e340fSJANAK DESAI 			current->files = new_fd;
1684cf2e340fSJANAK DESAI 			new_fd = fd;
1685cf2e340fSJANAK DESAI 		}
1686cf2e340fSJANAK DESAI 
1687cf2e340fSJANAK DESAI 		task_unlock(current);
1688cf2e340fSJANAK DESAI 	}
1689cf2e340fSJANAK DESAI 
1690c0b2fc31SSerge Hallyn 	if (new_nsproxy)
1691444f378bSLinus Torvalds 		put_nsproxy(new_nsproxy);
1692c0b2fc31SSerge Hallyn 
1693cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
1694cf2e340fSJANAK DESAI 	if (new_fd)
1695cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
1696cf2e340fSJANAK DESAI 
1697cf2e340fSJANAK DESAI bad_unshare_cleanup_vm:
1698cf2e340fSJANAK DESAI 	if (new_mm)
1699cf2e340fSJANAK DESAI 		mmput(new_mm);
1700cf2e340fSJANAK DESAI 
1701cf2e340fSJANAK DESAI bad_unshare_cleanup_sigh:
1702cf2e340fSJANAK DESAI 	if (new_sigh)
1703cf2e340fSJANAK DESAI 		if (atomic_dec_and_test(&new_sigh->count))
1704cf2e340fSJANAK DESAI 			kmem_cache_free(sighand_cachep, new_sigh);
1705cf2e340fSJANAK DESAI 
1706cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
1707cf2e340fSJANAK DESAI 	if (new_fs)
1708cf2e340fSJANAK DESAI 		put_fs_struct(new_fs);
1709cf2e340fSJANAK DESAI 
1710cf2e340fSJANAK DESAI bad_unshare_cleanup_thread:
1711cf2e340fSJANAK DESAI bad_unshare_out:
1712cf2e340fSJANAK DESAI 	return err;
1713cf2e340fSJANAK DESAI }
17143b125388SAl Viro 
17153b125388SAl Viro /*
17163b125388SAl Viro  *	Helper to unshare the files of the current task.
17173b125388SAl Viro  *	We don't want to expose copy_files internals to
17183b125388SAl Viro  *	the exec layer of the kernel.
17193b125388SAl Viro  */
17203b125388SAl Viro 
17213b125388SAl Viro int unshare_files(struct files_struct **displaced)
17223b125388SAl Viro {
17233b125388SAl Viro 	struct task_struct *task = current;
172450704516SAl Viro 	struct files_struct *copy = NULL;
17253b125388SAl Viro 	int error;
17263b125388SAl Viro 
17273b125388SAl Viro 	error = unshare_fd(CLONE_FILES, &copy);
17283b125388SAl Viro 	if (error || !copy) {
17293b125388SAl Viro 		*displaced = NULL;
17303b125388SAl Viro 		return error;
17313b125388SAl Viro 	}
17323b125388SAl Viro 	*displaced = task->files;
17333b125388SAl Viro 	task_lock(task);
17343b125388SAl Viro 	task->files = copy;
17353b125388SAl Viro 	task_unlock(task);
17363b125388SAl Viro 	return 0;
17373b125388SAl Viro }
1738