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> 31615d6e87SDavidlohr Bueso #include <linux/mm.h> 32615d6e87SDavidlohr Bueso #include <linux/vmacache.h> 33ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 34c59ede7bSRandy.Dunlap #include <linux/capability.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 36b4f48b63SPaul Menage #include <linux/cgroup.h> 371da177e4SLinus Torvalds #include <linux/security.h> 38a1e78772SMel Gorman #include <linux/hugetlb.h> 39e2cfabdfSWill Drewry #include <linux/seccomp.h> 401da177e4SLinus Torvalds #include <linux/swap.h> 411da177e4SLinus Torvalds #include <linux/syscalls.h> 421da177e4SLinus Torvalds #include <linux/jiffies.h> 431da177e4SLinus Torvalds #include <linux/futex.h> 448141c7f3SLinus Torvalds #include <linux/compat.h> 45207205a2SEric Dumazet #include <linux/kthread.h> 467c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 47ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 481da177e4SLinus Torvalds #include <linux/ptrace.h> 491da177e4SLinus Torvalds #include <linux/mount.h> 501da177e4SLinus Torvalds #include <linux/audit.h> 5178fb7466SPavel Emelianov #include <linux/memcontrol.h> 52f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 535e2bf014SMike Galbraith #include <linux/proc_fs.h> 541da177e4SLinus Torvalds #include <linux/profile.h> 551da177e4SLinus Torvalds #include <linux/rmap.h> 56f8af4da3SHugh Dickins #include <linux/ksm.h> 571da177e4SLinus Torvalds #include <linux/acct.h> 588f0ab514SJay Lan #include <linux/tsacct_kern.h> 599f46080cSMatt Helsley #include <linux/cn_proc.h> 60ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 61ca74e92bSShailabh Nagar #include <linux/delayacct.h> 62ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 630a425405SArjan van de Ven #include <linux/random.h> 64522ed776SMiloslav Trmac #include <linux/tty.h> 65fd0928dfSJens Axboe #include <linux/blkdev.h> 665ad4e53bSAl Viro #include <linux/fs_struct.h> 677c9f8861SEric Sandeen #include <linux/magic.h> 68cdd6c482SIngo Molnar #include <linux/perf_event.h> 6942c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 708e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 713d5992d2SYing Han #include <linux/oom.h> 72ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 73d80e731eSOleg Nesterov #include <linux/signalfd.h> 740326f5a9SSrikar Dronamraju #include <linux/uprobes.h> 75a27bb332SKent Overstreet #include <linux/aio.h> 7652f5684cSGideon Israel Dsouza #include <linux/compiler.h> 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds #include <asm/pgtable.h> 791da177e4SLinus Torvalds #include <asm/pgalloc.h> 801da177e4SLinus Torvalds #include <asm/uaccess.h> 811da177e4SLinus Torvalds #include <asm/mmu_context.h> 821da177e4SLinus Torvalds #include <asm/cacheflush.h> 831da177e4SLinus Torvalds #include <asm/tlbflush.h> 841da177e4SLinus Torvalds 85ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 86ad8d75ffSSteven Rostedt 8743d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 8843d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 8943d2b113SKAMEZAWA Hiroyuki 901da177e4SLinus Torvalds /* 911da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 921da177e4SLinus Torvalds */ 931da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 941da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 101db1466b3SPaul E. McKenney 102db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 103db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 104db1466b3SPaul E. McKenney { 105db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 106db1466b3SPaul E. McKenney } 107db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 108db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds int nr_processes(void) 1111da177e4SLinus Torvalds { 1121da177e4SLinus Torvalds int cpu; 1131da177e4SLinus Torvalds int total = 0; 1141da177e4SLinus Torvalds 1151d510750SIan Campbell for_each_possible_cpu(cpu) 1161da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds return total; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 121f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 122f19b9f74SAkinobu Mita { 123f19b9f74SAkinobu Mita } 124f19b9f74SAkinobu Mita 125f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 126e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 12741101809SThomas Gleixner 12841101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 12941101809SThomas Gleixner { 13041101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 13141101809SThomas Gleixner } 13241101809SThomas Gleixner 13341101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 13441101809SThomas Gleixner { 13541101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 13641101809SThomas Gleixner } 1371da177e4SLinus Torvalds #endif 1381da177e4SLinus Torvalds 139f19b9f74SAkinobu Mita void __weak arch_release_thread_info(struct thread_info *ti) 140f19b9f74SAkinobu Mita { 141f19b9f74SAkinobu Mita } 142f19b9f74SAkinobu Mita 143f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR 14441101809SThomas Gleixner 1450d15d74aSThomas Gleixner /* 1460d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1470d15d74aSThomas Gleixner * kmemcache based allocator. 1480d15d74aSThomas Gleixner */ 1490d15d74aSThomas Gleixner # if THREAD_SIZE >= PAGE_SIZE 150b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 151b6a84016SEric Dumazet int node) 152b69c49b7SFUJITA Tomonori { 15352383431SVladimir Davydov struct page *page = alloc_kmem_pages_node(node, THREADINFO_GFP, 1542889f608SThomas Gleixner THREAD_SIZE_ORDER); 155b6a84016SEric Dumazet 156b6a84016SEric Dumazet return page ? page_address(page) : NULL; 157b69c49b7SFUJITA Tomonori } 158b69c49b7SFUJITA Tomonori 159b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti) 160b69c49b7SFUJITA Tomonori { 16152383431SVladimir Davydov free_kmem_pages((unsigned long)ti, THREAD_SIZE_ORDER); 162b69c49b7SFUJITA Tomonori } 1630d15d74aSThomas Gleixner # else 1640d15d74aSThomas Gleixner static struct kmem_cache *thread_info_cache; 1650d15d74aSThomas Gleixner 1660d15d74aSThomas Gleixner static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 1670d15d74aSThomas Gleixner int node) 1680d15d74aSThomas Gleixner { 1690d15d74aSThomas Gleixner return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node); 1700d15d74aSThomas Gleixner } 1710d15d74aSThomas Gleixner 1720d15d74aSThomas Gleixner static void free_thread_info(struct thread_info *ti) 1730d15d74aSThomas Gleixner { 1740d15d74aSThomas Gleixner kmem_cache_free(thread_info_cache, ti); 1750d15d74aSThomas Gleixner } 1760d15d74aSThomas Gleixner 1770d15d74aSThomas Gleixner void thread_info_cache_init(void) 1780d15d74aSThomas Gleixner { 1790d15d74aSThomas Gleixner thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE, 1800d15d74aSThomas Gleixner THREAD_SIZE, 0, NULL); 1810d15d74aSThomas Gleixner BUG_ON(thread_info_cache == NULL); 1820d15d74aSThomas Gleixner } 1830d15d74aSThomas Gleixner # endif 184b69c49b7SFUJITA Tomonori #endif 185b69c49b7SFUJITA Tomonori 1861da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 187e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 190e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 193e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 196e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 199e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 202e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 2031da177e4SLinus Torvalds 204c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account) 205c6a7f572SKOSAKI Motohiro { 206c6a7f572SKOSAKI Motohiro struct zone *zone = page_zone(virt_to_page(ti)); 207c6a7f572SKOSAKI Motohiro 208c6a7f572SKOSAKI Motohiro mod_zone_page_state(zone, NR_KERNEL_STACK, account); 209c6a7f572SKOSAKI Motohiro } 210c6a7f572SKOSAKI Motohiro 2111da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 2121da177e4SLinus Torvalds { 213c6a7f572SKOSAKI Motohiro account_kernel_stack(tsk->stack, -1); 214f19b9f74SAkinobu Mita arch_release_thread_info(tsk->stack); 215f7e4217bSRoman Zippel free_thread_info(tsk->stack); 21623f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 217fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 218e2cfabdfSWill Drewry put_seccomp_filter(tsk); 219f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 2201da177e4SLinus Torvalds free_task_struct(tsk); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 2231da177e4SLinus Torvalds 224ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig) 225ea6d290cSOleg Nesterov { 22697101eb4SOleg Nesterov taskstats_tgid_free(sig); 2271c5354deSMike Galbraith sched_autogroup_exit(sig); 228ea6d290cSOleg Nesterov kmem_cache_free(signal_cachep, sig); 229ea6d290cSOleg Nesterov } 230ea6d290cSOleg Nesterov 231ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig) 232ea6d290cSOleg Nesterov { 2331c5354deSMike Galbraith if (atomic_dec_and_test(&sig->sigcnt)) 234ea6d290cSOleg Nesterov free_signal_struct(sig); 235ea6d290cSOleg Nesterov } 236ea6d290cSOleg Nesterov 237158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 2381da177e4SLinus Torvalds { 239270f722dSEugene Teo WARN_ON(!tsk->exit_state); 2401da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 2411da177e4SLinus Torvalds WARN_ON(tsk == current); 2421da177e4SLinus Torvalds 243156654f4SMike Galbraith task_numa_free(tsk); 2441a2a4d06SKees Cook security_task_free(tsk); 245e0e81739SDavid Howells exit_creds(tsk); 24635df17c5SShailabh Nagar delayacct_tsk_free(tsk); 247ea6d290cSOleg Nesterov put_signal_struct(tsk->signal); 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 2501da177e4SLinus Torvalds free_task(tsk); 2511da177e4SLinus Torvalds } 25277c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct); 2531da177e4SLinus Torvalds 2546c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { } 25561c4628bSSuresh Siddha 2561da177e4SLinus Torvalds void __init fork_init(unsigned long mempages) 2571da177e4SLinus Torvalds { 258f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 2591da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 2601da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 2611da177e4SLinus Torvalds #endif 2621da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 2631da177e4SLinus Torvalds task_struct_cachep = 2641da177e4SLinus Torvalds kmem_cache_create("task_struct", sizeof(struct task_struct), 2652dff4405SVegard Nossum ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL); 2661da177e4SLinus Torvalds #endif 2671da177e4SLinus Torvalds 26861c4628bSSuresh Siddha /* do the arch specific task caches init */ 26961c4628bSSuresh Siddha arch_task_cache_init(); 27061c4628bSSuresh Siddha 2711da177e4SLinus Torvalds /* 2721da177e4SLinus Torvalds * The default maximum number of threads is set to a safe 2731da177e4SLinus Torvalds * value: the thread structures can take up at most half 2741da177e4SLinus Torvalds * of memory. 2751da177e4SLinus Torvalds */ 2761da177e4SLinus Torvalds max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds /* 2791da177e4SLinus Torvalds * we need to allow at least 20 threads to boot a system 2801da177e4SLinus Torvalds */ 2811da177e4SLinus Torvalds if (max_threads < 20) 2821da177e4SLinus Torvalds max_threads = 20; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 2851da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 2861da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 2871da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 29052f5684cSGideon Israel Dsouza int __weak arch_dup_task_struct(struct task_struct *dst, 29161c4628bSSuresh Siddha struct task_struct *src) 29261c4628bSSuresh Siddha { 29361c4628bSSuresh Siddha *dst = *src; 29461c4628bSSuresh Siddha return 0; 29561c4628bSSuresh Siddha } 29661c4628bSSuresh Siddha 2971da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds struct task_struct *tsk; 3001da177e4SLinus Torvalds struct thread_info *ti; 3017c9f8861SEric Sandeen unsigned long *stackend; 302207205a2SEric Dumazet int node = tsk_fork_get_node(orig); 3033e26c149SPeter Zijlstra int err; 3041da177e4SLinus Torvalds 305504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 3061da177e4SLinus Torvalds if (!tsk) 3071da177e4SLinus Torvalds return NULL; 3081da177e4SLinus Torvalds 309b6a84016SEric Dumazet ti = alloc_thread_info_node(tsk, node); 310f19b9f74SAkinobu Mita if (!ti) 311f19b9f74SAkinobu Mita goto free_tsk; 3121da177e4SLinus Torvalds 31361c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 31461c4628bSSuresh Siddha if (err) 315f19b9f74SAkinobu Mita goto free_ti; 31661c4628bSSuresh Siddha 31787bec58aSAndrew Morton tsk->stack = ti; 318dbd95212SKees Cook #ifdef CONFIG_SECCOMP 319dbd95212SKees Cook /* 320dbd95212SKees Cook * We must handle setting up seccomp filters once we're under 321dbd95212SKees Cook * the sighand lock in case orig has changed between now and 322dbd95212SKees Cook * then. Until then, filter must be NULL to avoid messing up 323dbd95212SKees Cook * the usage counts on the error path calling free_task. 324dbd95212SKees Cook */ 325dbd95212SKees Cook tsk->seccomp.filter = NULL; 326dbd95212SKees Cook #endif 32787bec58aSAndrew Morton 32887bec58aSAndrew Morton setup_thread_stack(tsk, orig); 3298e7cac79SAvi Kivity clear_user_return_notifier(tsk); 330f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 3317c9f8861SEric Sandeen stackend = end_of_stack(tsk); 3327c9f8861SEric Sandeen *stackend = STACK_END_MAGIC; /* for overflow detection */ 3331da177e4SLinus Torvalds 3340a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 3350a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 3360a425405SArjan van de Ven #endif 3370a425405SArjan van de Ven 338fb0a685cSDaniel Rebelo de Oliveira /* 339fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 340fb0a685cSDaniel Rebelo de Oliveira * parent) 341fb0a685cSDaniel Rebelo de Oliveira */ 3421da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 3436c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 3442056a782SJens Axboe tsk->btrace_seq = 0; 3456c5c9341SAlexey Dobriyan #endif 346a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 3475640f768SEric Dumazet tsk->task_frag.page = NULL; 348c6a7f572SKOSAKI Motohiro 349c6a7f572SKOSAKI Motohiro account_kernel_stack(ti, 1); 350c6a7f572SKOSAKI Motohiro 3511da177e4SLinus Torvalds return tsk; 35261c4628bSSuresh Siddha 353f19b9f74SAkinobu Mita free_ti: 35461c4628bSSuresh Siddha free_thread_info(ti); 355f19b9f74SAkinobu Mita free_tsk: 35661c4628bSSuresh Siddha free_task_struct(tsk); 35761c4628bSSuresh Siddha return NULL; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds #ifdef CONFIG_MMU 361a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 3621da177e4SLinus Torvalds { 363297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 3641da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 3651da177e4SLinus Torvalds int retval; 3661da177e4SLinus Torvalds unsigned long charge; 3671da177e4SLinus Torvalds 36832cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 3691da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 370ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 371f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 372ad339451SIngo Molnar /* 373ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 374ad339451SIngo Molnar */ 375ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 3767ee78232SHugh Dickins 3771da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 3781da177e4SLinus Torvalds rb_parent = NULL; 3791da177e4SLinus Torvalds pprev = &mm->mmap; 380f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 381f8af4da3SHugh Dickins if (retval) 382f8af4da3SHugh Dickins goto out; 383ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 384ba76149fSAndrea Arcangeli if (retval) 385ba76149fSAndrea Arcangeli goto out; 3861da177e4SLinus Torvalds 387297c5eeeSLinus Torvalds prev = NULL; 388fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 3891da177e4SLinus Torvalds struct file *file; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 392ab50b8edSHugh Dickins vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 39344de9d0cSHuang Shijie -vma_pages(mpnt)); 3941da177e4SLinus Torvalds continue; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds charge = 0; 3971da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 398b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 399b2412b7fSHuang Shijie 400191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 4011da177e4SLinus Torvalds goto fail_nomem; 4021da177e4SLinus Torvalds charge = len; 4031da177e4SLinus Torvalds } 404e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 4051da177e4SLinus Torvalds if (!tmp) 4061da177e4SLinus Torvalds goto fail_nomem; 4071da177e4SLinus Torvalds *tmp = *mpnt; 4085beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 409ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 410ef0855d3SOleg Nesterov if (retval) 4111da177e4SLinus Torvalds goto fail_nomem_policy; 412a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 4135beb4930SRik van Riel if (anon_vma_fork(tmp, mpnt)) 4145beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 4151da177e4SLinus Torvalds tmp->vm_flags &= ~VM_LOCKED; 416297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 4171da177e4SLinus Torvalds file = tmp->vm_file; 4181da177e4SLinus Torvalds if (file) { 419496ad9aaSAl Viro struct inode *inode = file_inode(file); 420b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 421b88ed205SHugh Dickins 4221da177e4SLinus Torvalds get_file(file); 4231da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 4241da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 4253d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 426b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 427b88ed205SHugh Dickins mapping->i_mmap_writable++; 428b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 429b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 4309826a516SMichel Lespinasse if (unlikely(tmp->vm_flags & VM_NONLINEAR)) 4319826a516SMichel Lespinasse vma_nonlinear_insert(tmp, 4329826a516SMichel Lespinasse &mapping->i_mmap_nonlinear); 4339826a516SMichel Lespinasse else 4349826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 4359826a516SMichel Lespinasse &mapping->i_mmap); 436b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 4373d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds /* 441a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 442a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 443a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 444a1e78772SMel Gorman */ 445a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 446a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 447a1e78772SMel Gorman 448a1e78772SMel Gorman /* 4497ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 4501da177e4SLinus Torvalds */ 4511da177e4SLinus Torvalds *pprev = tmp; 4521da177e4SLinus Torvalds pprev = &tmp->vm_next; 453297c5eeeSLinus Torvalds tmp->vm_prev = prev; 454297c5eeeSLinus Torvalds prev = tmp; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 4571da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 4581da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds mm->map_count++; 4610b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 4641da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds if (retval) 4671da177e4SLinus Torvalds goto out; 4681da177e4SLinus Torvalds } 469d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 470d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 4711da177e4SLinus Torvalds retval = 0; 4721da177e4SLinus Torvalds out: 4737ee78232SHugh Dickins up_write(&mm->mmap_sem); 474fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 4751da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 47632cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 4771da177e4SLinus Torvalds return retval; 4785beb4930SRik van Riel fail_nomem_anon_vma_fork: 479ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 4801da177e4SLinus Torvalds fail_nomem_policy: 4811da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 4821da177e4SLinus Torvalds fail_nomem: 4831da177e4SLinus Torvalds retval = -ENOMEM; 4841da177e4SLinus Torvalds vm_unacct_memory(charge); 4851da177e4SLinus Torvalds goto out; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 4891da177e4SLinus Torvalds { 4901da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 4911da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 4921da177e4SLinus Torvalds return -ENOMEM; 4931da177e4SLinus Torvalds return 0; 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 4971da177e4SLinus Torvalds { 4985e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds #else 5011da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 5021da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 5031da177e4SLinus Torvalds #define mm_free_pgd(mm) 5041da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 5071da177e4SLinus Torvalds 508e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 5091da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 5101da177e4SLinus Torvalds 5114cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 5124cb0e11bSHidehiro Kawai 5134cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 5144cb0e11bSHidehiro Kawai { 5154cb0e11bSHidehiro Kawai default_dump_filter = 5164cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 5174cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 5184cb0e11bSHidehiro Kawai return 1; 5194cb0e11bSHidehiro Kawai } 5204cb0e11bSHidehiro Kawai 5214cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 5224cb0e11bSHidehiro Kawai 5231da177e4SLinus Torvalds #include <linux/init_task.h> 5241da177e4SLinus Torvalds 525858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 526858f0993SAlexey Dobriyan { 527858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 528858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 529db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 530858f0993SAlexey Dobriyan #endif 531858f0993SAlexey Dobriyan } 532858f0993SAlexey Dobriyan 53378fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 5341da177e4SLinus Torvalds { 53541f727fdSVladimir Davydov mm->mmap = NULL; 53641f727fdSVladimir Davydov mm->mm_rb = RB_ROOT; 53741f727fdSVladimir Davydov mm->vmacache_seqnum = 0; 5381da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 5391da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 5401da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 5411da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 542999d9fc1SOleg Nesterov mm->core_state = NULL; 543e1f56c89SKirill A. Shutemov atomic_long_set(&mm->nr_ptes, 0); 54441f727fdSVladimir Davydov mm->map_count = 0; 54541f727fdSVladimir Davydov mm->locked_vm = 0; 546*ce65cefaSVladimir Davydov mm->pinned_vm = 0; 547d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 5481da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 54941f727fdSVladimir Davydov mm_init_cpumask(mm); 550858f0993SAlexey Dobriyan mm_init_aio(mm); 551cf475ad2SBalbir Singh mm_init_owner(mm, p); 55241f727fdSVladimir Davydov mmu_notifier_mm_init(mm); 55320841405SRik van Riel clear_tlb_flush_pending(mm); 55441f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 55541f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 55641f727fdSVladimir Davydov #endif 5571da177e4SLinus Torvalds 558a0715cc2SAlex Thorlton if (current->mm) { 559a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 560a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 561a0715cc2SAlex Thorlton } else { 562a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 5631da177e4SLinus Torvalds mm->def_flags = 0; 564a0715cc2SAlex Thorlton } 565a0715cc2SAlex Thorlton 56641f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 56741f727fdSVladimir Davydov goto fail_nopgd; 56878fb7466SPavel Emelianov 56941f727fdSVladimir Davydov if (init_new_context(p, mm)) 57041f727fdSVladimir Davydov goto fail_nocontext; 57141f727fdSVladimir Davydov 57241f727fdSVladimir Davydov return mm; 57341f727fdSVladimir Davydov 57441f727fdSVladimir Davydov fail_nocontext: 57541f727fdSVladimir Davydov mm_free_pgd(mm); 57641f727fdSVladimir Davydov fail_nopgd: 5771da177e4SLinus Torvalds free_mm(mm); 5781da177e4SLinus Torvalds return NULL; 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 581c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm) 582c3f0327fSKonstantin Khlebnikov { 583c3f0327fSKonstantin Khlebnikov int i; 584c3f0327fSKonstantin Khlebnikov 585c3f0327fSKonstantin Khlebnikov for (i = 0; i < NR_MM_COUNTERS; i++) { 586c3f0327fSKonstantin Khlebnikov long x = atomic_long_read(&mm->rss_stat.count[i]); 587c3f0327fSKonstantin Khlebnikov 588c3f0327fSKonstantin Khlebnikov if (unlikely(x)) 589c3f0327fSKonstantin Khlebnikov printk(KERN_ALERT "BUG: Bad rss-counter state " 590c3f0327fSKonstantin Khlebnikov "mm:%p idx:%d val:%ld\n", mm, i, x); 591c3f0327fSKonstantin Khlebnikov } 592c3f0327fSKonstantin Khlebnikov 593e009bb30SKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 594c3f0327fSKonstantin Khlebnikov VM_BUG_ON(mm->pmd_huge_pte); 595c3f0327fSKonstantin Khlebnikov #endif 596c3f0327fSKonstantin Khlebnikov } 597c3f0327fSKonstantin Khlebnikov 5981da177e4SLinus Torvalds /* 5991da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 6001da177e4SLinus Torvalds */ 6011da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 6021da177e4SLinus Torvalds { 6031da177e4SLinus Torvalds struct mm_struct *mm; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds mm = allocate_mm(); 606de03c72cSKOSAKI Motohiro if (!mm) 607de03c72cSKOSAKI Motohiro return NULL; 608de03c72cSKOSAKI Motohiro 6091da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 6106345d24dSLinus Torvalds return mm_init(mm, current); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds /* 6141da177e4SLinus Torvalds * Called when the last reference to the mm 6151da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 6161da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 6171da177e4SLinus Torvalds */ 6187ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 6191da177e4SLinus Torvalds { 6201da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 6211da177e4SLinus Torvalds mm_free_pgd(mm); 6221da177e4SLinus Torvalds destroy_context(mm); 623cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 624c3f0327fSKonstantin Khlebnikov check_mm(mm); 6251da177e4SLinus Torvalds free_mm(mm); 6261da177e4SLinus Torvalds } 6276d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /* 6301da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 6311da177e4SLinus Torvalds */ 6321da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 6331da177e4SLinus Torvalds { 6340ae26f1bSAndrew Morton might_sleep(); 6350ae26f1bSAndrew Morton 6361da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 637d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 6381da177e4SLinus Torvalds exit_aio(mm); 6391c2fb7a4SAndrea Arcangeli ksm_exit(mm); 640ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 6411da177e4SLinus Torvalds exit_mmap(mm); 642925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 6431da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 6441da177e4SLinus Torvalds spin_lock(&mmlist_lock); 6451da177e4SLinus Torvalds list_del(&mm->mmlist); 6461da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 6471da177e4SLinus Torvalds } 648801460d0SHiroshi Shimamoto if (mm->binfmt) 649801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 6501da177e4SLinus Torvalds mmdrop(mm); 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 6541da177e4SLinus Torvalds 65538646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 65638646013SJiri Slaby { 65738646013SJiri Slaby if (new_exe_file) 65838646013SJiri Slaby get_file(new_exe_file); 65938646013SJiri Slaby if (mm->exe_file) 66038646013SJiri Slaby fput(mm->exe_file); 66138646013SJiri Slaby mm->exe_file = new_exe_file; 66238646013SJiri Slaby } 66338646013SJiri Slaby 66438646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 66538646013SJiri Slaby { 66638646013SJiri Slaby struct file *exe_file; 66738646013SJiri Slaby 6682dd8ad81SKonstantin Khlebnikov /* We need mmap_sem to protect against races with removal of exe_file */ 66938646013SJiri Slaby down_read(&mm->mmap_sem); 67038646013SJiri Slaby exe_file = mm->exe_file; 67138646013SJiri Slaby if (exe_file) 67238646013SJiri Slaby get_file(exe_file); 67338646013SJiri Slaby up_read(&mm->mmap_sem); 67438646013SJiri Slaby return exe_file; 67538646013SJiri Slaby } 67638646013SJiri Slaby 67738646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 67838646013SJiri Slaby { 67938646013SJiri Slaby /* It's safe to write the exe_file pointer without exe_file_lock because 68038646013SJiri Slaby * this is called during fork when the task is not yet in /proc */ 68138646013SJiri Slaby newmm->exe_file = get_mm_exe_file(oldmm); 68238646013SJiri Slaby } 68338646013SJiri Slaby 6841da177e4SLinus Torvalds /** 6851da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 6861da177e4SLinus Torvalds * 687246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 6881da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 6891da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 6901da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 6911da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 6921da177e4SLinus Torvalds */ 6931da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 6941da177e4SLinus Torvalds { 6951da177e4SLinus Torvalds struct mm_struct *mm; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds task_lock(task); 6981da177e4SLinus Torvalds mm = task->mm; 6991da177e4SLinus Torvalds if (mm) { 700246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 7011da177e4SLinus Torvalds mm = NULL; 7021da177e4SLinus Torvalds else 7031da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds task_unlock(task); 7061da177e4SLinus Torvalds return mm; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 7091da177e4SLinus Torvalds 7108cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 7118cdb878dSChristopher Yeoh { 7128cdb878dSChristopher Yeoh struct mm_struct *mm; 7138cdb878dSChristopher Yeoh int err; 7148cdb878dSChristopher Yeoh 7158cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 7168cdb878dSChristopher Yeoh if (err) 7178cdb878dSChristopher Yeoh return ERR_PTR(err); 7188cdb878dSChristopher Yeoh 7198cdb878dSChristopher Yeoh mm = get_task_mm(task); 7208cdb878dSChristopher Yeoh if (mm && mm != current->mm && 7218cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 7228cdb878dSChristopher Yeoh mmput(mm); 7238cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 7248cdb878dSChristopher Yeoh } 7258cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 7268cdb878dSChristopher Yeoh 7278cdb878dSChristopher Yeoh return mm; 7288cdb878dSChristopher Yeoh } 7298cdb878dSChristopher Yeoh 73057b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 731c415c3b4SOleg Nesterov { 732d68b46feSOleg Nesterov struct completion *vfork; 733c415c3b4SOleg Nesterov 734d68b46feSOleg Nesterov task_lock(tsk); 735d68b46feSOleg Nesterov vfork = tsk->vfork_done; 736d68b46feSOleg Nesterov if (likely(vfork)) { 737c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 738d68b46feSOleg Nesterov complete(vfork); 739d68b46feSOleg Nesterov } 740d68b46feSOleg Nesterov task_unlock(tsk); 741d68b46feSOleg Nesterov } 742d68b46feSOleg Nesterov 743d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 744d68b46feSOleg Nesterov struct completion *vfork) 745d68b46feSOleg Nesterov { 746d68b46feSOleg Nesterov int killed; 747d68b46feSOleg Nesterov 748d68b46feSOleg Nesterov freezer_do_not_count(); 749d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 750d68b46feSOleg Nesterov freezer_count(); 751d68b46feSOleg Nesterov 752d68b46feSOleg Nesterov if (killed) { 753d68b46feSOleg Nesterov task_lock(child); 754d68b46feSOleg Nesterov child->vfork_done = NULL; 755d68b46feSOleg Nesterov task_unlock(child); 756d68b46feSOleg Nesterov } 757d68b46feSOleg Nesterov 758d68b46feSOleg Nesterov put_task_struct(child); 759d68b46feSOleg Nesterov return killed; 760c415c3b4SOleg Nesterov } 761c415c3b4SOleg Nesterov 7621da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 7631da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 7641da177e4SLinus Torvalds * error success whatever. 7651da177e4SLinus Torvalds * 7661da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 7671da177e4SLinus Torvalds * from the current process. 7681da177e4SLinus Torvalds * 7691da177e4SLinus Torvalds * This difference is important for error handling, when we 7701da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 7711da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 7721da177e4SLinus Torvalds * restoring the old one. . . 7731da177e4SLinus Torvalds * Eric Biederman 10 January 1998 7741da177e4SLinus Torvalds */ 7751da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 7761da177e4SLinus Torvalds { 7778141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 7788141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 779fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 7808141c7f3SLinus Torvalds exit_robust_list(tsk); 781fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 782fc6b177dSPeter Zijlstra } 7838141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 784fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 7858141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 786fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 787fc6b177dSPeter Zijlstra } 7888141c7f3SLinus Torvalds #endif 789322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 790322a2c10SThomas Gleixner exit_pi_state_list(tsk); 7918141c7f3SLinus Torvalds #endif 7928141c7f3SLinus Torvalds 7930326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 7940326f5a9SSrikar Dronamraju 7951da177e4SLinus Torvalds /* Get rid of any cached register state */ 7961da177e4SLinus Torvalds deactivate_mm(tsk, mm); 7971da177e4SLinus Torvalds 798fec1d011SRoland McGrath /* 799fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 800fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 801fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 802d68b46feSOleg Nesterov * trouble, say, a killed vfork parent shouldn't touch this mm. 803d68b46feSOleg Nesterov * Userland only wants this done for a sys_exit. 804fec1d011SRoland McGrath */ 8059c8a8228SEric Dumazet if (tsk->clear_child_tid) { 8069c8a8228SEric Dumazet if (!(tsk->flags & PF_SIGNALED) && 8079c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 8081da177e4SLinus Torvalds /* 8091da177e4SLinus Torvalds * We don't check the error code - if userspace has 8101da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 8111da177e4SLinus Torvalds */ 8129c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 8139c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 8149c8a8228SEric Dumazet 1, NULL, NULL, 0); 8159c8a8228SEric Dumazet } 8169c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 8171da177e4SLinus Torvalds } 818f7505d64SKonstantin Khlebnikov 819f7505d64SKonstantin Khlebnikov /* 820f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 821f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 822f7505d64SKonstantin Khlebnikov */ 823f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 824f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 827a0a7ec30SJANAK DESAI /* 828a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 829a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 830a0a7ec30SJANAK DESAI */ 831ff252c1fSDaeSeok Youn static struct mm_struct *dup_mm(struct task_struct *tsk) 832a0a7ec30SJANAK DESAI { 833a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 834a0a7ec30SJANAK DESAI int err; 835a0a7ec30SJANAK DESAI 836a0a7ec30SJANAK DESAI mm = allocate_mm(); 837a0a7ec30SJANAK DESAI if (!mm) 838a0a7ec30SJANAK DESAI goto fail_nomem; 839a0a7ec30SJANAK DESAI 840a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 841a0a7ec30SJANAK DESAI 84278fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 843a0a7ec30SJANAK DESAI goto fail_nomem; 844a0a7ec30SJANAK DESAI 845925d1c40SMatt Helsley dup_mm_exe_file(oldmm, mm); 846925d1c40SMatt Helsley 847a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 848a0a7ec30SJANAK DESAI if (err) 849a0a7ec30SJANAK DESAI goto free_pt; 850a0a7ec30SJANAK DESAI 851a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 852a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 853a0a7ec30SJANAK DESAI 854801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 855801460d0SHiroshi Shimamoto goto free_pt; 856801460d0SHiroshi Shimamoto 857a0a7ec30SJANAK DESAI return mm; 858a0a7ec30SJANAK DESAI 859a0a7ec30SJANAK DESAI free_pt: 860801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 861801460d0SHiroshi Shimamoto mm->binfmt = NULL; 862a0a7ec30SJANAK DESAI mmput(mm); 863a0a7ec30SJANAK DESAI 864a0a7ec30SJANAK DESAI fail_nomem: 865a0a7ec30SJANAK DESAI return NULL; 866a0a7ec30SJANAK DESAI } 867a0a7ec30SJANAK DESAI 8681da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 8691da177e4SLinus Torvalds { 8701da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 8711da177e4SLinus Torvalds int retval; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 8741da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 87517406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 87617406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 87717406b82SMandeep Singh Baines #endif 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds tsk->mm = NULL; 8801da177e4SLinus Torvalds tsk->active_mm = NULL; 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds /* 8831da177e4SLinus Torvalds * Are we cloning a kernel thread? 8841da177e4SLinus Torvalds * 8851da177e4SLinus Torvalds * We need to steal a active VM for that.. 8861da177e4SLinus Torvalds */ 8871da177e4SLinus Torvalds oldmm = current->mm; 8881da177e4SLinus Torvalds if (!oldmm) 8891da177e4SLinus Torvalds return 0; 8901da177e4SLinus Torvalds 891615d6e87SDavidlohr Bueso /* initialize the new vmacache entries */ 892615d6e87SDavidlohr Bueso vmacache_flush(tsk); 893615d6e87SDavidlohr Bueso 8941da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 8951da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 8961da177e4SLinus Torvalds mm = oldmm; 8971da177e4SLinus Torvalds goto good_mm; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds retval = -ENOMEM; 901a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 9021da177e4SLinus Torvalds if (!mm) 9031da177e4SLinus Torvalds goto fail_nomem; 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds good_mm: 9061da177e4SLinus Torvalds tsk->mm = mm; 9071da177e4SLinus Torvalds tsk->active_mm = mm; 9081da177e4SLinus Torvalds return 0; 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds fail_nomem: 9111da177e4SLinus Torvalds return retval; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 914a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 9151da177e4SLinus Torvalds { 916498052bbSAl Viro struct fs_struct *fs = current->fs; 9171da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 918498052bbSAl Viro /* tsk->fs is already what we want */ 9192a4419b5SNick Piggin spin_lock(&fs->lock); 920498052bbSAl Viro if (fs->in_exec) { 9212a4419b5SNick Piggin spin_unlock(&fs->lock); 922498052bbSAl Viro return -EAGAIN; 923498052bbSAl Viro } 924498052bbSAl Viro fs->users++; 9252a4419b5SNick Piggin spin_unlock(&fs->lock); 9261da177e4SLinus Torvalds return 0; 9271da177e4SLinus Torvalds } 928498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 9291da177e4SLinus Torvalds if (!tsk->fs) 9301da177e4SLinus Torvalds return -ENOMEM; 9311da177e4SLinus Torvalds return 0; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 934a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 935a016f338SJANAK DESAI { 936a016f338SJANAK DESAI struct files_struct *oldf, *newf; 937a016f338SJANAK DESAI int error = 0; 938a016f338SJANAK DESAI 939a016f338SJANAK DESAI /* 940a016f338SJANAK DESAI * A background process may not have any files ... 941a016f338SJANAK DESAI */ 942a016f338SJANAK DESAI oldf = current->files; 943a016f338SJANAK DESAI if (!oldf) 944a016f338SJANAK DESAI goto out; 945a016f338SJANAK DESAI 946a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 947a016f338SJANAK DESAI atomic_inc(&oldf->count); 948a016f338SJANAK DESAI goto out; 949a016f338SJANAK DESAI } 950a016f338SJANAK DESAI 951a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 952a016f338SJANAK DESAI if (!newf) 953a016f338SJANAK DESAI goto out; 954a016f338SJANAK DESAI 955a016f338SJANAK DESAI tsk->files = newf; 956a016f338SJANAK DESAI error = 0; 957a016f338SJANAK DESAI out: 958a016f338SJANAK DESAI return error; 959a016f338SJANAK DESAI } 960a016f338SJANAK DESAI 961fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 962fd0928dfSJens Axboe { 963fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 964fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 9656e736be7STejun Heo struct io_context *new_ioc; 966fd0928dfSJens Axboe 967fd0928dfSJens Axboe if (!ioc) 968fd0928dfSJens Axboe return 0; 969fadad878SJens Axboe /* 970fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 971fadad878SJens Axboe */ 972fadad878SJens Axboe if (clone_flags & CLONE_IO) { 9733d48749dSTejun Heo ioc_task_link(ioc); 9743d48749dSTejun Heo tsk->io_context = ioc; 975fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 9766e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 9776e736be7STejun Heo if (unlikely(!new_ioc)) 978fd0928dfSJens Axboe return -ENOMEM; 979fd0928dfSJens Axboe 9806e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 98111a3122fSTejun Heo put_io_context(new_ioc); 982fd0928dfSJens Axboe } 983fd0928dfSJens Axboe #endif 984fd0928dfSJens Axboe return 0; 985fd0928dfSJens Axboe } 986fd0928dfSJens Axboe 987a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 9881da177e4SLinus Torvalds { 9891da177e4SLinus Torvalds struct sighand_struct *sig; 9901da177e4SLinus Torvalds 99160348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 9921da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 9931da177e4SLinus Torvalds return 0; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 996e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 9971da177e4SLinus Torvalds if (!sig) 9981da177e4SLinus Torvalds return -ENOMEM; 9991da177e4SLinus Torvalds atomic_set(&sig->count, 1); 10001da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 10011da177e4SLinus Torvalds return 0; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 1004a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1005c81addc9SOleg Nesterov { 1006d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 1007d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1008c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1009c81addc9SOleg Nesterov } 1010d80e731eSOleg Nesterov } 1011c81addc9SOleg Nesterov 1012f06febc9SFrank Mayhar 1013f06febc9SFrank Mayhar /* 1014f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1015f06febc9SFrank Mayhar */ 1016f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1017f06febc9SFrank Mayhar { 101878d7d407SJiri Slaby unsigned long cpu_limit; 101978d7d407SJiri Slaby 1020f06febc9SFrank Mayhar /* Thread group counters. */ 1021f06febc9SFrank Mayhar thread_group_cputime_init(sig); 1022f06febc9SFrank Mayhar 102378d7d407SJiri Slaby cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 102478d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 102578d7d407SJiri Slaby sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); 10266279a751SOleg Nesterov sig->cputimer.running = 1; 10276279a751SOleg Nesterov } 10286279a751SOleg Nesterov 1029f06febc9SFrank Mayhar /* The timer lists. */ 1030f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1031f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1032f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1033f06febc9SFrank Mayhar } 1034f06febc9SFrank Mayhar 1035a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 10361da177e4SLinus Torvalds { 10371da177e4SLinus Torvalds struct signal_struct *sig; 10381da177e4SLinus Torvalds 10394ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1040490dea45SPeter Zijlstra return 0; 10416279a751SOleg Nesterov 1042a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 10431da177e4SLinus Torvalds tsk->signal = sig; 10441da177e4SLinus Torvalds if (!sig) 10451da177e4SLinus Torvalds return -ENOMEM; 10461da177e4SLinus Torvalds 1047b3ac022cSOleg Nesterov sig->nr_threads = 1; 10481da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1049b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 10500c740d0aSOleg Nesterov 10510c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 10520c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 10530c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 10540c740d0aSOleg Nesterov 10551da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1056db51aeccSOleg Nesterov sig->curr_target = tsk; 10571da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 10581da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 10591da177e4SLinus Torvalds 1060c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 10611da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds task_lock(current->group_leader); 10641da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 10651da177e4SLinus Torvalds task_unlock(current->group_leader); 10661da177e4SLinus Torvalds 10676279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 10686279a751SOleg Nesterov 1069522ed776SMiloslav Trmac tty_audit_fork(sig); 10705091faa4SMike Galbraith sched_autogroup_fork(sig); 1071522ed776SMiloslav Trmac 10724714d1d3SBen Blum #ifdef CONFIG_CGROUPS 1073257058aeSTejun Heo init_rwsem(&sig->group_rwsem); 10744714d1d3SBen Blum #endif 10754714d1d3SBen Blum 1076a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1077dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 107828b83c51SKOSAKI Motohiro 1079ebec18a6SLennart Poettering sig->has_child_subreaper = current->signal->has_child_subreaper || 1080ebec18a6SLennart Poettering current->signal->is_child_subreaper; 1081ebec18a6SLennart Poettering 10829b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 10839b1bf12dSKOSAKI Motohiro 10841da177e4SLinus Torvalds return 0; 10851da177e4SLinus Torvalds } 10861da177e4SLinus Torvalds 1087dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1088dbd95212SKees Cook { 1089dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1090dbd95212SKees Cook /* 1091dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1092dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1093dbd95212SKees Cook * needed because this new task is not yet running and cannot 1094dbd95212SKees Cook * be racing exec. 1095dbd95212SKees Cook */ 1096dbd95212SKees Cook BUG_ON(!spin_is_locked(¤t->sighand->siglock)); 1097dbd95212SKees Cook 1098dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1099dbd95212SKees Cook get_seccomp_filter(current); 1100dbd95212SKees Cook p->seccomp = current->seccomp; 1101dbd95212SKees Cook 1102dbd95212SKees Cook /* 1103dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1104dbd95212SKees Cook * between the task_struct being duplicated and holding the 1105dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1106dbd95212SKees Cook */ 1107dbd95212SKees Cook if (task_no_new_privs(current)) 1108dbd95212SKees Cook task_set_no_new_privs(p); 1109dbd95212SKees Cook 1110dbd95212SKees Cook /* 1111dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1112dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1113dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1114dbd95212SKees Cook */ 1115dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1116dbd95212SKees Cook set_tsk_thread_flag(p, TIF_SECCOMP); 1117dbd95212SKees Cook #endif 1118dbd95212SKees Cook } 1119dbd95212SKees Cook 112017da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 11211da177e4SLinus Torvalds { 11221da177e4SLinus Torvalds current->clear_child_tid = tidptr; 11231da177e4SLinus Torvalds 1124b488893aSPavel Emelyanov return task_pid_vnr(current); 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds 1127a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 112823f78d4aSIngo Molnar { 11291d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1130e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1131fb00aca4SPeter Zijlstra p->pi_waiters = RB_ROOT; 1132fb00aca4SPeter Zijlstra p->pi_waiters_leftmost = NULL; 113323f78d4aSIngo Molnar p->pi_blocked_on = NULL; 113423f78d4aSIngo Molnar #endif 113523f78d4aSIngo Molnar } 113623f78d4aSIngo Molnar 1137f98bafa0SOleg Nesterov #ifdef CONFIG_MEMCG 1138cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 1139cf475ad2SBalbir Singh { 1140cf475ad2SBalbir Singh mm->owner = p; 1141cf475ad2SBalbir Singh } 1142f98bafa0SOleg Nesterov #endif /* CONFIG_MEMCG */ 1143cf475ad2SBalbir Singh 11441da177e4SLinus Torvalds /* 1145f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1146f06febc9SFrank Mayhar */ 1147f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1148f06febc9SFrank Mayhar { 114964861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 115064861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1151f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1152f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1153f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1154f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1155f06febc9SFrank Mayhar } 1156f06febc9SFrank Mayhar 115781907739SOleg Nesterov static inline void 115881907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 115981907739SOleg Nesterov { 116081907739SOleg Nesterov task->pids[type].pid = pid; 116181907739SOleg Nesterov } 116281907739SOleg Nesterov 1163f06febc9SFrank Mayhar /* 11641da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 11651da177e4SLinus Torvalds * but does not actually start it yet. 11661da177e4SLinus Torvalds * 11671da177e4SLinus Torvalds * It copies the registers, and all the appropriate 11681da177e4SLinus Torvalds * parts of the process environment (as per the clone 11691da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 11701da177e4SLinus Torvalds */ 117136c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 11721da177e4SLinus Torvalds unsigned long stack_start, 11731da177e4SLinus Torvalds unsigned long stack_size, 11741da177e4SLinus Torvalds int __user *child_tidptr, 117509a05394SRoland McGrath struct pid *pid, 117609a05394SRoland McGrath int trace) 11771da177e4SLinus Torvalds { 11781da177e4SLinus Torvalds int retval; 1179a24efe62SMariusz Kozlowski struct task_struct *p; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 11821da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11831da177e4SLinus Torvalds 1184e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1185e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1186e66eded8SEric W. Biederman 11871da177e4SLinus Torvalds /* 11881da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 11891da177e4SLinus Torvalds * can only be started up within the thread group. 11901da177e4SLinus Torvalds */ 11911da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 11921da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /* 11951da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 11961da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 11971da177e4SLinus Torvalds * for various simplifications in other code. 11981da177e4SLinus Torvalds */ 11991da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 12001da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 12011da177e4SLinus Torvalds 1202123be07bSSukadev Bhattiprolu /* 1203123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1204123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1205123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1206123be07bSSukadev Bhattiprolu * from creating siblings. 1207123be07bSSukadev Bhattiprolu */ 1208123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1209123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1210123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1211123be07bSSukadev Bhattiprolu 12128382fcacSEric W. Biederman /* 121340a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 121440a0d32dSOleg Nesterov * do not allow it to share a thread group or signal handlers or 121540a0d32dSOleg Nesterov * parent with the forking task. 12168382fcacSEric W. Biederman */ 12171f7f4ddeSEric W. Biederman if (clone_flags & CLONE_SIGHAND) { 121840a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 121940a0d32dSOleg Nesterov (task_active_pid_ns(current) != 1220c2b1df2eSAndy Lutomirski current->nsproxy->pid_ns_for_children)) 12218382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 122240a0d32dSOleg Nesterov } 12238382fcacSEric W. Biederman 12241da177e4SLinus Torvalds retval = security_task_create(clone_flags); 12251da177e4SLinus Torvalds if (retval) 12261da177e4SLinus Torvalds goto fork_out; 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds retval = -ENOMEM; 12291da177e4SLinus Torvalds p = dup_task_struct(current); 12301da177e4SLinus Torvalds if (!p) 12311da177e4SLinus Torvalds goto fork_out; 12321da177e4SLinus Torvalds 1233f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1234f7e8b616SSteven Rostedt 1235bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1236bea493a0SPeter Zijlstra 1237d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1238de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1239de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1240de30a2b3SIngo Molnar #endif 12411da177e4SLinus Torvalds retval = -EAGAIN; 12423b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 124378d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1244b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1245b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 12461da177e4SLinus Torvalds goto bad_fork_free; 12471da177e4SLinus Torvalds } 124872fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 12491da177e4SLinus Torvalds 1250f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1251f1752eecSDavid Howells if (retval < 0) 1252f1752eecSDavid Howells goto bad_fork_free; 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds /* 12551da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 12561da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 12571da177e4SLinus Torvalds * to stop root fork bombs. 12581da177e4SLinus Torvalds */ 125904ec93feSLi Zefan retval = -EAGAIN; 12601da177e4SLinus Torvalds if (nr_threads >= max_threads) 12611da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12621da177e4SLinus Torvalds 1263a1261f54SAl Viro if (!try_module_get(task_thread_info(p)->exec_domain->module)) 12641da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12651da177e4SLinus Torvalds 1266ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1267514ddb44SDavid Rientjes p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER); 1268514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 12691da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 12701da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1271f41d911fSPaul E. McKenney rcu_copy_process(p); 12721da177e4SLinus Torvalds p->vfork_done = NULL; 12731da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds init_sigpending(&p->pending); 12761da177e4SLinus Torvalds 127764861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 127864861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 12799fbc42eaSFrederic Weisbecker #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 1280d37f761dSFrederic Weisbecker p->prev_cputime.utime = p->prev_cputime.stime = 0; 1281d99ca3b9SHidetoshi Seto #endif 12826a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 12836a61671bSFrederic Weisbecker seqlock_init(&p->vtime_seqlock); 12846a61671bSFrederic Weisbecker p->vtime_snap = 0; 12856a61671bSFrederic Weisbecker p->vtime_snap_whence = VTIME_SLEEPING; 12866a61671bSFrederic Weisbecker #endif 12876a61671bSFrederic Weisbecker 1288a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1289a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1290a3a2e76cSKAMEZAWA Hiroyuki #endif 1291172ba844SBalbir Singh 12926976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 12936976675dSArjan van de Ven 12945995477aSAndrea Righi task_io_accounting_init(&p->ioac); 12951da177e4SLinus Torvalds acct_clear_integrals(p); 12961da177e4SLinus Torvalds 1297f06febc9SFrank Mayhar posix_cpu_timers_init(p); 12981da177e4SLinus Torvalds 1299ccbf62d8SThomas Gleixner p->start_time = ktime_get_ns(); 130057e0be04SThomas Gleixner p->real_start_time = ktime_get_boot_ns(); 13011da177e4SLinus Torvalds p->io_context = NULL; 13021da177e4SLinus Torvalds p->audit_context = NULL; 13034714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1304257058aeSTejun Heo threadgroup_change_begin(current); 1305b4f48b63SPaul Menage cgroup_fork(p); 13061da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1307846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 13081da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 13091da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 13101da177e4SLinus Torvalds p->mempolicy = NULL; 1311e8604cb4SLi Zefan goto bad_fork_cleanup_threadgroup_lock; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds #endif 1314778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1315778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1316778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1317cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1318778d3b0fSMichal Hocko #endif 1319de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1320de30a2b3SIngo Molnar p->irq_events = 0; 1321de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1322de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1323de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1324de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1325de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1326de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1327de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1328de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1329de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1330de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1331de30a2b3SIngo Molnar p->hardirq_context = 0; 1332de30a2b3SIngo Molnar p->softirq_context = 0; 1333de30a2b3SIngo Molnar #endif 1334fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1335fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1336fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1337fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1338fbb9ce95SIngo Molnar #endif 13391da177e4SLinus Torvalds 1340408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1341408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1342408894eeSIngo Molnar #endif 1343cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 1344cafe5635SKent Overstreet p->sequential_io = 0; 1345cafe5635SKent Overstreet p->sequential_io_avg = 0; 1346cafe5635SKent Overstreet #endif 13470f481406SMarkus Metzger 13483c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 1349aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 1350aab03e05SDario Faggioli if (retval) 1351aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 13526ab423e0SPeter Zijlstra 1353cdd6c482SIngo Molnar retval = perf_event_init_task(p); 13546ab423e0SPeter Zijlstra if (retval) 13556ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1356fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1357fb0a685cSDaniel Rebelo de Oliveira if (retval) 1358f1752eecSDavid Howells goto bad_fork_cleanup_policy; 13591da177e4SLinus Torvalds /* copy all the process information */ 1360fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1361fb0a685cSDaniel Rebelo de Oliveira if (retval) 13621da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1363fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1364fb0a685cSDaniel Rebelo de Oliveira if (retval) 13651da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1366fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1367fb0a685cSDaniel Rebelo de Oliveira if (retval) 13681da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1369fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1370fb0a685cSDaniel Rebelo de Oliveira if (retval) 13711da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1372fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1373fb0a685cSDaniel Rebelo de Oliveira if (retval) 13741da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1375fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1376fb0a685cSDaniel Rebelo de Oliveira if (retval) 13771da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1378fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1379fb0a685cSDaniel Rebelo de Oliveira if (retval) 1380d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1381fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1382fb0a685cSDaniel Rebelo de Oliveira if (retval) 1383fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 1384afa86fc4SAl Viro retval = copy_thread(clone_flags, stack_start, stack_size, p); 13851da177e4SLinus Torvalds if (retval) 1386fd0928dfSJens Axboe goto bad_fork_cleanup_io; 13871da177e4SLinus Torvalds 1388425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1389425fb2b4SPavel Emelyanov retval = -ENOMEM; 1390c2b1df2eSAndy Lutomirski pid = alloc_pid(p->nsproxy->pid_ns_for_children); 1391425fb2b4SPavel Emelyanov if (!pid) 1392fd0928dfSJens Axboe goto bad_fork_cleanup_io; 1393425fb2b4SPavel Emelyanov } 1394425fb2b4SPavel Emelyanov 13951da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 13961da177e4SLinus Torvalds /* 13971da177e4SLinus Torvalds * Clear TID on mm_release()? 13981da177e4SLinus Torvalds */ 13991da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 140073c10101SJens Axboe #ifdef CONFIG_BLOCK 140173c10101SJens Axboe p->plug = NULL; 140273c10101SJens Axboe #endif 140342b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 14048f17d3a5SIngo Molnar p->robust_list = NULL; 14058f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 14068f17d3a5SIngo Molnar p->compat_robust_list = NULL; 14078f17d3a5SIngo Molnar #endif 1408c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1409c87e2837SIngo Molnar p->pi_state_cache = NULL; 141042b2dd0aSAlexey Dobriyan #endif 14111da177e4SLinus Torvalds /* 1412f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1413f9a3879aSGOTO Masanori */ 1414f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 1415f9a3879aSGOTO Masanori p->sas_ss_sp = p->sas_ss_size = 0; 1416f9a3879aSGOTO Masanori 1417f9a3879aSGOTO Masanori /* 14186580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 14196580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 14201da177e4SLinus Torvalds */ 14216580807dSOleg Nesterov user_disable_single_step(p); 14221da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1423ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1424ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1425ed75e8d5SLaurent Vivier #endif 14269745512cSArjan van de Ven clear_all_latency_tracing(p); 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds /* ok, now we should be set up.. */ 142918c830dfSOleg Nesterov p->pid = pid_nr(pid); 143018c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 14315f8aadd8SOleg Nesterov p->exit_signal = -1; 143218c830dfSOleg Nesterov p->group_leader = current->group_leader; 143318c830dfSOleg Nesterov p->tgid = current->tgid; 143418c830dfSOleg Nesterov } else { 143518c830dfSOleg Nesterov if (clone_flags & CLONE_PARENT) 14365f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 14375f8aadd8SOleg Nesterov else 14385f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 143918c830dfSOleg Nesterov p->group_leader = p; 144018c830dfSOleg Nesterov p->tgid = p->pid; 144118c830dfSOleg Nesterov } 14425f8aadd8SOleg Nesterov 14439d823e8fSWu Fengguang p->nr_dirtied = 0; 14449d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 144583712358SWu Fengguang p->dirty_paused_when = 0; 14469d823e8fSWu Fengguang 1447bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 144847e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 1449158e1645SAl Viro p->task_works = NULL; 14501da177e4SLinus Torvalds 145118c830dfSOleg Nesterov /* 145218c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 145318c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 145418c830dfSOleg Nesterov */ 14551da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 14582d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 14591da177e4SLinus Torvalds p->real_parent = current->real_parent; 14602d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 14612d5516cbSOleg Nesterov } else { 14621da177e4SLinus Torvalds p->real_parent = current; 14632d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 14642d5516cbSOleg Nesterov } 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 14674a2c7a78SOleg Nesterov 14684a2c7a78SOleg Nesterov /* 1469dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 1470dbd95212SKees Cook * before holding sighand lock. 1471dbd95212SKees Cook */ 1472dbd95212SKees Cook copy_seccomp(p); 1473dbd95212SKees Cook 1474dbd95212SKees Cook /* 14754a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 14764a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 14774a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 14784a2c7a78SOleg Nesterov * it's process group. 14794a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 14804a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 14814a2c7a78SOleg Nesterov */ 14824a2c7a78SOleg Nesterov recalc_sigpending(); 14834a2c7a78SOleg Nesterov if (signal_pending(current)) { 14844a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 14854a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 14864a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 1487f7e8b616SSteven Rostedt goto bad_fork_free_pid; 14884a2c7a78SOleg Nesterov } 14894a2c7a78SOleg Nesterov 149073b9ebfeSOleg Nesterov if (likely(p->pid)) { 14914b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 14921da177e4SLinus Torvalds 149381907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 14941da177e4SLinus Torvalds if (thread_group_leader(p)) { 149581907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 149681907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 149781907739SOleg Nesterov 14981c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 149917cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 15001c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 15011c4042c2SEric W. Biederman } 15025cd17569SEric W. Biederman 1503fea9d175SOleg Nesterov p->signal->leader_pid = pid; 15049c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 15059cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 15065e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 150781907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 150881907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 1509909ea964SChristoph Lameter __this_cpu_inc(process_counts); 151080628ca0SOleg Nesterov } else { 151180628ca0SOleg Nesterov current->signal->nr_threads++; 151280628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 151380628ca0SOleg Nesterov atomic_inc(¤t->signal->sigcnt); 151480628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 151580628ca0SOleg Nesterov &p->group_leader->thread_group); 15160c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 15170c740d0aSOleg Nesterov &p->signal->thread_head); 15181da177e4SLinus Torvalds } 151981907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 15201da177e4SLinus Torvalds nr_threads++; 152173b9ebfeSOleg Nesterov } 152273b9ebfeSOleg Nesterov 15231da177e4SLinus Torvalds total_forks++; 15243f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 15254af4206bSOleg Nesterov syscall_tracepoint_update(p); 15261da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 15274af4206bSOleg Nesterov 1528c13cf856SAndrew Morton proc_fork_connector(p); 1529817929ecSPaul Menage cgroup_post_fork(p); 15304714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1531257058aeSTejun Heo threadgroup_change_end(current); 1532cdd6c482SIngo Molnar perf_event_fork(p); 153343d2b113SKAMEZAWA Hiroyuki 153443d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 15353ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 153643d2b113SKAMEZAWA Hiroyuki 15371da177e4SLinus Torvalds return p; 15381da177e4SLinus Torvalds 1539425fb2b4SPavel Emelyanov bad_fork_free_pid: 1540425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1541425fb2b4SPavel Emelyanov free_pid(pid); 1542fd0928dfSJens Axboe bad_fork_cleanup_io: 1543b69f2292SLouis Rilling if (p->io_context) 1544b69f2292SLouis Rilling exit_io_context(p); 1545ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1546444f378bSLinus Torvalds exit_task_namespaces(p); 15471da177e4SLinus Torvalds bad_fork_cleanup_mm: 1548c9f01245SDavid Rientjes if (p->mm) 15491da177e4SLinus Torvalds mmput(p->mm); 15501da177e4SLinus Torvalds bad_fork_cleanup_signal: 15514ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 15521c5354deSMike Galbraith free_signal_struct(p->signal); 15531da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1554a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 15551da177e4SLinus Torvalds bad_fork_cleanup_fs: 15561da177e4SLinus Torvalds exit_fs(p); /* blocking */ 15571da177e4SLinus Torvalds bad_fork_cleanup_files: 15581da177e4SLinus Torvalds exit_files(p); /* blocking */ 15591da177e4SLinus Torvalds bad_fork_cleanup_semundo: 15601da177e4SLinus Torvalds exit_sem(p); 15611da177e4SLinus Torvalds bad_fork_cleanup_audit: 15621da177e4SLinus Torvalds audit_free(p); 15631da177e4SLinus Torvalds bad_fork_cleanup_policy: 1564cdd6c482SIngo Molnar perf_event_free_task(p); 15651da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1566f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1567e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock: 15681da177e4SLinus Torvalds #endif 15694714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1570257058aeSTejun Heo threadgroup_change_end(current); 157135df17c5SShailabh Nagar delayacct_tsk_free(p); 1572a1261f54SAl Viro module_put(task_thread_info(p)->exec_domain->module); 15731da177e4SLinus Torvalds bad_fork_cleanup_count: 1574d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1575e0e81739SDavid Howells exit_creds(p); 15761da177e4SLinus Torvalds bad_fork_free: 15771da177e4SLinus Torvalds free_task(p); 1578fe7d37d1SOleg Nesterov fork_out: 1579fe7d37d1SOleg Nesterov return ERR_PTR(retval); 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 1582f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1583f106eee1SOleg Nesterov { 1584f106eee1SOleg Nesterov enum pid_type type; 1585f106eee1SOleg Nesterov 1586f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1587f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1588f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1589f106eee1SOleg Nesterov } 1590f106eee1SOleg Nesterov } 1591f106eee1SOleg Nesterov 15920db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 15931da177e4SLinus Torvalds { 159436c8b586SIngo Molnar struct task_struct *task; 159562e791c1SAl Viro task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0); 1596f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1597f106eee1SOleg Nesterov init_idle_pids(task->pids); 15981da177e4SLinus Torvalds init_idle(task, cpu); 1599f106eee1SOleg Nesterov } 160073b9ebfeSOleg Nesterov 16011da177e4SLinus Torvalds return task; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds /* 16051da177e4SLinus Torvalds * Ok, this is the main fork-routine. 16061da177e4SLinus Torvalds * 16071da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 16081da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 16091da177e4SLinus Torvalds */ 16101da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 16111da177e4SLinus Torvalds unsigned long stack_start, 16121da177e4SLinus Torvalds unsigned long stack_size, 16131da177e4SLinus Torvalds int __user *parent_tidptr, 16141da177e4SLinus Torvalds int __user *child_tidptr) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds struct task_struct *p; 16171da177e4SLinus Torvalds int trace = 0; 161892476d7fSEric W. Biederman long nr; 16191da177e4SLinus Torvalds 1620bdff746aSAndrew Morton /* 16214b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 16224b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 16234b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 16244b9d33e6STejun Heo * for the type of forking is enabled. 162509a05394SRoland McGrath */ 1626e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 16274b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 16284b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 16294b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 16304b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 16314b9d33e6STejun Heo else 16324b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 16334b9d33e6STejun Heo 16344b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 16354b9d33e6STejun Heo trace = 0; 16364b9d33e6STejun Heo } 16371da177e4SLinus Torvalds 163862e791c1SAl Viro p = copy_process(clone_flags, stack_start, stack_size, 163909a05394SRoland McGrath child_tidptr, NULL, trace); 16401da177e4SLinus Torvalds /* 16411da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 16421da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 16431da177e4SLinus Torvalds */ 16441da177e4SLinus Torvalds if (!IS_ERR(p)) { 16451da177e4SLinus Torvalds struct completion vfork; 16464e52365fSMatthew Dempsky struct pid *pid; 16471da177e4SLinus Torvalds 16480a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 16490a16b607SMathieu Desnoyers 16504e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 16514e52365fSMatthew Dempsky nr = pid_vnr(pid); 165230e49c26SPavel Emelyanov 165330e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 165430e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1655a6f5e063SSukadev Bhattiprolu 16561da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 16571da177e4SLinus Torvalds p->vfork_done = &vfork; 16581da177e4SLinus Torvalds init_completion(&vfork); 1659d68b46feSOleg Nesterov get_task_struct(p); 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16623e51e3edSSamir Bellabes wake_up_new_task(p); 16631da177e4SLinus Torvalds 16644b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 16654b9d33e6STejun Heo if (unlikely(trace)) 16664e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 166709a05394SRoland McGrath 16681da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1669d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 16704e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 16719f59ce5dSChuck Ebbert } 16724e52365fSMatthew Dempsky 16734e52365fSMatthew Dempsky put_pid(pid); 16741da177e4SLinus Torvalds } else { 167592476d7fSEric W. Biederman nr = PTR_ERR(p); 16761da177e4SLinus Torvalds } 167792476d7fSEric W. Biederman return nr; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 16802aa3a7f8SAl Viro /* 16812aa3a7f8SAl Viro * Create a kernel thread. 16822aa3a7f8SAl Viro */ 16832aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 16842aa3a7f8SAl Viro { 1685e80d6661SAl Viro return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, 16862aa3a7f8SAl Viro (unsigned long)arg, NULL, NULL); 16872aa3a7f8SAl Viro } 16882aa3a7f8SAl Viro 1689d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 1690d2125043SAl Viro SYSCALL_DEFINE0(fork) 1691d2125043SAl Viro { 1692d2125043SAl Viro #ifdef CONFIG_MMU 1693e80d6661SAl Viro return do_fork(SIGCHLD, 0, 0, NULL, NULL); 1694d2125043SAl Viro #else 1695d2125043SAl Viro /* can not support in nommu mode */ 16965d59e182SDaeseok Youn return -EINVAL; 1697d2125043SAl Viro #endif 1698d2125043SAl Viro } 1699d2125043SAl Viro #endif 1700d2125043SAl Viro 1701d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 1702d2125043SAl Viro SYSCALL_DEFINE0(vfork) 1703d2125043SAl Viro { 1704e80d6661SAl Viro return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 1705d2125043SAl Viro 0, NULL, NULL); 1706d2125043SAl Viro } 1707d2125043SAl Viro #endif 1708d2125043SAl Viro 1709d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 1710d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 1711d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1712d2125043SAl Viro int __user *, parent_tidptr, 1713d2125043SAl Viro int, tls_val, 1714d2125043SAl Viro int __user *, child_tidptr) 1715d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 1716d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 1717d2125043SAl Viro int __user *, parent_tidptr, 1718d2125043SAl Viro int __user *, child_tidptr, 1719d2125043SAl Viro int, tls_val) 1720dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 1721dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 1722dfa9771aSMichal Simek int, stack_size, 1723dfa9771aSMichal Simek int __user *, parent_tidptr, 1724dfa9771aSMichal Simek int __user *, child_tidptr, 1725dfa9771aSMichal Simek int, tls_val) 1726d2125043SAl Viro #else 1727d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1728d2125043SAl Viro int __user *, parent_tidptr, 1729d2125043SAl Viro int __user *, child_tidptr, 1730d2125043SAl Viro int, tls_val) 1731d2125043SAl Viro #endif 1732d2125043SAl Viro { 17332cf09666SAl Viro return do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr); 1734d2125043SAl Viro } 1735d2125043SAl Viro #endif 1736d2125043SAl Viro 17375fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 17385fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 17395fd63b30SRavikiran G Thirumalai #endif 17405fd63b30SRavikiran G Thirumalai 174151cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 1742aa1757f9SOleg Nesterov { 1743aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1744aa1757f9SOleg Nesterov 1745aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1746b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 1747fba2afaaSDavide Libenzi } 1748aa1757f9SOleg Nesterov 17491da177e4SLinus Torvalds void __init proc_caches_init(void) 17501da177e4SLinus Torvalds { 17511da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 17521da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 17532dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 17542dff4405SVegard Nossum SLAB_NOTRACK, sighand_ctor); 17551da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 17561da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 17572dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17581da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 17591da177e4SLinus Torvalds sizeof(struct files_struct), 0, 17602dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17611da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 17621da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 17632dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17646345d24dSLinus Torvalds /* 17656345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 17666345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 17676345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 17686345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 17696345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 17706345d24dSLinus Torvalds */ 17711da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 17725fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 17732dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 177433e5d769SDavid Howells vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC); 17758feae131SDavid Howells mmap_init(); 177666577193SAl Viro nsproxy_cache_init(); 17771da177e4SLinus Torvalds } 1778cf2e340fSJANAK DESAI 1779cf2e340fSJANAK DESAI /* 17809bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 1781cf2e340fSJANAK DESAI */ 17829bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 1783cf2e340fSJANAK DESAI { 17849bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 17859bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 178650804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 1787b2e0d987SEric W. Biederman CLONE_NEWUSER|CLONE_NEWPID)) 1788cf2e340fSJANAK DESAI return -EINVAL; 17899bfb23fcSOleg Nesterov /* 17909bfb23fcSOleg Nesterov * Not implemented, but pretend it works if there is nothing to 17919bfb23fcSOleg Nesterov * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND 17929bfb23fcSOleg Nesterov * needs to unshare vm. 17939bfb23fcSOleg Nesterov */ 17949bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 17959bfb23fcSOleg Nesterov /* FIXME: get_task_mm() increments ->mm_users */ 17969bfb23fcSOleg Nesterov if (atomic_read(¤t->mm->mm_users) > 1) 17979bfb23fcSOleg Nesterov return -EINVAL; 17989bfb23fcSOleg Nesterov } 1799cf2e340fSJANAK DESAI 1800cf2e340fSJANAK DESAI return 0; 1801cf2e340fSJANAK DESAI } 1802cf2e340fSJANAK DESAI 1803cf2e340fSJANAK DESAI /* 180499d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1805cf2e340fSJANAK DESAI */ 1806cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1807cf2e340fSJANAK DESAI { 1808cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1809cf2e340fSJANAK DESAI 1810498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 1811498052bbSAl Viro return 0; 1812498052bbSAl Viro 1813498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 1814498052bbSAl Viro if (fs->users == 1) 1815498052bbSAl Viro return 0; 1816498052bbSAl Viro 1817498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 181899d1419dSJANAK DESAI if (!*new_fsp) 181999d1419dSJANAK DESAI return -ENOMEM; 1820cf2e340fSJANAK DESAI 1821cf2e340fSJANAK DESAI return 0; 1822cf2e340fSJANAK DESAI } 1823cf2e340fSJANAK DESAI 1824cf2e340fSJANAK DESAI /* 1825a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1826cf2e340fSJANAK DESAI */ 1827cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1828cf2e340fSJANAK DESAI { 1829cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1830a016f338SJANAK DESAI int error = 0; 1831cf2e340fSJANAK DESAI 1832cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1833a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1834a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1835a016f338SJANAK DESAI if (!*new_fdp) 1836a016f338SJANAK DESAI return error; 1837a016f338SJANAK DESAI } 1838cf2e340fSJANAK DESAI 1839cf2e340fSJANAK DESAI return 0; 1840cf2e340fSJANAK DESAI } 1841cf2e340fSJANAK DESAI 1842cf2e340fSJANAK DESAI /* 1843cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1844cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1845cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1846cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1847cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1848cf2e340fSJANAK DESAI * task_struct. 1849cf2e340fSJANAK DESAI */ 18506559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 1851cf2e340fSJANAK DESAI { 1852cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1853cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1854b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 1855cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 18569edff4abSManfred Spraul int do_sysvsem = 0; 18579bfb23fcSOleg Nesterov int err; 1858cf2e340fSJANAK DESAI 185950804fe3SEric W. Biederman /* 1860b2e0d987SEric W. Biederman * If unsharing a user namespace must also unshare the thread. 1861b2e0d987SEric W. Biederman */ 1862b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 1863e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 1864b2e0d987SEric W. Biederman /* 186550804fe3SEric W. Biederman * If unsharing a thread from a thread group, must also unshare vm. 186650804fe3SEric W. Biederman */ 186750804fe3SEric W. Biederman if (unshare_flags & CLONE_THREAD) 186850804fe3SEric W. Biederman unshare_flags |= CLONE_VM; 186950804fe3SEric W. Biederman /* 187050804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 187150804fe3SEric W. Biederman */ 187250804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 187350804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 18746013f67fSManfred Spraul /* 18759bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 18769bfb23fcSOleg Nesterov */ 18779bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 18789bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 187950804fe3SEric W. Biederman 188050804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 188150804fe3SEric W. Biederman if (err) 188250804fe3SEric W. Biederman goto bad_unshare_out; 18839bfb23fcSOleg Nesterov /* 18846013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 18856013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 18866013f67fSManfred Spraul * namespace are unreachable. 18876013f67fSManfred Spraul */ 18886013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 18899edff4abSManfred Spraul do_sysvsem = 1; 1890fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 1891fb0a685cSDaniel Rebelo de Oliveira if (err) 18929bfb23fcSOleg Nesterov goto bad_unshare_out; 1893fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 1894fb0a685cSDaniel Rebelo de Oliveira if (err) 18959bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 1896b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 1897fb0a685cSDaniel Rebelo de Oliveira if (err) 18989edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 1899b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 1900b2e0d987SEric W. Biederman new_cred, new_fs); 1901b2e0d987SEric W. Biederman if (err) 1902b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 1903cf2e340fSJANAK DESAI 1904b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 19059edff4abSManfred Spraul if (do_sysvsem) { 19069edff4abSManfred Spraul /* 19079edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 19089edff4abSManfred Spraul */ 19099edff4abSManfred Spraul exit_sem(current); 19109edff4abSManfred Spraul } 1911ab516013SSerge E. Hallyn 19126f977e6bSAlan Cox if (new_nsproxy) 1913cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 1914cf2e340fSJANAK DESAI 1915cf7b708cSPavel Emelyanov task_lock(current); 1916cf7b708cSPavel Emelyanov 1917cf2e340fSJANAK DESAI if (new_fs) { 1918cf2e340fSJANAK DESAI fs = current->fs; 19192a4419b5SNick Piggin spin_lock(&fs->lock); 1920cf2e340fSJANAK DESAI current->fs = new_fs; 1921498052bbSAl Viro if (--fs->users) 1922498052bbSAl Viro new_fs = NULL; 1923498052bbSAl Viro else 1924cf2e340fSJANAK DESAI new_fs = fs; 19252a4419b5SNick Piggin spin_unlock(&fs->lock); 1926cf2e340fSJANAK DESAI } 1927cf2e340fSJANAK DESAI 1928cf2e340fSJANAK DESAI if (new_fd) { 1929cf2e340fSJANAK DESAI fd = current->files; 1930cf2e340fSJANAK DESAI current->files = new_fd; 1931cf2e340fSJANAK DESAI new_fd = fd; 1932cf2e340fSJANAK DESAI } 1933cf2e340fSJANAK DESAI 1934cf2e340fSJANAK DESAI task_unlock(current); 1935b2e0d987SEric W. Biederman 1936b2e0d987SEric W. Biederman if (new_cred) { 1937b2e0d987SEric W. Biederman /* Install the new user namespace */ 1938b2e0d987SEric W. Biederman commit_creds(new_cred); 1939b2e0d987SEric W. Biederman new_cred = NULL; 1940b2e0d987SEric W. Biederman } 1941cf2e340fSJANAK DESAI } 1942cf2e340fSJANAK DESAI 1943b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 1944b2e0d987SEric W. Biederman if (new_cred) 1945b2e0d987SEric W. Biederman put_cred(new_cred); 1946cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 1947cf2e340fSJANAK DESAI if (new_fd) 1948cf2e340fSJANAK DESAI put_files_struct(new_fd); 1949cf2e340fSJANAK DESAI 1950cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 1951cf2e340fSJANAK DESAI if (new_fs) 1952498052bbSAl Viro free_fs_struct(new_fs); 1953cf2e340fSJANAK DESAI 1954cf2e340fSJANAK DESAI bad_unshare_out: 1955cf2e340fSJANAK DESAI return err; 1956cf2e340fSJANAK DESAI } 19573b125388SAl Viro 19583b125388SAl Viro /* 19593b125388SAl Viro * Helper to unshare the files of the current task. 19603b125388SAl Viro * We don't want to expose copy_files internals to 19613b125388SAl Viro * the exec layer of the kernel. 19623b125388SAl Viro */ 19633b125388SAl Viro 19643b125388SAl Viro int unshare_files(struct files_struct **displaced) 19653b125388SAl Viro { 19663b125388SAl Viro struct task_struct *task = current; 196750704516SAl Viro struct files_struct *copy = NULL; 19683b125388SAl Viro int error; 19693b125388SAl Viro 19703b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 19713b125388SAl Viro if (error || !copy) { 19723b125388SAl Viro *displaced = NULL; 19733b125388SAl Viro return error; 19743b125388SAl Viro } 19753b125388SAl Viro *displaced = task->files; 19763b125388SAl Viro task_lock(task); 19773b125388SAl Viro task->files = copy; 19783b125388SAl Viro task_unlock(task); 19793b125388SAl Viro return 0; 19803b125388SAl Viro } 1981