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> 37e2cfabdfSWill Drewry #include <linux/seccomp.h> 381da177e4SLinus Torvalds #include <linux/swap.h> 391da177e4SLinus Torvalds #include <linux/syscalls.h> 401da177e4SLinus Torvalds #include <linux/jiffies.h> 411da177e4SLinus Torvalds #include <linux/futex.h> 428141c7f3SLinus Torvalds #include <linux/compat.h> 43207205a2SEric Dumazet #include <linux/kthread.h> 447c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 45ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 461da177e4SLinus Torvalds #include <linux/ptrace.h> 471da177e4SLinus Torvalds #include <linux/mount.h> 481da177e4SLinus Torvalds #include <linux/audit.h> 4978fb7466SPavel Emelianov #include <linux/memcontrol.h> 50f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 515e2bf014SMike Galbraith #include <linux/proc_fs.h> 521da177e4SLinus Torvalds #include <linux/profile.h> 531da177e4SLinus Torvalds #include <linux/rmap.h> 54f8af4da3SHugh Dickins #include <linux/ksm.h> 551da177e4SLinus Torvalds #include <linux/acct.h> 568f0ab514SJay Lan #include <linux/tsacct_kern.h> 579f46080cSMatt Helsley #include <linux/cn_proc.h> 58ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 59ca74e92bSShailabh Nagar #include <linux/delayacct.h> 60ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 610a425405SArjan van de Ven #include <linux/random.h> 62522ed776SMiloslav Trmac #include <linux/tty.h> 63fd0928dfSJens Axboe #include <linux/blkdev.h> 645ad4e53bSAl Viro #include <linux/fs_struct.h> 657c9f8861SEric Sandeen #include <linux/magic.h> 66cdd6c482SIngo Molnar #include <linux/perf_event.h> 6742c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 688e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 693d5992d2SYing Han #include <linux/oom.h> 70ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 71d80e731eSOleg Nesterov #include <linux/signalfd.h> 720326f5a9SSrikar Dronamraju #include <linux/uprobes.h> 73a27bb332SKent Overstreet #include <linux/aio.h> 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds #include <asm/pgtable.h> 761da177e4SLinus Torvalds #include <asm/pgalloc.h> 771da177e4SLinus Torvalds #include <asm/uaccess.h> 781da177e4SLinus Torvalds #include <asm/mmu_context.h> 791da177e4SLinus Torvalds #include <asm/cacheflush.h> 801da177e4SLinus Torvalds #include <asm/tlbflush.h> 811da177e4SLinus Torvalds 82ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 83ad8d75ffSSteven Rostedt 8443d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 8543d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 8643d2b113SKAMEZAWA Hiroyuki 871da177e4SLinus Torvalds /* 881da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 911da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 98db1466b3SPaul E. McKenney 99db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 100db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 101db1466b3SPaul E. McKenney { 102db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 103db1466b3SPaul E. McKenney } 104db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 105db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds int nr_processes(void) 1081da177e4SLinus Torvalds { 1091da177e4SLinus Torvalds int cpu; 1101da177e4SLinus Torvalds int total = 0; 1111da177e4SLinus Torvalds 1121d510750SIan Campbell for_each_possible_cpu(cpu) 1131da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds return total; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 118f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 119f19b9f74SAkinobu Mita { 120f19b9f74SAkinobu Mita } 121f19b9f74SAkinobu Mita 122f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 123e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 12441101809SThomas Gleixner 12541101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 12641101809SThomas Gleixner { 12741101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 12841101809SThomas Gleixner } 12941101809SThomas Gleixner 13041101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 13141101809SThomas Gleixner { 13241101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 13341101809SThomas Gleixner } 1341da177e4SLinus Torvalds #endif 1351da177e4SLinus Torvalds 136f19b9f74SAkinobu Mita void __weak arch_release_thread_info(struct thread_info *ti) 137f19b9f74SAkinobu Mita { 138f19b9f74SAkinobu Mita } 139f19b9f74SAkinobu Mita 140f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR 14141101809SThomas Gleixner 1420d15d74aSThomas Gleixner /* 1430d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1440d15d74aSThomas Gleixner * kmemcache based allocator. 1450d15d74aSThomas Gleixner */ 1460d15d74aSThomas Gleixner # if THREAD_SIZE >= PAGE_SIZE 147b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 148b6a84016SEric Dumazet int node) 149b69c49b7SFUJITA Tomonori { 1502ad306b1SGlauber Costa struct page *page = alloc_pages_node(node, THREADINFO_GFP_ACCOUNTED, 1512889f608SThomas Gleixner THREAD_SIZE_ORDER); 152b6a84016SEric Dumazet 153b6a84016SEric Dumazet return page ? page_address(page) : NULL; 154b69c49b7SFUJITA Tomonori } 155b69c49b7SFUJITA Tomonori 156b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti) 157b69c49b7SFUJITA Tomonori { 1582ad306b1SGlauber Costa free_memcg_kmem_pages((unsigned long)ti, THREAD_SIZE_ORDER); 159b69c49b7SFUJITA Tomonori } 1600d15d74aSThomas Gleixner # else 1610d15d74aSThomas Gleixner static struct kmem_cache *thread_info_cache; 1620d15d74aSThomas Gleixner 1630d15d74aSThomas Gleixner static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 1640d15d74aSThomas Gleixner int node) 1650d15d74aSThomas Gleixner { 1660d15d74aSThomas Gleixner return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node); 1670d15d74aSThomas Gleixner } 1680d15d74aSThomas Gleixner 1690d15d74aSThomas Gleixner static void free_thread_info(struct thread_info *ti) 1700d15d74aSThomas Gleixner { 1710d15d74aSThomas Gleixner kmem_cache_free(thread_info_cache, ti); 1720d15d74aSThomas Gleixner } 1730d15d74aSThomas Gleixner 1740d15d74aSThomas Gleixner void thread_info_cache_init(void) 1750d15d74aSThomas Gleixner { 1760d15d74aSThomas Gleixner thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE, 1770d15d74aSThomas Gleixner THREAD_SIZE, 0, NULL); 1780d15d74aSThomas Gleixner BUG_ON(thread_info_cache == NULL); 1790d15d74aSThomas Gleixner } 1800d15d74aSThomas Gleixner # endif 181b69c49b7SFUJITA Tomonori #endif 182b69c49b7SFUJITA Tomonori 1831da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 184e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 187e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 190e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 193e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 196e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 199e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 2001da177e4SLinus Torvalds 201c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account) 202c6a7f572SKOSAKI Motohiro { 203c6a7f572SKOSAKI Motohiro struct zone *zone = page_zone(virt_to_page(ti)); 204c6a7f572SKOSAKI Motohiro 205c6a7f572SKOSAKI Motohiro mod_zone_page_state(zone, NR_KERNEL_STACK, account); 206c6a7f572SKOSAKI Motohiro } 207c6a7f572SKOSAKI Motohiro 2081da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 2091da177e4SLinus Torvalds { 210c6a7f572SKOSAKI Motohiro account_kernel_stack(tsk->stack, -1); 211f19b9f74SAkinobu Mita arch_release_thread_info(tsk->stack); 212f7e4217bSRoman Zippel free_thread_info(tsk->stack); 21323f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 214fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 215e2cfabdfSWill Drewry put_seccomp_filter(tsk); 216f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 2171da177e4SLinus Torvalds free_task_struct(tsk); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 2201da177e4SLinus Torvalds 221ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig) 222ea6d290cSOleg Nesterov { 22397101eb4SOleg Nesterov taskstats_tgid_free(sig); 2241c5354deSMike Galbraith sched_autogroup_exit(sig); 225ea6d290cSOleg Nesterov kmem_cache_free(signal_cachep, sig); 226ea6d290cSOleg Nesterov } 227ea6d290cSOleg Nesterov 228ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig) 229ea6d290cSOleg Nesterov { 2301c5354deSMike Galbraith if (atomic_dec_and_test(&sig->sigcnt)) 231ea6d290cSOleg Nesterov free_signal_struct(sig); 232ea6d290cSOleg Nesterov } 233ea6d290cSOleg Nesterov 234158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 2351da177e4SLinus Torvalds { 236270f722dSEugene Teo WARN_ON(!tsk->exit_state); 2371da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 2381da177e4SLinus Torvalds WARN_ON(tsk == current); 2391da177e4SLinus Torvalds 2401a2a4d06SKees Cook security_task_free(tsk); 241e0e81739SDavid Howells exit_creds(tsk); 24235df17c5SShailabh Nagar delayacct_tsk_free(tsk); 243ea6d290cSOleg Nesterov put_signal_struct(tsk->signal); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 2461da177e4SLinus Torvalds free_task(tsk); 2471da177e4SLinus Torvalds } 24877c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct); 2491da177e4SLinus Torvalds 2506c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { } 25161c4628bSSuresh Siddha 2521da177e4SLinus Torvalds void __init fork_init(unsigned long mempages) 2531da177e4SLinus Torvalds { 254f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 2551da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 2561da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 2571da177e4SLinus Torvalds #endif 2581da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 2591da177e4SLinus Torvalds task_struct_cachep = 2601da177e4SLinus Torvalds kmem_cache_create("task_struct", sizeof(struct task_struct), 2612dff4405SVegard Nossum ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL); 2621da177e4SLinus Torvalds #endif 2631da177e4SLinus Torvalds 26461c4628bSSuresh Siddha /* do the arch specific task caches init */ 26561c4628bSSuresh Siddha arch_task_cache_init(); 26661c4628bSSuresh Siddha 2671da177e4SLinus Torvalds /* 2681da177e4SLinus Torvalds * The default maximum number of threads is set to a safe 2691da177e4SLinus Torvalds * value: the thread structures can take up at most half 2701da177e4SLinus Torvalds * of memory. 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* 2751da177e4SLinus Torvalds * we need to allow at least 20 threads to boot a system 2761da177e4SLinus Torvalds */ 2771da177e4SLinus Torvalds if (max_threads < 20) 2781da177e4SLinus Torvalds max_threads = 20; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 2811da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 2821da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 2831da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 28661c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst, 28761c4628bSSuresh Siddha struct task_struct *src) 28861c4628bSSuresh Siddha { 28961c4628bSSuresh Siddha *dst = *src; 29061c4628bSSuresh Siddha return 0; 29161c4628bSSuresh Siddha } 29261c4628bSSuresh Siddha 2931da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds struct task_struct *tsk; 2961da177e4SLinus Torvalds struct thread_info *ti; 2977c9f8861SEric Sandeen unsigned long *stackend; 298207205a2SEric Dumazet int node = tsk_fork_get_node(orig); 2993e26c149SPeter Zijlstra int err; 3001da177e4SLinus Torvalds 301504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 3021da177e4SLinus Torvalds if (!tsk) 3031da177e4SLinus Torvalds return NULL; 3041da177e4SLinus Torvalds 305b6a84016SEric Dumazet ti = alloc_thread_info_node(tsk, node); 306f19b9f74SAkinobu Mita if (!ti) 307f19b9f74SAkinobu Mita goto free_tsk; 3081da177e4SLinus Torvalds 30961c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 31061c4628bSSuresh Siddha if (err) 311f19b9f74SAkinobu Mita goto free_ti; 31261c4628bSSuresh Siddha 31387bec58aSAndrew Morton tsk->stack = ti; 31487bec58aSAndrew Morton 31587bec58aSAndrew Morton setup_thread_stack(tsk, orig); 3168e7cac79SAvi Kivity clear_user_return_notifier(tsk); 317f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 3187c9f8861SEric Sandeen stackend = end_of_stack(tsk); 3197c9f8861SEric Sandeen *stackend = STACK_END_MAGIC; /* for overflow detection */ 3201da177e4SLinus Torvalds 3210a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 3220a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 3230a425405SArjan van de Ven #endif 3240a425405SArjan van de Ven 325fb0a685cSDaniel Rebelo de Oliveira /* 326fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 327fb0a685cSDaniel Rebelo de Oliveira * parent) 328fb0a685cSDaniel Rebelo de Oliveira */ 3291da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 3306c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 3312056a782SJens Axboe tsk->btrace_seq = 0; 3326c5c9341SAlexey Dobriyan #endif 333a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 3345640f768SEric Dumazet tsk->task_frag.page = NULL; 335c6a7f572SKOSAKI Motohiro 336c6a7f572SKOSAKI Motohiro account_kernel_stack(ti, 1); 337c6a7f572SKOSAKI Motohiro 3381da177e4SLinus Torvalds return tsk; 33961c4628bSSuresh Siddha 340f19b9f74SAkinobu Mita free_ti: 34161c4628bSSuresh Siddha free_thread_info(ti); 342f19b9f74SAkinobu Mita free_tsk: 34361c4628bSSuresh Siddha free_task_struct(tsk); 34461c4628bSSuresh Siddha return NULL; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds #ifdef CONFIG_MMU 348a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 3491da177e4SLinus Torvalds { 350297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 3511da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 3521da177e4SLinus Torvalds int retval; 3531da177e4SLinus Torvalds unsigned long charge; 3541da177e4SLinus Torvalds struct mempolicy *pol; 3551da177e4SLinus Torvalds 35632cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 3571da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 358ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 359f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 360ad339451SIngo Molnar /* 361ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 362ad339451SIngo Molnar */ 363ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 3647ee78232SHugh Dickins 3651da177e4SLinus Torvalds mm->locked_vm = 0; 3661da177e4SLinus Torvalds mm->mmap = NULL; 3671da177e4SLinus Torvalds mm->mmap_cache = NULL; 3681da177e4SLinus Torvalds mm->map_count = 0; 36994894244SRusty Russell cpumask_clear(mm_cpumask(mm)); 3701da177e4SLinus Torvalds mm->mm_rb = RB_ROOT; 3711da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 3721da177e4SLinus Torvalds rb_parent = NULL; 3731da177e4SLinus Torvalds pprev = &mm->mmap; 374f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 375f8af4da3SHugh Dickins if (retval) 376f8af4da3SHugh Dickins goto out; 377ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 378ba76149fSAndrea Arcangeli if (retval) 379ba76149fSAndrea Arcangeli goto out; 3801da177e4SLinus Torvalds 381297c5eeeSLinus Torvalds prev = NULL; 382fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 3831da177e4SLinus Torvalds struct file *file; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 386ab50b8edSHugh Dickins vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 38744de9d0cSHuang Shijie -vma_pages(mpnt)); 3881da177e4SLinus Torvalds continue; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds charge = 0; 3911da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 392b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 393b2412b7fSHuang Shijie 394191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 3951da177e4SLinus Torvalds goto fail_nomem; 3961da177e4SLinus Torvalds charge = len; 3971da177e4SLinus Torvalds } 398e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 3991da177e4SLinus Torvalds if (!tmp) 4001da177e4SLinus Torvalds goto fail_nomem; 4011da177e4SLinus Torvalds *tmp = *mpnt; 4025beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 403846a16bfSLee Schermerhorn pol = mpol_dup(vma_policy(mpnt)); 4041da177e4SLinus Torvalds retval = PTR_ERR(pol); 4051da177e4SLinus Torvalds if (IS_ERR(pol)) 4061da177e4SLinus Torvalds goto fail_nomem_policy; 4071da177e4SLinus Torvalds vma_set_policy(tmp, pol); 408a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 4095beb4930SRik van Riel if (anon_vma_fork(tmp, mpnt)) 4105beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 4111da177e4SLinus Torvalds tmp->vm_flags &= ~VM_LOCKED; 412297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 4131da177e4SLinus Torvalds file = tmp->vm_file; 4141da177e4SLinus Torvalds if (file) { 415496ad9aaSAl Viro struct inode *inode = file_inode(file); 416b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 417b88ed205SHugh Dickins 4181da177e4SLinus Torvalds get_file(file); 4191da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 4201da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 4213d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 422b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 423b88ed205SHugh Dickins mapping->i_mmap_writable++; 424b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 425b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 4269826a516SMichel Lespinasse if (unlikely(tmp->vm_flags & VM_NONLINEAR)) 4279826a516SMichel Lespinasse vma_nonlinear_insert(tmp, 4289826a516SMichel Lespinasse &mapping->i_mmap_nonlinear); 4299826a516SMichel Lespinasse else 4309826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 4319826a516SMichel Lespinasse &mapping->i_mmap); 432b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 4333d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* 437a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 438a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 439a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 440a1e78772SMel Gorman */ 441a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 442a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 443a1e78772SMel Gorman 444a1e78772SMel Gorman /* 4457ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 4461da177e4SLinus Torvalds */ 4471da177e4SLinus Torvalds *pprev = tmp; 4481da177e4SLinus Torvalds pprev = &tmp->vm_next; 449297c5eeeSLinus Torvalds tmp->vm_prev = prev; 450297c5eeeSLinus Torvalds prev = tmp; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 4531da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 4541da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds mm->map_count++; 4570b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 4601da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds if (retval) 4631da177e4SLinus Torvalds goto out; 4641da177e4SLinus Torvalds } 465d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 466d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 4671da177e4SLinus Torvalds retval = 0; 4681da177e4SLinus Torvalds out: 4697ee78232SHugh Dickins up_write(&mm->mmap_sem); 470fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 4711da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 47232cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 4731da177e4SLinus Torvalds return retval; 4745beb4930SRik van Riel fail_nomem_anon_vma_fork: 4755beb4930SRik van Riel mpol_put(pol); 4761da177e4SLinus Torvalds fail_nomem_policy: 4771da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 4781da177e4SLinus Torvalds fail_nomem: 4791da177e4SLinus Torvalds retval = -ENOMEM; 4801da177e4SLinus Torvalds vm_unacct_memory(charge); 4811da177e4SLinus Torvalds goto out; 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 4851da177e4SLinus Torvalds { 4861da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 4871da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 4881da177e4SLinus Torvalds return -ENOMEM; 4891da177e4SLinus Torvalds return 0; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 4931da177e4SLinus Torvalds { 4945e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds #else 4971da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 4981da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 4991da177e4SLinus Torvalds #define mm_free_pgd(mm) 5001da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 5031da177e4SLinus Torvalds 504e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 5051da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 5061da177e4SLinus Torvalds 5074cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 5084cb0e11bSHidehiro Kawai 5094cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 5104cb0e11bSHidehiro Kawai { 5114cb0e11bSHidehiro Kawai default_dump_filter = 5124cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 5134cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 5144cb0e11bSHidehiro Kawai return 1; 5154cb0e11bSHidehiro Kawai } 5164cb0e11bSHidehiro Kawai 5174cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 5184cb0e11bSHidehiro Kawai 5191da177e4SLinus Torvalds #include <linux/init_task.h> 5201da177e4SLinus Torvalds 521858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 522858f0993SAlexey Dobriyan { 523858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 524858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 525858f0993SAlexey Dobriyan INIT_HLIST_HEAD(&mm->ioctx_list); 526858f0993SAlexey Dobriyan #endif 527858f0993SAlexey Dobriyan } 528858f0993SAlexey Dobriyan 52978fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 5301da177e4SLinus Torvalds { 5311da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 5321da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 5331da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 5341da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 535f8af4da3SHugh Dickins mm->flags = (current->mm) ? 536f8af4da3SHugh Dickins (current->mm->flags & MMF_INIT_MASK) : default_dump_filter; 537999d9fc1SOleg Nesterov mm->core_state = NULL; 5381da177e4SLinus Torvalds mm->nr_ptes = 0; 539d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 5401da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 541858f0993SAlexey Dobriyan mm_init_aio(mm); 542cf475ad2SBalbir Singh mm_init_owner(mm, p); 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds if (likely(!mm_alloc_pgd(mm))) { 5451da177e4SLinus Torvalds mm->def_flags = 0; 546cddb8a5cSAndrea Arcangeli mmu_notifier_mm_init(mm); 5471da177e4SLinus Torvalds return mm; 5481da177e4SLinus Torvalds } 54978fb7466SPavel Emelianov 5501da177e4SLinus Torvalds free_mm(mm); 5511da177e4SLinus Torvalds return NULL; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 554c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm) 555c3f0327fSKonstantin Khlebnikov { 556c3f0327fSKonstantin Khlebnikov int i; 557c3f0327fSKonstantin Khlebnikov 558c3f0327fSKonstantin Khlebnikov for (i = 0; i < NR_MM_COUNTERS; i++) { 559c3f0327fSKonstantin Khlebnikov long x = atomic_long_read(&mm->rss_stat.count[i]); 560c3f0327fSKonstantin Khlebnikov 561c3f0327fSKonstantin Khlebnikov if (unlikely(x)) 562c3f0327fSKonstantin Khlebnikov printk(KERN_ALERT "BUG: Bad rss-counter state " 563c3f0327fSKonstantin Khlebnikov "mm:%p idx:%d val:%ld\n", mm, i, x); 564c3f0327fSKonstantin Khlebnikov } 565c3f0327fSKonstantin Khlebnikov 566c3f0327fSKonstantin Khlebnikov #ifdef CONFIG_TRANSPARENT_HUGEPAGE 567c3f0327fSKonstantin Khlebnikov VM_BUG_ON(mm->pmd_huge_pte); 568c3f0327fSKonstantin Khlebnikov #endif 569c3f0327fSKonstantin Khlebnikov } 570c3f0327fSKonstantin Khlebnikov 5711da177e4SLinus Torvalds /* 5721da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 5731da177e4SLinus Torvalds */ 5741da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds struct mm_struct *mm; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds mm = allocate_mm(); 579de03c72cSKOSAKI Motohiro if (!mm) 580de03c72cSKOSAKI Motohiro return NULL; 581de03c72cSKOSAKI Motohiro 5821da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 5836345d24dSLinus Torvalds mm_init_cpumask(mm); 5846345d24dSLinus Torvalds return mm_init(mm, current); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds /* 5881da177e4SLinus Torvalds * Called when the last reference to the mm 5891da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 5901da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 5911da177e4SLinus Torvalds */ 5927ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 5951da177e4SLinus Torvalds mm_free_pgd(mm); 5961da177e4SLinus Torvalds destroy_context(mm); 597cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 598c3f0327fSKonstantin Khlebnikov check_mm(mm); 5991da177e4SLinus Torvalds free_mm(mm); 6001da177e4SLinus Torvalds } 6016d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* 6041da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 6051da177e4SLinus Torvalds */ 6061da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 6071da177e4SLinus Torvalds { 6080ae26f1bSAndrew Morton might_sleep(); 6090ae26f1bSAndrew Morton 6101da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 611d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 6121da177e4SLinus Torvalds exit_aio(mm); 6131c2fb7a4SAndrea Arcangeli ksm_exit(mm); 614ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 6151da177e4SLinus Torvalds exit_mmap(mm); 616925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 6171da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 6181da177e4SLinus Torvalds spin_lock(&mmlist_lock); 6191da177e4SLinus Torvalds list_del(&mm->mmlist); 6201da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 6211da177e4SLinus Torvalds } 622801460d0SHiroshi Shimamoto if (mm->binfmt) 623801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 6241da177e4SLinus Torvalds mmdrop(mm); 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 6281da177e4SLinus Torvalds 62938646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 63038646013SJiri Slaby { 63138646013SJiri Slaby if (new_exe_file) 63238646013SJiri Slaby get_file(new_exe_file); 63338646013SJiri Slaby if (mm->exe_file) 63438646013SJiri Slaby fput(mm->exe_file); 63538646013SJiri Slaby mm->exe_file = new_exe_file; 63638646013SJiri Slaby } 63738646013SJiri Slaby 63838646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 63938646013SJiri Slaby { 64038646013SJiri Slaby struct file *exe_file; 64138646013SJiri Slaby 6422dd8ad81SKonstantin Khlebnikov /* We need mmap_sem to protect against races with removal of exe_file */ 64338646013SJiri Slaby down_read(&mm->mmap_sem); 64438646013SJiri Slaby exe_file = mm->exe_file; 64538646013SJiri Slaby if (exe_file) 64638646013SJiri Slaby get_file(exe_file); 64738646013SJiri Slaby up_read(&mm->mmap_sem); 64838646013SJiri Slaby return exe_file; 64938646013SJiri Slaby } 65038646013SJiri Slaby 65138646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 65238646013SJiri Slaby { 65338646013SJiri Slaby /* It's safe to write the exe_file pointer without exe_file_lock because 65438646013SJiri Slaby * this is called during fork when the task is not yet in /proc */ 65538646013SJiri Slaby newmm->exe_file = get_mm_exe_file(oldmm); 65638646013SJiri Slaby } 65738646013SJiri Slaby 6581da177e4SLinus Torvalds /** 6591da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 6601da177e4SLinus Torvalds * 661246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 6621da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 6631da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 6641da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 6651da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 6661da177e4SLinus Torvalds */ 6671da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 6681da177e4SLinus Torvalds { 6691da177e4SLinus Torvalds struct mm_struct *mm; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds task_lock(task); 6721da177e4SLinus Torvalds mm = task->mm; 6731da177e4SLinus Torvalds if (mm) { 674246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 6751da177e4SLinus Torvalds mm = NULL; 6761da177e4SLinus Torvalds else 6771da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds task_unlock(task); 6801da177e4SLinus Torvalds return mm; 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 6831da177e4SLinus Torvalds 6848cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 6858cdb878dSChristopher Yeoh { 6868cdb878dSChristopher Yeoh struct mm_struct *mm; 6878cdb878dSChristopher Yeoh int err; 6888cdb878dSChristopher Yeoh 6898cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 6908cdb878dSChristopher Yeoh if (err) 6918cdb878dSChristopher Yeoh return ERR_PTR(err); 6928cdb878dSChristopher Yeoh 6938cdb878dSChristopher Yeoh mm = get_task_mm(task); 6948cdb878dSChristopher Yeoh if (mm && mm != current->mm && 6958cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 6968cdb878dSChristopher Yeoh mmput(mm); 6978cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 6988cdb878dSChristopher Yeoh } 6998cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 7008cdb878dSChristopher Yeoh 7018cdb878dSChristopher Yeoh return mm; 7028cdb878dSChristopher Yeoh } 7038cdb878dSChristopher Yeoh 70457b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 705c415c3b4SOleg Nesterov { 706d68b46feSOleg Nesterov struct completion *vfork; 707c415c3b4SOleg Nesterov 708d68b46feSOleg Nesterov task_lock(tsk); 709d68b46feSOleg Nesterov vfork = tsk->vfork_done; 710d68b46feSOleg Nesterov if (likely(vfork)) { 711c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 712d68b46feSOleg Nesterov complete(vfork); 713d68b46feSOleg Nesterov } 714d68b46feSOleg Nesterov task_unlock(tsk); 715d68b46feSOleg Nesterov } 716d68b46feSOleg Nesterov 717d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 718d68b46feSOleg Nesterov struct completion *vfork) 719d68b46feSOleg Nesterov { 720d68b46feSOleg Nesterov int killed; 721d68b46feSOleg Nesterov 722d68b46feSOleg Nesterov freezer_do_not_count(); 723d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 724d68b46feSOleg Nesterov freezer_count(); 725d68b46feSOleg Nesterov 726d68b46feSOleg Nesterov if (killed) { 727d68b46feSOleg Nesterov task_lock(child); 728d68b46feSOleg Nesterov child->vfork_done = NULL; 729d68b46feSOleg Nesterov task_unlock(child); 730d68b46feSOleg Nesterov } 731d68b46feSOleg Nesterov 732d68b46feSOleg Nesterov put_task_struct(child); 733d68b46feSOleg Nesterov return killed; 734c415c3b4SOleg Nesterov } 735c415c3b4SOleg Nesterov 7361da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 7371da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 7381da177e4SLinus Torvalds * error success whatever. 7391da177e4SLinus Torvalds * 7401da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 7411da177e4SLinus Torvalds * from the current process. 7421da177e4SLinus Torvalds * 7431da177e4SLinus Torvalds * This difference is important for error handling, when we 7441da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 7451da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 7461da177e4SLinus Torvalds * restoring the old one. . . 7471da177e4SLinus Torvalds * Eric Biederman 10 January 1998 7481da177e4SLinus Torvalds */ 7491da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 7501da177e4SLinus Torvalds { 7518141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 7528141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 753fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 7548141c7f3SLinus Torvalds exit_robust_list(tsk); 755fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 756fc6b177dSPeter Zijlstra } 7578141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 758fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 7598141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 760fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 761fc6b177dSPeter Zijlstra } 7628141c7f3SLinus Torvalds #endif 763322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 764322a2c10SThomas Gleixner exit_pi_state_list(tsk); 7658141c7f3SLinus Torvalds #endif 7668141c7f3SLinus Torvalds 7670326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 7680326f5a9SSrikar Dronamraju 7691da177e4SLinus Torvalds /* Get rid of any cached register state */ 7701da177e4SLinus Torvalds deactivate_mm(tsk, mm); 7711da177e4SLinus Torvalds 772fec1d011SRoland McGrath /* 773fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 774fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 775fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 776d68b46feSOleg Nesterov * trouble, say, a killed vfork parent shouldn't touch this mm. 777d68b46feSOleg Nesterov * Userland only wants this done for a sys_exit. 778fec1d011SRoland McGrath */ 7799c8a8228SEric Dumazet if (tsk->clear_child_tid) { 7809c8a8228SEric Dumazet if (!(tsk->flags & PF_SIGNALED) && 7819c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 7821da177e4SLinus Torvalds /* 7831da177e4SLinus Torvalds * We don't check the error code - if userspace has 7841da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 7851da177e4SLinus Torvalds */ 7869c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 7879c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 7889c8a8228SEric Dumazet 1, NULL, NULL, 0); 7899c8a8228SEric Dumazet } 7909c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 7911da177e4SLinus Torvalds } 792f7505d64SKonstantin Khlebnikov 793f7505d64SKonstantin Khlebnikov /* 794f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 795f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 796f7505d64SKonstantin Khlebnikov */ 797f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 798f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 801a0a7ec30SJANAK DESAI /* 802a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 803a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 804a0a7ec30SJANAK DESAI */ 805402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk) 806a0a7ec30SJANAK DESAI { 807a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 808a0a7ec30SJANAK DESAI int err; 809a0a7ec30SJANAK DESAI 810a0a7ec30SJANAK DESAI if (!oldmm) 811a0a7ec30SJANAK DESAI return NULL; 812a0a7ec30SJANAK DESAI 813a0a7ec30SJANAK DESAI mm = allocate_mm(); 814a0a7ec30SJANAK DESAI if (!mm) 815a0a7ec30SJANAK DESAI goto fail_nomem; 816a0a7ec30SJANAK DESAI 817a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 8186345d24dSLinus Torvalds mm_init_cpumask(mm); 819a0a7ec30SJANAK DESAI 820e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE 821e7a00c45SAndrea Arcangeli mm->pmd_huge_pte = NULL; 822e7a00c45SAndrea Arcangeli #endif 8235bca2303SMel Gorman #ifdef CONFIG_NUMA_BALANCING 8245bca2303SMel Gorman mm->first_nid = NUMA_PTE_SCAN_INIT; 8255bca2303SMel Gorman #endif 82678fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 827a0a7ec30SJANAK DESAI goto fail_nomem; 828a0a7ec30SJANAK DESAI 829a0a7ec30SJANAK DESAI if (init_new_context(tsk, mm)) 830a0a7ec30SJANAK DESAI goto fail_nocontext; 831a0a7ec30SJANAK DESAI 832925d1c40SMatt Helsley dup_mm_exe_file(oldmm, mm); 833925d1c40SMatt Helsley 834a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 835a0a7ec30SJANAK DESAI if (err) 836a0a7ec30SJANAK DESAI goto free_pt; 837a0a7ec30SJANAK DESAI 838a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 839a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 840a0a7ec30SJANAK DESAI 841801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 842801460d0SHiroshi Shimamoto goto free_pt; 843801460d0SHiroshi Shimamoto 844a0a7ec30SJANAK DESAI return mm; 845a0a7ec30SJANAK DESAI 846a0a7ec30SJANAK DESAI free_pt: 847801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 848801460d0SHiroshi Shimamoto mm->binfmt = NULL; 849a0a7ec30SJANAK DESAI mmput(mm); 850a0a7ec30SJANAK DESAI 851a0a7ec30SJANAK DESAI fail_nomem: 852a0a7ec30SJANAK DESAI return NULL; 853a0a7ec30SJANAK DESAI 854a0a7ec30SJANAK DESAI fail_nocontext: 855a0a7ec30SJANAK DESAI /* 856a0a7ec30SJANAK DESAI * If init_new_context() failed, we cannot use mmput() to free the mm 857a0a7ec30SJANAK DESAI * because it calls destroy_context() 858a0a7ec30SJANAK DESAI */ 859a0a7ec30SJANAK DESAI mm_free_pgd(mm); 860a0a7ec30SJANAK DESAI free_mm(mm); 861a0a7ec30SJANAK DESAI return NULL; 862a0a7ec30SJANAK DESAI } 863a0a7ec30SJANAK DESAI 8641da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 8651da177e4SLinus Torvalds { 8661da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 8671da177e4SLinus Torvalds int retval; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 8701da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 87117406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 87217406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 87317406b82SMandeep Singh Baines #endif 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds tsk->mm = NULL; 8761da177e4SLinus Torvalds tsk->active_mm = NULL; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds /* 8791da177e4SLinus Torvalds * Are we cloning a kernel thread? 8801da177e4SLinus Torvalds * 8811da177e4SLinus Torvalds * We need to steal a active VM for that.. 8821da177e4SLinus Torvalds */ 8831da177e4SLinus Torvalds oldmm = current->mm; 8841da177e4SLinus Torvalds if (!oldmm) 8851da177e4SLinus Torvalds return 0; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 8881da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 8891da177e4SLinus Torvalds mm = oldmm; 8901da177e4SLinus Torvalds goto good_mm; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds retval = -ENOMEM; 894a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 8951da177e4SLinus Torvalds if (!mm) 8961da177e4SLinus Torvalds goto fail_nomem; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds good_mm: 8991da177e4SLinus Torvalds tsk->mm = mm; 9001da177e4SLinus Torvalds tsk->active_mm = mm; 9011da177e4SLinus Torvalds return 0; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds fail_nomem: 9041da177e4SLinus Torvalds return retval; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 907a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 9081da177e4SLinus Torvalds { 909498052bbSAl Viro struct fs_struct *fs = current->fs; 9101da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 911498052bbSAl Viro /* tsk->fs is already what we want */ 9122a4419b5SNick Piggin spin_lock(&fs->lock); 913498052bbSAl Viro if (fs->in_exec) { 9142a4419b5SNick Piggin spin_unlock(&fs->lock); 915498052bbSAl Viro return -EAGAIN; 916498052bbSAl Viro } 917498052bbSAl Viro fs->users++; 9182a4419b5SNick Piggin spin_unlock(&fs->lock); 9191da177e4SLinus Torvalds return 0; 9201da177e4SLinus Torvalds } 921498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 9221da177e4SLinus Torvalds if (!tsk->fs) 9231da177e4SLinus Torvalds return -ENOMEM; 9241da177e4SLinus Torvalds return 0; 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 927a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 928a016f338SJANAK DESAI { 929a016f338SJANAK DESAI struct files_struct *oldf, *newf; 930a016f338SJANAK DESAI int error = 0; 931a016f338SJANAK DESAI 932a016f338SJANAK DESAI /* 933a016f338SJANAK DESAI * A background process may not have any files ... 934a016f338SJANAK DESAI */ 935a016f338SJANAK DESAI oldf = current->files; 936a016f338SJANAK DESAI if (!oldf) 937a016f338SJANAK DESAI goto out; 938a016f338SJANAK DESAI 939a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 940a016f338SJANAK DESAI atomic_inc(&oldf->count); 941a016f338SJANAK DESAI goto out; 942a016f338SJANAK DESAI } 943a016f338SJANAK DESAI 944a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 945a016f338SJANAK DESAI if (!newf) 946a016f338SJANAK DESAI goto out; 947a016f338SJANAK DESAI 948a016f338SJANAK DESAI tsk->files = newf; 949a016f338SJANAK DESAI error = 0; 950a016f338SJANAK DESAI out: 951a016f338SJANAK DESAI return error; 952a016f338SJANAK DESAI } 953a016f338SJANAK DESAI 954fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 955fd0928dfSJens Axboe { 956fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 957fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 9586e736be7STejun Heo struct io_context *new_ioc; 959fd0928dfSJens Axboe 960fd0928dfSJens Axboe if (!ioc) 961fd0928dfSJens Axboe return 0; 962fadad878SJens Axboe /* 963fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 964fadad878SJens Axboe */ 965fadad878SJens Axboe if (clone_flags & CLONE_IO) { 9663d48749dSTejun Heo ioc_task_link(ioc); 9673d48749dSTejun Heo tsk->io_context = ioc; 968fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 9696e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 9706e736be7STejun Heo if (unlikely(!new_ioc)) 971fd0928dfSJens Axboe return -ENOMEM; 972fd0928dfSJens Axboe 9736e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 97411a3122fSTejun Heo put_io_context(new_ioc); 975fd0928dfSJens Axboe } 976fd0928dfSJens Axboe #endif 977fd0928dfSJens Axboe return 0; 978fd0928dfSJens Axboe } 979fd0928dfSJens Axboe 980a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 9811da177e4SLinus Torvalds { 9821da177e4SLinus Torvalds struct sighand_struct *sig; 9831da177e4SLinus Torvalds 98460348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 9851da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 9861da177e4SLinus Torvalds return 0; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 989e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 9901da177e4SLinus Torvalds if (!sig) 9911da177e4SLinus Torvalds return -ENOMEM; 9921da177e4SLinus Torvalds atomic_set(&sig->count, 1); 9931da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 9941da177e4SLinus Torvalds return 0; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds 997a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 998c81addc9SOleg Nesterov { 999d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 1000d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1001c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1002c81addc9SOleg Nesterov } 1003d80e731eSOleg Nesterov } 1004c81addc9SOleg Nesterov 1005f06febc9SFrank Mayhar 1006f06febc9SFrank Mayhar /* 1007f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1008f06febc9SFrank Mayhar */ 1009f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1010f06febc9SFrank Mayhar { 101178d7d407SJiri Slaby unsigned long cpu_limit; 101278d7d407SJiri Slaby 1013f06febc9SFrank Mayhar /* Thread group counters. */ 1014f06febc9SFrank Mayhar thread_group_cputime_init(sig); 1015f06febc9SFrank Mayhar 101678d7d407SJiri Slaby cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 101778d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 101878d7d407SJiri Slaby sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); 10196279a751SOleg Nesterov sig->cputimer.running = 1; 10206279a751SOleg Nesterov } 10216279a751SOleg Nesterov 1022f06febc9SFrank Mayhar /* The timer lists. */ 1023f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1024f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1025f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1026f06febc9SFrank Mayhar } 1027f06febc9SFrank Mayhar 1028a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 10291da177e4SLinus Torvalds { 10301da177e4SLinus Torvalds struct signal_struct *sig; 10311da177e4SLinus Torvalds 10324ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1033490dea45SPeter Zijlstra return 0; 10346279a751SOleg Nesterov 1035a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 10361da177e4SLinus Torvalds tsk->signal = sig; 10371da177e4SLinus Torvalds if (!sig) 10381da177e4SLinus Torvalds return -ENOMEM; 10391da177e4SLinus Torvalds 1040b3ac022cSOleg Nesterov sig->nr_threads = 1; 10411da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1042b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 10431da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1044db51aeccSOleg Nesterov sig->curr_target = tsk; 10451da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 10461da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 10471da177e4SLinus Torvalds 1048c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 10491da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds task_lock(current->group_leader); 10521da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 10531da177e4SLinus Torvalds task_unlock(current->group_leader); 10541da177e4SLinus Torvalds 10556279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 10566279a751SOleg Nesterov 1057522ed776SMiloslav Trmac tty_audit_fork(sig); 10585091faa4SMike Galbraith sched_autogroup_fork(sig); 1059522ed776SMiloslav Trmac 10604714d1d3SBen Blum #ifdef CONFIG_CGROUPS 1061257058aeSTejun Heo init_rwsem(&sig->group_rwsem); 10624714d1d3SBen Blum #endif 10634714d1d3SBen Blum 1064a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1065dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 106628b83c51SKOSAKI Motohiro 1067ebec18a6SLennart Poettering sig->has_child_subreaper = current->signal->has_child_subreaper || 1068ebec18a6SLennart Poettering current->signal->is_child_subreaper; 1069ebec18a6SLennart Poettering 10709b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 10719b1bf12dSKOSAKI Motohiro 10721da177e4SLinus Torvalds return 0; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 1075a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p) 10761da177e4SLinus Torvalds { 10771da177e4SLinus Torvalds unsigned long new_flags = p->flags; 10781da177e4SLinus Torvalds 107921aa9af0STejun Heo new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER); 10801da177e4SLinus Torvalds new_flags |= PF_FORKNOEXEC; 10811da177e4SLinus Torvalds p->flags = new_flags; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 108417da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 10851da177e4SLinus Torvalds { 10861da177e4SLinus Torvalds current->clear_child_tid = tidptr; 10871da177e4SLinus Torvalds 1088b488893aSPavel Emelyanov return task_pid_vnr(current); 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 1091a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 109223f78d4aSIngo Molnar { 10931d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1094e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1095732375c6SDima Zavin plist_head_init(&p->pi_waiters); 109623f78d4aSIngo Molnar p->pi_blocked_on = NULL; 109723f78d4aSIngo Molnar #endif 109823f78d4aSIngo Molnar } 109923f78d4aSIngo Molnar 1100cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER 1101cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 1102cf475ad2SBalbir Singh { 1103cf475ad2SBalbir Singh mm->owner = p; 1104cf475ad2SBalbir Singh } 1105cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */ 1106cf475ad2SBalbir Singh 11071da177e4SLinus Torvalds /* 1108f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1109f06febc9SFrank Mayhar */ 1110f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1111f06febc9SFrank Mayhar { 111264861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 111364861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1114f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1115f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1116f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1117f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1118f06febc9SFrank Mayhar } 1119f06febc9SFrank Mayhar 112081907739SOleg Nesterov static inline void 112181907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 112281907739SOleg Nesterov { 112381907739SOleg Nesterov task->pids[type].pid = pid; 112481907739SOleg Nesterov } 112581907739SOleg Nesterov 1126f06febc9SFrank Mayhar /* 11271da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 11281da177e4SLinus Torvalds * but does not actually start it yet. 11291da177e4SLinus Torvalds * 11301da177e4SLinus Torvalds * It copies the registers, and all the appropriate 11311da177e4SLinus Torvalds * parts of the process environment (as per the clone 11321da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 11331da177e4SLinus Torvalds */ 113436c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 11351da177e4SLinus Torvalds unsigned long stack_start, 11361da177e4SLinus Torvalds unsigned long stack_size, 11371da177e4SLinus Torvalds int __user *child_tidptr, 113809a05394SRoland McGrath struct pid *pid, 113909a05394SRoland McGrath int trace) 11401da177e4SLinus Torvalds { 11411da177e4SLinus Torvalds int retval; 1142a24efe62SMariusz Kozlowski struct task_struct *p; 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 11451da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11461da177e4SLinus Torvalds 1147e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1148e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1149e66eded8SEric W. Biederman 11501da177e4SLinus Torvalds /* 11511da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 11521da177e4SLinus Torvalds * can only be started up within the thread group. 11531da177e4SLinus Torvalds */ 11541da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 11551da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /* 11581da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 11591da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 11601da177e4SLinus Torvalds * for various simplifications in other code. 11611da177e4SLinus Torvalds */ 11621da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 11631da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11641da177e4SLinus Torvalds 1165123be07bSSukadev Bhattiprolu /* 1166123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1167123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1168123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1169123be07bSSukadev Bhattiprolu * from creating siblings. 1170123be07bSSukadev Bhattiprolu */ 1171123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1172123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1173123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1174123be07bSSukadev Bhattiprolu 11758382fcacSEric W. Biederman /* 11768382fcacSEric W. Biederman * If the new process will be in a different pid namespace 11778382fcacSEric W. Biederman * don't allow the creation of threads. 11788382fcacSEric W. Biederman */ 11798382fcacSEric W. Biederman if ((clone_flags & (CLONE_VM|CLONE_NEWPID)) && 1180*c2b1df2eSAndy Lutomirski (task_active_pid_ns(current) != 1181*c2b1df2eSAndy Lutomirski current->nsproxy->pid_ns_for_children)) 11828382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 11838382fcacSEric W. Biederman 11841da177e4SLinus Torvalds retval = security_task_create(clone_flags); 11851da177e4SLinus Torvalds if (retval) 11861da177e4SLinus Torvalds goto fork_out; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds retval = -ENOMEM; 11891da177e4SLinus Torvalds p = dup_task_struct(current); 11901da177e4SLinus Torvalds if (!p) 11911da177e4SLinus Torvalds goto fork_out; 11921da177e4SLinus Torvalds 1193f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1194e2cfabdfSWill Drewry get_seccomp_filter(p); 1195f7e8b616SSteven Rostedt 1196bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1197bea493a0SPeter Zijlstra 1198d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1199de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1200de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1201de30a2b3SIngo Molnar #endif 12021da177e4SLinus Torvalds retval = -EAGAIN; 12033b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 120478d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1205b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1206b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 12071da177e4SLinus Torvalds goto bad_fork_free; 12081da177e4SLinus Torvalds } 120972fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 12101da177e4SLinus Torvalds 1211f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1212f1752eecSDavid Howells if (retval < 0) 1213f1752eecSDavid Howells goto bad_fork_free; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds /* 12161da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 12171da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 12181da177e4SLinus Torvalds * to stop root fork bombs. 12191da177e4SLinus Torvalds */ 122004ec93feSLi Zefan retval = -EAGAIN; 12211da177e4SLinus Torvalds if (nr_threads >= max_threads) 12221da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12231da177e4SLinus Torvalds 1224a1261f54SAl Viro if (!try_module_get(task_thread_info(p)->exec_domain->module)) 12251da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds p->did_exec = 0; 1228ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 12291da177e4SLinus Torvalds copy_flags(clone_flags, p); 12301da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 12311da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1232f41d911fSPaul E. McKenney rcu_copy_process(p); 12331da177e4SLinus Torvalds p->vfork_done = NULL; 12341da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds init_sigpending(&p->pending); 12371da177e4SLinus Torvalds 123864861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 123964861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 12409fbc42eaSFrederic Weisbecker #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 1241d37f761dSFrederic Weisbecker p->prev_cputime.utime = p->prev_cputime.stime = 0; 1242d99ca3b9SHidetoshi Seto #endif 12436a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 12446a61671bSFrederic Weisbecker seqlock_init(&p->vtime_seqlock); 12456a61671bSFrederic Weisbecker p->vtime_snap = 0; 12466a61671bSFrederic Weisbecker p->vtime_snap_whence = VTIME_SLEEPING; 12476a61671bSFrederic Weisbecker #endif 12486a61671bSFrederic Weisbecker 1249a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1250a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1251a3a2e76cSKAMEZAWA Hiroyuki #endif 1252172ba844SBalbir Singh 12536976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 12546976675dSArjan van de Ven 12555995477aSAndrea Righi task_io_accounting_init(&p->ioac); 12561da177e4SLinus Torvalds acct_clear_integrals(p); 12571da177e4SLinus Torvalds 1258f06febc9SFrank Mayhar posix_cpu_timers_init(p); 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&p->start_time); 1261924b42d5STomas Janousek p->real_start_time = p->start_time; 1262924b42d5STomas Janousek monotonic_to_bootbased(&p->real_start_time); 12631da177e4SLinus Torvalds p->io_context = NULL; 12641da177e4SLinus Torvalds p->audit_context = NULL; 12654714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1266257058aeSTejun Heo threadgroup_change_begin(current); 1267b4f48b63SPaul Menage cgroup_fork(p); 12681da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1269846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 12701da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 12711da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 12721da177e4SLinus Torvalds p->mempolicy = NULL; 1273b4f48b63SPaul Menage goto bad_fork_cleanup_cgroup; 12741da177e4SLinus Torvalds } 1275c61afb18SPaul Jackson mpol_fix_fork_child_flag(p); 12761da177e4SLinus Torvalds #endif 1277778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1278778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1279778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1280cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1281778d3b0fSMichal Hocko #endif 1282de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1283de30a2b3SIngo Molnar p->irq_events = 0; 1284de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1285de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1286de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1287de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1288de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1289de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1290de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1291de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1292de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1293de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1294de30a2b3SIngo Molnar p->hardirq_context = 0; 1295de30a2b3SIngo Molnar p->softirq_context = 0; 1296de30a2b3SIngo Molnar #endif 1297fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1298fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1299fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1300fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1301fbb9ce95SIngo Molnar #endif 13021da177e4SLinus Torvalds 1303408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1304408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1305408894eeSIngo Molnar #endif 1306c255a458SAndrew Morton #ifdef CONFIG_MEMCG 1307569b846dSKAMEZAWA Hiroyuki p->memcg_batch.do_batch = 0; 1308569b846dSKAMEZAWA Hiroyuki p->memcg_batch.memcg = NULL; 1309569b846dSKAMEZAWA Hiroyuki #endif 1310cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 1311cafe5635SKent Overstreet p->sequential_io = 0; 1312cafe5635SKent Overstreet p->sequential_io_avg = 0; 1313cafe5635SKent Overstreet #endif 13140f481406SMarkus Metzger 13153c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 13163e51e3edSSamir Bellabes sched_fork(p); 13176ab423e0SPeter Zijlstra 1318cdd6c482SIngo Molnar retval = perf_event_init_task(p); 13196ab423e0SPeter Zijlstra if (retval) 13206ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1321fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1322fb0a685cSDaniel Rebelo de Oliveira if (retval) 1323f1752eecSDavid Howells goto bad_fork_cleanup_policy; 13241da177e4SLinus Torvalds /* copy all the process information */ 1325fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1326fb0a685cSDaniel Rebelo de Oliveira if (retval) 13271da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1328fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1329fb0a685cSDaniel Rebelo de Oliveira if (retval) 13301da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1331fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1332fb0a685cSDaniel Rebelo de Oliveira if (retval) 13331da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1334fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1335fb0a685cSDaniel Rebelo de Oliveira if (retval) 13361da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1337fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1338fb0a685cSDaniel Rebelo de Oliveira if (retval) 13391da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1340fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1341fb0a685cSDaniel Rebelo de Oliveira if (retval) 13421da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1343fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1344fb0a685cSDaniel Rebelo de Oliveira if (retval) 1345d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1346fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1347fb0a685cSDaniel Rebelo de Oliveira if (retval) 1348fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 1349afa86fc4SAl Viro retval = copy_thread(clone_flags, stack_start, stack_size, p); 13501da177e4SLinus Torvalds if (retval) 1351fd0928dfSJens Axboe goto bad_fork_cleanup_io; 13521da177e4SLinus Torvalds 1353425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1354425fb2b4SPavel Emelyanov retval = -ENOMEM; 1355*c2b1df2eSAndy Lutomirski pid = alloc_pid(p->nsproxy->pid_ns_for_children); 1356425fb2b4SPavel Emelyanov if (!pid) 1357fd0928dfSJens Axboe goto bad_fork_cleanup_io; 1358425fb2b4SPavel Emelyanov } 1359425fb2b4SPavel Emelyanov 13601da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 13611da177e4SLinus Torvalds /* 13621da177e4SLinus Torvalds * Clear TID on mm_release()? 13631da177e4SLinus Torvalds */ 13641da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 136573c10101SJens Axboe #ifdef CONFIG_BLOCK 136673c10101SJens Axboe p->plug = NULL; 136773c10101SJens Axboe #endif 136842b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 13698f17d3a5SIngo Molnar p->robust_list = NULL; 13708f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 13718f17d3a5SIngo Molnar p->compat_robust_list = NULL; 13728f17d3a5SIngo Molnar #endif 1373c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1374c87e2837SIngo Molnar p->pi_state_cache = NULL; 137542b2dd0aSAlexey Dobriyan #endif 13760326f5a9SSrikar Dronamraju uprobe_copy_process(p); 13771da177e4SLinus Torvalds /* 1378f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1379f9a3879aSGOTO Masanori */ 1380f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 1381f9a3879aSGOTO Masanori p->sas_ss_sp = p->sas_ss_size = 0; 1382f9a3879aSGOTO Masanori 1383f9a3879aSGOTO Masanori /* 13846580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 13856580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 13861da177e4SLinus Torvalds */ 13876580807dSOleg Nesterov user_disable_single_step(p); 13881da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1389ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1390ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1391ed75e8d5SLaurent Vivier #endif 13929745512cSArjan van de Ven clear_all_latency_tracing(p); 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds /* ok, now we should be set up.. */ 139518c830dfSOleg Nesterov p->pid = pid_nr(pid); 139618c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 13975f8aadd8SOleg Nesterov p->exit_signal = -1; 139818c830dfSOleg Nesterov p->group_leader = current->group_leader; 139918c830dfSOleg Nesterov p->tgid = current->tgid; 140018c830dfSOleg Nesterov } else { 140118c830dfSOleg Nesterov if (clone_flags & CLONE_PARENT) 14025f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 14035f8aadd8SOleg Nesterov else 14045f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 140518c830dfSOleg Nesterov p->group_leader = p; 140618c830dfSOleg Nesterov p->tgid = p->pid; 140718c830dfSOleg Nesterov } 14085f8aadd8SOleg Nesterov 14091da177e4SLinus Torvalds p->pdeath_signal = 0; 14101da177e4SLinus Torvalds p->exit_state = 0; 14111da177e4SLinus Torvalds 14129d823e8fSWu Fengguang p->nr_dirtied = 0; 14139d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 141483712358SWu Fengguang p->dirty_paused_when = 0; 14159d823e8fSWu Fengguang 141647e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 1417158e1645SAl Viro p->task_works = NULL; 14181da177e4SLinus Torvalds 141918c830dfSOleg Nesterov /* 142018c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 142118c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 142218c830dfSOleg Nesterov */ 14231da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 14262d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 14271da177e4SLinus Torvalds p->real_parent = current->real_parent; 14282d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 14292d5516cbSOleg Nesterov } else { 14301da177e4SLinus Torvalds p->real_parent = current; 14312d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 14322d5516cbSOleg Nesterov } 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 14354a2c7a78SOleg Nesterov 14364a2c7a78SOleg Nesterov /* 14374a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 14384a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 14394a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 14404a2c7a78SOleg Nesterov * it's process group. 14414a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 14424a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 14434a2c7a78SOleg Nesterov */ 14444a2c7a78SOleg Nesterov recalc_sigpending(); 14454a2c7a78SOleg Nesterov if (signal_pending(current)) { 14464a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 14474a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 14484a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 1449f7e8b616SSteven Rostedt goto bad_fork_free_pid; 14504a2c7a78SOleg Nesterov } 14514a2c7a78SOleg Nesterov 145273b9ebfeSOleg Nesterov if (likely(p->pid)) { 14534b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 14541da177e4SLinus Torvalds 145581907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 14561da177e4SLinus Torvalds if (thread_group_leader(p)) { 145781907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 145881907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 145981907739SOleg Nesterov 14601c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 146117cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 14621c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 14631c4042c2SEric W. Biederman } 14645cd17569SEric W. Biederman 1465fea9d175SOleg Nesterov p->signal->leader_pid = pid; 14669c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 14679cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 14685e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 146981907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 147081907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 1471909ea964SChristoph Lameter __this_cpu_inc(process_counts); 147280628ca0SOleg Nesterov } else { 147380628ca0SOleg Nesterov current->signal->nr_threads++; 147480628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 147580628ca0SOleg Nesterov atomic_inc(¤t->signal->sigcnt); 147680628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 147780628ca0SOleg Nesterov &p->group_leader->thread_group); 14781da177e4SLinus Torvalds } 147981907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 14801da177e4SLinus Torvalds nr_threads++; 148173b9ebfeSOleg Nesterov } 148273b9ebfeSOleg Nesterov 14831da177e4SLinus Torvalds total_forks++; 14843f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 14851da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 1486c13cf856SAndrew Morton proc_fork_connector(p); 1487817929ecSPaul Menage cgroup_post_fork(p); 14884714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1489257058aeSTejun Heo threadgroup_change_end(current); 1490cdd6c482SIngo Molnar perf_event_fork(p); 149143d2b113SKAMEZAWA Hiroyuki 149243d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 149343d2b113SKAMEZAWA Hiroyuki 14941da177e4SLinus Torvalds return p; 14951da177e4SLinus Torvalds 1496425fb2b4SPavel Emelyanov bad_fork_free_pid: 1497425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1498425fb2b4SPavel Emelyanov free_pid(pid); 1499fd0928dfSJens Axboe bad_fork_cleanup_io: 1500b69f2292SLouis Rilling if (p->io_context) 1501b69f2292SLouis Rilling exit_io_context(p); 1502ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1503444f378bSLinus Torvalds exit_task_namespaces(p); 15041da177e4SLinus Torvalds bad_fork_cleanup_mm: 1505c9f01245SDavid Rientjes if (p->mm) 15061da177e4SLinus Torvalds mmput(p->mm); 15071da177e4SLinus Torvalds bad_fork_cleanup_signal: 15084ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 15091c5354deSMike Galbraith free_signal_struct(p->signal); 15101da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1511a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 15121da177e4SLinus Torvalds bad_fork_cleanup_fs: 15131da177e4SLinus Torvalds exit_fs(p); /* blocking */ 15141da177e4SLinus Torvalds bad_fork_cleanup_files: 15151da177e4SLinus Torvalds exit_files(p); /* blocking */ 15161da177e4SLinus Torvalds bad_fork_cleanup_semundo: 15171da177e4SLinus Torvalds exit_sem(p); 15181da177e4SLinus Torvalds bad_fork_cleanup_audit: 15191da177e4SLinus Torvalds audit_free(p); 15201da177e4SLinus Torvalds bad_fork_cleanup_policy: 1521cdd6c482SIngo Molnar perf_event_free_task(p); 15221da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1523f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1524b4f48b63SPaul Menage bad_fork_cleanup_cgroup: 15251da177e4SLinus Torvalds #endif 15264714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1527257058aeSTejun Heo threadgroup_change_end(current); 15285edee61eSTejun Heo cgroup_exit(p, 0); 152935df17c5SShailabh Nagar delayacct_tsk_free(p); 1530a1261f54SAl Viro module_put(task_thread_info(p)->exec_domain->module); 15311da177e4SLinus Torvalds bad_fork_cleanup_count: 1532d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1533e0e81739SDavid Howells exit_creds(p); 15341da177e4SLinus Torvalds bad_fork_free: 15351da177e4SLinus Torvalds free_task(p); 1536fe7d37d1SOleg Nesterov fork_out: 1537fe7d37d1SOleg Nesterov return ERR_PTR(retval); 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 1540f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1541f106eee1SOleg Nesterov { 1542f106eee1SOleg Nesterov enum pid_type type; 1543f106eee1SOleg Nesterov 1544f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1545f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1546f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1547f106eee1SOleg Nesterov } 1548f106eee1SOleg Nesterov } 1549f106eee1SOleg Nesterov 15500db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 15511da177e4SLinus Torvalds { 155236c8b586SIngo Molnar struct task_struct *task; 155362e791c1SAl Viro task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0); 1554f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1555f106eee1SOleg Nesterov init_idle_pids(task->pids); 15561da177e4SLinus Torvalds init_idle(task, cpu); 1557f106eee1SOleg Nesterov } 155873b9ebfeSOleg Nesterov 15591da177e4SLinus Torvalds return task; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds /* 15631da177e4SLinus Torvalds * Ok, this is the main fork-routine. 15641da177e4SLinus Torvalds * 15651da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 15661da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 15671da177e4SLinus Torvalds */ 15681da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 15691da177e4SLinus Torvalds unsigned long stack_start, 15701da177e4SLinus Torvalds unsigned long stack_size, 15711da177e4SLinus Torvalds int __user *parent_tidptr, 15721da177e4SLinus Torvalds int __user *child_tidptr) 15731da177e4SLinus Torvalds { 15741da177e4SLinus Torvalds struct task_struct *p; 15751da177e4SLinus Torvalds int trace = 0; 157692476d7fSEric W. Biederman long nr; 15771da177e4SLinus Torvalds 1578bdff746aSAndrew Morton /* 157918b6e041SSerge Hallyn * Do some preliminary argument and permissions checking before we 158018b6e041SSerge Hallyn * actually start allocating stuff 158118b6e041SSerge Hallyn */ 158250804fe3SEric W. Biederman if (clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) { 158350804fe3SEric W. Biederman if (clone_flags & (CLONE_THREAD|CLONE_PARENT)) 158418b6e041SSerge Hallyn return -EINVAL; 158518b6e041SSerge Hallyn } 158618b6e041SSerge Hallyn 158718b6e041SSerge Hallyn /* 15884b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 15894b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 15904b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 15914b9d33e6STejun Heo * for the type of forking is enabled. 159209a05394SRoland McGrath */ 1593e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 15944b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 15954b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 15964b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 15974b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 15984b9d33e6STejun Heo else 15994b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 16004b9d33e6STejun Heo 16014b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 16024b9d33e6STejun Heo trace = 0; 16034b9d33e6STejun Heo } 16041da177e4SLinus Torvalds 160562e791c1SAl Viro p = copy_process(clone_flags, stack_start, stack_size, 160609a05394SRoland McGrath child_tidptr, NULL, trace); 16071da177e4SLinus Torvalds /* 16081da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 16091da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 16101da177e4SLinus Torvalds */ 16111da177e4SLinus Torvalds if (!IS_ERR(p)) { 16121da177e4SLinus Torvalds struct completion vfork; 16131da177e4SLinus Torvalds 16140a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 16150a16b607SMathieu Desnoyers 16166c5f3e7bSPavel Emelyanov nr = task_pid_vnr(p); 161730e49c26SPavel Emelyanov 161830e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 161930e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1620a6f5e063SSukadev Bhattiprolu 16211da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 16221da177e4SLinus Torvalds p->vfork_done = &vfork; 16231da177e4SLinus Torvalds init_completion(&vfork); 1624d68b46feSOleg Nesterov get_task_struct(p); 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds 16273e51e3edSSamir Bellabes wake_up_new_task(p); 16281da177e4SLinus Torvalds 16294b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 16304b9d33e6STejun Heo if (unlikely(trace)) 16314b9d33e6STejun Heo ptrace_event(trace, nr); 163209a05394SRoland McGrath 16331da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1634d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 1635a288eeccSTejun Heo ptrace_event(PTRACE_EVENT_VFORK_DONE, nr); 16369f59ce5dSChuck Ebbert } 16371da177e4SLinus Torvalds } else { 163892476d7fSEric W. Biederman nr = PTR_ERR(p); 16391da177e4SLinus Torvalds } 164092476d7fSEric W. Biederman return nr; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 16432aa3a7f8SAl Viro /* 16442aa3a7f8SAl Viro * Create a kernel thread. 16452aa3a7f8SAl Viro */ 16462aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 16472aa3a7f8SAl Viro { 1648e80d6661SAl Viro return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, 16492aa3a7f8SAl Viro (unsigned long)arg, NULL, NULL); 16502aa3a7f8SAl Viro } 16512aa3a7f8SAl Viro 1652d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 1653d2125043SAl Viro SYSCALL_DEFINE0(fork) 1654d2125043SAl Viro { 1655d2125043SAl Viro #ifdef CONFIG_MMU 1656e80d6661SAl Viro return do_fork(SIGCHLD, 0, 0, NULL, NULL); 1657d2125043SAl Viro #else 1658d2125043SAl Viro /* can not support in nommu mode */ 1659d2125043SAl Viro return(-EINVAL); 1660d2125043SAl Viro #endif 1661d2125043SAl Viro } 1662d2125043SAl Viro #endif 1663d2125043SAl Viro 1664d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 1665d2125043SAl Viro SYSCALL_DEFINE0(vfork) 1666d2125043SAl Viro { 1667e80d6661SAl Viro return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 1668d2125043SAl Viro 0, NULL, NULL); 1669d2125043SAl Viro } 1670d2125043SAl Viro #endif 1671d2125043SAl Viro 1672d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 1673d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 1674d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1675d2125043SAl Viro int __user *, parent_tidptr, 1676d2125043SAl Viro int, tls_val, 1677d2125043SAl Viro int __user *, child_tidptr) 1678d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 1679d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 1680d2125043SAl Viro int __user *, parent_tidptr, 1681d2125043SAl Viro int __user *, child_tidptr, 1682d2125043SAl Viro int, tls_val) 1683dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 1684dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 1685dfa9771aSMichal Simek int, stack_size, 1686dfa9771aSMichal Simek int __user *, parent_tidptr, 1687dfa9771aSMichal Simek int __user *, child_tidptr, 1688dfa9771aSMichal Simek int, tls_val) 1689d2125043SAl Viro #else 1690d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1691d2125043SAl Viro int __user *, parent_tidptr, 1692d2125043SAl Viro int __user *, child_tidptr, 1693d2125043SAl Viro int, tls_val) 1694d2125043SAl Viro #endif 1695d2125043SAl Viro { 16962cf09666SAl Viro return do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr); 1697d2125043SAl Viro } 1698d2125043SAl Viro #endif 1699d2125043SAl Viro 17005fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 17015fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 17025fd63b30SRavikiran G Thirumalai #endif 17035fd63b30SRavikiran G Thirumalai 170451cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 1705aa1757f9SOleg Nesterov { 1706aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1707aa1757f9SOleg Nesterov 1708aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1709b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 1710fba2afaaSDavide Libenzi } 1711aa1757f9SOleg Nesterov 17121da177e4SLinus Torvalds void __init proc_caches_init(void) 17131da177e4SLinus Torvalds { 17141da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 17151da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 17162dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 17172dff4405SVegard Nossum SLAB_NOTRACK, sighand_ctor); 17181da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 17191da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 17202dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17211da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 17221da177e4SLinus Torvalds sizeof(struct files_struct), 0, 17232dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17241da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 17251da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 17262dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17276345d24dSLinus Torvalds /* 17286345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 17296345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 17306345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 17316345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 17326345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 17336345d24dSLinus Torvalds */ 17341da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 17355fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 17362dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 173733e5d769SDavid Howells vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC); 17388feae131SDavid Howells mmap_init(); 173966577193SAl Viro nsproxy_cache_init(); 17401da177e4SLinus Torvalds } 1741cf2e340fSJANAK DESAI 1742cf2e340fSJANAK DESAI /* 17439bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 1744cf2e340fSJANAK DESAI */ 17459bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 1746cf2e340fSJANAK DESAI { 17479bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 17489bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 174950804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 1750b2e0d987SEric W. Biederman CLONE_NEWUSER|CLONE_NEWPID)) 1751cf2e340fSJANAK DESAI return -EINVAL; 17529bfb23fcSOleg Nesterov /* 17539bfb23fcSOleg Nesterov * Not implemented, but pretend it works if there is nothing to 17549bfb23fcSOleg Nesterov * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND 17559bfb23fcSOleg Nesterov * needs to unshare vm. 17569bfb23fcSOleg Nesterov */ 17579bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 17589bfb23fcSOleg Nesterov /* FIXME: get_task_mm() increments ->mm_users */ 17599bfb23fcSOleg Nesterov if (atomic_read(¤t->mm->mm_users) > 1) 17609bfb23fcSOleg Nesterov return -EINVAL; 17619bfb23fcSOleg Nesterov } 1762cf2e340fSJANAK DESAI 1763cf2e340fSJANAK DESAI return 0; 1764cf2e340fSJANAK DESAI } 1765cf2e340fSJANAK DESAI 1766cf2e340fSJANAK DESAI /* 176799d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1768cf2e340fSJANAK DESAI */ 1769cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1770cf2e340fSJANAK DESAI { 1771cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1772cf2e340fSJANAK DESAI 1773498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 1774498052bbSAl Viro return 0; 1775498052bbSAl Viro 1776498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 1777498052bbSAl Viro if (fs->users == 1) 1778498052bbSAl Viro return 0; 1779498052bbSAl Viro 1780498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 178199d1419dSJANAK DESAI if (!*new_fsp) 178299d1419dSJANAK DESAI return -ENOMEM; 1783cf2e340fSJANAK DESAI 1784cf2e340fSJANAK DESAI return 0; 1785cf2e340fSJANAK DESAI } 1786cf2e340fSJANAK DESAI 1787cf2e340fSJANAK DESAI /* 1788a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1789cf2e340fSJANAK DESAI */ 1790cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1791cf2e340fSJANAK DESAI { 1792cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1793a016f338SJANAK DESAI int error = 0; 1794cf2e340fSJANAK DESAI 1795cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1796a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1797a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1798a016f338SJANAK DESAI if (!*new_fdp) 1799a016f338SJANAK DESAI return error; 1800a016f338SJANAK DESAI } 1801cf2e340fSJANAK DESAI 1802cf2e340fSJANAK DESAI return 0; 1803cf2e340fSJANAK DESAI } 1804cf2e340fSJANAK DESAI 1805cf2e340fSJANAK DESAI /* 1806cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1807cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1808cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1809cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1810cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1811cf2e340fSJANAK DESAI * task_struct. 1812cf2e340fSJANAK DESAI */ 18136559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 1814cf2e340fSJANAK DESAI { 1815cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1816cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1817b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 1818cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 18199edff4abSManfred Spraul int do_sysvsem = 0; 18209bfb23fcSOleg Nesterov int err; 1821cf2e340fSJANAK DESAI 182250804fe3SEric W. Biederman /* 1823b2e0d987SEric W. Biederman * If unsharing a user namespace must also unshare the thread. 1824b2e0d987SEric W. Biederman */ 1825b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 1826e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 1827b2e0d987SEric W. Biederman /* 182850804fe3SEric W. Biederman * If unsharing a pid namespace must also unshare the thread. 182950804fe3SEric W. Biederman */ 183050804fe3SEric W. Biederman if (unshare_flags & CLONE_NEWPID) 183150804fe3SEric W. Biederman unshare_flags |= CLONE_THREAD; 183250804fe3SEric W. Biederman /* 183350804fe3SEric W. Biederman * If unsharing a thread from a thread group, must also unshare vm. 183450804fe3SEric W. Biederman */ 183550804fe3SEric W. Biederman if (unshare_flags & CLONE_THREAD) 183650804fe3SEric W. Biederman unshare_flags |= CLONE_VM; 183750804fe3SEric W. Biederman /* 183850804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 183950804fe3SEric W. Biederman */ 184050804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 184150804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 18426013f67fSManfred Spraul /* 18439bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 18449bfb23fcSOleg Nesterov */ 18459bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 18469bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 184750804fe3SEric W. Biederman 184850804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 184950804fe3SEric W. Biederman if (err) 185050804fe3SEric W. Biederman goto bad_unshare_out; 18519bfb23fcSOleg Nesterov /* 18526013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 18536013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 18546013f67fSManfred Spraul * namespace are unreachable. 18556013f67fSManfred Spraul */ 18566013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 18579edff4abSManfred Spraul do_sysvsem = 1; 1858fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 1859fb0a685cSDaniel Rebelo de Oliveira if (err) 18609bfb23fcSOleg Nesterov goto bad_unshare_out; 1861fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 1862fb0a685cSDaniel Rebelo de Oliveira if (err) 18639bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 1864b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 1865fb0a685cSDaniel Rebelo de Oliveira if (err) 18669edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 1867b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 1868b2e0d987SEric W. Biederman new_cred, new_fs); 1869b2e0d987SEric W. Biederman if (err) 1870b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 1871cf2e340fSJANAK DESAI 1872b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 18739edff4abSManfred Spraul if (do_sysvsem) { 18749edff4abSManfred Spraul /* 18759edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 18769edff4abSManfred Spraul */ 18779edff4abSManfred Spraul exit_sem(current); 18789edff4abSManfred Spraul } 1879ab516013SSerge E. Hallyn 18806f977e6bSAlan Cox if (new_nsproxy) 1881cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 1882cf2e340fSJANAK DESAI 1883cf7b708cSPavel Emelyanov task_lock(current); 1884cf7b708cSPavel Emelyanov 1885cf2e340fSJANAK DESAI if (new_fs) { 1886cf2e340fSJANAK DESAI fs = current->fs; 18872a4419b5SNick Piggin spin_lock(&fs->lock); 1888cf2e340fSJANAK DESAI current->fs = new_fs; 1889498052bbSAl Viro if (--fs->users) 1890498052bbSAl Viro new_fs = NULL; 1891498052bbSAl Viro else 1892cf2e340fSJANAK DESAI new_fs = fs; 18932a4419b5SNick Piggin spin_unlock(&fs->lock); 1894cf2e340fSJANAK DESAI } 1895cf2e340fSJANAK DESAI 1896cf2e340fSJANAK DESAI if (new_fd) { 1897cf2e340fSJANAK DESAI fd = current->files; 1898cf2e340fSJANAK DESAI current->files = new_fd; 1899cf2e340fSJANAK DESAI new_fd = fd; 1900cf2e340fSJANAK DESAI } 1901cf2e340fSJANAK DESAI 1902cf2e340fSJANAK DESAI task_unlock(current); 1903b2e0d987SEric W. Biederman 1904b2e0d987SEric W. Biederman if (new_cred) { 1905b2e0d987SEric W. Biederman /* Install the new user namespace */ 1906b2e0d987SEric W. Biederman commit_creds(new_cred); 1907b2e0d987SEric W. Biederman new_cred = NULL; 1908b2e0d987SEric W. Biederman } 1909cf2e340fSJANAK DESAI } 1910cf2e340fSJANAK DESAI 1911b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 1912b2e0d987SEric W. Biederman if (new_cred) 1913b2e0d987SEric W. Biederman put_cred(new_cred); 1914cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 1915cf2e340fSJANAK DESAI if (new_fd) 1916cf2e340fSJANAK DESAI put_files_struct(new_fd); 1917cf2e340fSJANAK DESAI 1918cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 1919cf2e340fSJANAK DESAI if (new_fs) 1920498052bbSAl Viro free_fs_struct(new_fs); 1921cf2e340fSJANAK DESAI 1922cf2e340fSJANAK DESAI bad_unshare_out: 1923cf2e340fSJANAK DESAI return err; 1924cf2e340fSJANAK DESAI } 19253b125388SAl Viro 19263b125388SAl Viro /* 19273b125388SAl Viro * Helper to unshare the files of the current task. 19283b125388SAl Viro * We don't want to expose copy_files internals to 19293b125388SAl Viro * the exec layer of the kernel. 19303b125388SAl Viro */ 19313b125388SAl Viro 19323b125388SAl Viro int unshare_files(struct files_struct **displaced) 19333b125388SAl Viro { 19343b125388SAl Viro struct task_struct *task = current; 193550704516SAl Viro struct files_struct *copy = NULL; 19363b125388SAl Viro int error; 19373b125388SAl Viro 19383b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 19393b125388SAl Viro if (error || !copy) { 19403b125388SAl Viro *displaced = NULL; 19413b125388SAl Viro return error; 19423b125388SAl Viro } 19433b125388SAl Viro *displaced = task->files; 19443b125388SAl Viro task_lock(task); 19453b125388SAl Viro task->files = copy; 19463b125388SAl Viro task_unlock(task); 19473b125388SAl Viro return 0; 19483b125388SAl Viro } 1949