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/slab.h> 151da177e4SLinus Torvalds #include <linux/init.h> 161da177e4SLinus Torvalds #include <linux/unistd.h> 171da177e4SLinus Torvalds #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/vmalloc.h> 191da177e4SLinus Torvalds #include <linux/completion.h> 201da177e4SLinus Torvalds #include <linux/personality.h> 211da177e4SLinus Torvalds #include <linux/mempolicy.h> 221da177e4SLinus Torvalds #include <linux/sem.h> 231da177e4SLinus Torvalds #include <linux/file.h> 249f3acc31SAl Viro #include <linux/fdtable.h> 25da9cbc87SJens Axboe #include <linux/iocontext.h> 261da177e4SLinus Torvalds #include <linux/key.h> 271da177e4SLinus Torvalds #include <linux/binfmts.h> 281da177e4SLinus Torvalds #include <linux/mman.h> 29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 301da177e4SLinus Torvalds #include <linux/fs.h> 31ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 32c59ede7bSRandy.Dunlap #include <linux/capability.h> 331da177e4SLinus Torvalds #include <linux/cpu.h> 34b4f48b63SPaul Menage #include <linux/cgroup.h> 351da177e4SLinus Torvalds #include <linux/security.h> 36a1e78772SMel Gorman #include <linux/hugetlb.h> 371da177e4SLinus Torvalds #include <linux/swap.h> 381da177e4SLinus Torvalds #include <linux/syscalls.h> 391da177e4SLinus Torvalds #include <linux/jiffies.h> 401da177e4SLinus Torvalds #include <linux/futex.h> 418141c7f3SLinus Torvalds #include <linux/compat.h> 42207205a2SEric Dumazet #include <linux/kthread.h> 437c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 44ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 451da177e4SLinus Torvalds #include <linux/ptrace.h> 461da177e4SLinus Torvalds #include <linux/mount.h> 471da177e4SLinus Torvalds #include <linux/audit.h> 4878fb7466SPavel Emelianov #include <linux/memcontrol.h> 49f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 501da177e4SLinus Torvalds #include <linux/profile.h> 511da177e4SLinus Torvalds #include <linux/rmap.h> 52f8af4da3SHugh Dickins #include <linux/ksm.h> 531da177e4SLinus Torvalds #include <linux/acct.h> 548f0ab514SJay Lan #include <linux/tsacct_kern.h> 559f46080cSMatt Helsley #include <linux/cn_proc.h> 56ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 57ca74e92bSShailabh Nagar #include <linux/delayacct.h> 58ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 590a425405SArjan van de Ven #include <linux/random.h> 60522ed776SMiloslav Trmac #include <linux/tty.h> 61fd0928dfSJens Axboe #include <linux/blkdev.h> 625ad4e53bSAl Viro #include <linux/fs_struct.h> 637c9f8861SEric Sandeen #include <linux/magic.h> 64cdd6c482SIngo Molnar #include <linux/perf_event.h> 6542c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 668e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 673d5992d2SYing Han #include <linux/oom.h> 68ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #include <asm/pgtable.h> 711da177e4SLinus Torvalds #include <asm/pgalloc.h> 721da177e4SLinus Torvalds #include <asm/uaccess.h> 731da177e4SLinus Torvalds #include <asm/mmu_context.h> 741da177e4SLinus Torvalds #include <asm/cacheflush.h> 751da177e4SLinus Torvalds #include <asm/tlbflush.h> 761da177e4SLinus Torvalds 77ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 78ad8d75ffSSteven Rostedt 791da177e4SLinus Torvalds /* 801da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 811da177e4SLinus Torvalds */ 821da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 831da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 90db1466b3SPaul E. McKenney 91db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 92db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 93db1466b3SPaul E. McKenney { 94db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 95db1466b3SPaul E. McKenney } 96db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 97db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds int nr_processes(void) 1001da177e4SLinus Torvalds { 1011da177e4SLinus Torvalds int cpu; 1021da177e4SLinus Torvalds int total = 0; 1031da177e4SLinus Torvalds 1041d510750SIan Campbell for_each_possible_cpu(cpu) 1051da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds return total; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 111504f52b5SEric Dumazet # define alloc_task_struct_node(node) \ 112504f52b5SEric Dumazet kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node) 113504f52b5SEric Dumazet # define free_task_struct(tsk) \ 114504f52b5SEric Dumazet kmem_cache_free(task_struct_cachep, (tsk)) 115e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 1161da177e4SLinus Torvalds #endif 1171da177e4SLinus Torvalds 118b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR 119b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 120b6a84016SEric Dumazet int node) 121b69c49b7SFUJITA Tomonori { 122b69c49b7SFUJITA Tomonori #ifdef CONFIG_DEBUG_STACK_USAGE 123b69c49b7SFUJITA Tomonori gfp_t mask = GFP_KERNEL | __GFP_ZERO; 124b69c49b7SFUJITA Tomonori #else 125b69c49b7SFUJITA Tomonori gfp_t mask = GFP_KERNEL; 126b69c49b7SFUJITA Tomonori #endif 127b6a84016SEric Dumazet struct page *page = alloc_pages_node(node, mask, THREAD_SIZE_ORDER); 128b6a84016SEric Dumazet 129b6a84016SEric Dumazet return page ? page_address(page) : NULL; 130b69c49b7SFUJITA Tomonori } 131b69c49b7SFUJITA Tomonori 132b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti) 133b69c49b7SFUJITA Tomonori { 134b69c49b7SFUJITA Tomonori free_pages((unsigned long)ti, THREAD_SIZE_ORDER); 135b69c49b7SFUJITA Tomonori } 136b69c49b7SFUJITA Tomonori #endif 137b69c49b7SFUJITA Tomonori 1381da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 139e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 142e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 145e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 148e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 151e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 154e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 1551da177e4SLinus Torvalds 156c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account) 157c6a7f572SKOSAKI Motohiro { 158c6a7f572SKOSAKI Motohiro struct zone *zone = page_zone(virt_to_page(ti)); 159c6a7f572SKOSAKI Motohiro 160c6a7f572SKOSAKI Motohiro mod_zone_page_state(zone, NR_KERNEL_STACK, account); 161c6a7f572SKOSAKI Motohiro } 162c6a7f572SKOSAKI Motohiro 1631da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 1641da177e4SLinus Torvalds { 165c6a7f572SKOSAKI Motohiro account_kernel_stack(tsk->stack, -1); 166f7e4217bSRoman Zippel free_thread_info(tsk->stack); 16723f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 168fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 1691da177e4SLinus Torvalds free_task_struct(tsk); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 1721da177e4SLinus Torvalds 173ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig) 174ea6d290cSOleg Nesterov { 17597101eb4SOleg Nesterov taskstats_tgid_free(sig); 1761c5354deSMike Galbraith sched_autogroup_exit(sig); 177ea6d290cSOleg Nesterov kmem_cache_free(signal_cachep, sig); 178ea6d290cSOleg Nesterov } 179ea6d290cSOleg Nesterov 180ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig) 181ea6d290cSOleg Nesterov { 1821c5354deSMike Galbraith if (atomic_dec_and_test(&sig->sigcnt)) 183ea6d290cSOleg Nesterov free_signal_struct(sig); 184ea6d290cSOleg Nesterov } 185ea6d290cSOleg Nesterov 186158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 1871da177e4SLinus Torvalds { 188270f722dSEugene Teo WARN_ON(!tsk->exit_state); 1891da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 1901da177e4SLinus Torvalds WARN_ON(tsk == current); 1911da177e4SLinus Torvalds 192e0e81739SDavid Howells exit_creds(tsk); 19335df17c5SShailabh Nagar delayacct_tsk_free(tsk); 194ea6d290cSOleg Nesterov put_signal_struct(tsk->signal); 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 1971da177e4SLinus Torvalds free_task(tsk); 1981da177e4SLinus Torvalds } 19977c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct); 2001da177e4SLinus Torvalds 2012adee9b3SSuresh Siddha /* 2022adee9b3SSuresh Siddha * macro override instead of weak attribute alias, to workaround 2032adee9b3SSuresh Siddha * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions. 2042adee9b3SSuresh Siddha */ 2052adee9b3SSuresh Siddha #ifndef arch_task_cache_init 2062adee9b3SSuresh Siddha #define arch_task_cache_init() 2072adee9b3SSuresh Siddha #endif 20861c4628bSSuresh Siddha 2091da177e4SLinus Torvalds void __init fork_init(unsigned long mempages) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 2121da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 2131da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 2141da177e4SLinus Torvalds #endif 2151da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 2161da177e4SLinus Torvalds task_struct_cachep = 2171da177e4SLinus Torvalds kmem_cache_create("task_struct", sizeof(struct task_struct), 2182dff4405SVegard Nossum ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL); 2191da177e4SLinus Torvalds #endif 2201da177e4SLinus Torvalds 22161c4628bSSuresh Siddha /* do the arch specific task caches init */ 22261c4628bSSuresh Siddha arch_task_cache_init(); 22361c4628bSSuresh Siddha 2241da177e4SLinus Torvalds /* 2251da177e4SLinus Torvalds * The default maximum number of threads is set to a safe 2261da177e4SLinus Torvalds * value: the thread structures can take up at most half 2271da177e4SLinus Torvalds * of memory. 2281da177e4SLinus Torvalds */ 2291da177e4SLinus Torvalds max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* 2321da177e4SLinus Torvalds * we need to allow at least 20 threads to boot a system 2331da177e4SLinus Torvalds */ 2341da177e4SLinus Torvalds if (max_threads < 20) 2351da177e4SLinus Torvalds max_threads = 20; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 2381da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 2391da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 2401da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 24361c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst, 24461c4628bSSuresh Siddha struct task_struct *src) 24561c4628bSSuresh Siddha { 24661c4628bSSuresh Siddha *dst = *src; 24761c4628bSSuresh Siddha return 0; 24861c4628bSSuresh Siddha } 24961c4628bSSuresh Siddha 2501da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 2511da177e4SLinus Torvalds { 2521da177e4SLinus Torvalds struct task_struct *tsk; 2531da177e4SLinus Torvalds struct thread_info *ti; 2547c9f8861SEric Sandeen unsigned long *stackend; 255207205a2SEric Dumazet int node = tsk_fork_get_node(orig); 2563e26c149SPeter Zijlstra int err; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds prepare_to_copy(orig); 2591da177e4SLinus Torvalds 260504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 2611da177e4SLinus Torvalds if (!tsk) 2621da177e4SLinus Torvalds return NULL; 2631da177e4SLinus Torvalds 264b6a84016SEric Dumazet ti = alloc_thread_info_node(tsk, node); 2651da177e4SLinus Torvalds if (!ti) { 2661da177e4SLinus Torvalds free_task_struct(tsk); 2671da177e4SLinus Torvalds return NULL; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 27061c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 27161c4628bSSuresh Siddha if (err) 27261c4628bSSuresh Siddha goto out; 27361c4628bSSuresh Siddha 274f7e4217bSRoman Zippel tsk->stack = ti; 2753e26c149SPeter Zijlstra 27610ebffdeSAl Viro setup_thread_stack(tsk, orig); 2778e7cac79SAvi Kivity clear_user_return_notifier(tsk); 278f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 2797c9f8861SEric Sandeen stackend = end_of_stack(tsk); 2807c9f8861SEric Sandeen *stackend = STACK_END_MAGIC; /* for overflow detection */ 2811da177e4SLinus Torvalds 2820a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 2830a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 2840a425405SArjan van de Ven #endif 2850a425405SArjan van de Ven 286fb0a685cSDaniel Rebelo de Oliveira /* 287fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 288fb0a685cSDaniel Rebelo de Oliveira * parent) 289fb0a685cSDaniel Rebelo de Oliveira */ 2901da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 2916c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 2922056a782SJens Axboe tsk->btrace_seq = 0; 2936c5c9341SAlexey Dobriyan #endif 294a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 295c6a7f572SKOSAKI Motohiro 296c6a7f572SKOSAKI Motohiro account_kernel_stack(ti, 1); 297c6a7f572SKOSAKI Motohiro 2981da177e4SLinus Torvalds return tsk; 29961c4628bSSuresh Siddha 30061c4628bSSuresh Siddha out: 30161c4628bSSuresh Siddha free_thread_info(ti); 30261c4628bSSuresh Siddha free_task_struct(tsk); 30361c4628bSSuresh Siddha return NULL; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds #ifdef CONFIG_MMU 307a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 3081da177e4SLinus Torvalds { 309297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 3101da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 3111da177e4SLinus Torvalds int retval; 3121da177e4SLinus Torvalds unsigned long charge; 3131da177e4SLinus Torvalds struct mempolicy *pol; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 316ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 317ad339451SIngo Molnar /* 318ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 319ad339451SIngo Molnar */ 320ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 3217ee78232SHugh Dickins 3221da177e4SLinus Torvalds mm->locked_vm = 0; 3231da177e4SLinus Torvalds mm->mmap = NULL; 3241da177e4SLinus Torvalds mm->mmap_cache = NULL; 3251da177e4SLinus Torvalds mm->free_area_cache = oldmm->mmap_base; 3261363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 3271da177e4SLinus Torvalds mm->map_count = 0; 32894894244SRusty Russell cpumask_clear(mm_cpumask(mm)); 3291da177e4SLinus Torvalds mm->mm_rb = RB_ROOT; 3301da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 3311da177e4SLinus Torvalds rb_parent = NULL; 3321da177e4SLinus Torvalds pprev = &mm->mmap; 333f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 334f8af4da3SHugh Dickins if (retval) 335f8af4da3SHugh Dickins goto out; 336ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 337ba76149fSAndrea Arcangeli if (retval) 338ba76149fSAndrea Arcangeli goto out; 3391da177e4SLinus Torvalds 340297c5eeeSLinus Torvalds prev = NULL; 341fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 3421da177e4SLinus Torvalds struct file *file; 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 3453b6bfcdbSHugh Dickins long pages = vma_pages(mpnt); 3463b6bfcdbSHugh Dickins mm->total_vm -= pages; 347ab50b8edSHugh Dickins vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 3483b6bfcdbSHugh Dickins -pages); 3491da177e4SLinus Torvalds continue; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds charge = 0; 3521da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 3531da177e4SLinus Torvalds unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT; 3541da177e4SLinus Torvalds if (security_vm_enough_memory(len)) 3551da177e4SLinus Torvalds goto fail_nomem; 3561da177e4SLinus Torvalds charge = len; 3571da177e4SLinus Torvalds } 358e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 3591da177e4SLinus Torvalds if (!tmp) 3601da177e4SLinus Torvalds goto fail_nomem; 3611da177e4SLinus Torvalds *tmp = *mpnt; 3625beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 363846a16bfSLee Schermerhorn pol = mpol_dup(vma_policy(mpnt)); 3641da177e4SLinus Torvalds retval = PTR_ERR(pol); 3651da177e4SLinus Torvalds if (IS_ERR(pol)) 3661da177e4SLinus Torvalds goto fail_nomem_policy; 3671da177e4SLinus Torvalds vma_set_policy(tmp, pol); 368a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 3695beb4930SRik van Riel if (anon_vma_fork(tmp, mpnt)) 3705beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 3711da177e4SLinus Torvalds tmp->vm_flags &= ~VM_LOCKED; 372297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 3731da177e4SLinus Torvalds file = tmp->vm_file; 3741da177e4SLinus Torvalds if (file) { 375f3a43f3fSJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 376b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 377b88ed205SHugh Dickins 3781da177e4SLinus Torvalds get_file(file); 3791da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 3801da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3813d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 382b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 383b88ed205SHugh Dickins mapping->i_mmap_writable++; 384b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 385b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 3861da177e4SLinus Torvalds vma_prio_tree_add(tmp, mpnt); 387b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 3883d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* 392a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 393a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 394a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 395a1e78772SMel Gorman */ 396a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 397a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 398a1e78772SMel Gorman 399a1e78772SMel Gorman /* 4007ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 4011da177e4SLinus Torvalds */ 4021da177e4SLinus Torvalds *pprev = tmp; 4031da177e4SLinus Torvalds pprev = &tmp->vm_next; 404297c5eeeSLinus Torvalds tmp->vm_prev = prev; 405297c5eeeSLinus Torvalds prev = tmp; 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 4081da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 4091da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds mm->map_count++; 4120b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 4151da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds if (retval) 4181da177e4SLinus Torvalds goto out; 4191da177e4SLinus Torvalds } 420d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 421d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 4221da177e4SLinus Torvalds retval = 0; 4231da177e4SLinus Torvalds out: 4247ee78232SHugh Dickins up_write(&mm->mmap_sem); 425fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 4261da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 4271da177e4SLinus Torvalds return retval; 4285beb4930SRik van Riel fail_nomem_anon_vma_fork: 4295beb4930SRik van Riel mpol_put(pol); 4301da177e4SLinus Torvalds fail_nomem_policy: 4311da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 4321da177e4SLinus Torvalds fail_nomem: 4331da177e4SLinus Torvalds retval = -ENOMEM; 4341da177e4SLinus Torvalds vm_unacct_memory(charge); 4351da177e4SLinus Torvalds goto out; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 4391da177e4SLinus Torvalds { 4401da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 4411da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 4421da177e4SLinus Torvalds return -ENOMEM; 4431da177e4SLinus Torvalds return 0; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 4471da177e4SLinus Torvalds { 4485e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds #else 4511da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 4521da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 4531da177e4SLinus Torvalds #define mm_free_pgd(mm) 4541da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 4571da177e4SLinus Torvalds 458e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 4591da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 4601da177e4SLinus Torvalds 4614cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 4624cb0e11bSHidehiro Kawai 4634cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 4644cb0e11bSHidehiro Kawai { 4654cb0e11bSHidehiro Kawai default_dump_filter = 4664cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 4674cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 4684cb0e11bSHidehiro Kawai return 1; 4694cb0e11bSHidehiro Kawai } 4704cb0e11bSHidehiro Kawai 4714cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 4724cb0e11bSHidehiro Kawai 4731da177e4SLinus Torvalds #include <linux/init_task.h> 4741da177e4SLinus Torvalds 475858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 476858f0993SAlexey Dobriyan { 477858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 478858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 479858f0993SAlexey Dobriyan INIT_HLIST_HEAD(&mm->ioctx_list); 480858f0993SAlexey Dobriyan #endif 481858f0993SAlexey Dobriyan } 482858f0993SAlexey Dobriyan 48378fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 4861da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 4871da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 4881da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 489f8af4da3SHugh Dickins mm->flags = (current->mm) ? 490f8af4da3SHugh Dickins (current->mm->flags & MMF_INIT_MASK) : default_dump_filter; 491999d9fc1SOleg Nesterov mm->core_state = NULL; 4921da177e4SLinus Torvalds mm->nr_ptes = 0; 493d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 4941da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 4951da177e4SLinus Torvalds mm->free_area_cache = TASK_UNMAPPED_BASE; 4961363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 497858f0993SAlexey Dobriyan mm_init_aio(mm); 498cf475ad2SBalbir Singh mm_init_owner(mm, p); 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if (likely(!mm_alloc_pgd(mm))) { 5011da177e4SLinus Torvalds mm->def_flags = 0; 502cddb8a5cSAndrea Arcangeli mmu_notifier_mm_init(mm); 5031da177e4SLinus Torvalds return mm; 5041da177e4SLinus Torvalds } 50578fb7466SPavel Emelianov 5061da177e4SLinus Torvalds free_mm(mm); 5071da177e4SLinus Torvalds return NULL; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds /* 5111da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 5121da177e4SLinus Torvalds */ 5131da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds struct mm_struct *mm; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds mm = allocate_mm(); 518de03c72cSKOSAKI Motohiro if (!mm) 519de03c72cSKOSAKI Motohiro return NULL; 520de03c72cSKOSAKI Motohiro 5211da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 5226345d24dSLinus Torvalds mm_init_cpumask(mm); 5236345d24dSLinus Torvalds return mm_init(mm, current); 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds /* 5271da177e4SLinus Torvalds * Called when the last reference to the mm 5281da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 5291da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 5301da177e4SLinus Torvalds */ 5317ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 5321da177e4SLinus Torvalds { 5331da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 5341da177e4SLinus Torvalds mm_free_pgd(mm); 5351da177e4SLinus Torvalds destroy_context(mm); 536cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 537e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE 538e7a00c45SAndrea Arcangeli VM_BUG_ON(mm->pmd_huge_pte); 539e7a00c45SAndrea Arcangeli #endif 5401da177e4SLinus Torvalds free_mm(mm); 5411da177e4SLinus Torvalds } 5426d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* 5451da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 5461da177e4SLinus Torvalds */ 5471da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 5481da177e4SLinus Torvalds { 5490ae26f1bSAndrew Morton might_sleep(); 5500ae26f1bSAndrew Morton 5511da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 5521da177e4SLinus Torvalds exit_aio(mm); 5531c2fb7a4SAndrea Arcangeli ksm_exit(mm); 554ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 5551da177e4SLinus Torvalds exit_mmap(mm); 556925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 5571da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 5581da177e4SLinus Torvalds spin_lock(&mmlist_lock); 5591da177e4SLinus Torvalds list_del(&mm->mmlist); 5601da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds put_swap_token(mm); 563801460d0SHiroshi Shimamoto if (mm->binfmt) 564801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 5651da177e4SLinus Torvalds mmdrop(mm); 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 5691da177e4SLinus Torvalds 57038646013SJiri Slaby /* 57138646013SJiri Slaby * We added or removed a vma mapping the executable. The vmas are only mapped 57238646013SJiri Slaby * during exec and are not mapped with the mmap system call. 57338646013SJiri Slaby * Callers must hold down_write() on the mm's mmap_sem for these 57438646013SJiri Slaby */ 57538646013SJiri Slaby void added_exe_file_vma(struct mm_struct *mm) 57638646013SJiri Slaby { 57738646013SJiri Slaby mm->num_exe_file_vmas++; 57838646013SJiri Slaby } 57938646013SJiri Slaby 58038646013SJiri Slaby void removed_exe_file_vma(struct mm_struct *mm) 58138646013SJiri Slaby { 58238646013SJiri Slaby mm->num_exe_file_vmas--; 58338646013SJiri Slaby if ((mm->num_exe_file_vmas == 0) && mm->exe_file) { 58438646013SJiri Slaby fput(mm->exe_file); 58538646013SJiri Slaby mm->exe_file = NULL; 58638646013SJiri Slaby } 58738646013SJiri Slaby 58838646013SJiri Slaby } 58938646013SJiri Slaby 59038646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 59138646013SJiri Slaby { 59238646013SJiri Slaby if (new_exe_file) 59338646013SJiri Slaby get_file(new_exe_file); 59438646013SJiri Slaby if (mm->exe_file) 59538646013SJiri Slaby fput(mm->exe_file); 59638646013SJiri Slaby mm->exe_file = new_exe_file; 59738646013SJiri Slaby mm->num_exe_file_vmas = 0; 59838646013SJiri Slaby } 59938646013SJiri Slaby 60038646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 60138646013SJiri Slaby { 60238646013SJiri Slaby struct file *exe_file; 60338646013SJiri Slaby 60438646013SJiri Slaby /* We need mmap_sem to protect against races with removal of 60538646013SJiri Slaby * VM_EXECUTABLE vmas */ 60638646013SJiri Slaby down_read(&mm->mmap_sem); 60738646013SJiri Slaby exe_file = mm->exe_file; 60838646013SJiri Slaby if (exe_file) 60938646013SJiri Slaby get_file(exe_file); 61038646013SJiri Slaby up_read(&mm->mmap_sem); 61138646013SJiri Slaby return exe_file; 61238646013SJiri Slaby } 61338646013SJiri Slaby 61438646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 61538646013SJiri Slaby { 61638646013SJiri Slaby /* It's safe to write the exe_file pointer without exe_file_lock because 61738646013SJiri Slaby * this is called during fork when the task is not yet in /proc */ 61838646013SJiri Slaby newmm->exe_file = get_mm_exe_file(oldmm); 61938646013SJiri Slaby } 62038646013SJiri Slaby 6211da177e4SLinus Torvalds /** 6221da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 6231da177e4SLinus Torvalds * 624246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 6251da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 6261da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 6271da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 6281da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 6291da177e4SLinus Torvalds */ 6301da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds struct mm_struct *mm; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds task_lock(task); 6351da177e4SLinus Torvalds mm = task->mm; 6361da177e4SLinus Torvalds if (mm) { 637246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 6381da177e4SLinus Torvalds mm = NULL; 6391da177e4SLinus Torvalds else 6401da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds task_unlock(task); 6431da177e4SLinus Torvalds return mm; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 6481da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 6491da177e4SLinus Torvalds * error success whatever. 6501da177e4SLinus Torvalds * 6511da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 6521da177e4SLinus Torvalds * from the current process. 6531da177e4SLinus Torvalds * 6541da177e4SLinus Torvalds * This difference is important for error handling, when we 6551da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 6561da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 6571da177e4SLinus Torvalds * restoring the old one. . . 6581da177e4SLinus Torvalds * Eric Biederman 10 January 1998 6591da177e4SLinus Torvalds */ 6601da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 6611da177e4SLinus Torvalds { 6621da177e4SLinus Torvalds struct completion *vfork_done = tsk->vfork_done; 6631da177e4SLinus Torvalds 6648141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 6658141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 666fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 6678141c7f3SLinus Torvalds exit_robust_list(tsk); 668fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 669fc6b177dSPeter Zijlstra } 6708141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 671fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 6728141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 673fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 674fc6b177dSPeter Zijlstra } 6758141c7f3SLinus Torvalds #endif 676322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 677322a2c10SThomas Gleixner exit_pi_state_list(tsk); 6788141c7f3SLinus Torvalds #endif 6798141c7f3SLinus Torvalds 6801da177e4SLinus Torvalds /* Get rid of any cached register state */ 6811da177e4SLinus Torvalds deactivate_mm(tsk, mm); 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds /* notify parent sleeping on vfork() */ 6841da177e4SLinus Torvalds if (vfork_done) { 6851da177e4SLinus Torvalds tsk->vfork_done = NULL; 6861da177e4SLinus Torvalds complete(vfork_done); 6871da177e4SLinus Torvalds } 688fec1d011SRoland McGrath 689fec1d011SRoland McGrath /* 690fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 691fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 692fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 693fec1d011SRoland McGrath * trouble otherwise. Userland only wants this done for a sys_exit. 694fec1d011SRoland McGrath */ 6959c8a8228SEric Dumazet if (tsk->clear_child_tid) { 6969c8a8228SEric Dumazet if (!(tsk->flags & PF_SIGNALED) && 6979c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 6981da177e4SLinus Torvalds /* 6991da177e4SLinus Torvalds * We don't check the error code - if userspace has 7001da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 7011da177e4SLinus Torvalds */ 7029c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 7039c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 7049c8a8228SEric Dumazet 1, NULL, NULL, 0); 7059c8a8228SEric Dumazet } 7069c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 710a0a7ec30SJANAK DESAI /* 711a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 712a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 713a0a7ec30SJANAK DESAI */ 714402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk) 715a0a7ec30SJANAK DESAI { 716a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 717a0a7ec30SJANAK DESAI int err; 718a0a7ec30SJANAK DESAI 719a0a7ec30SJANAK DESAI if (!oldmm) 720a0a7ec30SJANAK DESAI return NULL; 721a0a7ec30SJANAK DESAI 722a0a7ec30SJANAK DESAI mm = allocate_mm(); 723a0a7ec30SJANAK DESAI if (!mm) 724a0a7ec30SJANAK DESAI goto fail_nomem; 725a0a7ec30SJANAK DESAI 726a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 7276345d24dSLinus Torvalds mm_init_cpumask(mm); 728a0a7ec30SJANAK DESAI 7297602bdf2SAshwin Chaugule /* Initializing for Swap token stuff */ 7307602bdf2SAshwin Chaugule mm->token_priority = 0; 7317602bdf2SAshwin Chaugule mm->last_interval = 0; 7327602bdf2SAshwin Chaugule 733e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE 734e7a00c45SAndrea Arcangeli mm->pmd_huge_pte = NULL; 735e7a00c45SAndrea Arcangeli #endif 736e7a00c45SAndrea Arcangeli 73778fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 738a0a7ec30SJANAK DESAI goto fail_nomem; 739a0a7ec30SJANAK DESAI 740a0a7ec30SJANAK DESAI if (init_new_context(tsk, mm)) 741a0a7ec30SJANAK DESAI goto fail_nocontext; 742a0a7ec30SJANAK DESAI 743925d1c40SMatt Helsley dup_mm_exe_file(oldmm, mm); 744925d1c40SMatt Helsley 745a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 746a0a7ec30SJANAK DESAI if (err) 747a0a7ec30SJANAK DESAI goto free_pt; 748a0a7ec30SJANAK DESAI 749a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 750a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 751a0a7ec30SJANAK DESAI 752801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 753801460d0SHiroshi Shimamoto goto free_pt; 754801460d0SHiroshi Shimamoto 755a0a7ec30SJANAK DESAI return mm; 756a0a7ec30SJANAK DESAI 757a0a7ec30SJANAK DESAI free_pt: 758801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 759801460d0SHiroshi Shimamoto mm->binfmt = NULL; 760a0a7ec30SJANAK DESAI mmput(mm); 761a0a7ec30SJANAK DESAI 762a0a7ec30SJANAK DESAI fail_nomem: 763a0a7ec30SJANAK DESAI return NULL; 764a0a7ec30SJANAK DESAI 765a0a7ec30SJANAK DESAI fail_nocontext: 766a0a7ec30SJANAK DESAI /* 767a0a7ec30SJANAK DESAI * If init_new_context() failed, we cannot use mmput() to free the mm 768a0a7ec30SJANAK DESAI * because it calls destroy_context() 769a0a7ec30SJANAK DESAI */ 770a0a7ec30SJANAK DESAI mm_free_pgd(mm); 771a0a7ec30SJANAK DESAI free_mm(mm); 772a0a7ec30SJANAK DESAI return NULL; 773a0a7ec30SJANAK DESAI } 774a0a7ec30SJANAK DESAI 7751da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 7761da177e4SLinus Torvalds { 7771da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 7781da177e4SLinus Torvalds int retval; 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 7811da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 78217406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 78317406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 78417406b82SMandeep Singh Baines #endif 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds tsk->mm = NULL; 7871da177e4SLinus Torvalds tsk->active_mm = NULL; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds /* 7901da177e4SLinus Torvalds * Are we cloning a kernel thread? 7911da177e4SLinus Torvalds * 7921da177e4SLinus Torvalds * We need to steal a active VM for that.. 7931da177e4SLinus Torvalds */ 7941da177e4SLinus Torvalds oldmm = current->mm; 7951da177e4SLinus Torvalds if (!oldmm) 7961da177e4SLinus Torvalds return 0; 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 7991da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 8001da177e4SLinus Torvalds mm = oldmm; 8011da177e4SLinus Torvalds goto good_mm; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds retval = -ENOMEM; 805a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 8061da177e4SLinus Torvalds if (!mm) 8071da177e4SLinus Torvalds goto fail_nomem; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds good_mm: 8107602bdf2SAshwin Chaugule /* Initializing for Swap token stuff */ 8117602bdf2SAshwin Chaugule mm->token_priority = 0; 8127602bdf2SAshwin Chaugule mm->last_interval = 0; 8137602bdf2SAshwin Chaugule 8141da177e4SLinus Torvalds tsk->mm = mm; 8151da177e4SLinus Torvalds tsk->active_mm = mm; 8161da177e4SLinus Torvalds return 0; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds fail_nomem: 8191da177e4SLinus Torvalds return retval; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 822a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 8231da177e4SLinus Torvalds { 824498052bbSAl Viro struct fs_struct *fs = current->fs; 8251da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 826498052bbSAl Viro /* tsk->fs is already what we want */ 8272a4419b5SNick Piggin spin_lock(&fs->lock); 828498052bbSAl Viro if (fs->in_exec) { 8292a4419b5SNick Piggin spin_unlock(&fs->lock); 830498052bbSAl Viro return -EAGAIN; 831498052bbSAl Viro } 832498052bbSAl Viro fs->users++; 8332a4419b5SNick Piggin spin_unlock(&fs->lock); 8341da177e4SLinus Torvalds return 0; 8351da177e4SLinus Torvalds } 836498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 8371da177e4SLinus Torvalds if (!tsk->fs) 8381da177e4SLinus Torvalds return -ENOMEM; 8391da177e4SLinus Torvalds return 0; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 842a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 843a016f338SJANAK DESAI { 844a016f338SJANAK DESAI struct files_struct *oldf, *newf; 845a016f338SJANAK DESAI int error = 0; 846a016f338SJANAK DESAI 847a016f338SJANAK DESAI /* 848a016f338SJANAK DESAI * A background process may not have any files ... 849a016f338SJANAK DESAI */ 850a016f338SJANAK DESAI oldf = current->files; 851a016f338SJANAK DESAI if (!oldf) 852a016f338SJANAK DESAI goto out; 853a016f338SJANAK DESAI 854a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 855a016f338SJANAK DESAI atomic_inc(&oldf->count); 856a016f338SJANAK DESAI goto out; 857a016f338SJANAK DESAI } 858a016f338SJANAK DESAI 859a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 860a016f338SJANAK DESAI if (!newf) 861a016f338SJANAK DESAI goto out; 862a016f338SJANAK DESAI 863a016f338SJANAK DESAI tsk->files = newf; 864a016f338SJANAK DESAI error = 0; 865a016f338SJANAK DESAI out: 866a016f338SJANAK DESAI return error; 867a016f338SJANAK DESAI } 868a016f338SJANAK DESAI 869fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 870fd0928dfSJens Axboe { 871fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 872fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 873fd0928dfSJens Axboe 874fd0928dfSJens Axboe if (!ioc) 875fd0928dfSJens Axboe return 0; 876fadad878SJens Axboe /* 877fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 878fadad878SJens Axboe */ 879fadad878SJens Axboe if (clone_flags & CLONE_IO) { 880fadad878SJens Axboe tsk->io_context = ioc_task_link(ioc); 881fadad878SJens Axboe if (unlikely(!tsk->io_context)) 882fadad878SJens Axboe return -ENOMEM; 883fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 884fd0928dfSJens Axboe tsk->io_context = alloc_io_context(GFP_KERNEL, -1); 885fd0928dfSJens Axboe if (unlikely(!tsk->io_context)) 886fd0928dfSJens Axboe return -ENOMEM; 887fd0928dfSJens Axboe 888fd0928dfSJens Axboe tsk->io_context->ioprio = ioc->ioprio; 889fd0928dfSJens Axboe } 890fd0928dfSJens Axboe #endif 891fd0928dfSJens Axboe return 0; 892fd0928dfSJens Axboe } 893fd0928dfSJens Axboe 894a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 8951da177e4SLinus Torvalds { 8961da177e4SLinus Torvalds struct sighand_struct *sig; 8971da177e4SLinus Torvalds 89860348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 8991da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 9001da177e4SLinus Torvalds return 0; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 903e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 9041da177e4SLinus Torvalds if (!sig) 9051da177e4SLinus Torvalds return -ENOMEM; 9061da177e4SLinus Torvalds atomic_set(&sig->count, 1); 9071da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 9081da177e4SLinus Torvalds return 0; 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds 911a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 912c81addc9SOleg Nesterov { 913c81addc9SOleg Nesterov if (atomic_dec_and_test(&sighand->count)) 914c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 915c81addc9SOleg Nesterov } 916c81addc9SOleg Nesterov 917f06febc9SFrank Mayhar 918f06febc9SFrank Mayhar /* 919f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 920f06febc9SFrank Mayhar */ 921f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 922f06febc9SFrank Mayhar { 92378d7d407SJiri Slaby unsigned long cpu_limit; 92478d7d407SJiri Slaby 925f06febc9SFrank Mayhar /* Thread group counters. */ 926f06febc9SFrank Mayhar thread_group_cputime_init(sig); 927f06febc9SFrank Mayhar 92878d7d407SJiri Slaby cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 92978d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 93078d7d407SJiri Slaby sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); 9316279a751SOleg Nesterov sig->cputimer.running = 1; 9326279a751SOleg Nesterov } 9336279a751SOleg Nesterov 934f06febc9SFrank Mayhar /* The timer lists. */ 935f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 936f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 937f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 938f06febc9SFrank Mayhar } 939f06febc9SFrank Mayhar 940a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 9411da177e4SLinus Torvalds { 9421da177e4SLinus Torvalds struct signal_struct *sig; 9431da177e4SLinus Torvalds 9444ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 945490dea45SPeter Zijlstra return 0; 9466279a751SOleg Nesterov 947a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 9481da177e4SLinus Torvalds tsk->signal = sig; 9491da177e4SLinus Torvalds if (!sig) 9501da177e4SLinus Torvalds return -ENOMEM; 9511da177e4SLinus Torvalds 952b3ac022cSOleg Nesterov sig->nr_threads = 1; 9531da177e4SLinus Torvalds atomic_set(&sig->live, 1); 954b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 9551da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 956b3bfa0cbSSukadev Bhattiprolu if (clone_flags & CLONE_NEWPID) 957b3bfa0cbSSukadev Bhattiprolu sig->flags |= SIGNAL_UNKILLABLE; 958db51aeccSOleg Nesterov sig->curr_target = tsk; 9591da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 9601da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 9611da177e4SLinus Torvalds 962c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 9631da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds task_lock(current->group_leader); 9661da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 9671da177e4SLinus Torvalds task_unlock(current->group_leader); 9681da177e4SLinus Torvalds 9696279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 9706279a751SOleg Nesterov 971522ed776SMiloslav Trmac tty_audit_fork(sig); 9725091faa4SMike Galbraith sched_autogroup_fork(sig); 973522ed776SMiloslav Trmac 9744714d1d3SBen Blum #ifdef CONFIG_CGROUPS 9754714d1d3SBen Blum init_rwsem(&sig->threadgroup_fork_lock); 9764714d1d3SBen Blum #endif 9774714d1d3SBen Blum 97828b83c51SKOSAKI Motohiro sig->oom_adj = current->signal->oom_adj; 979a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 980dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 98128b83c51SKOSAKI Motohiro 9829b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 9839b1bf12dSKOSAKI Motohiro 9841da177e4SLinus Torvalds return 0; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 987a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p) 9881da177e4SLinus Torvalds { 9891da177e4SLinus Torvalds unsigned long new_flags = p->flags; 9901da177e4SLinus Torvalds 99121aa9af0STejun Heo new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER); 9921da177e4SLinus Torvalds new_flags |= PF_FORKNOEXEC; 99309a05394SRoland McGrath new_flags |= PF_STARTING; 9941da177e4SLinus Torvalds p->flags = new_flags; 9952e131895SRafael J. Wysocki clear_freeze_flag(p); 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 99817da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 9991da177e4SLinus Torvalds { 10001da177e4SLinus Torvalds current->clear_child_tid = tidptr; 10011da177e4SLinus Torvalds 1002b488893aSPavel Emelyanov return task_pid_vnr(current); 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 1005a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 100623f78d4aSIngo Molnar { 10071d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1008e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1009732375c6SDima Zavin plist_head_init(&p->pi_waiters); 101023f78d4aSIngo Molnar p->pi_blocked_on = NULL; 101123f78d4aSIngo Molnar #endif 101223f78d4aSIngo Molnar } 101323f78d4aSIngo Molnar 1014cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER 1015cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 1016cf475ad2SBalbir Singh { 1017cf475ad2SBalbir Singh mm->owner = p; 1018cf475ad2SBalbir Singh } 1019cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */ 1020cf475ad2SBalbir Singh 10211da177e4SLinus Torvalds /* 1022f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1023f06febc9SFrank Mayhar */ 1024f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1025f06febc9SFrank Mayhar { 1026*64861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 1027*64861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1028f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1029f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1030f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1031f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1032f06febc9SFrank Mayhar } 1033f06febc9SFrank Mayhar 1034f06febc9SFrank Mayhar /* 10351da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 10361da177e4SLinus Torvalds * but does not actually start it yet. 10371da177e4SLinus Torvalds * 10381da177e4SLinus Torvalds * It copies the registers, and all the appropriate 10391da177e4SLinus Torvalds * parts of the process environment (as per the clone 10401da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 10411da177e4SLinus Torvalds */ 104236c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 10431da177e4SLinus Torvalds unsigned long stack_start, 10441da177e4SLinus Torvalds struct pt_regs *regs, 10451da177e4SLinus Torvalds unsigned long stack_size, 10461da177e4SLinus Torvalds int __user *child_tidptr, 104709a05394SRoland McGrath struct pid *pid, 104809a05394SRoland McGrath int trace) 10491da177e4SLinus Torvalds { 10501da177e4SLinus Torvalds int retval; 1051a24efe62SMariusz Kozlowski struct task_struct *p; 1052b4f48b63SPaul Menage int cgroup_callbacks_done = 0; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 10551da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* 10581da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 10591da177e4SLinus Torvalds * can only be started up within the thread group. 10601da177e4SLinus Torvalds */ 10611da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 10621da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds /* 10651da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 10661da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 10671da177e4SLinus Torvalds * for various simplifications in other code. 10681da177e4SLinus Torvalds */ 10691da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 10701da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 10711da177e4SLinus Torvalds 1072123be07bSSukadev Bhattiprolu /* 1073123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1074123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1075123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1076123be07bSSukadev Bhattiprolu * from creating siblings. 1077123be07bSSukadev Bhattiprolu */ 1078123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1079123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1080123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1081123be07bSSukadev Bhattiprolu 10821da177e4SLinus Torvalds retval = security_task_create(clone_flags); 10831da177e4SLinus Torvalds if (retval) 10841da177e4SLinus Torvalds goto fork_out; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds retval = -ENOMEM; 10871da177e4SLinus Torvalds p = dup_task_struct(current); 10881da177e4SLinus Torvalds if (!p) 10891da177e4SLinus Torvalds goto fork_out; 10901da177e4SLinus Torvalds 1091f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1092f7e8b616SSteven Rostedt 1093bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1094bea493a0SPeter Zijlstra 1095d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1096de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1097de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1098de30a2b3SIngo Molnar #endif 10991da177e4SLinus Torvalds retval = -EAGAIN; 11003b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 110178d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 11021da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) && 110318b6e041SSerge Hallyn p->real_cred->user != INIT_USER) 11041da177e4SLinus Torvalds goto bad_fork_free; 11051da177e4SLinus Torvalds } 110672fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 11071da177e4SLinus Torvalds 1108f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1109f1752eecSDavid Howells if (retval < 0) 1110f1752eecSDavid Howells goto bad_fork_free; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds /* 11131da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 11141da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 11151da177e4SLinus Torvalds * to stop root fork bombs. 11161da177e4SLinus Torvalds */ 111704ec93feSLi Zefan retval = -EAGAIN; 11181da177e4SLinus Torvalds if (nr_threads >= max_threads) 11191da177e4SLinus Torvalds goto bad_fork_cleanup_count; 11201da177e4SLinus Torvalds 1121a1261f54SAl Viro if (!try_module_get(task_thread_info(p)->exec_domain->module)) 11221da177e4SLinus Torvalds goto bad_fork_cleanup_count; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds p->did_exec = 0; 1125ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 11261da177e4SLinus Torvalds copy_flags(clone_flags, p); 11271da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 11281da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1129f41d911fSPaul E. McKenney rcu_copy_process(p); 11301da177e4SLinus Torvalds p->vfork_done = NULL; 11311da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds init_sigpending(&p->pending); 11341da177e4SLinus Torvalds 1135*64861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 1136*64861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 1137d99ca3b9SHidetoshi Seto #ifndef CONFIG_VIRT_CPU_ACCOUNTING 1138*64861634SMartin Schwidefsky p->prev_utime = p->prev_stime = 0; 1139d99ca3b9SHidetoshi Seto #endif 1140a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1141a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1142a3a2e76cSKAMEZAWA Hiroyuki #endif 1143172ba844SBalbir Singh 11446976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 11456976675dSArjan van de Ven 11465995477aSAndrea Righi task_io_accounting_init(&p->ioac); 11471da177e4SLinus Torvalds acct_clear_integrals(p); 11481da177e4SLinus Torvalds 1149f06febc9SFrank Mayhar posix_cpu_timers_init(p); 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&p->start_time); 1152924b42d5STomas Janousek p->real_start_time = p->start_time; 1153924b42d5STomas Janousek monotonic_to_bootbased(&p->real_start_time); 11541da177e4SLinus Torvalds p->io_context = NULL; 11551da177e4SLinus Torvalds p->audit_context = NULL; 11564714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 11574714d1d3SBen Blum threadgroup_fork_read_lock(current); 1158b4f48b63SPaul Menage cgroup_fork(p); 11591da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1160846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 11611da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 11621da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 11631da177e4SLinus Torvalds p->mempolicy = NULL; 1164b4f48b63SPaul Menage goto bad_fork_cleanup_cgroup; 11651da177e4SLinus Torvalds } 1166c61afb18SPaul Jackson mpol_fix_fork_child_flag(p); 11671da177e4SLinus Torvalds #endif 1168778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1169778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1170778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1171778d3b0fSMichal Hocko #endif 1172de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1173de30a2b3SIngo Molnar p->irq_events = 0; 1174b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW 1175b36e4758SRussell King p->hardirqs_enabled = 1; 1176b36e4758SRussell King #else 1177de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1178b36e4758SRussell King #endif 1179de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1180de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1181de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1182de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1183de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1184de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1185de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1186de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1187de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1188de30a2b3SIngo Molnar p->hardirq_context = 0; 1189de30a2b3SIngo Molnar p->softirq_context = 0; 1190de30a2b3SIngo Molnar #endif 1191fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1192fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1193fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1194fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1195fbb9ce95SIngo Molnar #endif 11961da177e4SLinus Torvalds 1197408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1198408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1199408894eeSIngo Molnar #endif 1200569b846dSKAMEZAWA Hiroyuki #ifdef CONFIG_CGROUP_MEM_RES_CTLR 1201569b846dSKAMEZAWA Hiroyuki p->memcg_batch.do_batch = 0; 1202569b846dSKAMEZAWA Hiroyuki p->memcg_batch.memcg = NULL; 1203569b846dSKAMEZAWA Hiroyuki #endif 12040f481406SMarkus Metzger 12053c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 12063e51e3edSSamir Bellabes sched_fork(p); 12076ab423e0SPeter Zijlstra 1208cdd6c482SIngo Molnar retval = perf_event_init_task(p); 12096ab423e0SPeter Zijlstra if (retval) 12106ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1211fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1212fb0a685cSDaniel Rebelo de Oliveira if (retval) 1213f1752eecSDavid Howells goto bad_fork_cleanup_policy; 12141da177e4SLinus Torvalds /* copy all the process information */ 1215fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1216fb0a685cSDaniel Rebelo de Oliveira if (retval) 12171da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1218fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1219fb0a685cSDaniel Rebelo de Oliveira if (retval) 12201da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1221fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1222fb0a685cSDaniel Rebelo de Oliveira if (retval) 12231da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1224fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1225fb0a685cSDaniel Rebelo de Oliveira if (retval) 12261da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1227fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1228fb0a685cSDaniel Rebelo de Oliveira if (retval) 12291da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1230fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1231fb0a685cSDaniel Rebelo de Oliveira if (retval) 12321da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1233fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1234fb0a685cSDaniel Rebelo de Oliveira if (retval) 1235d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1236fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1237fb0a685cSDaniel Rebelo de Oliveira if (retval) 1238fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 12396f2c55b8SAlexey Dobriyan retval = copy_thread(clone_flags, stack_start, stack_size, p, regs); 12401da177e4SLinus Torvalds if (retval) 1241fd0928dfSJens Axboe goto bad_fork_cleanup_io; 12421da177e4SLinus Torvalds 1243425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1244425fb2b4SPavel Emelyanov retval = -ENOMEM; 124561bce0f1SEric W. Biederman pid = alloc_pid(p->nsproxy->pid_ns); 1246425fb2b4SPavel Emelyanov if (!pid) 1247fd0928dfSJens Axboe goto bad_fork_cleanup_io; 1248425fb2b4SPavel Emelyanov } 1249425fb2b4SPavel Emelyanov 1250425fb2b4SPavel Emelyanov p->pid = pid_nr(pid); 1251425fb2b4SPavel Emelyanov p->tgid = p->pid; 1252425fb2b4SPavel Emelyanov if (clone_flags & CLONE_THREAD) 1253425fb2b4SPavel Emelyanov p->tgid = current->tgid; 1254425fb2b4SPavel Emelyanov 12551da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 12561da177e4SLinus Torvalds /* 12571da177e4SLinus Torvalds * Clear TID on mm_release()? 12581da177e4SLinus Torvalds */ 12591da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 126073c10101SJens Axboe #ifdef CONFIG_BLOCK 126173c10101SJens Axboe p->plug = NULL; 126273c10101SJens Axboe #endif 126342b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 12648f17d3a5SIngo Molnar p->robust_list = NULL; 12658f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 12668f17d3a5SIngo Molnar p->compat_robust_list = NULL; 12678f17d3a5SIngo Molnar #endif 1268c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1269c87e2837SIngo Molnar p->pi_state_cache = NULL; 127042b2dd0aSAlexey Dobriyan #endif 12711da177e4SLinus Torvalds /* 1272f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1273f9a3879aSGOTO Masanori */ 1274f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 1275f9a3879aSGOTO Masanori p->sas_ss_sp = p->sas_ss_size = 0; 1276f9a3879aSGOTO Masanori 1277f9a3879aSGOTO Masanori /* 12786580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 12796580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 12801da177e4SLinus Torvalds */ 12816580807dSOleg Nesterov user_disable_single_step(p); 12821da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1283ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1284ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1285ed75e8d5SLaurent Vivier #endif 12869745512cSArjan van de Ven clear_all_latency_tracing(p); 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds /* ok, now we should be set up.. */ 12891da177e4SLinus Torvalds p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL); 12901da177e4SLinus Torvalds p->pdeath_signal = 0; 12911da177e4SLinus Torvalds p->exit_state = 0; 12921da177e4SLinus Torvalds 12939d823e8fSWu Fengguang p->nr_dirtied = 0; 12949d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 12959d823e8fSWu Fengguang 12961da177e4SLinus Torvalds /* 12971da177e4SLinus Torvalds * Ok, make it visible to the rest of the system. 12981da177e4SLinus Torvalds * We dont wake it up yet. 12991da177e4SLinus Torvalds */ 13001da177e4SLinus Torvalds p->group_leader = p; 130147e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 13021da177e4SLinus Torvalds 1303b4f48b63SPaul Menage /* Now that the task is set up, run cgroup callbacks if 1304b4f48b63SPaul Menage * necessary. We need to run them before the task is visible 1305b4f48b63SPaul Menage * on the tasklist. */ 1306b4f48b63SPaul Menage cgroup_fork_callbacks(p); 1307b4f48b63SPaul Menage cgroup_callbacks_done = 1; 1308b4f48b63SPaul Menage 13091da177e4SLinus Torvalds /* Need tasklist lock for parent etc handling! */ 13101da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 13132d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 13141da177e4SLinus Torvalds p->real_parent = current->real_parent; 13152d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 13162d5516cbSOleg Nesterov } else { 13171da177e4SLinus Torvalds p->real_parent = current; 13182d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 13192d5516cbSOleg Nesterov } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 13224a2c7a78SOleg Nesterov 13234a2c7a78SOleg Nesterov /* 13244a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 13254a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 13264a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 13274a2c7a78SOleg Nesterov * it's process group. 13284a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 13294a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 13304a2c7a78SOleg Nesterov */ 13314a2c7a78SOleg Nesterov recalc_sigpending(); 13324a2c7a78SOleg Nesterov if (signal_pending(current)) { 13334a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 13344a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 13354a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 1336f7e8b616SSteven Rostedt goto bad_fork_free_pid; 13374a2c7a78SOleg Nesterov } 13384a2c7a78SOleg Nesterov 13393f17da69SOleg Nesterov if (clone_flags & CLONE_THREAD) { 1340b3ac022cSOleg Nesterov current->signal->nr_threads++; 13414ab6c083SOleg Nesterov atomic_inc(¤t->signal->live); 1342b3ac022cSOleg Nesterov atomic_inc(¤t->signal->sigcnt); 13431da177e4SLinus Torvalds p->group_leader = current->group_leader; 134447e65328SOleg Nesterov list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group); 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 134773b9ebfeSOleg Nesterov if (likely(p->pid)) { 13484b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds if (thread_group_leader(p)) { 135145a68628SEric W. Biederman if (is_child_reaper(pid)) 135230e49c26SPavel Emelyanov p->nsproxy->pid_ns->child_reaper = p; 13535cd17569SEric W. Biederman 1354fea9d175SOleg Nesterov p->signal->leader_pid = pid; 13559c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 13565cd17569SEric W. Biederman attach_pid(p, PIDTYPE_PGID, task_pgrp(current)); 13575cd17569SEric W. Biederman attach_pid(p, PIDTYPE_SID, task_session(current)); 13589cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 13595e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 1360909ea964SChristoph Lameter __this_cpu_inc(process_counts); 13611da177e4SLinus Torvalds } 136285868995SSukadev Bhattiprolu attach_pid(p, PIDTYPE_PID, pid); 13631da177e4SLinus Torvalds nr_threads++; 136473b9ebfeSOleg Nesterov } 136573b9ebfeSOleg Nesterov 13661da177e4SLinus Torvalds total_forks++; 13673f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 13681da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 1369c13cf856SAndrew Morton proc_fork_connector(p); 1370817929ecSPaul Menage cgroup_post_fork(p); 13714714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 13724714d1d3SBen Blum threadgroup_fork_read_unlock(current); 1373cdd6c482SIngo Molnar perf_event_fork(p); 13741da177e4SLinus Torvalds return p; 13751da177e4SLinus Torvalds 1376425fb2b4SPavel Emelyanov bad_fork_free_pid: 1377425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1378425fb2b4SPavel Emelyanov free_pid(pid); 1379fd0928dfSJens Axboe bad_fork_cleanup_io: 1380b69f2292SLouis Rilling if (p->io_context) 1381b69f2292SLouis Rilling exit_io_context(p); 1382ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1383444f378bSLinus Torvalds exit_task_namespaces(p); 13841da177e4SLinus Torvalds bad_fork_cleanup_mm: 1385c9f01245SDavid Rientjes if (p->mm) 13861da177e4SLinus Torvalds mmput(p->mm); 13871da177e4SLinus Torvalds bad_fork_cleanup_signal: 13884ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 13891c5354deSMike Galbraith free_signal_struct(p->signal); 13901da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1391a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 13921da177e4SLinus Torvalds bad_fork_cleanup_fs: 13931da177e4SLinus Torvalds exit_fs(p); /* blocking */ 13941da177e4SLinus Torvalds bad_fork_cleanup_files: 13951da177e4SLinus Torvalds exit_files(p); /* blocking */ 13961da177e4SLinus Torvalds bad_fork_cleanup_semundo: 13971da177e4SLinus Torvalds exit_sem(p); 13981da177e4SLinus Torvalds bad_fork_cleanup_audit: 13991da177e4SLinus Torvalds audit_free(p); 14001da177e4SLinus Torvalds bad_fork_cleanup_policy: 1401cdd6c482SIngo Molnar perf_event_free_task(p); 14021da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1403f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1404b4f48b63SPaul Menage bad_fork_cleanup_cgroup: 14051da177e4SLinus Torvalds #endif 14064714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 14074714d1d3SBen Blum threadgroup_fork_read_unlock(current); 1408b4f48b63SPaul Menage cgroup_exit(p, cgroup_callbacks_done); 140935df17c5SShailabh Nagar delayacct_tsk_free(p); 1410a1261f54SAl Viro module_put(task_thread_info(p)->exec_domain->module); 14111da177e4SLinus Torvalds bad_fork_cleanup_count: 1412d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1413e0e81739SDavid Howells exit_creds(p); 14141da177e4SLinus Torvalds bad_fork_free: 14151da177e4SLinus Torvalds free_task(p); 1416fe7d37d1SOleg Nesterov fork_out: 1417fe7d37d1SOleg Nesterov return ERR_PTR(retval); 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14206b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs) 14211da177e4SLinus Torvalds { 14221da177e4SLinus Torvalds memset(regs, 0, sizeof(struct pt_regs)); 14231da177e4SLinus Torvalds return regs; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 1426f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1427f106eee1SOleg Nesterov { 1428f106eee1SOleg Nesterov enum pid_type type; 1429f106eee1SOleg Nesterov 1430f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1431f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1432f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1433f106eee1SOleg Nesterov } 1434f106eee1SOleg Nesterov } 1435f106eee1SOleg Nesterov 14369abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu) 14371da177e4SLinus Torvalds { 143836c8b586SIngo Molnar struct task_struct *task; 14391da177e4SLinus Torvalds struct pt_regs regs; 14401da177e4SLinus Torvalds 144130e49c26SPavel Emelyanov task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, 144209a05394SRoland McGrath &init_struct_pid, 0); 1443f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1444f106eee1SOleg Nesterov init_idle_pids(task->pids); 14451da177e4SLinus Torvalds init_idle(task, cpu); 1446f106eee1SOleg Nesterov } 144773b9ebfeSOleg Nesterov 14481da177e4SLinus Torvalds return task; 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds /* 14521da177e4SLinus Torvalds * Ok, this is the main fork-routine. 14531da177e4SLinus Torvalds * 14541da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 14551da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 14561da177e4SLinus Torvalds */ 14571da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 14581da177e4SLinus Torvalds unsigned long stack_start, 14591da177e4SLinus Torvalds struct pt_regs *regs, 14601da177e4SLinus Torvalds unsigned long stack_size, 14611da177e4SLinus Torvalds int __user *parent_tidptr, 14621da177e4SLinus Torvalds int __user *child_tidptr) 14631da177e4SLinus Torvalds { 14641da177e4SLinus Torvalds struct task_struct *p; 14651da177e4SLinus Torvalds int trace = 0; 146692476d7fSEric W. Biederman long nr; 14671da177e4SLinus Torvalds 1468bdff746aSAndrew Morton /* 146918b6e041SSerge Hallyn * Do some preliminary argument and permissions checking before we 147018b6e041SSerge Hallyn * actually start allocating stuff 147118b6e041SSerge Hallyn */ 147218b6e041SSerge Hallyn if (clone_flags & CLONE_NEWUSER) { 147318b6e041SSerge Hallyn if (clone_flags & CLONE_THREAD) 147418b6e041SSerge Hallyn return -EINVAL; 147518b6e041SSerge Hallyn /* hopefully this check will go away when userns support is 147618b6e041SSerge Hallyn * complete 147718b6e041SSerge Hallyn */ 14787657d904SSerge E. Hallyn if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) || 14797657d904SSerge E. Hallyn !capable(CAP_SETGID)) 148018b6e041SSerge Hallyn return -EPERM; 148118b6e041SSerge Hallyn } 148218b6e041SSerge Hallyn 148318b6e041SSerge Hallyn /* 14844b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 14854b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 14864b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 14874b9d33e6STejun Heo * for the type of forking is enabled. 148809a05394SRoland McGrath */ 14894b9d33e6STejun Heo if (likely(user_mode(regs)) && !(clone_flags & CLONE_UNTRACED)) { 14904b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 14914b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 14924b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 14934b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 14944b9d33e6STejun Heo else 14954b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 14964b9d33e6STejun Heo 14974b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 14984b9d33e6STejun Heo trace = 0; 14994b9d33e6STejun Heo } 15001da177e4SLinus Torvalds 1501a6f5e063SSukadev Bhattiprolu p = copy_process(clone_flags, stack_start, regs, stack_size, 150209a05394SRoland McGrath child_tidptr, NULL, trace); 15031da177e4SLinus Torvalds /* 15041da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 15051da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 15061da177e4SLinus Torvalds */ 15071da177e4SLinus Torvalds if (!IS_ERR(p)) { 15081da177e4SLinus Torvalds struct completion vfork; 15091da177e4SLinus Torvalds 15100a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 15110a16b607SMathieu Desnoyers 15126c5f3e7bSPavel Emelyanov nr = task_pid_vnr(p); 151330e49c26SPavel Emelyanov 151430e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 151530e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1516a6f5e063SSukadev Bhattiprolu 15171da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 15181da177e4SLinus Torvalds p->vfork_done = &vfork; 15191da177e4SLinus Torvalds init_completion(&vfork); 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 1522a64e6494SAl Viro audit_finish_fork(p); 15234b9d33e6STejun Heo 15244b9d33e6STejun Heo /* 152509a05394SRoland McGrath * We set PF_STARTING at creation in case tracing wants to 152609a05394SRoland McGrath * use this to distinguish a fully live task from one that 15274b9d33e6STejun Heo * hasn't finished SIGSTOP raising yet. Now we clear it 15284b9d33e6STejun Heo * and set the child going. 152909a05394SRoland McGrath */ 153009a05394SRoland McGrath p->flags &= ~PF_STARTING; 153109a05394SRoland McGrath 15323e51e3edSSamir Bellabes wake_up_new_task(p); 15331da177e4SLinus Torvalds 15344b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 15354b9d33e6STejun Heo if (unlikely(trace)) 15364b9d33e6STejun Heo ptrace_event(trace, nr); 153709a05394SRoland McGrath 15381da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1539ba96a0c8SRafael J. Wysocki freezer_do_not_count(); 15401da177e4SLinus Torvalds wait_for_completion(&vfork); 1541ba96a0c8SRafael J. Wysocki freezer_count(); 1542a288eeccSTejun Heo ptrace_event(PTRACE_EVENT_VFORK_DONE, nr); 15439f59ce5dSChuck Ebbert } 15441da177e4SLinus Torvalds } else { 154592476d7fSEric W. Biederman nr = PTR_ERR(p); 15461da177e4SLinus Torvalds } 154792476d7fSEric W. Biederman return nr; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15505fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 15515fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 15525fd63b30SRavikiran G Thirumalai #endif 15535fd63b30SRavikiran G Thirumalai 155451cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 1555aa1757f9SOleg Nesterov { 1556aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1557aa1757f9SOleg Nesterov 1558aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1559b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 1560fba2afaaSDavide Libenzi } 1561aa1757f9SOleg Nesterov 15621da177e4SLinus Torvalds void __init proc_caches_init(void) 15631da177e4SLinus Torvalds { 15641da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 15651da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 15662dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 15672dff4405SVegard Nossum SLAB_NOTRACK, sighand_ctor); 15681da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 15691da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 15702dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 15711da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 15721da177e4SLinus Torvalds sizeof(struct files_struct), 0, 15732dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 15741da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 15751da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 15762dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 15776345d24dSLinus Torvalds /* 15786345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 15796345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 15806345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 15816345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 15826345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 15836345d24dSLinus Torvalds */ 15841da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 15855fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 15862dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 158733e5d769SDavid Howells vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC); 15888feae131SDavid Howells mmap_init(); 158966577193SAl Viro nsproxy_cache_init(); 15901da177e4SLinus Torvalds } 1591cf2e340fSJANAK DESAI 1592cf2e340fSJANAK DESAI /* 15939bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 1594cf2e340fSJANAK DESAI */ 15959bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 1596cf2e340fSJANAK DESAI { 15979bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 15989bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 15999bfb23fcSOleg Nesterov CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET)) 1600cf2e340fSJANAK DESAI return -EINVAL; 16019bfb23fcSOleg Nesterov /* 16029bfb23fcSOleg Nesterov * Not implemented, but pretend it works if there is nothing to 16039bfb23fcSOleg Nesterov * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND 16049bfb23fcSOleg Nesterov * needs to unshare vm. 16059bfb23fcSOleg Nesterov */ 16069bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 16079bfb23fcSOleg Nesterov /* FIXME: get_task_mm() increments ->mm_users */ 16089bfb23fcSOleg Nesterov if (atomic_read(¤t->mm->mm_users) > 1) 16099bfb23fcSOleg Nesterov return -EINVAL; 16109bfb23fcSOleg Nesterov } 1611cf2e340fSJANAK DESAI 1612cf2e340fSJANAK DESAI return 0; 1613cf2e340fSJANAK DESAI } 1614cf2e340fSJANAK DESAI 1615cf2e340fSJANAK DESAI /* 161699d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1617cf2e340fSJANAK DESAI */ 1618cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1619cf2e340fSJANAK DESAI { 1620cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1621cf2e340fSJANAK DESAI 1622498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 1623498052bbSAl Viro return 0; 1624498052bbSAl Viro 1625498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 1626498052bbSAl Viro if (fs->users == 1) 1627498052bbSAl Viro return 0; 1628498052bbSAl Viro 1629498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 163099d1419dSJANAK DESAI if (!*new_fsp) 163199d1419dSJANAK DESAI return -ENOMEM; 1632cf2e340fSJANAK DESAI 1633cf2e340fSJANAK DESAI return 0; 1634cf2e340fSJANAK DESAI } 1635cf2e340fSJANAK DESAI 1636cf2e340fSJANAK DESAI /* 1637a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1638cf2e340fSJANAK DESAI */ 1639cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1640cf2e340fSJANAK DESAI { 1641cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1642a016f338SJANAK DESAI int error = 0; 1643cf2e340fSJANAK DESAI 1644cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1645a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1646a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1647a016f338SJANAK DESAI if (!*new_fdp) 1648a016f338SJANAK DESAI return error; 1649a016f338SJANAK DESAI } 1650cf2e340fSJANAK DESAI 1651cf2e340fSJANAK DESAI return 0; 1652cf2e340fSJANAK DESAI } 1653cf2e340fSJANAK DESAI 1654cf2e340fSJANAK DESAI /* 1655cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1656cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1657cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1658cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1659cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1660cf2e340fSJANAK DESAI * task_struct. 1661cf2e340fSJANAK DESAI */ 16626559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 1663cf2e340fSJANAK DESAI { 1664cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1665cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1666cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 16679edff4abSManfred Spraul int do_sysvsem = 0; 16689bfb23fcSOleg Nesterov int err; 1669cf2e340fSJANAK DESAI 16709bfb23fcSOleg Nesterov err = check_unshare_flags(unshare_flags); 16719bfb23fcSOleg Nesterov if (err) 167206f9d4f9SEric W. Biederman goto bad_unshare_out; 167306f9d4f9SEric W. Biederman 16746013f67fSManfred Spraul /* 16759bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 16769bfb23fcSOleg Nesterov */ 16779bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 16789bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 16799bfb23fcSOleg Nesterov /* 16806013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 16816013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 16826013f67fSManfred Spraul * namespace are unreachable. 16836013f67fSManfred Spraul */ 16846013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 16859edff4abSManfred Spraul do_sysvsem = 1; 1686fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 1687fb0a685cSDaniel Rebelo de Oliveira if (err) 16889bfb23fcSOleg Nesterov goto bad_unshare_out; 1689fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 1690fb0a685cSDaniel Rebelo de Oliveira if (err) 16919bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 1692fb0a685cSDaniel Rebelo de Oliveira err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, new_fs); 1693fb0a685cSDaniel Rebelo de Oliveira if (err) 16949edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 1695cf2e340fSJANAK DESAI 16969bfb23fcSOleg Nesterov if (new_fs || new_fd || do_sysvsem || new_nsproxy) { 16979edff4abSManfred Spraul if (do_sysvsem) { 16989edff4abSManfred Spraul /* 16999edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 17009edff4abSManfred Spraul */ 17019edff4abSManfred Spraul exit_sem(current); 17029edff4abSManfred Spraul } 1703ab516013SSerge E. Hallyn 1704c0b2fc31SSerge Hallyn if (new_nsproxy) { 1705cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 1706cf7b708cSPavel Emelyanov new_nsproxy = NULL; 1707c0b2fc31SSerge Hallyn } 1708cf2e340fSJANAK DESAI 1709cf7b708cSPavel Emelyanov task_lock(current); 1710cf7b708cSPavel Emelyanov 1711cf2e340fSJANAK DESAI if (new_fs) { 1712cf2e340fSJANAK DESAI fs = current->fs; 17132a4419b5SNick Piggin spin_lock(&fs->lock); 1714cf2e340fSJANAK DESAI current->fs = new_fs; 1715498052bbSAl Viro if (--fs->users) 1716498052bbSAl Viro new_fs = NULL; 1717498052bbSAl Viro else 1718cf2e340fSJANAK DESAI new_fs = fs; 17192a4419b5SNick Piggin spin_unlock(&fs->lock); 1720cf2e340fSJANAK DESAI } 1721cf2e340fSJANAK DESAI 1722cf2e340fSJANAK DESAI if (new_fd) { 1723cf2e340fSJANAK DESAI fd = current->files; 1724cf2e340fSJANAK DESAI current->files = new_fd; 1725cf2e340fSJANAK DESAI new_fd = fd; 1726cf2e340fSJANAK DESAI } 1727cf2e340fSJANAK DESAI 1728cf2e340fSJANAK DESAI task_unlock(current); 1729cf2e340fSJANAK DESAI } 1730cf2e340fSJANAK DESAI 1731c0b2fc31SSerge Hallyn if (new_nsproxy) 1732444f378bSLinus Torvalds put_nsproxy(new_nsproxy); 1733c0b2fc31SSerge Hallyn 1734cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 1735cf2e340fSJANAK DESAI if (new_fd) 1736cf2e340fSJANAK DESAI put_files_struct(new_fd); 1737cf2e340fSJANAK DESAI 1738cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 1739cf2e340fSJANAK DESAI if (new_fs) 1740498052bbSAl Viro free_fs_struct(new_fs); 1741cf2e340fSJANAK DESAI 1742cf2e340fSJANAK DESAI bad_unshare_out: 1743cf2e340fSJANAK DESAI return err; 1744cf2e340fSJANAK DESAI } 17453b125388SAl Viro 17463b125388SAl Viro /* 17473b125388SAl Viro * Helper to unshare the files of the current task. 17483b125388SAl Viro * We don't want to expose copy_files internals to 17493b125388SAl Viro * the exec layer of the kernel. 17503b125388SAl Viro */ 17513b125388SAl Viro 17523b125388SAl Viro int unshare_files(struct files_struct **displaced) 17533b125388SAl Viro { 17543b125388SAl Viro struct task_struct *task = current; 175550704516SAl Viro struct files_struct *copy = NULL; 17563b125388SAl Viro int error; 17573b125388SAl Viro 17583b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 17593b125388SAl Viro if (error || !copy) { 17603b125388SAl Viro *displaced = NULL; 17613b125388SAl Viro return error; 17623b125388SAl Viro } 17633b125388SAl Viro *displaced = task->files; 17643b125388SAl Viro task_lock(task); 17653b125388SAl Viro task->files = copy; 17663b125388SAl Viro task_unlock(task); 17673b125388SAl Viro return 0; 17683b125388SAl Viro } 1769