xref: /openbmc/linux/kernel/fork.c (revision 0ae26f1b3159f3acb21ae1e866c3c7e16edd450f)
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/config.h>
151da177e4SLinus Torvalds #include <linux/slab.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/unistd.h>
181da177e4SLinus Torvalds #include <linux/smp_lock.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/vmalloc.h>
211da177e4SLinus Torvalds #include <linux/completion.h>
221da177e4SLinus Torvalds #include <linux/namespace.h>
231da177e4SLinus Torvalds #include <linux/personality.h>
241da177e4SLinus Torvalds #include <linux/mempolicy.h>
251da177e4SLinus Torvalds #include <linux/sem.h>
261da177e4SLinus Torvalds #include <linux/file.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>
31c59ede7bSRandy.Dunlap #include <linux/capability.h>
321da177e4SLinus Torvalds #include <linux/cpu.h>
331da177e4SLinus Torvalds #include <linux/cpuset.h>
341da177e4SLinus Torvalds #include <linux/security.h>
351da177e4SLinus Torvalds #include <linux/swap.h>
361da177e4SLinus Torvalds #include <linux/syscalls.h>
371da177e4SLinus Torvalds #include <linux/jiffies.h>
381da177e4SLinus Torvalds #include <linux/futex.h>
39ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
401da177e4SLinus Torvalds #include <linux/ptrace.h>
411da177e4SLinus Torvalds #include <linux/mount.h>
421da177e4SLinus Torvalds #include <linux/audit.h>
431da177e4SLinus Torvalds #include <linux/profile.h>
441da177e4SLinus Torvalds #include <linux/rmap.h>
451da177e4SLinus Torvalds #include <linux/acct.h>
469f46080cSMatt Helsley #include <linux/cn_proc.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #include <asm/pgtable.h>
491da177e4SLinus Torvalds #include <asm/pgalloc.h>
501da177e4SLinus Torvalds #include <asm/uaccess.h>
511da177e4SLinus Torvalds #include <asm/mmu_context.h>
521da177e4SLinus Torvalds #include <asm/cacheflush.h>
531da177e4SLinus Torvalds #include <asm/tlbflush.h>
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
591da177e4SLinus Torvalds int nr_threads; 		/* The idle threads do not count.. */
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds  __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds EXPORT_SYMBOL(tasklist_lock);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds int nr_processes(void)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	int cpu;
721da177e4SLinus Torvalds 	int total = 0;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	for_each_online_cpu(cpu)
751da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	return total;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
811da177e4SLinus Torvalds # define alloc_task_struct()	kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
821da177e4SLinus Torvalds # define free_task_struct(tsk)	kmem_cache_free(task_struct_cachep, (tsk))
831da177e4SLinus Torvalds static kmem_cache_t *task_struct_cachep;
841da177e4SLinus Torvalds #endif
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
876b3934efSOleg Nesterov static kmem_cache_t *signal_cachep;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
901da177e4SLinus Torvalds kmem_cache_t *sighand_cachep;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
931da177e4SLinus Torvalds kmem_cache_t *files_cachep;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
961da177e4SLinus Torvalds kmem_cache_t *fs_cachep;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
991da177e4SLinus Torvalds kmem_cache_t *vm_area_cachep;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
1021da177e4SLinus Torvalds static kmem_cache_t *mm_cachep;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	free_thread_info(tsk->thread_info);
1071da177e4SLinus Torvalds 	free_task_struct(tsk);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1101da177e4SLinus Torvalds 
111158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
1141da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1151da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	security_task_free(tsk);
1181da177e4SLinus Torvalds 	free_uid(tsk->user);
1191da177e4SLinus Torvalds 	put_group_info(tsk->group_info);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
1221da177e4SLinus Torvalds 		free_task(tsk);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
1281da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
1291da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
1301da177e4SLinus Torvalds #endif
1311da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
1321da177e4SLinus Torvalds 	task_struct_cachep =
1331da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
1341da177e4SLinus Torvalds 			ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
1351da177e4SLinus Torvalds #endif
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	/*
1381da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
1391da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
1401da177e4SLinus Torvalds 	 * of memory.
1411da177e4SLinus Torvalds 	 */
1421da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	/*
1451da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
1461da177e4SLinus Torvalds 	 */
1471da177e4SLinus Torvalds 	if(max_threads < 20)
1481da177e4SLinus Torvalds 		max_threads = 20;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
1511da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
1521da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
1531da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	struct task_struct *tsk;
1591da177e4SLinus Torvalds 	struct thread_info *ti;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	prepare_to_copy(orig);
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	tsk = alloc_task_struct();
1641da177e4SLinus Torvalds 	if (!tsk)
1651da177e4SLinus Torvalds 		return NULL;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	ti = alloc_thread_info(tsk);
1681da177e4SLinus Torvalds 	if (!ti) {
1691da177e4SLinus Torvalds 		free_task_struct(tsk);
1701da177e4SLinus Torvalds 		return NULL;
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	*tsk = *orig;
1741da177e4SLinus Torvalds 	tsk->thread_info = ti;
17510ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	/* One for us, one for whoever does the "release_task()" (usually parent) */
1781da177e4SLinus Torvalds 	atomic_set(&tsk->usage,2);
1794b5d37acSGiancarlo Formicuccia 	atomic_set(&tsk->fs_excl, 0);
1802056a782SJens Axboe 	tsk->btrace_seq = 0;
181a0aa7f68SJens Axboe 	tsk->splice_pipe = NULL;
1821da177e4SLinus Torvalds 	return tsk;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds #ifdef CONFIG_MMU
1861da177e4SLinus Torvalds static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, **pprev;
1891da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
1901da177e4SLinus Torvalds 	int retval;
1911da177e4SLinus Torvalds 	unsigned long charge;
1921da177e4SLinus Torvalds 	struct mempolicy *pol;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
195fd3e42fcSHugh Dickins 	flush_cache_mm(oldmm);
1967ee78232SHugh Dickins 	down_write(&mm->mmap_sem);
1977ee78232SHugh Dickins 
1981da177e4SLinus Torvalds 	mm->locked_vm = 0;
1991da177e4SLinus Torvalds 	mm->mmap = NULL;
2001da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
2011da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
2021363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
2031da177e4SLinus Torvalds 	mm->map_count = 0;
2041da177e4SLinus Torvalds 	cpus_clear(mm->cpu_vm_mask);
2051da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
2061da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
2071da177e4SLinus Torvalds 	rb_parent = NULL;
2081da177e4SLinus Torvalds 	pprev = &mm->mmap;
2091da177e4SLinus Torvalds 
210fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
2111da177e4SLinus Torvalds 		struct file *file;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
2143b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
2153b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
216ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
2173b6bfcdbSHugh Dickins 								-pages);
2181da177e4SLinus Torvalds 			continue;
2191da177e4SLinus Torvalds 		}
2201da177e4SLinus Torvalds 		charge = 0;
2211da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
2221da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
2231da177e4SLinus Torvalds 			if (security_vm_enough_memory(len))
2241da177e4SLinus Torvalds 				goto fail_nomem;
2251da177e4SLinus Torvalds 			charge = len;
2261da177e4SLinus Torvalds 		}
2271da177e4SLinus Torvalds 		tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2281da177e4SLinus Torvalds 		if (!tmp)
2291da177e4SLinus Torvalds 			goto fail_nomem;
2301da177e4SLinus Torvalds 		*tmp = *mpnt;
2311da177e4SLinus Torvalds 		pol = mpol_copy(vma_policy(mpnt));
2321da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
2331da177e4SLinus Torvalds 		if (IS_ERR(pol))
2341da177e4SLinus Torvalds 			goto fail_nomem_policy;
2351da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
2361da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
2371da177e4SLinus Torvalds 		tmp->vm_mm = mm;
2381da177e4SLinus Torvalds 		tmp->vm_next = NULL;
2391da177e4SLinus Torvalds 		anon_vma_link(tmp);
2401da177e4SLinus Torvalds 		file = tmp->vm_file;
2411da177e4SLinus Torvalds 		if (file) {
2421da177e4SLinus Torvalds 			struct inode *inode = file->f_dentry->d_inode;
2431da177e4SLinus Torvalds 			get_file(file);
2441da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
2451da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 			/* insert tmp into the share list, just after mpnt */
2481da177e4SLinus Torvalds 			spin_lock(&file->f_mapping->i_mmap_lock);
2491da177e4SLinus Torvalds 			tmp->vm_truncate_count = mpnt->vm_truncate_count;
2501da177e4SLinus Torvalds 			flush_dcache_mmap_lock(file->f_mapping);
2511da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
2521da177e4SLinus Torvalds 			flush_dcache_mmap_unlock(file->f_mapping);
2531da177e4SLinus Torvalds 			spin_unlock(&file->f_mapping->i_mmap_lock);
2541da177e4SLinus Torvalds 		}
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 		/*
2577ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
2581da177e4SLinus Torvalds 		 */
2591da177e4SLinus Torvalds 		*pprev = tmp;
2601da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
2631da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
2641da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 		mm->map_count++;
2670b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
2701da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 		if (retval)
2731da177e4SLinus Torvalds 			goto out;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	retval = 0;
2761da177e4SLinus Torvalds out:
2777ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
278fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
2791da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
2801da177e4SLinus Torvalds 	return retval;
2811da177e4SLinus Torvalds fail_nomem_policy:
2821da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
2831da177e4SLinus Torvalds fail_nomem:
2841da177e4SLinus Torvalds 	retval = -ENOMEM;
2851da177e4SLinus Torvalds 	vm_unacct_memory(charge);
2861da177e4SLinus Torvalds 	goto out;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
2921da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
2931da177e4SLinus Torvalds 		return -ENOMEM;
2941da177e4SLinus Torvalds 	return 0;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	pgd_free(mm->pgd);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds #else
3021da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
3031da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
3041da177e4SLinus Torvalds #define mm_free_pgd(mm)
3051da177e4SLinus Torvalds #endif /* CONFIG_MMU */
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds  __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds #define allocate_mm()	(kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
3101da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds #include <linux/init_task.h>
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds static struct mm_struct * mm_init(struct mm_struct * mm)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
3171da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
3181da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
3191da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
3201da177e4SLinus Torvalds 	mm->core_waiters = 0;
3211da177e4SLinus Torvalds 	mm->nr_ptes = 0;
3224294621fSHugh Dickins 	set_mm_counter(mm, file_rss, 0);
323404351e6SHugh Dickins 	set_mm_counter(mm, anon_rss, 0);
3241da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
3251da177e4SLinus Torvalds 	rwlock_init(&mm->ioctx_list_lock);
3261da177e4SLinus Torvalds 	mm->ioctx_list = NULL;
3271da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
3281363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
3311da177e4SLinus Torvalds 		mm->def_flags = 0;
3321da177e4SLinus Torvalds 		return mm;
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 	free_mm(mm);
3351da177e4SLinus Torvalds 	return NULL;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds struct mm_struct * mm_alloc(void)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	struct mm_struct * mm;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	mm = allocate_mm();
3461da177e4SLinus Torvalds 	if (mm) {
3471da177e4SLinus Torvalds 		memset(mm, 0, sizeof(*mm));
3481da177e4SLinus Torvalds 		mm = mm_init(mm);
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 	return mm;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  * Called when the last reference to the mm
3551da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
3561da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
3571da177e4SLinus Torvalds  */
3581da177e4SLinus Torvalds void fastcall __mmdrop(struct mm_struct *mm)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
3611da177e4SLinus Torvalds 	mm_free_pgd(mm);
3621da177e4SLinus Torvalds 	destroy_context(mm);
3631da177e4SLinus Torvalds 	free_mm(mm);
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds /*
3671da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
3681da177e4SLinus Torvalds  */
3691da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
3701da177e4SLinus Torvalds {
371*0ae26f1bSAndrew Morton 	might_sleep();
372*0ae26f1bSAndrew Morton 
3731da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
3741da177e4SLinus Torvalds 		exit_aio(mm);
3751da177e4SLinus Torvalds 		exit_mmap(mm);
3761da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
3771da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
3781da177e4SLinus Torvalds 			list_del(&mm->mmlist);
3791da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
3801da177e4SLinus Torvalds 		}
3811da177e4SLinus Torvalds 		put_swap_token(mm);
3821da177e4SLinus Torvalds 		mmdrop(mm);
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /**
3881da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
3891da177e4SLinus Torvalds  *
3901da177e4SLinus Torvalds  * Returns %NULL if the task has no mm.  Checks PF_BORROWED_MM (meaning
3911da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
3921da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
3931da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
3941da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
3951da177e4SLinus Torvalds  */
3961da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
3971da177e4SLinus Torvalds {
3981da177e4SLinus Torvalds 	struct mm_struct *mm;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	task_lock(task);
4011da177e4SLinus Torvalds 	mm = task->mm;
4021da177e4SLinus Torvalds 	if (mm) {
4031da177e4SLinus Torvalds 		if (task->flags & PF_BORROWED_MM)
4041da177e4SLinus Torvalds 			mm = NULL;
4051da177e4SLinus Torvalds 		else
4061da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 	task_unlock(task);
4091da177e4SLinus Torvalds 	return mm;
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
4141da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
4151da177e4SLinus Torvalds  * error success whatever.
4161da177e4SLinus Torvalds  *
4171da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
4181da177e4SLinus Torvalds  * from the current process.
4191da177e4SLinus Torvalds  *
4201da177e4SLinus Torvalds  * This difference is important for error handling, when we
4211da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
4221da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
4231da177e4SLinus Torvalds  * restoring the old one. . .
4241da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
4251da177e4SLinus Torvalds  */
4261da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct completion *vfork_done = tsk->vfork_done;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	/* Get rid of any cached register state */
4311da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	/* notify parent sleeping on vfork() */
4341da177e4SLinus Torvalds 	if (vfork_done) {
4351da177e4SLinus Torvalds 		tsk->vfork_done = NULL;
4361da177e4SLinus Torvalds 		complete(vfork_done);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 	if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
4391da177e4SLinus Torvalds 		u32 __user * tidptr = tsk->clear_child_tid;
4401da177e4SLinus Torvalds 		tsk->clear_child_tid = NULL;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 		/*
4431da177e4SLinus Torvalds 		 * We don't check the error code - if userspace has
4441da177e4SLinus Torvalds 		 * not set up a proper pointer then tough luck.
4451da177e4SLinus Torvalds 		 */
4461da177e4SLinus Torvalds 		put_user(0, tidptr);
4471da177e4SLinus Torvalds 		sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
451a0a7ec30SJANAK DESAI /*
452a0a7ec30SJANAK DESAI  * Allocate a new mm structure and copy contents from the
453a0a7ec30SJANAK DESAI  * mm structure of the passed in task structure.
454a0a7ec30SJANAK DESAI  */
455a0a7ec30SJANAK DESAI static struct mm_struct *dup_mm(struct task_struct *tsk)
456a0a7ec30SJANAK DESAI {
457a0a7ec30SJANAK DESAI 	struct mm_struct *mm, *oldmm = current->mm;
458a0a7ec30SJANAK DESAI 	int err;
459a0a7ec30SJANAK DESAI 
460a0a7ec30SJANAK DESAI 	if (!oldmm)
461a0a7ec30SJANAK DESAI 		return NULL;
462a0a7ec30SJANAK DESAI 
463a0a7ec30SJANAK DESAI 	mm = allocate_mm();
464a0a7ec30SJANAK DESAI 	if (!mm)
465a0a7ec30SJANAK DESAI 		goto fail_nomem;
466a0a7ec30SJANAK DESAI 
467a0a7ec30SJANAK DESAI 	memcpy(mm, oldmm, sizeof(*mm));
468a0a7ec30SJANAK DESAI 
469a0a7ec30SJANAK DESAI 	if (!mm_init(mm))
470a0a7ec30SJANAK DESAI 		goto fail_nomem;
471a0a7ec30SJANAK DESAI 
472a0a7ec30SJANAK DESAI 	if (init_new_context(tsk, mm))
473a0a7ec30SJANAK DESAI 		goto fail_nocontext;
474a0a7ec30SJANAK DESAI 
475a0a7ec30SJANAK DESAI 	err = dup_mmap(mm, oldmm);
476a0a7ec30SJANAK DESAI 	if (err)
477a0a7ec30SJANAK DESAI 		goto free_pt;
478a0a7ec30SJANAK DESAI 
479a0a7ec30SJANAK DESAI 	mm->hiwater_rss = get_mm_rss(mm);
480a0a7ec30SJANAK DESAI 	mm->hiwater_vm = mm->total_vm;
481a0a7ec30SJANAK DESAI 
482a0a7ec30SJANAK DESAI 	return mm;
483a0a7ec30SJANAK DESAI 
484a0a7ec30SJANAK DESAI free_pt:
485a0a7ec30SJANAK DESAI 	mmput(mm);
486a0a7ec30SJANAK DESAI 
487a0a7ec30SJANAK DESAI fail_nomem:
488a0a7ec30SJANAK DESAI 	return NULL;
489a0a7ec30SJANAK DESAI 
490a0a7ec30SJANAK DESAI fail_nocontext:
491a0a7ec30SJANAK DESAI 	/*
492a0a7ec30SJANAK DESAI 	 * If init_new_context() failed, we cannot use mmput() to free the mm
493a0a7ec30SJANAK DESAI 	 * because it calls destroy_context()
494a0a7ec30SJANAK DESAI 	 */
495a0a7ec30SJANAK DESAI 	mm_free_pgd(mm);
496a0a7ec30SJANAK DESAI 	free_mm(mm);
497a0a7ec30SJANAK DESAI 	return NULL;
498a0a7ec30SJANAK DESAI }
499a0a7ec30SJANAK DESAI 
5001da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	struct mm_struct * mm, *oldmm;
5031da177e4SLinus Torvalds 	int retval;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
5061da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	tsk->mm = NULL;
5091da177e4SLinus Torvalds 	tsk->active_mm = NULL;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	/*
5121da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
5131da177e4SLinus Torvalds 	 *
5141da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
5151da177e4SLinus Torvalds 	 */
5161da177e4SLinus Torvalds 	oldmm = current->mm;
5171da177e4SLinus Torvalds 	if (!oldmm)
5181da177e4SLinus Torvalds 		return 0;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
5211da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
5221da177e4SLinus Torvalds 		mm = oldmm;
5231da177e4SLinus Torvalds 		goto good_mm;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	retval = -ENOMEM;
527a0a7ec30SJANAK DESAI 	mm = dup_mm(tsk);
5281da177e4SLinus Torvalds 	if (!mm)
5291da177e4SLinus Torvalds 		goto fail_nomem;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds good_mm:
5321da177e4SLinus Torvalds 	tsk->mm = mm;
5331da177e4SLinus Torvalds 	tsk->active_mm = mm;
5341da177e4SLinus Torvalds 	return 0;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds fail_nomem:
5371da177e4SLinus Torvalds 	return retval;
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
5431da177e4SLinus Torvalds 	/* We don't need to lock fs - think why ;-) */
5441da177e4SLinus Torvalds 	if (fs) {
5451da177e4SLinus Torvalds 		atomic_set(&fs->count, 1);
5461da177e4SLinus Torvalds 		rwlock_init(&fs->lock);
5471da177e4SLinus Torvalds 		fs->umask = old->umask;
5481da177e4SLinus Torvalds 		read_lock(&old->lock);
5491da177e4SLinus Torvalds 		fs->rootmnt = mntget(old->rootmnt);
5501da177e4SLinus Torvalds 		fs->root = dget(old->root);
5511da177e4SLinus Torvalds 		fs->pwdmnt = mntget(old->pwdmnt);
5521da177e4SLinus Torvalds 		fs->pwd = dget(old->pwd);
5531da177e4SLinus Torvalds 		if (old->altroot) {
5541da177e4SLinus Torvalds 			fs->altrootmnt = mntget(old->altrootmnt);
5551da177e4SLinus Torvalds 			fs->altroot = dget(old->altroot);
5561da177e4SLinus Torvalds 		} else {
5571da177e4SLinus Torvalds 			fs->altrootmnt = NULL;
5581da177e4SLinus Torvalds 			fs->altroot = NULL;
5591da177e4SLinus Torvalds 		}
5601da177e4SLinus Torvalds 		read_unlock(&old->lock);
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 	return fs;
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds struct fs_struct *copy_fs_struct(struct fs_struct *old)
5661da177e4SLinus Torvalds {
5671da177e4SLinus Torvalds 	return __copy_fs_struct(old);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(copy_fs_struct);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
5751da177e4SLinus Torvalds 		atomic_inc(&current->fs->count);
5761da177e4SLinus Torvalds 		return 0;
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds 	tsk->fs = __copy_fs_struct(current->fs);
5791da177e4SLinus Torvalds 	if (!tsk->fs)
5801da177e4SLinus Torvalds 		return -ENOMEM;
5811da177e4SLinus Torvalds 	return 0;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
584ab2af1f5SDipankar Sarma static int count_open_files(struct fdtable *fdt)
5851da177e4SLinus Torvalds {
586ab2af1f5SDipankar Sarma 	int size = fdt->max_fdset;
5871da177e4SLinus Torvalds 	int i;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	/* Find the last open fd */
5901da177e4SLinus Torvalds 	for (i = size/(8*sizeof(long)); i > 0; ) {
591badf1662SDipankar Sarma 		if (fdt->open_fds->fds_bits[--i])
5921da177e4SLinus Torvalds 			break;
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 	i = (i+1) * 8 * sizeof(long);
5951da177e4SLinus Torvalds 	return i;
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
598badf1662SDipankar Sarma static struct files_struct *alloc_files(void)
599badf1662SDipankar Sarma {
600badf1662SDipankar Sarma 	struct files_struct *newf;
601badf1662SDipankar Sarma 	struct fdtable *fdt;
602badf1662SDipankar Sarma 
603badf1662SDipankar Sarma 	newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
604badf1662SDipankar Sarma 	if (!newf)
605badf1662SDipankar Sarma 		goto out;
606badf1662SDipankar Sarma 
607badf1662SDipankar Sarma 	atomic_set(&newf->count, 1);
608badf1662SDipankar Sarma 
609badf1662SDipankar Sarma 	spin_lock_init(&newf->file_lock);
6100c9e63fdSEric Dumazet 	newf->next_fd = 0;
611ab2af1f5SDipankar Sarma 	fdt = &newf->fdtab;
612badf1662SDipankar Sarma 	fdt->max_fds = NR_OPEN_DEFAULT;
6130c9e63fdSEric Dumazet 	fdt->max_fdset = EMBEDDED_FD_SET_SIZE;
6140c9e63fdSEric Dumazet 	fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
6150c9e63fdSEric Dumazet 	fdt->open_fds = (fd_set *)&newf->open_fds_init;
616badf1662SDipankar Sarma 	fdt->fd = &newf->fd_array[0];
617ab2af1f5SDipankar Sarma 	INIT_RCU_HEAD(&fdt->rcu);
618ab2af1f5SDipankar Sarma 	fdt->free_files = NULL;
619ab2af1f5SDipankar Sarma 	fdt->next = NULL;
620ab2af1f5SDipankar Sarma 	rcu_assign_pointer(newf->fdt, fdt);
621badf1662SDipankar Sarma out:
622badf1662SDipankar Sarma 	return newf;
623badf1662SDipankar Sarma }
624badf1662SDipankar Sarma 
625a016f338SJANAK DESAI /*
626a016f338SJANAK DESAI  * Allocate a new files structure and copy contents from the
627a016f338SJANAK DESAI  * passed in files structure.
628a016f338SJANAK DESAI  */
629a016f338SJANAK DESAI static struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
6301da177e4SLinus Torvalds {
631a016f338SJANAK DESAI 	struct files_struct *newf;
6321da177e4SLinus Torvalds 	struct file **old_fds, **new_fds;
633a016f338SJANAK DESAI 	int open_files, size, i, expand;
634badf1662SDipankar Sarma 	struct fdtable *old_fdt, *new_fdt;
6351da177e4SLinus Torvalds 
636badf1662SDipankar Sarma 	newf = alloc_files();
6371da177e4SLinus Torvalds 	if (!newf)
6381da177e4SLinus Torvalds 		goto out;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	spin_lock(&oldf->file_lock);
641badf1662SDipankar Sarma 	old_fdt = files_fdtable(oldf);
642badf1662SDipankar Sarma 	new_fdt = files_fdtable(newf);
643badf1662SDipankar Sarma 	size = old_fdt->max_fdset;
644ab2af1f5SDipankar Sarma 	open_files = count_open_files(old_fdt);
6451da177e4SLinus Torvalds 	expand = 0;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	/*
6481da177e4SLinus Torvalds 	 * Check whether we need to allocate a larger fd array or fd set.
6491da177e4SLinus Torvalds 	 * Note: we're not a clone task, so the open count won't  change.
6501da177e4SLinus Torvalds 	 */
651badf1662SDipankar Sarma 	if (open_files > new_fdt->max_fdset) {
652badf1662SDipankar Sarma 		new_fdt->max_fdset = 0;
6531da177e4SLinus Torvalds 		expand = 1;
6541da177e4SLinus Torvalds 	}
655badf1662SDipankar Sarma 	if (open_files > new_fdt->max_fds) {
656badf1662SDipankar Sarma 		new_fdt->max_fds = 0;
6571da177e4SLinus Torvalds 		expand = 1;
6581da177e4SLinus Torvalds 	}
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	/* if the old fdset gets grown now, we'll only copy up to "size" fds */
6611da177e4SLinus Torvalds 	if (expand) {
6621da177e4SLinus Torvalds 		spin_unlock(&oldf->file_lock);
6631da177e4SLinus Torvalds 		spin_lock(&newf->file_lock);
664a016f338SJANAK DESAI 		*errorp = expand_files(newf, open_files-1);
6651da177e4SLinus Torvalds 		spin_unlock(&newf->file_lock);
666a016f338SJANAK DESAI 		if (*errorp < 0)
6671da177e4SLinus Torvalds 			goto out_release;
668ab2af1f5SDipankar Sarma 		new_fdt = files_fdtable(newf);
669ab2af1f5SDipankar Sarma 		/*
670ab2af1f5SDipankar Sarma 		 * Reacquire the oldf lock and a pointer to its fd table
671ab2af1f5SDipankar Sarma 		 * who knows it may have a new bigger fd table. We need
672ab2af1f5SDipankar Sarma 		 * the latest pointer.
673ab2af1f5SDipankar Sarma 		 */
6741da177e4SLinus Torvalds 		spin_lock(&oldf->file_lock);
675ab2af1f5SDipankar Sarma 		old_fdt = files_fdtable(oldf);
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
678badf1662SDipankar Sarma 	old_fds = old_fdt->fd;
679badf1662SDipankar Sarma 	new_fds = new_fdt->fd;
6801da177e4SLinus Torvalds 
681badf1662SDipankar Sarma 	memcpy(new_fdt->open_fds->fds_bits, old_fdt->open_fds->fds_bits, open_files/8);
682badf1662SDipankar Sarma 	memcpy(new_fdt->close_on_exec->fds_bits, old_fdt->close_on_exec->fds_bits, open_files/8);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	for (i = open_files; i != 0; i--) {
6851da177e4SLinus Torvalds 		struct file *f = *old_fds++;
6861da177e4SLinus Torvalds 		if (f) {
6871da177e4SLinus Torvalds 			get_file(f);
6881da177e4SLinus Torvalds 		} else {
6891da177e4SLinus Torvalds 			/*
6901da177e4SLinus Torvalds 			 * The fd may be claimed in the fd bitmap but not yet
6911da177e4SLinus Torvalds 			 * instantiated in the files array if a sibling thread
6921da177e4SLinus Torvalds 			 * is partway through open().  So make sure that this
6931da177e4SLinus Torvalds 			 * fd is available to the new process.
6941da177e4SLinus Torvalds 			 */
695badf1662SDipankar Sarma 			FD_CLR(open_files - i, new_fdt->open_fds);
6961da177e4SLinus Torvalds 		}
697ab2af1f5SDipankar Sarma 		rcu_assign_pointer(*new_fds++, f);
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 	spin_unlock(&oldf->file_lock);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	/* compute the remainder to be cleared */
702badf1662SDipankar Sarma 	size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	/* This is long word aligned thus could use a optimized version */
7051da177e4SLinus Torvalds 	memset(new_fds, 0, size);
7061da177e4SLinus Torvalds 
707badf1662SDipankar Sarma 	if (new_fdt->max_fdset > open_files) {
708badf1662SDipankar Sarma 		int left = (new_fdt->max_fdset-open_files)/8;
7091da177e4SLinus Torvalds 		int start = open_files / (8 * sizeof(unsigned long));
7101da177e4SLinus Torvalds 
711badf1662SDipankar Sarma 		memset(&new_fdt->open_fds->fds_bits[start], 0, left);
712badf1662SDipankar Sarma 		memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds out:
716a016f338SJANAK DESAI 	return newf;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds out_release:
719badf1662SDipankar Sarma 	free_fdset (new_fdt->close_on_exec, new_fdt->max_fdset);
720badf1662SDipankar Sarma 	free_fdset (new_fdt->open_fds, new_fdt->max_fdset);
721badf1662SDipankar Sarma 	free_fd_array(new_fdt->fd, new_fdt->max_fds);
7221da177e4SLinus Torvalds 	kmem_cache_free(files_cachep, newf);
72342862298SKirill Korotaev 	return NULL;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
726a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
727a016f338SJANAK DESAI {
728a016f338SJANAK DESAI 	struct files_struct *oldf, *newf;
729a016f338SJANAK DESAI 	int error = 0;
730a016f338SJANAK DESAI 
731a016f338SJANAK DESAI 	/*
732a016f338SJANAK DESAI 	 * A background process may not have any files ...
733a016f338SJANAK DESAI 	 */
734a016f338SJANAK DESAI 	oldf = current->files;
735a016f338SJANAK DESAI 	if (!oldf)
736a016f338SJANAK DESAI 		goto out;
737a016f338SJANAK DESAI 
738a016f338SJANAK DESAI 	if (clone_flags & CLONE_FILES) {
739a016f338SJANAK DESAI 		atomic_inc(&oldf->count);
740a016f338SJANAK DESAI 		goto out;
741a016f338SJANAK DESAI 	}
742a016f338SJANAK DESAI 
743a016f338SJANAK DESAI 	/*
744a016f338SJANAK DESAI 	 * Note: we may be using current for both targets (See exec.c)
745a016f338SJANAK DESAI 	 * This works because we cache current->files (old) as oldf. Don't
746a016f338SJANAK DESAI 	 * break this.
747a016f338SJANAK DESAI 	 */
748a016f338SJANAK DESAI 	tsk->files = NULL;
749a016f338SJANAK DESAI 	error = -ENOMEM;
750a016f338SJANAK DESAI 	newf = dup_fd(oldf, &error);
751a016f338SJANAK DESAI 	if (!newf)
752a016f338SJANAK DESAI 		goto out;
753a016f338SJANAK DESAI 
754a016f338SJANAK DESAI 	tsk->files = newf;
755a016f338SJANAK DESAI 	error = 0;
756a016f338SJANAK DESAI out:
757a016f338SJANAK DESAI 	return error;
758a016f338SJANAK DESAI }
759a016f338SJANAK DESAI 
7601da177e4SLinus Torvalds /*
7611da177e4SLinus Torvalds  *	Helper to unshare the files of the current task.
7621da177e4SLinus Torvalds  *	We don't want to expose copy_files internals to
7631da177e4SLinus Torvalds  *	the exec layer of the kernel.
7641da177e4SLinus Torvalds  */
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds int unshare_files(void)
7671da177e4SLinus Torvalds {
7681da177e4SLinus Torvalds 	struct files_struct *files  = current->files;
7691da177e4SLinus Torvalds 	int rc;
7701da177e4SLinus Torvalds 
771910dea7fSEric Sesterhenn 	BUG_ON(!files);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	/* This can race but the race causes us to copy when we don't
7741da177e4SLinus Torvalds 	   need to and drop the copy */
7751da177e4SLinus Torvalds 	if(atomic_read(&files->count) == 1)
7761da177e4SLinus Torvalds 	{
7771da177e4SLinus Torvalds 		atomic_inc(&files->count);
7781da177e4SLinus Torvalds 		return 0;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	rc = copy_files(0, current);
7811da177e4SLinus Torvalds 	if(rc)
7821da177e4SLinus Torvalds 		current->files = files;
7831da177e4SLinus Torvalds 	return rc;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds EXPORT_SYMBOL(unshare_files);
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	struct sighand_struct *sig;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
7931da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
7941da177e4SLinus Torvalds 		return 0;
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
797e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
7981da177e4SLinus Torvalds 	if (!sig)
7991da177e4SLinus Torvalds 		return -ENOMEM;
8001da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
8011da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
8021da177e4SLinus Torvalds 	return 0;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
805a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand)
806c81addc9SOleg Nesterov {
807c81addc9SOleg Nesterov 	if (atomic_dec_and_test(&sighand->count))
808c81addc9SOleg Nesterov 		kmem_cache_free(sighand_cachep, sighand);
809c81addc9SOleg Nesterov }
810c81addc9SOleg Nesterov 
8111da177e4SLinus Torvalds static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	struct signal_struct *sig;
8141da177e4SLinus Torvalds 	int ret;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	if (clone_flags & CLONE_THREAD) {
8171da177e4SLinus Torvalds 		atomic_inc(&current->signal->count);
8181da177e4SLinus Torvalds 		atomic_inc(&current->signal->live);
8191da177e4SLinus Torvalds 		return 0;
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds 	sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
8221da177e4SLinus Torvalds 	tsk->signal = sig;
8231da177e4SLinus Torvalds 	if (!sig)
8241da177e4SLinus Torvalds 		return -ENOMEM;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	ret = copy_thread_group_keys(tsk);
8271da177e4SLinus Torvalds 	if (ret < 0) {
8281da177e4SLinus Torvalds 		kmem_cache_free(signal_cachep, sig);
8291da177e4SLinus Torvalds 		return ret;
8301da177e4SLinus Torvalds 	}
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
8331da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
8341da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
8351da177e4SLinus Torvalds 	sig->flags = 0;
8361da177e4SLinus Torvalds 	sig->group_exit_code = 0;
8371da177e4SLinus Torvalds 	sig->group_exit_task = NULL;
8381da177e4SLinus Torvalds 	sig->group_stop_count = 0;
8391da177e4SLinus Torvalds 	sig->curr_target = NULL;
8401da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
8411da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
8421da177e4SLinus Torvalds 
8437978672cSGeorge Anzinger 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_REL);
8442ff678b8SThomas Gleixner 	sig->it_real_incr.tv64 = 0;
8451da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
84605cfb614SRoman Zippel 	sig->tsk = tsk;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	sig->it_virt_expires = cputime_zero;
8491da177e4SLinus Torvalds 	sig->it_virt_incr = cputime_zero;
8501da177e4SLinus Torvalds 	sig->it_prof_expires = cputime_zero;
8511da177e4SLinus Torvalds 	sig->it_prof_incr = cputime_zero;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	sig->leader = 0;	/* session leadership doesn't inherit */
8541da177e4SLinus Torvalds 	sig->tty_old_pgrp = 0;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
8571da177e4SLinus Torvalds 	sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
8581da177e4SLinus Torvalds 	sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
8591da177e4SLinus Torvalds 	sig->sched_time = 0;
8601da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
8611da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
8621da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	task_lock(current->group_leader);
8651da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
8661da177e4SLinus Torvalds 	task_unlock(current->group_leader);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
8691da177e4SLinus Torvalds 		/*
8701da177e4SLinus Torvalds 		 * New sole thread in the process gets an expiry time
8711da177e4SLinus Torvalds 		 * of the whole CPU time limit.
8721da177e4SLinus Torvalds 		 */
8731da177e4SLinus Torvalds 		tsk->it_prof_expires =
8741da177e4SLinus Torvalds 			secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
8751da177e4SLinus Torvalds 	}
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	return 0;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8806b3934efSOleg Nesterov void __cleanup_signal(struct signal_struct *sig)
8816b3934efSOleg Nesterov {
8826b3934efSOleg Nesterov 	exit_thread_group_keys(sig);
8836b3934efSOleg Nesterov 	kmem_cache_free(signal_cachep, sig);
8846b3934efSOleg Nesterov }
8856b3934efSOleg Nesterov 
8866b3934efSOleg Nesterov static inline void cleanup_signal(struct task_struct *tsk)
8876b3934efSOleg Nesterov {
8886b3934efSOleg Nesterov 	struct signal_struct *sig = tsk->signal;
8896b3934efSOleg Nesterov 
8906b3934efSOleg Nesterov 	atomic_dec(&sig->live);
8916b3934efSOleg Nesterov 
8926b3934efSOleg Nesterov 	if (atomic_dec_and_test(&sig->count))
8936b3934efSOleg Nesterov 		__cleanup_signal(sig);
8946b3934efSOleg Nesterov }
8956b3934efSOleg Nesterov 
8961da177e4SLinus Torvalds static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
8971da177e4SLinus Torvalds {
8981da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
8991da177e4SLinus Torvalds 
900d1209d04SAlan Stern 	new_flags &= ~(PF_SUPERPRIV | PF_NOFREEZE);
9011da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
9021da177e4SLinus Torvalds 	if (!(clone_flags & CLONE_PTRACE))
9031da177e4SLinus Torvalds 		p->ptrace = 0;
9041da177e4SLinus Torvalds 	p->flags = new_flags;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds asmlinkage long sys_set_tid_address(int __user *tidptr)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	return current->pid;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds /*
9151da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
9161da177e4SLinus Torvalds  * but does not actually start it yet.
9171da177e4SLinus Torvalds  *
9181da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
9191da177e4SLinus Torvalds  * parts of the process environment (as per the clone
9201da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
9211da177e4SLinus Torvalds  */
9221da177e4SLinus Torvalds static task_t *copy_process(unsigned long clone_flags,
9231da177e4SLinus Torvalds 				 unsigned long stack_start,
9241da177e4SLinus Torvalds 				 struct pt_regs *regs,
9251da177e4SLinus Torvalds 				 unsigned long stack_size,
9261da177e4SLinus Torvalds 				 int __user *parent_tidptr,
9271da177e4SLinus Torvalds 				 int __user *child_tidptr,
9281da177e4SLinus Torvalds 				 int pid)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	int retval;
9311da177e4SLinus Torvalds 	struct task_struct *p = NULL;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
9341da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	/*
9371da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
9381da177e4SLinus Torvalds 	 * can only be started up within the thread group.
9391da177e4SLinus Torvalds 	 */
9401da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
9411da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/*
9441da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
9451da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
9461da177e4SLinus Torvalds 	 * for various simplifications in other code.
9471da177e4SLinus Torvalds 	 */
9481da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
9491da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
9521da177e4SLinus Torvalds 	if (retval)
9531da177e4SLinus Torvalds 		goto fork_out;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	retval = -ENOMEM;
9561da177e4SLinus Torvalds 	p = dup_task_struct(current);
9571da177e4SLinus Torvalds 	if (!p)
9581da177e4SLinus Torvalds 		goto fork_out;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	retval = -EAGAIN;
9611da177e4SLinus Torvalds 	if (atomic_read(&p->user->processes) >=
9621da177e4SLinus Torvalds 			p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
9631da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
9641da177e4SLinus Torvalds 				p->user != &root_user)
9651da177e4SLinus Torvalds 			goto bad_fork_free;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	atomic_inc(&p->user->__count);
9691da177e4SLinus Torvalds 	atomic_inc(&p->user->processes);
9701da177e4SLinus Torvalds 	get_group_info(p->group_info);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	/*
9731da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
9741da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
9751da177e4SLinus Torvalds 	 * to stop root fork bombs.
9761da177e4SLinus Torvalds 	 */
9771da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
9781da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
9791da177e4SLinus Torvalds 
980a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
9811da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	if (p->binfmt && !try_module_get(p->binfmt->module))
9841da177e4SLinus Torvalds 		goto bad_fork_cleanup_put_domain;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	p->did_exec = 0;
9871da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
9881da177e4SLinus Torvalds 	p->pid = pid;
9891da177e4SLinus Torvalds 	retval = -EFAULT;
9901da177e4SLinus Torvalds 	if (clone_flags & CLONE_PARENT_SETTID)
9911da177e4SLinus Torvalds 		if (put_user(p->pid, parent_tidptr))
9921da177e4SLinus Torvalds 			goto bad_fork_cleanup;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	p->proc_dentry = NULL;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
9971da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
9981da177e4SLinus Torvalds 	p->vfork_done = NULL;
9991da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
10001da177e4SLinus Torvalds 	spin_lock_init(&p->proc_lock);
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SIGPENDING);
10031da177e4SLinus Torvalds 	init_sigpending(&p->pending);
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	p->utime = cputime_zero;
10061da177e4SLinus Torvalds 	p->stime = cputime_zero;
10071da177e4SLinus Torvalds  	p->sched_time = 0;
10081da177e4SLinus Torvalds 	p->rchar = 0;		/* I/O counter: bytes read */
10091da177e4SLinus Torvalds 	p->wchar = 0;		/* I/O counter: bytes written */
10101da177e4SLinus Torvalds 	p->syscr = 0;		/* I/O counter: read syscalls */
10111da177e4SLinus Torvalds 	p->syscw = 0;		/* I/O counter: write syscalls */
10121da177e4SLinus Torvalds 	acct_clear_integrals(p);
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds  	p->it_virt_expires = cputime_zero;
10151da177e4SLinus Torvalds 	p->it_prof_expires = cputime_zero;
10161da177e4SLinus Torvalds  	p->it_sched_expires = 0;
10171da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[0]);
10181da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[1]);
10191da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[2]);
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	p->lock_depth = -1;		/* -1 = no lock */
10221da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
10231da177e4SLinus Torvalds 	p->security = NULL;
10241da177e4SLinus Torvalds 	p->io_context = NULL;
10251da177e4SLinus Torvalds 	p->io_wait = NULL;
10261da177e4SLinus Torvalds 	p->audit_context = NULL;
1027b4b26418SPaul Jackson 	cpuset_fork(p);
10281da177e4SLinus Torvalds #ifdef CONFIG_NUMA
10291da177e4SLinus Torvalds  	p->mempolicy = mpol_copy(p->mempolicy);
10301da177e4SLinus Torvalds  	if (IS_ERR(p->mempolicy)) {
10311da177e4SLinus Torvalds  		retval = PTR_ERR(p->mempolicy);
10321da177e4SLinus Torvalds  		p->mempolicy = NULL;
1033b4b26418SPaul Jackson  		goto bad_fork_cleanup_cpuset;
10341da177e4SLinus Torvalds  	}
1035c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(p);
10361da177e4SLinus Torvalds #endif
10371da177e4SLinus Torvalds 
1038408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1039408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
1040408894eeSIngo Molnar #endif
1041408894eeSIngo Molnar 
10421da177e4SLinus Torvalds 	p->tgid = p->pid;
10431da177e4SLinus Torvalds 	if (clone_flags & CLONE_THREAD)
10441da177e4SLinus Torvalds 		p->tgid = current->tgid;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	if ((retval = security_task_alloc(p)))
10471da177e4SLinus Torvalds 		goto bad_fork_cleanup_policy;
10481da177e4SLinus Torvalds 	if ((retval = audit_alloc(p)))
10491da177e4SLinus Torvalds 		goto bad_fork_cleanup_security;
10501da177e4SLinus Torvalds 	/* copy all the process information */
10511da177e4SLinus Torvalds 	if ((retval = copy_semundo(clone_flags, p)))
10521da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
10531da177e4SLinus Torvalds 	if ((retval = copy_files(clone_flags, p)))
10541da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
10551da177e4SLinus Torvalds 	if ((retval = copy_fs(clone_flags, p)))
10561da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
10571da177e4SLinus Torvalds 	if ((retval = copy_sighand(clone_flags, p)))
10581da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
10591da177e4SLinus Torvalds 	if ((retval = copy_signal(clone_flags, p)))
10601da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
10611da177e4SLinus Torvalds 	if ((retval = copy_mm(clone_flags, p)))
10621da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
10631da177e4SLinus Torvalds 	if ((retval = copy_keys(clone_flags, p)))
10641da177e4SLinus Torvalds 		goto bad_fork_cleanup_mm;
10651da177e4SLinus Torvalds 	if ((retval = copy_namespace(clone_flags, p)))
10661da177e4SLinus Torvalds 		goto bad_fork_cleanup_keys;
10671da177e4SLinus Torvalds 	retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
10681da177e4SLinus Torvalds 	if (retval)
10691da177e4SLinus Torvalds 		goto bad_fork_cleanup_namespace;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
10721da177e4SLinus Torvalds 	/*
10731da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
10741da177e4SLinus Torvalds 	 */
10751da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
10768f17d3a5SIngo Molnar 	p->robust_list = NULL;
10778f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT
10788f17d3a5SIngo Molnar 	p->compat_robust_list = NULL;
10798f17d3a5SIngo Molnar #endif
10801da177e4SLinus Torvalds 	/*
1081f9a3879aSGOTO Masanori 	 * sigaltstack should be cleared when sharing the same VM
1082f9a3879aSGOTO Masanori 	 */
1083f9a3879aSGOTO Masanori 	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1084f9a3879aSGOTO Masanori 		p->sas_ss_sp = p->sas_ss_size = 0;
1085f9a3879aSGOTO Masanori 
1086f9a3879aSGOTO Masanori 	/*
10871da177e4SLinus Torvalds 	 * Syscall tracing should be turned off in the child regardless
10881da177e4SLinus Torvalds 	 * of CLONE_PTRACE.
10891da177e4SLinus Torvalds 	 */
10901da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1091ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1092ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1093ed75e8d5SLaurent Vivier #endif
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	/* Our parent execution domain becomes current domain
10961da177e4SLinus Torvalds 	   These must match for thread signalling to apply */
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	p->parent_exec_id = p->self_exec_id;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
11011da177e4SLinus Torvalds 	p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
11021da177e4SLinus Torvalds 	p->pdeath_signal = 0;
11031da177e4SLinus Torvalds 	p->exit_state = 0;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	/*
11061da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
11071da177e4SLinus Torvalds 	 * We dont wake it up yet.
11081da177e4SLinus Torvalds 	 */
11091da177e4SLinus Torvalds 	p->group_leader = p;
111047e65328SOleg Nesterov 	INIT_LIST_HEAD(&p->thread_group);
11111da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->ptrace_children);
11121da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->ptrace_list);
11131da177e4SLinus Torvalds 
1114476d139cSNick Piggin 	/* Perform scheduler related setup. Assign this task to a CPU. */
1115476d139cSNick Piggin 	sched_fork(p, clone_flags);
1116476d139cSNick Piggin 
11171da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
11181da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	/*
1121476d139cSNick Piggin 	 * The task hasn't been attached yet, so its cpus_allowed mask will
1122476d139cSNick Piggin 	 * not be changed, nor will its assigned CPU.
1123476d139cSNick Piggin 	 *
1124476d139cSNick Piggin 	 * The cpus_allowed mask of the parent may have changed after it was
1125476d139cSNick Piggin 	 * copied first time - so re-copy it here, then check the child's CPU
1126476d139cSNick Piggin 	 * to ensure it is on a valid CPU (and if not, just force it back to
1127476d139cSNick Piggin 	 * parent's CPU). This avoids alot of nasty races.
11281da177e4SLinus Torvalds 	 */
11291da177e4SLinus Torvalds 	p->cpus_allowed = current->cpus_allowed;
113026ff6ad9SSrivatsa Vaddagiri 	if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
113126ff6ad9SSrivatsa Vaddagiri 			!cpu_online(task_cpu(p))))
11321da177e4SLinus Torvalds 		set_task_cpu(p, smp_processor_id());
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
11351da177e4SLinus Torvalds 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
11361da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
11371da177e4SLinus Torvalds 	else
11381da177e4SLinus Torvalds 		p->real_parent = current;
11391da177e4SLinus Torvalds 	p->parent = p->real_parent;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	spin_lock(&current->sighand->siglock);
11424a2c7a78SOleg Nesterov 
11434a2c7a78SOleg Nesterov 	/*
11444a2c7a78SOleg Nesterov 	 * Process group and session signals need to be delivered to just the
11454a2c7a78SOleg Nesterov 	 * parent before the fork or both the parent and the child after the
11464a2c7a78SOleg Nesterov 	 * fork. Restart if a signal comes in before we add the new process to
11474a2c7a78SOleg Nesterov 	 * it's process group.
11484a2c7a78SOleg Nesterov 	 * A fatal signal pending means that current will exit, so the new
11494a2c7a78SOleg Nesterov 	 * thread can't slip out of an OOM kill (or normal SIGKILL).
11504a2c7a78SOleg Nesterov  	 */
11514a2c7a78SOleg Nesterov  	recalc_sigpending();
11524a2c7a78SOleg Nesterov 	if (signal_pending(current)) {
11534a2c7a78SOleg Nesterov 		spin_unlock(&current->sighand->siglock);
11544a2c7a78SOleg Nesterov 		write_unlock_irq(&tasklist_lock);
11554a2c7a78SOleg Nesterov 		retval = -ERESTARTNOINTR;
11564a2c7a78SOleg Nesterov 		goto bad_fork_cleanup_namespace;
11574a2c7a78SOleg Nesterov 	}
11584a2c7a78SOleg Nesterov 
11593f17da69SOleg Nesterov 	if (clone_flags & CLONE_THREAD) {
11601da177e4SLinus Torvalds 		/*
11611da177e4SLinus Torvalds 		 * Important: if an exit-all has been started then
11621da177e4SLinus Torvalds 		 * do not create this new thread - the whole thread
11631da177e4SLinus Torvalds 		 * group is supposed to exit anyway.
11641da177e4SLinus Torvalds 		 */
11651da177e4SLinus Torvalds 		if (current->signal->flags & SIGNAL_GROUP_EXIT) {
11661da177e4SLinus Torvalds 			spin_unlock(&current->sighand->siglock);
11671da177e4SLinus Torvalds 			write_unlock_irq(&tasklist_lock);
11681da177e4SLinus Torvalds 			retval = -EAGAIN;
11691da177e4SLinus Torvalds 			goto bad_fork_cleanup_namespace;
11701da177e4SLinus Torvalds 		}
117147e65328SOleg Nesterov 
11721da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
117347e65328SOleg Nesterov 		list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 		if (!cputime_eq(current->signal->it_virt_expires,
11761da177e4SLinus Torvalds 				cputime_zero) ||
11771da177e4SLinus Torvalds 		    !cputime_eq(current->signal->it_prof_expires,
11781da177e4SLinus Torvalds 				cputime_zero) ||
11791da177e4SLinus Torvalds 		    current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
11801da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[0]) ||
11811da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[1]) ||
11821da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[2])) {
11831da177e4SLinus Torvalds 			/*
11841da177e4SLinus Torvalds 			 * Have child wake up on its first tick to check
11851da177e4SLinus Torvalds 			 * for process CPU timers.
11861da177e4SLinus Torvalds 			 */
11871da177e4SLinus Torvalds 			p->it_prof_expires = jiffies_to_cputime(1);
11881da177e4SLinus Torvalds 		}
11891da177e4SLinus Torvalds 	}
11901da177e4SLinus Torvalds 
119122e2c507SJens Axboe 	/*
119222e2c507SJens Axboe 	 * inherit ioprio
119322e2c507SJens Axboe 	 */
119422e2c507SJens Axboe 	p->ioprio = current->ioprio;
119522e2c507SJens Axboe 
119673b9ebfeSOleg Nesterov 	if (likely(p->pid)) {
1197c97d9893SOleg Nesterov 		add_parent(p);
11981da177e4SLinus Torvalds 		if (unlikely(p->ptrace & PT_PTRACED))
11991da177e4SLinus Torvalds 			__ptrace_link(p, current->parent);
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 		if (thread_group_leader(p)) {
12029a5d3023SOren Laadan 			p->signal->tty = current->signal->tty;
12039a5d3023SOren Laadan 			p->signal->pgrp = process_group(current);
12049a5d3023SOren Laadan 			p->signal->session = current->signal->session;
12051da177e4SLinus Torvalds 			attach_pid(p, PIDTYPE_PGID, process_group(p));
12061da177e4SLinus Torvalds 			attach_pid(p, PIDTYPE_SID, p->signal->session);
1207c97d9893SOleg Nesterov 
12085e85d4abSEric W. Biederman 			list_add_tail_rcu(&p->tasks, &init_task.tasks);
12091da177e4SLinus Torvalds 			__get_cpu_var(process_counts)++;
12101da177e4SLinus Torvalds 		}
1211dadac81bSOleg Nesterov 		attach_pid(p, PIDTYPE_PID, p->pid);
12121da177e4SLinus Torvalds 		nr_threads++;
121373b9ebfeSOleg Nesterov 	}
121473b9ebfeSOleg Nesterov 
12151da177e4SLinus Torvalds 	total_forks++;
12163f17da69SOleg Nesterov 	spin_unlock(&current->sighand->siglock);
12171da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1218c13cf856SAndrew Morton 	proc_fork_connector(p);
12191da177e4SLinus Torvalds 	return p;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds bad_fork_cleanup_namespace:
12221da177e4SLinus Torvalds 	exit_namespace(p);
12231da177e4SLinus Torvalds bad_fork_cleanup_keys:
12241da177e4SLinus Torvalds 	exit_keys(p);
12251da177e4SLinus Torvalds bad_fork_cleanup_mm:
12261da177e4SLinus Torvalds 	if (p->mm)
12271da177e4SLinus Torvalds 		mmput(p->mm);
12281da177e4SLinus Torvalds bad_fork_cleanup_signal:
12296b3934efSOleg Nesterov 	cleanup_signal(p);
12301da177e4SLinus Torvalds bad_fork_cleanup_sighand:
1231a7e5328aSOleg Nesterov 	__cleanup_sighand(p->sighand);
12321da177e4SLinus Torvalds bad_fork_cleanup_fs:
12331da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
12341da177e4SLinus Torvalds bad_fork_cleanup_files:
12351da177e4SLinus Torvalds 	exit_files(p); /* blocking */
12361da177e4SLinus Torvalds bad_fork_cleanup_semundo:
12371da177e4SLinus Torvalds 	exit_sem(p);
12381da177e4SLinus Torvalds bad_fork_cleanup_audit:
12391da177e4SLinus Torvalds 	audit_free(p);
12401da177e4SLinus Torvalds bad_fork_cleanup_security:
12411da177e4SLinus Torvalds 	security_task_free(p);
12421da177e4SLinus Torvalds bad_fork_cleanup_policy:
12431da177e4SLinus Torvalds #ifdef CONFIG_NUMA
12441da177e4SLinus Torvalds 	mpol_free(p->mempolicy);
1245b4b26418SPaul Jackson bad_fork_cleanup_cpuset:
12461da177e4SLinus Torvalds #endif
1247b4b26418SPaul Jackson 	cpuset_exit(p);
12481da177e4SLinus Torvalds bad_fork_cleanup:
12491da177e4SLinus Torvalds 	if (p->binfmt)
12501da177e4SLinus Torvalds 		module_put(p->binfmt->module);
12511da177e4SLinus Torvalds bad_fork_cleanup_put_domain:
1252a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
12531da177e4SLinus Torvalds bad_fork_cleanup_count:
12541da177e4SLinus Torvalds 	put_group_info(p->group_info);
12551da177e4SLinus Torvalds 	atomic_dec(&p->user->processes);
12561da177e4SLinus Torvalds 	free_uid(p->user);
12571da177e4SLinus Torvalds bad_fork_free:
12581da177e4SLinus Torvalds 	free_task(p);
1259fe7d37d1SOleg Nesterov fork_out:
1260fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
12641da177e4SLinus Torvalds {
12651da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
12661da177e4SLinus Torvalds 	return regs;
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds task_t * __devinit fork_idle(int cpu)
12701da177e4SLinus Torvalds {
12711da177e4SLinus Torvalds 	task_t *task;
12721da177e4SLinus Torvalds 	struct pt_regs regs;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0);
12751da177e4SLinus Torvalds 	if (!task)
12761da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
12771da177e4SLinus Torvalds 	init_idle(task, cpu);
127873b9ebfeSOleg Nesterov 
12791da177e4SLinus Torvalds 	return task;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds static inline int fork_traceflag (unsigned clone_flags)
12831da177e4SLinus Torvalds {
12841da177e4SLinus Torvalds 	if (clone_flags & CLONE_UNTRACED)
12851da177e4SLinus Torvalds 		return 0;
12861da177e4SLinus Torvalds 	else if (clone_flags & CLONE_VFORK) {
12871da177e4SLinus Torvalds 		if (current->ptrace & PT_TRACE_VFORK)
12881da177e4SLinus Torvalds 			return PTRACE_EVENT_VFORK;
12891da177e4SLinus Torvalds 	} else if ((clone_flags & CSIGNAL) != SIGCHLD) {
12901da177e4SLinus Torvalds 		if (current->ptrace & PT_TRACE_CLONE)
12911da177e4SLinus Torvalds 			return PTRACE_EVENT_CLONE;
12921da177e4SLinus Torvalds 	} else if (current->ptrace & PT_TRACE_FORK)
12931da177e4SLinus Torvalds 		return PTRACE_EVENT_FORK;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	return 0;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds /*
12991da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
13001da177e4SLinus Torvalds  *
13011da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
13021da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
13031da177e4SLinus Torvalds  */
13041da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
13051da177e4SLinus Torvalds 	      unsigned long stack_start,
13061da177e4SLinus Torvalds 	      struct pt_regs *regs,
13071da177e4SLinus Torvalds 	      unsigned long stack_size,
13081da177e4SLinus Torvalds 	      int __user *parent_tidptr,
13091da177e4SLinus Torvalds 	      int __user *child_tidptr)
13101da177e4SLinus Torvalds {
13111da177e4SLinus Torvalds 	struct task_struct *p;
13121da177e4SLinus Torvalds 	int trace = 0;
131392476d7fSEric W. Biederman 	struct pid *pid = alloc_pid();
131492476d7fSEric W. Biederman 	long nr;
13151da177e4SLinus Torvalds 
131692476d7fSEric W. Biederman 	if (!pid)
13171da177e4SLinus Torvalds 		return -EAGAIN;
131892476d7fSEric W. Biederman 	nr = pid->nr;
13191da177e4SLinus Torvalds 	if (unlikely(current->ptrace)) {
13201da177e4SLinus Torvalds 		trace = fork_traceflag (clone_flags);
13211da177e4SLinus Torvalds 		if (trace)
13221da177e4SLinus Torvalds 			clone_flags |= CLONE_PTRACE;
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
132592476d7fSEric W. Biederman 	p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, nr);
13261da177e4SLinus Torvalds 	/*
13271da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
13281da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
13291da177e4SLinus Torvalds 	 */
13301da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
13311da177e4SLinus Torvalds 		struct completion vfork;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
13341da177e4SLinus Torvalds 			p->vfork_done = &vfork;
13351da177e4SLinus Torvalds 			init_completion(&vfork);
13361da177e4SLinus Torvalds 		}
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 		if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
13391da177e4SLinus Torvalds 			/*
13401da177e4SLinus Torvalds 			 * We'll start up with an immediate SIGSTOP.
13411da177e4SLinus Torvalds 			 */
13421da177e4SLinus Torvalds 			sigaddset(&p->pending.signal, SIGSTOP);
13431da177e4SLinus Torvalds 			set_tsk_thread_flag(p, TIF_SIGPENDING);
13441da177e4SLinus Torvalds 		}
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 		if (!(clone_flags & CLONE_STOPPED))
13471da177e4SLinus Torvalds 			wake_up_new_task(p, clone_flags);
13481da177e4SLinus Torvalds 		else
13491da177e4SLinus Torvalds 			p->state = TASK_STOPPED;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 		if (unlikely (trace)) {
135292476d7fSEric W. Biederman 			current->ptrace_message = nr;
13531da177e4SLinus Torvalds 			ptrace_notify ((trace << 8) | SIGTRAP);
13541da177e4SLinus Torvalds 		}
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
13571da177e4SLinus Torvalds 			wait_for_completion(&vfork);
13581da177e4SLinus Torvalds 			if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
13591da177e4SLinus Torvalds 				ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
13601da177e4SLinus Torvalds 		}
13611da177e4SLinus Torvalds 	} else {
136292476d7fSEric W. Biederman 		free_pid(pid);
136392476d7fSEric W. Biederman 		nr = PTR_ERR(p);
13641da177e4SLinus Torvalds 	}
136592476d7fSEric W. Biederman 	return nr;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
13685fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN
13695fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0
13705fd63b30SRavikiran G Thirumalai #endif
13715fd63b30SRavikiran G Thirumalai 
1372aa1757f9SOleg Nesterov static void sighand_ctor(void *data, kmem_cache_t *cachep, unsigned long flags)
1373aa1757f9SOleg Nesterov {
1374aa1757f9SOleg Nesterov 	struct sighand_struct *sighand = data;
1375aa1757f9SOleg Nesterov 
1376aa1757f9SOleg Nesterov 	if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
1377aa1757f9SOleg Nesterov 					SLAB_CTOR_CONSTRUCTOR)
1378aa1757f9SOleg Nesterov 		spin_lock_init(&sighand->siglock);
1379aa1757f9SOleg Nesterov }
1380aa1757f9SOleg Nesterov 
13811da177e4SLinus Torvalds void __init proc_caches_init(void)
13821da177e4SLinus Torvalds {
13831da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
13841da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
1385aa1757f9SOleg Nesterov 			SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU,
1386aa1757f9SOleg Nesterov 			sighand_ctor, NULL);
13871da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
13881da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
13891da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13901da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
13911da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
13921da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13931da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
13941da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
13951da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13961da177e4SLinus Torvalds 	vm_area_cachep = kmem_cache_create("vm_area_struct",
13971da177e4SLinus Torvalds 			sizeof(struct vm_area_struct), 0,
13981da177e4SLinus Torvalds 			SLAB_PANIC, NULL, NULL);
13991da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
14005fd63b30SRavikiran G Thirumalai 			sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
14011da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
14021da177e4SLinus Torvalds }
1403cf2e340fSJANAK DESAI 
1404cf2e340fSJANAK DESAI 
1405cf2e340fSJANAK DESAI /*
1406cf2e340fSJANAK DESAI  * Check constraints on flags passed to the unshare system call and
1407cf2e340fSJANAK DESAI  * force unsharing of additional process context as appropriate.
1408cf2e340fSJANAK DESAI  */
1409cf2e340fSJANAK DESAI static inline void check_unshare_flags(unsigned long *flags_ptr)
1410cf2e340fSJANAK DESAI {
1411cf2e340fSJANAK DESAI 	/*
1412cf2e340fSJANAK DESAI 	 * If unsharing a thread from a thread group, must also
1413cf2e340fSJANAK DESAI 	 * unshare vm.
1414cf2e340fSJANAK DESAI 	 */
1415cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_THREAD)
1416cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_VM;
1417cf2e340fSJANAK DESAI 
1418cf2e340fSJANAK DESAI 	/*
1419cf2e340fSJANAK DESAI 	 * If unsharing vm, must also unshare signal handlers.
1420cf2e340fSJANAK DESAI 	 */
1421cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_VM)
1422cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_SIGHAND;
1423cf2e340fSJANAK DESAI 
1424cf2e340fSJANAK DESAI 	/*
1425cf2e340fSJANAK DESAI 	 * If unsharing signal handlers and the task was created
1426cf2e340fSJANAK DESAI 	 * using CLONE_THREAD, then must unshare the thread
1427cf2e340fSJANAK DESAI 	 */
1428cf2e340fSJANAK DESAI 	if ((*flags_ptr & CLONE_SIGHAND) &&
1429cf2e340fSJANAK DESAI 	    (atomic_read(&current->signal->count) > 1))
1430cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_THREAD;
1431cf2e340fSJANAK DESAI 
1432cf2e340fSJANAK DESAI 	/*
1433cf2e340fSJANAK DESAI 	 * If unsharing namespace, must also unshare filesystem information.
1434cf2e340fSJANAK DESAI 	 */
1435cf2e340fSJANAK DESAI 	if (*flags_ptr & CLONE_NEWNS)
1436cf2e340fSJANAK DESAI 		*flags_ptr |= CLONE_FS;
1437cf2e340fSJANAK DESAI }
1438cf2e340fSJANAK DESAI 
1439cf2e340fSJANAK DESAI /*
1440cf2e340fSJANAK DESAI  * Unsharing of tasks created with CLONE_THREAD is not supported yet
1441cf2e340fSJANAK DESAI  */
1442cf2e340fSJANAK DESAI static int unshare_thread(unsigned long unshare_flags)
1443cf2e340fSJANAK DESAI {
1444cf2e340fSJANAK DESAI 	if (unshare_flags & CLONE_THREAD)
1445cf2e340fSJANAK DESAI 		return -EINVAL;
1446cf2e340fSJANAK DESAI 
1447cf2e340fSJANAK DESAI 	return 0;
1448cf2e340fSJANAK DESAI }
1449cf2e340fSJANAK DESAI 
1450cf2e340fSJANAK DESAI /*
145199d1419dSJANAK DESAI  * Unshare the filesystem structure if it is being shared
1452cf2e340fSJANAK DESAI  */
1453cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1454cf2e340fSJANAK DESAI {
1455cf2e340fSJANAK DESAI 	struct fs_struct *fs = current->fs;
1456cf2e340fSJANAK DESAI 
1457cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FS) &&
145899d1419dSJANAK DESAI 	    (fs && atomic_read(&fs->count) > 1)) {
145999d1419dSJANAK DESAI 		*new_fsp = __copy_fs_struct(current->fs);
146099d1419dSJANAK DESAI 		if (!*new_fsp)
146199d1419dSJANAK DESAI 			return -ENOMEM;
146299d1419dSJANAK DESAI 	}
1463cf2e340fSJANAK DESAI 
1464cf2e340fSJANAK DESAI 	return 0;
1465cf2e340fSJANAK DESAI }
1466cf2e340fSJANAK DESAI 
1467cf2e340fSJANAK DESAI /*
1468741a2951SJANAK DESAI  * Unshare the namespace structure if it is being shared
1469cf2e340fSJANAK DESAI  */
1470741a2951SJANAK DESAI static int unshare_namespace(unsigned long unshare_flags, struct namespace **new_nsp, struct fs_struct *new_fs)
1471cf2e340fSJANAK DESAI {
1472cf2e340fSJANAK DESAI 	struct namespace *ns = current->namespace;
1473cf2e340fSJANAK DESAI 
1474cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_NEWNS) &&
1475741a2951SJANAK DESAI 	    (ns && atomic_read(&ns->count) > 1)) {
1476741a2951SJANAK DESAI 		if (!capable(CAP_SYS_ADMIN))
1477741a2951SJANAK DESAI 			return -EPERM;
1478741a2951SJANAK DESAI 
1479741a2951SJANAK DESAI 		*new_nsp = dup_namespace(current, new_fs ? new_fs : current->fs);
1480741a2951SJANAK DESAI 		if (!*new_nsp)
1481741a2951SJANAK DESAI 			return -ENOMEM;
1482741a2951SJANAK DESAI 	}
1483cf2e340fSJANAK DESAI 
1484cf2e340fSJANAK DESAI 	return 0;
1485cf2e340fSJANAK DESAI }
1486cf2e340fSJANAK DESAI 
1487cf2e340fSJANAK DESAI /*
1488cf2e340fSJANAK DESAI  * Unsharing of sighand for tasks created with CLONE_SIGHAND is not
1489cf2e340fSJANAK DESAI  * supported yet
1490cf2e340fSJANAK DESAI  */
1491cf2e340fSJANAK DESAI static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
1492cf2e340fSJANAK DESAI {
1493cf2e340fSJANAK DESAI 	struct sighand_struct *sigh = current->sighand;
1494cf2e340fSJANAK DESAI 
1495cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_SIGHAND) &&
1496cf2e340fSJANAK DESAI 	    (sigh && atomic_read(&sigh->count) > 1))
1497cf2e340fSJANAK DESAI 		return -EINVAL;
1498cf2e340fSJANAK DESAI 	else
1499cf2e340fSJANAK DESAI 		return 0;
1500cf2e340fSJANAK DESAI }
1501cf2e340fSJANAK DESAI 
1502cf2e340fSJANAK DESAI /*
1503a0a7ec30SJANAK DESAI  * Unshare vm if it is being shared
1504cf2e340fSJANAK DESAI  */
1505cf2e340fSJANAK DESAI static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp)
1506cf2e340fSJANAK DESAI {
1507cf2e340fSJANAK DESAI 	struct mm_struct *mm = current->mm;
1508cf2e340fSJANAK DESAI 
1509cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_VM) &&
1510a0a7ec30SJANAK DESAI 	    (mm && atomic_read(&mm->mm_users) > 1)) {
15112d61b867SOleg Nesterov 		return -EINVAL;
1512a0a7ec30SJANAK DESAI 	}
1513cf2e340fSJANAK DESAI 
1514cf2e340fSJANAK DESAI 	return 0;
1515cf2e340fSJANAK DESAI }
1516cf2e340fSJANAK DESAI 
1517cf2e340fSJANAK DESAI /*
1518a016f338SJANAK DESAI  * Unshare file descriptor table if it is being shared
1519cf2e340fSJANAK DESAI  */
1520cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1521cf2e340fSJANAK DESAI {
1522cf2e340fSJANAK DESAI 	struct files_struct *fd = current->files;
1523a016f338SJANAK DESAI 	int error = 0;
1524cf2e340fSJANAK DESAI 
1525cf2e340fSJANAK DESAI 	if ((unshare_flags & CLONE_FILES) &&
1526a016f338SJANAK DESAI 	    (fd && atomic_read(&fd->count) > 1)) {
1527a016f338SJANAK DESAI 		*new_fdp = dup_fd(fd, &error);
1528a016f338SJANAK DESAI 		if (!*new_fdp)
1529a016f338SJANAK DESAI 			return error;
1530a016f338SJANAK DESAI 	}
1531cf2e340fSJANAK DESAI 
1532cf2e340fSJANAK DESAI 	return 0;
1533cf2e340fSJANAK DESAI }
1534cf2e340fSJANAK DESAI 
1535cf2e340fSJANAK DESAI /*
1536cf2e340fSJANAK DESAI  * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
1537cf2e340fSJANAK DESAI  * supported yet
1538cf2e340fSJANAK DESAI  */
1539cf2e340fSJANAK DESAI static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp)
1540cf2e340fSJANAK DESAI {
1541cf2e340fSJANAK DESAI 	if (unshare_flags & CLONE_SYSVSEM)
1542cf2e340fSJANAK DESAI 		return -EINVAL;
1543cf2e340fSJANAK DESAI 
1544cf2e340fSJANAK DESAI 	return 0;
1545cf2e340fSJANAK DESAI }
1546cf2e340fSJANAK DESAI 
1547cf2e340fSJANAK DESAI /*
1548cf2e340fSJANAK DESAI  * unshare allows a process to 'unshare' part of the process
1549cf2e340fSJANAK DESAI  * context which was originally shared using clone.  copy_*
1550cf2e340fSJANAK DESAI  * functions used by do_fork() cannot be used here directly
1551cf2e340fSJANAK DESAI  * because they modify an inactive task_struct that is being
1552cf2e340fSJANAK DESAI  * constructed. Here we are modifying the current, active,
1553cf2e340fSJANAK DESAI  * task_struct.
1554cf2e340fSJANAK DESAI  */
1555cf2e340fSJANAK DESAI asmlinkage long sys_unshare(unsigned long unshare_flags)
1556cf2e340fSJANAK DESAI {
1557cf2e340fSJANAK DESAI 	int err = 0;
1558cf2e340fSJANAK DESAI 	struct fs_struct *fs, *new_fs = NULL;
1559cf2e340fSJANAK DESAI 	struct namespace *ns, *new_ns = NULL;
1560cf2e340fSJANAK DESAI 	struct sighand_struct *sigh, *new_sigh = NULL;
1561cf2e340fSJANAK DESAI 	struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
1562cf2e340fSJANAK DESAI 	struct files_struct *fd, *new_fd = NULL;
1563cf2e340fSJANAK DESAI 	struct sem_undo_list *new_ulist = NULL;
1564cf2e340fSJANAK DESAI 
1565cf2e340fSJANAK DESAI 	check_unshare_flags(&unshare_flags);
1566cf2e340fSJANAK DESAI 
156706f9d4f9SEric W. Biederman 	/* Return -EINVAL for all unsupported flags */
156806f9d4f9SEric W. Biederman 	err = -EINVAL;
156906f9d4f9SEric W. Biederman 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
157006f9d4f9SEric W. Biederman 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM))
157106f9d4f9SEric W. Biederman 		goto bad_unshare_out;
157206f9d4f9SEric W. Biederman 
1573cf2e340fSJANAK DESAI 	if ((err = unshare_thread(unshare_flags)))
1574cf2e340fSJANAK DESAI 		goto bad_unshare_out;
1575cf2e340fSJANAK DESAI 	if ((err = unshare_fs(unshare_flags, &new_fs)))
1576cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_thread;
1577741a2951SJANAK DESAI 	if ((err = unshare_namespace(unshare_flags, &new_ns, new_fs)))
1578cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_fs;
1579cf2e340fSJANAK DESAI 	if ((err = unshare_sighand(unshare_flags, &new_sigh)))
1580cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_ns;
1581cf2e340fSJANAK DESAI 	if ((err = unshare_vm(unshare_flags, &new_mm)))
1582cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_sigh;
1583cf2e340fSJANAK DESAI 	if ((err = unshare_fd(unshare_flags, &new_fd)))
1584cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_vm;
1585cf2e340fSJANAK DESAI 	if ((err = unshare_semundo(unshare_flags, &new_ulist)))
1586cf2e340fSJANAK DESAI 		goto bad_unshare_cleanup_fd;
1587cf2e340fSJANAK DESAI 
1588cf2e340fSJANAK DESAI 	if (new_fs || new_ns || new_sigh || new_mm || new_fd || new_ulist) {
1589cf2e340fSJANAK DESAI 
1590cf2e340fSJANAK DESAI 		task_lock(current);
1591cf2e340fSJANAK DESAI 
1592cf2e340fSJANAK DESAI 		if (new_fs) {
1593cf2e340fSJANAK DESAI 			fs = current->fs;
1594cf2e340fSJANAK DESAI 			current->fs = new_fs;
1595cf2e340fSJANAK DESAI 			new_fs = fs;
1596cf2e340fSJANAK DESAI 		}
1597cf2e340fSJANAK DESAI 
1598cf2e340fSJANAK DESAI 		if (new_ns) {
1599cf2e340fSJANAK DESAI 			ns = current->namespace;
1600cf2e340fSJANAK DESAI 			current->namespace = new_ns;
1601cf2e340fSJANAK DESAI 			new_ns = ns;
1602cf2e340fSJANAK DESAI 		}
1603cf2e340fSJANAK DESAI 
1604cf2e340fSJANAK DESAI 		if (new_sigh) {
1605cf2e340fSJANAK DESAI 			sigh = current->sighand;
1606e0e8eb54SEric W. Biederman 			rcu_assign_pointer(current->sighand, new_sigh);
1607cf2e340fSJANAK DESAI 			new_sigh = sigh;
1608cf2e340fSJANAK DESAI 		}
1609cf2e340fSJANAK DESAI 
1610cf2e340fSJANAK DESAI 		if (new_mm) {
1611cf2e340fSJANAK DESAI 			mm = current->mm;
1612cf2e340fSJANAK DESAI 			active_mm = current->active_mm;
1613cf2e340fSJANAK DESAI 			current->mm = new_mm;
1614cf2e340fSJANAK DESAI 			current->active_mm = new_mm;
1615cf2e340fSJANAK DESAI 			activate_mm(active_mm, new_mm);
1616cf2e340fSJANAK DESAI 			new_mm = mm;
1617cf2e340fSJANAK DESAI 		}
1618cf2e340fSJANAK DESAI 
1619cf2e340fSJANAK DESAI 		if (new_fd) {
1620cf2e340fSJANAK DESAI 			fd = current->files;
1621cf2e340fSJANAK DESAI 			current->files = new_fd;
1622cf2e340fSJANAK DESAI 			new_fd = fd;
1623cf2e340fSJANAK DESAI 		}
1624cf2e340fSJANAK DESAI 
1625cf2e340fSJANAK DESAI 		task_unlock(current);
1626cf2e340fSJANAK DESAI 	}
1627cf2e340fSJANAK DESAI 
1628cf2e340fSJANAK DESAI bad_unshare_cleanup_fd:
1629cf2e340fSJANAK DESAI 	if (new_fd)
1630cf2e340fSJANAK DESAI 		put_files_struct(new_fd);
1631cf2e340fSJANAK DESAI 
1632cf2e340fSJANAK DESAI bad_unshare_cleanup_vm:
1633cf2e340fSJANAK DESAI 	if (new_mm)
1634cf2e340fSJANAK DESAI 		mmput(new_mm);
1635cf2e340fSJANAK DESAI 
1636cf2e340fSJANAK DESAI bad_unshare_cleanup_sigh:
1637cf2e340fSJANAK DESAI 	if (new_sigh)
1638cf2e340fSJANAK DESAI 		if (atomic_dec_and_test(&new_sigh->count))
1639cf2e340fSJANAK DESAI 			kmem_cache_free(sighand_cachep, new_sigh);
1640cf2e340fSJANAK DESAI 
1641cf2e340fSJANAK DESAI bad_unshare_cleanup_ns:
1642cf2e340fSJANAK DESAI 	if (new_ns)
1643cf2e340fSJANAK DESAI 		put_namespace(new_ns);
1644cf2e340fSJANAK DESAI 
1645cf2e340fSJANAK DESAI bad_unshare_cleanup_fs:
1646cf2e340fSJANAK DESAI 	if (new_fs)
1647cf2e340fSJANAK DESAI 		put_fs_struct(new_fs);
1648cf2e340fSJANAK DESAI 
1649cf2e340fSJANAK DESAI bad_unshare_cleanup_thread:
1650cf2e340fSJANAK DESAI bad_unshare_out:
1651cf2e340fSJANAK DESAI 	return err;
1652cf2e340fSJANAK DESAI }
1653