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> 381da177e4SLinus Torvalds #include <linux/ptrace.h> 391da177e4SLinus Torvalds #include <linux/mount.h> 401da177e4SLinus Torvalds #include <linux/audit.h> 411da177e4SLinus Torvalds #include <linux/profile.h> 421da177e4SLinus Torvalds #include <linux/rmap.h> 431da177e4SLinus Torvalds #include <linux/acct.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include <asm/pgtable.h> 461da177e4SLinus Torvalds #include <asm/pgalloc.h> 471da177e4SLinus Torvalds #include <asm/uaccess.h> 481da177e4SLinus Torvalds #include <asm/mmu_context.h> 491da177e4SLinus Torvalds #include <asm/cacheflush.h> 501da177e4SLinus Torvalds #include <asm/tlbflush.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* 531da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 541da177e4SLinus Torvalds */ 551da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 561da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds EXPORT_SYMBOL(tasklist_lock); 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds int nr_processes(void) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds int cpu; 691da177e4SLinus Torvalds int total = 0; 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds for_each_online_cpu(cpu) 721da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds return total; 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 781da177e4SLinus Torvalds # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL) 791da177e4SLinus Torvalds # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk)) 801da177e4SLinus Torvalds static kmem_cache_t *task_struct_cachep; 811da177e4SLinus Torvalds #endif 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 841da177e4SLinus Torvalds kmem_cache_t *signal_cachep; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 871da177e4SLinus Torvalds kmem_cache_t *sighand_cachep; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 901da177e4SLinus Torvalds kmem_cache_t *files_cachep; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 931da177e4SLinus Torvalds kmem_cache_t *fs_cachep; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 961da177e4SLinus Torvalds kmem_cache_t *vm_area_cachep; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 991da177e4SLinus Torvalds static kmem_cache_t *mm_cachep; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 1021da177e4SLinus Torvalds { 1031da177e4SLinus Torvalds free_thread_info(tsk->thread_info); 1041da177e4SLinus Torvalds free_task_struct(tsk); 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds void __put_task_struct(struct task_struct *tsk) 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE))); 1111da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 1121da177e4SLinus Torvalds WARN_ON(tsk == current); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds if (unlikely(tsk->audit_context)) 1151da177e4SLinus Torvalds audit_free(tsk); 1161da177e4SLinus Torvalds security_task_free(tsk); 1171da177e4SLinus Torvalds free_uid(tsk->user); 1181da177e4SLinus Torvalds put_group_info(tsk->group_info); 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 1211da177e4SLinus Torvalds free_task(tsk); 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds void __init fork_init(unsigned long mempages) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 1271da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 1281da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 1291da177e4SLinus Torvalds #endif 1301da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 1311da177e4SLinus Torvalds task_struct_cachep = 1321da177e4SLinus Torvalds kmem_cache_create("task_struct", sizeof(struct task_struct), 1331da177e4SLinus Torvalds ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL); 1341da177e4SLinus Torvalds #endif 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* 1371da177e4SLinus Torvalds * The default maximum number of threads is set to a safe 1381da177e4SLinus Torvalds * value: the thread structures can take up at most half 1391da177e4SLinus Torvalds * of memory. 1401da177e4SLinus Torvalds */ 1411da177e4SLinus Torvalds max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /* 1441da177e4SLinus Torvalds * we need to allow at least 20 threads to boot a system 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds if(max_threads < 20) 1471da177e4SLinus Torvalds max_threads = 20; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 1501da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 1511da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 1521da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 1561da177e4SLinus Torvalds { 1571da177e4SLinus Torvalds struct task_struct *tsk; 1581da177e4SLinus Torvalds struct thread_info *ti; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds prepare_to_copy(orig); 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds tsk = alloc_task_struct(); 1631da177e4SLinus Torvalds if (!tsk) 1641da177e4SLinus Torvalds return NULL; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds ti = alloc_thread_info(tsk); 1671da177e4SLinus Torvalds if (!ti) { 1681da177e4SLinus Torvalds free_task_struct(tsk); 1691da177e4SLinus Torvalds return NULL; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds *ti = *orig->thread_info; 1731da177e4SLinus Torvalds *tsk = *orig; 1741da177e4SLinus Torvalds tsk->thread_info = ti; 1751da177e4SLinus Torvalds ti->task = tsk; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds /* One for us, one for whoever does the "release_task()" (usually parent) */ 1781da177e4SLinus Torvalds atomic_set(&tsk->usage,2); 1791da177e4SLinus Torvalds return tsk; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds #ifdef CONFIG_MMU 1831da177e4SLinus Torvalds static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm) 1841da177e4SLinus Torvalds { 1851da177e4SLinus Torvalds struct vm_area_struct * mpnt, *tmp, **pprev; 1861da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 1871da177e4SLinus Torvalds int retval; 1881da177e4SLinus Torvalds unsigned long charge; 1891da177e4SLinus Torvalds struct mempolicy *pol; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 1921da177e4SLinus Torvalds flush_cache_mm(current->mm); 1931da177e4SLinus Torvalds mm->locked_vm = 0; 1941da177e4SLinus Torvalds mm->mmap = NULL; 1951da177e4SLinus Torvalds mm->mmap_cache = NULL; 1961da177e4SLinus Torvalds mm->free_area_cache = oldmm->mmap_base; 1971363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 1981da177e4SLinus Torvalds mm->map_count = 0; 1991da177e4SLinus Torvalds set_mm_counter(mm, rss, 0); 2001da177e4SLinus Torvalds set_mm_counter(mm, anon_rss, 0); 2011da177e4SLinus Torvalds cpus_clear(mm->cpu_vm_mask); 2021da177e4SLinus Torvalds mm->mm_rb = RB_ROOT; 2031da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 2041da177e4SLinus Torvalds rb_parent = NULL; 2051da177e4SLinus Torvalds pprev = &mm->mmap; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) { 2081da177e4SLinus Torvalds struct file *file; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 2113b6bfcdbSHugh Dickins long pages = vma_pages(mpnt); 2123b6bfcdbSHugh Dickins mm->total_vm -= pages; 2131da177e4SLinus Torvalds __vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 2143b6bfcdbSHugh Dickins -pages); 2151da177e4SLinus Torvalds continue; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds charge = 0; 2181da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 2191da177e4SLinus Torvalds unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT; 2201da177e4SLinus Torvalds if (security_vm_enough_memory(len)) 2211da177e4SLinus Torvalds goto fail_nomem; 2221da177e4SLinus Torvalds charge = len; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL); 2251da177e4SLinus Torvalds if (!tmp) 2261da177e4SLinus Torvalds goto fail_nomem; 2271da177e4SLinus Torvalds *tmp = *mpnt; 2281da177e4SLinus Torvalds pol = mpol_copy(vma_policy(mpnt)); 2291da177e4SLinus Torvalds retval = PTR_ERR(pol); 2301da177e4SLinus Torvalds if (IS_ERR(pol)) 2311da177e4SLinus Torvalds goto fail_nomem_policy; 2321da177e4SLinus Torvalds vma_set_policy(tmp, pol); 2331da177e4SLinus Torvalds tmp->vm_flags &= ~VM_LOCKED; 2341da177e4SLinus Torvalds tmp->vm_mm = mm; 2351da177e4SLinus Torvalds tmp->vm_next = NULL; 2361da177e4SLinus Torvalds anon_vma_link(tmp); 2371da177e4SLinus Torvalds file = tmp->vm_file; 2381da177e4SLinus Torvalds if (file) { 2391da177e4SLinus Torvalds struct inode *inode = file->f_dentry->d_inode; 2401da177e4SLinus Torvalds get_file(file); 2411da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 2421da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds /* insert tmp into the share list, just after mpnt */ 2451da177e4SLinus Torvalds spin_lock(&file->f_mapping->i_mmap_lock); 2461da177e4SLinus Torvalds tmp->vm_truncate_count = mpnt->vm_truncate_count; 2471da177e4SLinus Torvalds flush_dcache_mmap_lock(file->f_mapping); 2481da177e4SLinus Torvalds vma_prio_tree_add(tmp, mpnt); 2491da177e4SLinus Torvalds flush_dcache_mmap_unlock(file->f_mapping); 2501da177e4SLinus Torvalds spin_unlock(&file->f_mapping->i_mmap_lock); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds /* 2541da177e4SLinus Torvalds * Link in the new vma and copy the page table entries: 25545918e1aSHugh Dickins * link in first so that swapoff can see swap entries. 25645918e1aSHugh Dickins * Note that, exceptionally, here the vma is inserted 25745918e1aSHugh Dickins * without holding mm->mmap_sem. 2581da177e4SLinus Torvalds */ 2591da177e4SLinus Torvalds spin_lock(&mm->page_table_lock); 2601da177e4SLinus Torvalds *pprev = tmp; 2611da177e4SLinus Torvalds pprev = &tmp->vm_next; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 2641da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 2651da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds mm->map_count++; 2681da177e4SLinus Torvalds retval = copy_page_range(mm, current->mm, tmp); 2691da177e4SLinus Torvalds spin_unlock(&mm->page_table_lock); 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 2721da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds if (retval) 2751da177e4SLinus Torvalds goto out; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds retval = 0; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds out: 2801da177e4SLinus Torvalds flush_tlb_mm(current->mm); 2811da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 2821da177e4SLinus Torvalds return retval; 2831da177e4SLinus Torvalds fail_nomem_policy: 2841da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 2851da177e4SLinus Torvalds fail_nomem: 2861da177e4SLinus Torvalds retval = -ENOMEM; 2871da177e4SLinus Torvalds vm_unacct_memory(charge); 2881da177e4SLinus Torvalds goto out; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm) 2921da177e4SLinus Torvalds { 2931da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 2941da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 2951da177e4SLinus Torvalds return -ENOMEM; 2961da177e4SLinus Torvalds return 0; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds pgd_free(mm->pgd); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds #else 3041da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 3051da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 3061da177e4SLinus Torvalds #define mm_free_pgd(mm) 3071da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL)) 3121da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds #include <linux/init_task.h> 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds static struct mm_struct * mm_init(struct mm_struct * mm) 3171da177e4SLinus Torvalds { 3181da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 3191da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 3201da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 3211da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 3221da177e4SLinus Torvalds mm->core_waiters = 0; 3231da177e4SLinus Torvalds mm->nr_ptes = 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->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm); 3281da177e4SLinus Torvalds mm->free_area_cache = TASK_UNMAPPED_BASE; 3291363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds if (likely(!mm_alloc_pgd(mm))) { 3321da177e4SLinus Torvalds mm->def_flags = 0; 3331da177e4SLinus Torvalds return mm; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds free_mm(mm); 3361da177e4SLinus Torvalds return NULL; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /* 3401da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 3411da177e4SLinus Torvalds */ 3421da177e4SLinus Torvalds struct mm_struct * mm_alloc(void) 3431da177e4SLinus Torvalds { 3441da177e4SLinus Torvalds struct mm_struct * mm; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds mm = allocate_mm(); 3471da177e4SLinus Torvalds if (mm) { 3481da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 3491da177e4SLinus Torvalds mm = mm_init(mm); 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds return mm; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* 3551da177e4SLinus Torvalds * Called when the last reference to the mm 3561da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 3571da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 3581da177e4SLinus Torvalds */ 3591da177e4SLinus Torvalds void fastcall __mmdrop(struct mm_struct *mm) 3601da177e4SLinus Torvalds { 3611da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 3621da177e4SLinus Torvalds mm_free_pgd(mm); 3631da177e4SLinus Torvalds destroy_context(mm); 3641da177e4SLinus Torvalds free_mm(mm); 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds /* 3681da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 3691da177e4SLinus Torvalds */ 3701da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 3711da177e4SLinus Torvalds { 3721da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 3731da177e4SLinus Torvalds exit_aio(mm); 3741da177e4SLinus Torvalds exit_mmap(mm); 3751da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 3761da177e4SLinus Torvalds spin_lock(&mmlist_lock); 3771da177e4SLinus Torvalds list_del(&mm->mmlist); 3781da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds put_swap_token(mm); 3811da177e4SLinus Torvalds mmdrop(mm); 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds /** 3871da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 3881da177e4SLinus Torvalds * 3891da177e4SLinus Torvalds * Returns %NULL if the task has no mm. Checks PF_BORROWED_MM (meaning 3901da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 3911da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 3921da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 3931da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 3941da177e4SLinus Torvalds */ 3951da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds struct mm_struct *mm; 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds task_lock(task); 4001da177e4SLinus Torvalds mm = task->mm; 4011da177e4SLinus Torvalds if (mm) { 4021da177e4SLinus Torvalds if (task->flags & PF_BORROWED_MM) 4031da177e4SLinus Torvalds mm = NULL; 4041da177e4SLinus Torvalds else 4051da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds task_unlock(task); 4081da177e4SLinus Torvalds return mm; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 4131da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 4141da177e4SLinus Torvalds * error success whatever. 4151da177e4SLinus Torvalds * 4161da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 4171da177e4SLinus Torvalds * from the current process. 4181da177e4SLinus Torvalds * 4191da177e4SLinus Torvalds * This difference is important for error handling, when we 4201da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 4211da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 4221da177e4SLinus Torvalds * restoring the old one. . . 4231da177e4SLinus Torvalds * Eric Biederman 10 January 1998 4241da177e4SLinus Torvalds */ 4251da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds struct completion *vfork_done = tsk->vfork_done; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* Get rid of any cached register state */ 4301da177e4SLinus Torvalds deactivate_mm(tsk, mm); 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds /* notify parent sleeping on vfork() */ 4331da177e4SLinus Torvalds if (vfork_done) { 4341da177e4SLinus Torvalds tsk->vfork_done = NULL; 4351da177e4SLinus Torvalds complete(vfork_done); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) { 4381da177e4SLinus Torvalds u32 __user * tidptr = tsk->clear_child_tid; 4391da177e4SLinus Torvalds tsk->clear_child_tid = NULL; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds /* 4421da177e4SLinus Torvalds * We don't check the error code - if userspace has 4431da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 4441da177e4SLinus Torvalds */ 4451da177e4SLinus Torvalds put_user(0, tidptr); 4461da177e4SLinus Torvalds sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0); 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk) 4511da177e4SLinus Torvalds { 4521da177e4SLinus Torvalds struct mm_struct * mm, *oldmm; 4531da177e4SLinus Torvalds int retval; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 4561da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds tsk->mm = NULL; 4591da177e4SLinus Torvalds tsk->active_mm = NULL; 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds /* 4621da177e4SLinus Torvalds * Are we cloning a kernel thread? 4631da177e4SLinus Torvalds * 4641da177e4SLinus Torvalds * We need to steal a active VM for that.. 4651da177e4SLinus Torvalds */ 4661da177e4SLinus Torvalds oldmm = current->mm; 4671da177e4SLinus Torvalds if (!oldmm) 4681da177e4SLinus Torvalds return 0; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 4711da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 4721da177e4SLinus Torvalds mm = oldmm; 4731da177e4SLinus Torvalds /* 4741da177e4SLinus Torvalds * There are cases where the PTL is held to ensure no 4751da177e4SLinus Torvalds * new threads start up in user mode using an mm, which 4761da177e4SLinus Torvalds * allows optimizing out ipis; the tlb_gather_mmu code 4771da177e4SLinus Torvalds * is an example. 4781da177e4SLinus Torvalds */ 4791da177e4SLinus Torvalds spin_unlock_wait(&oldmm->page_table_lock); 4801da177e4SLinus Torvalds goto good_mm; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds retval = -ENOMEM; 4841da177e4SLinus Torvalds mm = allocate_mm(); 4851da177e4SLinus Torvalds if (!mm) 4861da177e4SLinus Torvalds goto fail_nomem; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* Copy the current MM stuff.. */ 4891da177e4SLinus Torvalds memcpy(mm, oldmm, sizeof(*mm)); 4901da177e4SLinus Torvalds if (!mm_init(mm)) 4911da177e4SLinus Torvalds goto fail_nomem; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds if (init_new_context(tsk,mm)) 4941da177e4SLinus Torvalds goto fail_nocontext; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds retval = dup_mmap(mm, oldmm); 4971da177e4SLinus Torvalds if (retval) 4981da177e4SLinus Torvalds goto free_pt; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds mm->hiwater_rss = get_mm_counter(mm,rss); 5011da177e4SLinus Torvalds mm->hiwater_vm = mm->total_vm; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds good_mm: 5041da177e4SLinus Torvalds tsk->mm = mm; 5051da177e4SLinus Torvalds tsk->active_mm = mm; 5061da177e4SLinus Torvalds return 0; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds free_pt: 5091da177e4SLinus Torvalds mmput(mm); 5101da177e4SLinus Torvalds fail_nomem: 5111da177e4SLinus Torvalds return retval; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds fail_nocontext: 5141da177e4SLinus Torvalds /* 5151da177e4SLinus Torvalds * If init_new_context() failed, we cannot use mmput() to free the mm 5161da177e4SLinus Torvalds * because it calls destroy_context() 5171da177e4SLinus Torvalds */ 5181da177e4SLinus Torvalds mm_free_pgd(mm); 5191da177e4SLinus Torvalds free_mm(mm); 5201da177e4SLinus Torvalds return retval; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old) 5241da177e4SLinus Torvalds { 5251da177e4SLinus Torvalds struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL); 5261da177e4SLinus Torvalds /* We don't need to lock fs - think why ;-) */ 5271da177e4SLinus Torvalds if (fs) { 5281da177e4SLinus Torvalds atomic_set(&fs->count, 1); 5291da177e4SLinus Torvalds rwlock_init(&fs->lock); 5301da177e4SLinus Torvalds fs->umask = old->umask; 5311da177e4SLinus Torvalds read_lock(&old->lock); 5321da177e4SLinus Torvalds fs->rootmnt = mntget(old->rootmnt); 5331da177e4SLinus Torvalds fs->root = dget(old->root); 5341da177e4SLinus Torvalds fs->pwdmnt = mntget(old->pwdmnt); 5351da177e4SLinus Torvalds fs->pwd = dget(old->pwd); 5361da177e4SLinus Torvalds if (old->altroot) { 5371da177e4SLinus Torvalds fs->altrootmnt = mntget(old->altrootmnt); 5381da177e4SLinus Torvalds fs->altroot = dget(old->altroot); 5391da177e4SLinus Torvalds } else { 5401da177e4SLinus Torvalds fs->altrootmnt = NULL; 5411da177e4SLinus Torvalds fs->altroot = NULL; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds read_unlock(&old->lock); 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds return fs; 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds struct fs_struct *copy_fs_struct(struct fs_struct *old) 5491da177e4SLinus Torvalds { 5501da177e4SLinus Torvalds return __copy_fs_struct(old); 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(copy_fs_struct); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk) 5561da177e4SLinus Torvalds { 5571da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 5581da177e4SLinus Torvalds atomic_inc(¤t->fs->count); 5591da177e4SLinus Torvalds return 0; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds tsk->fs = __copy_fs_struct(current->fs); 5621da177e4SLinus Torvalds if (!tsk->fs) 5631da177e4SLinus Torvalds return -ENOMEM; 5641da177e4SLinus Torvalds return 0; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds static int count_open_files(struct files_struct *files, int size) 5681da177e4SLinus Torvalds { 5691da177e4SLinus Torvalds int i; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* Find the last open fd */ 5721da177e4SLinus Torvalds for (i = size/(8*sizeof(long)); i > 0; ) { 5731da177e4SLinus Torvalds if (files->open_fds->fds_bits[--i]) 5741da177e4SLinus Torvalds break; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds i = (i+1) * 8 * sizeof(long); 5771da177e4SLinus Torvalds return i; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds static int copy_files(unsigned long clone_flags, struct task_struct * tsk) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds struct files_struct *oldf, *newf; 5831da177e4SLinus Torvalds struct file **old_fds, **new_fds; 5841da177e4SLinus Torvalds int open_files, size, i, error = 0, expand; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* 5871da177e4SLinus Torvalds * A background process may not have any files ... 5881da177e4SLinus Torvalds */ 5891da177e4SLinus Torvalds oldf = current->files; 5901da177e4SLinus Torvalds if (!oldf) 5911da177e4SLinus Torvalds goto out; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds if (clone_flags & CLONE_FILES) { 5941da177e4SLinus Torvalds atomic_inc(&oldf->count); 5951da177e4SLinus Torvalds goto out; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /* 5991da177e4SLinus Torvalds * Note: we may be using current for both targets (See exec.c) 6001da177e4SLinus Torvalds * This works because we cache current->files (old) as oldf. Don't 6011da177e4SLinus Torvalds * break this. 6021da177e4SLinus Torvalds */ 6031da177e4SLinus Torvalds tsk->files = NULL; 6041da177e4SLinus Torvalds error = -ENOMEM; 6051da177e4SLinus Torvalds newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL); 6061da177e4SLinus Torvalds if (!newf) 6071da177e4SLinus Torvalds goto out; 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds atomic_set(&newf->count, 1); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds spin_lock_init(&newf->file_lock); 6121da177e4SLinus Torvalds newf->next_fd = 0; 6131da177e4SLinus Torvalds newf->max_fds = NR_OPEN_DEFAULT; 6141da177e4SLinus Torvalds newf->max_fdset = __FD_SETSIZE; 6151da177e4SLinus Torvalds newf->close_on_exec = &newf->close_on_exec_init; 6161da177e4SLinus Torvalds newf->open_fds = &newf->open_fds_init; 6171da177e4SLinus Torvalds newf->fd = &newf->fd_array[0]; 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds spin_lock(&oldf->file_lock); 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds open_files = count_open_files(oldf, oldf->max_fdset); 6221da177e4SLinus Torvalds expand = 0; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* 6251da177e4SLinus Torvalds * Check whether we need to allocate a larger fd array or fd set. 6261da177e4SLinus Torvalds * Note: we're not a clone task, so the open count won't change. 6271da177e4SLinus Torvalds */ 6281da177e4SLinus Torvalds if (open_files > newf->max_fdset) { 6291da177e4SLinus Torvalds newf->max_fdset = 0; 6301da177e4SLinus Torvalds expand = 1; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds if (open_files > newf->max_fds) { 6331da177e4SLinus Torvalds newf->max_fds = 0; 6341da177e4SLinus Torvalds expand = 1; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds /* if the old fdset gets grown now, we'll only copy up to "size" fds */ 6381da177e4SLinus Torvalds if (expand) { 6391da177e4SLinus Torvalds spin_unlock(&oldf->file_lock); 6401da177e4SLinus Torvalds spin_lock(&newf->file_lock); 6411da177e4SLinus Torvalds error = expand_files(newf, open_files-1); 6421da177e4SLinus Torvalds spin_unlock(&newf->file_lock); 6431da177e4SLinus Torvalds if (error < 0) 6441da177e4SLinus Torvalds goto out_release; 6451da177e4SLinus Torvalds spin_lock(&oldf->file_lock); 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds old_fds = oldf->fd; 6491da177e4SLinus Torvalds new_fds = newf->fd; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8); 6521da177e4SLinus Torvalds memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds for (i = open_files; i != 0; i--) { 6551da177e4SLinus Torvalds struct file *f = *old_fds++; 6561da177e4SLinus Torvalds if (f) { 6571da177e4SLinus Torvalds get_file(f); 6581da177e4SLinus Torvalds } else { 6591da177e4SLinus Torvalds /* 6601da177e4SLinus Torvalds * The fd may be claimed in the fd bitmap but not yet 6611da177e4SLinus Torvalds * instantiated in the files array if a sibling thread 6621da177e4SLinus Torvalds * is partway through open(). So make sure that this 6631da177e4SLinus Torvalds * fd is available to the new process. 6641da177e4SLinus Torvalds */ 6651da177e4SLinus Torvalds FD_CLR(open_files - i, newf->open_fds); 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds *new_fds++ = f; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds spin_unlock(&oldf->file_lock); 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds /* compute the remainder to be cleared */ 6721da177e4SLinus Torvalds size = (newf->max_fds - open_files) * sizeof(struct file *); 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds /* This is long word aligned thus could use a optimized version */ 6751da177e4SLinus Torvalds memset(new_fds, 0, size); 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds if (newf->max_fdset > open_files) { 6781da177e4SLinus Torvalds int left = (newf->max_fdset-open_files)/8; 6791da177e4SLinus Torvalds int start = open_files / (8 * sizeof(unsigned long)); 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds memset(&newf->open_fds->fds_bits[start], 0, left); 6821da177e4SLinus Torvalds memset(&newf->close_on_exec->fds_bits[start], 0, left); 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds tsk->files = newf; 6861da177e4SLinus Torvalds error = 0; 6871da177e4SLinus Torvalds out: 6881da177e4SLinus Torvalds return error; 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds out_release: 6911da177e4SLinus Torvalds free_fdset (newf->close_on_exec, newf->max_fdset); 6921da177e4SLinus Torvalds free_fdset (newf->open_fds, newf->max_fdset); 6931da177e4SLinus Torvalds free_fd_array(newf->fd, newf->max_fds); 6941da177e4SLinus Torvalds kmem_cache_free(files_cachep, newf); 6951da177e4SLinus Torvalds goto out; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds /* 6991da177e4SLinus Torvalds * Helper to unshare the files of the current task. 7001da177e4SLinus Torvalds * We don't want to expose copy_files internals to 7011da177e4SLinus Torvalds * the exec layer of the kernel. 7021da177e4SLinus Torvalds */ 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds int unshare_files(void) 7051da177e4SLinus Torvalds { 7061da177e4SLinus Torvalds struct files_struct *files = current->files; 7071da177e4SLinus Torvalds int rc; 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds if(!files) 7101da177e4SLinus Torvalds BUG(); 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* This can race but the race causes us to copy when we don't 7131da177e4SLinus Torvalds need to and drop the copy */ 7141da177e4SLinus Torvalds if(atomic_read(&files->count) == 1) 7151da177e4SLinus Torvalds { 7161da177e4SLinus Torvalds atomic_inc(&files->count); 7171da177e4SLinus Torvalds return 0; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds rc = copy_files(0, current); 7201da177e4SLinus Torvalds if(rc) 7211da177e4SLinus Torvalds current->files = files; 7221da177e4SLinus Torvalds return rc; 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds EXPORT_SYMBOL(unshare_files); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk) 7281da177e4SLinus Torvalds { 7291da177e4SLinus Torvalds struct sighand_struct *sig; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) { 7321da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 7331da177e4SLinus Torvalds return 0; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 7361da177e4SLinus Torvalds tsk->sighand = sig; 7371da177e4SLinus Torvalds if (!sig) 7381da177e4SLinus Torvalds return -ENOMEM; 7391da177e4SLinus Torvalds spin_lock_init(&sig->siglock); 7401da177e4SLinus Torvalds atomic_set(&sig->count, 1); 7411da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 7421da177e4SLinus Torvalds return 0; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk) 7461da177e4SLinus Torvalds { 7471da177e4SLinus Torvalds struct signal_struct *sig; 7481da177e4SLinus Torvalds int ret; 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds if (clone_flags & CLONE_THREAD) { 7511da177e4SLinus Torvalds atomic_inc(¤t->signal->count); 7521da177e4SLinus Torvalds atomic_inc(¤t->signal->live); 7531da177e4SLinus Torvalds return 0; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL); 7561da177e4SLinus Torvalds tsk->signal = sig; 7571da177e4SLinus Torvalds if (!sig) 7581da177e4SLinus Torvalds return -ENOMEM; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds ret = copy_thread_group_keys(tsk); 7611da177e4SLinus Torvalds if (ret < 0) { 7621da177e4SLinus Torvalds kmem_cache_free(signal_cachep, sig); 7631da177e4SLinus Torvalds return ret; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds atomic_set(&sig->count, 1); 7671da177e4SLinus Torvalds atomic_set(&sig->live, 1); 7681da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 7691da177e4SLinus Torvalds sig->flags = 0; 7701da177e4SLinus Torvalds sig->group_exit_code = 0; 7711da177e4SLinus Torvalds sig->group_exit_task = NULL; 7721da177e4SLinus Torvalds sig->group_stop_count = 0; 7731da177e4SLinus Torvalds sig->curr_target = NULL; 7741da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 7751da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds sig->it_real_value = sig->it_real_incr = 0; 7781da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 7791da177e4SLinus Torvalds sig->real_timer.data = (unsigned long) tsk; 7801da177e4SLinus Torvalds init_timer(&sig->real_timer); 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds sig->it_virt_expires = cputime_zero; 7831da177e4SLinus Torvalds sig->it_virt_incr = cputime_zero; 7841da177e4SLinus Torvalds sig->it_prof_expires = cputime_zero; 7851da177e4SLinus Torvalds sig->it_prof_incr = cputime_zero; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds sig->tty = current->signal->tty; 7881da177e4SLinus Torvalds sig->pgrp = process_group(current); 7891da177e4SLinus Torvalds sig->session = current->signal->session; 7901da177e4SLinus Torvalds sig->leader = 0; /* session leadership doesn't inherit */ 7911da177e4SLinus Torvalds sig->tty_old_pgrp = 0; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero; 7941da177e4SLinus Torvalds sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0; 7951da177e4SLinus Torvalds sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0; 7961da177e4SLinus Torvalds sig->sched_time = 0; 7971da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->cpu_timers[0]); 7981da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->cpu_timers[1]); 7991da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->cpu_timers[2]); 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds task_lock(current->group_leader); 8021da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 8031da177e4SLinus Torvalds task_unlock(current->group_leader); 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 8061da177e4SLinus Torvalds /* 8071da177e4SLinus Torvalds * New sole thread in the process gets an expiry time 8081da177e4SLinus Torvalds * of the whole CPU time limit. 8091da177e4SLinus Torvalds */ 8101da177e4SLinus Torvalds tsk->it_prof_expires = 8111da177e4SLinus Torvalds secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur); 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds return 0; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds static inline void copy_flags(unsigned long clone_flags, struct task_struct *p) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds unsigned long new_flags = p->flags; 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds new_flags &= ~PF_SUPERPRIV; 8221da177e4SLinus Torvalds new_flags |= PF_FORKNOEXEC; 8231da177e4SLinus Torvalds if (!(clone_flags & CLONE_PTRACE)) 8241da177e4SLinus Torvalds p->ptrace = 0; 8251da177e4SLinus Torvalds p->flags = new_flags; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds asmlinkage long sys_set_tid_address(int __user *tidptr) 8291da177e4SLinus Torvalds { 8301da177e4SLinus Torvalds current->clear_child_tid = tidptr; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds return current->pid; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* 8361da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 8371da177e4SLinus Torvalds * but does not actually start it yet. 8381da177e4SLinus Torvalds * 8391da177e4SLinus Torvalds * It copies the registers, and all the appropriate 8401da177e4SLinus Torvalds * parts of the process environment (as per the clone 8411da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 8421da177e4SLinus Torvalds */ 8431da177e4SLinus Torvalds static task_t *copy_process(unsigned long clone_flags, 8441da177e4SLinus Torvalds unsigned long stack_start, 8451da177e4SLinus Torvalds struct pt_regs *regs, 8461da177e4SLinus Torvalds unsigned long stack_size, 8471da177e4SLinus Torvalds int __user *parent_tidptr, 8481da177e4SLinus Torvalds int __user *child_tidptr, 8491da177e4SLinus Torvalds int pid) 8501da177e4SLinus Torvalds { 8511da177e4SLinus Torvalds int retval; 8521da177e4SLinus Torvalds struct task_struct *p = NULL; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 8551da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds /* 8581da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 8591da177e4SLinus Torvalds * can only be started up within the thread group. 8601da177e4SLinus Torvalds */ 8611da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 8621da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds /* 8651da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 8661da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 8671da177e4SLinus Torvalds * for various simplifications in other code. 8681da177e4SLinus Torvalds */ 8691da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 8701da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds retval = security_task_create(clone_flags); 8731da177e4SLinus Torvalds if (retval) 8741da177e4SLinus Torvalds goto fork_out; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds retval = -ENOMEM; 8771da177e4SLinus Torvalds p = dup_task_struct(current); 8781da177e4SLinus Torvalds if (!p) 8791da177e4SLinus Torvalds goto fork_out; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds retval = -EAGAIN; 8821da177e4SLinus Torvalds if (atomic_read(&p->user->processes) >= 8831da177e4SLinus Torvalds p->signal->rlim[RLIMIT_NPROC].rlim_cur) { 8841da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) && 8851da177e4SLinus Torvalds p->user != &root_user) 8861da177e4SLinus Torvalds goto bad_fork_free; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds atomic_inc(&p->user->__count); 8901da177e4SLinus Torvalds atomic_inc(&p->user->processes); 8911da177e4SLinus Torvalds get_group_info(p->group_info); 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds /* 8941da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 8951da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 8961da177e4SLinus Torvalds * to stop root fork bombs. 8971da177e4SLinus Torvalds */ 8981da177e4SLinus Torvalds if (nr_threads >= max_threads) 8991da177e4SLinus Torvalds goto bad_fork_cleanup_count; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds if (!try_module_get(p->thread_info->exec_domain->module)) 9021da177e4SLinus Torvalds goto bad_fork_cleanup_count; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds if (p->binfmt && !try_module_get(p->binfmt->module)) 9051da177e4SLinus Torvalds goto bad_fork_cleanup_put_domain; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds p->did_exec = 0; 9081da177e4SLinus Torvalds copy_flags(clone_flags, p); 9091da177e4SLinus Torvalds p->pid = pid; 9101da177e4SLinus Torvalds retval = -EFAULT; 9111da177e4SLinus Torvalds if (clone_flags & CLONE_PARENT_SETTID) 9121da177e4SLinus Torvalds if (put_user(p->pid, parent_tidptr)) 9131da177e4SLinus Torvalds goto bad_fork_cleanup; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds p->proc_dentry = NULL; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 9181da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 9191da177e4SLinus Torvalds p->vfork_done = NULL; 9201da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 9211da177e4SLinus Torvalds spin_lock_init(&p->proc_lock); 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SIGPENDING); 9241da177e4SLinus Torvalds init_sigpending(&p->pending); 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds p->utime = cputime_zero; 9271da177e4SLinus Torvalds p->stime = cputime_zero; 9281da177e4SLinus Torvalds p->sched_time = 0; 9291da177e4SLinus Torvalds p->rchar = 0; /* I/O counter: bytes read */ 9301da177e4SLinus Torvalds p->wchar = 0; /* I/O counter: bytes written */ 9311da177e4SLinus Torvalds p->syscr = 0; /* I/O counter: read syscalls */ 9321da177e4SLinus Torvalds p->syscw = 0; /* I/O counter: write syscalls */ 9331da177e4SLinus Torvalds acct_clear_integrals(p); 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds p->it_virt_expires = cputime_zero; 9361da177e4SLinus Torvalds p->it_prof_expires = cputime_zero; 9371da177e4SLinus Torvalds p->it_sched_expires = 0; 9381da177e4SLinus Torvalds INIT_LIST_HEAD(&p->cpu_timers[0]); 9391da177e4SLinus Torvalds INIT_LIST_HEAD(&p->cpu_timers[1]); 9401da177e4SLinus Torvalds INIT_LIST_HEAD(&p->cpu_timers[2]); 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds p->lock_depth = -1; /* -1 = no lock */ 9431da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&p->start_time); 9441da177e4SLinus Torvalds p->security = NULL; 9451da177e4SLinus Torvalds p->io_context = NULL; 9461da177e4SLinus Torvalds p->io_wait = NULL; 9471da177e4SLinus Torvalds p->audit_context = NULL; 9481da177e4SLinus Torvalds #ifdef CONFIG_NUMA 9491da177e4SLinus Torvalds p->mempolicy = mpol_copy(p->mempolicy); 9501da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 9511da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 9521da177e4SLinus Torvalds p->mempolicy = NULL; 9531da177e4SLinus Torvalds goto bad_fork_cleanup; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds #endif 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds p->tgid = p->pid; 9581da177e4SLinus Torvalds if (clone_flags & CLONE_THREAD) 9591da177e4SLinus Torvalds p->tgid = current->tgid; 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds if ((retval = security_task_alloc(p))) 9621da177e4SLinus Torvalds goto bad_fork_cleanup_policy; 9631da177e4SLinus Torvalds if ((retval = audit_alloc(p))) 9641da177e4SLinus Torvalds goto bad_fork_cleanup_security; 9651da177e4SLinus Torvalds /* copy all the process information */ 9661da177e4SLinus Torvalds if ((retval = copy_semundo(clone_flags, p))) 9671da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 9681da177e4SLinus Torvalds if ((retval = copy_files(clone_flags, p))) 9691da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 9701da177e4SLinus Torvalds if ((retval = copy_fs(clone_flags, p))) 9711da177e4SLinus Torvalds goto bad_fork_cleanup_files; 9721da177e4SLinus Torvalds if ((retval = copy_sighand(clone_flags, p))) 9731da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 9741da177e4SLinus Torvalds if ((retval = copy_signal(clone_flags, p))) 9751da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 9761da177e4SLinus Torvalds if ((retval = copy_mm(clone_flags, p))) 9771da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 9781da177e4SLinus Torvalds if ((retval = copy_keys(clone_flags, p))) 9791da177e4SLinus Torvalds goto bad_fork_cleanup_mm; 9801da177e4SLinus Torvalds if ((retval = copy_namespace(clone_flags, p))) 9811da177e4SLinus Torvalds goto bad_fork_cleanup_keys; 9821da177e4SLinus Torvalds retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs); 9831da177e4SLinus Torvalds if (retval) 9841da177e4SLinus Torvalds goto bad_fork_cleanup_namespace; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 9871da177e4SLinus Torvalds /* 9881da177e4SLinus Torvalds * Clear TID on mm_release()? 9891da177e4SLinus Torvalds */ 9901da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds /* 9931da177e4SLinus Torvalds * Syscall tracing should be turned off in the child regardless 9941da177e4SLinus Torvalds * of CLONE_PTRACE. 9951da177e4SLinus Torvalds */ 9961da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 997*ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 998*ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 999*ed75e8d5SLaurent Vivier #endif 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* Our parent execution domain becomes current domain 10021da177e4SLinus Torvalds These must match for thread signalling to apply */ 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds p->parent_exec_id = p->self_exec_id; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /* ok, now we should be set up.. */ 10071da177e4SLinus Torvalds p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL); 10081da177e4SLinus Torvalds p->pdeath_signal = 0; 10091da177e4SLinus Torvalds p->exit_state = 0; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds /* 10121da177e4SLinus Torvalds * Ok, make it visible to the rest of the system. 10131da177e4SLinus Torvalds * We dont wake it up yet. 10141da177e4SLinus Torvalds */ 10151da177e4SLinus Torvalds p->group_leader = p; 10161da177e4SLinus Torvalds INIT_LIST_HEAD(&p->ptrace_children); 10171da177e4SLinus Torvalds INIT_LIST_HEAD(&p->ptrace_list); 10181da177e4SLinus Torvalds 1019476d139cSNick Piggin /* Perform scheduler related setup. Assign this task to a CPU. */ 1020476d139cSNick Piggin sched_fork(p, clone_flags); 1021476d139cSNick Piggin 10221da177e4SLinus Torvalds /* Need tasklist lock for parent etc handling! */ 10231da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* 1026476d139cSNick Piggin * The task hasn't been attached yet, so its cpus_allowed mask will 1027476d139cSNick Piggin * not be changed, nor will its assigned CPU. 1028476d139cSNick Piggin * 1029476d139cSNick Piggin * The cpus_allowed mask of the parent may have changed after it was 1030476d139cSNick Piggin * copied first time - so re-copy it here, then check the child's CPU 1031476d139cSNick Piggin * to ensure it is on a valid CPU (and if not, just force it back to 1032476d139cSNick Piggin * parent's CPU). This avoids alot of nasty races. 10331da177e4SLinus Torvalds */ 10341da177e4SLinus Torvalds p->cpus_allowed = current->cpus_allowed; 1035476d139cSNick Piggin if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed))) 10361da177e4SLinus Torvalds set_task_cpu(p, smp_processor_id()); 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds /* 10391da177e4SLinus Torvalds * Check for pending SIGKILL! The new thread should not be allowed 10401da177e4SLinus Torvalds * to slip out of an OOM kill. (or normal SIGKILL.) 10411da177e4SLinus Torvalds */ 10421da177e4SLinus Torvalds if (sigismember(¤t->pending.signal, SIGKILL)) { 10431da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 10441da177e4SLinus Torvalds retval = -EINTR; 10451da177e4SLinus Torvalds goto bad_fork_cleanup_namespace; 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 10491da177e4SLinus Torvalds if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) 10501da177e4SLinus Torvalds p->real_parent = current->real_parent; 10511da177e4SLinus Torvalds else 10521da177e4SLinus Torvalds p->real_parent = current; 10531da177e4SLinus Torvalds p->parent = p->real_parent; 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds if (clone_flags & CLONE_THREAD) { 10561da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 10571da177e4SLinus Torvalds /* 10581da177e4SLinus Torvalds * Important: if an exit-all has been started then 10591da177e4SLinus Torvalds * do not create this new thread - the whole thread 10601da177e4SLinus Torvalds * group is supposed to exit anyway. 10611da177e4SLinus Torvalds */ 10621da177e4SLinus Torvalds if (current->signal->flags & SIGNAL_GROUP_EXIT) { 10631da177e4SLinus Torvalds spin_unlock(¤t->sighand->siglock); 10641da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 10651da177e4SLinus Torvalds retval = -EAGAIN; 10661da177e4SLinus Torvalds goto bad_fork_cleanup_namespace; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds p->group_leader = current->group_leader; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds if (current->signal->group_stop_count > 0) { 10711da177e4SLinus Torvalds /* 10721da177e4SLinus Torvalds * There is an all-stop in progress for the group. 10731da177e4SLinus Torvalds * We ourselves will stop as soon as we check signals. 10741da177e4SLinus Torvalds * Make the new thread part of that group stop too. 10751da177e4SLinus Torvalds */ 10761da177e4SLinus Torvalds current->signal->group_stop_count++; 10771da177e4SLinus Torvalds set_tsk_thread_flag(p, TIF_SIGPENDING); 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds if (!cputime_eq(current->signal->it_virt_expires, 10811da177e4SLinus Torvalds cputime_zero) || 10821da177e4SLinus Torvalds !cputime_eq(current->signal->it_prof_expires, 10831da177e4SLinus Torvalds cputime_zero) || 10841da177e4SLinus Torvalds current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY || 10851da177e4SLinus Torvalds !list_empty(¤t->signal->cpu_timers[0]) || 10861da177e4SLinus Torvalds !list_empty(¤t->signal->cpu_timers[1]) || 10871da177e4SLinus Torvalds !list_empty(¤t->signal->cpu_timers[2])) { 10881da177e4SLinus Torvalds /* 10891da177e4SLinus Torvalds * Have child wake up on its first tick to check 10901da177e4SLinus Torvalds * for process CPU timers. 10911da177e4SLinus Torvalds */ 10921da177e4SLinus Torvalds p->it_prof_expires = jiffies_to_cputime(1); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds spin_unlock(¤t->sighand->siglock); 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 109822e2c507SJens Axboe /* 109922e2c507SJens Axboe * inherit ioprio 110022e2c507SJens Axboe */ 110122e2c507SJens Axboe p->ioprio = current->ioprio; 110222e2c507SJens Axboe 11031da177e4SLinus Torvalds SET_LINKS(p); 11041da177e4SLinus Torvalds if (unlikely(p->ptrace & PT_PTRACED)) 11051da177e4SLinus Torvalds __ptrace_link(p, current->parent); 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds cpuset_fork(p); 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds attach_pid(p, PIDTYPE_PID, p->pid); 11101da177e4SLinus Torvalds attach_pid(p, PIDTYPE_TGID, p->tgid); 11111da177e4SLinus Torvalds if (thread_group_leader(p)) { 11121da177e4SLinus Torvalds attach_pid(p, PIDTYPE_PGID, process_group(p)); 11131da177e4SLinus Torvalds attach_pid(p, PIDTYPE_SID, p->signal->session); 11141da177e4SLinus Torvalds if (p->pid) 11151da177e4SLinus Torvalds __get_cpu_var(process_counts)++; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds nr_threads++; 11191da177e4SLinus Torvalds total_forks++; 11201da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 11211da177e4SLinus Torvalds retval = 0; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds fork_out: 11241da177e4SLinus Torvalds if (retval) 11251da177e4SLinus Torvalds return ERR_PTR(retval); 11261da177e4SLinus Torvalds return p; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds bad_fork_cleanup_namespace: 11291da177e4SLinus Torvalds exit_namespace(p); 11301da177e4SLinus Torvalds bad_fork_cleanup_keys: 11311da177e4SLinus Torvalds exit_keys(p); 11321da177e4SLinus Torvalds bad_fork_cleanup_mm: 11331da177e4SLinus Torvalds if (p->mm) 11341da177e4SLinus Torvalds mmput(p->mm); 11351da177e4SLinus Torvalds bad_fork_cleanup_signal: 11361da177e4SLinus Torvalds exit_signal(p); 11371da177e4SLinus Torvalds bad_fork_cleanup_sighand: 11381da177e4SLinus Torvalds exit_sighand(p); 11391da177e4SLinus Torvalds bad_fork_cleanup_fs: 11401da177e4SLinus Torvalds exit_fs(p); /* blocking */ 11411da177e4SLinus Torvalds bad_fork_cleanup_files: 11421da177e4SLinus Torvalds exit_files(p); /* blocking */ 11431da177e4SLinus Torvalds bad_fork_cleanup_semundo: 11441da177e4SLinus Torvalds exit_sem(p); 11451da177e4SLinus Torvalds bad_fork_cleanup_audit: 11461da177e4SLinus Torvalds audit_free(p); 11471da177e4SLinus Torvalds bad_fork_cleanup_security: 11481da177e4SLinus Torvalds security_task_free(p); 11491da177e4SLinus Torvalds bad_fork_cleanup_policy: 11501da177e4SLinus Torvalds #ifdef CONFIG_NUMA 11511da177e4SLinus Torvalds mpol_free(p->mempolicy); 11521da177e4SLinus Torvalds #endif 11531da177e4SLinus Torvalds bad_fork_cleanup: 11541da177e4SLinus Torvalds if (p->binfmt) 11551da177e4SLinus Torvalds module_put(p->binfmt->module); 11561da177e4SLinus Torvalds bad_fork_cleanup_put_domain: 11571da177e4SLinus Torvalds module_put(p->thread_info->exec_domain->module); 11581da177e4SLinus Torvalds bad_fork_cleanup_count: 11591da177e4SLinus Torvalds put_group_info(p->group_info); 11601da177e4SLinus Torvalds atomic_dec(&p->user->processes); 11611da177e4SLinus Torvalds free_uid(p->user); 11621da177e4SLinus Torvalds bad_fork_free: 11631da177e4SLinus Torvalds free_task(p); 11641da177e4SLinus Torvalds goto fork_out; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs) 11681da177e4SLinus Torvalds { 11691da177e4SLinus Torvalds memset(regs, 0, sizeof(struct pt_regs)); 11701da177e4SLinus Torvalds return regs; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds task_t * __devinit fork_idle(int cpu) 11741da177e4SLinus Torvalds { 11751da177e4SLinus Torvalds task_t *task; 11761da177e4SLinus Torvalds struct pt_regs regs; 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, NULL, 0); 11791da177e4SLinus Torvalds if (!task) 11801da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 11811da177e4SLinus Torvalds init_idle(task, cpu); 11821da177e4SLinus Torvalds unhash_process(task); 11831da177e4SLinus Torvalds return task; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds static inline int fork_traceflag (unsigned clone_flags) 11871da177e4SLinus Torvalds { 11881da177e4SLinus Torvalds if (clone_flags & CLONE_UNTRACED) 11891da177e4SLinus Torvalds return 0; 11901da177e4SLinus Torvalds else if (clone_flags & CLONE_VFORK) { 11911da177e4SLinus Torvalds if (current->ptrace & PT_TRACE_VFORK) 11921da177e4SLinus Torvalds return PTRACE_EVENT_VFORK; 11931da177e4SLinus Torvalds } else if ((clone_flags & CSIGNAL) != SIGCHLD) { 11941da177e4SLinus Torvalds if (current->ptrace & PT_TRACE_CLONE) 11951da177e4SLinus Torvalds return PTRACE_EVENT_CLONE; 11961da177e4SLinus Torvalds } else if (current->ptrace & PT_TRACE_FORK) 11971da177e4SLinus Torvalds return PTRACE_EVENT_FORK; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds return 0; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds /* 12031da177e4SLinus Torvalds * Ok, this is the main fork-routine. 12041da177e4SLinus Torvalds * 12051da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 12061da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 12071da177e4SLinus Torvalds */ 12081da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 12091da177e4SLinus Torvalds unsigned long stack_start, 12101da177e4SLinus Torvalds struct pt_regs *regs, 12111da177e4SLinus Torvalds unsigned long stack_size, 12121da177e4SLinus Torvalds int __user *parent_tidptr, 12131da177e4SLinus Torvalds int __user *child_tidptr) 12141da177e4SLinus Torvalds { 12151da177e4SLinus Torvalds struct task_struct *p; 12161da177e4SLinus Torvalds int trace = 0; 12171da177e4SLinus Torvalds long pid = alloc_pidmap(); 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds if (pid < 0) 12201da177e4SLinus Torvalds return -EAGAIN; 12211da177e4SLinus Torvalds if (unlikely(current->ptrace)) { 12221da177e4SLinus Torvalds trace = fork_traceflag (clone_flags); 12231da177e4SLinus Torvalds if (trace) 12241da177e4SLinus Torvalds clone_flags |= CLONE_PTRACE; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid); 12281da177e4SLinus Torvalds /* 12291da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 12301da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 12311da177e4SLinus Torvalds */ 12321da177e4SLinus Torvalds if (!IS_ERR(p)) { 12331da177e4SLinus Torvalds struct completion vfork; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 12361da177e4SLinus Torvalds p->vfork_done = &vfork; 12371da177e4SLinus Torvalds init_completion(&vfork); 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) { 12411da177e4SLinus Torvalds /* 12421da177e4SLinus Torvalds * We'll start up with an immediate SIGSTOP. 12431da177e4SLinus Torvalds */ 12441da177e4SLinus Torvalds sigaddset(&p->pending.signal, SIGSTOP); 12451da177e4SLinus Torvalds set_tsk_thread_flag(p, TIF_SIGPENDING); 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds if (!(clone_flags & CLONE_STOPPED)) 12491da177e4SLinus Torvalds wake_up_new_task(p, clone_flags); 12501da177e4SLinus Torvalds else 12511da177e4SLinus Torvalds p->state = TASK_STOPPED; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds if (unlikely (trace)) { 12541da177e4SLinus Torvalds current->ptrace_message = pid; 12551da177e4SLinus Torvalds ptrace_notify ((trace << 8) | SIGTRAP); 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 12591da177e4SLinus Torvalds wait_for_completion(&vfork); 12601da177e4SLinus Torvalds if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) 12611da177e4SLinus Torvalds ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP); 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds } else { 12641da177e4SLinus Torvalds free_pidmap(pid); 12651da177e4SLinus Torvalds pid = PTR_ERR(p); 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds return pid; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds void __init proc_caches_init(void) 12711da177e4SLinus Torvalds { 12721da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 12731da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 12741da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 12751da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 12761da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 12771da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 12781da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 12791da177e4SLinus Torvalds sizeof(struct files_struct), 0, 12801da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 12811da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 12821da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 12831da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 12841da177e4SLinus Torvalds vm_area_cachep = kmem_cache_create("vm_area_struct", 12851da177e4SLinus Torvalds sizeof(struct vm_area_struct), 0, 12861da177e4SLinus Torvalds SLAB_PANIC, NULL, NULL); 12871da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 12881da177e4SLinus Torvalds sizeof(struct mm_struct), 0, 12891da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 12901da177e4SLinus Torvalds } 1291