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> 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds #include <asm/pgtable.h> 751da177e4SLinus Torvalds #include <asm/pgalloc.h> 761da177e4SLinus Torvalds #include <asm/uaccess.h> 771da177e4SLinus Torvalds #include <asm/mmu_context.h> 781da177e4SLinus Torvalds #include <asm/cacheflush.h> 791da177e4SLinus Torvalds #include <asm/tlbflush.h> 801da177e4SLinus Torvalds 81ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 82ad8d75ffSSteven Rostedt 8343d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 8443d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 8543d2b113SKAMEZAWA Hiroyuki 861da177e4SLinus Torvalds /* 871da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 901da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 97db1466b3SPaul E. McKenney 98db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 99db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 100db1466b3SPaul E. McKenney { 101db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 102db1466b3SPaul E. McKenney } 103db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 104db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds int nr_processes(void) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds int cpu; 1091da177e4SLinus Torvalds int total = 0; 1101da177e4SLinus Torvalds 1111d510750SIan Campbell for_each_possible_cpu(cpu) 1121da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds return total; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 117f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 118f19b9f74SAkinobu Mita { 119f19b9f74SAkinobu Mita } 120f19b9f74SAkinobu Mita 121f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 122e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 12341101809SThomas Gleixner 12441101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 12541101809SThomas Gleixner { 12641101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 12741101809SThomas Gleixner } 12841101809SThomas Gleixner 12941101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 13041101809SThomas Gleixner { 13141101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 13241101809SThomas Gleixner } 1331da177e4SLinus Torvalds #endif 1341da177e4SLinus Torvalds 135f19b9f74SAkinobu Mita void __weak arch_release_thread_info(struct thread_info *ti) 136f19b9f74SAkinobu Mita { 137f19b9f74SAkinobu Mita } 138f19b9f74SAkinobu Mita 139f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR 14041101809SThomas Gleixner 1410d15d74aSThomas Gleixner /* 1420d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1430d15d74aSThomas Gleixner * kmemcache based allocator. 1440d15d74aSThomas Gleixner */ 1450d15d74aSThomas Gleixner # if THREAD_SIZE >= PAGE_SIZE 146b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 147b6a84016SEric Dumazet int node) 148b69c49b7SFUJITA Tomonori { 1492889f608SThomas Gleixner struct page *page = alloc_pages_node(node, THREADINFO_GFP, 1502889f608SThomas Gleixner THREAD_SIZE_ORDER); 151b6a84016SEric Dumazet 152b6a84016SEric Dumazet return page ? page_address(page) : NULL; 153b69c49b7SFUJITA Tomonori } 154b69c49b7SFUJITA Tomonori 155b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti) 156b69c49b7SFUJITA Tomonori { 157b69c49b7SFUJITA Tomonori free_pages((unsigned long)ti, THREAD_SIZE_ORDER); 158b69c49b7SFUJITA Tomonori } 1590d15d74aSThomas Gleixner # else 1600d15d74aSThomas Gleixner static struct kmem_cache *thread_info_cache; 1610d15d74aSThomas Gleixner 1620d15d74aSThomas Gleixner static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 1630d15d74aSThomas Gleixner int node) 1640d15d74aSThomas Gleixner { 1650d15d74aSThomas Gleixner return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node); 1660d15d74aSThomas Gleixner } 1670d15d74aSThomas Gleixner 1680d15d74aSThomas Gleixner static void free_thread_info(struct thread_info *ti) 1690d15d74aSThomas Gleixner { 1700d15d74aSThomas Gleixner kmem_cache_free(thread_info_cache, ti); 1710d15d74aSThomas Gleixner } 1720d15d74aSThomas Gleixner 1730d15d74aSThomas Gleixner void thread_info_cache_init(void) 1740d15d74aSThomas Gleixner { 1750d15d74aSThomas Gleixner thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE, 1760d15d74aSThomas Gleixner THREAD_SIZE, 0, NULL); 1770d15d74aSThomas Gleixner BUG_ON(thread_info_cache == NULL); 1780d15d74aSThomas Gleixner } 1790d15d74aSThomas Gleixner # endif 180b69c49b7SFUJITA Tomonori #endif 181b69c49b7SFUJITA Tomonori 1821da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 183e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 186e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 189e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 192e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 195e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 198e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 1991da177e4SLinus Torvalds 200c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account) 201c6a7f572SKOSAKI Motohiro { 202c6a7f572SKOSAKI Motohiro struct zone *zone = page_zone(virt_to_page(ti)); 203c6a7f572SKOSAKI Motohiro 204c6a7f572SKOSAKI Motohiro mod_zone_page_state(zone, NR_KERNEL_STACK, account); 205c6a7f572SKOSAKI Motohiro } 206c6a7f572SKOSAKI Motohiro 2071da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 2081da177e4SLinus Torvalds { 209c6a7f572SKOSAKI Motohiro account_kernel_stack(tsk->stack, -1); 210f19b9f74SAkinobu Mita arch_release_thread_info(tsk->stack); 211f7e4217bSRoman Zippel free_thread_info(tsk->stack); 21223f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 213fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 214e2cfabdfSWill Drewry put_seccomp_filter(tsk); 215f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 2161da177e4SLinus Torvalds free_task_struct(tsk); 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 2191da177e4SLinus Torvalds 220ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig) 221ea6d290cSOleg Nesterov { 22297101eb4SOleg Nesterov taskstats_tgid_free(sig); 2231c5354deSMike Galbraith sched_autogroup_exit(sig); 224ea6d290cSOleg Nesterov kmem_cache_free(signal_cachep, sig); 225ea6d290cSOleg Nesterov } 226ea6d290cSOleg Nesterov 227ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig) 228ea6d290cSOleg Nesterov { 2291c5354deSMike Galbraith if (atomic_dec_and_test(&sig->sigcnt)) 230ea6d290cSOleg Nesterov free_signal_struct(sig); 231ea6d290cSOleg Nesterov } 232ea6d290cSOleg Nesterov 233158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 2341da177e4SLinus Torvalds { 235270f722dSEugene Teo WARN_ON(!tsk->exit_state); 2361da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 2371da177e4SLinus Torvalds WARN_ON(tsk == current); 2381da177e4SLinus Torvalds 2391a2a4d06SKees Cook security_task_free(tsk); 240e0e81739SDavid Howells exit_creds(tsk); 24135df17c5SShailabh Nagar delayacct_tsk_free(tsk); 242ea6d290cSOleg Nesterov put_signal_struct(tsk->signal); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 2451da177e4SLinus Torvalds free_task(tsk); 2461da177e4SLinus Torvalds } 24777c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct); 2481da177e4SLinus Torvalds 2496c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { } 25061c4628bSSuresh Siddha 2511da177e4SLinus Torvalds void __init fork_init(unsigned long mempages) 2521da177e4SLinus Torvalds { 253f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 2541da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 2551da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 2561da177e4SLinus Torvalds #endif 2571da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 2581da177e4SLinus Torvalds task_struct_cachep = 2591da177e4SLinus Torvalds kmem_cache_create("task_struct", sizeof(struct task_struct), 2602dff4405SVegard Nossum ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL); 2611da177e4SLinus Torvalds #endif 2621da177e4SLinus Torvalds 26361c4628bSSuresh Siddha /* do the arch specific task caches init */ 26461c4628bSSuresh Siddha arch_task_cache_init(); 26561c4628bSSuresh Siddha 2661da177e4SLinus Torvalds /* 2671da177e4SLinus Torvalds * The default maximum number of threads is set to a safe 2681da177e4SLinus Torvalds * value: the thread structures can take up at most half 2691da177e4SLinus Torvalds * of memory. 2701da177e4SLinus Torvalds */ 2711da177e4SLinus Torvalds max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds /* 2741da177e4SLinus Torvalds * we need to allow at least 20 threads to boot a system 2751da177e4SLinus Torvalds */ 2761da177e4SLinus Torvalds if (max_threads < 20) 2771da177e4SLinus Torvalds max_threads = 20; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 2801da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 2811da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 2821da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 28561c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst, 28661c4628bSSuresh Siddha struct task_struct *src) 28761c4628bSSuresh Siddha { 28861c4628bSSuresh Siddha *dst = *src; 28961c4628bSSuresh Siddha return 0; 29061c4628bSSuresh Siddha } 29161c4628bSSuresh Siddha 2921da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds struct task_struct *tsk; 2951da177e4SLinus Torvalds struct thread_info *ti; 2967c9f8861SEric Sandeen unsigned long *stackend; 297207205a2SEric Dumazet int node = tsk_fork_get_node(orig); 2983e26c149SPeter Zijlstra int err; 2991da177e4SLinus Torvalds 300504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 3011da177e4SLinus Torvalds if (!tsk) 3021da177e4SLinus Torvalds return NULL; 3031da177e4SLinus Torvalds 304b6a84016SEric Dumazet ti = alloc_thread_info_node(tsk, node); 305f19b9f74SAkinobu Mita if (!ti) 306f19b9f74SAkinobu Mita goto free_tsk; 3071da177e4SLinus Torvalds 30861c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 30961c4628bSSuresh Siddha if (err) 310f19b9f74SAkinobu Mita goto free_ti; 31161c4628bSSuresh Siddha 31287bec58aSAndrew Morton tsk->stack = ti; 31387bec58aSAndrew Morton 31487bec58aSAndrew Morton setup_thread_stack(tsk, orig); 3158e7cac79SAvi Kivity clear_user_return_notifier(tsk); 316f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 3177c9f8861SEric Sandeen stackend = end_of_stack(tsk); 3187c9f8861SEric Sandeen *stackend = STACK_END_MAGIC; /* for overflow detection */ 3191da177e4SLinus Torvalds 3200a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 3210a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 3220a425405SArjan van de Ven #endif 3230a425405SArjan van de Ven 324fb0a685cSDaniel Rebelo de Oliveira /* 325fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 326fb0a685cSDaniel Rebelo de Oliveira * parent) 327fb0a685cSDaniel Rebelo de Oliveira */ 3281da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 3296c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 3302056a782SJens Axboe tsk->btrace_seq = 0; 3316c5c9341SAlexey Dobriyan #endif 332a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 3335640f768SEric Dumazet tsk->task_frag.page = NULL; 334c6a7f572SKOSAKI Motohiro 335c6a7f572SKOSAKI Motohiro account_kernel_stack(ti, 1); 336c6a7f572SKOSAKI Motohiro 3371da177e4SLinus Torvalds return tsk; 33861c4628bSSuresh Siddha 339f19b9f74SAkinobu Mita free_ti: 34061c4628bSSuresh Siddha free_thread_info(ti); 341f19b9f74SAkinobu Mita free_tsk: 34261c4628bSSuresh Siddha free_task_struct(tsk); 34361c4628bSSuresh Siddha return NULL; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds #ifdef CONFIG_MMU 347a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 3481da177e4SLinus Torvalds { 349297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 3501da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 3511da177e4SLinus Torvalds int retval; 3521da177e4SLinus Torvalds unsigned long charge; 3531da177e4SLinus Torvalds struct mempolicy *pol; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 356ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 357f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 358ad339451SIngo Molnar /* 359ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 360ad339451SIngo Molnar */ 361ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 3627ee78232SHugh Dickins 3631da177e4SLinus Torvalds mm->locked_vm = 0; 3641da177e4SLinus Torvalds mm->mmap = NULL; 3651da177e4SLinus Torvalds mm->mmap_cache = NULL; 3661da177e4SLinus Torvalds mm->free_area_cache = oldmm->mmap_base; 3671363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 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) { 415f3a43f3fSJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 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); 4721da177e4SLinus Torvalds return retval; 4735beb4930SRik van Riel fail_nomem_anon_vma_fork: 4745beb4930SRik van Riel mpol_put(pol); 4751da177e4SLinus Torvalds fail_nomem_policy: 4761da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 4771da177e4SLinus Torvalds fail_nomem: 4781da177e4SLinus Torvalds retval = -ENOMEM; 4791da177e4SLinus Torvalds vm_unacct_memory(charge); 4801da177e4SLinus Torvalds goto out; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 4861da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 4871da177e4SLinus Torvalds return -ENOMEM; 4881da177e4SLinus Torvalds return 0; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 4921da177e4SLinus Torvalds { 4935e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds #else 4961da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 4971da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 4981da177e4SLinus Torvalds #define mm_free_pgd(mm) 4991da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 5021da177e4SLinus Torvalds 503e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 5041da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 5051da177e4SLinus Torvalds 5064cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 5074cb0e11bSHidehiro Kawai 5084cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 5094cb0e11bSHidehiro Kawai { 5104cb0e11bSHidehiro Kawai default_dump_filter = 5114cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 5124cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 5134cb0e11bSHidehiro Kawai return 1; 5144cb0e11bSHidehiro Kawai } 5154cb0e11bSHidehiro Kawai 5164cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 5174cb0e11bSHidehiro Kawai 5181da177e4SLinus Torvalds #include <linux/init_task.h> 5191da177e4SLinus Torvalds 520858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 521858f0993SAlexey Dobriyan { 522858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 523858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 524858f0993SAlexey Dobriyan INIT_HLIST_HEAD(&mm->ioctx_list); 525858f0993SAlexey Dobriyan #endif 526858f0993SAlexey Dobriyan } 527858f0993SAlexey Dobriyan 52878fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 5291da177e4SLinus Torvalds { 5301da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 5311da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 5321da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 5331da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 534f8af4da3SHugh Dickins mm->flags = (current->mm) ? 535f8af4da3SHugh Dickins (current->mm->flags & MMF_INIT_MASK) : default_dump_filter; 536999d9fc1SOleg Nesterov mm->core_state = NULL; 5371da177e4SLinus Torvalds mm->nr_ptes = 0; 538d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 5391da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 5401da177e4SLinus Torvalds mm->free_area_cache = TASK_UNMAPPED_BASE; 5411363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 542858f0993SAlexey Dobriyan mm_init_aio(mm); 543cf475ad2SBalbir Singh mm_init_owner(mm, p); 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds if (likely(!mm_alloc_pgd(mm))) { 5461da177e4SLinus Torvalds mm->def_flags = 0; 547cddb8a5cSAndrea Arcangeli mmu_notifier_mm_init(mm); 5481da177e4SLinus Torvalds return mm; 5491da177e4SLinus Torvalds } 55078fb7466SPavel Emelianov 5511da177e4SLinus Torvalds free_mm(mm); 5521da177e4SLinus Torvalds return NULL; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 555c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm) 556c3f0327fSKonstantin Khlebnikov { 557c3f0327fSKonstantin Khlebnikov int i; 558c3f0327fSKonstantin Khlebnikov 559c3f0327fSKonstantin Khlebnikov for (i = 0; i < NR_MM_COUNTERS; i++) { 560c3f0327fSKonstantin Khlebnikov long x = atomic_long_read(&mm->rss_stat.count[i]); 561c3f0327fSKonstantin Khlebnikov 562c3f0327fSKonstantin Khlebnikov if (unlikely(x)) 563c3f0327fSKonstantin Khlebnikov printk(KERN_ALERT "BUG: Bad rss-counter state " 564c3f0327fSKonstantin Khlebnikov "mm:%p idx:%d val:%ld\n", mm, i, x); 565c3f0327fSKonstantin Khlebnikov } 566c3f0327fSKonstantin Khlebnikov 567c3f0327fSKonstantin Khlebnikov #ifdef CONFIG_TRANSPARENT_HUGEPAGE 568c3f0327fSKonstantin Khlebnikov VM_BUG_ON(mm->pmd_huge_pte); 569c3f0327fSKonstantin Khlebnikov #endif 570c3f0327fSKonstantin Khlebnikov } 571c3f0327fSKonstantin Khlebnikov 5721da177e4SLinus Torvalds /* 5731da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 5741da177e4SLinus Torvalds */ 5751da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 5761da177e4SLinus Torvalds { 5771da177e4SLinus Torvalds struct mm_struct *mm; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds mm = allocate_mm(); 580de03c72cSKOSAKI Motohiro if (!mm) 581de03c72cSKOSAKI Motohiro return NULL; 582de03c72cSKOSAKI Motohiro 5831da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 5846345d24dSLinus Torvalds mm_init_cpumask(mm); 5856345d24dSLinus Torvalds return mm_init(mm, current); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds /* 5891da177e4SLinus Torvalds * Called when the last reference to the mm 5901da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 5911da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 5921da177e4SLinus Torvalds */ 5937ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 5961da177e4SLinus Torvalds mm_free_pgd(mm); 5971da177e4SLinus Torvalds destroy_context(mm); 598cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 599c3f0327fSKonstantin Khlebnikov check_mm(mm); 6001da177e4SLinus Torvalds free_mm(mm); 6011da177e4SLinus Torvalds } 6026d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds /* 6051da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 6061da177e4SLinus Torvalds */ 6071da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 6081da177e4SLinus Torvalds { 6090ae26f1bSAndrew Morton might_sleep(); 6100ae26f1bSAndrew Morton 6111da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 612d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 6131da177e4SLinus Torvalds exit_aio(mm); 6141c2fb7a4SAndrea Arcangeli ksm_exit(mm); 615ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 6161da177e4SLinus Torvalds exit_mmap(mm); 617925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 6181da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 6191da177e4SLinus Torvalds spin_lock(&mmlist_lock); 6201da177e4SLinus Torvalds list_del(&mm->mmlist); 6211da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 6221da177e4SLinus Torvalds } 623801460d0SHiroshi Shimamoto if (mm->binfmt) 624801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 6251da177e4SLinus Torvalds mmdrop(mm); 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 6291da177e4SLinus Torvalds 63038646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 63138646013SJiri Slaby { 63238646013SJiri Slaby if (new_exe_file) 63338646013SJiri Slaby get_file(new_exe_file); 63438646013SJiri Slaby if (mm->exe_file) 63538646013SJiri Slaby fput(mm->exe_file); 63638646013SJiri Slaby mm->exe_file = new_exe_file; 63738646013SJiri Slaby } 63838646013SJiri Slaby 63938646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 64038646013SJiri Slaby { 64138646013SJiri Slaby struct file *exe_file; 64238646013SJiri Slaby 6432dd8ad81SKonstantin Khlebnikov /* We need mmap_sem to protect against races with removal of exe_file */ 64438646013SJiri Slaby down_read(&mm->mmap_sem); 64538646013SJiri Slaby exe_file = mm->exe_file; 64638646013SJiri Slaby if (exe_file) 64738646013SJiri Slaby get_file(exe_file); 64838646013SJiri Slaby up_read(&mm->mmap_sem); 64938646013SJiri Slaby return exe_file; 65038646013SJiri Slaby } 65138646013SJiri Slaby 65238646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 65338646013SJiri Slaby { 65438646013SJiri Slaby /* It's safe to write the exe_file pointer without exe_file_lock because 65538646013SJiri Slaby * this is called during fork when the task is not yet in /proc */ 65638646013SJiri Slaby newmm->exe_file = get_mm_exe_file(oldmm); 65738646013SJiri Slaby } 65838646013SJiri Slaby 6591da177e4SLinus Torvalds /** 6601da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 6611da177e4SLinus Torvalds * 662246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 6631da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 6641da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 6651da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 6661da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 6671da177e4SLinus Torvalds */ 6681da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 6691da177e4SLinus Torvalds { 6701da177e4SLinus Torvalds struct mm_struct *mm; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds task_lock(task); 6731da177e4SLinus Torvalds mm = task->mm; 6741da177e4SLinus Torvalds if (mm) { 675246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 6761da177e4SLinus Torvalds mm = NULL; 6771da177e4SLinus Torvalds else 6781da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds task_unlock(task); 6811da177e4SLinus Torvalds return mm; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 6841da177e4SLinus Torvalds 6858cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 6868cdb878dSChristopher Yeoh { 6878cdb878dSChristopher Yeoh struct mm_struct *mm; 6888cdb878dSChristopher Yeoh int err; 6898cdb878dSChristopher Yeoh 6908cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 6918cdb878dSChristopher Yeoh if (err) 6928cdb878dSChristopher Yeoh return ERR_PTR(err); 6938cdb878dSChristopher Yeoh 6948cdb878dSChristopher Yeoh mm = get_task_mm(task); 6958cdb878dSChristopher Yeoh if (mm && mm != current->mm && 6968cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 6978cdb878dSChristopher Yeoh mmput(mm); 6988cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 6998cdb878dSChristopher Yeoh } 7008cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 7018cdb878dSChristopher Yeoh 7028cdb878dSChristopher Yeoh return mm; 7038cdb878dSChristopher Yeoh } 7048cdb878dSChristopher Yeoh 70557b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 706c415c3b4SOleg Nesterov { 707d68b46feSOleg Nesterov struct completion *vfork; 708c415c3b4SOleg Nesterov 709d68b46feSOleg Nesterov task_lock(tsk); 710d68b46feSOleg Nesterov vfork = tsk->vfork_done; 711d68b46feSOleg Nesterov if (likely(vfork)) { 712c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 713d68b46feSOleg Nesterov complete(vfork); 714d68b46feSOleg Nesterov } 715d68b46feSOleg Nesterov task_unlock(tsk); 716d68b46feSOleg Nesterov } 717d68b46feSOleg Nesterov 718d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 719d68b46feSOleg Nesterov struct completion *vfork) 720d68b46feSOleg Nesterov { 721d68b46feSOleg Nesterov int killed; 722d68b46feSOleg Nesterov 723d68b46feSOleg Nesterov freezer_do_not_count(); 724d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 725d68b46feSOleg Nesterov freezer_count(); 726d68b46feSOleg Nesterov 727d68b46feSOleg Nesterov if (killed) { 728d68b46feSOleg Nesterov task_lock(child); 729d68b46feSOleg Nesterov child->vfork_done = NULL; 730d68b46feSOleg Nesterov task_unlock(child); 731d68b46feSOleg Nesterov } 732d68b46feSOleg Nesterov 733d68b46feSOleg Nesterov put_task_struct(child); 734d68b46feSOleg Nesterov return killed; 735c415c3b4SOleg Nesterov } 736c415c3b4SOleg Nesterov 7371da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 7381da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 7391da177e4SLinus Torvalds * error success whatever. 7401da177e4SLinus Torvalds * 7411da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 7421da177e4SLinus Torvalds * from the current process. 7431da177e4SLinus Torvalds * 7441da177e4SLinus Torvalds * This difference is important for error handling, when we 7451da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 7461da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 7471da177e4SLinus Torvalds * restoring the old one. . . 7481da177e4SLinus Torvalds * Eric Biederman 10 January 1998 7491da177e4SLinus Torvalds */ 7501da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 7511da177e4SLinus Torvalds { 7528141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 7538141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 754fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 7558141c7f3SLinus Torvalds exit_robust_list(tsk); 756fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 757fc6b177dSPeter Zijlstra } 7588141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 759fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 7608141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 761fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 762fc6b177dSPeter Zijlstra } 7638141c7f3SLinus Torvalds #endif 764322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 765322a2c10SThomas Gleixner exit_pi_state_list(tsk); 7668141c7f3SLinus Torvalds #endif 7678141c7f3SLinus Torvalds 7680326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 7690326f5a9SSrikar Dronamraju 7701da177e4SLinus Torvalds /* Get rid of any cached register state */ 7711da177e4SLinus Torvalds deactivate_mm(tsk, mm); 7721da177e4SLinus Torvalds 773fec1d011SRoland McGrath /* 774fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 775fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 776fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 777d68b46feSOleg Nesterov * trouble, say, a killed vfork parent shouldn't touch this mm. 778d68b46feSOleg Nesterov * Userland only wants this done for a sys_exit. 779fec1d011SRoland McGrath */ 7809c8a8228SEric Dumazet if (tsk->clear_child_tid) { 7819c8a8228SEric Dumazet if (!(tsk->flags & PF_SIGNALED) && 7829c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * We don't check the error code - if userspace has 7851da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 7861da177e4SLinus Torvalds */ 7879c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 7889c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 7899c8a8228SEric Dumazet 1, NULL, NULL, 0); 7909c8a8228SEric Dumazet } 7919c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 7921da177e4SLinus Torvalds } 793f7505d64SKonstantin Khlebnikov 794f7505d64SKonstantin Khlebnikov /* 795f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 796f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 797f7505d64SKonstantin Khlebnikov */ 798f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 799f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 802a0a7ec30SJANAK DESAI /* 803a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 804a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 805a0a7ec30SJANAK DESAI */ 806402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk) 807a0a7ec30SJANAK DESAI { 808a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 809a0a7ec30SJANAK DESAI int err; 810a0a7ec30SJANAK DESAI 811a0a7ec30SJANAK DESAI if (!oldmm) 812a0a7ec30SJANAK DESAI return NULL; 813a0a7ec30SJANAK DESAI 814a0a7ec30SJANAK DESAI mm = allocate_mm(); 815a0a7ec30SJANAK DESAI if (!mm) 816a0a7ec30SJANAK DESAI goto fail_nomem; 817a0a7ec30SJANAK DESAI 818a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 8196345d24dSLinus Torvalds mm_init_cpumask(mm); 820a0a7ec30SJANAK DESAI 821e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE 822e7a00c45SAndrea Arcangeli mm->pmd_huge_pte = NULL; 823e7a00c45SAndrea Arcangeli #endif 82478fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 825a0a7ec30SJANAK DESAI goto fail_nomem; 826a0a7ec30SJANAK DESAI 827a0a7ec30SJANAK DESAI if (init_new_context(tsk, mm)) 828a0a7ec30SJANAK DESAI goto fail_nocontext; 829a0a7ec30SJANAK DESAI 830925d1c40SMatt Helsley dup_mm_exe_file(oldmm, mm); 831925d1c40SMatt Helsley 832a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 833a0a7ec30SJANAK DESAI if (err) 834a0a7ec30SJANAK DESAI goto free_pt; 835a0a7ec30SJANAK DESAI 836a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 837a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 838a0a7ec30SJANAK DESAI 839801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 840801460d0SHiroshi Shimamoto goto free_pt; 841801460d0SHiroshi Shimamoto 842a0a7ec30SJANAK DESAI return mm; 843a0a7ec30SJANAK DESAI 844a0a7ec30SJANAK DESAI free_pt: 845801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 846801460d0SHiroshi Shimamoto mm->binfmt = NULL; 847a0a7ec30SJANAK DESAI mmput(mm); 848a0a7ec30SJANAK DESAI 849a0a7ec30SJANAK DESAI fail_nomem: 850a0a7ec30SJANAK DESAI return NULL; 851a0a7ec30SJANAK DESAI 852a0a7ec30SJANAK DESAI fail_nocontext: 853a0a7ec30SJANAK DESAI /* 854a0a7ec30SJANAK DESAI * If init_new_context() failed, we cannot use mmput() to free the mm 855a0a7ec30SJANAK DESAI * because it calls destroy_context() 856a0a7ec30SJANAK DESAI */ 857a0a7ec30SJANAK DESAI mm_free_pgd(mm); 858a0a7ec30SJANAK DESAI free_mm(mm); 859a0a7ec30SJANAK DESAI return NULL; 860a0a7ec30SJANAK DESAI } 861a0a7ec30SJANAK DESAI 8621da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 8651da177e4SLinus Torvalds int retval; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 8681da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 86917406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 87017406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 87117406b82SMandeep Singh Baines #endif 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds tsk->mm = NULL; 8741da177e4SLinus Torvalds tsk->active_mm = NULL; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* 8771da177e4SLinus Torvalds * Are we cloning a kernel thread? 8781da177e4SLinus Torvalds * 8791da177e4SLinus Torvalds * We need to steal a active VM for that.. 8801da177e4SLinus Torvalds */ 8811da177e4SLinus Torvalds oldmm = current->mm; 8821da177e4SLinus Torvalds if (!oldmm) 8831da177e4SLinus Torvalds return 0; 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 8861da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 8871da177e4SLinus Torvalds mm = oldmm; 8881da177e4SLinus Torvalds goto good_mm; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds retval = -ENOMEM; 892a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 8931da177e4SLinus Torvalds if (!mm) 8941da177e4SLinus Torvalds goto fail_nomem; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds good_mm: 8971da177e4SLinus Torvalds tsk->mm = mm; 8981da177e4SLinus Torvalds tsk->active_mm = mm; 8991da177e4SLinus Torvalds return 0; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds fail_nomem: 9021da177e4SLinus Torvalds return retval; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 905a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 9061da177e4SLinus Torvalds { 907498052bbSAl Viro struct fs_struct *fs = current->fs; 9081da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 909498052bbSAl Viro /* tsk->fs is already what we want */ 9102a4419b5SNick Piggin spin_lock(&fs->lock); 911498052bbSAl Viro if (fs->in_exec) { 9122a4419b5SNick Piggin spin_unlock(&fs->lock); 913498052bbSAl Viro return -EAGAIN; 914498052bbSAl Viro } 915498052bbSAl Viro fs->users++; 9162a4419b5SNick Piggin spin_unlock(&fs->lock); 9171da177e4SLinus Torvalds return 0; 9181da177e4SLinus Torvalds } 919498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 9201da177e4SLinus Torvalds if (!tsk->fs) 9211da177e4SLinus Torvalds return -ENOMEM; 9221da177e4SLinus Torvalds return 0; 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds 925a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 926a016f338SJANAK DESAI { 927a016f338SJANAK DESAI struct files_struct *oldf, *newf; 928a016f338SJANAK DESAI int error = 0; 929a016f338SJANAK DESAI 930a016f338SJANAK DESAI /* 931a016f338SJANAK DESAI * A background process may not have any files ... 932a016f338SJANAK DESAI */ 933a016f338SJANAK DESAI oldf = current->files; 934a016f338SJANAK DESAI if (!oldf) 935a016f338SJANAK DESAI goto out; 936a016f338SJANAK DESAI 937a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 938a016f338SJANAK DESAI atomic_inc(&oldf->count); 939a016f338SJANAK DESAI goto out; 940a016f338SJANAK DESAI } 941a016f338SJANAK DESAI 942a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 943a016f338SJANAK DESAI if (!newf) 944a016f338SJANAK DESAI goto out; 945a016f338SJANAK DESAI 946a016f338SJANAK DESAI tsk->files = newf; 947a016f338SJANAK DESAI error = 0; 948a016f338SJANAK DESAI out: 949a016f338SJANAK DESAI return error; 950a016f338SJANAK DESAI } 951a016f338SJANAK DESAI 952fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 953fd0928dfSJens Axboe { 954fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 955fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 9566e736be7STejun Heo struct io_context *new_ioc; 957fd0928dfSJens Axboe 958fd0928dfSJens Axboe if (!ioc) 959fd0928dfSJens Axboe return 0; 960fadad878SJens Axboe /* 961fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 962fadad878SJens Axboe */ 963fadad878SJens Axboe if (clone_flags & CLONE_IO) { 9643d48749dSTejun Heo ioc_task_link(ioc); 9653d48749dSTejun Heo tsk->io_context = ioc; 966fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 9676e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 9686e736be7STejun Heo if (unlikely(!new_ioc)) 969fd0928dfSJens Axboe return -ENOMEM; 970fd0928dfSJens Axboe 9716e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 97211a3122fSTejun Heo put_io_context(new_ioc); 973fd0928dfSJens Axboe } 974fd0928dfSJens Axboe #endif 975fd0928dfSJens Axboe return 0; 976fd0928dfSJens Axboe } 977fd0928dfSJens Axboe 978a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 9791da177e4SLinus Torvalds { 9801da177e4SLinus Torvalds struct sighand_struct *sig; 9811da177e4SLinus Torvalds 98260348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 9831da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 9841da177e4SLinus Torvalds return 0; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 987e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 9881da177e4SLinus Torvalds if (!sig) 9891da177e4SLinus Torvalds return -ENOMEM; 9901da177e4SLinus Torvalds atomic_set(&sig->count, 1); 9911da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 9921da177e4SLinus Torvalds return 0; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 995a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 996c81addc9SOleg Nesterov { 997d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 998d80e731eSOleg Nesterov signalfd_cleanup(sighand); 999c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1000c81addc9SOleg Nesterov } 1001d80e731eSOleg Nesterov } 1002c81addc9SOleg Nesterov 1003f06febc9SFrank Mayhar 1004f06febc9SFrank Mayhar /* 1005f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1006f06febc9SFrank Mayhar */ 1007f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1008f06febc9SFrank Mayhar { 100978d7d407SJiri Slaby unsigned long cpu_limit; 101078d7d407SJiri Slaby 1011f06febc9SFrank Mayhar /* Thread group counters. */ 1012f06febc9SFrank Mayhar thread_group_cputime_init(sig); 1013f06febc9SFrank Mayhar 101478d7d407SJiri Slaby cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 101578d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 101678d7d407SJiri Slaby sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); 10176279a751SOleg Nesterov sig->cputimer.running = 1; 10186279a751SOleg Nesterov } 10196279a751SOleg Nesterov 1020f06febc9SFrank Mayhar /* The timer lists. */ 1021f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1022f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1023f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1024f06febc9SFrank Mayhar } 1025f06febc9SFrank Mayhar 1026a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 10271da177e4SLinus Torvalds { 10281da177e4SLinus Torvalds struct signal_struct *sig; 10291da177e4SLinus Torvalds 10304ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1031490dea45SPeter Zijlstra return 0; 10326279a751SOleg Nesterov 1033a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 10341da177e4SLinus Torvalds tsk->signal = sig; 10351da177e4SLinus Torvalds if (!sig) 10361da177e4SLinus Torvalds return -ENOMEM; 10371da177e4SLinus Torvalds 1038b3ac022cSOleg Nesterov sig->nr_threads = 1; 10391da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1040b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 10411da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1042b3bfa0cbSSukadev Bhattiprolu if (clone_flags & CLONE_NEWPID) 1043b3bfa0cbSSukadev Bhattiprolu sig->flags |= SIGNAL_UNKILLABLE; 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 1120f06febc9SFrank Mayhar /* 11211da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 11221da177e4SLinus Torvalds * but does not actually start it yet. 11231da177e4SLinus Torvalds * 11241da177e4SLinus Torvalds * It copies the registers, and all the appropriate 11251da177e4SLinus Torvalds * parts of the process environment (as per the clone 11261da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 11271da177e4SLinus Torvalds */ 112836c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 11291da177e4SLinus Torvalds unsigned long stack_start, 11301da177e4SLinus Torvalds unsigned long stack_size, 11311da177e4SLinus Torvalds int __user *child_tidptr, 113209a05394SRoland McGrath struct pid *pid, 113309a05394SRoland McGrath int trace) 11341da177e4SLinus Torvalds { 11351da177e4SLinus Torvalds int retval; 1136a24efe62SMariusz Kozlowski struct task_struct *p; 1137b4f48b63SPaul Menage int cgroup_callbacks_done = 0; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 11401da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds /* 11431da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 11441da177e4SLinus Torvalds * can only be started up within the thread group. 11451da177e4SLinus Torvalds */ 11461da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 11471da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds /* 11501da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 11511da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 11521da177e4SLinus Torvalds * for various simplifications in other code. 11531da177e4SLinus Torvalds */ 11541da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 11551da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11561da177e4SLinus Torvalds 1157123be07bSSukadev Bhattiprolu /* 1158123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1159123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1160123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1161123be07bSSukadev Bhattiprolu * from creating siblings. 1162123be07bSSukadev Bhattiprolu */ 1163123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1164123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1165123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1166123be07bSSukadev Bhattiprolu 11671da177e4SLinus Torvalds retval = security_task_create(clone_flags); 11681da177e4SLinus Torvalds if (retval) 11691da177e4SLinus Torvalds goto fork_out; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds retval = -ENOMEM; 11721da177e4SLinus Torvalds p = dup_task_struct(current); 11731da177e4SLinus Torvalds if (!p) 11741da177e4SLinus Torvalds goto fork_out; 11751da177e4SLinus Torvalds 1176f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1177e2cfabdfSWill Drewry get_seccomp_filter(p); 1178f7e8b616SSteven Rostedt 1179bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1180bea493a0SPeter Zijlstra 1181d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1182de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1183de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1184de30a2b3SIngo Molnar #endif 11851da177e4SLinus Torvalds retval = -EAGAIN; 11863b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 118778d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 11881da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) && 118918b6e041SSerge Hallyn p->real_cred->user != INIT_USER) 11901da177e4SLinus Torvalds goto bad_fork_free; 11911da177e4SLinus Torvalds } 119272fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 11931da177e4SLinus Torvalds 1194f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1195f1752eecSDavid Howells if (retval < 0) 1196f1752eecSDavid Howells goto bad_fork_free; 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds /* 11991da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 12001da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 12011da177e4SLinus Torvalds * to stop root fork bombs. 12021da177e4SLinus Torvalds */ 120304ec93feSLi Zefan retval = -EAGAIN; 12041da177e4SLinus Torvalds if (nr_threads >= max_threads) 12051da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12061da177e4SLinus Torvalds 1207a1261f54SAl Viro if (!try_module_get(task_thread_info(p)->exec_domain->module)) 12081da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds p->did_exec = 0; 1211ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 12121da177e4SLinus Torvalds copy_flags(clone_flags, p); 12131da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 12141da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1215f41d911fSPaul E. McKenney rcu_copy_process(p); 12161da177e4SLinus Torvalds p->vfork_done = NULL; 12171da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds init_sigpending(&p->pending); 12201da177e4SLinus Torvalds 122164861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 122264861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 1223d99ca3b9SHidetoshi Seto #ifndef CONFIG_VIRT_CPU_ACCOUNTING 122464861634SMartin Schwidefsky p->prev_utime = p->prev_stime = 0; 1225d99ca3b9SHidetoshi Seto #endif 1226a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1227a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1228a3a2e76cSKAMEZAWA Hiroyuki #endif 1229172ba844SBalbir Singh 12306976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 12316976675dSArjan van de Ven 12325995477aSAndrea Righi task_io_accounting_init(&p->ioac); 12331da177e4SLinus Torvalds acct_clear_integrals(p); 12341da177e4SLinus Torvalds 1235f06febc9SFrank Mayhar posix_cpu_timers_init(p); 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&p->start_time); 1238924b42d5STomas Janousek p->real_start_time = p->start_time; 1239924b42d5STomas Janousek monotonic_to_bootbased(&p->real_start_time); 12401da177e4SLinus Torvalds p->io_context = NULL; 12411da177e4SLinus Torvalds p->audit_context = NULL; 12424714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1243257058aeSTejun Heo threadgroup_change_begin(current); 1244b4f48b63SPaul Menage cgroup_fork(p); 12451da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1246846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 12471da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 12481da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 12491da177e4SLinus Torvalds p->mempolicy = NULL; 1250b4f48b63SPaul Menage goto bad_fork_cleanup_cgroup; 12511da177e4SLinus Torvalds } 1252c61afb18SPaul Jackson mpol_fix_fork_child_flag(p); 12531da177e4SLinus Torvalds #endif 1254778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1255778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1256778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1257cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1258778d3b0fSMichal Hocko #endif 1259de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1260de30a2b3SIngo Molnar p->irq_events = 0; 1261de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1262de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1263de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1264de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1265de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1266de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1267de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1268de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1269de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1270de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1271de30a2b3SIngo Molnar p->hardirq_context = 0; 1272de30a2b3SIngo Molnar p->softirq_context = 0; 1273de30a2b3SIngo Molnar #endif 1274fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1275fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1276fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1277fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1278fbb9ce95SIngo Molnar #endif 12791da177e4SLinus Torvalds 1280408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1281408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1282408894eeSIngo Molnar #endif 1283c255a458SAndrew Morton #ifdef CONFIG_MEMCG 1284569b846dSKAMEZAWA Hiroyuki p->memcg_batch.do_batch = 0; 1285569b846dSKAMEZAWA Hiroyuki p->memcg_batch.memcg = NULL; 1286569b846dSKAMEZAWA Hiroyuki #endif 12870f481406SMarkus Metzger 12883c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 12893e51e3edSSamir Bellabes sched_fork(p); 12906ab423e0SPeter Zijlstra 1291cdd6c482SIngo Molnar retval = perf_event_init_task(p); 12926ab423e0SPeter Zijlstra if (retval) 12936ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1294fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1295fb0a685cSDaniel Rebelo de Oliveira if (retval) 1296f1752eecSDavid Howells goto bad_fork_cleanup_policy; 12971da177e4SLinus Torvalds /* copy all the process information */ 1298fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1299fb0a685cSDaniel Rebelo de Oliveira if (retval) 13001da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1301fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1302fb0a685cSDaniel Rebelo de Oliveira if (retval) 13031da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1304fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1305fb0a685cSDaniel Rebelo de Oliveira if (retval) 13061da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1307fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1308fb0a685cSDaniel Rebelo de Oliveira if (retval) 13091da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1310fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1311fb0a685cSDaniel Rebelo de Oliveira if (retval) 13121da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1313fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1314fb0a685cSDaniel Rebelo de Oliveira if (retval) 13151da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1316fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1317fb0a685cSDaniel Rebelo de Oliveira if (retval) 1318d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1319fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1320fb0a685cSDaniel Rebelo de Oliveira if (retval) 1321fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 1322afa86fc4SAl Viro retval = copy_thread(clone_flags, stack_start, stack_size, p); 13231da177e4SLinus Torvalds if (retval) 1324fd0928dfSJens Axboe goto bad_fork_cleanup_io; 13251da177e4SLinus Torvalds 1326425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1327425fb2b4SPavel Emelyanov retval = -ENOMEM; 132861bce0f1SEric W. Biederman pid = alloc_pid(p->nsproxy->pid_ns); 1329425fb2b4SPavel Emelyanov if (!pid) 1330fd0928dfSJens Axboe goto bad_fork_cleanup_io; 1331425fb2b4SPavel Emelyanov } 1332425fb2b4SPavel Emelyanov 1333425fb2b4SPavel Emelyanov p->pid = pid_nr(pid); 1334425fb2b4SPavel Emelyanov p->tgid = p->pid; 1335425fb2b4SPavel Emelyanov if (clone_flags & CLONE_THREAD) 1336425fb2b4SPavel Emelyanov p->tgid = current->tgid; 1337425fb2b4SPavel Emelyanov 13381da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 13391da177e4SLinus Torvalds /* 13401da177e4SLinus Torvalds * Clear TID on mm_release()? 13411da177e4SLinus Torvalds */ 13421da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 134373c10101SJens Axboe #ifdef CONFIG_BLOCK 134473c10101SJens Axboe p->plug = NULL; 134573c10101SJens Axboe #endif 134642b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 13478f17d3a5SIngo Molnar p->robust_list = NULL; 13488f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 13498f17d3a5SIngo Molnar p->compat_robust_list = NULL; 13508f17d3a5SIngo Molnar #endif 1351c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1352c87e2837SIngo Molnar p->pi_state_cache = NULL; 135342b2dd0aSAlexey Dobriyan #endif 13540326f5a9SSrikar Dronamraju uprobe_copy_process(p); 13551da177e4SLinus Torvalds /* 1356f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1357f9a3879aSGOTO Masanori */ 1358f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 1359f9a3879aSGOTO Masanori p->sas_ss_sp = p->sas_ss_size = 0; 1360f9a3879aSGOTO Masanori 1361f9a3879aSGOTO Masanori /* 13626580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 13636580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 13641da177e4SLinus Torvalds */ 13656580807dSOleg Nesterov user_disable_single_step(p); 13661da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1367ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1368ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1369ed75e8d5SLaurent Vivier #endif 13709745512cSArjan van de Ven clear_all_latency_tracing(p); 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds /* ok, now we should be set up.. */ 13735f8aadd8SOleg Nesterov if (clone_flags & CLONE_THREAD) 13745f8aadd8SOleg Nesterov p->exit_signal = -1; 13755f8aadd8SOleg Nesterov else if (clone_flags & CLONE_PARENT) 13765f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 13775f8aadd8SOleg Nesterov else 13785f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 13795f8aadd8SOleg Nesterov 13801da177e4SLinus Torvalds p->pdeath_signal = 0; 13811da177e4SLinus Torvalds p->exit_state = 0; 13821da177e4SLinus Torvalds 13839d823e8fSWu Fengguang p->nr_dirtied = 0; 13849d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 138583712358SWu Fengguang p->dirty_paused_when = 0; 13869d823e8fSWu Fengguang 13871da177e4SLinus Torvalds /* 13881da177e4SLinus Torvalds * Ok, make it visible to the rest of the system. 13891da177e4SLinus Torvalds * We dont wake it up yet. 13901da177e4SLinus Torvalds */ 13911da177e4SLinus Torvalds p->group_leader = p; 139247e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 1393158e1645SAl Viro p->task_works = NULL; 13941da177e4SLinus Torvalds 1395b4f48b63SPaul Menage /* Now that the task is set up, run cgroup callbacks if 1396b4f48b63SPaul Menage * necessary. We need to run them before the task is visible 1397b4f48b63SPaul Menage * on the tasklist. */ 1398b4f48b63SPaul Menage cgroup_fork_callbacks(p); 1399b4f48b63SPaul Menage cgroup_callbacks_done = 1; 1400b4f48b63SPaul Menage 14011da177e4SLinus Torvalds /* Need tasklist lock for parent etc handling! */ 14021da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 14052d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 14061da177e4SLinus Torvalds p->real_parent = current->real_parent; 14072d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 14082d5516cbSOleg Nesterov } else { 14091da177e4SLinus Torvalds p->real_parent = current; 14102d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 14112d5516cbSOleg Nesterov } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 14144a2c7a78SOleg Nesterov 14154a2c7a78SOleg Nesterov /* 14164a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 14174a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 14184a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 14194a2c7a78SOleg Nesterov * it's process group. 14204a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 14214a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 14224a2c7a78SOleg Nesterov */ 14234a2c7a78SOleg Nesterov recalc_sigpending(); 14244a2c7a78SOleg Nesterov if (signal_pending(current)) { 14254a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 14264a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 14274a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 1428f7e8b616SSteven Rostedt goto bad_fork_free_pid; 14294a2c7a78SOleg Nesterov } 14304a2c7a78SOleg Nesterov 14313f17da69SOleg Nesterov if (clone_flags & CLONE_THREAD) { 1432b3ac022cSOleg Nesterov current->signal->nr_threads++; 14334ab6c083SOleg Nesterov atomic_inc(¤t->signal->live); 1434b3ac022cSOleg Nesterov atomic_inc(¤t->signal->sigcnt); 14351da177e4SLinus Torvalds p->group_leader = current->group_leader; 143647e65328SOleg Nesterov list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group); 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 143973b9ebfeSOleg Nesterov if (likely(p->pid)) { 14404b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds if (thread_group_leader(p)) { 144345a68628SEric W. Biederman if (is_child_reaper(pid)) 144430e49c26SPavel Emelyanov p->nsproxy->pid_ns->child_reaper = p; 14455cd17569SEric W. Biederman 1446fea9d175SOleg Nesterov p->signal->leader_pid = pid; 14479c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 14485cd17569SEric W. Biederman attach_pid(p, PIDTYPE_PGID, task_pgrp(current)); 14495cd17569SEric W. Biederman attach_pid(p, PIDTYPE_SID, task_session(current)); 14509cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 14515e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 1452909ea964SChristoph Lameter __this_cpu_inc(process_counts); 14531da177e4SLinus Torvalds } 145485868995SSukadev Bhattiprolu attach_pid(p, PIDTYPE_PID, pid); 14551da177e4SLinus Torvalds nr_threads++; 145673b9ebfeSOleg Nesterov } 145773b9ebfeSOleg Nesterov 14581da177e4SLinus Torvalds total_forks++; 14593f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 14601da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 1461c13cf856SAndrew Morton proc_fork_connector(p); 1462817929ecSPaul Menage cgroup_post_fork(p); 14634714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1464257058aeSTejun Heo threadgroup_change_end(current); 1465cdd6c482SIngo Molnar perf_event_fork(p); 146643d2b113SKAMEZAWA Hiroyuki 146743d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 146843d2b113SKAMEZAWA Hiroyuki 14691da177e4SLinus Torvalds return p; 14701da177e4SLinus Torvalds 1471425fb2b4SPavel Emelyanov bad_fork_free_pid: 1472425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1473425fb2b4SPavel Emelyanov free_pid(pid); 1474fd0928dfSJens Axboe bad_fork_cleanup_io: 1475b69f2292SLouis Rilling if (p->io_context) 1476b69f2292SLouis Rilling exit_io_context(p); 1477ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 14785e2bf014SMike Galbraith if (unlikely(clone_flags & CLONE_NEWPID)) 14795e2bf014SMike Galbraith pid_ns_release_proc(p->nsproxy->pid_ns); 1480444f378bSLinus Torvalds exit_task_namespaces(p); 14811da177e4SLinus Torvalds bad_fork_cleanup_mm: 1482c9f01245SDavid Rientjes if (p->mm) 14831da177e4SLinus Torvalds mmput(p->mm); 14841da177e4SLinus Torvalds bad_fork_cleanup_signal: 14854ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 14861c5354deSMike Galbraith free_signal_struct(p->signal); 14871da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1488a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 14891da177e4SLinus Torvalds bad_fork_cleanup_fs: 14901da177e4SLinus Torvalds exit_fs(p); /* blocking */ 14911da177e4SLinus Torvalds bad_fork_cleanup_files: 14921da177e4SLinus Torvalds exit_files(p); /* blocking */ 14931da177e4SLinus Torvalds bad_fork_cleanup_semundo: 14941da177e4SLinus Torvalds exit_sem(p); 14951da177e4SLinus Torvalds bad_fork_cleanup_audit: 14961da177e4SLinus Torvalds audit_free(p); 14971da177e4SLinus Torvalds bad_fork_cleanup_policy: 1498cdd6c482SIngo Molnar perf_event_free_task(p); 14991da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1500f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1501b4f48b63SPaul Menage bad_fork_cleanup_cgroup: 15021da177e4SLinus Torvalds #endif 15034714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1504257058aeSTejun Heo threadgroup_change_end(current); 1505b4f48b63SPaul Menage cgroup_exit(p, cgroup_callbacks_done); 150635df17c5SShailabh Nagar delayacct_tsk_free(p); 1507a1261f54SAl Viro module_put(task_thread_info(p)->exec_domain->module); 15081da177e4SLinus Torvalds bad_fork_cleanup_count: 1509d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1510e0e81739SDavid Howells exit_creds(p); 15111da177e4SLinus Torvalds bad_fork_free: 15121da177e4SLinus Torvalds free_task(p); 1513fe7d37d1SOleg Nesterov fork_out: 1514fe7d37d1SOleg Nesterov return ERR_PTR(retval); 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds 1517f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1518f106eee1SOleg Nesterov { 1519f106eee1SOleg Nesterov enum pid_type type; 1520f106eee1SOleg Nesterov 1521f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1522f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1523f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1524f106eee1SOleg Nesterov } 1525f106eee1SOleg Nesterov } 1526f106eee1SOleg Nesterov 15279abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu) 15281da177e4SLinus Torvalds { 152936c8b586SIngo Molnar struct task_struct *task; 153062e791c1SAl Viro task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0); 1531f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1532f106eee1SOleg Nesterov init_idle_pids(task->pids); 15331da177e4SLinus Torvalds init_idle(task, cpu); 1534f106eee1SOleg Nesterov } 153573b9ebfeSOleg Nesterov 15361da177e4SLinus Torvalds return task; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds /* 15401da177e4SLinus Torvalds * Ok, this is the main fork-routine. 15411da177e4SLinus Torvalds * 15421da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 15431da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 15441da177e4SLinus Torvalds */ 15451da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 15461da177e4SLinus Torvalds unsigned long stack_start, 15471da177e4SLinus Torvalds unsigned long stack_size, 15481da177e4SLinus Torvalds int __user *parent_tidptr, 15491da177e4SLinus Torvalds int __user *child_tidptr) 15501da177e4SLinus Torvalds { 15511da177e4SLinus Torvalds struct task_struct *p; 15521da177e4SLinus Torvalds int trace = 0; 155392476d7fSEric W. Biederman long nr; 15541da177e4SLinus Torvalds 1555bdff746aSAndrew Morton /* 155618b6e041SSerge Hallyn * Do some preliminary argument and permissions checking before we 155718b6e041SSerge Hallyn * actually start allocating stuff 155818b6e041SSerge Hallyn */ 155918b6e041SSerge Hallyn if (clone_flags & CLONE_NEWUSER) { 156018b6e041SSerge Hallyn if (clone_flags & CLONE_THREAD) 156118b6e041SSerge Hallyn return -EINVAL; 156218b6e041SSerge Hallyn /* hopefully this check will go away when userns support is 156318b6e041SSerge Hallyn * complete 156418b6e041SSerge Hallyn */ 15657657d904SSerge E. Hallyn if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) || 15667657d904SSerge E. Hallyn !capable(CAP_SETGID)) 156718b6e041SSerge Hallyn return -EPERM; 156818b6e041SSerge Hallyn } 156918b6e041SSerge Hallyn 157018b6e041SSerge Hallyn /* 15714b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 15724b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 15734b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 15744b9d33e6STejun Heo * for the type of forking is enabled. 157509a05394SRoland McGrath */ 1576*e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 15774b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 15784b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 15794b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 15804b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 15814b9d33e6STejun Heo else 15824b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 15834b9d33e6STejun Heo 15844b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 15854b9d33e6STejun Heo trace = 0; 15864b9d33e6STejun Heo } 15871da177e4SLinus Torvalds 158862e791c1SAl Viro p = copy_process(clone_flags, stack_start, stack_size, 158909a05394SRoland McGrath child_tidptr, NULL, trace); 15901da177e4SLinus Torvalds /* 15911da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 15921da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 15931da177e4SLinus Torvalds */ 15941da177e4SLinus Torvalds if (!IS_ERR(p)) { 15951da177e4SLinus Torvalds struct completion vfork; 15961da177e4SLinus Torvalds 15970a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 15980a16b607SMathieu Desnoyers 15996c5f3e7bSPavel Emelyanov nr = task_pid_vnr(p); 160030e49c26SPavel Emelyanov 160130e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 160230e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1603a6f5e063SSukadev Bhattiprolu 16041da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 16051da177e4SLinus Torvalds p->vfork_done = &vfork; 16061da177e4SLinus Torvalds init_completion(&vfork); 1607d68b46feSOleg Nesterov get_task_struct(p); 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16103e51e3edSSamir Bellabes wake_up_new_task(p); 16111da177e4SLinus Torvalds 16124b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 16134b9d33e6STejun Heo if (unlikely(trace)) 16144b9d33e6STejun Heo ptrace_event(trace, nr); 161509a05394SRoland McGrath 16161da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1617d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 1618a288eeccSTejun Heo ptrace_event(PTRACE_EVENT_VFORK_DONE, nr); 16199f59ce5dSChuck Ebbert } 16201da177e4SLinus Torvalds } else { 162192476d7fSEric W. Biederman nr = PTR_ERR(p); 16221da177e4SLinus Torvalds } 162392476d7fSEric W. Biederman return nr; 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds 16262aa3a7f8SAl Viro #ifdef CONFIG_GENERIC_KERNEL_THREAD 16272aa3a7f8SAl Viro /* 16282aa3a7f8SAl Viro * Create a kernel thread. 16292aa3a7f8SAl Viro */ 16302aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 16312aa3a7f8SAl Viro { 1632*e80d6661SAl Viro return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, 16332aa3a7f8SAl Viro (unsigned long)arg, NULL, NULL); 16342aa3a7f8SAl Viro } 16352aa3a7f8SAl Viro #endif 16362aa3a7f8SAl Viro 1637d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 1638d2125043SAl Viro SYSCALL_DEFINE0(fork) 1639d2125043SAl Viro { 1640d2125043SAl Viro #ifdef CONFIG_MMU 1641*e80d6661SAl Viro return do_fork(SIGCHLD, 0, 0, NULL, NULL); 1642d2125043SAl Viro #else 1643d2125043SAl Viro /* can not support in nommu mode */ 1644d2125043SAl Viro return(-EINVAL); 1645d2125043SAl Viro #endif 1646d2125043SAl Viro } 1647d2125043SAl Viro #endif 1648d2125043SAl Viro 1649d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 1650d2125043SAl Viro SYSCALL_DEFINE0(vfork) 1651d2125043SAl Viro { 1652*e80d6661SAl Viro return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 1653d2125043SAl Viro 0, NULL, NULL); 1654d2125043SAl Viro } 1655d2125043SAl Viro #endif 1656d2125043SAl Viro 1657d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 1658d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 1659d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1660d2125043SAl Viro int __user *, parent_tidptr, 1661d2125043SAl Viro int, tls_val, 1662d2125043SAl Viro int __user *, child_tidptr) 1663d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 1664d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 1665d2125043SAl Viro int __user *, parent_tidptr, 1666d2125043SAl Viro int __user *, child_tidptr, 1667d2125043SAl Viro int, tls_val) 1668d2125043SAl Viro #else 1669d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1670d2125043SAl Viro int __user *, parent_tidptr, 1671d2125043SAl Viro int __user *, child_tidptr, 1672d2125043SAl Viro int, tls_val) 1673d2125043SAl Viro #endif 1674d2125043SAl Viro { 1675*e80d6661SAl Viro return do_fork(clone_flags, newsp, 0, 1676d2125043SAl Viro parent_tidptr, child_tidptr); 1677d2125043SAl Viro } 1678d2125043SAl Viro #endif 1679d2125043SAl Viro 16805fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 16815fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 16825fd63b30SRavikiran G Thirumalai #endif 16835fd63b30SRavikiran G Thirumalai 168451cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 1685aa1757f9SOleg Nesterov { 1686aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1687aa1757f9SOleg Nesterov 1688aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1689b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 1690fba2afaaSDavide Libenzi } 1691aa1757f9SOleg Nesterov 16921da177e4SLinus Torvalds void __init proc_caches_init(void) 16931da177e4SLinus Torvalds { 16941da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 16951da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 16962dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 16972dff4405SVegard Nossum SLAB_NOTRACK, sighand_ctor); 16981da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 16991da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 17002dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17011da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 17021da177e4SLinus Torvalds sizeof(struct files_struct), 0, 17032dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17041da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 17051da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 17062dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17076345d24dSLinus Torvalds /* 17086345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 17096345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 17106345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 17116345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 17126345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 17136345d24dSLinus Torvalds */ 17141da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 17155fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 17162dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 171733e5d769SDavid Howells vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC); 17188feae131SDavid Howells mmap_init(); 171966577193SAl Viro nsproxy_cache_init(); 17201da177e4SLinus Torvalds } 1721cf2e340fSJANAK DESAI 1722cf2e340fSJANAK DESAI /* 17239bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 1724cf2e340fSJANAK DESAI */ 17259bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 1726cf2e340fSJANAK DESAI { 17279bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 17289bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 17299bfb23fcSOleg Nesterov CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET)) 1730cf2e340fSJANAK DESAI return -EINVAL; 17319bfb23fcSOleg Nesterov /* 17329bfb23fcSOleg Nesterov * Not implemented, but pretend it works if there is nothing to 17339bfb23fcSOleg Nesterov * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND 17349bfb23fcSOleg Nesterov * needs to unshare vm. 17359bfb23fcSOleg Nesterov */ 17369bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 17379bfb23fcSOleg Nesterov /* FIXME: get_task_mm() increments ->mm_users */ 17389bfb23fcSOleg Nesterov if (atomic_read(¤t->mm->mm_users) > 1) 17399bfb23fcSOleg Nesterov return -EINVAL; 17409bfb23fcSOleg Nesterov } 1741cf2e340fSJANAK DESAI 1742cf2e340fSJANAK DESAI return 0; 1743cf2e340fSJANAK DESAI } 1744cf2e340fSJANAK DESAI 1745cf2e340fSJANAK DESAI /* 174699d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1747cf2e340fSJANAK DESAI */ 1748cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1749cf2e340fSJANAK DESAI { 1750cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1751cf2e340fSJANAK DESAI 1752498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 1753498052bbSAl Viro return 0; 1754498052bbSAl Viro 1755498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 1756498052bbSAl Viro if (fs->users == 1) 1757498052bbSAl Viro return 0; 1758498052bbSAl Viro 1759498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 176099d1419dSJANAK DESAI if (!*new_fsp) 176199d1419dSJANAK DESAI return -ENOMEM; 1762cf2e340fSJANAK DESAI 1763cf2e340fSJANAK DESAI return 0; 1764cf2e340fSJANAK DESAI } 1765cf2e340fSJANAK DESAI 1766cf2e340fSJANAK DESAI /* 1767a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1768cf2e340fSJANAK DESAI */ 1769cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1770cf2e340fSJANAK DESAI { 1771cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1772a016f338SJANAK DESAI int error = 0; 1773cf2e340fSJANAK DESAI 1774cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1775a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1776a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1777a016f338SJANAK DESAI if (!*new_fdp) 1778a016f338SJANAK DESAI return error; 1779a016f338SJANAK DESAI } 1780cf2e340fSJANAK DESAI 1781cf2e340fSJANAK DESAI return 0; 1782cf2e340fSJANAK DESAI } 1783cf2e340fSJANAK DESAI 1784cf2e340fSJANAK DESAI /* 1785cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1786cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1787cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1788cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1789cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1790cf2e340fSJANAK DESAI * task_struct. 1791cf2e340fSJANAK DESAI */ 17926559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 1793cf2e340fSJANAK DESAI { 1794cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1795cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1796cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 17979edff4abSManfred Spraul int do_sysvsem = 0; 17989bfb23fcSOleg Nesterov int err; 1799cf2e340fSJANAK DESAI 18009bfb23fcSOleg Nesterov err = check_unshare_flags(unshare_flags); 18019bfb23fcSOleg Nesterov if (err) 180206f9d4f9SEric W. Biederman goto bad_unshare_out; 180306f9d4f9SEric W. Biederman 18046013f67fSManfred Spraul /* 18059bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 18069bfb23fcSOleg Nesterov */ 18079bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 18089bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 18099bfb23fcSOleg Nesterov /* 18106013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 18116013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 18126013f67fSManfred Spraul * namespace are unreachable. 18136013f67fSManfred Spraul */ 18146013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 18159edff4abSManfred Spraul do_sysvsem = 1; 1816fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 1817fb0a685cSDaniel Rebelo de Oliveira if (err) 18189bfb23fcSOleg Nesterov goto bad_unshare_out; 1819fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 1820fb0a685cSDaniel Rebelo de Oliveira if (err) 18219bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 1822fb0a685cSDaniel Rebelo de Oliveira err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, new_fs); 1823fb0a685cSDaniel Rebelo de Oliveira if (err) 18249edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 1825cf2e340fSJANAK DESAI 18269bfb23fcSOleg Nesterov if (new_fs || new_fd || do_sysvsem || new_nsproxy) { 18279edff4abSManfred Spraul if (do_sysvsem) { 18289edff4abSManfred Spraul /* 18299edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 18309edff4abSManfred Spraul */ 18319edff4abSManfred Spraul exit_sem(current); 18329edff4abSManfred Spraul } 1833ab516013SSerge E. Hallyn 1834c0b2fc31SSerge Hallyn if (new_nsproxy) { 1835cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 1836cf7b708cSPavel Emelyanov new_nsproxy = NULL; 1837c0b2fc31SSerge Hallyn } 1838cf2e340fSJANAK DESAI 1839cf7b708cSPavel Emelyanov task_lock(current); 1840cf7b708cSPavel Emelyanov 1841cf2e340fSJANAK DESAI if (new_fs) { 1842cf2e340fSJANAK DESAI fs = current->fs; 18432a4419b5SNick Piggin spin_lock(&fs->lock); 1844cf2e340fSJANAK DESAI current->fs = new_fs; 1845498052bbSAl Viro if (--fs->users) 1846498052bbSAl Viro new_fs = NULL; 1847498052bbSAl Viro else 1848cf2e340fSJANAK DESAI new_fs = fs; 18492a4419b5SNick Piggin spin_unlock(&fs->lock); 1850cf2e340fSJANAK DESAI } 1851cf2e340fSJANAK DESAI 1852cf2e340fSJANAK DESAI if (new_fd) { 1853cf2e340fSJANAK DESAI fd = current->files; 1854cf2e340fSJANAK DESAI current->files = new_fd; 1855cf2e340fSJANAK DESAI new_fd = fd; 1856cf2e340fSJANAK DESAI } 1857cf2e340fSJANAK DESAI 1858cf2e340fSJANAK DESAI task_unlock(current); 1859cf2e340fSJANAK DESAI } 1860cf2e340fSJANAK DESAI 1861c0b2fc31SSerge Hallyn if (new_nsproxy) 1862444f378bSLinus Torvalds put_nsproxy(new_nsproxy); 1863c0b2fc31SSerge Hallyn 1864cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 1865cf2e340fSJANAK DESAI if (new_fd) 1866cf2e340fSJANAK DESAI put_files_struct(new_fd); 1867cf2e340fSJANAK DESAI 1868cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 1869cf2e340fSJANAK DESAI if (new_fs) 1870498052bbSAl Viro free_fs_struct(new_fs); 1871cf2e340fSJANAK DESAI 1872cf2e340fSJANAK DESAI bad_unshare_out: 1873cf2e340fSJANAK DESAI return err; 1874cf2e340fSJANAK DESAI } 18753b125388SAl Viro 18763b125388SAl Viro /* 18773b125388SAl Viro * Helper to unshare the files of the current task. 18783b125388SAl Viro * We don't want to expose copy_files internals to 18793b125388SAl Viro * the exec layer of the kernel. 18803b125388SAl Viro */ 18813b125388SAl Viro 18823b125388SAl Viro int unshare_files(struct files_struct **displaced) 18833b125388SAl Viro { 18843b125388SAl Viro struct task_struct *task = current; 188550704516SAl Viro struct files_struct *copy = NULL; 18863b125388SAl Viro int error; 18873b125388SAl Viro 18883b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 18893b125388SAl Viro if (error || !copy) { 18903b125388SAl Viro *displaced = NULL; 18913b125388SAl Viro return error; 18923b125388SAl Viro } 18933b125388SAl Viro *displaced = task->files; 18943b125388SAl Viro task_lock(task); 18953b125388SAl Viro task->files = copy; 18963b125388SAl Viro task_unlock(task); 18973b125388SAl Viro return 0; 18983b125388SAl Viro } 1899