xref: /openbmc/linux/kernel/fork.c (revision d1209d049bbc3df66650f8417637be4f7b57b604)
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>
311da177e4SLinus Torvalds #include <linux/cpu.h>
321da177e4SLinus Torvalds #include <linux/cpuset.h>
331da177e4SLinus Torvalds #include <linux/security.h>
341da177e4SLinus Torvalds #include <linux/swap.h>
351da177e4SLinus Torvalds #include <linux/syscalls.h>
361da177e4SLinus Torvalds #include <linux/jiffies.h>
371da177e4SLinus Torvalds #include <linux/futex.h>
38ab2af1f5SDipankar Sarma #include <linux/rcupdate.h>
391da177e4SLinus Torvalds #include <linux/ptrace.h>
401da177e4SLinus Torvalds #include <linux/mount.h>
411da177e4SLinus Torvalds #include <linux/audit.h>
421da177e4SLinus Torvalds #include <linux/profile.h>
431da177e4SLinus Torvalds #include <linux/rmap.h>
441da177e4SLinus Torvalds #include <linux/acct.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <asm/pgtable.h>
471da177e4SLinus Torvalds #include <asm/pgalloc.h>
481da177e4SLinus Torvalds #include <asm/uaccess.h>
491da177e4SLinus Torvalds #include <asm/mmu_context.h>
501da177e4SLinus Torvalds #include <asm/cacheflush.h>
511da177e4SLinus Torvalds #include <asm/tlbflush.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * Protected counters by write_lock_irq(&tasklist_lock)
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds unsigned long total_forks;	/* Handle normal Linux uptimes. */
571da177e4SLinus Torvalds int nr_threads; 		/* The idle threads do not count.. */
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds int max_threads;		/* tunable limit on nr_threads */
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds  __cacheline_aligned DEFINE_RWLOCK(tasklist_lock);  /* outer */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds EXPORT_SYMBOL(tasklist_lock);
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds int nr_processes(void)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	int cpu;
701da177e4SLinus Torvalds 	int total = 0;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	for_each_online_cpu(cpu)
731da177e4SLinus Torvalds 		total += per_cpu(process_counts, cpu);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	return total;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
791da177e4SLinus Torvalds # define alloc_task_struct()	kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
801da177e4SLinus Torvalds # define free_task_struct(tsk)	kmem_cache_free(task_struct_cachep, (tsk))
811da177e4SLinus Torvalds static kmem_cache_t *task_struct_cachep;
821da177e4SLinus Torvalds #endif
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */
851da177e4SLinus Torvalds kmem_cache_t *signal_cachep;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */
881da177e4SLinus Torvalds kmem_cache_t *sighand_cachep;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */
911da177e4SLinus Torvalds kmem_cache_t *files_cachep;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */
941da177e4SLinus Torvalds kmem_cache_t *fs_cachep;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */
971da177e4SLinus Torvalds kmem_cache_t *vm_area_cachep;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */
1001da177e4SLinus Torvalds static kmem_cache_t *mm_cachep;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds void free_task(struct task_struct *tsk)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	free_thread_info(tsk->thread_info);
1051da177e4SLinus Torvalds 	free_task_struct(tsk);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds EXPORT_SYMBOL(free_task);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds void __put_task_struct(struct task_struct *tsk)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
1121da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1131da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	if (unlikely(tsk->audit_context))
1161da177e4SLinus Torvalds 		audit_free(tsk);
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 	*ti = *orig->thread_info;
1741da177e4SLinus Torvalds 	*tsk = *orig;
1751da177e4SLinus Torvalds 	tsk->thread_info = ti;
1761da177e4SLinus Torvalds 	ti->task = tsk;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/* One for us, one for whoever does the "release_task()" (usually parent) */
1791da177e4SLinus Torvalds 	atomic_set(&tsk->usage,2);
1804b5d37acSGiancarlo Formicuccia 	atomic_set(&tsk->fs_excl, 0);
1811da177e4SLinus Torvalds 	return tsk;
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds #ifdef CONFIG_MMU
1851da177e4SLinus Torvalds static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	struct vm_area_struct * mpnt, *tmp, **pprev;
1881da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
1891da177e4SLinus Torvalds 	int retval;
1901da177e4SLinus Torvalds 	unsigned long charge;
1911da177e4SLinus Torvalds 	struct mempolicy *pol;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
1941da177e4SLinus Torvalds 	flush_cache_mm(current->mm);
1951da177e4SLinus Torvalds 	mm->locked_vm = 0;
1961da177e4SLinus Torvalds 	mm->mmap = NULL;
1971da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
1981da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
1991363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
2001da177e4SLinus Torvalds 	mm->map_count = 0;
2011da177e4SLinus Torvalds 	set_mm_counter(mm, rss, 0);
2021da177e4SLinus Torvalds 	set_mm_counter(mm, anon_rss, 0);
2031da177e4SLinus Torvalds 	cpus_clear(mm->cpu_vm_mask);
2041da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
2051da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
2061da177e4SLinus Torvalds 	rb_parent = NULL;
2071da177e4SLinus Torvalds 	pprev = &mm->mmap;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
2101da177e4SLinus Torvalds 		struct file *file;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
2133b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
2143b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
2151da177e4SLinus Torvalds 			__vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
2163b6bfcdbSHugh Dickins 								-pages);
2171da177e4SLinus Torvalds 			continue;
2181da177e4SLinus Torvalds 		}
2191da177e4SLinus Torvalds 		charge = 0;
2201da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
2211da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
2221da177e4SLinus Torvalds 			if (security_vm_enough_memory(len))
2231da177e4SLinus Torvalds 				goto fail_nomem;
2241da177e4SLinus Torvalds 			charge = len;
2251da177e4SLinus Torvalds 		}
2261da177e4SLinus Torvalds 		tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2271da177e4SLinus Torvalds 		if (!tmp)
2281da177e4SLinus Torvalds 			goto fail_nomem;
2291da177e4SLinus Torvalds 		*tmp = *mpnt;
2301da177e4SLinus Torvalds 		pol = mpol_copy(vma_policy(mpnt));
2311da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
2321da177e4SLinus Torvalds 		if (IS_ERR(pol))
2331da177e4SLinus Torvalds 			goto fail_nomem_policy;
2341da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
2351da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
2361da177e4SLinus Torvalds 		tmp->vm_mm = mm;
2371da177e4SLinus Torvalds 		tmp->vm_next = NULL;
2381da177e4SLinus Torvalds 		anon_vma_link(tmp);
2391da177e4SLinus Torvalds 		file = tmp->vm_file;
2401da177e4SLinus Torvalds 		if (file) {
2411da177e4SLinus Torvalds 			struct inode *inode = file->f_dentry->d_inode;
2421da177e4SLinus Torvalds 			get_file(file);
2431da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
2441da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 			/* insert tmp into the share list, just after mpnt */
2471da177e4SLinus Torvalds 			spin_lock(&file->f_mapping->i_mmap_lock);
2481da177e4SLinus Torvalds 			tmp->vm_truncate_count = mpnt->vm_truncate_count;
2491da177e4SLinus Torvalds 			flush_dcache_mmap_lock(file->f_mapping);
2501da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
2511da177e4SLinus Torvalds 			flush_dcache_mmap_unlock(file->f_mapping);
2521da177e4SLinus Torvalds 			spin_unlock(&file->f_mapping->i_mmap_lock);
2531da177e4SLinus Torvalds 		}
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 		/*
2561da177e4SLinus Torvalds 		 * Link in the new vma and copy the page table entries:
25745918e1aSHugh Dickins 		 * link in first so that swapoff can see swap entries.
25845918e1aSHugh Dickins 		 * Note that, exceptionally, here the vma is inserted
25945918e1aSHugh Dickins 		 * without holding mm->mmap_sem.
2601da177e4SLinus Torvalds 		 */
2611da177e4SLinus Torvalds 		spin_lock(&mm->page_table_lock);
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++;
2701da177e4SLinus Torvalds 		retval = copy_page_range(mm, current->mm, tmp);
2711da177e4SLinus Torvalds 		spin_unlock(&mm->page_table_lock);
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
2741da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		if (retval)
2771da177e4SLinus Torvalds 			goto out;
2781da177e4SLinus Torvalds 	}
2791da177e4SLinus Torvalds 	retval = 0;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds out:
2821da177e4SLinus Torvalds 	flush_tlb_mm(current->mm);
2831da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
2841da177e4SLinus Torvalds 	return retval;
2851da177e4SLinus Torvalds fail_nomem_policy:
2861da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
2871da177e4SLinus Torvalds fail_nomem:
2881da177e4SLinus Torvalds 	retval = -ENOMEM;
2891da177e4SLinus Torvalds 	vm_unacct_memory(charge);
2901da177e4SLinus Torvalds 	goto out;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
2961da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
2971da177e4SLinus Torvalds 		return -ENOMEM;
2981da177e4SLinus Torvalds 	return 0;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	pgd_free(mm->pgd);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds #else
3061da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
3071da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
3081da177e4SLinus Torvalds #define mm_free_pgd(mm)
3091da177e4SLinus Torvalds #endif /* CONFIG_MMU */
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds  __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds #define allocate_mm()	(kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
3141da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds #include <linux/init_task.h>
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static struct mm_struct * mm_init(struct mm_struct * mm)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
3211da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
3221da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
3231da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
3241da177e4SLinus Torvalds 	mm->core_waiters = 0;
3251da177e4SLinus Torvalds 	mm->nr_ptes = 0;
3261da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
3271da177e4SLinus Torvalds 	rwlock_init(&mm->ioctx_list_lock);
3281da177e4SLinus Torvalds 	mm->ioctx_list = NULL;
3291da177e4SLinus Torvalds 	mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
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 
4521da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	struct mm_struct * mm, *oldmm;
4551da177e4SLinus Torvalds 	int retval;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
4581da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	tsk->mm = NULL;
4611da177e4SLinus Torvalds 	tsk->active_mm = NULL;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	/*
4641da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
4651da177e4SLinus Torvalds 	 *
4661da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
4671da177e4SLinus Torvalds 	 */
4681da177e4SLinus Torvalds 	oldmm = current->mm;
4691da177e4SLinus Torvalds 	if (!oldmm)
4701da177e4SLinus Torvalds 		return 0;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
4731da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
4741da177e4SLinus Torvalds 		mm = oldmm;
4751da177e4SLinus Torvalds 		/*
4761da177e4SLinus Torvalds 		 * There are cases where the PTL is held to ensure no
4771da177e4SLinus Torvalds 		 * new threads start up in user mode using an mm, which
4781da177e4SLinus Torvalds 		 * allows optimizing out ipis; the tlb_gather_mmu code
4791da177e4SLinus Torvalds 		 * is an example.
4801da177e4SLinus Torvalds 		 */
4811da177e4SLinus Torvalds 		spin_unlock_wait(&oldmm->page_table_lock);
4821da177e4SLinus Torvalds 		goto good_mm;
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	retval = -ENOMEM;
4861da177e4SLinus Torvalds 	mm = allocate_mm();
4871da177e4SLinus Torvalds 	if (!mm)
4881da177e4SLinus Torvalds 		goto fail_nomem;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	/* Copy the current MM stuff.. */
4911da177e4SLinus Torvalds 	memcpy(mm, oldmm, sizeof(*mm));
4921da177e4SLinus Torvalds 	if (!mm_init(mm))
4931da177e4SLinus Torvalds 		goto fail_nomem;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	if (init_new_context(tsk,mm))
4961da177e4SLinus Torvalds 		goto fail_nocontext;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	retval = dup_mmap(mm, oldmm);
4991da177e4SLinus Torvalds 	if (retval)
5001da177e4SLinus Torvalds 		goto free_pt;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	mm->hiwater_rss = get_mm_counter(mm,rss);
5031da177e4SLinus Torvalds 	mm->hiwater_vm = mm->total_vm;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds good_mm:
5061da177e4SLinus Torvalds 	tsk->mm = mm;
5071da177e4SLinus Torvalds 	tsk->active_mm = mm;
5081da177e4SLinus Torvalds 	return 0;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds free_pt:
5111da177e4SLinus Torvalds 	mmput(mm);
5121da177e4SLinus Torvalds fail_nomem:
5131da177e4SLinus Torvalds 	return retval;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds fail_nocontext:
5161da177e4SLinus Torvalds 	/*
5171da177e4SLinus Torvalds 	 * If init_new_context() failed, we cannot use mmput() to free the mm
5181da177e4SLinus Torvalds 	 * because it calls destroy_context()
5191da177e4SLinus Torvalds 	 */
5201da177e4SLinus Torvalds 	mm_free_pgd(mm);
5211da177e4SLinus Torvalds 	free_mm(mm);
5221da177e4SLinus Torvalds 	return retval;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
5281da177e4SLinus Torvalds 	/* We don't need to lock fs - think why ;-) */
5291da177e4SLinus Torvalds 	if (fs) {
5301da177e4SLinus Torvalds 		atomic_set(&fs->count, 1);
5311da177e4SLinus Torvalds 		rwlock_init(&fs->lock);
5321da177e4SLinus Torvalds 		fs->umask = old->umask;
5331da177e4SLinus Torvalds 		read_lock(&old->lock);
5341da177e4SLinus Torvalds 		fs->rootmnt = mntget(old->rootmnt);
5351da177e4SLinus Torvalds 		fs->root = dget(old->root);
5361da177e4SLinus Torvalds 		fs->pwdmnt = mntget(old->pwdmnt);
5371da177e4SLinus Torvalds 		fs->pwd = dget(old->pwd);
5381da177e4SLinus Torvalds 		if (old->altroot) {
5391da177e4SLinus Torvalds 			fs->altrootmnt = mntget(old->altrootmnt);
5401da177e4SLinus Torvalds 			fs->altroot = dget(old->altroot);
5411da177e4SLinus Torvalds 		} else {
5421da177e4SLinus Torvalds 			fs->altrootmnt = NULL;
5431da177e4SLinus Torvalds 			fs->altroot = NULL;
5441da177e4SLinus Torvalds 		}
5451da177e4SLinus Torvalds 		read_unlock(&old->lock);
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 	return fs;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds struct fs_struct *copy_fs_struct(struct fs_struct *old)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	return __copy_fs_struct(old);
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(copy_fs_struct);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
5601da177e4SLinus Torvalds 		atomic_inc(&current->fs->count);
5611da177e4SLinus Torvalds 		return 0;
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds 	tsk->fs = __copy_fs_struct(current->fs);
5641da177e4SLinus Torvalds 	if (!tsk->fs)
5651da177e4SLinus Torvalds 		return -ENOMEM;
5661da177e4SLinus Torvalds 	return 0;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
569ab2af1f5SDipankar Sarma static int count_open_files(struct fdtable *fdt)
5701da177e4SLinus Torvalds {
571ab2af1f5SDipankar Sarma 	int size = fdt->max_fdset;
5721da177e4SLinus Torvalds 	int i;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/* Find the last open fd */
5751da177e4SLinus Torvalds 	for (i = size/(8*sizeof(long)); i > 0; ) {
576badf1662SDipankar Sarma 		if (fdt->open_fds->fds_bits[--i])
5771da177e4SLinus Torvalds 			break;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 	i = (i+1) * 8 * sizeof(long);
5801da177e4SLinus Torvalds 	return i;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
583badf1662SDipankar Sarma static struct files_struct *alloc_files(void)
584badf1662SDipankar Sarma {
585badf1662SDipankar Sarma 	struct files_struct *newf;
586badf1662SDipankar Sarma 	struct fdtable *fdt;
587badf1662SDipankar Sarma 
588badf1662SDipankar Sarma 	newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
589badf1662SDipankar Sarma 	if (!newf)
590badf1662SDipankar Sarma 		goto out;
591badf1662SDipankar Sarma 
592badf1662SDipankar Sarma 	atomic_set(&newf->count, 1);
593badf1662SDipankar Sarma 
594badf1662SDipankar Sarma 	spin_lock_init(&newf->file_lock);
595ab2af1f5SDipankar Sarma 	fdt = &newf->fdtab;
596badf1662SDipankar Sarma 	fdt->next_fd = 0;
597badf1662SDipankar Sarma 	fdt->max_fds = NR_OPEN_DEFAULT;
598badf1662SDipankar Sarma 	fdt->max_fdset = __FD_SETSIZE;
599badf1662SDipankar Sarma 	fdt->close_on_exec = &newf->close_on_exec_init;
600badf1662SDipankar Sarma 	fdt->open_fds = &newf->open_fds_init;
601badf1662SDipankar Sarma 	fdt->fd = &newf->fd_array[0];
602ab2af1f5SDipankar Sarma 	INIT_RCU_HEAD(&fdt->rcu);
603ab2af1f5SDipankar Sarma 	fdt->free_files = NULL;
604ab2af1f5SDipankar Sarma 	fdt->next = NULL;
605ab2af1f5SDipankar Sarma 	rcu_assign_pointer(newf->fdt, fdt);
606badf1662SDipankar Sarma out:
607badf1662SDipankar Sarma 	return newf;
608badf1662SDipankar Sarma }
609badf1662SDipankar Sarma 
6101da177e4SLinus Torvalds static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
6111da177e4SLinus Torvalds {
6121da177e4SLinus Torvalds 	struct files_struct *oldf, *newf;
6131da177e4SLinus Torvalds 	struct file **old_fds, **new_fds;
6141da177e4SLinus Torvalds 	int open_files, size, i, error = 0, expand;
615badf1662SDipankar Sarma 	struct fdtable *old_fdt, *new_fdt;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	/*
6181da177e4SLinus Torvalds 	 * A background process may not have any files ...
6191da177e4SLinus Torvalds 	 */
6201da177e4SLinus Torvalds 	oldf = current->files;
6211da177e4SLinus Torvalds 	if (!oldf)
6221da177e4SLinus Torvalds 		goto out;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	if (clone_flags & CLONE_FILES) {
6251da177e4SLinus Torvalds 		atomic_inc(&oldf->count);
6261da177e4SLinus Torvalds 		goto out;
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	/*
6301da177e4SLinus Torvalds 	 * Note: we may be using current for both targets (See exec.c)
6311da177e4SLinus Torvalds 	 * This works because we cache current->files (old) as oldf. Don't
6321da177e4SLinus Torvalds 	 * break this.
6331da177e4SLinus Torvalds 	 */
6341da177e4SLinus Torvalds 	tsk->files = NULL;
6351da177e4SLinus Torvalds 	error = -ENOMEM;
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);
6641da177e4SLinus Torvalds 		error = expand_files(newf, open_files-1);
6651da177e4SLinus Torvalds 		spin_unlock(&newf->file_lock);
6661da177e4SLinus Torvalds 		if (error < 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 	tsk->files = newf;
7161da177e4SLinus Torvalds 	error = 0;
7171da177e4SLinus Torvalds out:
7181da177e4SLinus Torvalds 	return error;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds out_release:
721badf1662SDipankar Sarma 	free_fdset (new_fdt->close_on_exec, new_fdt->max_fdset);
722badf1662SDipankar Sarma 	free_fdset (new_fdt->open_fds, new_fdt->max_fdset);
723badf1662SDipankar Sarma 	free_fd_array(new_fdt->fd, new_fdt->max_fds);
7241da177e4SLinus Torvalds 	kmem_cache_free(files_cachep, newf);
7251da177e4SLinus Torvalds 	goto out;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds /*
7291da177e4SLinus Torvalds  *	Helper to unshare the files of the current task.
7301da177e4SLinus Torvalds  *	We don't want to expose copy_files internals to
7311da177e4SLinus Torvalds  *	the exec layer of the kernel.
7321da177e4SLinus Torvalds  */
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds int unshare_files(void)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct files_struct *files  = current->files;
7371da177e4SLinus Torvalds 	int rc;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	if(!files)
7401da177e4SLinus Torvalds 		BUG();
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/* This can race but the race causes us to copy when we don't
7431da177e4SLinus Torvalds 	   need to and drop the copy */
7441da177e4SLinus Torvalds 	if(atomic_read(&files->count) == 1)
7451da177e4SLinus Torvalds 	{
7461da177e4SLinus Torvalds 		atomic_inc(&files->count);
7471da177e4SLinus Torvalds 		return 0;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 	rc = copy_files(0, current);
7501da177e4SLinus Torvalds 	if(rc)
7511da177e4SLinus Torvalds 		current->files = files;
7521da177e4SLinus Torvalds 	return rc;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds EXPORT_SYMBOL(unshare_files);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	struct sighand_struct *sig;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
7621da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
7631da177e4SLinus Torvalds 		return 0;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
7661da177e4SLinus Torvalds 	tsk->sighand = sig;
7671da177e4SLinus Torvalds 	if (!sig)
7681da177e4SLinus Torvalds 		return -ENOMEM;
7691da177e4SLinus Torvalds 	spin_lock_init(&sig->siglock);
7701da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
7711da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
7721da177e4SLinus Torvalds 	return 0;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
7761da177e4SLinus Torvalds {
7771da177e4SLinus Torvalds 	struct signal_struct *sig;
7781da177e4SLinus Torvalds 	int ret;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	if (clone_flags & CLONE_THREAD) {
7811da177e4SLinus Torvalds 		atomic_inc(&current->signal->count);
7821da177e4SLinus Torvalds 		atomic_inc(&current->signal->live);
7831da177e4SLinus Torvalds 		return 0;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 	sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
7861da177e4SLinus Torvalds 	tsk->signal = sig;
7871da177e4SLinus Torvalds 	if (!sig)
7881da177e4SLinus Torvalds 		return -ENOMEM;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	ret = copy_thread_group_keys(tsk);
7911da177e4SLinus Torvalds 	if (ret < 0) {
7921da177e4SLinus Torvalds 		kmem_cache_free(signal_cachep, sig);
7931da177e4SLinus Torvalds 		return ret;
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
7971da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
7981da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
7991da177e4SLinus Torvalds 	sig->flags = 0;
8001da177e4SLinus Torvalds 	sig->group_exit_code = 0;
8011da177e4SLinus Torvalds 	sig->group_exit_task = NULL;
8021da177e4SLinus Torvalds 	sig->group_stop_count = 0;
8031da177e4SLinus Torvalds 	sig->curr_target = NULL;
8041da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
8051da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	sig->it_real_value = sig->it_real_incr = 0;
8081da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
8091da177e4SLinus Torvalds 	sig->real_timer.data = (unsigned long) tsk;
8101da177e4SLinus Torvalds 	init_timer(&sig->real_timer);
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	sig->it_virt_expires = cputime_zero;
8131da177e4SLinus Torvalds 	sig->it_virt_incr = cputime_zero;
8141da177e4SLinus Torvalds 	sig->it_prof_expires = cputime_zero;
8151da177e4SLinus Torvalds 	sig->it_prof_incr = cputime_zero;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	sig->tty = current->signal->tty;
8181da177e4SLinus Torvalds 	sig->pgrp = process_group(current);
8191da177e4SLinus Torvalds 	sig->session = current->signal->session;
8201da177e4SLinus Torvalds 	sig->leader = 0;	/* session leadership doesn't inherit */
8211da177e4SLinus Torvalds 	sig->tty_old_pgrp = 0;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
8241da177e4SLinus Torvalds 	sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
8251da177e4SLinus Torvalds 	sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
8261da177e4SLinus Torvalds 	sig->sched_time = 0;
8271da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
8281da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
8291da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	task_lock(current->group_leader);
8321da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
8331da177e4SLinus Torvalds 	task_unlock(current->group_leader);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
8361da177e4SLinus Torvalds 		/*
8371da177e4SLinus Torvalds 		 * New sole thread in the process gets an expiry time
8381da177e4SLinus Torvalds 		 * of the whole CPU time limit.
8391da177e4SLinus Torvalds 		 */
8401da177e4SLinus Torvalds 		tsk->it_prof_expires =
8411da177e4SLinus Torvalds 			secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	return 0;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
8501da177e4SLinus Torvalds 
851*d1209d04SAlan Stern 	new_flags &= ~(PF_SUPERPRIV | PF_NOFREEZE);
8521da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
8531da177e4SLinus Torvalds 	if (!(clone_flags & CLONE_PTRACE))
8541da177e4SLinus Torvalds 		p->ptrace = 0;
8551da177e4SLinus Torvalds 	p->flags = new_flags;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds asmlinkage long sys_set_tid_address(int __user *tidptr)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	return current->pid;
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds /*
8661da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
8671da177e4SLinus Torvalds  * but does not actually start it yet.
8681da177e4SLinus Torvalds  *
8691da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
8701da177e4SLinus Torvalds  * parts of the process environment (as per the clone
8711da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
8721da177e4SLinus Torvalds  */
8731da177e4SLinus Torvalds static task_t *copy_process(unsigned long clone_flags,
8741da177e4SLinus Torvalds 				 unsigned long stack_start,
8751da177e4SLinus Torvalds 				 struct pt_regs *regs,
8761da177e4SLinus Torvalds 				 unsigned long stack_size,
8771da177e4SLinus Torvalds 				 int __user *parent_tidptr,
8781da177e4SLinus Torvalds 				 int __user *child_tidptr,
8791da177e4SLinus Torvalds 				 int pid)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	int retval;
8821da177e4SLinus Torvalds 	struct task_struct *p = NULL;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
8851da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	/*
8881da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
8891da177e4SLinus Torvalds 	 * can only be started up within the thread group.
8901da177e4SLinus Torvalds 	 */
8911da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
8921da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	/*
8951da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
8961da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
8971da177e4SLinus Torvalds 	 * for various simplifications in other code.
8981da177e4SLinus Torvalds 	 */
8991da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
9001da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
9031da177e4SLinus Torvalds 	if (retval)
9041da177e4SLinus Torvalds 		goto fork_out;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	retval = -ENOMEM;
9071da177e4SLinus Torvalds 	p = dup_task_struct(current);
9081da177e4SLinus Torvalds 	if (!p)
9091da177e4SLinus Torvalds 		goto fork_out;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	retval = -EAGAIN;
9121da177e4SLinus Torvalds 	if (atomic_read(&p->user->processes) >=
9131da177e4SLinus Torvalds 			p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
9141da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
9151da177e4SLinus Torvalds 				p->user != &root_user)
9161da177e4SLinus Torvalds 			goto bad_fork_free;
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	atomic_inc(&p->user->__count);
9201da177e4SLinus Torvalds 	atomic_inc(&p->user->processes);
9211da177e4SLinus Torvalds 	get_group_info(p->group_info);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	/*
9241da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
9251da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
9261da177e4SLinus Torvalds 	 * to stop root fork bombs.
9271da177e4SLinus Torvalds 	 */
9281da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
9291da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	if (!try_module_get(p->thread_info->exec_domain->module))
9321da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	if (p->binfmt && !try_module_get(p->binfmt->module))
9351da177e4SLinus Torvalds 		goto bad_fork_cleanup_put_domain;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	p->did_exec = 0;
9381da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
9391da177e4SLinus Torvalds 	p->pid = pid;
9401da177e4SLinus Torvalds 	retval = -EFAULT;
9411da177e4SLinus Torvalds 	if (clone_flags & CLONE_PARENT_SETTID)
9421da177e4SLinus Torvalds 		if (put_user(p->pid, parent_tidptr))
9431da177e4SLinus Torvalds 			goto bad_fork_cleanup;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	p->proc_dentry = NULL;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
9481da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
9491da177e4SLinus Torvalds 	p->vfork_done = NULL;
9501da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
9511da177e4SLinus Torvalds 	spin_lock_init(&p->proc_lock);
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SIGPENDING);
9541da177e4SLinus Torvalds 	init_sigpending(&p->pending);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	p->utime = cputime_zero;
9571da177e4SLinus Torvalds 	p->stime = cputime_zero;
9581da177e4SLinus Torvalds  	p->sched_time = 0;
9591da177e4SLinus Torvalds 	p->rchar = 0;		/* I/O counter: bytes read */
9601da177e4SLinus Torvalds 	p->wchar = 0;		/* I/O counter: bytes written */
9611da177e4SLinus Torvalds 	p->syscr = 0;		/* I/O counter: read syscalls */
9621da177e4SLinus Torvalds 	p->syscw = 0;		/* I/O counter: write syscalls */
9631da177e4SLinus Torvalds 	acct_clear_integrals(p);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds  	p->it_virt_expires = cputime_zero;
9661da177e4SLinus Torvalds 	p->it_prof_expires = cputime_zero;
9671da177e4SLinus Torvalds  	p->it_sched_expires = 0;
9681da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[0]);
9691da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[1]);
9701da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[2]);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	p->lock_depth = -1;		/* -1 = no lock */
9731da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
9741da177e4SLinus Torvalds 	p->security = NULL;
9751da177e4SLinus Torvalds 	p->io_context = NULL;
9761da177e4SLinus Torvalds 	p->io_wait = NULL;
9771da177e4SLinus Torvalds 	p->audit_context = NULL;
9781da177e4SLinus Torvalds #ifdef CONFIG_NUMA
9791da177e4SLinus Torvalds  	p->mempolicy = mpol_copy(p->mempolicy);
9801da177e4SLinus Torvalds  	if (IS_ERR(p->mempolicy)) {
9811da177e4SLinus Torvalds  		retval = PTR_ERR(p->mempolicy);
9821da177e4SLinus Torvalds  		p->mempolicy = NULL;
9831da177e4SLinus Torvalds  		goto bad_fork_cleanup;
9841da177e4SLinus Torvalds  	}
9851da177e4SLinus Torvalds #endif
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	p->tgid = p->pid;
9881da177e4SLinus Torvalds 	if (clone_flags & CLONE_THREAD)
9891da177e4SLinus Torvalds 		p->tgid = current->tgid;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	if ((retval = security_task_alloc(p)))
9921da177e4SLinus Torvalds 		goto bad_fork_cleanup_policy;
9931da177e4SLinus Torvalds 	if ((retval = audit_alloc(p)))
9941da177e4SLinus Torvalds 		goto bad_fork_cleanup_security;
9951da177e4SLinus Torvalds 	/* copy all the process information */
9961da177e4SLinus Torvalds 	if ((retval = copy_semundo(clone_flags, p)))
9971da177e4SLinus Torvalds 		goto bad_fork_cleanup_audit;
9981da177e4SLinus Torvalds 	if ((retval = copy_files(clone_flags, p)))
9991da177e4SLinus Torvalds 		goto bad_fork_cleanup_semundo;
10001da177e4SLinus Torvalds 	if ((retval = copy_fs(clone_flags, p)))
10011da177e4SLinus Torvalds 		goto bad_fork_cleanup_files;
10021da177e4SLinus Torvalds 	if ((retval = copy_sighand(clone_flags, p)))
10031da177e4SLinus Torvalds 		goto bad_fork_cleanup_fs;
10041da177e4SLinus Torvalds 	if ((retval = copy_signal(clone_flags, p)))
10051da177e4SLinus Torvalds 		goto bad_fork_cleanup_sighand;
10061da177e4SLinus Torvalds 	if ((retval = copy_mm(clone_flags, p)))
10071da177e4SLinus Torvalds 		goto bad_fork_cleanup_signal;
10081da177e4SLinus Torvalds 	if ((retval = copy_keys(clone_flags, p)))
10091da177e4SLinus Torvalds 		goto bad_fork_cleanup_mm;
10101da177e4SLinus Torvalds 	if ((retval = copy_namespace(clone_flags, p)))
10111da177e4SLinus Torvalds 		goto bad_fork_cleanup_keys;
10121da177e4SLinus Torvalds 	retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
10131da177e4SLinus Torvalds 	if (retval)
10141da177e4SLinus Torvalds 		goto bad_fork_cleanup_namespace;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
10171da177e4SLinus Torvalds 	/*
10181da177e4SLinus Torvalds 	 * Clear TID on mm_release()?
10191da177e4SLinus Torvalds 	 */
10201da177e4SLinus Torvalds 	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	/*
10231da177e4SLinus Torvalds 	 * Syscall tracing should be turned off in the child regardless
10241da177e4SLinus Torvalds 	 * of CLONE_PTRACE.
10251da177e4SLinus Torvalds 	 */
10261da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1027ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU
1028ed75e8d5SLaurent Vivier 	clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1029ed75e8d5SLaurent Vivier #endif
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	/* Our parent execution domain becomes current domain
10321da177e4SLinus Torvalds 	   These must match for thread signalling to apply */
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	p->parent_exec_id = p->self_exec_id;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	/* ok, now we should be set up.. */
10371da177e4SLinus Torvalds 	p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
10381da177e4SLinus Torvalds 	p->pdeath_signal = 0;
10391da177e4SLinus Torvalds 	p->exit_state = 0;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	/*
10421da177e4SLinus Torvalds 	 * Ok, make it visible to the rest of the system.
10431da177e4SLinus Torvalds 	 * We dont wake it up yet.
10441da177e4SLinus Torvalds 	 */
10451da177e4SLinus Torvalds 	p->group_leader = p;
10461da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->ptrace_children);
10471da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->ptrace_list);
10481da177e4SLinus Torvalds 
1049476d139cSNick Piggin 	/* Perform scheduler related setup. Assign this task to a CPU. */
1050476d139cSNick Piggin 	sched_fork(p, clone_flags);
1051476d139cSNick Piggin 
10521da177e4SLinus Torvalds 	/* Need tasklist lock for parent etc handling! */
10531da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	/*
1056476d139cSNick Piggin 	 * The task hasn't been attached yet, so its cpus_allowed mask will
1057476d139cSNick Piggin 	 * not be changed, nor will its assigned CPU.
1058476d139cSNick Piggin 	 *
1059476d139cSNick Piggin 	 * The cpus_allowed mask of the parent may have changed after it was
1060476d139cSNick Piggin 	 * copied first time - so re-copy it here, then check the child's CPU
1061476d139cSNick Piggin 	 * to ensure it is on a valid CPU (and if not, just force it back to
1062476d139cSNick Piggin 	 * parent's CPU). This avoids alot of nasty races.
10631da177e4SLinus Torvalds 	 */
10641da177e4SLinus Torvalds 	p->cpus_allowed = current->cpus_allowed;
106526ff6ad9SSrivatsa Vaddagiri 	if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
106626ff6ad9SSrivatsa Vaddagiri 			!cpu_online(task_cpu(p))))
10671da177e4SLinus Torvalds 		set_task_cpu(p, smp_processor_id());
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	/*
10701da177e4SLinus Torvalds 	 * Check for pending SIGKILL! The new thread should not be allowed
10711da177e4SLinus Torvalds 	 * to slip out of an OOM kill. (or normal SIGKILL.)
10721da177e4SLinus Torvalds 	 */
10731da177e4SLinus Torvalds 	if (sigismember(&current->pending.signal, SIGKILL)) {
10741da177e4SLinus Torvalds 		write_unlock_irq(&tasklist_lock);
10751da177e4SLinus Torvalds 		retval = -EINTR;
10761da177e4SLinus Torvalds 		goto bad_fork_cleanup_namespace;
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	/* CLONE_PARENT re-uses the old parent */
10801da177e4SLinus Torvalds 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
10811da177e4SLinus Torvalds 		p->real_parent = current->real_parent;
10821da177e4SLinus Torvalds 	else
10831da177e4SLinus Torvalds 		p->real_parent = current;
10841da177e4SLinus Torvalds 	p->parent = p->real_parent;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	if (clone_flags & CLONE_THREAD) {
10871da177e4SLinus Torvalds 		spin_lock(&current->sighand->siglock);
10881da177e4SLinus Torvalds 		/*
10891da177e4SLinus Torvalds 		 * Important: if an exit-all has been started then
10901da177e4SLinus Torvalds 		 * do not create this new thread - the whole thread
10911da177e4SLinus Torvalds 		 * group is supposed to exit anyway.
10921da177e4SLinus Torvalds 		 */
10931da177e4SLinus Torvalds 		if (current->signal->flags & SIGNAL_GROUP_EXIT) {
10941da177e4SLinus Torvalds 			spin_unlock(&current->sighand->siglock);
10951da177e4SLinus Torvalds 			write_unlock_irq(&tasklist_lock);
10961da177e4SLinus Torvalds 			retval = -EAGAIN;
10971da177e4SLinus Torvalds 			goto bad_fork_cleanup_namespace;
10981da177e4SLinus Torvalds 		}
10991da177e4SLinus Torvalds 		p->group_leader = current->group_leader;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 		if (current->signal->group_stop_count > 0) {
11021da177e4SLinus Torvalds 			/*
11031da177e4SLinus Torvalds 			 * There is an all-stop in progress for the group.
11041da177e4SLinus Torvalds 			 * We ourselves will stop as soon as we check signals.
11051da177e4SLinus Torvalds 			 * Make the new thread part of that group stop too.
11061da177e4SLinus Torvalds 			 */
11071da177e4SLinus Torvalds 			current->signal->group_stop_count++;
11081da177e4SLinus Torvalds 			set_tsk_thread_flag(p, TIF_SIGPENDING);
11091da177e4SLinus Torvalds 		}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 		if (!cputime_eq(current->signal->it_virt_expires,
11121da177e4SLinus Torvalds 				cputime_zero) ||
11131da177e4SLinus Torvalds 		    !cputime_eq(current->signal->it_prof_expires,
11141da177e4SLinus Torvalds 				cputime_zero) ||
11151da177e4SLinus Torvalds 		    current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
11161da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[0]) ||
11171da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[1]) ||
11181da177e4SLinus Torvalds 		    !list_empty(&current->signal->cpu_timers[2])) {
11191da177e4SLinus Torvalds 			/*
11201da177e4SLinus Torvalds 			 * Have child wake up on its first tick to check
11211da177e4SLinus Torvalds 			 * for process CPU timers.
11221da177e4SLinus Torvalds 			 */
11231da177e4SLinus Torvalds 			p->it_prof_expires = jiffies_to_cputime(1);
11241da177e4SLinus Torvalds 		}
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 		spin_unlock(&current->sighand->siglock);
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 
112922e2c507SJens Axboe 	/*
113022e2c507SJens Axboe 	 * inherit ioprio
113122e2c507SJens Axboe 	 */
113222e2c507SJens Axboe 	p->ioprio = current->ioprio;
113322e2c507SJens Axboe 
11341da177e4SLinus Torvalds 	SET_LINKS(p);
11351da177e4SLinus Torvalds 	if (unlikely(p->ptrace & PT_PTRACED))
11361da177e4SLinus Torvalds 		__ptrace_link(p, current->parent);
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	cpuset_fork(p);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	attach_pid(p, PIDTYPE_PID, p->pid);
11411da177e4SLinus Torvalds 	attach_pid(p, PIDTYPE_TGID, p->tgid);
11421da177e4SLinus Torvalds 	if (thread_group_leader(p)) {
11431da177e4SLinus Torvalds 		attach_pid(p, PIDTYPE_PGID, process_group(p));
11441da177e4SLinus Torvalds 		attach_pid(p, PIDTYPE_SID, p->signal->session);
11451da177e4SLinus Torvalds 		if (p->pid)
11461da177e4SLinus Torvalds 			__get_cpu_var(process_counts)++;
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 
1149b0d62e6dSJason Baron 	if (!current->signal->tty && p->signal->tty)
1150b0d62e6dSJason Baron 		p->signal->tty = NULL;
1151b0d62e6dSJason Baron 
11521da177e4SLinus Torvalds 	nr_threads++;
11531da177e4SLinus Torvalds 	total_forks++;
11541da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
11551da177e4SLinus Torvalds 	retval = 0;
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds fork_out:
11581da177e4SLinus Torvalds 	if (retval)
11591da177e4SLinus Torvalds 		return ERR_PTR(retval);
11601da177e4SLinus Torvalds 	return p;
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds bad_fork_cleanup_namespace:
11631da177e4SLinus Torvalds 	exit_namespace(p);
11641da177e4SLinus Torvalds bad_fork_cleanup_keys:
11651da177e4SLinus Torvalds 	exit_keys(p);
11661da177e4SLinus Torvalds bad_fork_cleanup_mm:
11671da177e4SLinus Torvalds 	if (p->mm)
11681da177e4SLinus Torvalds 		mmput(p->mm);
11691da177e4SLinus Torvalds bad_fork_cleanup_signal:
11701da177e4SLinus Torvalds 	exit_signal(p);
11711da177e4SLinus Torvalds bad_fork_cleanup_sighand:
11721da177e4SLinus Torvalds 	exit_sighand(p);
11731da177e4SLinus Torvalds bad_fork_cleanup_fs:
11741da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
11751da177e4SLinus Torvalds bad_fork_cleanup_files:
11761da177e4SLinus Torvalds 	exit_files(p); /* blocking */
11771da177e4SLinus Torvalds bad_fork_cleanup_semundo:
11781da177e4SLinus Torvalds 	exit_sem(p);
11791da177e4SLinus Torvalds bad_fork_cleanup_audit:
11801da177e4SLinus Torvalds 	audit_free(p);
11811da177e4SLinus Torvalds bad_fork_cleanup_security:
11821da177e4SLinus Torvalds 	security_task_free(p);
11831da177e4SLinus Torvalds bad_fork_cleanup_policy:
11841da177e4SLinus Torvalds #ifdef CONFIG_NUMA
11851da177e4SLinus Torvalds 	mpol_free(p->mempolicy);
11861da177e4SLinus Torvalds #endif
11871da177e4SLinus Torvalds bad_fork_cleanup:
11881da177e4SLinus Torvalds 	if (p->binfmt)
11891da177e4SLinus Torvalds 		module_put(p->binfmt->module);
11901da177e4SLinus Torvalds bad_fork_cleanup_put_domain:
11911da177e4SLinus Torvalds 	module_put(p->thread_info->exec_domain->module);
11921da177e4SLinus Torvalds bad_fork_cleanup_count:
11931da177e4SLinus Torvalds 	put_group_info(p->group_info);
11941da177e4SLinus Torvalds 	atomic_dec(&p->user->processes);
11951da177e4SLinus Torvalds 	free_uid(p->user);
11961da177e4SLinus Torvalds bad_fork_free:
11971da177e4SLinus Torvalds 	free_task(p);
11981da177e4SLinus Torvalds 	goto fork_out;
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
12021da177e4SLinus Torvalds {
12031da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
12041da177e4SLinus Torvalds 	return regs;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds task_t * __devinit fork_idle(int cpu)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	task_t *task;
12101da177e4SLinus Torvalds 	struct pt_regs regs;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0);
12131da177e4SLinus Torvalds 	if (!task)
12141da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
12151da177e4SLinus Torvalds 	init_idle(task, cpu);
12161da177e4SLinus Torvalds 	unhash_process(task);
12171da177e4SLinus Torvalds 	return task;
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds static inline int fork_traceflag (unsigned clone_flags)
12211da177e4SLinus Torvalds {
12221da177e4SLinus Torvalds 	if (clone_flags & CLONE_UNTRACED)
12231da177e4SLinus Torvalds 		return 0;
12241da177e4SLinus Torvalds 	else if (clone_flags & CLONE_VFORK) {
12251da177e4SLinus Torvalds 		if (current->ptrace & PT_TRACE_VFORK)
12261da177e4SLinus Torvalds 			return PTRACE_EVENT_VFORK;
12271da177e4SLinus Torvalds 	} else if ((clone_flags & CSIGNAL) != SIGCHLD) {
12281da177e4SLinus Torvalds 		if (current->ptrace & PT_TRACE_CLONE)
12291da177e4SLinus Torvalds 			return PTRACE_EVENT_CLONE;
12301da177e4SLinus Torvalds 	} else if (current->ptrace & PT_TRACE_FORK)
12311da177e4SLinus Torvalds 		return PTRACE_EVENT_FORK;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	return 0;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds /*
12371da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
12381da177e4SLinus Torvalds  *
12391da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
12401da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
12411da177e4SLinus Torvalds  */
12421da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
12431da177e4SLinus Torvalds 	      unsigned long stack_start,
12441da177e4SLinus Torvalds 	      struct pt_regs *regs,
12451da177e4SLinus Torvalds 	      unsigned long stack_size,
12461da177e4SLinus Torvalds 	      int __user *parent_tidptr,
12471da177e4SLinus Torvalds 	      int __user *child_tidptr)
12481da177e4SLinus Torvalds {
12491da177e4SLinus Torvalds 	struct task_struct *p;
12501da177e4SLinus Torvalds 	int trace = 0;
12511da177e4SLinus Torvalds 	long pid = alloc_pidmap();
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	if (pid < 0)
12541da177e4SLinus Torvalds 		return -EAGAIN;
12551da177e4SLinus Torvalds 	if (unlikely(current->ptrace)) {
12561da177e4SLinus Torvalds 		trace = fork_traceflag (clone_flags);
12571da177e4SLinus Torvalds 		if (trace)
12581da177e4SLinus Torvalds 			clone_flags |= CLONE_PTRACE;
12591da177e4SLinus Torvalds 	}
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
12621da177e4SLinus Torvalds 	/*
12631da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
12641da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
12651da177e4SLinus Torvalds 	 */
12661da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
12671da177e4SLinus Torvalds 		struct completion vfork;
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
12701da177e4SLinus Torvalds 			p->vfork_done = &vfork;
12711da177e4SLinus Torvalds 			init_completion(&vfork);
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 		if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
12751da177e4SLinus Torvalds 			/*
12761da177e4SLinus Torvalds 			 * We'll start up with an immediate SIGSTOP.
12771da177e4SLinus Torvalds 			 */
12781da177e4SLinus Torvalds 			sigaddset(&p->pending.signal, SIGSTOP);
12791da177e4SLinus Torvalds 			set_tsk_thread_flag(p, TIF_SIGPENDING);
12801da177e4SLinus Torvalds 		}
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 		if (!(clone_flags & CLONE_STOPPED))
12831da177e4SLinus Torvalds 			wake_up_new_task(p, clone_flags);
12841da177e4SLinus Torvalds 		else
12851da177e4SLinus Torvalds 			p->state = TASK_STOPPED;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 		if (unlikely (trace)) {
12881da177e4SLinus Torvalds 			current->ptrace_message = pid;
12891da177e4SLinus Torvalds 			ptrace_notify ((trace << 8) | SIGTRAP);
12901da177e4SLinus Torvalds 		}
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
12931da177e4SLinus Torvalds 			wait_for_completion(&vfork);
12941da177e4SLinus Torvalds 			if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
12951da177e4SLinus Torvalds 				ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
12961da177e4SLinus Torvalds 		}
12971da177e4SLinus Torvalds 	} else {
12981da177e4SLinus Torvalds 		free_pidmap(pid);
12991da177e4SLinus Torvalds 		pid = PTR_ERR(p);
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 	return pid;
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds void __init proc_caches_init(void)
13051da177e4SLinus Torvalds {
13061da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
13071da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
13081da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13091da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
13101da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
13111da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13121da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
13131da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
13141da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13151da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
13161da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
13171da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13181da177e4SLinus Torvalds 	vm_area_cachep = kmem_cache_create("vm_area_struct",
13191da177e4SLinus Torvalds 			sizeof(struct vm_area_struct), 0,
13201da177e4SLinus Torvalds 			SLAB_PANIC, NULL, NULL);
13211da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
13221da177e4SLinus Torvalds 			sizeof(struct mm_struct), 0,
13231da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13241da177e4SLinus Torvalds }
1325