xref: /openbmc/linux/kernel/fork.c (revision c59ede7b78db329949d9cdcd7064e22d357560ef)
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>
31*c59ede7bSRandy.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) */
871da177e4SLinus Torvalds 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 
1111da177e4SLinus Torvalds void __put_task_struct(struct task_struct *tsk)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
1141da177e4SLinus Torvalds 	WARN_ON(atomic_read(&tsk->usage));
1151da177e4SLinus Torvalds 	WARN_ON(tsk == current);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (unlikely(tsk->audit_context))
1181da177e4SLinus Torvalds 		audit_free(tsk);
1191da177e4SLinus Torvalds 	security_task_free(tsk);
1201da177e4SLinus Torvalds 	free_uid(tsk->user);
1211da177e4SLinus Torvalds 	put_group_info(tsk->group_info);
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (!profile_handoff_task(tsk))
1241da177e4SLinus Torvalds 		free_task(tsk);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds void __init fork_init(unsigned long mempages)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
1301da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN
1311da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN	L1_CACHE_BYTES
1321da177e4SLinus Torvalds #endif
1331da177e4SLinus Torvalds 	/* create a slab on which task_structs can be allocated */
1341da177e4SLinus Torvalds 	task_struct_cachep =
1351da177e4SLinus Torvalds 		kmem_cache_create("task_struct", sizeof(struct task_struct),
1361da177e4SLinus Torvalds 			ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
1371da177e4SLinus Torvalds #endif
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/*
1401da177e4SLinus Torvalds 	 * The default maximum number of threads is set to a safe
1411da177e4SLinus Torvalds 	 * value: the thread structures can take up at most half
1421da177e4SLinus Torvalds 	 * of memory.
1431da177e4SLinus Torvalds 	 */
1441da177e4SLinus Torvalds 	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/*
1471da177e4SLinus Torvalds 	 * we need to allow at least 20 threads to boot a system
1481da177e4SLinus Torvalds 	 */
1491da177e4SLinus Torvalds 	if(max_threads < 20)
1501da177e4SLinus Torvalds 		max_threads = 20;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
1531da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
1541da177e4SLinus Torvalds 	init_task.signal->rlim[RLIMIT_SIGPENDING] =
1551da177e4SLinus Torvalds 		init_task.signal->rlim[RLIMIT_NPROC];
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	struct task_struct *tsk;
1611da177e4SLinus Torvalds 	struct thread_info *ti;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	prepare_to_copy(orig);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	tsk = alloc_task_struct();
1661da177e4SLinus Torvalds 	if (!tsk)
1671da177e4SLinus Torvalds 		return NULL;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	ti = alloc_thread_info(tsk);
1701da177e4SLinus Torvalds 	if (!ti) {
1711da177e4SLinus Torvalds 		free_task_struct(tsk);
1721da177e4SLinus Torvalds 		return NULL;
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	*tsk = *orig;
1761da177e4SLinus Torvalds 	tsk->thread_info = ti;
17710ebffdeSAl Viro 	setup_thread_stack(tsk, orig);
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* One for us, one for whoever does the "release_task()" (usually parent) */
1801da177e4SLinus Torvalds 	atomic_set(&tsk->usage,2);
1814b5d37acSGiancarlo Formicuccia 	atomic_set(&tsk->fs_excl, 0);
1821da177e4SLinus Torvalds 	return tsk;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds #ifdef CONFIG_MMU
1861da177e4SLinus Torvalds static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	struct vm_area_struct *mpnt, *tmp, **pprev;
1891da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
1901da177e4SLinus Torvalds 	int retval;
1911da177e4SLinus Torvalds 	unsigned long charge;
1921da177e4SLinus Torvalds 	struct mempolicy *pol;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	down_write(&oldmm->mmap_sem);
195fd3e42fcSHugh Dickins 	flush_cache_mm(oldmm);
1967ee78232SHugh Dickins 	down_write(&mm->mmap_sem);
1977ee78232SHugh Dickins 
1981da177e4SLinus Torvalds 	mm->locked_vm = 0;
1991da177e4SLinus Torvalds 	mm->mmap = NULL;
2001da177e4SLinus Torvalds 	mm->mmap_cache = NULL;
2011da177e4SLinus Torvalds 	mm->free_area_cache = oldmm->mmap_base;
2021363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
2031da177e4SLinus Torvalds 	mm->map_count = 0;
2041da177e4SLinus Torvalds 	cpus_clear(mm->cpu_vm_mask);
2051da177e4SLinus Torvalds 	mm->mm_rb = RB_ROOT;
2061da177e4SLinus Torvalds 	rb_link = &mm->mm_rb.rb_node;
2071da177e4SLinus Torvalds 	rb_parent = NULL;
2081da177e4SLinus Torvalds 	pprev = &mm->mmap;
2091da177e4SLinus Torvalds 
210fd3e42fcSHugh Dickins 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
2111da177e4SLinus Torvalds 		struct file *file;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_DONTCOPY) {
2143b6bfcdbSHugh Dickins 			long pages = vma_pages(mpnt);
2153b6bfcdbSHugh Dickins 			mm->total_vm -= pages;
216ab50b8edSHugh Dickins 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
2173b6bfcdbSHugh Dickins 								-pages);
2181da177e4SLinus Torvalds 			continue;
2191da177e4SLinus Torvalds 		}
2201da177e4SLinus Torvalds 		charge = 0;
2211da177e4SLinus Torvalds 		if (mpnt->vm_flags & VM_ACCOUNT) {
2221da177e4SLinus Torvalds 			unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
2231da177e4SLinus Torvalds 			if (security_vm_enough_memory(len))
2241da177e4SLinus Torvalds 				goto fail_nomem;
2251da177e4SLinus Torvalds 			charge = len;
2261da177e4SLinus Torvalds 		}
2271da177e4SLinus Torvalds 		tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2281da177e4SLinus Torvalds 		if (!tmp)
2291da177e4SLinus Torvalds 			goto fail_nomem;
2301da177e4SLinus Torvalds 		*tmp = *mpnt;
2311da177e4SLinus Torvalds 		pol = mpol_copy(vma_policy(mpnt));
2321da177e4SLinus Torvalds 		retval = PTR_ERR(pol);
2331da177e4SLinus Torvalds 		if (IS_ERR(pol))
2341da177e4SLinus Torvalds 			goto fail_nomem_policy;
2351da177e4SLinus Torvalds 		vma_set_policy(tmp, pol);
2361da177e4SLinus Torvalds 		tmp->vm_flags &= ~VM_LOCKED;
2371da177e4SLinus Torvalds 		tmp->vm_mm = mm;
2381da177e4SLinus Torvalds 		tmp->vm_next = NULL;
2391da177e4SLinus Torvalds 		anon_vma_link(tmp);
2401da177e4SLinus Torvalds 		file = tmp->vm_file;
2411da177e4SLinus Torvalds 		if (file) {
2421da177e4SLinus Torvalds 			struct inode *inode = file->f_dentry->d_inode;
2431da177e4SLinus Torvalds 			get_file(file);
2441da177e4SLinus Torvalds 			if (tmp->vm_flags & VM_DENYWRITE)
2451da177e4SLinus Torvalds 				atomic_dec(&inode->i_writecount);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 			/* insert tmp into the share list, just after mpnt */
2481da177e4SLinus Torvalds 			spin_lock(&file->f_mapping->i_mmap_lock);
2491da177e4SLinus Torvalds 			tmp->vm_truncate_count = mpnt->vm_truncate_count;
2501da177e4SLinus Torvalds 			flush_dcache_mmap_lock(file->f_mapping);
2511da177e4SLinus Torvalds 			vma_prio_tree_add(tmp, mpnt);
2521da177e4SLinus Torvalds 			flush_dcache_mmap_unlock(file->f_mapping);
2531da177e4SLinus Torvalds 			spin_unlock(&file->f_mapping->i_mmap_lock);
2541da177e4SLinus Torvalds 		}
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 		/*
2577ee78232SHugh Dickins 		 * Link in the new vma and copy the page table entries.
2581da177e4SLinus Torvalds 		 */
2591da177e4SLinus Torvalds 		*pprev = tmp;
2601da177e4SLinus Torvalds 		pprev = &tmp->vm_next;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 		__vma_link_rb(mm, tmp, rb_link, rb_parent);
2631da177e4SLinus Torvalds 		rb_link = &tmp->vm_rb.rb_right;
2641da177e4SLinus Torvalds 		rb_parent = &tmp->vm_rb;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 		mm->map_count++;
2670b0db14cSHugh Dickins 		retval = copy_page_range(mm, oldmm, mpnt);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		if (tmp->vm_ops && tmp->vm_ops->open)
2701da177e4SLinus Torvalds 			tmp->vm_ops->open(tmp);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 		if (retval)
2731da177e4SLinus Torvalds 			goto out;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	retval = 0;
2761da177e4SLinus Torvalds out:
2777ee78232SHugh Dickins 	up_write(&mm->mmap_sem);
278fd3e42fcSHugh Dickins 	flush_tlb_mm(oldmm);
2791da177e4SLinus Torvalds 	up_write(&oldmm->mmap_sem);
2801da177e4SLinus Torvalds 	return retval;
2811da177e4SLinus Torvalds fail_nomem_policy:
2821da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, tmp);
2831da177e4SLinus Torvalds fail_nomem:
2841da177e4SLinus Torvalds 	retval = -ENOMEM;
2851da177e4SLinus Torvalds 	vm_unacct_memory(charge);
2861da177e4SLinus Torvalds 	goto out;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	mm->pgd = pgd_alloc(mm);
2921da177e4SLinus Torvalds 	if (unlikely(!mm->pgd))
2931da177e4SLinus Torvalds 		return -ENOMEM;
2941da177e4SLinus Torvalds 	return 0;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	pgd_free(mm->pgd);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds #else
3021da177e4SLinus Torvalds #define dup_mmap(mm, oldmm)	(0)
3031da177e4SLinus Torvalds #define mm_alloc_pgd(mm)	(0)
3041da177e4SLinus Torvalds #define mm_free_pgd(mm)
3051da177e4SLinus Torvalds #endif /* CONFIG_MMU */
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds  __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds #define allocate_mm()	(kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
3101da177e4SLinus Torvalds #define free_mm(mm)	(kmem_cache_free(mm_cachep, (mm)))
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds #include <linux/init_task.h>
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds static struct mm_struct * mm_init(struct mm_struct * mm)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	atomic_set(&mm->mm_users, 1);
3171da177e4SLinus Torvalds 	atomic_set(&mm->mm_count, 1);
3181da177e4SLinus Torvalds 	init_rwsem(&mm->mmap_sem);
3191da177e4SLinus Torvalds 	INIT_LIST_HEAD(&mm->mmlist);
3201da177e4SLinus Torvalds 	mm->core_waiters = 0;
3211da177e4SLinus Torvalds 	mm->nr_ptes = 0;
3224294621fSHugh Dickins 	set_mm_counter(mm, file_rss, 0);
323404351e6SHugh Dickins 	set_mm_counter(mm, anon_rss, 0);
3241da177e4SLinus Torvalds 	spin_lock_init(&mm->page_table_lock);
3251da177e4SLinus Torvalds 	rwlock_init(&mm->ioctx_list_lock);
3261da177e4SLinus Torvalds 	mm->ioctx_list = NULL;
3271da177e4SLinus Torvalds 	mm->free_area_cache = TASK_UNMAPPED_BASE;
3281363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	if (likely(!mm_alloc_pgd(mm))) {
3311da177e4SLinus Torvalds 		mm->def_flags = 0;
3321da177e4SLinus Torvalds 		return mm;
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 	free_mm(mm);
3351da177e4SLinus Torvalds 	return NULL;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  * Allocate and initialize an mm_struct.
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds struct mm_struct * mm_alloc(void)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	struct mm_struct * mm;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	mm = allocate_mm();
3461da177e4SLinus Torvalds 	if (mm) {
3471da177e4SLinus Torvalds 		memset(mm, 0, sizeof(*mm));
3481da177e4SLinus Torvalds 		mm = mm_init(mm);
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 	return mm;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  * Called when the last reference to the mm
3551da177e4SLinus Torvalds  * is dropped: either by a lazy thread or by
3561da177e4SLinus Torvalds  * mmput. Free the page directory and the mm.
3571da177e4SLinus Torvalds  */
3581da177e4SLinus Torvalds void fastcall __mmdrop(struct mm_struct *mm)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	BUG_ON(mm == &init_mm);
3611da177e4SLinus Torvalds 	mm_free_pgd(mm);
3621da177e4SLinus Torvalds 	destroy_context(mm);
3631da177e4SLinus Torvalds 	free_mm(mm);
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds /*
3671da177e4SLinus Torvalds  * Decrement the use count and release all resources for an mm.
3681da177e4SLinus Torvalds  */
3691da177e4SLinus Torvalds void mmput(struct mm_struct *mm)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	if (atomic_dec_and_test(&mm->mm_users)) {
3721da177e4SLinus Torvalds 		exit_aio(mm);
3731da177e4SLinus Torvalds 		exit_mmap(mm);
3741da177e4SLinus Torvalds 		if (!list_empty(&mm->mmlist)) {
3751da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
3761da177e4SLinus Torvalds 			list_del(&mm->mmlist);
3771da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 		put_swap_token(mm);
3801da177e4SLinus Torvalds 		mmdrop(mm);
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /**
3861da177e4SLinus Torvalds  * get_task_mm - acquire a reference to the task's mm
3871da177e4SLinus Torvalds  *
3881da177e4SLinus Torvalds  * Returns %NULL if the task has no mm.  Checks PF_BORROWED_MM (meaning
3891da177e4SLinus Torvalds  * this kernel workthread has transiently adopted a user mm with use_mm,
3901da177e4SLinus Torvalds  * to do its AIO) is not set and if so returns a reference to it, after
3911da177e4SLinus Torvalds  * bumping up the use count.  User must release the mm via mmput()
3921da177e4SLinus Torvalds  * after use.  Typically used by /proc and ptrace.
3931da177e4SLinus Torvalds  */
3941da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	struct mm_struct *mm;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	task_lock(task);
3991da177e4SLinus Torvalds 	mm = task->mm;
4001da177e4SLinus Torvalds 	if (mm) {
4011da177e4SLinus Torvalds 		if (task->flags & PF_BORROWED_MM)
4021da177e4SLinus Torvalds 			mm = NULL;
4031da177e4SLinus Torvalds 		else
4041da177e4SLinus Torvalds 			atomic_inc(&mm->mm_users);
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 	task_unlock(task);
4071da177e4SLinus Torvalds 	return mm;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release.
4121da177e4SLinus Torvalds  * mmput is called whenever we stop holding onto a mm_struct,
4131da177e4SLinus Torvalds  * error success whatever.
4141da177e4SLinus Torvalds  *
4151da177e4SLinus Torvalds  * mm_release is called after a mm_struct has been removed
4161da177e4SLinus Torvalds  * from the current process.
4171da177e4SLinus Torvalds  *
4181da177e4SLinus Torvalds  * This difference is important for error handling, when we
4191da177e4SLinus Torvalds  * only half set up a mm_struct for a new process and need to restore
4201da177e4SLinus Torvalds  * the old one.  Because we mmput the new mm_struct before
4211da177e4SLinus Torvalds  * restoring the old one. . .
4221da177e4SLinus Torvalds  * Eric Biederman 10 January 1998
4231da177e4SLinus Torvalds  */
4241da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	struct completion *vfork_done = tsk->vfork_done;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/* Get rid of any cached register state */
4291da177e4SLinus Torvalds 	deactivate_mm(tsk, mm);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	/* notify parent sleeping on vfork() */
4321da177e4SLinus Torvalds 	if (vfork_done) {
4331da177e4SLinus Torvalds 		tsk->vfork_done = NULL;
4341da177e4SLinus Torvalds 		complete(vfork_done);
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 	if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
4371da177e4SLinus Torvalds 		u32 __user * tidptr = tsk->clear_child_tid;
4381da177e4SLinus Torvalds 		tsk->clear_child_tid = NULL;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 		/*
4411da177e4SLinus Torvalds 		 * We don't check the error code - if userspace has
4421da177e4SLinus Torvalds 		 * not set up a proper pointer then tough luck.
4431da177e4SLinus Torvalds 		 */
4441da177e4SLinus Torvalds 		put_user(0, tidptr);
4451da177e4SLinus Torvalds 		sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
4461da177e4SLinus Torvalds 	}
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct mm_struct * mm, *oldmm;
4521da177e4SLinus Torvalds 	int retval;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	tsk->min_flt = tsk->maj_flt = 0;
4551da177e4SLinus Torvalds 	tsk->nvcsw = tsk->nivcsw = 0;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	tsk->mm = NULL;
4581da177e4SLinus Torvalds 	tsk->active_mm = NULL;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	/*
4611da177e4SLinus Torvalds 	 * Are we cloning a kernel thread?
4621da177e4SLinus Torvalds 	 *
4631da177e4SLinus Torvalds 	 * We need to steal a active VM for that..
4641da177e4SLinus Torvalds 	 */
4651da177e4SLinus Torvalds 	oldmm = current->mm;
4661da177e4SLinus Torvalds 	if (!oldmm)
4671da177e4SLinus Torvalds 		return 0;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	if (clone_flags & CLONE_VM) {
4701da177e4SLinus Torvalds 		atomic_inc(&oldmm->mm_users);
4711da177e4SLinus Torvalds 		mm = oldmm;
4721da177e4SLinus Torvalds 		goto good_mm;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	retval = -ENOMEM;
4761da177e4SLinus Torvalds 	mm = allocate_mm();
4771da177e4SLinus Torvalds 	if (!mm)
4781da177e4SLinus Torvalds 		goto fail_nomem;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	/* Copy the current MM stuff.. */
4811da177e4SLinus Torvalds 	memcpy(mm, oldmm, sizeof(*mm));
4821da177e4SLinus Torvalds 	if (!mm_init(mm))
4831da177e4SLinus Torvalds 		goto fail_nomem;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	if (init_new_context(tsk,mm))
4861da177e4SLinus Torvalds 		goto fail_nocontext;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	retval = dup_mmap(mm, oldmm);
4891da177e4SLinus Torvalds 	if (retval)
4901da177e4SLinus Torvalds 		goto free_pt;
4911da177e4SLinus Torvalds 
4924294621fSHugh Dickins 	mm->hiwater_rss = get_mm_rss(mm);
4931da177e4SLinus Torvalds 	mm->hiwater_vm = mm->total_vm;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds good_mm:
4961da177e4SLinus Torvalds 	tsk->mm = mm;
4971da177e4SLinus Torvalds 	tsk->active_mm = mm;
4981da177e4SLinus Torvalds 	return 0;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds free_pt:
5011da177e4SLinus Torvalds 	mmput(mm);
5021da177e4SLinus Torvalds fail_nomem:
5031da177e4SLinus Torvalds 	return retval;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds fail_nocontext:
5061da177e4SLinus Torvalds 	/*
5071da177e4SLinus Torvalds 	 * If init_new_context() failed, we cannot use mmput() to free the mm
5081da177e4SLinus Torvalds 	 * because it calls destroy_context()
5091da177e4SLinus Torvalds 	 */
5101da177e4SLinus Torvalds 	mm_free_pgd(mm);
5111da177e4SLinus Torvalds 	free_mm(mm);
5121da177e4SLinus Torvalds 	return retval;
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
5181da177e4SLinus Torvalds 	/* We don't need to lock fs - think why ;-) */
5191da177e4SLinus Torvalds 	if (fs) {
5201da177e4SLinus Torvalds 		atomic_set(&fs->count, 1);
5211da177e4SLinus Torvalds 		rwlock_init(&fs->lock);
5221da177e4SLinus Torvalds 		fs->umask = old->umask;
5231da177e4SLinus Torvalds 		read_lock(&old->lock);
5241da177e4SLinus Torvalds 		fs->rootmnt = mntget(old->rootmnt);
5251da177e4SLinus Torvalds 		fs->root = dget(old->root);
5261da177e4SLinus Torvalds 		fs->pwdmnt = mntget(old->pwdmnt);
5271da177e4SLinus Torvalds 		fs->pwd = dget(old->pwd);
5281da177e4SLinus Torvalds 		if (old->altroot) {
5291da177e4SLinus Torvalds 			fs->altrootmnt = mntget(old->altrootmnt);
5301da177e4SLinus Torvalds 			fs->altroot = dget(old->altroot);
5311da177e4SLinus Torvalds 		} else {
5321da177e4SLinus Torvalds 			fs->altrootmnt = NULL;
5331da177e4SLinus Torvalds 			fs->altroot = NULL;
5341da177e4SLinus Torvalds 		}
5351da177e4SLinus Torvalds 		read_unlock(&old->lock);
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 	return fs;
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds struct fs_struct *copy_fs_struct(struct fs_struct *old)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	return __copy_fs_struct(old);
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(copy_fs_struct);
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	if (clone_flags & CLONE_FS) {
5501da177e4SLinus Torvalds 		atomic_inc(&current->fs->count);
5511da177e4SLinus Torvalds 		return 0;
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds 	tsk->fs = __copy_fs_struct(current->fs);
5541da177e4SLinus Torvalds 	if (!tsk->fs)
5551da177e4SLinus Torvalds 		return -ENOMEM;
5561da177e4SLinus Torvalds 	return 0;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
559ab2af1f5SDipankar Sarma static int count_open_files(struct fdtable *fdt)
5601da177e4SLinus Torvalds {
561ab2af1f5SDipankar Sarma 	int size = fdt->max_fdset;
5621da177e4SLinus Torvalds 	int i;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	/* Find the last open fd */
5651da177e4SLinus Torvalds 	for (i = size/(8*sizeof(long)); i > 0; ) {
566badf1662SDipankar Sarma 		if (fdt->open_fds->fds_bits[--i])
5671da177e4SLinus Torvalds 			break;
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 	i = (i+1) * 8 * sizeof(long);
5701da177e4SLinus Torvalds 	return i;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
573badf1662SDipankar Sarma static struct files_struct *alloc_files(void)
574badf1662SDipankar Sarma {
575badf1662SDipankar Sarma 	struct files_struct *newf;
576badf1662SDipankar Sarma 	struct fdtable *fdt;
577badf1662SDipankar Sarma 
578badf1662SDipankar Sarma 	newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
579badf1662SDipankar Sarma 	if (!newf)
580badf1662SDipankar Sarma 		goto out;
581badf1662SDipankar Sarma 
582badf1662SDipankar Sarma 	atomic_set(&newf->count, 1);
583badf1662SDipankar Sarma 
584badf1662SDipankar Sarma 	spin_lock_init(&newf->file_lock);
585ab2af1f5SDipankar Sarma 	fdt = &newf->fdtab;
586badf1662SDipankar Sarma 	fdt->next_fd = 0;
587badf1662SDipankar Sarma 	fdt->max_fds = NR_OPEN_DEFAULT;
588badf1662SDipankar Sarma 	fdt->max_fdset = __FD_SETSIZE;
589badf1662SDipankar Sarma 	fdt->close_on_exec = &newf->close_on_exec_init;
590badf1662SDipankar Sarma 	fdt->open_fds = &newf->open_fds_init;
591badf1662SDipankar Sarma 	fdt->fd = &newf->fd_array[0];
592ab2af1f5SDipankar Sarma 	INIT_RCU_HEAD(&fdt->rcu);
593ab2af1f5SDipankar Sarma 	fdt->free_files = NULL;
594ab2af1f5SDipankar Sarma 	fdt->next = NULL;
595ab2af1f5SDipankar Sarma 	rcu_assign_pointer(newf->fdt, fdt);
596badf1662SDipankar Sarma out:
597badf1662SDipankar Sarma 	return newf;
598badf1662SDipankar Sarma }
599badf1662SDipankar Sarma 
6001da177e4SLinus Torvalds static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	struct files_struct *oldf, *newf;
6031da177e4SLinus Torvalds 	struct file **old_fds, **new_fds;
6041da177e4SLinus Torvalds 	int open_files, size, i, error = 0, expand;
605badf1662SDipankar Sarma 	struct fdtable *old_fdt, *new_fdt;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/*
6081da177e4SLinus Torvalds 	 * A background process may not have any files ...
6091da177e4SLinus Torvalds 	 */
6101da177e4SLinus Torvalds 	oldf = current->files;
6111da177e4SLinus Torvalds 	if (!oldf)
6121da177e4SLinus Torvalds 		goto out;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	if (clone_flags & CLONE_FILES) {
6151da177e4SLinus Torvalds 		atomic_inc(&oldf->count);
6161da177e4SLinus Torvalds 		goto out;
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	/*
6201da177e4SLinus Torvalds 	 * Note: we may be using current for both targets (See exec.c)
6211da177e4SLinus Torvalds 	 * This works because we cache current->files (old) as oldf. Don't
6221da177e4SLinus Torvalds 	 * break this.
6231da177e4SLinus Torvalds 	 */
6241da177e4SLinus Torvalds 	tsk->files = NULL;
6251da177e4SLinus Torvalds 	error = -ENOMEM;
626badf1662SDipankar Sarma 	newf = alloc_files();
6271da177e4SLinus Torvalds 	if (!newf)
6281da177e4SLinus Torvalds 		goto out;
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	spin_lock(&oldf->file_lock);
631badf1662SDipankar Sarma 	old_fdt = files_fdtable(oldf);
632badf1662SDipankar Sarma 	new_fdt = files_fdtable(newf);
633badf1662SDipankar Sarma 	size = old_fdt->max_fdset;
634ab2af1f5SDipankar Sarma 	open_files = count_open_files(old_fdt);
6351da177e4SLinus Torvalds 	expand = 0;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	/*
6381da177e4SLinus Torvalds 	 * Check whether we need to allocate a larger fd array or fd set.
6391da177e4SLinus Torvalds 	 * Note: we're not a clone task, so the open count won't  change.
6401da177e4SLinus Torvalds 	 */
641badf1662SDipankar Sarma 	if (open_files > new_fdt->max_fdset) {
642badf1662SDipankar Sarma 		new_fdt->max_fdset = 0;
6431da177e4SLinus Torvalds 		expand = 1;
6441da177e4SLinus Torvalds 	}
645badf1662SDipankar Sarma 	if (open_files > new_fdt->max_fds) {
646badf1662SDipankar Sarma 		new_fdt->max_fds = 0;
6471da177e4SLinus Torvalds 		expand = 1;
6481da177e4SLinus Torvalds 	}
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/* if the old fdset gets grown now, we'll only copy up to "size" fds */
6511da177e4SLinus Torvalds 	if (expand) {
6521da177e4SLinus Torvalds 		spin_unlock(&oldf->file_lock);
6531da177e4SLinus Torvalds 		spin_lock(&newf->file_lock);
6541da177e4SLinus Torvalds 		error = expand_files(newf, open_files-1);
6551da177e4SLinus Torvalds 		spin_unlock(&newf->file_lock);
6561da177e4SLinus Torvalds 		if (error < 0)
6571da177e4SLinus Torvalds 			goto out_release;
658ab2af1f5SDipankar Sarma 		new_fdt = files_fdtable(newf);
659ab2af1f5SDipankar Sarma 		/*
660ab2af1f5SDipankar Sarma 		 * Reacquire the oldf lock and a pointer to its fd table
661ab2af1f5SDipankar Sarma 		 * who knows it may have a new bigger fd table. We need
662ab2af1f5SDipankar Sarma 		 * the latest pointer.
663ab2af1f5SDipankar Sarma 		 */
6641da177e4SLinus Torvalds 		spin_lock(&oldf->file_lock);
665ab2af1f5SDipankar Sarma 		old_fdt = files_fdtable(oldf);
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
668badf1662SDipankar Sarma 	old_fds = old_fdt->fd;
669badf1662SDipankar Sarma 	new_fds = new_fdt->fd;
6701da177e4SLinus Torvalds 
671badf1662SDipankar Sarma 	memcpy(new_fdt->open_fds->fds_bits, old_fdt->open_fds->fds_bits, open_files/8);
672badf1662SDipankar Sarma 	memcpy(new_fdt->close_on_exec->fds_bits, old_fdt->close_on_exec->fds_bits, open_files/8);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	for (i = open_files; i != 0; i--) {
6751da177e4SLinus Torvalds 		struct file *f = *old_fds++;
6761da177e4SLinus Torvalds 		if (f) {
6771da177e4SLinus Torvalds 			get_file(f);
6781da177e4SLinus Torvalds 		} else {
6791da177e4SLinus Torvalds 			/*
6801da177e4SLinus Torvalds 			 * The fd may be claimed in the fd bitmap but not yet
6811da177e4SLinus Torvalds 			 * instantiated in the files array if a sibling thread
6821da177e4SLinus Torvalds 			 * is partway through open().  So make sure that this
6831da177e4SLinus Torvalds 			 * fd is available to the new process.
6841da177e4SLinus Torvalds 			 */
685badf1662SDipankar Sarma 			FD_CLR(open_files - i, new_fdt->open_fds);
6861da177e4SLinus Torvalds 		}
687ab2af1f5SDipankar Sarma 		rcu_assign_pointer(*new_fds++, f);
6881da177e4SLinus Torvalds 	}
6891da177e4SLinus Torvalds 	spin_unlock(&oldf->file_lock);
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	/* compute the remainder to be cleared */
692badf1662SDipankar Sarma 	size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	/* This is long word aligned thus could use a optimized version */
6951da177e4SLinus Torvalds 	memset(new_fds, 0, size);
6961da177e4SLinus Torvalds 
697badf1662SDipankar Sarma 	if (new_fdt->max_fdset > open_files) {
698badf1662SDipankar Sarma 		int left = (new_fdt->max_fdset-open_files)/8;
6991da177e4SLinus Torvalds 		int start = open_files / (8 * sizeof(unsigned long));
7001da177e4SLinus Torvalds 
701badf1662SDipankar Sarma 		memset(&new_fdt->open_fds->fds_bits[start], 0, left);
702badf1662SDipankar Sarma 		memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	tsk->files = newf;
7061da177e4SLinus Torvalds 	error = 0;
7071da177e4SLinus Torvalds out:
7081da177e4SLinus Torvalds 	return error;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds out_release:
711badf1662SDipankar Sarma 	free_fdset (new_fdt->close_on_exec, new_fdt->max_fdset);
712badf1662SDipankar Sarma 	free_fdset (new_fdt->open_fds, new_fdt->max_fdset);
713badf1662SDipankar Sarma 	free_fd_array(new_fdt->fd, new_fdt->max_fds);
7141da177e4SLinus Torvalds 	kmem_cache_free(files_cachep, newf);
7151da177e4SLinus Torvalds 	goto out;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /*
7191da177e4SLinus Torvalds  *	Helper to unshare the files of the current task.
7201da177e4SLinus Torvalds  *	We don't want to expose copy_files internals to
7211da177e4SLinus Torvalds  *	the exec layer of the kernel.
7221da177e4SLinus Torvalds  */
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds int unshare_files(void)
7251da177e4SLinus Torvalds {
7261da177e4SLinus Torvalds 	struct files_struct *files  = current->files;
7271da177e4SLinus Torvalds 	int rc;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	if(!files)
7301da177e4SLinus Torvalds 		BUG();
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	/* This can race but the race causes us to copy when we don't
7331da177e4SLinus Torvalds 	   need to and drop the copy */
7341da177e4SLinus Torvalds 	if(atomic_read(&files->count) == 1)
7351da177e4SLinus Torvalds 	{
7361da177e4SLinus Torvalds 		atomic_inc(&files->count);
7371da177e4SLinus Torvalds 		return 0;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 	rc = copy_files(0, current);
7401da177e4SLinus Torvalds 	if(rc)
7411da177e4SLinus Torvalds 		current->files = files;
7421da177e4SLinus Torvalds 	return rc;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds EXPORT_SYMBOL(unshare_files);
7461da177e4SLinus Torvalds 
747e56d0903SIngo Molnar void sighand_free_cb(struct rcu_head *rhp)
748e56d0903SIngo Molnar {
749e56d0903SIngo Molnar 	struct sighand_struct *sp;
750e56d0903SIngo Molnar 
751e56d0903SIngo Molnar 	sp = container_of(rhp, struct sighand_struct, rcu);
752e56d0903SIngo Molnar 	kmem_cache_free(sighand_cachep, sp);
753e56d0903SIngo Molnar }
754e56d0903SIngo Molnar 
7551da177e4SLinus Torvalds static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	struct sighand_struct *sig;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
7601da177e4SLinus Torvalds 		atomic_inc(&current->sighand->count);
7611da177e4SLinus Torvalds 		return 0;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 	sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
764e56d0903SIngo Molnar 	rcu_assign_pointer(tsk->sighand, sig);
7651da177e4SLinus Torvalds 	if (!sig)
7661da177e4SLinus Torvalds 		return -ENOMEM;
7671da177e4SLinus Torvalds 	spin_lock_init(&sig->siglock);
7681da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
7691da177e4SLinus Torvalds 	memcpy(sig->action, current->sighand->action, sizeof(sig->action));
7701da177e4SLinus Torvalds 	return 0;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	struct signal_struct *sig;
7761da177e4SLinus Torvalds 	int ret;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	if (clone_flags & CLONE_THREAD) {
7791da177e4SLinus Torvalds 		atomic_inc(&current->signal->count);
7801da177e4SLinus Torvalds 		atomic_inc(&current->signal->live);
7811da177e4SLinus Torvalds 		return 0;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 	sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
7841da177e4SLinus Torvalds 	tsk->signal = sig;
7851da177e4SLinus Torvalds 	if (!sig)
7861da177e4SLinus Torvalds 		return -ENOMEM;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	ret = copy_thread_group_keys(tsk);
7891da177e4SLinus Torvalds 	if (ret < 0) {
7901da177e4SLinus Torvalds 		kmem_cache_free(signal_cachep, sig);
7911da177e4SLinus Torvalds 		return ret;
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	atomic_set(&sig->count, 1);
7951da177e4SLinus Torvalds 	atomic_set(&sig->live, 1);
7961da177e4SLinus Torvalds 	init_waitqueue_head(&sig->wait_chldexit);
7971da177e4SLinus Torvalds 	sig->flags = 0;
7981da177e4SLinus Torvalds 	sig->group_exit_code = 0;
7991da177e4SLinus Torvalds 	sig->group_exit_task = NULL;
8001da177e4SLinus Torvalds 	sig->group_stop_count = 0;
8011da177e4SLinus Torvalds 	sig->curr_target = NULL;
8021da177e4SLinus Torvalds 	init_sigpending(&sig->shared_pending);
8031da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->posix_timers);
8041da177e4SLinus Torvalds 
8052ff678b8SThomas Gleixner 	hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC);
8062ff678b8SThomas Gleixner 	sig->it_real_incr.tv64 = 0;
8071da177e4SLinus Torvalds 	sig->real_timer.function = it_real_fn;
8082ff678b8SThomas Gleixner 	sig->real_timer.data = tsk;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	sig->it_virt_expires = cputime_zero;
8111da177e4SLinus Torvalds 	sig->it_virt_incr = cputime_zero;
8121da177e4SLinus Torvalds 	sig->it_prof_expires = cputime_zero;
8131da177e4SLinus Torvalds 	sig->it_prof_incr = cputime_zero;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	sig->leader = 0;	/* session leadership doesn't inherit */
8161da177e4SLinus Torvalds 	sig->tty_old_pgrp = 0;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
8191da177e4SLinus Torvalds 	sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
8201da177e4SLinus Torvalds 	sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
8211da177e4SLinus Torvalds 	sig->sched_time = 0;
8221da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[0]);
8231da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[1]);
8241da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sig->cpu_timers[2]);
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	task_lock(current->group_leader);
8271da177e4SLinus Torvalds 	memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
8281da177e4SLinus Torvalds 	task_unlock(current->group_leader);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
8311da177e4SLinus Torvalds 		/*
8321da177e4SLinus Torvalds 		 * New sole thread in the process gets an expiry time
8331da177e4SLinus Torvalds 		 * of the whole CPU time limit.
8341da177e4SLinus Torvalds 		 */
8351da177e4SLinus Torvalds 		tsk->it_prof_expires =
8361da177e4SLinus Torvalds 			secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	return 0;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	unsigned long new_flags = p->flags;
8451da177e4SLinus Torvalds 
846d1209d04SAlan Stern 	new_flags &= ~(PF_SUPERPRIV | PF_NOFREEZE);
8471da177e4SLinus Torvalds 	new_flags |= PF_FORKNOEXEC;
8481da177e4SLinus Torvalds 	if (!(clone_flags & CLONE_PTRACE))
8491da177e4SLinus Torvalds 		p->ptrace = 0;
8501da177e4SLinus Torvalds 	p->flags = new_flags;
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds asmlinkage long sys_set_tid_address(int __user *tidptr)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	current->clear_child_tid = tidptr;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	return current->pid;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds /*
8611da177e4SLinus Torvalds  * This creates a new process as a copy of the old one,
8621da177e4SLinus Torvalds  * but does not actually start it yet.
8631da177e4SLinus Torvalds  *
8641da177e4SLinus Torvalds  * It copies the registers, and all the appropriate
8651da177e4SLinus Torvalds  * parts of the process environment (as per the clone
8661da177e4SLinus Torvalds  * flags). The actual kick-off is left to the caller.
8671da177e4SLinus Torvalds  */
8681da177e4SLinus Torvalds static task_t *copy_process(unsigned long clone_flags,
8691da177e4SLinus Torvalds 				 unsigned long stack_start,
8701da177e4SLinus Torvalds 				 struct pt_regs *regs,
8711da177e4SLinus Torvalds 				 unsigned long stack_size,
8721da177e4SLinus Torvalds 				 int __user *parent_tidptr,
8731da177e4SLinus Torvalds 				 int __user *child_tidptr,
8741da177e4SLinus Torvalds 				 int pid)
8751da177e4SLinus Torvalds {
8761da177e4SLinus Torvalds 	int retval;
8771da177e4SLinus Torvalds 	struct task_struct *p = NULL;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
8801da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	/*
8831da177e4SLinus Torvalds 	 * Thread groups must share signals as well, and detached threads
8841da177e4SLinus Torvalds 	 * can only be started up within the thread group.
8851da177e4SLinus Torvalds 	 */
8861da177e4SLinus Torvalds 	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
8871da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	/*
8901da177e4SLinus Torvalds 	 * Shared signal handlers imply shared VM. By way of the above,
8911da177e4SLinus Torvalds 	 * thread groups also imply shared VM. Blocking this case allows
8921da177e4SLinus Torvalds 	 * for various simplifications in other code.
8931da177e4SLinus Torvalds 	 */
8941da177e4SLinus Torvalds 	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
8951da177e4SLinus Torvalds 		return ERR_PTR(-EINVAL);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	retval = security_task_create(clone_flags);
8981da177e4SLinus Torvalds 	if (retval)
8991da177e4SLinus Torvalds 		goto fork_out;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	retval = -ENOMEM;
9021da177e4SLinus Torvalds 	p = dup_task_struct(current);
9031da177e4SLinus Torvalds 	if (!p)
9041da177e4SLinus Torvalds 		goto fork_out;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	retval = -EAGAIN;
9071da177e4SLinus Torvalds 	if (atomic_read(&p->user->processes) >=
9081da177e4SLinus Torvalds 			p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
9091da177e4SLinus Torvalds 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
9101da177e4SLinus Torvalds 				p->user != &root_user)
9111da177e4SLinus Torvalds 			goto bad_fork_free;
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	atomic_inc(&p->user->__count);
9151da177e4SLinus Torvalds 	atomic_inc(&p->user->processes);
9161da177e4SLinus Torvalds 	get_group_info(p->group_info);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/*
9191da177e4SLinus Torvalds 	 * If multiple threads are within copy_process(), then this check
9201da177e4SLinus Torvalds 	 * triggers too late. This doesn't hurt, the check is only there
9211da177e4SLinus Torvalds 	 * to stop root fork bombs.
9221da177e4SLinus Torvalds 	 */
9231da177e4SLinus Torvalds 	if (nr_threads >= max_threads)
9241da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
9251da177e4SLinus Torvalds 
926a1261f54SAl Viro 	if (!try_module_get(task_thread_info(p)->exec_domain->module))
9271da177e4SLinus Torvalds 		goto bad_fork_cleanup_count;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	if (p->binfmt && !try_module_get(p->binfmt->module))
9301da177e4SLinus Torvalds 		goto bad_fork_cleanup_put_domain;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	p->did_exec = 0;
9331da177e4SLinus Torvalds 	copy_flags(clone_flags, p);
9341da177e4SLinus Torvalds 	p->pid = pid;
9351da177e4SLinus Torvalds 	retval = -EFAULT;
9361da177e4SLinus Torvalds 	if (clone_flags & CLONE_PARENT_SETTID)
9371da177e4SLinus Torvalds 		if (put_user(p->pid, parent_tidptr))
9381da177e4SLinus Torvalds 			goto bad_fork_cleanup;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	p->proc_dentry = NULL;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->children);
9431da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->sibling);
9441da177e4SLinus Torvalds 	p->vfork_done = NULL;
9451da177e4SLinus Torvalds 	spin_lock_init(&p->alloc_lock);
9461da177e4SLinus Torvalds 	spin_lock_init(&p->proc_lock);
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	clear_tsk_thread_flag(p, TIF_SIGPENDING);
9491da177e4SLinus Torvalds 	init_sigpending(&p->pending);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	p->utime = cputime_zero;
9521da177e4SLinus Torvalds 	p->stime = cputime_zero;
9531da177e4SLinus Torvalds  	p->sched_time = 0;
9541da177e4SLinus Torvalds 	p->rchar = 0;		/* I/O counter: bytes read */
9551da177e4SLinus Torvalds 	p->wchar = 0;		/* I/O counter: bytes written */
9561da177e4SLinus Torvalds 	p->syscr = 0;		/* I/O counter: read syscalls */
9571da177e4SLinus Torvalds 	p->syscw = 0;		/* I/O counter: write syscalls */
9581da177e4SLinus Torvalds 	acct_clear_integrals(p);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds  	p->it_virt_expires = cputime_zero;
9611da177e4SLinus Torvalds 	p->it_prof_expires = cputime_zero;
9621da177e4SLinus Torvalds  	p->it_sched_expires = 0;
9631da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[0]);
9641da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[1]);
9651da177e4SLinus Torvalds  	INIT_LIST_HEAD(&p->cpu_timers[2]);
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	p->lock_depth = -1;		/* -1 = no lock */
9681da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&p->start_time);
9691da177e4SLinus Torvalds 	p->security = NULL;
9701da177e4SLinus Torvalds 	p->io_context = NULL;
9711da177e4SLinus Torvalds 	p->io_wait = NULL;
9721da177e4SLinus Torvalds 	p->audit_context = NULL;
973b4b26418SPaul Jackson 	cpuset_fork(p);
9741da177e4SLinus Torvalds #ifdef CONFIG_NUMA
9751da177e4SLinus Torvalds  	p->mempolicy = mpol_copy(p->mempolicy);
9761da177e4SLinus Torvalds  	if (IS_ERR(p->mempolicy)) {
9771da177e4SLinus Torvalds  		retval = PTR_ERR(p->mempolicy);
9781da177e4SLinus Torvalds  		p->mempolicy = NULL;
979b4b26418SPaul Jackson  		goto bad_fork_cleanup_cpuset;
9801da177e4SLinus Torvalds  	}
9811da177e4SLinus Torvalds #endif
9821da177e4SLinus Torvalds 
983408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
984408894eeSIngo Molnar 	p->blocked_on = NULL; /* not blocked yet */
985408894eeSIngo Molnar #endif
986408894eeSIngo Molnar 
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 	attach_pid(p, PIDTYPE_PID, p->pid);
11391da177e4SLinus Torvalds 	attach_pid(p, PIDTYPE_TGID, p->tgid);
11401da177e4SLinus Torvalds 	if (thread_group_leader(p)) {
11419a5d3023SOren Laadan 		p->signal->tty = current->signal->tty;
11429a5d3023SOren Laadan 		p->signal->pgrp = process_group(current);
11439a5d3023SOren Laadan 		p->signal->session = current->signal->session;
11441da177e4SLinus Torvalds 		attach_pid(p, PIDTYPE_PGID, process_group(p));
11451da177e4SLinus Torvalds 		attach_pid(p, PIDTYPE_SID, p->signal->session);
11461da177e4SLinus Torvalds 		if (p->pid)
11471da177e4SLinus Torvalds 			__get_cpu_var(process_counts)++;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	nr_threads++;
11511da177e4SLinus Torvalds 	total_forks++;
11521da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
1153c13cf856SAndrew Morton 	proc_fork_connector(p);
11541da177e4SLinus Torvalds 	return p;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds bad_fork_cleanup_namespace:
11571da177e4SLinus Torvalds 	exit_namespace(p);
11581da177e4SLinus Torvalds bad_fork_cleanup_keys:
11591da177e4SLinus Torvalds 	exit_keys(p);
11601da177e4SLinus Torvalds bad_fork_cleanup_mm:
11611da177e4SLinus Torvalds 	if (p->mm)
11621da177e4SLinus Torvalds 		mmput(p->mm);
11631da177e4SLinus Torvalds bad_fork_cleanup_signal:
11641da177e4SLinus Torvalds 	exit_signal(p);
11651da177e4SLinus Torvalds bad_fork_cleanup_sighand:
11661da177e4SLinus Torvalds 	exit_sighand(p);
11671da177e4SLinus Torvalds bad_fork_cleanup_fs:
11681da177e4SLinus Torvalds 	exit_fs(p); /* blocking */
11691da177e4SLinus Torvalds bad_fork_cleanup_files:
11701da177e4SLinus Torvalds 	exit_files(p); /* blocking */
11711da177e4SLinus Torvalds bad_fork_cleanup_semundo:
11721da177e4SLinus Torvalds 	exit_sem(p);
11731da177e4SLinus Torvalds bad_fork_cleanup_audit:
11741da177e4SLinus Torvalds 	audit_free(p);
11751da177e4SLinus Torvalds bad_fork_cleanup_security:
11761da177e4SLinus Torvalds 	security_task_free(p);
11771da177e4SLinus Torvalds bad_fork_cleanup_policy:
11781da177e4SLinus Torvalds #ifdef CONFIG_NUMA
11791da177e4SLinus Torvalds 	mpol_free(p->mempolicy);
1180b4b26418SPaul Jackson bad_fork_cleanup_cpuset:
11811da177e4SLinus Torvalds #endif
1182b4b26418SPaul Jackson 	cpuset_exit(p);
11831da177e4SLinus Torvalds bad_fork_cleanup:
11841da177e4SLinus Torvalds 	if (p->binfmt)
11851da177e4SLinus Torvalds 		module_put(p->binfmt->module);
11861da177e4SLinus Torvalds bad_fork_cleanup_put_domain:
1187a1261f54SAl Viro 	module_put(task_thread_info(p)->exec_domain->module);
11881da177e4SLinus Torvalds bad_fork_cleanup_count:
11891da177e4SLinus Torvalds 	put_group_info(p->group_info);
11901da177e4SLinus Torvalds 	atomic_dec(&p->user->processes);
11911da177e4SLinus Torvalds 	free_uid(p->user);
11921da177e4SLinus Torvalds bad_fork_free:
11931da177e4SLinus Torvalds 	free_task(p);
1194fe7d37d1SOleg Nesterov fork_out:
1195fe7d37d1SOleg Nesterov 	return ERR_PTR(retval);
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
11991da177e4SLinus Torvalds {
12001da177e4SLinus Torvalds 	memset(regs, 0, sizeof(struct pt_regs));
12011da177e4SLinus Torvalds 	return regs;
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds task_t * __devinit fork_idle(int cpu)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	task_t *task;
12071da177e4SLinus Torvalds 	struct pt_regs regs;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0);
12101da177e4SLinus Torvalds 	if (!task)
12111da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
12121da177e4SLinus Torvalds 	init_idle(task, cpu);
12131da177e4SLinus Torvalds 	unhash_process(task);
12141da177e4SLinus Torvalds 	return task;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds static inline int fork_traceflag (unsigned clone_flags)
12181da177e4SLinus Torvalds {
12191da177e4SLinus Torvalds 	if (clone_flags & CLONE_UNTRACED)
12201da177e4SLinus Torvalds 		return 0;
12211da177e4SLinus Torvalds 	else if (clone_flags & CLONE_VFORK) {
12221da177e4SLinus Torvalds 		if (current->ptrace & PT_TRACE_VFORK)
12231da177e4SLinus Torvalds 			return PTRACE_EVENT_VFORK;
12241da177e4SLinus Torvalds 	} else if ((clone_flags & CSIGNAL) != SIGCHLD) {
12251da177e4SLinus Torvalds 		if (current->ptrace & PT_TRACE_CLONE)
12261da177e4SLinus Torvalds 			return PTRACE_EVENT_CLONE;
12271da177e4SLinus Torvalds 	} else if (current->ptrace & PT_TRACE_FORK)
12281da177e4SLinus Torvalds 		return PTRACE_EVENT_FORK;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	return 0;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds /*
12341da177e4SLinus Torvalds  *  Ok, this is the main fork-routine.
12351da177e4SLinus Torvalds  *
12361da177e4SLinus Torvalds  * It copies the process, and if successful kick-starts
12371da177e4SLinus Torvalds  * it and waits for it to finish using the VM if required.
12381da177e4SLinus Torvalds  */
12391da177e4SLinus Torvalds long do_fork(unsigned long clone_flags,
12401da177e4SLinus Torvalds 	      unsigned long stack_start,
12411da177e4SLinus Torvalds 	      struct pt_regs *regs,
12421da177e4SLinus Torvalds 	      unsigned long stack_size,
12431da177e4SLinus Torvalds 	      int __user *parent_tidptr,
12441da177e4SLinus Torvalds 	      int __user *child_tidptr)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds 	struct task_struct *p;
12471da177e4SLinus Torvalds 	int trace = 0;
12481da177e4SLinus Torvalds 	long pid = alloc_pidmap();
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	if (pid < 0)
12511da177e4SLinus Torvalds 		return -EAGAIN;
12521da177e4SLinus Torvalds 	if (unlikely(current->ptrace)) {
12531da177e4SLinus Torvalds 		trace = fork_traceflag (clone_flags);
12541da177e4SLinus Torvalds 		if (trace)
12551da177e4SLinus Torvalds 			clone_flags |= CLONE_PTRACE;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
12591da177e4SLinus Torvalds 	/*
12601da177e4SLinus Torvalds 	 * Do this prior waking up the new thread - the thread pointer
12611da177e4SLinus Torvalds 	 * might get invalid after that point, if the thread exits quickly.
12621da177e4SLinus Torvalds 	 */
12631da177e4SLinus Torvalds 	if (!IS_ERR(p)) {
12641da177e4SLinus Torvalds 		struct completion vfork;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
12671da177e4SLinus Torvalds 			p->vfork_done = &vfork;
12681da177e4SLinus Torvalds 			init_completion(&vfork);
12691da177e4SLinus Torvalds 		}
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
12721da177e4SLinus Torvalds 			/*
12731da177e4SLinus Torvalds 			 * We'll start up with an immediate SIGSTOP.
12741da177e4SLinus Torvalds 			 */
12751da177e4SLinus Torvalds 			sigaddset(&p->pending.signal, SIGSTOP);
12761da177e4SLinus Torvalds 			set_tsk_thread_flag(p, TIF_SIGPENDING);
12771da177e4SLinus Torvalds 		}
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 		if (!(clone_flags & CLONE_STOPPED))
12801da177e4SLinus Torvalds 			wake_up_new_task(p, clone_flags);
12811da177e4SLinus Torvalds 		else
12821da177e4SLinus Torvalds 			p->state = TASK_STOPPED;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 		if (unlikely (trace)) {
12851da177e4SLinus Torvalds 			current->ptrace_message = pid;
12861da177e4SLinus Torvalds 			ptrace_notify ((trace << 8) | SIGTRAP);
12871da177e4SLinus Torvalds 		}
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 		if (clone_flags & CLONE_VFORK) {
12901da177e4SLinus Torvalds 			wait_for_completion(&vfork);
12911da177e4SLinus Torvalds 			if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
12921da177e4SLinus Torvalds 				ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
12931da177e4SLinus Torvalds 		}
12941da177e4SLinus Torvalds 	} else {
12951da177e4SLinus Torvalds 		free_pidmap(pid);
12961da177e4SLinus Torvalds 		pid = PTR_ERR(p);
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds 	return pid;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds void __init proc_caches_init(void)
13021da177e4SLinus Torvalds {
13031da177e4SLinus Torvalds 	sighand_cachep = kmem_cache_create("sighand_cache",
13041da177e4SLinus Torvalds 			sizeof(struct sighand_struct), 0,
13051da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13061da177e4SLinus Torvalds 	signal_cachep = kmem_cache_create("signal_cache",
13071da177e4SLinus Torvalds 			sizeof(struct signal_struct), 0,
13081da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13091da177e4SLinus Torvalds 	files_cachep = kmem_cache_create("files_cache",
13101da177e4SLinus Torvalds 			sizeof(struct files_struct), 0,
13111da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13121da177e4SLinus Torvalds 	fs_cachep = kmem_cache_create("fs_cache",
13131da177e4SLinus Torvalds 			sizeof(struct fs_struct), 0,
13141da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13151da177e4SLinus Torvalds 	vm_area_cachep = kmem_cache_create("vm_area_struct",
13161da177e4SLinus Torvalds 			sizeof(struct vm_area_struct), 0,
13171da177e4SLinus Torvalds 			SLAB_PANIC, NULL, NULL);
13181da177e4SLinus Torvalds 	mm_cachep = kmem_cache_create("mm_struct",
13191da177e4SLinus Torvalds 			sizeof(struct mm_struct), 0,
13201da177e4SLinus Torvalds 			SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
13211da177e4SLinus Torvalds }
1322