xref: /openbmc/linux/kernel/fork.c (revision 999d9fc1670bc082928b93b11d1f2e0e417d973c)
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>
301da177e4SLinus Torvalds #include <linux/fs.h>
31ab516013SSerge E. Hallyn #include <linux/nsproxy.h>
32c59ede7bSRandy.Dunlap #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/cpu.h>
34b4f48b63SPaul Menage #include <linux/cgroup.h>
351da177e4SLinus Torvalds #include <linux/security.h>
36a1e78772SMel Gorman #include <linux/hugetlb.h>
371da177e4SLinus Torvalds #include <linux/swap.h>
381da177e4SLinus Torvalds #include <linux/syscalls.h>
391da177e4SLinus Torvalds #include <linux/jiffies.h>
401da177e4SLinus Torvalds #include <linux/futex.h>
417c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h>
42ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
431da177e4SLinus Torvalds #include <linux/ptrace.h>
441da177e4SLinus Torvalds #include <linux/mount.h>
451da177e4SLinus Torvalds #include <linux/audit.h>
4678fb7466SPavel Emelianov #include <linux/memcontrol.h>
471da177e4SLinus Torvalds #include <linux/profile.h>
481da177e4SLinus Torvalds #include <linux/rmap.h>
491da177e4SLinus Torvalds #include <linux/acct.h>
508f0ab514SJay Lan #include <linux/tsacct_kern.h>
519f46080cSMatt Helsley #include <linux/cn_proc.h>
52ba96a0c8SRafael J. Wysocki #include <linux/freezer.h>
53ca74e92bSShailabh Nagar #include <linux/delayacct.h>
54ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h>
550a425405SArjan van de Ven #include <linux/random.h>
56522ed776SMiloslav Trmac #include <linux/tty.h>
576f4e6433SPavel Emelyanov #include <linux/proc_fs.h>
58fd0928dfSJens Axboe #include <linux/blkdev.h>
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #include <asm/pgtable.h>
611da177e4SLinus Torvalds #include <asm/pgalloc.h>
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds #include <asm/mmu_context.h>
641da177e4SLinus Torvalds #include <asm/cacheflush.h>
651da177e4SLinus Torvalds #include <asm/tlbflush.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
691da177e4SLinus Torvalds  */
701da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
711da177e4SLinus Torvalds int nr_threads; 		/* The idle threads do not count.. */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds int nr_processes(void)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	int cpu;
821da177e4SLinus Torvalds 	int total = 0;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	for_each_online_cpu(cpu)
851da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	return total;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
911da177e4SLinus Torvalds # define alloc_task_struct()	kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
921da177e4SLinus Torvalds # define free_task_struct(tsk)	kmem_cache_free(task_struct_cachep, (tsk))
93e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep;
941da177e4SLinus Torvalds #endif
951da177e4SLinus Torvalds 
96b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR
97b69c49b7SFUJITA Tomonori static inline struct thread_info *alloc_thread_info(struct task_struct *tsk)
98b69c49b7SFUJITA Tomonori {
99b69c49b7SFUJITA Tomonori #ifdef CONFIG_DEBUG_STACK_USAGE
100b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL | __GFP_ZERO;
101b69c49b7SFUJITA Tomonori #else
102b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL;
103b69c49b7SFUJITA Tomonori #endif
104b69c49b7SFUJITA Tomonori 	return (struct thread_info *)__get_free_pages(mask, THREAD_SIZE_ORDER);
105b69c49b7SFUJITA Tomonori }
106b69c49b7SFUJITA Tomonori 
107b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti)
108b69c49b7SFUJITA Tomonori {
109b69c49b7SFUJITA Tomonori 	free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
110b69c49b7SFUJITA Tomonori }
111b69c49b7SFUJITA Tomonori #endif
112b69c49b7SFUJITA Tomonori 
1131da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
114e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
117e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
120e18b890bSChristoph Lameter struct kmem_cache *files_cachep;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
123e18b890bSChristoph Lameter struct kmem_cache *fs_cachep;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
126e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
129e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1321da177e4SLinus Torvalds {
1333e26c149SPeter Zijlstra 	prop_local_destroy_single(&tsk->dirties);
134f7e4217bSRoman Zippel 	free_thread_info(tsk->stack);
13523f78d4aSIngo Molnar 	rt_mutex_debug_task_free(tsk);
1361da177e4SLinus Torvalds 	free_task_struct(tsk);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1391da177e4SLinus Torvalds 
140158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
1411da177e4SLinus Torvalds {
142270f722dSEugene Teo 	WARN_ON(!tsk->exit_state);
1431da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1441da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	security_task_free(tsk);
1471da177e4SLinus Torvalds 	free_uid(tsk->user);
1481da177e4SLinus Torvalds 	put_group_info(tsk->group_info);
14935df17c5SShailabh Nagar 	delayacct_tsk_free(tsk);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
1521da177e4SLinus Torvalds 		free_task(tsk);
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1552adee9b3SSuresh Siddha /*
1562adee9b3SSuresh Siddha  * macro override instead of weak attribute alias, to workaround
1572adee9b3SSuresh Siddha  * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
1582adee9b3SSuresh Siddha  */
1592adee9b3SSuresh Siddha #ifndef arch_task_cache_init
1602adee9b3SSuresh Siddha #define arch_task_cache_init()
1612adee9b3SSuresh Siddha #endif
16261c4628bSSuresh Siddha 
1631da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
1661da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
1671da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
1681da177e4SLinus Torvalds #endif
1691da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
1701da177e4SLinus Torvalds 	task_struct_cachep =
1711da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
17220c2df83SPaul Mundt 			ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL);
1731da177e4SLinus Torvalds #endif
1741da177e4SLinus Torvalds 
17561c4628bSSuresh Siddha 	/* do the arch specific task caches init */
17661c4628bSSuresh Siddha 	arch_task_cache_init();
17761c4628bSSuresh Siddha 
1781da177e4SLinus Torvalds 	/*
1791da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
1801da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
1811da177e4SLinus Torvalds 	 * of memory.
1821da177e4SLinus Torvalds 	 */
1831da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	/*
1861da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
1871da177e4SLinus Torvalds 	 */
1881da177e4SLinus Torvalds 	if(max_threads < 20)
1891da177e4SLinus Torvalds 		max_threads = 20;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
1921da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
1931da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
1941da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
19761c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
19861c4628bSSuresh Siddha 					       struct task_struct *src)
19961c4628bSSuresh Siddha {
20061c4628bSSuresh Siddha 	*dst = *src;
20161c4628bSSuresh Siddha 	return 0;
20261c4628bSSuresh Siddha }
20361c4628bSSuresh Siddha 
2041da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	struct task_struct *tsk;
2071da177e4SLinus Torvalds 	struct thread_info *ti;
2083e26c149SPeter Zijlstra 	int err;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	prepare_to_copy(orig);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	tsk = alloc_task_struct();
2131da177e4SLinus Torvalds 	if (!tsk)
2141da177e4SLinus Torvalds 		return NULL;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	ti = alloc_thread_info(tsk);
2171da177e4SLinus Torvalds 	if (!ti) {
2181da177e4SLinus Torvalds 		free_task_struct(tsk);
2191da177e4SLinus Torvalds 		return NULL;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
22261c4628bSSuresh Siddha  	err = arch_dup_task_struct(tsk, orig);
22361c4628bSSuresh Siddha 	if (err)
22461c4628bSSuresh Siddha 		goto out;
22561c4628bSSuresh Siddha 
226f7e4217bSRoman Zippel 	tsk->stack = ti;
2273e26c149SPeter Zijlstra 
2283e26c149SPeter Zijlstra 	err = prop_local_init_single(&tsk->dirties);
22961c4628bSSuresh Siddha 	if (err)
23061c4628bSSuresh Siddha 		goto out;
2313e26c149SPeter Zijlstra 
23210ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
2331da177e4SLinus Torvalds 
2340a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
2350a425405SArjan van de Ven 	tsk->stack_canary = get_random_int();
2360a425405SArjan van de Ven #endif
2370a425405SArjan van de Ven 
2381da177e4SLinus Torvalds 	/* One for us, one for whoever does the "release_task()" (usually parent) */
2391da177e4SLinus Torvalds 	atomic_set(&tsk->usage,2);
2404b5d37acSGiancarlo Formicuccia 	atomic_set(&tsk->fs_excl, 0);
2416c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
2422056a782SJens Axboe 	tsk->btrace_seq = 0;
2436c5c9341SAlexey Dobriyan #endif
244a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
2451da177e4SLinus Torvalds 	return tsk;
24661c4628bSSuresh Siddha 
24761c4628bSSuresh Siddha out:
24861c4628bSSuresh Siddha 	free_thread_info(ti);
24961c4628bSSuresh Siddha 	free_task_struct(tsk);
25061c4628bSSuresh Siddha 	return NULL;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds #ifdef CONFIG_MMU
254a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, **pprev;
2571da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
2581da177e4SLinus Torvalds 	int retval;
2591da177e4SLinus Torvalds 	unsigned long charge;
2601da177e4SLinus Torvalds 	struct mempolicy *pol;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
263ec8c0446SRalf Baechle 	flush_cache_dup_mm(oldmm);
264ad339451SIngo Molnar 	/*
265ad339451SIngo Molnar 	 * Not linked in yet - no deadlock potential:
266ad339451SIngo Molnar 	 */
267ad339451SIngo Molnar 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
2687ee78232SHugh Dickins 
2691da177e4SLinus Torvalds 	mm->locked_vm = 0;
2701da177e4SLinus Torvalds 	mm->mmap = NULL;
2711da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
2721da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
2731363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
2741da177e4SLinus Torvalds 	mm->map_count = 0;
2751da177e4SLinus Torvalds 	cpus_clear(mm->cpu_vm_mask);
2761da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
2771da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
2781da177e4SLinus Torvalds 	rb_parent = NULL;
2791da177e4SLinus Torvalds 	pprev = &mm->mmap;
2801da177e4SLinus Torvalds 
281fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
2821da177e4SLinus Torvalds 		struct file *file;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
2853b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
2863b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
287ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
2883b6bfcdbSHugh Dickins 								-pages);
2891da177e4SLinus Torvalds 			continue;
2901da177e4SLinus Torvalds 		}
2911da177e4SLinus Torvalds 		charge = 0;
2921da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
2931da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
2941da177e4SLinus Torvalds 			if (security_vm_enough_memory(len))
2951da177e4SLinus Torvalds 				goto fail_nomem;
2961da177e4SLinus Torvalds 			charge = len;
2971da177e4SLinus Torvalds 		}
298e94b1766SChristoph Lameter 		tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
2991da177e4SLinus Torvalds 		if (!tmp)
3001da177e4SLinus Torvalds 			goto fail_nomem;
3011da177e4SLinus Torvalds 		*tmp = *mpnt;
302846a16bfSLee Schermerhorn 		pol = mpol_dup(vma_policy(mpnt));
3031da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
3041da177e4SLinus Torvalds 		if (IS_ERR(pol))
3051da177e4SLinus Torvalds 			goto fail_nomem_policy;
3061da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
3071da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
3081da177e4SLinus Torvalds 		tmp->vm_mm = mm;
3091da177e4SLinus Torvalds 		tmp->vm_next = NULL;
3101da177e4SLinus Torvalds 		anon_vma_link(tmp);
3111da177e4SLinus Torvalds 		file = tmp->vm_file;
3121da177e4SLinus Torvalds 		if (file) {
313f3a43f3fSJosef "Jeff" Sipek 			struct inode *inode = file->f_path.dentry->d_inode;
3141da177e4SLinus Torvalds 			get_file(file);
3151da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
3161da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 			/* insert tmp into the share list, just after mpnt */
3191da177e4SLinus Torvalds 			spin_lock(&file->f_mapping->i_mmap_lock);
3201da177e4SLinus Torvalds 			tmp->vm_truncate_count = mpnt->vm_truncate_count;
3211da177e4SLinus Torvalds 			flush_dcache_mmap_lock(file->f_mapping);
3221da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
3231da177e4SLinus Torvalds 			flush_dcache_mmap_unlock(file->f_mapping);
3241da177e4SLinus Torvalds 			spin_unlock(&file->f_mapping->i_mmap_lock);
3251da177e4SLinus Torvalds 		}
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 		/*
328a1e78772SMel Gorman 		 * Clear hugetlb-related page reserves for children. This only
329a1e78772SMel Gorman 		 * affects MAP_PRIVATE mappings. Faults generated by the child
330a1e78772SMel Gorman 		 * are not guaranteed to succeed, even if read-only
331a1e78772SMel Gorman 		 */
332a1e78772SMel Gorman 		if (is_vm_hugetlb_page(tmp))
333a1e78772SMel Gorman 			reset_vma_resv_huge_pages(tmp);
334a1e78772SMel Gorman 
335a1e78772SMel Gorman 		/*
3367ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
3371da177e4SLinus Torvalds 		 */
3381da177e4SLinus Torvalds 		*pprev = tmp;
3391da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
3421da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
3431da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		mm->map_count++;
3460b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
3491da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		if (retval)
3521da177e4SLinus Torvalds 			goto out;
3531da177e4SLinus Torvalds 	}
354d6dd61c8SJeremy Fitzhardinge 	/* a new mm has just been created */
355d6dd61c8SJeremy Fitzhardinge 	arch_dup_mmap(oldmm, mm);
3561da177e4SLinus Torvalds 	retval = 0;
3571da177e4SLinus Torvalds out:
3587ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
359fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
3601da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
3611da177e4SLinus Torvalds 	return retval;
3621da177e4SLinus Torvalds fail_nomem_policy:
3631da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
3641da177e4SLinus Torvalds fail_nomem:
3651da177e4SLinus Torvalds 	retval = -ENOMEM;
3661da177e4SLinus Torvalds 	vm_unacct_memory(charge);
3671da177e4SLinus Torvalds 	goto out;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
3731da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
3741da177e4SLinus Torvalds 		return -ENOMEM;
3751da177e4SLinus Torvalds 	return 0;
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm)
3791da177e4SLinus Torvalds {
3805e541973SBenjamin Herrenschmidt 	pgd_free(mm, mm->pgd);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds #else
3831da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
3841da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
3851da177e4SLinus Torvalds #define mm_free_pgd(mm)
3861da177e4SLinus Torvalds #endif /* CONFIG_MMU */
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
3891da177e4SLinus Torvalds 
390e94b1766SChristoph Lameter #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
3911da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds #include <linux/init_task.h>
3941da177e4SLinus Torvalds 
39578fb7466SPavel Emelianov static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
3981da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
3991da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
4001da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
4013cb4a0bbSKawai, Hidehiro 	mm->flags = (current->mm) ? current->mm->flags
4023cb4a0bbSKawai, Hidehiro 				  : MMF_DUMP_FILTER_DEFAULT;
403*999d9fc1SOleg Nesterov 	mm->core_state = NULL;
4041da177e4SLinus Torvalds 	mm->nr_ptes = 0;
4054294621fSHugh Dickins 	set_mm_counter(mm, file_rss, 0);
406404351e6SHugh Dickins 	set_mm_counter(mm, anon_rss, 0);
4071da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
4081da177e4SLinus Torvalds 	rwlock_init(&mm->ioctx_list_lock);
4091da177e4SLinus Torvalds 	mm->ioctx_list = NULL;
4101da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
4111363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
412cf475ad2SBalbir Singh 	mm_init_owner(mm, p);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
4151da177e4SLinus Torvalds 		mm->def_flags = 0;
4161da177e4SLinus Torvalds 		return mm;
4171da177e4SLinus Torvalds 	}
41878fb7466SPavel Emelianov 
4191da177e4SLinus Torvalds 	free_mm(mm);
4201da177e4SLinus Torvalds 	return NULL;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds /*
4241da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
4251da177e4SLinus Torvalds  */
4261da177e4SLinus Torvalds struct mm_struct * mm_alloc(void)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct mm_struct * mm;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	mm = allocate_mm();
4311da177e4SLinus Torvalds 	if (mm) {
4321da177e4SLinus Torvalds 		memset(mm, 0, sizeof(*mm));
43378fb7466SPavel Emelianov 		mm = mm_init(mm, current);
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 	return mm;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /*
4391da177e4SLinus Torvalds  * Called when the last reference to the mm
4401da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
4411da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
4421da177e4SLinus Torvalds  */
4437ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
4461da177e4SLinus Torvalds 	mm_free_pgd(mm);
4471da177e4SLinus Torvalds 	destroy_context(mm);
4481da177e4SLinus Torvalds 	free_mm(mm);
4491da177e4SLinus Torvalds }
4506d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
4541da177e4SLinus Torvalds  */
4551da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
4561da177e4SLinus Torvalds {
4570ae26f1bSAndrew Morton 	might_sleep();
4580ae26f1bSAndrew Morton 
4591da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
4601da177e4SLinus Torvalds 		exit_aio(mm);
4611da177e4SLinus Torvalds 		exit_mmap(mm);
462925d1c40SMatt Helsley 		set_mm_exe_file(mm, NULL);
4631da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
4641da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
4651da177e4SLinus Torvalds 			list_del(&mm->mmlist);
4661da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 		put_swap_token(mm);
4691da177e4SLinus Torvalds 		mmdrop(mm);
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /**
4751da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
4761da177e4SLinus Torvalds  *
477246bb0b1SOleg Nesterov  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
4781da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
4791da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
4801da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
4811da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
4821da177e4SLinus Torvalds  */
4831da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	struct mm_struct *mm;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	task_lock(task);
4881da177e4SLinus Torvalds 	mm = task->mm;
4891da177e4SLinus Torvalds 	if (mm) {
490246bb0b1SOleg Nesterov 		if (task->flags & PF_KTHREAD)
4911da177e4SLinus Torvalds 			mm = NULL;
4921da177e4SLinus Torvalds 		else
4931da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds 	task_unlock(task);
4961da177e4SLinus Torvalds 	return mm;
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
5011da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
5021da177e4SLinus Torvalds  * error success whatever.
5031da177e4SLinus Torvalds  *
5041da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
5051da177e4SLinus Torvalds  * from the current process.
5061da177e4SLinus Torvalds  *
5071da177e4SLinus Torvalds  * This difference is important for error handling, when we
5081da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
5091da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
5101da177e4SLinus Torvalds  * restoring the old one. . .
5111da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
5121da177e4SLinus Torvalds  */
5131da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	struct completion *vfork_done = tsk->vfork_done;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	/* Get rid of any cached register state */
5181da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	/* notify parent sleeping on vfork() */
5211da177e4SLinus Torvalds 	if (vfork_done) {
5221da177e4SLinus Torvalds 		tsk->vfork_done = NULL;
5231da177e4SLinus Torvalds 		complete(vfork_done);
5241da177e4SLinus Torvalds 	}
525fec1d011SRoland McGrath 
526fec1d011SRoland McGrath 	/*
527fec1d011SRoland McGrath 	 * If we're exiting normally, clear a user-space tid field if
528fec1d011SRoland McGrath 	 * requested.  We leave this alone when dying by signal, to leave
529fec1d011SRoland McGrath 	 * the value intact in a core dump, and to save the unnecessary
530fec1d011SRoland McGrath 	 * trouble otherwise.  Userland only wants this done for a sys_exit.
531fec1d011SRoland McGrath 	 */
532fec1d011SRoland McGrath 	if (tsk->clear_child_tid
533fec1d011SRoland McGrath 	    && !(tsk->flags & PF_SIGNALED)
534fec1d011SRoland McGrath 	    && atomic_read(&mm->mm_users) > 1) {
5351da177e4SLinus Torvalds 		u32 __user * tidptr = tsk->clear_child_tid;
5361da177e4SLinus Torvalds 		tsk->clear_child_tid = NULL;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 		/*
5391da177e4SLinus Torvalds 		 * We don't check the error code - if userspace has
5401da177e4SLinus Torvalds 		 * not set up a proper pointer then tough luck.
5411da177e4SLinus Torvalds 		 */
5421da177e4SLinus Torvalds 		put_user(0, tidptr);
5431da177e4SLinus Torvalds 		sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
547a0a7ec30SJANAK DESAI /*
548a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
549a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
550a0a7ec30SJANAK DESAI  */
551402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk)
552a0a7ec30SJANAK DESAI {
553a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
554a0a7ec30SJANAK DESAI 	int err;
555a0a7ec30SJANAK DESAI 
556a0a7ec30SJANAK DESAI 	if (!oldmm)
557a0a7ec30SJANAK DESAI 		return NULL;
558a0a7ec30SJANAK DESAI 
559a0a7ec30SJANAK DESAI 	mm = allocate_mm();
560a0a7ec30SJANAK DESAI 	if (!mm)
561a0a7ec30SJANAK DESAI 		goto fail_nomem;
562a0a7ec30SJANAK DESAI 
563a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
564a0a7ec30SJANAK DESAI 
5657602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
5667602bdf2SAshwin Chaugule 	mm->token_priority = 0;
5677602bdf2SAshwin Chaugule 	mm->last_interval = 0;
5687602bdf2SAshwin Chaugule 
56978fb7466SPavel Emelianov 	if (!mm_init(mm, tsk))
570a0a7ec30SJANAK DESAI 		goto fail_nomem;
571a0a7ec30SJANAK DESAI 
572a0a7ec30SJANAK DESAI 	if (init_new_context(tsk, mm))
573a0a7ec30SJANAK DESAI 		goto fail_nocontext;
574a0a7ec30SJANAK DESAI 
575925d1c40SMatt Helsley 	dup_mm_exe_file(oldmm, mm);
576925d1c40SMatt Helsley 
577a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
578a0a7ec30SJANAK DESAI 	if (err)
579a0a7ec30SJANAK DESAI 		goto free_pt;
580a0a7ec30SJANAK DESAI 
581a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
582a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
583a0a7ec30SJANAK DESAI 
584a0a7ec30SJANAK DESAI 	return mm;
585a0a7ec30SJANAK DESAI 
586a0a7ec30SJANAK DESAI free_pt:
587a0a7ec30SJANAK DESAI 	mmput(mm);
588a0a7ec30SJANAK DESAI 
589a0a7ec30SJANAK DESAI fail_nomem:
590a0a7ec30SJANAK DESAI 	return NULL;
591a0a7ec30SJANAK DESAI 
592a0a7ec30SJANAK DESAI fail_nocontext:
593a0a7ec30SJANAK DESAI 	/*
594a0a7ec30SJANAK DESAI 	 * If init_new_context() failed, we cannot use mmput() to free the mm
595a0a7ec30SJANAK DESAI 	 * because it calls destroy_context()
596a0a7ec30SJANAK DESAI 	 */
597a0a7ec30SJANAK DESAI 	mm_free_pgd(mm);
598a0a7ec30SJANAK DESAI 	free_mm(mm);
599a0a7ec30SJANAK DESAI 	return NULL;
600a0a7ec30SJANAK DESAI }
601a0a7ec30SJANAK DESAI 
6021da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
6031da177e4SLinus Torvalds {
6041da177e4SLinus Torvalds 	struct mm_struct * mm, *oldmm;
6051da177e4SLinus Torvalds 	int retval;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
6081da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	tsk->mm = NULL;
6111da177e4SLinus Torvalds 	tsk->active_mm = NULL;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	/*
6141da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
6151da177e4SLinus Torvalds 	 *
6161da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
6171da177e4SLinus Torvalds 	 */
6181da177e4SLinus Torvalds 	oldmm = current->mm;
6191da177e4SLinus Torvalds 	if (!oldmm)
6201da177e4SLinus Torvalds 		return 0;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
6231da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
6241da177e4SLinus Torvalds 		mm = oldmm;
6251da177e4SLinus Torvalds 		goto good_mm;
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	retval = -ENOMEM;
629a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
6301da177e4SLinus Torvalds 	if (!mm)
6311da177e4SLinus Torvalds 		goto fail_nomem;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds good_mm:
6347602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
6357602bdf2SAshwin Chaugule 	mm->token_priority = 0;
6367602bdf2SAshwin Chaugule 	mm->last_interval = 0;
6377602bdf2SAshwin Chaugule 
6381da177e4SLinus Torvalds 	tsk->mm = mm;
6391da177e4SLinus Torvalds 	tsk->active_mm = mm;
6401da177e4SLinus Torvalds 	return 0;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds fail_nomem:
6431da177e4SLinus Torvalds 	return retval;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
646a39bc516SAlexey Dobriyan static struct fs_struct *__copy_fs_struct(struct fs_struct *old)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
6491da177e4SLinus Torvalds 	/* We don't need to lock fs - think why ;-) */
6501da177e4SLinus Torvalds 	if (fs) {
6511da177e4SLinus Torvalds 		atomic_set(&fs->count, 1);
6521da177e4SLinus Torvalds 		rwlock_init(&fs->lock);
6531da177e4SLinus Torvalds 		fs->umask = old->umask;
6541da177e4SLinus Torvalds 		read_lock(&old->lock);
6556ac08c39SJan Blunck 		fs->root = old->root;
6566ac08c39SJan Blunck 		path_get(&old->root);
6576ac08c39SJan Blunck 		fs->pwd = old->pwd;
6586ac08c39SJan Blunck 		path_get(&old->pwd);
6596ac08c39SJan Blunck 		if (old->altroot.dentry) {
6606ac08c39SJan Blunck 			fs->altroot = old->altroot;
6616ac08c39SJan Blunck 			path_get(&old->altroot);
6621da177e4SLinus Torvalds 		} else {
6636ac08c39SJan Blunck 			fs->altroot.mnt = NULL;
6646ac08c39SJan Blunck 			fs->altroot.dentry = NULL;
6651da177e4SLinus Torvalds 		}
6661da177e4SLinus Torvalds 		read_unlock(&old->lock);
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 	return fs;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds struct fs_struct *copy_fs_struct(struct fs_struct *old)
6721da177e4SLinus Torvalds {
6731da177e4SLinus Torvalds 	return __copy_fs_struct(old);
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(copy_fs_struct);
6771da177e4SLinus Torvalds 
678a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
6811da177e4SLinus Torvalds 		atomic_inc(&current->fs->count);
6821da177e4SLinus Torvalds 		return 0;
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 	tsk->fs = __copy_fs_struct(current->fs);
6851da177e4SLinus Torvalds 	if (!tsk->fs)
6861da177e4SLinus Torvalds 		return -ENOMEM;
6871da177e4SLinus Torvalds 	return 0;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
690a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
691a016f338SJANAK DESAI {
692a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
693a016f338SJANAK DESAI 	int error = 0;
694a016f338SJANAK DESAI 
695a016f338SJANAK DESAI 	/*
696a016f338SJANAK DESAI 	 * A background process may not have any files ...
697a016f338SJANAK DESAI 	 */
698a016f338SJANAK DESAI 	oldf = current->files;
699a016f338SJANAK DESAI 	if (!oldf)
700a016f338SJANAK DESAI 		goto out;
701a016f338SJANAK DESAI 
702a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
703a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
704a016f338SJANAK DESAI 		goto out;
705a016f338SJANAK DESAI 	}
706a016f338SJANAK DESAI 
707a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
708a016f338SJANAK DESAI 	if (!newf)
709a016f338SJANAK DESAI 		goto out;
710a016f338SJANAK DESAI 
711a016f338SJANAK DESAI 	tsk->files = newf;
712a016f338SJANAK DESAI 	error = 0;
713a016f338SJANAK DESAI out:
714a016f338SJANAK DESAI 	return error;
715a016f338SJANAK DESAI }
716a016f338SJANAK DESAI 
717fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
718fd0928dfSJens Axboe {
719fd0928dfSJens Axboe #ifdef CONFIG_BLOCK
720fd0928dfSJens Axboe 	struct io_context *ioc = current->io_context;
721fd0928dfSJens Axboe 
722fd0928dfSJens Axboe 	if (!ioc)
723fd0928dfSJens Axboe 		return 0;
724fadad878SJens Axboe 	/*
725fadad878SJens Axboe 	 * Share io context with parent, if CLONE_IO is set
726fadad878SJens Axboe 	 */
727fadad878SJens Axboe 	if (clone_flags & CLONE_IO) {
728fadad878SJens Axboe 		tsk->io_context = ioc_task_link(ioc);
729fadad878SJens Axboe 		if (unlikely(!tsk->io_context))
730fadad878SJens Axboe 			return -ENOMEM;
731fadad878SJens Axboe 	} else if (ioprio_valid(ioc->ioprio)) {
732fd0928dfSJens Axboe 		tsk->io_context = alloc_io_context(GFP_KERNEL, -1);
733fd0928dfSJens Axboe 		if (unlikely(!tsk->io_context))
734fd0928dfSJens Axboe 			return -ENOMEM;
735fd0928dfSJens Axboe 
736fd0928dfSJens Axboe 		tsk->io_context->ioprio = ioc->ioprio;
737fd0928dfSJens Axboe 	}
738fd0928dfSJens Axboe #endif
739fd0928dfSJens Axboe 	return 0;
740fd0928dfSJens Axboe }
741fd0928dfSJens Axboe 
742a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	struct sighand_struct *sig;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
7471da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
7481da177e4SLinus Torvalds 		return 0;
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
751e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
7521da177e4SLinus Torvalds 	if (!sig)
7531da177e4SLinus Torvalds 		return -ENOMEM;
7541da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
7551da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
7561da177e4SLinus Torvalds 	return 0;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
759a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
760c81addc9SOleg Nesterov {
761c81addc9SOleg Nesterov 	if (atomic_dec_and_test(&sighand->count))
762c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
763c81addc9SOleg Nesterov }
764c81addc9SOleg Nesterov 
765a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	struct signal_struct *sig;
7681da177e4SLinus Torvalds 	int ret;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if (clone_flags & CLONE_THREAD) {
7711da177e4SLinus Torvalds 		atomic_inc(&current->signal->count);
7721da177e4SLinus Torvalds 		atomic_inc(&current->signal->live);
7731da177e4SLinus Torvalds 		return 0;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
7761da177e4SLinus Torvalds 	tsk->signal = sig;
7771da177e4SLinus Torvalds 	if (!sig)
7781da177e4SLinus Torvalds 		return -ENOMEM;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	ret = copy_thread_group_keys(tsk);
7811da177e4SLinus Torvalds 	if (ret < 0) {
7821da177e4SLinus Torvalds 		kmem_cache_free(signal_cachep, sig);
7831da177e4SLinus Torvalds 		return ret;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
7871da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
7881da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
7891da177e4SLinus Torvalds 	sig->flags = 0;
7901da177e4SLinus Torvalds 	sig->group_exit_code = 0;
7911da177e4SLinus Torvalds 	sig->group_exit_task = NULL;
7921da177e4SLinus Torvalds 	sig->group_stop_count = 0;
793db51aeccSOleg Nesterov 	sig->curr_target = tsk;
7941da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
7951da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
7961da177e4SLinus Torvalds 
797c9cb2e3dSThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7982ff678b8SThomas Gleixner 	sig->it_real_incr.tv64 = 0;
7991da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	sig->it_virt_expires = cputime_zero;
8021da177e4SLinus Torvalds 	sig->it_virt_incr = cputime_zero;
8031da177e4SLinus Torvalds 	sig->it_prof_expires = cputime_zero;
8041da177e4SLinus Torvalds 	sig->it_prof_incr = cputime_zero;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	sig->leader = 0;	/* session leadership doesn't inherit */
807ab521dc0SEric W. Biederman 	sig->tty_old_pgrp = NULL;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
8109ac52315SLaurent Vivier 	sig->gtime = cputime_zero;
8119ac52315SLaurent Vivier 	sig->cgtime = cputime_zero;
8121da177e4SLinus Torvalds 	sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
8131da177e4SLinus Torvalds 	sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
8146eaeeabaSEric Dumazet 	sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0;
815172ba844SBalbir Singh 	sig->sum_sched_runtime = 0;
8161da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
8171da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
8181da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
819ad4ecbcbSShailabh Nagar 	taskstats_tgid_init(sig);
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	task_lock(current->group_leader);
8221da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
8231da177e4SLinus Torvalds 	task_unlock(current->group_leader);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
8261da177e4SLinus Torvalds 		/*
8271da177e4SLinus Torvalds 		 * New sole thread in the process gets an expiry time
8281da177e4SLinus Torvalds 		 * of the whole CPU time limit.
8291da177e4SLinus Torvalds 		 */
8301da177e4SLinus Torvalds 		tsk->it_prof_expires =
8311da177e4SLinus Torvalds 			secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
8321da177e4SLinus Torvalds 	}
8330e464814SKaiGai Kohei 	acct_init_pacct(&sig->pacct);
8341da177e4SLinus Torvalds 
835522ed776SMiloslav Trmac 	tty_audit_fork(sig);
836522ed776SMiloslav Trmac 
8371da177e4SLinus Torvalds 	return 0;
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds 
8406b3934efSOleg Nesterov void __cleanup_signal(struct signal_struct *sig)
8416b3934efSOleg Nesterov {
8426b3934efSOleg Nesterov 	exit_thread_group_keys(sig);
8436b3934efSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
8446b3934efSOleg Nesterov }
8456b3934efSOleg Nesterov 
846a39bc516SAlexey Dobriyan static void cleanup_signal(struct task_struct *tsk)
8476b3934efSOleg Nesterov {
8486b3934efSOleg Nesterov 	struct signal_struct *sig = tsk->signal;
8496b3934efSOleg Nesterov 
8506b3934efSOleg Nesterov 	atomic_dec(&sig->live);
8516b3934efSOleg Nesterov 
8526b3934efSOleg Nesterov 	if (atomic_dec_and_test(&sig->count))
8536b3934efSOleg Nesterov 		__cleanup_signal(sig);
8546b3934efSOleg Nesterov }
8556b3934efSOleg Nesterov 
856a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p)
8571da177e4SLinus Torvalds {
8581da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
8591da177e4SLinus Torvalds 
86083144186SRafael J. Wysocki 	new_flags &= ~PF_SUPERPRIV;
8611da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
8621da177e4SLinus Torvalds 	if (!(clone_flags & CLONE_PTRACE))
8631da177e4SLinus Torvalds 		p->ptrace = 0;
8641da177e4SLinus Torvalds 	p->flags = new_flags;
8652e131895SRafael J. Wysocki 	clear_freeze_flag(p);
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds asmlinkage long sys_set_tid_address(int __user *tidptr)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
8711da177e4SLinus Torvalds 
872b488893aSPavel Emelyanov 	return task_pid_vnr(current);
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
875a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p)
87623f78d4aSIngo Molnar {
87723f78d4aSIngo Molnar 	spin_lock_init(&p->pi_lock);
878e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES
87923f78d4aSIngo Molnar 	plist_head_init(&p->pi_waiters, &p->pi_lock);
88023f78d4aSIngo Molnar 	p->pi_blocked_on = NULL;
88123f78d4aSIngo Molnar #endif
88223f78d4aSIngo Molnar }
88323f78d4aSIngo Molnar 
884cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER
885cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
886cf475ad2SBalbir Singh {
887cf475ad2SBalbir Singh 	mm->owner = p;
888cf475ad2SBalbir Singh }
889cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */
890cf475ad2SBalbir Singh 
8911da177e4SLinus Torvalds /*
8921da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
8931da177e4SLinus Torvalds  * but does not actually start it yet.
8941da177e4SLinus Torvalds  *
8951da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
8961da177e4SLinus Torvalds  * parts of the process environment (as per the clone
8971da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
8981da177e4SLinus Torvalds  */
89936c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags,
9001da177e4SLinus Torvalds 					unsigned long stack_start,
9011da177e4SLinus Torvalds 					struct pt_regs *regs,
9021da177e4SLinus Torvalds 					unsigned long stack_size,
9031da177e4SLinus Torvalds 					int __user *child_tidptr,
90485868995SSukadev Bhattiprolu 					struct pid *pid)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	int retval;
907a24efe62SMariusz Kozlowski 	struct task_struct *p;
908b4f48b63SPaul Menage 	int cgroup_callbacks_done = 0;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
9111da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	/*
9141da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
9151da177e4SLinus Torvalds 	 * can only be started up within the thread group.
9161da177e4SLinus Torvalds 	 */
9171da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
9181da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	/*
9211da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
9221da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
9231da177e4SLinus Torvalds 	 * for various simplifications in other code.
9241da177e4SLinus Torvalds 	 */
9251da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
9261da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
9291da177e4SLinus Torvalds 	if (retval)
9301da177e4SLinus Torvalds 		goto fork_out;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	retval = -ENOMEM;
9331da177e4SLinus Torvalds 	p = dup_task_struct(current);
9341da177e4SLinus Torvalds 	if (!p)
9351da177e4SLinus Torvalds 		goto fork_out;
9361da177e4SLinus Torvalds 
937bea493a0SPeter Zijlstra 	rt_mutex_init_task(p);
938bea493a0SPeter Zijlstra 
939d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING
940de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
941de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
942de30a2b3SIngo Molnar #endif
9431da177e4SLinus Torvalds 	retval = -EAGAIN;
9441da177e4SLinus Torvalds 	if (atomic_read(&p->user->processes) >=
9451da177e4SLinus Torvalds 			p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
9461da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
947acce292cSCedric Le Goater 		    p->user != current->nsproxy->user_ns->root_user)
9481da177e4SLinus Torvalds 			goto bad_fork_free;
9491da177e4SLinus Torvalds 	}
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	atomic_inc(&p->user->__count);
9521da177e4SLinus Torvalds 	atomic_inc(&p->user->processes);
9531da177e4SLinus Torvalds 	get_group_info(p->group_info);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	/*
9561da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
9571da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
9581da177e4SLinus Torvalds 	 * to stop root fork bombs.
9591da177e4SLinus Torvalds 	 */
9601da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
9611da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
9621da177e4SLinus Torvalds 
963a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
9641da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	if (p->binfmt && !try_module_get(p->binfmt->module))
9671da177e4SLinus Torvalds 		goto bad_fork_cleanup_put_domain;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	p->did_exec = 0;
970ca74e92bSShailabh Nagar 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
9711da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
9721da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
9731da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
974e260be67SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
975e260be67SPaul E. McKenney 	p->rcu_read_lock_nesting = 0;
976e260be67SPaul E. McKenney 	p->rcu_flipctr_idx = 0;
977e260be67SPaul E. McKenney #endif /* #ifdef CONFIG_PREEMPT_RCU */
9781da177e4SLinus Torvalds 	p->vfork_done = NULL;
9791da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SIGPENDING);
9821da177e4SLinus Torvalds 	init_sigpending(&p->pending);
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	p->utime = cputime_zero;
9851da177e4SLinus Torvalds 	p->stime = cputime_zero;
9869ac52315SLaurent Vivier 	p->gtime = cputime_zero;
987c66f08beSMichael Neuling 	p->utimescaled = cputime_zero;
988c66f08beSMichael Neuling 	p->stimescaled = cputime_zero;
98973a2bcb0SPeter Zijlstra 	p->prev_utime = cputime_zero;
9909301899bSBalbir Singh 	p->prev_stime = cputime_zero;
991172ba844SBalbir Singh 
99282a1fcb9SIngo Molnar #ifdef CONFIG_DETECT_SOFTLOCKUP
99382a1fcb9SIngo Molnar 	p->last_switch_count = 0;
99482a1fcb9SIngo Molnar 	p->last_switch_timestamp = 0;
99582a1fcb9SIngo Molnar #endif
99682a1fcb9SIngo Molnar 
9974b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
9981da177e4SLinus Torvalds 	p->rchar = 0;		/* I/O counter: bytes read */
9991da177e4SLinus Torvalds 	p->wchar = 0;		/* I/O counter: bytes written */
10001da177e4SLinus Torvalds 	p->syscr = 0;		/* I/O counter: read syscalls */
10011da177e4SLinus Torvalds 	p->syscw = 0;		/* I/O counter: write syscalls */
10024b98d11bSAlexey Dobriyan #endif
10037c3ab738SAndrew Morton 	task_io_accounting_init(p);
10041da177e4SLinus Torvalds 	acct_clear_integrals(p);
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	p->it_virt_expires = cputime_zero;
10071da177e4SLinus Torvalds 	p->it_prof_expires = cputime_zero;
10081da177e4SLinus Torvalds 	p->it_sched_expires = 0;
10091da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->cpu_timers[0]);
10101da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->cpu_timers[1]);
10111da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->cpu_timers[2]);
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	p->lock_depth = -1;		/* -1 = no lock */
10141da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
1015924b42d5STomas Janousek 	p->real_start_time = p->start_time;
1016924b42d5STomas Janousek 	monotonic_to_bootbased(&p->real_start_time);
101757c521ceSAlexey Dobriyan #ifdef CONFIG_SECURITY
10181da177e4SLinus Torvalds 	p->security = NULL;
101957c521ceSAlexey Dobriyan #endif
10203b7391deSSerge E. Hallyn 	p->cap_bset = current->cap_bset;
10211da177e4SLinus Torvalds 	p->io_context = NULL;
10221da177e4SLinus Torvalds 	p->audit_context = NULL;
1023b4f48b63SPaul Menage 	cgroup_fork(p);
10241da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1025846a16bfSLee Schermerhorn 	p->mempolicy = mpol_dup(p->mempolicy);
10261da177e4SLinus Torvalds  	if (IS_ERR(p->mempolicy)) {
10271da177e4SLinus Torvalds  		retval = PTR_ERR(p->mempolicy);
10281da177e4SLinus Torvalds  		p->mempolicy = NULL;
1029b4f48b63SPaul Menage  		goto bad_fork_cleanup_cgroup;
10301da177e4SLinus Torvalds  	}
1031c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(p);
10321da177e4SLinus Torvalds #endif
1033de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1034de30a2b3SIngo Molnar 	p->irq_events = 0;
1035b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1036b36e4758SRussell King 	p->hardirqs_enabled = 1;
1037b36e4758SRussell King #else
1038de30a2b3SIngo Molnar 	p->hardirqs_enabled = 0;
1039b36e4758SRussell King #endif
1040de30a2b3SIngo Molnar 	p->hardirq_enable_ip = 0;
1041de30a2b3SIngo Molnar 	p->hardirq_enable_event = 0;
1042de30a2b3SIngo Molnar 	p->hardirq_disable_ip = _THIS_IP_;
1043de30a2b3SIngo Molnar 	p->hardirq_disable_event = 0;
1044de30a2b3SIngo Molnar 	p->softirqs_enabled = 1;
1045de30a2b3SIngo Molnar 	p->softirq_enable_ip = _THIS_IP_;
1046de30a2b3SIngo Molnar 	p->softirq_enable_event = 0;
1047de30a2b3SIngo Molnar 	p->softirq_disable_ip = 0;
1048de30a2b3SIngo Molnar 	p->softirq_disable_event = 0;
1049de30a2b3SIngo Molnar 	p->hardirq_context = 0;
1050de30a2b3SIngo Molnar 	p->softirq_context = 0;
1051de30a2b3SIngo Molnar #endif
1052fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1053fbb9ce95SIngo Molnar 	p->lockdep_depth = 0; /* no locks held yet */
1054fbb9ce95SIngo Molnar 	p->curr_chain_key = 0;
1055fbb9ce95SIngo Molnar 	p->lockdep_recursion = 0;
1056fbb9ce95SIngo Molnar #endif
10571da177e4SLinus Torvalds 
1058408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1059408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1060408894eeSIngo Molnar #endif
1061408894eeSIngo Molnar 
10623c90e6e9SSrivatsa Vaddagiri 	/* Perform scheduler related setup. Assign this task to a CPU. */
10633c90e6e9SSrivatsa Vaddagiri 	sched_fork(p, clone_flags);
10643c90e6e9SSrivatsa Vaddagiri 
10651da177e4SLinus Torvalds 	if ((retval = security_task_alloc(p)))
10661da177e4SLinus Torvalds 		goto bad_fork_cleanup_policy;
10671da177e4SLinus Torvalds 	if ((retval = audit_alloc(p)))
10681da177e4SLinus Torvalds 		goto bad_fork_cleanup_security;
10691da177e4SLinus Torvalds 	/* copy all the process information */
10701da177e4SLinus Torvalds 	if ((retval = copy_semundo(clone_flags, p)))
10711da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
10721da177e4SLinus Torvalds 	if ((retval = copy_files(clone_flags, p)))
10731da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
10741da177e4SLinus Torvalds 	if ((retval = copy_fs(clone_flags, p)))
10751da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
10761da177e4SLinus Torvalds 	if ((retval = copy_sighand(clone_flags, p)))
10771da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
10781da177e4SLinus Torvalds 	if ((retval = copy_signal(clone_flags, p)))
10791da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
10801da177e4SLinus Torvalds 	if ((retval = copy_mm(clone_flags, p)))
10811da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
10821da177e4SLinus Torvalds 	if ((retval = copy_keys(clone_flags, p)))
10831da177e4SLinus Torvalds 		goto bad_fork_cleanup_mm;
1084ab516013SSerge E. Hallyn 	if ((retval = copy_namespaces(clone_flags, p)))
10851da177e4SLinus Torvalds 		goto bad_fork_cleanup_keys;
1086fadad878SJens Axboe 	if ((retval = copy_io(clone_flags, p)))
1087fd0928dfSJens Axboe 		goto bad_fork_cleanup_namespaces;
10881da177e4SLinus Torvalds 	retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
10891da177e4SLinus Torvalds 	if (retval)
1090fd0928dfSJens Axboe 		goto bad_fork_cleanup_io;
10911da177e4SLinus Torvalds 
1092425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid) {
1093425fb2b4SPavel Emelyanov 		retval = -ENOMEM;
1094425fb2b4SPavel Emelyanov 		pid = alloc_pid(task_active_pid_ns(p));
1095425fb2b4SPavel Emelyanov 		if (!pid)
1096fd0928dfSJens Axboe 			goto bad_fork_cleanup_io;
10976f4e6433SPavel Emelyanov 
10986f4e6433SPavel Emelyanov 		if (clone_flags & CLONE_NEWPID) {
10996f4e6433SPavel Emelyanov 			retval = pid_ns_prepare_proc(task_active_pid_ns(p));
11006f4e6433SPavel Emelyanov 			if (retval < 0)
11016f4e6433SPavel Emelyanov 				goto bad_fork_free_pid;
11026f4e6433SPavel Emelyanov 		}
1103425fb2b4SPavel Emelyanov 	}
1104425fb2b4SPavel Emelyanov 
1105425fb2b4SPavel Emelyanov 	p->pid = pid_nr(pid);
1106425fb2b4SPavel Emelyanov 	p->tgid = p->pid;
1107425fb2b4SPavel Emelyanov 	if (clone_flags & CLONE_THREAD)
1108425fb2b4SPavel Emelyanov 		p->tgid = current->tgid;
1109425fb2b4SPavel Emelyanov 
1110e885dcdeSSerge E. Hallyn 	if (current->nsproxy != p->nsproxy) {
1111e885dcdeSSerge E. Hallyn 		retval = ns_cgroup_clone(p, pid);
1112e885dcdeSSerge E. Hallyn 		if (retval)
1113e885dcdeSSerge E. Hallyn 			goto bad_fork_free_pid;
1114e885dcdeSSerge E. Hallyn 	}
1115e885dcdeSSerge E. Hallyn 
11161da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
11171da177e4SLinus Torvalds 	/*
11181da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
11191da177e4SLinus Torvalds 	 */
11201da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
112142b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
11228f17d3a5SIngo Molnar 	p->robust_list = NULL;
11238f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
11248f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
11258f17d3a5SIngo Molnar #endif
1126c87e2837SIngo Molnar 	INIT_LIST_HEAD(&p->pi_state_list);
1127c87e2837SIngo Molnar 	p->pi_state_cache = NULL;
112842b2dd0aSAlexey Dobriyan #endif
11291da177e4SLinus Torvalds 	/*
1130f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1131f9a3879aSGOTO Masanori 	 */
1132f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1133f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1134f9a3879aSGOTO Masanori 
1135f9a3879aSGOTO Masanori 	/*
11361da177e4SLinus Torvalds 	 * Syscall tracing should be turned off in the child regardless
11371da177e4SLinus Torvalds 	 * of CLONE_PTRACE.
11381da177e4SLinus Torvalds 	 */
11391da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1140ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1141ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1142ed75e8d5SLaurent Vivier #endif
11439745512cSArjan van de Ven 	clear_all_latency_tracing(p);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	/* Our parent execution domain becomes current domain
11461da177e4SLinus Torvalds 	   These must match for thread signalling to apply */
11471da177e4SLinus Torvalds 	p->parent_exec_id = p->self_exec_id;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
11501da177e4SLinus Torvalds 	p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
11511da177e4SLinus Torvalds 	p->pdeath_signal = 0;
11521da177e4SLinus Torvalds 	p->exit_state = 0;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	/*
11551da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
11561da177e4SLinus Torvalds 	 * We dont wake it up yet.
11571da177e4SLinus Torvalds 	 */
11581da177e4SLinus Torvalds 	p->group_leader = p;
115947e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
1160f470021aSRoland McGrath 	INIT_LIST_HEAD(&p->ptrace_entry);
1161f470021aSRoland McGrath 	INIT_LIST_HEAD(&p->ptraced);
11621da177e4SLinus Torvalds 
1163b4f48b63SPaul Menage 	/* Now that the task is set up, run cgroup callbacks if
1164b4f48b63SPaul Menage 	 * necessary. We need to run them before the task is visible
1165b4f48b63SPaul Menage 	 * on the tasklist. */
1166b4f48b63SPaul Menage 	cgroup_fork_callbacks(p);
1167b4f48b63SPaul Menage 	cgroup_callbacks_done = 1;
1168b4f48b63SPaul Menage 
11691da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
11701da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	/*
1173476d139cSNick Piggin 	 * The task hasn't been attached yet, so its cpus_allowed mask will
1174476d139cSNick Piggin 	 * not be changed, nor will its assigned CPU.
1175476d139cSNick Piggin 	 *
1176476d139cSNick Piggin 	 * The cpus_allowed mask of the parent may have changed after it was
1177476d139cSNick Piggin 	 * copied first time - so re-copy it here, then check the child's CPU
1178476d139cSNick Piggin 	 * to ensure it is on a valid CPU (and if not, just force it back to
1179476d139cSNick Piggin 	 * parent's CPU). This avoids alot of nasty races.
11801da177e4SLinus Torvalds 	 */
11811da177e4SLinus Torvalds 	p->cpus_allowed = current->cpus_allowed;
11826f505b16SPeter Zijlstra 	p->rt.nr_cpus_allowed = current->rt.nr_cpus_allowed;
118326ff6ad9SSrivatsa Vaddagiri 	if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
118426ff6ad9SSrivatsa Vaddagiri 			!cpu_online(task_cpu(p))))
11851da177e4SLinus Torvalds 		set_task_cpu(p, smp_processor_id());
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
11881da177e4SLinus Torvalds 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
11891da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
11901da177e4SLinus Torvalds 	else
11911da177e4SLinus Torvalds 		p->real_parent = current;
11921da177e4SLinus Torvalds 	p->parent = p->real_parent;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
11954a2c7a78SOleg Nesterov 
11964a2c7a78SOleg Nesterov 	/*
11974a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
11984a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
11994a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
12004a2c7a78SOleg Nesterov 	 * it's process group.
12014a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
12024a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
12034a2c7a78SOleg Nesterov  	 */
12044a2c7a78SOleg Nesterov 	recalc_sigpending();
12054a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
12064a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
12074a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
12084a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
1209425fb2b4SPavel Emelyanov 		goto bad_fork_free_pid;
12104a2c7a78SOleg Nesterov 	}
12114a2c7a78SOleg Nesterov 
12123f17da69SOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
12131da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
121447e65328SOleg Nesterov 		list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 		if (!cputime_eq(current->signal->it_virt_expires,
12171da177e4SLinus Torvalds 				cputime_zero) ||
12181da177e4SLinus Torvalds 		    !cputime_eq(current->signal->it_prof_expires,
12191da177e4SLinus Torvalds 				cputime_zero) ||
12201da177e4SLinus Torvalds 		    current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
12211da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[0]) ||
12221da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[1]) ||
12231da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[2])) {
12241da177e4SLinus Torvalds 			/*
12251da177e4SLinus Torvalds 			 * Have child wake up on its first tick to check
12261da177e4SLinus Torvalds 			 * for process CPU timers.
12271da177e4SLinus Torvalds 			 */
12281da177e4SLinus Torvalds 			p->it_prof_expires = jiffies_to_cputime(1);
12291da177e4SLinus Torvalds 		}
12301da177e4SLinus Torvalds 	}
12311da177e4SLinus Torvalds 
123273b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
1233f470021aSRoland McGrath 		list_add_tail(&p->sibling, &p->real_parent->children);
12341da177e4SLinus Torvalds 		if (unlikely(p->ptrace & PT_PTRACED))
12351da177e4SLinus Torvalds 			__ptrace_link(p, current->parent);
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
12385cd17569SEric W. Biederman 			if (clone_flags & CLONE_NEWPID)
123930e49c26SPavel Emelyanov 				p->nsproxy->pid_ns->child_reaper = p;
12405cd17569SEric W. Biederman 
1241fea9d175SOleg Nesterov 			p->signal->leader_pid = pid;
12429a5d3023SOren Laadan 			p->signal->tty = current->signal->tty;
12439a2e7057SPavel Emelyanov 			set_task_pgrp(p, task_pgrp_nr(current));
1244a47afb0fSPavel Emelianov 			set_task_session(p, task_session_nr(current));
12455cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
12465cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_SID, task_session(current));
12475e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
12481da177e4SLinus Torvalds 			__get_cpu_var(process_counts)++;
12491da177e4SLinus Torvalds 		}
125085868995SSukadev Bhattiprolu 		attach_pid(p, PIDTYPE_PID, pid);
12511da177e4SLinus Torvalds 		nr_threads++;
125273b9ebfeSOleg Nesterov 	}
125373b9ebfeSOleg Nesterov 
12541da177e4SLinus Torvalds 	total_forks++;
12553f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
12561da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1257c13cf856SAndrew Morton 	proc_fork_connector(p);
1258817929ecSPaul Menage 	cgroup_post_fork(p);
12591da177e4SLinus Torvalds 	return p;
12601da177e4SLinus Torvalds 
1261425fb2b4SPavel Emelyanov bad_fork_free_pid:
1262425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid)
1263425fb2b4SPavel Emelyanov 		free_pid(pid);
1264fd0928dfSJens Axboe bad_fork_cleanup_io:
1265fd0928dfSJens Axboe 	put_io_context(p->io_context);
1266ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces:
1267444f378bSLinus Torvalds 	exit_task_namespaces(p);
12681da177e4SLinus Torvalds bad_fork_cleanup_keys:
12691da177e4SLinus Torvalds 	exit_keys(p);
12701da177e4SLinus Torvalds bad_fork_cleanup_mm:
12711da177e4SLinus Torvalds 	if (p->mm)
12721da177e4SLinus Torvalds 		mmput(p->mm);
12731da177e4SLinus Torvalds bad_fork_cleanup_signal:
12746b3934efSOleg Nesterov 	cleanup_signal(p);
12751da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1276a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
12771da177e4SLinus Torvalds bad_fork_cleanup_fs:
12781da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
12791da177e4SLinus Torvalds bad_fork_cleanup_files:
12801da177e4SLinus Torvalds 	exit_files(p); /* blocking */
12811da177e4SLinus Torvalds bad_fork_cleanup_semundo:
12821da177e4SLinus Torvalds 	exit_sem(p);
12831da177e4SLinus Torvalds bad_fork_cleanup_audit:
12841da177e4SLinus Torvalds 	audit_free(p);
12851da177e4SLinus Torvalds bad_fork_cleanup_security:
12861da177e4SLinus Torvalds 	security_task_free(p);
12871da177e4SLinus Torvalds bad_fork_cleanup_policy:
12881da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1289f0be3d32SLee Schermerhorn 	mpol_put(p->mempolicy);
1290b4f48b63SPaul Menage bad_fork_cleanup_cgroup:
12911da177e4SLinus Torvalds #endif
1292b4f48b63SPaul Menage 	cgroup_exit(p, cgroup_callbacks_done);
129335df17c5SShailabh Nagar 	delayacct_tsk_free(p);
12941da177e4SLinus Torvalds 	if (p->binfmt)
12951da177e4SLinus Torvalds 		module_put(p->binfmt->module);
12961da177e4SLinus Torvalds bad_fork_cleanup_put_domain:
1297a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
12981da177e4SLinus Torvalds bad_fork_cleanup_count:
12991da177e4SLinus Torvalds 	put_group_info(p->group_info);
13001da177e4SLinus Torvalds 	atomic_dec(&p->user->processes);
13011da177e4SLinus Torvalds 	free_uid(p->user);
13021da177e4SLinus Torvalds bad_fork_free:
13031da177e4SLinus Torvalds 	free_task(p);
1304fe7d37d1SOleg Nesterov fork_out:
1305fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13086b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
13111da177e4SLinus Torvalds 	return regs;
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds 
13149abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu)
13151da177e4SLinus Torvalds {
131636c8b586SIngo Molnar 	struct task_struct *task;
13171da177e4SLinus Torvalds 	struct pt_regs regs;
13181da177e4SLinus Torvalds 
131930e49c26SPavel Emelyanov 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL,
132085868995SSukadev Bhattiprolu 				&init_struct_pid);
1321753ca4f3SAkinobu Mita 	if (!IS_ERR(task))
13221da177e4SLinus Torvalds 		init_idle(task, cpu);
132373b9ebfeSOleg Nesterov 
13241da177e4SLinus Torvalds 	return task;
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
1327a39bc516SAlexey Dobriyan static int fork_traceflag(unsigned clone_flags)
13281da177e4SLinus Torvalds {
13291da177e4SLinus Torvalds 	if (clone_flags & CLONE_UNTRACED)
13301da177e4SLinus Torvalds 		return 0;
13311da177e4SLinus Torvalds 	else if (clone_flags & CLONE_VFORK) {
13321da177e4SLinus Torvalds 		if (current->ptrace & PT_TRACE_VFORK)
13331da177e4SLinus Torvalds 			return PTRACE_EVENT_VFORK;
13341da177e4SLinus Torvalds 	} else if ((clone_flags & CSIGNAL) != SIGCHLD) {
13351da177e4SLinus Torvalds 		if (current->ptrace & PT_TRACE_CLONE)
13361da177e4SLinus Torvalds 			return PTRACE_EVENT_CLONE;
13371da177e4SLinus Torvalds 	} else if (current->ptrace & PT_TRACE_FORK)
13381da177e4SLinus Torvalds 		return PTRACE_EVENT_FORK;
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	return 0;
13411da177e4SLinus Torvalds }
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds /*
13441da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
13451da177e4SLinus Torvalds  *
13461da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
13471da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
13481da177e4SLinus Torvalds  */
13491da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
13501da177e4SLinus Torvalds 	      unsigned long stack_start,
13511da177e4SLinus Torvalds 	      struct pt_regs *regs,
13521da177e4SLinus Torvalds 	      unsigned long stack_size,
13531da177e4SLinus Torvalds 	      int __user *parent_tidptr,
13541da177e4SLinus Torvalds 	      int __user *child_tidptr)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	struct task_struct *p;
13571da177e4SLinus Torvalds 	int trace = 0;
135892476d7fSEric W. Biederman 	long nr;
13591da177e4SLinus Torvalds 
1360bdff746aSAndrew Morton 	/*
1361bdff746aSAndrew Morton 	 * We hope to recycle these flags after 2.6.26
1362bdff746aSAndrew Morton 	 */
1363bdff746aSAndrew Morton 	if (unlikely(clone_flags & CLONE_STOPPED)) {
1364bdff746aSAndrew Morton 		static int __read_mostly count = 100;
1365bdff746aSAndrew Morton 
1366bdff746aSAndrew Morton 		if (count > 0 && printk_ratelimit()) {
1367bdff746aSAndrew Morton 			char comm[TASK_COMM_LEN];
1368bdff746aSAndrew Morton 
1369bdff746aSAndrew Morton 			count--;
1370bdff746aSAndrew Morton 			printk(KERN_INFO "fork(): process `%s' used deprecated "
1371bdff746aSAndrew Morton 					"clone flags 0x%lx\n",
1372bdff746aSAndrew Morton 				get_task_comm(comm, current),
1373bdff746aSAndrew Morton 				clone_flags & CLONE_STOPPED);
1374bdff746aSAndrew Morton 		}
1375bdff746aSAndrew Morton 	}
1376bdff746aSAndrew Morton 
13771da177e4SLinus Torvalds 	if (unlikely(current->ptrace)) {
13781da177e4SLinus Torvalds 		trace = fork_traceflag (clone_flags);
13791da177e4SLinus Torvalds 		if (trace)
13801da177e4SLinus Torvalds 			clone_flags |= CLONE_PTRACE;
13811da177e4SLinus Torvalds 	}
13821da177e4SLinus Torvalds 
1383a6f5e063SSukadev Bhattiprolu 	p = copy_process(clone_flags, stack_start, regs, stack_size,
138430e49c26SPavel Emelyanov 			child_tidptr, NULL);
13851da177e4SLinus Torvalds 	/*
13861da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
13871da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
13881da177e4SLinus Torvalds 	 */
13891da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
13901da177e4SLinus Torvalds 		struct completion vfork;
13911da177e4SLinus Torvalds 
13926c5f3e7bSPavel Emelyanov 		nr = task_pid_vnr(p);
139330e49c26SPavel Emelyanov 
139430e49c26SPavel Emelyanov 		if (clone_flags & CLONE_PARENT_SETTID)
139530e49c26SPavel Emelyanov 			put_user(nr, parent_tidptr);
1396a6f5e063SSukadev Bhattiprolu 
13971da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
13981da177e4SLinus Torvalds 			p->vfork_done = &vfork;
13991da177e4SLinus Torvalds 			init_completion(&vfork);
14001da177e4SLinus Torvalds 		}
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 		if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
14031da177e4SLinus Torvalds 			/*
14041da177e4SLinus Torvalds 			 * We'll start up with an immediate SIGSTOP.
14051da177e4SLinus Torvalds 			 */
14061da177e4SLinus Torvalds 			sigaddset(&p->pending.signal, SIGSTOP);
14071da177e4SLinus Torvalds 			set_tsk_thread_flag(p, TIF_SIGPENDING);
14081da177e4SLinus Torvalds 		}
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 		if (!(clone_flags & CLONE_STOPPED))
14111da177e4SLinus Torvalds 			wake_up_new_task(p, clone_flags);
14121da177e4SLinus Torvalds 		else
1413d9ae90acSOleg Nesterov 			__set_task_state(p, TASK_STOPPED);
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 		if (unlikely (trace)) {
141692476d7fSEric W. Biederman 			current->ptrace_message = nr;
14171da177e4SLinus Torvalds 			ptrace_notify ((trace << 8) | SIGTRAP);
14181da177e4SLinus Torvalds 		}
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
1421ba96a0c8SRafael J. Wysocki 			freezer_do_not_count();
14221da177e4SLinus Torvalds 			wait_for_completion(&vfork);
1423ba96a0c8SRafael J. Wysocki 			freezer_count();
14249f59ce5dSChuck Ebbert 			if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) {
14259f59ce5dSChuck Ebbert 				current->ptrace_message = nr;
14261da177e4SLinus Torvalds 				ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
14271da177e4SLinus Torvalds 			}
14289f59ce5dSChuck Ebbert 		}
14291da177e4SLinus Torvalds 	} else {
143092476d7fSEric W. Biederman 		nr = PTR_ERR(p);
14311da177e4SLinus Torvalds 	}
143292476d7fSEric W. Biederman 	return nr;
14331da177e4SLinus Torvalds }
14341da177e4SLinus Torvalds 
14355fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN
14365fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0
14375fd63b30SRavikiran G Thirumalai #endif
14385fd63b30SRavikiran G Thirumalai 
14394ba9b9d0SChristoph Lameter static void sighand_ctor(struct kmem_cache *cachep, void *data)
1440aa1757f9SOleg Nesterov {
1441aa1757f9SOleg Nesterov 	struct sighand_struct *sighand = data;
1442aa1757f9SOleg Nesterov 
1443aa1757f9SOleg Nesterov 	spin_lock_init(&sighand->siglock);
1444b8fceee1SDavide Libenzi 	init_waitqueue_head(&sighand->signalfd_wqh);
1445fba2afaaSDavide Libenzi }
1446aa1757f9SOleg Nesterov 
14471da177e4SLinus Torvalds void __init proc_caches_init(void)
14481da177e4SLinus Torvalds {
14491da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
14501da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
1451aa1757f9SOleg Nesterov 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU,
145220c2df83SPaul Mundt 			sighand_ctor);
14531da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
14541da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
145520c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
14561da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
14571da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
145820c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
14591da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
14601da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
146120c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
14621da177e4SLinus Torvalds 	vm_area_cachep = kmem_cache_create("vm_area_struct",
14631da177e4SLinus Torvalds 			sizeof(struct vm_area_struct), 0,
146420c2df83SPaul Mundt 			SLAB_PANIC, NULL);
14651da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
14665fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
146720c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
14681da177e4SLinus Torvalds }
1469cf2e340fSJANAK DESAI 
1470cf2e340fSJANAK DESAI /*
1471cf2e340fSJANAK DESAI  * Check constraints on flags passed to the unshare system call and
1472cf2e340fSJANAK DESAI  * force unsharing of additional process context as appropriate.
1473cf2e340fSJANAK DESAI  */
1474a39bc516SAlexey Dobriyan static void check_unshare_flags(unsigned long *flags_ptr)
1475cf2e340fSJANAK DESAI {
1476cf2e340fSJANAK DESAI 	/*
1477cf2e340fSJANAK DESAI 	 * If unsharing a thread from a thread group, must also
1478cf2e340fSJANAK DESAI 	 * unshare vm.
1479cf2e340fSJANAK DESAI 	 */
1480cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_THREAD)
1481cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_VM;
1482cf2e340fSJANAK DESAI 
1483cf2e340fSJANAK DESAI 	/*
1484cf2e340fSJANAK DESAI 	 * If unsharing vm, must also unshare signal handlers.
1485cf2e340fSJANAK DESAI 	 */
1486cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_VM)
1487cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_SIGHAND;
1488cf2e340fSJANAK DESAI 
1489cf2e340fSJANAK DESAI 	/*
1490cf2e340fSJANAK DESAI 	 * If unsharing signal handlers and the task was created
1491cf2e340fSJANAK DESAI 	 * using CLONE_THREAD, then must unshare the thread
1492cf2e340fSJANAK DESAI 	 */
1493cf2e340fSJANAK DESAI 	if ((*flags_ptr & CLONE_SIGHAND) &&
1494cf2e340fSJANAK DESAI 	    (atomic_read(&current->signal->count) > 1))
1495cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_THREAD;
1496cf2e340fSJANAK DESAI 
1497cf2e340fSJANAK DESAI 	/*
1498cf2e340fSJANAK DESAI 	 * If unsharing namespace, must also unshare filesystem information.
1499cf2e340fSJANAK DESAI 	 */
1500cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_NEWNS)
1501cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_FS;
1502cf2e340fSJANAK DESAI }
1503cf2e340fSJANAK DESAI 
1504cf2e340fSJANAK DESAI /*
1505cf2e340fSJANAK DESAI  * Unsharing of tasks created with CLONE_THREAD is not supported yet
1506cf2e340fSJANAK DESAI  */
1507cf2e340fSJANAK DESAI static int unshare_thread(unsigned long unshare_flags)
1508cf2e340fSJANAK DESAI {
1509cf2e340fSJANAK DESAI 	if (unshare_flags & CLONE_THREAD)
1510cf2e340fSJANAK DESAI 		return -EINVAL;
1511cf2e340fSJANAK DESAI 
1512cf2e340fSJANAK DESAI 	return 0;
1513cf2e340fSJANAK DESAI }
1514cf2e340fSJANAK DESAI 
1515cf2e340fSJANAK DESAI /*
151699d1419dSJANAK DESAI  * Unshare the filesystem structure if it is being shared
1517cf2e340fSJANAK DESAI  */
1518cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1519cf2e340fSJANAK DESAI {
1520cf2e340fSJANAK DESAI 	struct fs_struct *fs = current->fs;
1521cf2e340fSJANAK DESAI 
1522cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FS) &&
152399d1419dSJANAK DESAI 	    (fs && atomic_read(&fs->count) > 1)) {
152499d1419dSJANAK DESAI 		*new_fsp = __copy_fs_struct(current->fs);
152599d1419dSJANAK DESAI 		if (!*new_fsp)
152699d1419dSJANAK DESAI 			return -ENOMEM;
152799d1419dSJANAK DESAI 	}
1528cf2e340fSJANAK DESAI 
1529cf2e340fSJANAK DESAI 	return 0;
1530cf2e340fSJANAK DESAI }
1531cf2e340fSJANAK DESAI 
1532cf2e340fSJANAK DESAI /*
1533dae3c5a0SOleg Nesterov  * Unsharing of sighand is not supported yet
1534cf2e340fSJANAK DESAI  */
1535cf2e340fSJANAK DESAI static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
1536cf2e340fSJANAK DESAI {
1537cf2e340fSJANAK DESAI 	struct sighand_struct *sigh = current->sighand;
1538cf2e340fSJANAK DESAI 
1539dae3c5a0SOleg Nesterov 	if ((unshare_flags & CLONE_SIGHAND) && atomic_read(&sigh->count) > 1)
1540cf2e340fSJANAK DESAI 		return -EINVAL;
1541cf2e340fSJANAK DESAI 	else
1542cf2e340fSJANAK DESAI 		return 0;
1543cf2e340fSJANAK DESAI }
1544cf2e340fSJANAK DESAI 
1545cf2e340fSJANAK DESAI /*
1546a0a7ec30SJANAK DESAI  * Unshare vm if it is being shared
1547cf2e340fSJANAK DESAI  */
1548cf2e340fSJANAK DESAI static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp)
1549cf2e340fSJANAK DESAI {
1550cf2e340fSJANAK DESAI 	struct mm_struct *mm = current->mm;
1551cf2e340fSJANAK DESAI 
1552cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_VM) &&
1553a0a7ec30SJANAK DESAI 	    (mm && atomic_read(&mm->mm_users) > 1)) {
15542d61b867SOleg Nesterov 		return -EINVAL;
1555a0a7ec30SJANAK DESAI 	}
1556cf2e340fSJANAK DESAI 
1557cf2e340fSJANAK DESAI 	return 0;
1558cf2e340fSJANAK DESAI }
1559cf2e340fSJANAK DESAI 
1560cf2e340fSJANAK DESAI /*
1561a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1562cf2e340fSJANAK DESAI  */
1563cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1564cf2e340fSJANAK DESAI {
1565cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1566a016f338SJANAK DESAI 	int error = 0;
1567cf2e340fSJANAK DESAI 
1568cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1569a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1570a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1571a016f338SJANAK DESAI 		if (!*new_fdp)
1572a016f338SJANAK DESAI 			return error;
1573a016f338SJANAK DESAI 	}
1574cf2e340fSJANAK DESAI 
1575cf2e340fSJANAK DESAI 	return 0;
1576cf2e340fSJANAK DESAI }
1577cf2e340fSJANAK DESAI 
1578cf2e340fSJANAK DESAI /*
1579cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1580cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1581cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1582cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1583cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1584cf2e340fSJANAK DESAI  * task_struct.
1585cf2e340fSJANAK DESAI  */
1586cf2e340fSJANAK DESAI asmlinkage long sys_unshare(unsigned long unshare_flags)
1587cf2e340fSJANAK DESAI {
1588cf2e340fSJANAK DESAI 	int err = 0;
1589cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1590dae3c5a0SOleg Nesterov 	struct sighand_struct *new_sigh = NULL;
1591cf2e340fSJANAK DESAI 	struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
1592cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1593cf7b708cSPavel Emelyanov 	struct nsproxy *new_nsproxy = NULL;
15949edff4abSManfred Spraul 	int do_sysvsem = 0;
1595cf2e340fSJANAK DESAI 
1596cf2e340fSJANAK DESAI 	check_unshare_flags(&unshare_flags);
1597cf2e340fSJANAK DESAI 
159806f9d4f9SEric W. Biederman 	/* Return -EINVAL for all unsupported flags */
159906f9d4f9SEric W. Biederman 	err = -EINVAL;
160006f9d4f9SEric W. Biederman 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
160125b21cb2SKirill Korotaev 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
16029dd776b6SEric W. Biederman 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWUSER|
16039dd776b6SEric W. Biederman 				CLONE_NEWNET))
160406f9d4f9SEric W. Biederman 		goto bad_unshare_out;
160506f9d4f9SEric W. Biederman 
16066013f67fSManfred Spraul 	/*
16076013f67fSManfred Spraul 	 * CLONE_NEWIPC must also detach from the undolist: after switching
16086013f67fSManfred Spraul 	 * to a new ipc namespace, the semaphore arrays from the old
16096013f67fSManfred Spraul 	 * namespace are unreachable.
16106013f67fSManfred Spraul 	 */
16116013f67fSManfred Spraul 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
16129edff4abSManfred Spraul 		do_sysvsem = 1;
1613cf2e340fSJANAK DESAI 	if ((err = unshare_thread(unshare_flags)))
1614cf2e340fSJANAK DESAI 		goto bad_unshare_out;
1615cf2e340fSJANAK DESAI 	if ((err = unshare_fs(unshare_flags, &new_fs)))
1616cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_thread;
1617cf2e340fSJANAK DESAI 	if ((err = unshare_sighand(unshare_flags, &new_sigh)))
1618e3222c4eSBadari Pulavarty 		goto bad_unshare_cleanup_fs;
1619cf2e340fSJANAK DESAI 	if ((err = unshare_vm(unshare_flags, &new_mm)))
1620cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_sigh;
1621cf2e340fSJANAK DESAI 	if ((err = unshare_fd(unshare_flags, &new_fd)))
1622cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_vm;
1623e3222c4eSBadari Pulavarty 	if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
1624e3222c4eSBadari Pulavarty 			new_fs)))
16259edff4abSManfred Spraul 		goto bad_unshare_cleanup_fd;
1626cf2e340fSJANAK DESAI 
16279edff4abSManfred Spraul 	if (new_fs ||  new_mm || new_fd || do_sysvsem || new_nsproxy) {
16289edff4abSManfred Spraul 		if (do_sysvsem) {
16299edff4abSManfred Spraul 			/*
16309edff4abSManfred Spraul 			 * CLONE_SYSVSEM is equivalent to sys_exit().
16319edff4abSManfred Spraul 			 */
16329edff4abSManfred Spraul 			exit_sem(current);
16339edff4abSManfred Spraul 		}
1634ab516013SSerge E. Hallyn 
1635c0b2fc31SSerge Hallyn 		if (new_nsproxy) {
1636cf7b708cSPavel Emelyanov 			switch_task_namespaces(current, new_nsproxy);
1637cf7b708cSPavel Emelyanov 			new_nsproxy = NULL;
1638c0b2fc31SSerge Hallyn 		}
1639cf2e340fSJANAK DESAI 
1640cf7b708cSPavel Emelyanov 		task_lock(current);
1641cf7b708cSPavel Emelyanov 
1642cf2e340fSJANAK DESAI 		if (new_fs) {
1643cf2e340fSJANAK DESAI 			fs = current->fs;
1644cf2e340fSJANAK DESAI 			current->fs = new_fs;
1645cf2e340fSJANAK DESAI 			new_fs = fs;
1646cf2e340fSJANAK DESAI 		}
1647cf2e340fSJANAK DESAI 
1648cf2e340fSJANAK DESAI 		if (new_mm) {
1649cf2e340fSJANAK DESAI 			mm = current->mm;
1650cf2e340fSJANAK DESAI 			active_mm = current->active_mm;
1651cf2e340fSJANAK DESAI 			current->mm = new_mm;
1652cf2e340fSJANAK DESAI 			current->active_mm = new_mm;
1653cf2e340fSJANAK DESAI 			activate_mm(active_mm, new_mm);
1654cf2e340fSJANAK DESAI 			new_mm = mm;
1655cf2e340fSJANAK DESAI 		}
1656cf2e340fSJANAK DESAI 
1657cf2e340fSJANAK DESAI 		if (new_fd) {
1658cf2e340fSJANAK DESAI 			fd = current->files;
1659cf2e340fSJANAK DESAI 			current->files = new_fd;
1660cf2e340fSJANAK DESAI 			new_fd = fd;
1661cf2e340fSJANAK DESAI 		}
1662cf2e340fSJANAK DESAI 
1663cf2e340fSJANAK DESAI 		task_unlock(current);
1664cf2e340fSJANAK DESAI 	}
1665cf2e340fSJANAK DESAI 
1666c0b2fc31SSerge Hallyn 	if (new_nsproxy)
1667444f378bSLinus Torvalds 		put_nsproxy(new_nsproxy);
1668c0b2fc31SSerge Hallyn 
1669cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
1670cf2e340fSJANAK DESAI 	if (new_fd)
1671cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
1672cf2e340fSJANAK DESAI 
1673cf2e340fSJANAK DESAI bad_unshare_cleanup_vm:
1674cf2e340fSJANAK DESAI 	if (new_mm)
1675cf2e340fSJANAK DESAI 		mmput(new_mm);
1676cf2e340fSJANAK DESAI 
1677cf2e340fSJANAK DESAI bad_unshare_cleanup_sigh:
1678cf2e340fSJANAK DESAI 	if (new_sigh)
1679cf2e340fSJANAK DESAI 		if (atomic_dec_and_test(&new_sigh->count))
1680cf2e340fSJANAK DESAI 			kmem_cache_free(sighand_cachep, new_sigh);
1681cf2e340fSJANAK DESAI 
1682cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
1683cf2e340fSJANAK DESAI 	if (new_fs)
1684cf2e340fSJANAK DESAI 		put_fs_struct(new_fs);
1685cf2e340fSJANAK DESAI 
1686cf2e340fSJANAK DESAI bad_unshare_cleanup_thread:
1687cf2e340fSJANAK DESAI bad_unshare_out:
1688cf2e340fSJANAK DESAI 	return err;
1689cf2e340fSJANAK DESAI }
16903b125388SAl Viro 
16913b125388SAl Viro /*
16923b125388SAl Viro  *	Helper to unshare the files of the current task.
16933b125388SAl Viro  *	We don't want to expose copy_files internals to
16943b125388SAl Viro  *	the exec layer of the kernel.
16953b125388SAl Viro  */
16963b125388SAl Viro 
16973b125388SAl Viro int unshare_files(struct files_struct **displaced)
16983b125388SAl Viro {
16993b125388SAl Viro 	struct task_struct *task = current;
170050704516SAl Viro 	struct files_struct *copy = NULL;
17013b125388SAl Viro 	int error;
17023b125388SAl Viro 
17033b125388SAl Viro 	error = unshare_fd(CLONE_FILES, &copy);
17043b125388SAl Viro 	if (error || !copy) {
17053b125388SAl Viro 		*displaced = NULL;
17063b125388SAl Viro 		return error;
17073b125388SAl Viro 	}
17083b125388SAl Viro 	*displaced = task->files;
17093b125388SAl Viro 	task_lock(task);
17103b125388SAl Viro 	task->files = copy;
17113b125388SAl Viro 	task_unlock(task);
17123b125388SAl Viro 	return 0;
17133b125388SAl Viro }
1714