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