xref: /openbmc/linux/kernel/fork.c (revision 801460d0cf5c5288153b722565773059b0f44348)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/kernel/fork.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  *  'fork.c' contains the help-routines for the 'fork' system call
91da177e4SLinus Torvalds  * (see also entry.S and others).
101da177e4SLinus Torvalds  * Fork is rather simple, once you get the hang of it, but the memory
111da177e4SLinus Torvalds  * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/unistd.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/vmalloc.h>
191da177e4SLinus Torvalds #include <linux/completion.h>
201da177e4SLinus Torvalds #include <linux/personality.h>
211da177e4SLinus Torvalds #include <linux/mempolicy.h>
221da177e4SLinus Torvalds #include <linux/sem.h>
231da177e4SLinus Torvalds #include <linux/file.h>
249f3acc31SAl Viro #include <linux/fdtable.h>
25da9cbc87SJens Axboe #include <linux/iocontext.h>
261da177e4SLinus Torvalds #include <linux/key.h>
271da177e4SLinus Torvalds #include <linux/binfmts.h>
281da177e4SLinus Torvalds #include <linux/mman.h>
29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
301da177e4SLinus Torvalds #include <linux/fs.h>
31ab516013SSerge E. Hallyn #include <linux/nsproxy.h>
32c59ede7bSRandy.Dunlap #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/cpu.h>
34b4f48b63SPaul Menage #include <linux/cgroup.h>
351da177e4SLinus Torvalds #include <linux/security.h>
36a1e78772SMel Gorman #include <linux/hugetlb.h>
371da177e4SLinus Torvalds #include <linux/swap.h>
381da177e4SLinus Torvalds #include <linux/syscalls.h>
391da177e4SLinus Torvalds #include <linux/jiffies.h>
4009a05394SRoland McGrath #include <linux/tracehook.h>
411da177e4SLinus Torvalds #include <linux/futex.h>
428141c7f3SLinus Torvalds #include <linux/compat.h>
437c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h>
44ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
451da177e4SLinus Torvalds #include <linux/ptrace.h>
461da177e4SLinus Torvalds #include <linux/mount.h>
471da177e4SLinus Torvalds #include <linux/audit.h>
4878fb7466SPavel Emelianov #include <linux/memcontrol.h>
49f201ae23SFrederic Weisbecker #include <linux/ftrace.h>
501da177e4SLinus Torvalds #include <linux/profile.h>
511da177e4SLinus Torvalds #include <linux/rmap.h>
52f8af4da3SHugh Dickins #include <linux/ksm.h>
531da177e4SLinus Torvalds #include <linux/acct.h>
548f0ab514SJay Lan #include <linux/tsacct_kern.h>
559f46080cSMatt Helsley #include <linux/cn_proc.h>
56ba96a0c8SRafael J. Wysocki #include <linux/freezer.h>
57ca74e92bSShailabh Nagar #include <linux/delayacct.h>
58ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h>
590a425405SArjan van de Ven #include <linux/random.h>
60522ed776SMiloslav Trmac #include <linux/tty.h>
616f4e6433SPavel Emelyanov #include <linux/proc_fs.h>
62fd0928dfSJens Axboe #include <linux/blkdev.h>
635ad4e53bSAl Viro #include <linux/fs_struct.h>
647c9f8861SEric Sandeen #include <linux/magic.h>
65cdd6c482SIngo Molnar #include <linux/perf_event.h>
6642c4ab41SStanislaw Gruszka #include <linux/posix-timers.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <asm/pgtable.h>
691da177e4SLinus Torvalds #include <asm/pgalloc.h>
701da177e4SLinus Torvalds #include <asm/uaccess.h>
711da177e4SLinus Torvalds #include <asm/mmu_context.h>
721da177e4SLinus Torvalds #include <asm/cacheflush.h>
731da177e4SLinus Torvalds #include <asm/tlbflush.h>
741da177e4SLinus Torvalds 
75ad8d75ffSSteven Rostedt #include <trace/events/sched.h>
76ad8d75ffSSteven Rostedt 
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
791da177e4SLinus Torvalds  */
801da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
811da177e4SLinus Torvalds int nr_threads; 		/* The idle threads do not count.. */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds int nr_processes(void)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	int cpu;
921da177e4SLinus Torvalds 	int total = 0;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	for_each_online_cpu(cpu)
951da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	return total;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
1011da177e4SLinus Torvalds # define alloc_task_struct()	kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
1021da177e4SLinus Torvalds # define free_task_struct(tsk)	kmem_cache_free(task_struct_cachep, (tsk))
103e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep;
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds 
106b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR
107b69c49b7SFUJITA Tomonori static inline struct thread_info *alloc_thread_info(struct task_struct *tsk)
108b69c49b7SFUJITA Tomonori {
109b69c49b7SFUJITA Tomonori #ifdef CONFIG_DEBUG_STACK_USAGE
110b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL | __GFP_ZERO;
111b69c49b7SFUJITA Tomonori #else
112b69c49b7SFUJITA Tomonori 	gfp_t mask = GFP_KERNEL;
113b69c49b7SFUJITA Tomonori #endif
114b69c49b7SFUJITA Tomonori 	return (struct thread_info *)__get_free_pages(mask, THREAD_SIZE_ORDER);
115b69c49b7SFUJITA Tomonori }
116b69c49b7SFUJITA Tomonori 
117b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti)
118b69c49b7SFUJITA Tomonori {
119b69c49b7SFUJITA Tomonori 	free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
120b69c49b7SFUJITA Tomonori }
121b69c49b7SFUJITA Tomonori #endif
122b69c49b7SFUJITA Tomonori 
1231da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
124e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
127e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
130e18b890bSChristoph Lameter struct kmem_cache *files_cachep;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
133e18b890bSChristoph Lameter struct kmem_cache *fs_cachep;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
136e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
139e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep;
1401da177e4SLinus Torvalds 
141c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account)
142c6a7f572SKOSAKI Motohiro {
143c6a7f572SKOSAKI Motohiro 	struct zone *zone = page_zone(virt_to_page(ti));
144c6a7f572SKOSAKI Motohiro 
145c6a7f572SKOSAKI Motohiro 	mod_zone_page_state(zone, NR_KERNEL_STACK, account);
146c6a7f572SKOSAKI Motohiro }
147c6a7f572SKOSAKI Motohiro 
1481da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1491da177e4SLinus Torvalds {
1503e26c149SPeter Zijlstra 	prop_local_destroy_single(&tsk->dirties);
151c6a7f572SKOSAKI Motohiro 	account_kernel_stack(tsk->stack, -1);
152f7e4217bSRoman Zippel 	free_thread_info(tsk->stack);
15323f78d4aSIngo Molnar 	rt_mutex_debug_task_free(tsk);
154fb52607aSFrederic Weisbecker 	ftrace_graph_exit_task(tsk);
1551da177e4SLinus Torvalds 	free_task_struct(tsk);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1581da177e4SLinus Torvalds 
159158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
1601da177e4SLinus Torvalds {
161270f722dSEugene Teo 	WARN_ON(!tsk->exit_state);
1621da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1631da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1641da177e4SLinus Torvalds 
165e0e81739SDavid Howells 	exit_creds(tsk);
16635df17c5SShailabh Nagar 	delayacct_tsk_free(tsk);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
1691da177e4SLinus Torvalds 		free_task(tsk);
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1722adee9b3SSuresh Siddha /*
1732adee9b3SSuresh Siddha  * macro override instead of weak attribute alias, to workaround
1742adee9b3SSuresh Siddha  * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
1752adee9b3SSuresh Siddha  */
1762adee9b3SSuresh Siddha #ifndef arch_task_cache_init
1772adee9b3SSuresh Siddha #define arch_task_cache_init()
1782adee9b3SSuresh Siddha #endif
17961c4628bSSuresh Siddha 
1801da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
1831da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
1841da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
1851da177e4SLinus Torvalds #endif
1861da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
1871da177e4SLinus Torvalds 	task_struct_cachep =
1881da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
1892dff4405SVegard Nossum 			ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL);
1901da177e4SLinus Torvalds #endif
1911da177e4SLinus Torvalds 
19261c4628bSSuresh Siddha 	/* do the arch specific task caches init */
19361c4628bSSuresh Siddha 	arch_task_cache_init();
19461c4628bSSuresh Siddha 
1951da177e4SLinus Torvalds 	/*
1961da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
1971da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
1981da177e4SLinus Torvalds 	 * of memory.
1991da177e4SLinus Torvalds 	 */
2001da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	/*
2031da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
2041da177e4SLinus Torvalds 	 */
2051da177e4SLinus Torvalds 	if(max_threads < 20)
2061da177e4SLinus Torvalds 		max_threads = 20;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
2091da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
2101da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
2111da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
21461c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
21561c4628bSSuresh Siddha 					       struct task_struct *src)
21661c4628bSSuresh Siddha {
21761c4628bSSuresh Siddha 	*dst = *src;
21861c4628bSSuresh Siddha 	return 0;
21961c4628bSSuresh Siddha }
22061c4628bSSuresh Siddha 
2211da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	struct task_struct *tsk;
2241da177e4SLinus Torvalds 	struct thread_info *ti;
2257c9f8861SEric Sandeen 	unsigned long *stackend;
2267c9f8861SEric Sandeen 
2273e26c149SPeter Zijlstra 	int err;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	prepare_to_copy(orig);
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	tsk = alloc_task_struct();
2321da177e4SLinus Torvalds 	if (!tsk)
2331da177e4SLinus Torvalds 		return NULL;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	ti = alloc_thread_info(tsk);
2361da177e4SLinus Torvalds 	if (!ti) {
2371da177e4SLinus Torvalds 		free_task_struct(tsk);
2381da177e4SLinus Torvalds 		return NULL;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 
24161c4628bSSuresh Siddha  	err = arch_dup_task_struct(tsk, orig);
24261c4628bSSuresh Siddha 	if (err)
24361c4628bSSuresh Siddha 		goto out;
24461c4628bSSuresh Siddha 
245f7e4217bSRoman Zippel 	tsk->stack = ti;
2463e26c149SPeter Zijlstra 
2473e26c149SPeter Zijlstra 	err = prop_local_init_single(&tsk->dirties);
24861c4628bSSuresh Siddha 	if (err)
24961c4628bSSuresh Siddha 		goto out;
2503e26c149SPeter Zijlstra 
25110ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
2527c9f8861SEric Sandeen 	stackend = end_of_stack(tsk);
2537c9f8861SEric Sandeen 	*stackend = STACK_END_MAGIC;	/* for overflow detection */
2541da177e4SLinus Torvalds 
2550a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR
2560a425405SArjan van de Ven 	tsk->stack_canary = get_random_int();
2570a425405SArjan van de Ven #endif
2580a425405SArjan van de Ven 
2591da177e4SLinus Torvalds 	/* One for us, one for whoever does the "release_task()" (usually parent) */
2601da177e4SLinus Torvalds 	atomic_set(&tsk->usage,2);
2614b5d37acSGiancarlo Formicuccia 	atomic_set(&tsk->fs_excl, 0);
2626c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
2632056a782SJens Axboe 	tsk->btrace_seq = 0;
2646c5c9341SAlexey Dobriyan #endif
265a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
266c6a7f572SKOSAKI Motohiro 
267c6a7f572SKOSAKI Motohiro 	account_kernel_stack(ti, 1);
268c6a7f572SKOSAKI Motohiro 
2691da177e4SLinus Torvalds 	return tsk;
27061c4628bSSuresh Siddha 
27161c4628bSSuresh Siddha out:
27261c4628bSSuresh Siddha 	free_thread_info(ti);
27361c4628bSSuresh Siddha 	free_task_struct(tsk);
27461c4628bSSuresh Siddha 	return NULL;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds #ifdef CONFIG_MMU
278a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, **pprev;
2811da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
2821da177e4SLinus Torvalds 	int retval;
2831da177e4SLinus Torvalds 	unsigned long charge;
2841da177e4SLinus Torvalds 	struct mempolicy *pol;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
287ec8c0446SRalf Baechle 	flush_cache_dup_mm(oldmm);
288ad339451SIngo Molnar 	/*
289ad339451SIngo Molnar 	 * Not linked in yet - no deadlock potential:
290ad339451SIngo Molnar 	 */
291ad339451SIngo Molnar 	down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
2927ee78232SHugh Dickins 
2931da177e4SLinus Torvalds 	mm->locked_vm = 0;
2941da177e4SLinus Torvalds 	mm->mmap = NULL;
2951da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
2961da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
2971363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
2981da177e4SLinus Torvalds 	mm->map_count = 0;
29994894244SRusty Russell 	cpumask_clear(mm_cpumask(mm));
3001da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
3011da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
3021da177e4SLinus Torvalds 	rb_parent = NULL;
3031da177e4SLinus Torvalds 	pprev = &mm->mmap;
304f8af4da3SHugh Dickins 	retval = ksm_fork(mm, oldmm);
305f8af4da3SHugh Dickins 	if (retval)
306f8af4da3SHugh Dickins 		goto out;
3071da177e4SLinus Torvalds 
308fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
3091da177e4SLinus Torvalds 		struct file *file;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
3123b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
3133b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
314ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
3153b6bfcdbSHugh Dickins 								-pages);
3161da177e4SLinus Torvalds 			continue;
3171da177e4SLinus Torvalds 		}
3181da177e4SLinus Torvalds 		charge = 0;
3191da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
3201da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
3211da177e4SLinus Torvalds 			if (security_vm_enough_memory(len))
3221da177e4SLinus Torvalds 				goto fail_nomem;
3231da177e4SLinus Torvalds 			charge = len;
3241da177e4SLinus Torvalds 		}
325e94b1766SChristoph Lameter 		tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
3261da177e4SLinus Torvalds 		if (!tmp)
3271da177e4SLinus Torvalds 			goto fail_nomem;
3281da177e4SLinus Torvalds 		*tmp = *mpnt;
329846a16bfSLee Schermerhorn 		pol = mpol_dup(vma_policy(mpnt));
3301da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
3311da177e4SLinus Torvalds 		if (IS_ERR(pol))
3321da177e4SLinus Torvalds 			goto fail_nomem_policy;
3331da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
3341da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
3351da177e4SLinus Torvalds 		tmp->vm_mm = mm;
3361da177e4SLinus Torvalds 		tmp->vm_next = NULL;
3371da177e4SLinus Torvalds 		anon_vma_link(tmp);
3381da177e4SLinus Torvalds 		file = tmp->vm_file;
3391da177e4SLinus Torvalds 		if (file) {
340f3a43f3fSJosef "Jeff" Sipek 			struct inode *inode = file->f_path.dentry->d_inode;
341b88ed205SHugh Dickins 			struct address_space *mapping = file->f_mapping;
342b88ed205SHugh Dickins 
3431da177e4SLinus Torvalds 			get_file(file);
3441da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
3451da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
346b88ed205SHugh Dickins 			spin_lock(&mapping->i_mmap_lock);
347b88ed205SHugh Dickins 			if (tmp->vm_flags & VM_SHARED)
348b88ed205SHugh Dickins 				mapping->i_mmap_writable++;
3491da177e4SLinus Torvalds 			tmp->vm_truncate_count = mpnt->vm_truncate_count;
350b88ed205SHugh Dickins 			flush_dcache_mmap_lock(mapping);
351b88ed205SHugh Dickins 			/* insert tmp into the share list, just after mpnt */
3521da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
353b88ed205SHugh Dickins 			flush_dcache_mmap_unlock(mapping);
354b88ed205SHugh Dickins 			spin_unlock(&mapping->i_mmap_lock);
3551da177e4SLinus Torvalds 		}
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		/*
358a1e78772SMel Gorman 		 * Clear hugetlb-related page reserves for children. This only
359a1e78772SMel Gorman 		 * affects MAP_PRIVATE mappings. Faults generated by the child
360a1e78772SMel Gorman 		 * are not guaranteed to succeed, even if read-only
361a1e78772SMel Gorman 		 */
362a1e78772SMel Gorman 		if (is_vm_hugetlb_page(tmp))
363a1e78772SMel Gorman 			reset_vma_resv_huge_pages(tmp);
364a1e78772SMel Gorman 
365a1e78772SMel Gorman 		/*
3667ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
3671da177e4SLinus Torvalds 		 */
3681da177e4SLinus Torvalds 		*pprev = tmp;
3691da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
3721da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
3731da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		mm->map_count++;
3760b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
3791da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 		if (retval)
3821da177e4SLinus Torvalds 			goto out;
3831da177e4SLinus Torvalds 	}
384d6dd61c8SJeremy Fitzhardinge 	/* a new mm has just been created */
385d6dd61c8SJeremy Fitzhardinge 	arch_dup_mmap(oldmm, mm);
3861da177e4SLinus Torvalds 	retval = 0;
3871da177e4SLinus Torvalds out:
3887ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
389fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
3901da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
3911da177e4SLinus Torvalds 	return retval;
3921da177e4SLinus Torvalds fail_nomem_policy:
3931da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
3941da177e4SLinus Torvalds fail_nomem:
3951da177e4SLinus Torvalds 	retval = -ENOMEM;
3961da177e4SLinus Torvalds 	vm_unacct_memory(charge);
3971da177e4SLinus Torvalds 	goto out;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
4031da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
4041da177e4SLinus Torvalds 		return -ENOMEM;
4051da177e4SLinus Torvalds 	return 0;
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm)
4091da177e4SLinus Torvalds {
4105e541973SBenjamin Herrenschmidt 	pgd_free(mm, mm->pgd);
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds #else
4131da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
4141da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
4151da177e4SLinus Torvalds #define mm_free_pgd(mm)
4161da177e4SLinus Torvalds #endif /* CONFIG_MMU */
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
4191da177e4SLinus Torvalds 
420e94b1766SChristoph Lameter #define allocate_mm()	(kmem_cache_alloc(mm_cachep, GFP_KERNEL))
4211da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
4221da177e4SLinus Torvalds 
4234cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
4244cb0e11bSHidehiro Kawai 
4254cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s)
4264cb0e11bSHidehiro Kawai {
4274cb0e11bSHidehiro Kawai 	default_dump_filter =
4284cb0e11bSHidehiro Kawai 		(simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
4294cb0e11bSHidehiro Kawai 		MMF_DUMP_FILTER_MASK;
4304cb0e11bSHidehiro Kawai 	return 1;
4314cb0e11bSHidehiro Kawai }
4324cb0e11bSHidehiro Kawai 
4334cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup);
4344cb0e11bSHidehiro Kawai 
4351da177e4SLinus Torvalds #include <linux/init_task.h>
4361da177e4SLinus Torvalds 
437858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm)
438858f0993SAlexey Dobriyan {
439858f0993SAlexey Dobriyan #ifdef CONFIG_AIO
440858f0993SAlexey Dobriyan 	spin_lock_init(&mm->ioctx_lock);
441858f0993SAlexey Dobriyan 	INIT_HLIST_HEAD(&mm->ioctx_list);
442858f0993SAlexey Dobriyan #endif
443858f0993SAlexey Dobriyan }
444858f0993SAlexey Dobriyan 
44578fb7466SPavel Emelianov static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
4481da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
4491da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
4501da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
451f8af4da3SHugh Dickins 	mm->flags = (current->mm) ?
452f8af4da3SHugh Dickins 		(current->mm->flags & MMF_INIT_MASK) : default_dump_filter;
453999d9fc1SOleg Nesterov 	mm->core_state = NULL;
4541da177e4SLinus Torvalds 	mm->nr_ptes = 0;
4554294621fSHugh Dickins 	set_mm_counter(mm, file_rss, 0);
456404351e6SHugh Dickins 	set_mm_counter(mm, anon_rss, 0);
4571da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
4581da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
4591363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
460858f0993SAlexey Dobriyan 	mm_init_aio(mm);
461cf475ad2SBalbir Singh 	mm_init_owner(mm, p);
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
4641da177e4SLinus Torvalds 		mm->def_flags = 0;
465cddb8a5cSAndrea Arcangeli 		mmu_notifier_mm_init(mm);
4661da177e4SLinus Torvalds 		return mm;
4671da177e4SLinus Torvalds 	}
46878fb7466SPavel Emelianov 
4691da177e4SLinus Torvalds 	free_mm(mm);
4701da177e4SLinus Torvalds 	return NULL;
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds /*
4741da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
4751da177e4SLinus Torvalds  */
4761da177e4SLinus Torvalds struct mm_struct * mm_alloc(void)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct mm_struct * mm;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	mm = allocate_mm();
4811da177e4SLinus Torvalds 	if (mm) {
4821da177e4SLinus Torvalds 		memset(mm, 0, sizeof(*mm));
48378fb7466SPavel Emelianov 		mm = mm_init(mm, current);
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 	return mm;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /*
4891da177e4SLinus Torvalds  * Called when the last reference to the mm
4901da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
4911da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
4921da177e4SLinus Torvalds  */
4937ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
4961da177e4SLinus Torvalds 	mm_free_pgd(mm);
4971da177e4SLinus Torvalds 	destroy_context(mm);
498cddb8a5cSAndrea Arcangeli 	mmu_notifier_mm_destroy(mm);
4991da177e4SLinus Torvalds 	free_mm(mm);
5001da177e4SLinus Torvalds }
5016d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds /*
5041da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
5051da177e4SLinus Torvalds  */
5061da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
5071da177e4SLinus Torvalds {
5080ae26f1bSAndrew Morton 	might_sleep();
5090ae26f1bSAndrew Morton 
5101da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
5111da177e4SLinus Torvalds 		exit_aio(mm);
5121c2fb7a4SAndrea Arcangeli 		ksm_exit(mm);
5131da177e4SLinus Torvalds 		exit_mmap(mm);
514925d1c40SMatt Helsley 		set_mm_exe_file(mm, NULL);
5151da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
5161da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
5171da177e4SLinus Torvalds 			list_del(&mm->mmlist);
5181da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
5191da177e4SLinus Torvalds 		}
5201da177e4SLinus Torvalds 		put_swap_token(mm);
521*801460d0SHiroshi Shimamoto 		if (mm->binfmt)
522*801460d0SHiroshi Shimamoto 			module_put(mm->binfmt->module);
5231da177e4SLinus Torvalds 		mmdrop(mm);
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /**
5291da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
5301da177e4SLinus Torvalds  *
531246bb0b1SOleg Nesterov  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
5321da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
5331da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
5341da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
5351da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
5361da177e4SLinus Torvalds  */
5371da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	struct mm_struct *mm;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	task_lock(task);
5421da177e4SLinus Torvalds 	mm = task->mm;
5431da177e4SLinus Torvalds 	if (mm) {
544246bb0b1SOleg Nesterov 		if (task->flags & PF_KTHREAD)
5451da177e4SLinus Torvalds 			mm = NULL;
5461da177e4SLinus Torvalds 		else
5471da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	task_unlock(task);
5501da177e4SLinus Torvalds 	return mm;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
5551da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
5561da177e4SLinus Torvalds  * error success whatever.
5571da177e4SLinus Torvalds  *
5581da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
5591da177e4SLinus Torvalds  * from the current process.
5601da177e4SLinus Torvalds  *
5611da177e4SLinus Torvalds  * This difference is important for error handling, when we
5621da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
5631da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
5641da177e4SLinus Torvalds  * restoring the old one. . .
5651da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
5661da177e4SLinus Torvalds  */
5671da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	struct completion *vfork_done = tsk->vfork_done;
5701da177e4SLinus Torvalds 
5718141c7f3SLinus Torvalds 	/* Get rid of any futexes when releasing the mm */
5728141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX
5738141c7f3SLinus Torvalds 	if (unlikely(tsk->robust_list))
5748141c7f3SLinus Torvalds 		exit_robust_list(tsk);
5758141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT
5768141c7f3SLinus Torvalds 	if (unlikely(tsk->compat_robust_list))
5778141c7f3SLinus Torvalds 		compat_exit_robust_list(tsk);
5788141c7f3SLinus Torvalds #endif
5798141c7f3SLinus Torvalds #endif
5808141c7f3SLinus Torvalds 
5811da177e4SLinus Torvalds 	/* Get rid of any cached register state */
5821da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	/* notify parent sleeping on vfork() */
5851da177e4SLinus Torvalds 	if (vfork_done) {
5861da177e4SLinus Torvalds 		tsk->vfork_done = NULL;
5871da177e4SLinus Torvalds 		complete(vfork_done);
5881da177e4SLinus Torvalds 	}
589fec1d011SRoland McGrath 
590fec1d011SRoland McGrath 	/*
591fec1d011SRoland McGrath 	 * If we're exiting normally, clear a user-space tid field if
592fec1d011SRoland McGrath 	 * requested.  We leave this alone when dying by signal, to leave
593fec1d011SRoland McGrath 	 * the value intact in a core dump, and to save the unnecessary
594fec1d011SRoland McGrath 	 * trouble otherwise.  Userland only wants this done for a sys_exit.
595fec1d011SRoland McGrath 	 */
5969c8a8228SEric Dumazet 	if (tsk->clear_child_tid) {
5979c8a8228SEric Dumazet 		if (!(tsk->flags & PF_SIGNALED) &&
5989c8a8228SEric Dumazet 		    atomic_read(&mm->mm_users) > 1) {
5991da177e4SLinus Torvalds 			/*
6001da177e4SLinus Torvalds 			 * We don't check the error code - if userspace has
6011da177e4SLinus Torvalds 			 * not set up a proper pointer then tough luck.
6021da177e4SLinus Torvalds 			 */
6039c8a8228SEric Dumazet 			put_user(0, tsk->clear_child_tid);
6049c8a8228SEric Dumazet 			sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
6059c8a8228SEric Dumazet 					1, NULL, NULL, 0);
6069c8a8228SEric Dumazet 		}
6079c8a8228SEric Dumazet 		tsk->clear_child_tid = NULL;
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
611a0a7ec30SJANAK DESAI /*
612a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
613a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
614a0a7ec30SJANAK DESAI  */
615402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk)
616a0a7ec30SJANAK DESAI {
617a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
618a0a7ec30SJANAK DESAI 	int err;
619a0a7ec30SJANAK DESAI 
620a0a7ec30SJANAK DESAI 	if (!oldmm)
621a0a7ec30SJANAK DESAI 		return NULL;
622a0a7ec30SJANAK DESAI 
623a0a7ec30SJANAK DESAI 	mm = allocate_mm();
624a0a7ec30SJANAK DESAI 	if (!mm)
625a0a7ec30SJANAK DESAI 		goto fail_nomem;
626a0a7ec30SJANAK DESAI 
627a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
628a0a7ec30SJANAK DESAI 
6297602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
6307602bdf2SAshwin Chaugule 	mm->token_priority = 0;
6317602bdf2SAshwin Chaugule 	mm->last_interval = 0;
6327602bdf2SAshwin Chaugule 
63378fb7466SPavel Emelianov 	if (!mm_init(mm, tsk))
634a0a7ec30SJANAK DESAI 		goto fail_nomem;
635a0a7ec30SJANAK DESAI 
636a0a7ec30SJANAK DESAI 	if (init_new_context(tsk, mm))
637a0a7ec30SJANAK DESAI 		goto fail_nocontext;
638a0a7ec30SJANAK DESAI 
639925d1c40SMatt Helsley 	dup_mm_exe_file(oldmm, mm);
640925d1c40SMatt Helsley 
641a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
642a0a7ec30SJANAK DESAI 	if (err)
643a0a7ec30SJANAK DESAI 		goto free_pt;
644a0a7ec30SJANAK DESAI 
645a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
646a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
647a0a7ec30SJANAK DESAI 
648*801460d0SHiroshi Shimamoto 	if (mm->binfmt && !try_module_get(mm->binfmt->module))
649*801460d0SHiroshi Shimamoto 		goto free_pt;
650*801460d0SHiroshi Shimamoto 
651a0a7ec30SJANAK DESAI 	return mm;
652a0a7ec30SJANAK DESAI 
653a0a7ec30SJANAK DESAI free_pt:
654*801460d0SHiroshi Shimamoto 	/* don't put binfmt in mmput, we haven't got module yet */
655*801460d0SHiroshi Shimamoto 	mm->binfmt = NULL;
656a0a7ec30SJANAK DESAI 	mmput(mm);
657a0a7ec30SJANAK DESAI 
658a0a7ec30SJANAK DESAI fail_nomem:
659a0a7ec30SJANAK DESAI 	return NULL;
660a0a7ec30SJANAK DESAI 
661a0a7ec30SJANAK DESAI fail_nocontext:
662a0a7ec30SJANAK DESAI 	/*
663a0a7ec30SJANAK DESAI 	 * If init_new_context() failed, we cannot use mmput() to free the mm
664a0a7ec30SJANAK DESAI 	 * because it calls destroy_context()
665a0a7ec30SJANAK DESAI 	 */
666a0a7ec30SJANAK DESAI 	mm_free_pgd(mm);
667a0a7ec30SJANAK DESAI 	free_mm(mm);
668a0a7ec30SJANAK DESAI 	return NULL;
669a0a7ec30SJANAK DESAI }
670a0a7ec30SJANAK DESAI 
6711da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
6721da177e4SLinus Torvalds {
6731da177e4SLinus Torvalds 	struct mm_struct * mm, *oldmm;
6741da177e4SLinus Torvalds 	int retval;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
6771da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
67817406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
67917406b82SMandeep Singh Baines 	tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
68017406b82SMandeep Singh Baines #endif
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	tsk->mm = NULL;
6831da177e4SLinus Torvalds 	tsk->active_mm = NULL;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	/*
6861da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
6871da177e4SLinus Torvalds 	 *
6881da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
6891da177e4SLinus Torvalds 	 */
6901da177e4SLinus Torvalds 	oldmm = current->mm;
6911da177e4SLinus Torvalds 	if (!oldmm)
6921da177e4SLinus Torvalds 		return 0;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
6951da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
6961da177e4SLinus Torvalds 		mm = oldmm;
6971da177e4SLinus Torvalds 		goto good_mm;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	retval = -ENOMEM;
701a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
7021da177e4SLinus Torvalds 	if (!mm)
7031da177e4SLinus Torvalds 		goto fail_nomem;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds good_mm:
7067602bdf2SAshwin Chaugule 	/* Initializing for Swap token stuff */
7077602bdf2SAshwin Chaugule 	mm->token_priority = 0;
7087602bdf2SAshwin Chaugule 	mm->last_interval = 0;
7097602bdf2SAshwin Chaugule 
7101da177e4SLinus Torvalds 	tsk->mm = mm;
7111da177e4SLinus Torvalds 	tsk->active_mm = mm;
7121da177e4SLinus Torvalds 	return 0;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds fail_nomem:
7151da177e4SLinus Torvalds 	return retval;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
718a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
7191da177e4SLinus Torvalds {
720498052bbSAl Viro 	struct fs_struct *fs = current->fs;
7211da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
722498052bbSAl Viro 		/* tsk->fs is already what we want */
723498052bbSAl Viro 		write_lock(&fs->lock);
724498052bbSAl Viro 		if (fs->in_exec) {
725498052bbSAl Viro 			write_unlock(&fs->lock);
726498052bbSAl Viro 			return -EAGAIN;
727498052bbSAl Viro 		}
728498052bbSAl Viro 		fs->users++;
729498052bbSAl Viro 		write_unlock(&fs->lock);
7301da177e4SLinus Torvalds 		return 0;
7311da177e4SLinus Torvalds 	}
732498052bbSAl Viro 	tsk->fs = copy_fs_struct(fs);
7331da177e4SLinus Torvalds 	if (!tsk->fs)
7341da177e4SLinus Torvalds 		return -ENOMEM;
7351da177e4SLinus Torvalds 	return 0;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
738a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
739a016f338SJANAK DESAI {
740a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
741a016f338SJANAK DESAI 	int error = 0;
742a016f338SJANAK DESAI 
743a016f338SJANAK DESAI 	/*
744a016f338SJANAK DESAI 	 * A background process may not have any files ...
745a016f338SJANAK DESAI 	 */
746a016f338SJANAK DESAI 	oldf = current->files;
747a016f338SJANAK DESAI 	if (!oldf)
748a016f338SJANAK DESAI 		goto out;
749a016f338SJANAK DESAI 
750a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
751a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
752a016f338SJANAK DESAI 		goto out;
753a016f338SJANAK DESAI 	}
754a016f338SJANAK DESAI 
755a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
756a016f338SJANAK DESAI 	if (!newf)
757a016f338SJANAK DESAI 		goto out;
758a016f338SJANAK DESAI 
759a016f338SJANAK DESAI 	tsk->files = newf;
760a016f338SJANAK DESAI 	error = 0;
761a016f338SJANAK DESAI out:
762a016f338SJANAK DESAI 	return error;
763a016f338SJANAK DESAI }
764a016f338SJANAK DESAI 
765fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
766fd0928dfSJens Axboe {
767fd0928dfSJens Axboe #ifdef CONFIG_BLOCK
768fd0928dfSJens Axboe 	struct io_context *ioc = current->io_context;
769fd0928dfSJens Axboe 
770fd0928dfSJens Axboe 	if (!ioc)
771fd0928dfSJens Axboe 		return 0;
772fadad878SJens Axboe 	/*
773fadad878SJens Axboe 	 * Share io context with parent, if CLONE_IO is set
774fadad878SJens Axboe 	 */
775fadad878SJens Axboe 	if (clone_flags & CLONE_IO) {
776fadad878SJens Axboe 		tsk->io_context = ioc_task_link(ioc);
777fadad878SJens Axboe 		if (unlikely(!tsk->io_context))
778fadad878SJens Axboe 			return -ENOMEM;
779fadad878SJens Axboe 	} else if (ioprio_valid(ioc->ioprio)) {
780fd0928dfSJens Axboe 		tsk->io_context = alloc_io_context(GFP_KERNEL, -1);
781fd0928dfSJens Axboe 		if (unlikely(!tsk->io_context))
782fd0928dfSJens Axboe 			return -ENOMEM;
783fd0928dfSJens Axboe 
784fd0928dfSJens Axboe 		tsk->io_context->ioprio = ioc->ioprio;
785fd0928dfSJens Axboe 	}
786fd0928dfSJens Axboe #endif
787fd0928dfSJens Axboe 	return 0;
788fd0928dfSJens Axboe }
789fd0928dfSJens Axboe 
790a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	struct sighand_struct *sig;
7931da177e4SLinus Torvalds 
79460348802SZhaolei 	if (clone_flags & CLONE_SIGHAND) {
7951da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
7961da177e4SLinus Torvalds 		return 0;
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
799e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
8001da177e4SLinus Torvalds 	if (!sig)
8011da177e4SLinus Torvalds 		return -ENOMEM;
8021da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
8031da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
8041da177e4SLinus Torvalds 	return 0;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
807a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
808c81addc9SOleg Nesterov {
809c81addc9SOleg Nesterov 	if (atomic_dec_and_test(&sighand->count))
810c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
811c81addc9SOleg Nesterov }
812c81addc9SOleg Nesterov 
813f06febc9SFrank Mayhar 
814f06febc9SFrank Mayhar /*
815f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a thread group.
816f06febc9SFrank Mayhar  */
817f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig)
818f06febc9SFrank Mayhar {
819f06febc9SFrank Mayhar 	/* Thread group counters. */
820f06febc9SFrank Mayhar 	thread_group_cputime_init(sig);
821f06febc9SFrank Mayhar 
822f06febc9SFrank Mayhar 	/* Expiration times and increments. */
82342c4ab41SStanislaw Gruszka 	sig->it[CPUCLOCK_PROF].expires = cputime_zero;
82442c4ab41SStanislaw Gruszka 	sig->it[CPUCLOCK_PROF].incr = cputime_zero;
82542c4ab41SStanislaw Gruszka 	sig->it[CPUCLOCK_VIRT].expires = cputime_zero;
82642c4ab41SStanislaw Gruszka 	sig->it[CPUCLOCK_VIRT].incr = cputime_zero;
827f06febc9SFrank Mayhar 
828f06febc9SFrank Mayhar 	/* Cached expiration times. */
829f06febc9SFrank Mayhar 	sig->cputime_expires.prof_exp = cputime_zero;
830f06febc9SFrank Mayhar 	sig->cputime_expires.virt_exp = cputime_zero;
831f06febc9SFrank Mayhar 	sig->cputime_expires.sched_exp = 0;
832f06febc9SFrank Mayhar 
8336279a751SOleg Nesterov 	if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
8346279a751SOleg Nesterov 		sig->cputime_expires.prof_exp =
8356279a751SOleg Nesterov 			secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
8366279a751SOleg Nesterov 		sig->cputimer.running = 1;
8376279a751SOleg Nesterov 	}
8386279a751SOleg Nesterov 
839f06febc9SFrank Mayhar 	/* The timer lists. */
840f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
841f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
842f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
843f06febc9SFrank Mayhar }
844f06febc9SFrank Mayhar 
845a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
8461da177e4SLinus Torvalds {
8471da177e4SLinus Torvalds 	struct signal_struct *sig;
8481da177e4SLinus Torvalds 
8494ab6c083SOleg Nesterov 	if (clone_flags & CLONE_THREAD)
850490dea45SPeter Zijlstra 		return 0;
8516279a751SOleg Nesterov 
8521da177e4SLinus Torvalds 	sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
8531da177e4SLinus Torvalds 	tsk->signal = sig;
8541da177e4SLinus Torvalds 	if (!sig)
8551da177e4SLinus Torvalds 		return -ENOMEM;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
8581da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
8591da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
8601da177e4SLinus Torvalds 	sig->flags = 0;
861b3bfa0cbSSukadev Bhattiprolu 	if (clone_flags & CLONE_NEWPID)
862b3bfa0cbSSukadev Bhattiprolu 		sig->flags |= SIGNAL_UNKILLABLE;
8631da177e4SLinus Torvalds 	sig->group_exit_code = 0;
8641da177e4SLinus Torvalds 	sig->group_exit_task = NULL;
8651da177e4SLinus Torvalds 	sig->group_stop_count = 0;
866db51aeccSOleg Nesterov 	sig->curr_target = tsk;
8671da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
8681da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
8691da177e4SLinus Torvalds 
870c9cb2e3dSThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8712ff678b8SThomas Gleixner 	sig->it_real_incr.tv64 = 0;
8721da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	sig->leader = 0;	/* session leadership doesn't inherit */
875ab521dc0SEric W. Biederman 	sig->tty_old_pgrp = NULL;
8769c9f4dedSAlan Cox 	sig->tty = NULL;
8771da177e4SLinus Torvalds 
87832bd671dSPeter Zijlstra 	sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
8799ac52315SLaurent Vivier 	sig->gtime = cputime_zero;
8809ac52315SLaurent Vivier 	sig->cgtime = cputime_zero;
8811da177e4SLinus Torvalds 	sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
8821da177e4SLinus Torvalds 	sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
8836eaeeabaSEric Dumazet 	sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0;
8841f10206cSJiri Pirko 	sig->maxrss = sig->cmaxrss = 0;
8855995477aSAndrea Righi 	task_io_accounting_init(&sig->ioac);
88632bd671dSPeter Zijlstra 	sig->sum_sched_runtime = 0;
887ad4ecbcbSShailabh Nagar 	taskstats_tgid_init(sig);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	task_lock(current->group_leader);
8901da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
8911da177e4SLinus Torvalds 	task_unlock(current->group_leader);
8921da177e4SLinus Torvalds 
8936279a751SOleg Nesterov 	posix_cpu_timers_init_group(sig);
8946279a751SOleg Nesterov 
8950e464814SKaiGai Kohei 	acct_init_pacct(&sig->pacct);
8961da177e4SLinus Torvalds 
897522ed776SMiloslav Trmac 	tty_audit_fork(sig);
898522ed776SMiloslav Trmac 
89928b83c51SKOSAKI Motohiro 	sig->oom_adj = current->signal->oom_adj;
90028b83c51SKOSAKI Motohiro 
9011da177e4SLinus Torvalds 	return 0;
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
9046b3934efSOleg Nesterov void __cleanup_signal(struct signal_struct *sig)
9056b3934efSOleg Nesterov {
906f06febc9SFrank Mayhar 	thread_group_cputime_free(sig);
9079c9f4dedSAlan Cox 	tty_kref_put(sig->tty);
9086b3934efSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
9096b3934efSOleg Nesterov }
9106b3934efSOleg Nesterov 
911a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p)
9121da177e4SLinus Torvalds {
9131da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
9141da177e4SLinus Torvalds 
91583144186SRafael J. Wysocki 	new_flags &= ~PF_SUPERPRIV;
9161da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
91709a05394SRoland McGrath 	new_flags |= PF_STARTING;
9181da177e4SLinus Torvalds 	p->flags = new_flags;
9192e131895SRafael J. Wysocki 	clear_freeze_flag(p);
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds 
92217da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
9231da177e4SLinus Torvalds {
9241da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
9251da177e4SLinus Torvalds 
926b488893aSPavel Emelyanov 	return task_pid_vnr(current);
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
929a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p)
93023f78d4aSIngo Molnar {
93123f78d4aSIngo Molnar 	spin_lock_init(&p->pi_lock);
932e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES
93323f78d4aSIngo Molnar 	plist_head_init(&p->pi_waiters, &p->pi_lock);
93423f78d4aSIngo Molnar 	p->pi_blocked_on = NULL;
93523f78d4aSIngo Molnar #endif
93623f78d4aSIngo Molnar }
93723f78d4aSIngo Molnar 
938cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER
939cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
940cf475ad2SBalbir Singh {
941cf475ad2SBalbir Singh 	mm->owner = p;
942cf475ad2SBalbir Singh }
943cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */
944cf475ad2SBalbir Singh 
9451da177e4SLinus Torvalds /*
946f06febc9SFrank Mayhar  * Initialize POSIX timer handling for a single task.
947f06febc9SFrank Mayhar  */
948f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk)
949f06febc9SFrank Mayhar {
950f06febc9SFrank Mayhar 	tsk->cputime_expires.prof_exp = cputime_zero;
951f06febc9SFrank Mayhar 	tsk->cputime_expires.virt_exp = cputime_zero;
952f06febc9SFrank Mayhar 	tsk->cputime_expires.sched_exp = 0;
953f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[0]);
954f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[1]);
955f06febc9SFrank Mayhar 	INIT_LIST_HEAD(&tsk->cpu_timers[2]);
956f06febc9SFrank Mayhar }
957f06febc9SFrank Mayhar 
958f06febc9SFrank Mayhar /*
9591da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
9601da177e4SLinus Torvalds  * but does not actually start it yet.
9611da177e4SLinus Torvalds  *
9621da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
9631da177e4SLinus Torvalds  * parts of the process environment (as per the clone
9641da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
9651da177e4SLinus Torvalds  */
96636c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags,
9671da177e4SLinus Torvalds 					unsigned long stack_start,
9681da177e4SLinus Torvalds 					struct pt_regs *regs,
9691da177e4SLinus Torvalds 					unsigned long stack_size,
9701da177e4SLinus Torvalds 					int __user *child_tidptr,
97109a05394SRoland McGrath 					struct pid *pid,
97209a05394SRoland McGrath 					int trace)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds 	int retval;
975a24efe62SMariusz Kozlowski 	struct task_struct *p;
976b4f48b63SPaul Menage 	int cgroup_callbacks_done = 0;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
9791da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	/*
9821da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
9831da177e4SLinus Torvalds 	 * can only be started up within the thread group.
9841da177e4SLinus Torvalds 	 */
9851da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
9861da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	/*
9891da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
9901da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
9911da177e4SLinus Torvalds 	 * for various simplifications in other code.
9921da177e4SLinus Torvalds 	 */
9931da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
9941da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9951da177e4SLinus Torvalds 
996123be07bSSukadev Bhattiprolu 	/*
997123be07bSSukadev Bhattiprolu 	 * Siblings of global init remain as zombies on exit since they are
998123be07bSSukadev Bhattiprolu 	 * not reaped by their parent (swapper). To solve this and to avoid
999123be07bSSukadev Bhattiprolu 	 * multi-rooted process trees, prevent global and container-inits
1000123be07bSSukadev Bhattiprolu 	 * from creating siblings.
1001123be07bSSukadev Bhattiprolu 	 */
1002123be07bSSukadev Bhattiprolu 	if ((clone_flags & CLONE_PARENT) &&
1003123be07bSSukadev Bhattiprolu 				current->signal->flags & SIGNAL_UNKILLABLE)
1004123be07bSSukadev Bhattiprolu 		return ERR_PTR(-EINVAL);
1005123be07bSSukadev Bhattiprolu 
10061da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
10071da177e4SLinus Torvalds 	if (retval)
10081da177e4SLinus Torvalds 		goto fork_out;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	retval = -ENOMEM;
10111da177e4SLinus Torvalds 	p = dup_task_struct(current);
10121da177e4SLinus Torvalds 	if (!p)
10131da177e4SLinus Torvalds 		goto fork_out;
10141da177e4SLinus Torvalds 
1015f7e8b616SSteven Rostedt 	ftrace_graph_init_task(p);
1016f7e8b616SSteven Rostedt 
1017bea493a0SPeter Zijlstra 	rt_mutex_init_task(p);
1018bea493a0SPeter Zijlstra 
1019d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING
1020de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1021de30a2b3SIngo Molnar 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1022de30a2b3SIngo Molnar #endif
10231da177e4SLinus Torvalds 	retval = -EAGAIN;
10243b11a1deSDavid Howells 	if (atomic_read(&p->real_cred->user->processes) >=
10251da177e4SLinus Torvalds 			p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
10261da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
102718b6e041SSerge Hallyn 		    p->real_cred->user != INIT_USER)
10281da177e4SLinus Torvalds 			goto bad_fork_free;
10291da177e4SLinus Torvalds 	}
10301da177e4SLinus Torvalds 
1031f1752eecSDavid Howells 	retval = copy_creds(p, clone_flags);
1032f1752eecSDavid Howells 	if (retval < 0)
1033f1752eecSDavid Howells 		goto bad_fork_free;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	/*
10361da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
10371da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
10381da177e4SLinus Torvalds 	 * to stop root fork bombs.
10391da177e4SLinus Torvalds 	 */
104004ec93feSLi Zefan 	retval = -EAGAIN;
10411da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
10421da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
10431da177e4SLinus Torvalds 
1044a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
10451da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	p->did_exec = 0;
1048ca74e92bSShailabh Nagar 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
10491da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
10501da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
10511da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
1052f41d911fSPaul E. McKenney 	rcu_copy_process(p);
10531da177e4SLinus Torvalds 	p->vfork_done = NULL;
10541da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	init_sigpending(&p->pending);
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	p->utime = cputime_zero;
10591da177e4SLinus Torvalds 	p->stime = cputime_zero;
10609ac52315SLaurent Vivier 	p->gtime = cputime_zero;
1061c66f08beSMichael Neuling 	p->utimescaled = cputime_zero;
1062c66f08beSMichael Neuling 	p->stimescaled = cputime_zero;
106373a2bcb0SPeter Zijlstra 	p->prev_utime = cputime_zero;
10649301899bSBalbir Singh 	p->prev_stime = cputime_zero;
1065172ba844SBalbir Singh 
10666976675dSArjan van de Ven 	p->default_timer_slack_ns = current->timer_slack_ns;
10676976675dSArjan van de Ven 
10685995477aSAndrea Righi 	task_io_accounting_init(&p->ioac);
10691da177e4SLinus Torvalds 	acct_clear_integrals(p);
10701da177e4SLinus Torvalds 
1071f06febc9SFrank Mayhar 	posix_cpu_timers_init(p);
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	p->lock_depth = -1;		/* -1 = no lock */
10741da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
1075924b42d5STomas Janousek 	p->real_start_time = p->start_time;
1076924b42d5STomas Janousek 	monotonic_to_bootbased(&p->real_start_time);
10771da177e4SLinus Torvalds 	p->io_context = NULL;
10781da177e4SLinus Torvalds 	p->audit_context = NULL;
1079b4f48b63SPaul Menage 	cgroup_fork(p);
10801da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1081846a16bfSLee Schermerhorn 	p->mempolicy = mpol_dup(p->mempolicy);
10821da177e4SLinus Torvalds  	if (IS_ERR(p->mempolicy)) {
10831da177e4SLinus Torvalds  		retval = PTR_ERR(p->mempolicy);
10841da177e4SLinus Torvalds  		p->mempolicy = NULL;
1085b4f48b63SPaul Menage  		goto bad_fork_cleanup_cgroup;
10861da177e4SLinus Torvalds  	}
1087c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(p);
10881da177e4SLinus Torvalds #endif
1089de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1090de30a2b3SIngo Molnar 	p->irq_events = 0;
1091b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1092b36e4758SRussell King 	p->hardirqs_enabled = 1;
1093b36e4758SRussell King #else
1094de30a2b3SIngo Molnar 	p->hardirqs_enabled = 0;
1095b36e4758SRussell King #endif
1096de30a2b3SIngo Molnar 	p->hardirq_enable_ip = 0;
1097de30a2b3SIngo Molnar 	p->hardirq_enable_event = 0;
1098de30a2b3SIngo Molnar 	p->hardirq_disable_ip = _THIS_IP_;
1099de30a2b3SIngo Molnar 	p->hardirq_disable_event = 0;
1100de30a2b3SIngo Molnar 	p->softirqs_enabled = 1;
1101de30a2b3SIngo Molnar 	p->softirq_enable_ip = _THIS_IP_;
1102de30a2b3SIngo Molnar 	p->softirq_enable_event = 0;
1103de30a2b3SIngo Molnar 	p->softirq_disable_ip = 0;
1104de30a2b3SIngo Molnar 	p->softirq_disable_event = 0;
1105de30a2b3SIngo Molnar 	p->hardirq_context = 0;
1106de30a2b3SIngo Molnar 	p->softirq_context = 0;
1107de30a2b3SIngo Molnar #endif
1108fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1109fbb9ce95SIngo Molnar 	p->lockdep_depth = 0; /* no locks held yet */
1110fbb9ce95SIngo Molnar 	p->curr_chain_key = 0;
1111fbb9ce95SIngo Molnar 	p->lockdep_recursion = 0;
1112fbb9ce95SIngo Molnar #endif
11131da177e4SLinus Torvalds 
1114408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1115408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1116408894eeSIngo Molnar #endif
11170f481406SMarkus Metzger 
11180f481406SMarkus Metzger 	p->bts = NULL;
1119408894eeSIngo Molnar 
1120d899bf7bSStefani Seibold 	p->stack_start = stack_start;
1121d899bf7bSStefani Seibold 
11223c90e6e9SSrivatsa Vaddagiri 	/* Perform scheduler related setup. Assign this task to a CPU. */
11233c90e6e9SSrivatsa Vaddagiri 	sched_fork(p, clone_flags);
11246ab423e0SPeter Zijlstra 
1125cdd6c482SIngo Molnar 	retval = perf_event_init_task(p);
11266ab423e0SPeter Zijlstra 	if (retval)
11276ab423e0SPeter Zijlstra 		goto bad_fork_cleanup_policy;
11283c90e6e9SSrivatsa Vaddagiri 
11291da177e4SLinus Torvalds 	if ((retval = audit_alloc(p)))
1130f1752eecSDavid Howells 		goto bad_fork_cleanup_policy;
11311da177e4SLinus Torvalds 	/* copy all the process information */
11321da177e4SLinus Torvalds 	if ((retval = copy_semundo(clone_flags, p)))
11331da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
11341da177e4SLinus Torvalds 	if ((retval = copy_files(clone_flags, p)))
11351da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
11361da177e4SLinus Torvalds 	if ((retval = copy_fs(clone_flags, p)))
11371da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
11381da177e4SLinus Torvalds 	if ((retval = copy_sighand(clone_flags, p)))
11391da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
11401da177e4SLinus Torvalds 	if ((retval = copy_signal(clone_flags, p)))
11411da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
11421da177e4SLinus Torvalds 	if ((retval = copy_mm(clone_flags, p)))
11431da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
1144ab516013SSerge E. Hallyn 	if ((retval = copy_namespaces(clone_flags, p)))
1145d84f4f99SDavid Howells 		goto bad_fork_cleanup_mm;
1146fadad878SJens Axboe 	if ((retval = copy_io(clone_flags, p)))
1147fd0928dfSJens Axboe 		goto bad_fork_cleanup_namespaces;
11486f2c55b8SAlexey Dobriyan 	retval = copy_thread(clone_flags, stack_start, stack_size, p, regs);
11491da177e4SLinus Torvalds 	if (retval)
1150fd0928dfSJens Axboe 		goto bad_fork_cleanup_io;
11511da177e4SLinus Torvalds 
1152425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid) {
1153425fb2b4SPavel Emelyanov 		retval = -ENOMEM;
115461bce0f1SEric W. Biederman 		pid = alloc_pid(p->nsproxy->pid_ns);
1155425fb2b4SPavel Emelyanov 		if (!pid)
1156fd0928dfSJens Axboe 			goto bad_fork_cleanup_io;
11576f4e6433SPavel Emelyanov 
11586f4e6433SPavel Emelyanov 		if (clone_flags & CLONE_NEWPID) {
115961bce0f1SEric W. Biederman 			retval = pid_ns_prepare_proc(p->nsproxy->pid_ns);
11606f4e6433SPavel Emelyanov 			if (retval < 0)
11616f4e6433SPavel Emelyanov 				goto bad_fork_free_pid;
11626f4e6433SPavel Emelyanov 		}
1163425fb2b4SPavel Emelyanov 	}
1164425fb2b4SPavel Emelyanov 
1165425fb2b4SPavel Emelyanov 	p->pid = pid_nr(pid);
1166425fb2b4SPavel Emelyanov 	p->tgid = p->pid;
1167425fb2b4SPavel Emelyanov 	if (clone_flags & CLONE_THREAD)
1168425fb2b4SPavel Emelyanov 		p->tgid = current->tgid;
1169425fb2b4SPavel Emelyanov 
1170e885dcdeSSerge E. Hallyn 	if (current->nsproxy != p->nsproxy) {
1171e885dcdeSSerge E. Hallyn 		retval = ns_cgroup_clone(p, pid);
1172e885dcdeSSerge E. Hallyn 		if (retval)
1173f7e8b616SSteven Rostedt 			goto bad_fork_free_pid;
1174e885dcdeSSerge E. Hallyn 	}
1175e885dcdeSSerge E. Hallyn 
11761da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
11771da177e4SLinus Torvalds 	/*
11781da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
11791da177e4SLinus Torvalds 	 */
11801da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
118142b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
11828f17d3a5SIngo Molnar 	p->robust_list = NULL;
11838f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
11848f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
11858f17d3a5SIngo Molnar #endif
1186c87e2837SIngo Molnar 	INIT_LIST_HEAD(&p->pi_state_list);
1187c87e2837SIngo Molnar 	p->pi_state_cache = NULL;
118842b2dd0aSAlexey Dobriyan #endif
11891da177e4SLinus Torvalds 	/*
1190f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1191f9a3879aSGOTO Masanori 	 */
1192f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1193f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1194f9a3879aSGOTO Masanori 
1195f9a3879aSGOTO Masanori 	/*
11961da177e4SLinus Torvalds 	 * Syscall tracing should be turned off in the child regardless
11971da177e4SLinus Torvalds 	 * of CLONE_PTRACE.
11981da177e4SLinus Torvalds 	 */
11991da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1200ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1201ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1202ed75e8d5SLaurent Vivier #endif
12039745512cSArjan van de Ven 	clear_all_latency_tracing(p);
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
12061da177e4SLinus Torvalds 	p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
12071da177e4SLinus Torvalds 	p->pdeath_signal = 0;
12081da177e4SLinus Torvalds 	p->exit_state = 0;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	/*
12111da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
12121da177e4SLinus Torvalds 	 * We dont wake it up yet.
12131da177e4SLinus Torvalds 	 */
12141da177e4SLinus Torvalds 	p->group_leader = p;
121547e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
12161da177e4SLinus Torvalds 
1217b4f48b63SPaul Menage 	/* Now that the task is set up, run cgroup callbacks if
1218b4f48b63SPaul Menage 	 * necessary. We need to run them before the task is visible
1219b4f48b63SPaul Menage 	 * on the tasklist. */
1220b4f48b63SPaul Menage 	cgroup_fork_callbacks(p);
1221b4f48b63SPaul Menage 	cgroup_callbacks_done = 1;
1222b4f48b63SPaul Menage 
12231da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
12241da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	/*
1227476d139cSNick Piggin 	 * The task hasn't been attached yet, so its cpus_allowed mask will
1228476d139cSNick Piggin 	 * not be changed, nor will its assigned CPU.
1229476d139cSNick Piggin 	 *
1230476d139cSNick Piggin 	 * The cpus_allowed mask of the parent may have changed after it was
1231476d139cSNick Piggin 	 * copied first time - so re-copy it here, then check the child's CPU
1232476d139cSNick Piggin 	 * to ensure it is on a valid CPU (and if not, just force it back to
1233476d139cSNick Piggin 	 * parent's CPU). This avoids alot of nasty races.
12341da177e4SLinus Torvalds 	 */
12351da177e4SLinus Torvalds 	p->cpus_allowed = current->cpus_allowed;
12366f505b16SPeter Zijlstra 	p->rt.nr_cpus_allowed = current->rt.nr_cpus_allowed;
123726ff6ad9SSrivatsa Vaddagiri 	if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
123826ff6ad9SSrivatsa Vaddagiri 			!cpu_online(task_cpu(p))))
12391da177e4SLinus Torvalds 		set_task_cpu(p, smp_processor_id());
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
12422d5516cbSOleg Nesterov 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
12431da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
12442d5516cbSOleg Nesterov 		p->parent_exec_id = current->parent_exec_id;
12452d5516cbSOleg Nesterov 	} else {
12461da177e4SLinus Torvalds 		p->real_parent = current;
12472d5516cbSOleg Nesterov 		p->parent_exec_id = current->self_exec_id;
12482d5516cbSOleg Nesterov 	}
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
12514a2c7a78SOleg Nesterov 
12524a2c7a78SOleg Nesterov 	/*
12534a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
12544a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
12554a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
12564a2c7a78SOleg Nesterov 	 * it's process group.
12574a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
12584a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
12594a2c7a78SOleg Nesterov  	 */
12604a2c7a78SOleg Nesterov 	recalc_sigpending();
12614a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
12624a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
12634a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
12644a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
1265f7e8b616SSteven Rostedt 		goto bad_fork_free_pid;
12664a2c7a78SOleg Nesterov 	}
12674a2c7a78SOleg Nesterov 
12683f17da69SOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
12694ab6c083SOleg Nesterov 		atomic_inc(&current->signal->count);
12704ab6c083SOleg Nesterov 		atomic_inc(&current->signal->live);
12711da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
127247e65328SOleg Nesterov 		list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 
127573b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
1276f470021aSRoland McGrath 		list_add_tail(&p->sibling, &p->real_parent->children);
127709a05394SRoland McGrath 		tracehook_finish_clone(p, clone_flags, trace);
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
12805cd17569SEric W. Biederman 			if (clone_flags & CLONE_NEWPID)
128130e49c26SPavel Emelyanov 				p->nsproxy->pid_ns->child_reaper = p;
12825cd17569SEric W. Biederman 
1283fea9d175SOleg Nesterov 			p->signal->leader_pid = pid;
12849c9f4dedSAlan Cox 			tty_kref_put(p->signal->tty);
12859c9f4dedSAlan Cox 			p->signal->tty = tty_kref_get(current->signal->tty);
12865cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
12875cd17569SEric W. Biederman 			attach_pid(p, PIDTYPE_SID, task_session(current));
12885e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
12891da177e4SLinus Torvalds 			__get_cpu_var(process_counts)++;
12901da177e4SLinus Torvalds 		}
129185868995SSukadev Bhattiprolu 		attach_pid(p, PIDTYPE_PID, pid);
12921da177e4SLinus Torvalds 		nr_threads++;
129373b9ebfeSOleg Nesterov 	}
129473b9ebfeSOleg Nesterov 
12951da177e4SLinus Torvalds 	total_forks++;
12963f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
12971da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1298c13cf856SAndrew Morton 	proc_fork_connector(p);
1299817929ecSPaul Menage 	cgroup_post_fork(p);
1300cdd6c482SIngo Molnar 	perf_event_fork(p);
13011da177e4SLinus Torvalds 	return p;
13021da177e4SLinus Torvalds 
1303425fb2b4SPavel Emelyanov bad_fork_free_pid:
1304425fb2b4SPavel Emelyanov 	if (pid != &init_struct_pid)
1305425fb2b4SPavel Emelyanov 		free_pid(pid);
1306fd0928dfSJens Axboe bad_fork_cleanup_io:
1307fd0928dfSJens Axboe 	put_io_context(p->io_context);
1308ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces:
1309444f378bSLinus Torvalds 	exit_task_namespaces(p);
13101da177e4SLinus Torvalds bad_fork_cleanup_mm:
13111da177e4SLinus Torvalds 	if (p->mm)
13121da177e4SLinus Torvalds 		mmput(p->mm);
13131da177e4SLinus Torvalds bad_fork_cleanup_signal:
13144ab6c083SOleg Nesterov 	if (!(clone_flags & CLONE_THREAD))
13154ab6c083SOleg Nesterov 		__cleanup_signal(p->signal);
13161da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1317a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
13181da177e4SLinus Torvalds bad_fork_cleanup_fs:
13191da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
13201da177e4SLinus Torvalds bad_fork_cleanup_files:
13211da177e4SLinus Torvalds 	exit_files(p); /* blocking */
13221da177e4SLinus Torvalds bad_fork_cleanup_semundo:
13231da177e4SLinus Torvalds 	exit_sem(p);
13241da177e4SLinus Torvalds bad_fork_cleanup_audit:
13251da177e4SLinus Torvalds 	audit_free(p);
13261da177e4SLinus Torvalds bad_fork_cleanup_policy:
1327cdd6c482SIngo Molnar 	perf_event_free_task(p);
13281da177e4SLinus Torvalds #ifdef CONFIG_NUMA
1329f0be3d32SLee Schermerhorn 	mpol_put(p->mempolicy);
1330b4f48b63SPaul Menage bad_fork_cleanup_cgroup:
13311da177e4SLinus Torvalds #endif
1332b4f48b63SPaul Menage 	cgroup_exit(p, cgroup_callbacks_done);
133335df17c5SShailabh Nagar 	delayacct_tsk_free(p);
1334a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
13351da177e4SLinus Torvalds bad_fork_cleanup_count:
1336d84f4f99SDavid Howells 	atomic_dec(&p->cred->user->processes);
1337e0e81739SDavid Howells 	exit_creds(p);
13381da177e4SLinus Torvalds bad_fork_free:
13391da177e4SLinus Torvalds 	free_task(p);
1340fe7d37d1SOleg Nesterov fork_out:
1341fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
13421da177e4SLinus Torvalds }
13431da177e4SLinus Torvalds 
13446b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
13471da177e4SLinus Torvalds 	return regs;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
13509abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu)
13511da177e4SLinus Torvalds {
135236c8b586SIngo Molnar 	struct task_struct *task;
13531da177e4SLinus Torvalds 	struct pt_regs regs;
13541da177e4SLinus Torvalds 
135530e49c26SPavel Emelyanov 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL,
135609a05394SRoland McGrath 			    &init_struct_pid, 0);
1357753ca4f3SAkinobu Mita 	if (!IS_ERR(task))
13581da177e4SLinus Torvalds 		init_idle(task, cpu);
135973b9ebfeSOleg Nesterov 
13601da177e4SLinus Torvalds 	return task;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds /*
13641da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
13651da177e4SLinus Torvalds  *
13661da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
13671da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
13681da177e4SLinus Torvalds  */
13691da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
13701da177e4SLinus Torvalds 	      unsigned long stack_start,
13711da177e4SLinus Torvalds 	      struct pt_regs *regs,
13721da177e4SLinus Torvalds 	      unsigned long stack_size,
13731da177e4SLinus Torvalds 	      int __user *parent_tidptr,
13741da177e4SLinus Torvalds 	      int __user *child_tidptr)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	struct task_struct *p;
13771da177e4SLinus Torvalds 	int trace = 0;
137892476d7fSEric W. Biederman 	long nr;
13791da177e4SLinus Torvalds 
1380bdff746aSAndrew Morton 	/*
138118b6e041SSerge Hallyn 	 * Do some preliminary argument and permissions checking before we
138218b6e041SSerge Hallyn 	 * actually start allocating stuff
138318b6e041SSerge Hallyn 	 */
138418b6e041SSerge Hallyn 	if (clone_flags & CLONE_NEWUSER) {
138518b6e041SSerge Hallyn 		if (clone_flags & CLONE_THREAD)
138618b6e041SSerge Hallyn 			return -EINVAL;
138718b6e041SSerge Hallyn 		/* hopefully this check will go away when userns support is
138818b6e041SSerge Hallyn 		 * complete
138918b6e041SSerge Hallyn 		 */
13907657d904SSerge E. Hallyn 		if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) ||
13917657d904SSerge E. Hallyn 				!capable(CAP_SETGID))
139218b6e041SSerge Hallyn 			return -EPERM;
139318b6e041SSerge Hallyn 	}
139418b6e041SSerge Hallyn 
139518b6e041SSerge Hallyn 	/*
1396bdff746aSAndrew Morton 	 * We hope to recycle these flags after 2.6.26
1397bdff746aSAndrew Morton 	 */
1398bdff746aSAndrew Morton 	if (unlikely(clone_flags & CLONE_STOPPED)) {
1399bdff746aSAndrew Morton 		static int __read_mostly count = 100;
1400bdff746aSAndrew Morton 
1401bdff746aSAndrew Morton 		if (count > 0 && printk_ratelimit()) {
1402bdff746aSAndrew Morton 			char comm[TASK_COMM_LEN];
1403bdff746aSAndrew Morton 
1404bdff746aSAndrew Morton 			count--;
1405bdff746aSAndrew Morton 			printk(KERN_INFO "fork(): process `%s' used deprecated "
1406bdff746aSAndrew Morton 					"clone flags 0x%lx\n",
1407bdff746aSAndrew Morton 				get_task_comm(comm, current),
1408bdff746aSAndrew Morton 				clone_flags & CLONE_STOPPED);
1409bdff746aSAndrew Morton 		}
1410bdff746aSAndrew Morton 	}
1411bdff746aSAndrew Morton 
141209a05394SRoland McGrath 	/*
141309a05394SRoland McGrath 	 * When called from kernel_thread, don't do user tracing stuff.
141409a05394SRoland McGrath 	 */
141509a05394SRoland McGrath 	if (likely(user_mode(regs)))
141609a05394SRoland McGrath 		trace = tracehook_prepare_clone(clone_flags);
14171da177e4SLinus Torvalds 
1418a6f5e063SSukadev Bhattiprolu 	p = copy_process(clone_flags, stack_start, regs, stack_size,
141909a05394SRoland McGrath 			 child_tidptr, NULL, trace);
14201da177e4SLinus Torvalds 	/*
14211da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
14221da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
14231da177e4SLinus Torvalds 	 */
14241da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
14251da177e4SLinus Torvalds 		struct completion vfork;
14261da177e4SLinus Torvalds 
14270a16b607SMathieu Desnoyers 		trace_sched_process_fork(current, p);
14280a16b607SMathieu Desnoyers 
14296c5f3e7bSPavel Emelyanov 		nr = task_pid_vnr(p);
143030e49c26SPavel Emelyanov 
143130e49c26SPavel Emelyanov 		if (clone_flags & CLONE_PARENT_SETTID)
143230e49c26SPavel Emelyanov 			put_user(nr, parent_tidptr);
1433a6f5e063SSukadev Bhattiprolu 
14341da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
14351da177e4SLinus Torvalds 			p->vfork_done = &vfork;
14361da177e4SLinus Torvalds 			init_completion(&vfork);
14371da177e4SLinus Torvalds 		}
14381da177e4SLinus Torvalds 
1439a64e6494SAl Viro 		audit_finish_fork(p);
1440087eb437SOleg Nesterov 		tracehook_report_clone(regs, clone_flags, nr, p);
144109a05394SRoland McGrath 
144209a05394SRoland McGrath 		/*
144309a05394SRoland McGrath 		 * We set PF_STARTING at creation in case tracing wants to
144409a05394SRoland McGrath 		 * use this to distinguish a fully live task from one that
144509a05394SRoland McGrath 		 * hasn't gotten to tracehook_report_clone() yet.  Now we
144609a05394SRoland McGrath 		 * clear it and set the child going.
144709a05394SRoland McGrath 		 */
144809a05394SRoland McGrath 		p->flags &= ~PF_STARTING;
144909a05394SRoland McGrath 
145009a05394SRoland McGrath 		if (unlikely(clone_flags & CLONE_STOPPED)) {
14511da177e4SLinus Torvalds 			/*
14521da177e4SLinus Torvalds 			 * We'll start up with an immediate SIGSTOP.
14531da177e4SLinus Torvalds 			 */
14541da177e4SLinus Torvalds 			sigaddset(&p->pending.signal, SIGSTOP);
14551da177e4SLinus Torvalds 			set_tsk_thread_flag(p, TIF_SIGPENDING);
1456d9ae90acSOleg Nesterov 			__set_task_state(p, TASK_STOPPED);
145709a05394SRoland McGrath 		} else {
145809a05394SRoland McGrath 			wake_up_new_task(p, clone_flags);
14591da177e4SLinus Torvalds 		}
14601da177e4SLinus Torvalds 
146109a05394SRoland McGrath 		tracehook_report_clone_complete(trace, regs,
146209a05394SRoland McGrath 						clone_flags, nr, p);
146309a05394SRoland McGrath 
14641da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
1465ba96a0c8SRafael J. Wysocki 			freezer_do_not_count();
14661da177e4SLinus Torvalds 			wait_for_completion(&vfork);
1467ba96a0c8SRafael J. Wysocki 			freezer_count();
1468daded34bSRoland McGrath 			tracehook_report_vfork_done(p, nr);
14699f59ce5dSChuck Ebbert 		}
14701da177e4SLinus Torvalds 	} else {
147192476d7fSEric W. Biederman 		nr = PTR_ERR(p);
14721da177e4SLinus Torvalds 	}
147392476d7fSEric W. Biederman 	return nr;
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
14765fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN
14775fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0
14785fd63b30SRavikiran G Thirumalai #endif
14795fd63b30SRavikiran G Thirumalai 
148051cc5068SAlexey Dobriyan static void sighand_ctor(void *data)
1481aa1757f9SOleg Nesterov {
1482aa1757f9SOleg Nesterov 	struct sighand_struct *sighand = data;
1483aa1757f9SOleg Nesterov 
1484aa1757f9SOleg Nesterov 	spin_lock_init(&sighand->siglock);
1485b8fceee1SDavide Libenzi 	init_waitqueue_head(&sighand->signalfd_wqh);
1486fba2afaaSDavide Libenzi }
1487aa1757f9SOleg Nesterov 
14881da177e4SLinus Torvalds void __init proc_caches_init(void)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
14911da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
14922dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
14932dff4405SVegard Nossum 			SLAB_NOTRACK, sighand_ctor);
14941da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
14951da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
14962dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
14971da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
14981da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
14992dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
15001da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
15011da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
15022dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
15031da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
15045fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
15052dff4405SVegard Nossum 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
150633e5d769SDavid Howells 	vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC);
15078feae131SDavid Howells 	mmap_init();
15081da177e4SLinus Torvalds }
1509cf2e340fSJANAK DESAI 
1510cf2e340fSJANAK DESAI /*
1511cf2e340fSJANAK DESAI  * Check constraints on flags passed to the unshare system call and
1512cf2e340fSJANAK DESAI  * force unsharing of additional process context as appropriate.
1513cf2e340fSJANAK DESAI  */
1514a39bc516SAlexey Dobriyan static void check_unshare_flags(unsigned long *flags_ptr)
1515cf2e340fSJANAK DESAI {
1516cf2e340fSJANAK DESAI 	/*
1517cf2e340fSJANAK DESAI 	 * If unsharing a thread from a thread group, must also
1518cf2e340fSJANAK DESAI 	 * unshare vm.
1519cf2e340fSJANAK DESAI 	 */
1520cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_THREAD)
1521cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_VM;
1522cf2e340fSJANAK DESAI 
1523cf2e340fSJANAK DESAI 	/*
1524cf2e340fSJANAK DESAI 	 * If unsharing vm, must also unshare signal handlers.
1525cf2e340fSJANAK DESAI 	 */
1526cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_VM)
1527cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_SIGHAND;
1528cf2e340fSJANAK DESAI 
1529cf2e340fSJANAK DESAI 	/*
1530cf2e340fSJANAK DESAI 	 * If unsharing signal handlers and the task was created
1531cf2e340fSJANAK DESAI 	 * using CLONE_THREAD, then must unshare the thread
1532cf2e340fSJANAK DESAI 	 */
1533cf2e340fSJANAK DESAI 	if ((*flags_ptr & CLONE_SIGHAND) &&
1534cf2e340fSJANAK DESAI 	    (atomic_read(&current->signal->count) > 1))
1535cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_THREAD;
1536cf2e340fSJANAK DESAI 
1537cf2e340fSJANAK DESAI 	/*
1538cf2e340fSJANAK DESAI 	 * If unsharing namespace, must also unshare filesystem information.
1539cf2e340fSJANAK DESAI 	 */
1540cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_NEWNS)
1541cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_FS;
1542cf2e340fSJANAK DESAI }
1543cf2e340fSJANAK DESAI 
1544cf2e340fSJANAK DESAI /*
1545cf2e340fSJANAK DESAI  * Unsharing of tasks created with CLONE_THREAD is not supported yet
1546cf2e340fSJANAK DESAI  */
1547cf2e340fSJANAK DESAI static int unshare_thread(unsigned long unshare_flags)
1548cf2e340fSJANAK DESAI {
1549cf2e340fSJANAK DESAI 	if (unshare_flags & CLONE_THREAD)
1550cf2e340fSJANAK DESAI 		return -EINVAL;
1551cf2e340fSJANAK DESAI 
1552cf2e340fSJANAK DESAI 	return 0;
1553cf2e340fSJANAK DESAI }
1554cf2e340fSJANAK DESAI 
1555cf2e340fSJANAK DESAI /*
155699d1419dSJANAK DESAI  * Unshare the filesystem structure if it is being shared
1557cf2e340fSJANAK DESAI  */
1558cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1559cf2e340fSJANAK DESAI {
1560cf2e340fSJANAK DESAI 	struct fs_struct *fs = current->fs;
1561cf2e340fSJANAK DESAI 
1562498052bbSAl Viro 	if (!(unshare_flags & CLONE_FS) || !fs)
1563498052bbSAl Viro 		return 0;
1564498052bbSAl Viro 
1565498052bbSAl Viro 	/* don't need lock here; in the worst case we'll do useless copy */
1566498052bbSAl Viro 	if (fs->users == 1)
1567498052bbSAl Viro 		return 0;
1568498052bbSAl Viro 
1569498052bbSAl Viro 	*new_fsp = copy_fs_struct(fs);
157099d1419dSJANAK DESAI 	if (!*new_fsp)
157199d1419dSJANAK DESAI 		return -ENOMEM;
1572cf2e340fSJANAK DESAI 
1573cf2e340fSJANAK DESAI 	return 0;
1574cf2e340fSJANAK DESAI }
1575cf2e340fSJANAK DESAI 
1576cf2e340fSJANAK DESAI /*
1577dae3c5a0SOleg Nesterov  * Unsharing of sighand is not supported yet
1578cf2e340fSJANAK DESAI  */
1579cf2e340fSJANAK DESAI static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
1580cf2e340fSJANAK DESAI {
1581cf2e340fSJANAK DESAI 	struct sighand_struct *sigh = current->sighand;
1582cf2e340fSJANAK DESAI 
1583dae3c5a0SOleg Nesterov 	if ((unshare_flags & CLONE_SIGHAND) && atomic_read(&sigh->count) > 1)
1584cf2e340fSJANAK DESAI 		return -EINVAL;
1585cf2e340fSJANAK DESAI 	else
1586cf2e340fSJANAK DESAI 		return 0;
1587cf2e340fSJANAK DESAI }
1588cf2e340fSJANAK DESAI 
1589cf2e340fSJANAK DESAI /*
1590a0a7ec30SJANAK DESAI  * Unshare vm if it is being shared
1591cf2e340fSJANAK DESAI  */
1592cf2e340fSJANAK DESAI static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp)
1593cf2e340fSJANAK DESAI {
1594cf2e340fSJANAK DESAI 	struct mm_struct *mm = current->mm;
1595cf2e340fSJANAK DESAI 
1596cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_VM) &&
1597a0a7ec30SJANAK DESAI 	    (mm && atomic_read(&mm->mm_users) > 1)) {
15982d61b867SOleg Nesterov 		return -EINVAL;
1599a0a7ec30SJANAK DESAI 	}
1600cf2e340fSJANAK DESAI 
1601cf2e340fSJANAK DESAI 	return 0;
1602cf2e340fSJANAK DESAI }
1603cf2e340fSJANAK DESAI 
1604cf2e340fSJANAK DESAI /*
1605a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1606cf2e340fSJANAK DESAI  */
1607cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1608cf2e340fSJANAK DESAI {
1609cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1610a016f338SJANAK DESAI 	int error = 0;
1611cf2e340fSJANAK DESAI 
1612cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1613a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1614a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1615a016f338SJANAK DESAI 		if (!*new_fdp)
1616a016f338SJANAK DESAI 			return error;
1617a016f338SJANAK DESAI 	}
1618cf2e340fSJANAK DESAI 
1619cf2e340fSJANAK DESAI 	return 0;
1620cf2e340fSJANAK DESAI }
1621cf2e340fSJANAK DESAI 
1622cf2e340fSJANAK DESAI /*
1623cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1624cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1625cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1626cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1627cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1628cf2e340fSJANAK DESAI  * task_struct.
1629cf2e340fSJANAK DESAI  */
16306559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1631cf2e340fSJANAK DESAI {
1632cf2e340fSJANAK DESAI 	int err = 0;
1633cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1634dae3c5a0SOleg Nesterov 	struct sighand_struct *new_sigh = NULL;
1635cf2e340fSJANAK DESAI 	struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
1636cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1637cf7b708cSPavel Emelyanov 	struct nsproxy *new_nsproxy = NULL;
16389edff4abSManfred Spraul 	int do_sysvsem = 0;
1639cf2e340fSJANAK DESAI 
1640cf2e340fSJANAK DESAI 	check_unshare_flags(&unshare_flags);
1641cf2e340fSJANAK DESAI 
164206f9d4f9SEric W. Biederman 	/* Return -EINVAL for all unsupported flags */
164306f9d4f9SEric W. Biederman 	err = -EINVAL;
164406f9d4f9SEric W. Biederman 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
164525b21cb2SKirill Korotaev 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
164618b6e041SSerge Hallyn 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET))
164706f9d4f9SEric W. Biederman 		goto bad_unshare_out;
164806f9d4f9SEric W. Biederman 
16496013f67fSManfred Spraul 	/*
16506013f67fSManfred Spraul 	 * CLONE_NEWIPC must also detach from the undolist: after switching
16516013f67fSManfred Spraul 	 * to a new ipc namespace, the semaphore arrays from the old
16526013f67fSManfred Spraul 	 * namespace are unreachable.
16536013f67fSManfred Spraul 	 */
16546013f67fSManfred Spraul 	if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
16559edff4abSManfred Spraul 		do_sysvsem = 1;
1656cf2e340fSJANAK DESAI 	if ((err = unshare_thread(unshare_flags)))
1657cf2e340fSJANAK DESAI 		goto bad_unshare_out;
1658cf2e340fSJANAK DESAI 	if ((err = unshare_fs(unshare_flags, &new_fs)))
1659cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_thread;
1660cf2e340fSJANAK DESAI 	if ((err = unshare_sighand(unshare_flags, &new_sigh)))
1661e3222c4eSBadari Pulavarty 		goto bad_unshare_cleanup_fs;
1662cf2e340fSJANAK DESAI 	if ((err = unshare_vm(unshare_flags, &new_mm)))
1663cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_sigh;
1664cf2e340fSJANAK DESAI 	if ((err = unshare_fd(unshare_flags, &new_fd)))
1665cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_vm;
1666e3222c4eSBadari Pulavarty 	if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
1667e3222c4eSBadari Pulavarty 			new_fs)))
16689edff4abSManfred Spraul 		goto bad_unshare_cleanup_fd;
1669cf2e340fSJANAK DESAI 
16709edff4abSManfred Spraul 	if (new_fs ||  new_mm || new_fd || do_sysvsem || new_nsproxy) {
16719edff4abSManfred Spraul 		if (do_sysvsem) {
16729edff4abSManfred Spraul 			/*
16739edff4abSManfred Spraul 			 * CLONE_SYSVSEM is equivalent to sys_exit().
16749edff4abSManfred Spraul 			 */
16759edff4abSManfred Spraul 			exit_sem(current);
16769edff4abSManfred Spraul 		}
1677ab516013SSerge E. Hallyn 
1678c0b2fc31SSerge Hallyn 		if (new_nsproxy) {
1679cf7b708cSPavel Emelyanov 			switch_task_namespaces(current, new_nsproxy);
1680cf7b708cSPavel Emelyanov 			new_nsproxy = NULL;
1681c0b2fc31SSerge Hallyn 		}
1682cf2e340fSJANAK DESAI 
1683cf7b708cSPavel Emelyanov 		task_lock(current);
1684cf7b708cSPavel Emelyanov 
1685cf2e340fSJANAK DESAI 		if (new_fs) {
1686cf2e340fSJANAK DESAI 			fs = current->fs;
1687498052bbSAl Viro 			write_lock(&fs->lock);
1688cf2e340fSJANAK DESAI 			current->fs = new_fs;
1689498052bbSAl Viro 			if (--fs->users)
1690498052bbSAl Viro 				new_fs = NULL;
1691498052bbSAl Viro 			else
1692cf2e340fSJANAK DESAI 				new_fs = fs;
1693498052bbSAl Viro 			write_unlock(&fs->lock);
1694cf2e340fSJANAK DESAI 		}
1695cf2e340fSJANAK DESAI 
1696cf2e340fSJANAK DESAI 		if (new_mm) {
1697cf2e340fSJANAK DESAI 			mm = current->mm;
1698cf2e340fSJANAK DESAI 			active_mm = current->active_mm;
1699cf2e340fSJANAK DESAI 			current->mm = new_mm;
1700cf2e340fSJANAK DESAI 			current->active_mm = new_mm;
1701cf2e340fSJANAK DESAI 			activate_mm(active_mm, new_mm);
1702cf2e340fSJANAK DESAI 			new_mm = mm;
1703cf2e340fSJANAK DESAI 		}
1704cf2e340fSJANAK DESAI 
1705cf2e340fSJANAK DESAI 		if (new_fd) {
1706cf2e340fSJANAK DESAI 			fd = current->files;
1707cf2e340fSJANAK DESAI 			current->files = new_fd;
1708cf2e340fSJANAK DESAI 			new_fd = fd;
1709cf2e340fSJANAK DESAI 		}
1710cf2e340fSJANAK DESAI 
1711cf2e340fSJANAK DESAI 		task_unlock(current);
1712cf2e340fSJANAK DESAI 	}
1713cf2e340fSJANAK DESAI 
1714c0b2fc31SSerge Hallyn 	if (new_nsproxy)
1715444f378bSLinus Torvalds 		put_nsproxy(new_nsproxy);
1716c0b2fc31SSerge Hallyn 
1717cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
1718cf2e340fSJANAK DESAI 	if (new_fd)
1719cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
1720cf2e340fSJANAK DESAI 
1721cf2e340fSJANAK DESAI bad_unshare_cleanup_vm:
1722cf2e340fSJANAK DESAI 	if (new_mm)
1723cf2e340fSJANAK DESAI 		mmput(new_mm);
1724cf2e340fSJANAK DESAI 
1725cf2e340fSJANAK DESAI bad_unshare_cleanup_sigh:
1726cf2e340fSJANAK DESAI 	if (new_sigh)
1727cf2e340fSJANAK DESAI 		if (atomic_dec_and_test(&new_sigh->count))
1728cf2e340fSJANAK DESAI 			kmem_cache_free(sighand_cachep, new_sigh);
1729cf2e340fSJANAK DESAI 
1730cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
1731cf2e340fSJANAK DESAI 	if (new_fs)
1732498052bbSAl Viro 		free_fs_struct(new_fs);
1733cf2e340fSJANAK DESAI 
1734cf2e340fSJANAK DESAI bad_unshare_cleanup_thread:
1735cf2e340fSJANAK DESAI bad_unshare_out:
1736cf2e340fSJANAK DESAI 	return err;
1737cf2e340fSJANAK DESAI }
17383b125388SAl Viro 
17393b125388SAl Viro /*
17403b125388SAl Viro  *	Helper to unshare the files of the current task.
17413b125388SAl Viro  *	We don't want to expose copy_files internals to
17423b125388SAl Viro  *	the exec layer of the kernel.
17433b125388SAl Viro  */
17443b125388SAl Viro 
17453b125388SAl Viro int unshare_files(struct files_struct **displaced)
17463b125388SAl Viro {
17473b125388SAl Viro 	struct task_struct *task = current;
174850704516SAl Viro 	struct files_struct *copy = NULL;
17493b125388SAl Viro 	int error;
17503b125388SAl Viro 
17513b125388SAl Viro 	error = unshare_fd(CLONE_FILES, &copy);
17523b125388SAl Viro 	if (error || !copy) {
17533b125388SAl Viro 		*displaced = NULL;
17543b125388SAl Viro 		return error;
17553b125388SAl Viro 	}
17563b125388SAl Viro 	*displaced = task->files;
17573b125388SAl Viro 	task_lock(task);
17583b125388SAl Viro 	task->files = copy;
17593b125388SAl Viro 	task_unlock(task);
17603b125388SAl Viro 	return 0;
17613b125388SAl Viro }
1762