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 297d4311ff1SAaron Tomlin void set_task_stack_end_magic(struct task_struct *tsk) 298d4311ff1SAaron Tomlin { 299d4311ff1SAaron Tomlin unsigned long *stackend; 300d4311ff1SAaron Tomlin 301d4311ff1SAaron Tomlin stackend = end_of_stack(tsk); 302d4311ff1SAaron Tomlin *stackend = STACK_END_MAGIC; /* for overflow detection */ 303d4311ff1SAaron Tomlin } 304d4311ff1SAaron Tomlin 3051da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds struct task_struct *tsk; 3081da177e4SLinus Torvalds struct thread_info *ti; 309207205a2SEric Dumazet int node = tsk_fork_get_node(orig); 3103e26c149SPeter Zijlstra int err; 3111da177e4SLinus Torvalds 312504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 3131da177e4SLinus Torvalds if (!tsk) 3141da177e4SLinus Torvalds return NULL; 3151da177e4SLinus Torvalds 316b6a84016SEric Dumazet ti = alloc_thread_info_node(tsk, node); 317f19b9f74SAkinobu Mita if (!ti) 318f19b9f74SAkinobu Mita goto free_tsk; 3191da177e4SLinus Torvalds 32061c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 32161c4628bSSuresh Siddha if (err) 322f19b9f74SAkinobu Mita goto free_ti; 32361c4628bSSuresh Siddha 32487bec58aSAndrew Morton tsk->stack = ti; 325dbd95212SKees Cook #ifdef CONFIG_SECCOMP 326dbd95212SKees Cook /* 327dbd95212SKees Cook * We must handle setting up seccomp filters once we're under 328dbd95212SKees Cook * the sighand lock in case orig has changed between now and 329dbd95212SKees Cook * then. Until then, filter must be NULL to avoid messing up 330dbd95212SKees Cook * the usage counts on the error path calling free_task. 331dbd95212SKees Cook */ 332dbd95212SKees Cook tsk->seccomp.filter = NULL; 333dbd95212SKees Cook #endif 33487bec58aSAndrew Morton 33587bec58aSAndrew Morton setup_thread_stack(tsk, orig); 3368e7cac79SAvi Kivity clear_user_return_notifier(tsk); 337f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 338d4311ff1SAaron Tomlin set_task_stack_end_magic(tsk); 3391da177e4SLinus Torvalds 3400a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 3410a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 3420a425405SArjan van de Ven #endif 3430a425405SArjan van de Ven 344fb0a685cSDaniel Rebelo de Oliveira /* 345fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 346fb0a685cSDaniel Rebelo de Oliveira * parent) 347fb0a685cSDaniel Rebelo de Oliveira */ 3481da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 3496c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 3502056a782SJens Axboe tsk->btrace_seq = 0; 3516c5c9341SAlexey Dobriyan #endif 352a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 3535640f768SEric Dumazet tsk->task_frag.page = NULL; 354c6a7f572SKOSAKI Motohiro 355c6a7f572SKOSAKI Motohiro account_kernel_stack(ti, 1); 356c6a7f572SKOSAKI Motohiro 3571da177e4SLinus Torvalds return tsk; 35861c4628bSSuresh Siddha 359f19b9f74SAkinobu Mita free_ti: 36061c4628bSSuresh Siddha free_thread_info(ti); 361f19b9f74SAkinobu Mita free_tsk: 36261c4628bSSuresh Siddha free_task_struct(tsk); 36361c4628bSSuresh Siddha return NULL; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds #ifdef CONFIG_MMU 367a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 3681da177e4SLinus Torvalds { 369297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 3701da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 3711da177e4SLinus Torvalds int retval; 3721da177e4SLinus Torvalds unsigned long charge; 3731da177e4SLinus Torvalds 37432cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 3751da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 376ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 377f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 378ad339451SIngo Molnar /* 379ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 380ad339451SIngo Molnar */ 381ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 3827ee78232SHugh Dickins 3834f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 3844f7d4614SVladimir Davydov mm->shared_vm = oldmm->shared_vm; 3854f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 3864f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 3874f7d4614SVladimir Davydov 3881da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 3891da177e4SLinus Torvalds rb_parent = NULL; 3901da177e4SLinus Torvalds pprev = &mm->mmap; 391f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 392f8af4da3SHugh Dickins if (retval) 393f8af4da3SHugh Dickins goto out; 394ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 395ba76149fSAndrea Arcangeli if (retval) 396ba76149fSAndrea Arcangeli goto out; 3971da177e4SLinus Torvalds 398297c5eeeSLinus Torvalds prev = NULL; 399fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 4001da177e4SLinus Torvalds struct file *file; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 403ab50b8edSHugh Dickins vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 40444de9d0cSHuang Shijie -vma_pages(mpnt)); 4051da177e4SLinus Torvalds continue; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds charge = 0; 4081da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 409b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 410b2412b7fSHuang Shijie 411191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 4121da177e4SLinus Torvalds goto fail_nomem; 4131da177e4SLinus Torvalds charge = len; 4141da177e4SLinus Torvalds } 415e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 4161da177e4SLinus Torvalds if (!tmp) 4171da177e4SLinus Torvalds goto fail_nomem; 4181da177e4SLinus Torvalds *tmp = *mpnt; 4195beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 420ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 421ef0855d3SOleg Nesterov if (retval) 4221da177e4SLinus Torvalds goto fail_nomem_policy; 423a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 4245beb4930SRik van Riel if (anon_vma_fork(tmp, mpnt)) 4255beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 4261da177e4SLinus Torvalds tmp->vm_flags &= ~VM_LOCKED; 427297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 4281da177e4SLinus Torvalds file = tmp->vm_file; 4291da177e4SLinus Torvalds if (file) { 430496ad9aaSAl Viro struct inode *inode = file_inode(file); 431b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 432b88ed205SHugh Dickins 4331da177e4SLinus Torvalds get_file(file); 4341da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 4351da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 4363d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 437b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 4384bb5f5d9SDavid Herrmann atomic_inc(&mapping->i_mmap_writable); 439b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 440b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 4419826a516SMichel Lespinasse if (unlikely(tmp->vm_flags & VM_NONLINEAR)) 4429826a516SMichel Lespinasse vma_nonlinear_insert(tmp, 4439826a516SMichel Lespinasse &mapping->i_mmap_nonlinear); 4449826a516SMichel Lespinasse else 4459826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 4469826a516SMichel Lespinasse &mapping->i_mmap); 447b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 4483d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds /* 452a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 453a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 454a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 455a1e78772SMel Gorman */ 456a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 457a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 458a1e78772SMel Gorman 459a1e78772SMel Gorman /* 4607ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 4611da177e4SLinus Torvalds */ 4621da177e4SLinus Torvalds *pprev = tmp; 4631da177e4SLinus Torvalds pprev = &tmp->vm_next; 464297c5eeeSLinus Torvalds tmp->vm_prev = prev; 465297c5eeeSLinus Torvalds prev = tmp; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 4681da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 4691da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds mm->map_count++; 4720b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 4751da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds if (retval) 4781da177e4SLinus Torvalds goto out; 4791da177e4SLinus Torvalds } 480d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 481d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 4821da177e4SLinus Torvalds retval = 0; 4831da177e4SLinus Torvalds out: 4847ee78232SHugh Dickins up_write(&mm->mmap_sem); 485fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 4861da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 48732cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 4881da177e4SLinus Torvalds return retval; 4895beb4930SRik van Riel fail_nomem_anon_vma_fork: 490ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 4911da177e4SLinus Torvalds fail_nomem_policy: 4921da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 4931da177e4SLinus Torvalds fail_nomem: 4941da177e4SLinus Torvalds retval = -ENOMEM; 4951da177e4SLinus Torvalds vm_unacct_memory(charge); 4961da177e4SLinus Torvalds goto out; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 5021da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 5031da177e4SLinus Torvalds return -ENOMEM; 5041da177e4SLinus Torvalds return 0; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 5081da177e4SLinus Torvalds { 5095e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds #else 5121da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 5131da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 5141da177e4SLinus Torvalds #define mm_free_pgd(mm) 5151da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 5181da177e4SLinus Torvalds 519e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 5201da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 5211da177e4SLinus Torvalds 5224cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 5234cb0e11bSHidehiro Kawai 5244cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 5254cb0e11bSHidehiro Kawai { 5264cb0e11bSHidehiro Kawai default_dump_filter = 5274cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 5284cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 5294cb0e11bSHidehiro Kawai return 1; 5304cb0e11bSHidehiro Kawai } 5314cb0e11bSHidehiro Kawai 5324cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 5334cb0e11bSHidehiro Kawai 5341da177e4SLinus Torvalds #include <linux/init_task.h> 5351da177e4SLinus Torvalds 536858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 537858f0993SAlexey Dobriyan { 538858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 539858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 540db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 541858f0993SAlexey Dobriyan #endif 542858f0993SAlexey Dobriyan } 543858f0993SAlexey Dobriyan 54433144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 54533144e84SVladimir Davydov { 54633144e84SVladimir Davydov #ifdef CONFIG_MEMCG 54733144e84SVladimir Davydov mm->owner = p; 54833144e84SVladimir Davydov #endif 54933144e84SVladimir Davydov } 55033144e84SVladimir Davydov 55178fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 5521da177e4SLinus Torvalds { 55341f727fdSVladimir Davydov mm->mmap = NULL; 55441f727fdSVladimir Davydov mm->mm_rb = RB_ROOT; 55541f727fdSVladimir Davydov mm->vmacache_seqnum = 0; 5561da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 5571da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 5581da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 5591da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 560999d9fc1SOleg Nesterov mm->core_state = NULL; 561e1f56c89SKirill A. Shutemov atomic_long_set(&mm->nr_ptes, 0); 56241f727fdSVladimir Davydov mm->map_count = 0; 56341f727fdSVladimir Davydov mm->locked_vm = 0; 564ce65cefaSVladimir Davydov mm->pinned_vm = 0; 565d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 5661da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 56741f727fdSVladimir Davydov mm_init_cpumask(mm); 568858f0993SAlexey Dobriyan mm_init_aio(mm); 569cf475ad2SBalbir Singh mm_init_owner(mm, p); 57041f727fdSVladimir Davydov mmu_notifier_mm_init(mm); 57120841405SRik van Riel clear_tlb_flush_pending(mm); 57241f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 57341f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 57441f727fdSVladimir Davydov #endif 5751da177e4SLinus Torvalds 576a0715cc2SAlex Thorlton if (current->mm) { 577a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 578a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 579a0715cc2SAlex Thorlton } else { 580a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 5811da177e4SLinus Torvalds mm->def_flags = 0; 582a0715cc2SAlex Thorlton } 583a0715cc2SAlex Thorlton 58441f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 58541f727fdSVladimir Davydov goto fail_nopgd; 58678fb7466SPavel Emelianov 58741f727fdSVladimir Davydov if (init_new_context(p, mm)) 58841f727fdSVladimir Davydov goto fail_nocontext; 58941f727fdSVladimir Davydov 59041f727fdSVladimir Davydov return mm; 59141f727fdSVladimir Davydov 59241f727fdSVladimir Davydov fail_nocontext: 59341f727fdSVladimir Davydov mm_free_pgd(mm); 59441f727fdSVladimir Davydov fail_nopgd: 5951da177e4SLinus Torvalds free_mm(mm); 5961da177e4SLinus Torvalds return NULL; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 599c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm) 600c3f0327fSKonstantin Khlebnikov { 601c3f0327fSKonstantin Khlebnikov int i; 602c3f0327fSKonstantin Khlebnikov 603c3f0327fSKonstantin Khlebnikov for (i = 0; i < NR_MM_COUNTERS; i++) { 604c3f0327fSKonstantin Khlebnikov long x = atomic_long_read(&mm->rss_stat.count[i]); 605c3f0327fSKonstantin Khlebnikov 606c3f0327fSKonstantin Khlebnikov if (unlikely(x)) 607c3f0327fSKonstantin Khlebnikov printk(KERN_ALERT "BUG: Bad rss-counter state " 608c3f0327fSKonstantin Khlebnikov "mm:%p idx:%d val:%ld\n", mm, i, x); 609c3f0327fSKonstantin Khlebnikov } 610e009bb30SKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 61196dad67fSSasha Levin VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 612c3f0327fSKonstantin Khlebnikov #endif 613c3f0327fSKonstantin Khlebnikov } 614c3f0327fSKonstantin Khlebnikov 6151da177e4SLinus Torvalds /* 6161da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 6171da177e4SLinus Torvalds */ 6181da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 6191da177e4SLinus Torvalds { 6201da177e4SLinus Torvalds struct mm_struct *mm; 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds mm = allocate_mm(); 623de03c72cSKOSAKI Motohiro if (!mm) 624de03c72cSKOSAKI Motohiro return NULL; 625de03c72cSKOSAKI Motohiro 6261da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 6276345d24dSLinus Torvalds return mm_init(mm, current); 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * Called when the last reference to the mm 6321da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 6331da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 6341da177e4SLinus Torvalds */ 6357ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 6381da177e4SLinus Torvalds mm_free_pgd(mm); 6391da177e4SLinus Torvalds destroy_context(mm); 640cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 641c3f0327fSKonstantin Khlebnikov check_mm(mm); 6421da177e4SLinus Torvalds free_mm(mm); 6431da177e4SLinus Torvalds } 6446d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* 6471da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 6481da177e4SLinus Torvalds */ 6491da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 6501da177e4SLinus Torvalds { 6510ae26f1bSAndrew Morton might_sleep(); 6520ae26f1bSAndrew Morton 6531da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 654d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 6551da177e4SLinus Torvalds exit_aio(mm); 6561c2fb7a4SAndrea Arcangeli ksm_exit(mm); 657ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 6581da177e4SLinus Torvalds exit_mmap(mm); 659925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 6601da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 6611da177e4SLinus Torvalds spin_lock(&mmlist_lock); 6621da177e4SLinus Torvalds list_del(&mm->mmlist); 6631da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 6641da177e4SLinus Torvalds } 665801460d0SHiroshi Shimamoto if (mm->binfmt) 666801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 6671da177e4SLinus Torvalds mmdrop(mm); 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 6711da177e4SLinus Torvalds 67238646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 67338646013SJiri Slaby { 67438646013SJiri Slaby if (new_exe_file) 67538646013SJiri Slaby get_file(new_exe_file); 67638646013SJiri Slaby if (mm->exe_file) 67738646013SJiri Slaby fput(mm->exe_file); 67838646013SJiri Slaby mm->exe_file = new_exe_file; 67938646013SJiri Slaby } 68038646013SJiri Slaby 68138646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 68238646013SJiri Slaby { 68338646013SJiri Slaby struct file *exe_file; 68438646013SJiri Slaby 6852dd8ad81SKonstantin Khlebnikov /* We need mmap_sem to protect against races with removal of exe_file */ 68638646013SJiri Slaby down_read(&mm->mmap_sem); 68738646013SJiri Slaby exe_file = mm->exe_file; 68838646013SJiri Slaby if (exe_file) 68938646013SJiri Slaby get_file(exe_file); 69038646013SJiri Slaby up_read(&mm->mmap_sem); 69138646013SJiri Slaby return exe_file; 69238646013SJiri Slaby } 69338646013SJiri Slaby 69438646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 69538646013SJiri Slaby { 69638646013SJiri Slaby /* It's safe to write the exe_file pointer without exe_file_lock because 69738646013SJiri Slaby * this is called during fork when the task is not yet in /proc */ 69838646013SJiri Slaby newmm->exe_file = get_mm_exe_file(oldmm); 69938646013SJiri Slaby } 70038646013SJiri Slaby 7011da177e4SLinus Torvalds /** 7021da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 7031da177e4SLinus Torvalds * 704246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 7051da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 7061da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 7071da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 7081da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 7091da177e4SLinus Torvalds */ 7101da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 7111da177e4SLinus Torvalds { 7121da177e4SLinus Torvalds struct mm_struct *mm; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds task_lock(task); 7151da177e4SLinus Torvalds mm = task->mm; 7161da177e4SLinus Torvalds if (mm) { 717246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 7181da177e4SLinus Torvalds mm = NULL; 7191da177e4SLinus Torvalds else 7201da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds task_unlock(task); 7231da177e4SLinus Torvalds return mm; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 7261da177e4SLinus Torvalds 7278cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 7288cdb878dSChristopher Yeoh { 7298cdb878dSChristopher Yeoh struct mm_struct *mm; 7308cdb878dSChristopher Yeoh int err; 7318cdb878dSChristopher Yeoh 7328cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 7338cdb878dSChristopher Yeoh if (err) 7348cdb878dSChristopher Yeoh return ERR_PTR(err); 7358cdb878dSChristopher Yeoh 7368cdb878dSChristopher Yeoh mm = get_task_mm(task); 7378cdb878dSChristopher Yeoh if (mm && mm != current->mm && 7388cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 7398cdb878dSChristopher Yeoh mmput(mm); 7408cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 7418cdb878dSChristopher Yeoh } 7428cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 7438cdb878dSChristopher Yeoh 7448cdb878dSChristopher Yeoh return mm; 7458cdb878dSChristopher Yeoh } 7468cdb878dSChristopher Yeoh 74757b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 748c415c3b4SOleg Nesterov { 749d68b46feSOleg Nesterov struct completion *vfork; 750c415c3b4SOleg Nesterov 751d68b46feSOleg Nesterov task_lock(tsk); 752d68b46feSOleg Nesterov vfork = tsk->vfork_done; 753d68b46feSOleg Nesterov if (likely(vfork)) { 754c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 755d68b46feSOleg Nesterov complete(vfork); 756d68b46feSOleg Nesterov } 757d68b46feSOleg Nesterov task_unlock(tsk); 758d68b46feSOleg Nesterov } 759d68b46feSOleg Nesterov 760d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 761d68b46feSOleg Nesterov struct completion *vfork) 762d68b46feSOleg Nesterov { 763d68b46feSOleg Nesterov int killed; 764d68b46feSOleg Nesterov 765d68b46feSOleg Nesterov freezer_do_not_count(); 766d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 767d68b46feSOleg Nesterov freezer_count(); 768d68b46feSOleg Nesterov 769d68b46feSOleg Nesterov if (killed) { 770d68b46feSOleg Nesterov task_lock(child); 771d68b46feSOleg Nesterov child->vfork_done = NULL; 772d68b46feSOleg Nesterov task_unlock(child); 773d68b46feSOleg Nesterov } 774d68b46feSOleg Nesterov 775d68b46feSOleg Nesterov put_task_struct(child); 776d68b46feSOleg Nesterov return killed; 777c415c3b4SOleg Nesterov } 778c415c3b4SOleg Nesterov 7791da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 7801da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 7811da177e4SLinus Torvalds * error success whatever. 7821da177e4SLinus Torvalds * 7831da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 7841da177e4SLinus Torvalds * from the current process. 7851da177e4SLinus Torvalds * 7861da177e4SLinus Torvalds * This difference is important for error handling, when we 7871da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 7881da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 7891da177e4SLinus Torvalds * restoring the old one. . . 7901da177e4SLinus Torvalds * Eric Biederman 10 January 1998 7911da177e4SLinus Torvalds */ 7921da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 7931da177e4SLinus Torvalds { 7948141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 7958141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 796fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 7978141c7f3SLinus Torvalds exit_robust_list(tsk); 798fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 799fc6b177dSPeter Zijlstra } 8008141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 801fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 8028141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 803fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 804fc6b177dSPeter Zijlstra } 8058141c7f3SLinus Torvalds #endif 806322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 807322a2c10SThomas Gleixner exit_pi_state_list(tsk); 8088141c7f3SLinus Torvalds #endif 8098141c7f3SLinus Torvalds 8100326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 8110326f5a9SSrikar Dronamraju 8121da177e4SLinus Torvalds /* Get rid of any cached register state */ 8131da177e4SLinus Torvalds deactivate_mm(tsk, mm); 8141da177e4SLinus Torvalds 815fec1d011SRoland McGrath /* 816fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 817fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 818fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 819d68b46feSOleg Nesterov * trouble, say, a killed vfork parent shouldn't touch this mm. 820d68b46feSOleg Nesterov * Userland only wants this done for a sys_exit. 821fec1d011SRoland McGrath */ 8229c8a8228SEric Dumazet if (tsk->clear_child_tid) { 8239c8a8228SEric Dumazet if (!(tsk->flags & PF_SIGNALED) && 8249c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 8251da177e4SLinus Torvalds /* 8261da177e4SLinus Torvalds * We don't check the error code - if userspace has 8271da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 8281da177e4SLinus Torvalds */ 8299c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 8309c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 8319c8a8228SEric Dumazet 1, NULL, NULL, 0); 8329c8a8228SEric Dumazet } 8339c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 8341da177e4SLinus Torvalds } 835f7505d64SKonstantin Khlebnikov 836f7505d64SKonstantin Khlebnikov /* 837f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 838f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 839f7505d64SKonstantin Khlebnikov */ 840f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 841f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 844a0a7ec30SJANAK DESAI /* 845a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 846a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 847a0a7ec30SJANAK DESAI */ 848ff252c1fSDaeSeok Youn static struct mm_struct *dup_mm(struct task_struct *tsk) 849a0a7ec30SJANAK DESAI { 850a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 851a0a7ec30SJANAK DESAI int err; 852a0a7ec30SJANAK DESAI 853a0a7ec30SJANAK DESAI mm = allocate_mm(); 854a0a7ec30SJANAK DESAI if (!mm) 855a0a7ec30SJANAK DESAI goto fail_nomem; 856a0a7ec30SJANAK DESAI 857a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 858a0a7ec30SJANAK DESAI 85978fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 860a0a7ec30SJANAK DESAI goto fail_nomem; 861a0a7ec30SJANAK DESAI 862925d1c40SMatt Helsley dup_mm_exe_file(oldmm, mm); 863925d1c40SMatt Helsley 864a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 865a0a7ec30SJANAK DESAI if (err) 866a0a7ec30SJANAK DESAI goto free_pt; 867a0a7ec30SJANAK DESAI 868a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 869a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 870a0a7ec30SJANAK DESAI 871801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 872801460d0SHiroshi Shimamoto goto free_pt; 873801460d0SHiroshi Shimamoto 874a0a7ec30SJANAK DESAI return mm; 875a0a7ec30SJANAK DESAI 876a0a7ec30SJANAK DESAI free_pt: 877801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 878801460d0SHiroshi Shimamoto mm->binfmt = NULL; 879a0a7ec30SJANAK DESAI mmput(mm); 880a0a7ec30SJANAK DESAI 881a0a7ec30SJANAK DESAI fail_nomem: 882a0a7ec30SJANAK DESAI return NULL; 883a0a7ec30SJANAK DESAI } 884a0a7ec30SJANAK DESAI 8851da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 8861da177e4SLinus Torvalds { 8871da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 8881da177e4SLinus Torvalds int retval; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 8911da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 89217406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 89317406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 89417406b82SMandeep Singh Baines #endif 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds tsk->mm = NULL; 8971da177e4SLinus Torvalds tsk->active_mm = NULL; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* 9001da177e4SLinus Torvalds * Are we cloning a kernel thread? 9011da177e4SLinus Torvalds * 9021da177e4SLinus Torvalds * We need to steal a active VM for that.. 9031da177e4SLinus Torvalds */ 9041da177e4SLinus Torvalds oldmm = current->mm; 9051da177e4SLinus Torvalds if (!oldmm) 9061da177e4SLinus Torvalds return 0; 9071da177e4SLinus Torvalds 908615d6e87SDavidlohr Bueso /* initialize the new vmacache entries */ 909615d6e87SDavidlohr Bueso vmacache_flush(tsk); 910615d6e87SDavidlohr Bueso 9111da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 9121da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 9131da177e4SLinus Torvalds mm = oldmm; 9141da177e4SLinus Torvalds goto good_mm; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds retval = -ENOMEM; 918a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 9191da177e4SLinus Torvalds if (!mm) 9201da177e4SLinus Torvalds goto fail_nomem; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds good_mm: 9231da177e4SLinus Torvalds tsk->mm = mm; 9241da177e4SLinus Torvalds tsk->active_mm = mm; 9251da177e4SLinus Torvalds return 0; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds fail_nomem: 9281da177e4SLinus Torvalds return retval; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 931a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 9321da177e4SLinus Torvalds { 933498052bbSAl Viro struct fs_struct *fs = current->fs; 9341da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 935498052bbSAl Viro /* tsk->fs is already what we want */ 9362a4419b5SNick Piggin spin_lock(&fs->lock); 937498052bbSAl Viro if (fs->in_exec) { 9382a4419b5SNick Piggin spin_unlock(&fs->lock); 939498052bbSAl Viro return -EAGAIN; 940498052bbSAl Viro } 941498052bbSAl Viro fs->users++; 9422a4419b5SNick Piggin spin_unlock(&fs->lock); 9431da177e4SLinus Torvalds return 0; 9441da177e4SLinus Torvalds } 945498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 9461da177e4SLinus Torvalds if (!tsk->fs) 9471da177e4SLinus Torvalds return -ENOMEM; 9481da177e4SLinus Torvalds return 0; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 951a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 952a016f338SJANAK DESAI { 953a016f338SJANAK DESAI struct files_struct *oldf, *newf; 954a016f338SJANAK DESAI int error = 0; 955a016f338SJANAK DESAI 956a016f338SJANAK DESAI /* 957a016f338SJANAK DESAI * A background process may not have any files ... 958a016f338SJANAK DESAI */ 959a016f338SJANAK DESAI oldf = current->files; 960a016f338SJANAK DESAI if (!oldf) 961a016f338SJANAK DESAI goto out; 962a016f338SJANAK DESAI 963a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 964a016f338SJANAK DESAI atomic_inc(&oldf->count); 965a016f338SJANAK DESAI goto out; 966a016f338SJANAK DESAI } 967a016f338SJANAK DESAI 968a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 969a016f338SJANAK DESAI if (!newf) 970a016f338SJANAK DESAI goto out; 971a016f338SJANAK DESAI 972a016f338SJANAK DESAI tsk->files = newf; 973a016f338SJANAK DESAI error = 0; 974a016f338SJANAK DESAI out: 975a016f338SJANAK DESAI return error; 976a016f338SJANAK DESAI } 977a016f338SJANAK DESAI 978fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 979fd0928dfSJens Axboe { 980fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 981fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 9826e736be7STejun Heo struct io_context *new_ioc; 983fd0928dfSJens Axboe 984fd0928dfSJens Axboe if (!ioc) 985fd0928dfSJens Axboe return 0; 986fadad878SJens Axboe /* 987fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 988fadad878SJens Axboe */ 989fadad878SJens Axboe if (clone_flags & CLONE_IO) { 9903d48749dSTejun Heo ioc_task_link(ioc); 9913d48749dSTejun Heo tsk->io_context = ioc; 992fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 9936e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 9946e736be7STejun Heo if (unlikely(!new_ioc)) 995fd0928dfSJens Axboe return -ENOMEM; 996fd0928dfSJens Axboe 9976e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 99811a3122fSTejun Heo put_io_context(new_ioc); 999fd0928dfSJens Axboe } 1000fd0928dfSJens Axboe #endif 1001fd0928dfSJens Axboe return 0; 1002fd0928dfSJens Axboe } 1003fd0928dfSJens Axboe 1004a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 10051da177e4SLinus Torvalds { 10061da177e4SLinus Torvalds struct sighand_struct *sig; 10071da177e4SLinus Torvalds 100860348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 10091da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 10101da177e4SLinus Torvalds return 0; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 1013e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 10141da177e4SLinus Torvalds if (!sig) 10151da177e4SLinus Torvalds return -ENOMEM; 10161da177e4SLinus Torvalds atomic_set(&sig->count, 1); 10171da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 10181da177e4SLinus Torvalds return 0; 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds 1021a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1022c81addc9SOleg Nesterov { 1023d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 1024d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1025*392809b2SOleg Nesterov /* 1026*392809b2SOleg Nesterov * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it 1027*392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1028*392809b2SOleg Nesterov */ 1029c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1030c81addc9SOleg Nesterov } 1031d80e731eSOleg Nesterov } 1032c81addc9SOleg Nesterov 1033f06febc9SFrank Mayhar /* 1034f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1035f06febc9SFrank Mayhar */ 1036f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1037f06febc9SFrank Mayhar { 103878d7d407SJiri Slaby unsigned long cpu_limit; 103978d7d407SJiri Slaby 1040f06febc9SFrank Mayhar /* Thread group counters. */ 1041f06febc9SFrank Mayhar thread_group_cputime_init(sig); 1042f06febc9SFrank Mayhar 104378d7d407SJiri Slaby cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 104478d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 104578d7d407SJiri Slaby sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); 10466279a751SOleg Nesterov sig->cputimer.running = 1; 10476279a751SOleg Nesterov } 10486279a751SOleg Nesterov 1049f06febc9SFrank Mayhar /* The timer lists. */ 1050f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1051f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1052f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1053f06febc9SFrank Mayhar } 1054f06febc9SFrank Mayhar 1055a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 10561da177e4SLinus Torvalds { 10571da177e4SLinus Torvalds struct signal_struct *sig; 10581da177e4SLinus Torvalds 10594ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1060490dea45SPeter Zijlstra return 0; 10616279a751SOleg Nesterov 1062a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 10631da177e4SLinus Torvalds tsk->signal = sig; 10641da177e4SLinus Torvalds if (!sig) 10651da177e4SLinus Torvalds return -ENOMEM; 10661da177e4SLinus Torvalds 1067b3ac022cSOleg Nesterov sig->nr_threads = 1; 10681da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1069b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 10700c740d0aSOleg Nesterov 10710c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 10720c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 10730c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 10740c740d0aSOleg Nesterov 10751da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1076db51aeccSOleg Nesterov sig->curr_target = tsk; 10771da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 10781da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 1079e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 10801da177e4SLinus Torvalds 1081c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 10821da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds task_lock(current->group_leader); 10851da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 10861da177e4SLinus Torvalds task_unlock(current->group_leader); 10871da177e4SLinus Torvalds 10886279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 10896279a751SOleg Nesterov 1090522ed776SMiloslav Trmac tty_audit_fork(sig); 10915091faa4SMike Galbraith sched_autogroup_fork(sig); 1092522ed776SMiloslav Trmac 10934714d1d3SBen Blum #ifdef CONFIG_CGROUPS 1094257058aeSTejun Heo init_rwsem(&sig->group_rwsem); 10954714d1d3SBen Blum #endif 10964714d1d3SBen Blum 1097a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1098dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 109928b83c51SKOSAKI Motohiro 1100ebec18a6SLennart Poettering sig->has_child_subreaper = current->signal->has_child_subreaper || 1101ebec18a6SLennart Poettering current->signal->is_child_subreaper; 1102ebec18a6SLennart Poettering 11039b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 11049b1bf12dSKOSAKI Motohiro 11051da177e4SLinus Torvalds return 0; 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 1108dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1109dbd95212SKees Cook { 1110dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1111dbd95212SKees Cook /* 1112dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1113dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1114dbd95212SKees Cook * needed because this new task is not yet running and cannot 1115dbd95212SKees Cook * be racing exec. 1116dbd95212SKees Cook */ 111769f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1118dbd95212SKees Cook 1119dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1120dbd95212SKees Cook get_seccomp_filter(current); 1121dbd95212SKees Cook p->seccomp = current->seccomp; 1122dbd95212SKees Cook 1123dbd95212SKees Cook /* 1124dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1125dbd95212SKees Cook * between the task_struct being duplicated and holding the 1126dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1127dbd95212SKees Cook */ 1128dbd95212SKees Cook if (task_no_new_privs(current)) 1129dbd95212SKees Cook task_set_no_new_privs(p); 1130dbd95212SKees Cook 1131dbd95212SKees Cook /* 1132dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1133dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1134dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1135dbd95212SKees Cook */ 1136dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1137dbd95212SKees Cook set_tsk_thread_flag(p, TIF_SECCOMP); 1138dbd95212SKees Cook #endif 1139dbd95212SKees Cook } 1140dbd95212SKees Cook 114117da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 11421da177e4SLinus Torvalds { 11431da177e4SLinus Torvalds current->clear_child_tid = tidptr; 11441da177e4SLinus Torvalds 1145b488893aSPavel Emelyanov return task_pid_vnr(current); 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 1148a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 114923f78d4aSIngo Molnar { 11501d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1151e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1152fb00aca4SPeter Zijlstra p->pi_waiters = RB_ROOT; 1153fb00aca4SPeter Zijlstra p->pi_waiters_leftmost = NULL; 115423f78d4aSIngo Molnar p->pi_blocked_on = NULL; 115523f78d4aSIngo Molnar #endif 115623f78d4aSIngo Molnar } 115723f78d4aSIngo Molnar 11581da177e4SLinus Torvalds /* 1159f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1160f06febc9SFrank Mayhar */ 1161f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1162f06febc9SFrank Mayhar { 116364861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 116464861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1165f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1166f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1167f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1168f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1169f06febc9SFrank Mayhar } 1170f06febc9SFrank Mayhar 117181907739SOleg Nesterov static inline void 117281907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 117381907739SOleg Nesterov { 117481907739SOleg Nesterov task->pids[type].pid = pid; 117581907739SOleg Nesterov } 117681907739SOleg Nesterov 1177f06febc9SFrank Mayhar /* 11781da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 11791da177e4SLinus Torvalds * but does not actually start it yet. 11801da177e4SLinus Torvalds * 11811da177e4SLinus Torvalds * It copies the registers, and all the appropriate 11821da177e4SLinus Torvalds * parts of the process environment (as per the clone 11831da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 11841da177e4SLinus Torvalds */ 118536c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 11861da177e4SLinus Torvalds unsigned long stack_start, 11871da177e4SLinus Torvalds unsigned long stack_size, 11881da177e4SLinus Torvalds int __user *child_tidptr, 118909a05394SRoland McGrath struct pid *pid, 119009a05394SRoland McGrath int trace) 11911da177e4SLinus Torvalds { 11921da177e4SLinus Torvalds int retval; 1193a24efe62SMariusz Kozlowski struct task_struct *p; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 11961da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11971da177e4SLinus Torvalds 1198e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1199e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1200e66eded8SEric W. Biederman 12011da177e4SLinus Torvalds /* 12021da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 12031da177e4SLinus Torvalds * can only be started up within the thread group. 12041da177e4SLinus Torvalds */ 12051da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 12061da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds /* 12091da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 12101da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 12111da177e4SLinus Torvalds * for various simplifications in other code. 12121da177e4SLinus Torvalds */ 12131da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 12141da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 12151da177e4SLinus Torvalds 1216123be07bSSukadev Bhattiprolu /* 1217123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1218123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1219123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1220123be07bSSukadev Bhattiprolu * from creating siblings. 1221123be07bSSukadev Bhattiprolu */ 1222123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1223123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1224123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1225123be07bSSukadev Bhattiprolu 12268382fcacSEric W. Biederman /* 122740a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 122840a0d32dSOleg Nesterov * do not allow it to share a thread group or signal handlers or 122940a0d32dSOleg Nesterov * parent with the forking task. 12308382fcacSEric W. Biederman */ 12311f7f4ddeSEric W. Biederman if (clone_flags & CLONE_SIGHAND) { 123240a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 123340a0d32dSOleg Nesterov (task_active_pid_ns(current) != 1234c2b1df2eSAndy Lutomirski current->nsproxy->pid_ns_for_children)) 12358382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 123640a0d32dSOleg Nesterov } 12378382fcacSEric W. Biederman 12381da177e4SLinus Torvalds retval = security_task_create(clone_flags); 12391da177e4SLinus Torvalds if (retval) 12401da177e4SLinus Torvalds goto fork_out; 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds retval = -ENOMEM; 12431da177e4SLinus Torvalds p = dup_task_struct(current); 12441da177e4SLinus Torvalds if (!p) 12451da177e4SLinus Torvalds goto fork_out; 12461da177e4SLinus Torvalds 1247f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1248f7e8b616SSteven Rostedt 1249bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1250bea493a0SPeter Zijlstra 1251d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1252de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1253de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1254de30a2b3SIngo Molnar #endif 12551da177e4SLinus Torvalds retval = -EAGAIN; 12563b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 125778d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1258b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1259b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 12601da177e4SLinus Torvalds goto bad_fork_free; 12611da177e4SLinus Torvalds } 126272fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 12631da177e4SLinus Torvalds 1264f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1265f1752eecSDavid Howells if (retval < 0) 1266f1752eecSDavid Howells goto bad_fork_free; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds /* 12691da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 12701da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 12711da177e4SLinus Torvalds * to stop root fork bombs. 12721da177e4SLinus Torvalds */ 127304ec93feSLi Zefan retval = -EAGAIN; 12741da177e4SLinus Torvalds if (nr_threads >= max_threads) 12751da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12761da177e4SLinus Torvalds 1277a1261f54SAl Viro if (!try_module_get(task_thread_info(p)->exec_domain->module)) 12781da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12791da177e4SLinus Torvalds 1280ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1281514ddb44SDavid Rientjes p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER); 1282514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 12831da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 12841da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1285f41d911fSPaul E. McKenney rcu_copy_process(p); 12861da177e4SLinus Torvalds p->vfork_done = NULL; 12871da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds init_sigpending(&p->pending); 12901da177e4SLinus Torvalds 129164861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 129264861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 12939fbc42eaSFrederic Weisbecker #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 1294d37f761dSFrederic Weisbecker p->prev_cputime.utime = p->prev_cputime.stime = 0; 1295d99ca3b9SHidetoshi Seto #endif 12966a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 12976a61671bSFrederic Weisbecker seqlock_init(&p->vtime_seqlock); 12986a61671bSFrederic Weisbecker p->vtime_snap = 0; 12996a61671bSFrederic Weisbecker p->vtime_snap_whence = VTIME_SLEEPING; 13006a61671bSFrederic Weisbecker #endif 13016a61671bSFrederic Weisbecker 1302a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1303a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1304a3a2e76cSKAMEZAWA Hiroyuki #endif 1305172ba844SBalbir Singh 13066976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 13076976675dSArjan van de Ven 13085995477aSAndrea Righi task_io_accounting_init(&p->ioac); 13091da177e4SLinus Torvalds acct_clear_integrals(p); 13101da177e4SLinus Torvalds 1311f06febc9SFrank Mayhar posix_cpu_timers_init(p); 13121da177e4SLinus Torvalds 1313ccbf62d8SThomas Gleixner p->start_time = ktime_get_ns(); 131457e0be04SThomas Gleixner p->real_start_time = ktime_get_boot_ns(); 13151da177e4SLinus Torvalds p->io_context = NULL; 13161da177e4SLinus Torvalds p->audit_context = NULL; 13174714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1318257058aeSTejun Heo threadgroup_change_begin(current); 1319b4f48b63SPaul Menage cgroup_fork(p); 13201da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1321846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 13221da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 13231da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 13241da177e4SLinus Torvalds p->mempolicy = NULL; 1325e8604cb4SLi Zefan goto bad_fork_cleanup_threadgroup_lock; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds #endif 1328778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1329778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1330778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1331cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1332778d3b0fSMichal Hocko #endif 1333de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1334de30a2b3SIngo Molnar p->irq_events = 0; 1335de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1336de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1337de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1338de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1339de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1340de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1341de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1342de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1343de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1344de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1345de30a2b3SIngo Molnar p->hardirq_context = 0; 1346de30a2b3SIngo Molnar p->softirq_context = 0; 1347de30a2b3SIngo Molnar #endif 1348fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1349fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1350fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1351fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1352fbb9ce95SIngo Molnar #endif 13531da177e4SLinus Torvalds 1354408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1355408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1356408894eeSIngo Molnar #endif 1357cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 1358cafe5635SKent Overstreet p->sequential_io = 0; 1359cafe5635SKent Overstreet p->sequential_io_avg = 0; 1360cafe5635SKent Overstreet #endif 13610f481406SMarkus Metzger 13623c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 1363aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 1364aab03e05SDario Faggioli if (retval) 1365aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 13666ab423e0SPeter Zijlstra 1367cdd6c482SIngo Molnar retval = perf_event_init_task(p); 13686ab423e0SPeter Zijlstra if (retval) 13696ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1370fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1371fb0a685cSDaniel Rebelo de Oliveira if (retval) 13726c72e350SPeter Zijlstra goto bad_fork_cleanup_perf; 13731da177e4SLinus Torvalds /* copy all the process information */ 1374ab602f79SJack Miller shm_init_task(p); 1375fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1376fb0a685cSDaniel Rebelo de Oliveira if (retval) 13771da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1378fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1379fb0a685cSDaniel Rebelo de Oliveira if (retval) 13801da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1381fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1382fb0a685cSDaniel Rebelo de Oliveira if (retval) 13831da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1384fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1385fb0a685cSDaniel Rebelo de Oliveira if (retval) 13861da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1387fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1388fb0a685cSDaniel Rebelo de Oliveira if (retval) 13891da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1390fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1391fb0a685cSDaniel Rebelo de Oliveira if (retval) 13921da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1393fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1394fb0a685cSDaniel Rebelo de Oliveira if (retval) 1395d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1396fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1397fb0a685cSDaniel Rebelo de Oliveira if (retval) 1398fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 1399afa86fc4SAl Viro retval = copy_thread(clone_flags, stack_start, stack_size, p); 14001da177e4SLinus Torvalds if (retval) 1401fd0928dfSJens Axboe goto bad_fork_cleanup_io; 14021da177e4SLinus Torvalds 1403425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1404425fb2b4SPavel Emelyanov retval = -ENOMEM; 1405c2b1df2eSAndy Lutomirski pid = alloc_pid(p->nsproxy->pid_ns_for_children); 1406425fb2b4SPavel Emelyanov if (!pid) 1407fd0928dfSJens Axboe goto bad_fork_cleanup_io; 1408425fb2b4SPavel Emelyanov } 1409425fb2b4SPavel Emelyanov 14101da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 14111da177e4SLinus Torvalds /* 14121da177e4SLinus Torvalds * Clear TID on mm_release()? 14131da177e4SLinus Torvalds */ 14141da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 141573c10101SJens Axboe #ifdef CONFIG_BLOCK 141673c10101SJens Axboe p->plug = NULL; 141773c10101SJens Axboe #endif 141842b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 14198f17d3a5SIngo Molnar p->robust_list = NULL; 14208f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 14218f17d3a5SIngo Molnar p->compat_robust_list = NULL; 14228f17d3a5SIngo Molnar #endif 1423c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1424c87e2837SIngo Molnar p->pi_state_cache = NULL; 142542b2dd0aSAlexey Dobriyan #endif 14261da177e4SLinus Torvalds /* 1427f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1428f9a3879aSGOTO Masanori */ 1429f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 1430f9a3879aSGOTO Masanori p->sas_ss_sp = p->sas_ss_size = 0; 1431f9a3879aSGOTO Masanori 1432f9a3879aSGOTO Masanori /* 14336580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 14346580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 14351da177e4SLinus Torvalds */ 14366580807dSOleg Nesterov user_disable_single_step(p); 14371da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1438ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1439ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1440ed75e8d5SLaurent Vivier #endif 14419745512cSArjan van de Ven clear_all_latency_tracing(p); 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /* ok, now we should be set up.. */ 144418c830dfSOleg Nesterov p->pid = pid_nr(pid); 144518c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 14465f8aadd8SOleg Nesterov p->exit_signal = -1; 144718c830dfSOleg Nesterov p->group_leader = current->group_leader; 144818c830dfSOleg Nesterov p->tgid = current->tgid; 144918c830dfSOleg Nesterov } else { 145018c830dfSOleg Nesterov if (clone_flags & CLONE_PARENT) 14515f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 14525f8aadd8SOleg Nesterov else 14535f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 145418c830dfSOleg Nesterov p->group_leader = p; 145518c830dfSOleg Nesterov p->tgid = p->pid; 145618c830dfSOleg Nesterov } 14575f8aadd8SOleg Nesterov 14589d823e8fSWu Fengguang p->nr_dirtied = 0; 14599d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 146083712358SWu Fengguang p->dirty_paused_when = 0; 14619d823e8fSWu Fengguang 1462bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 146347e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 1464158e1645SAl Viro p->task_works = NULL; 14651da177e4SLinus Torvalds 146618c830dfSOleg Nesterov /* 146718c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 146818c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 146918c830dfSOleg Nesterov */ 14701da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 14732d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 14741da177e4SLinus Torvalds p->real_parent = current->real_parent; 14752d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 14762d5516cbSOleg Nesterov } else { 14771da177e4SLinus Torvalds p->real_parent = current; 14782d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 14792d5516cbSOleg Nesterov } 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 14824a2c7a78SOleg Nesterov 14834a2c7a78SOleg Nesterov /* 1484dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 1485dbd95212SKees Cook * before holding sighand lock. 1486dbd95212SKees Cook */ 1487dbd95212SKees Cook copy_seccomp(p); 1488dbd95212SKees Cook 1489dbd95212SKees Cook /* 14904a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 14914a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 14924a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 14934a2c7a78SOleg Nesterov * it's process group. 14944a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 14954a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 14964a2c7a78SOleg Nesterov */ 14974a2c7a78SOleg Nesterov recalc_sigpending(); 14984a2c7a78SOleg Nesterov if (signal_pending(current)) { 14994a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 15004a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 15014a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 1502f7e8b616SSteven Rostedt goto bad_fork_free_pid; 15034a2c7a78SOleg Nesterov } 15044a2c7a78SOleg Nesterov 150573b9ebfeSOleg Nesterov if (likely(p->pid)) { 15064b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 15071da177e4SLinus Torvalds 150881907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 15091da177e4SLinus Torvalds if (thread_group_leader(p)) { 151081907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 151181907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 151281907739SOleg Nesterov 15131c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 151417cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 15151c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 15161c4042c2SEric W. Biederman } 15175cd17569SEric W. Biederman 1518fea9d175SOleg Nesterov p->signal->leader_pid = pid; 15199c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 15209cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 15215e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 152281907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 152381907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 1524909ea964SChristoph Lameter __this_cpu_inc(process_counts); 152580628ca0SOleg Nesterov } else { 152680628ca0SOleg Nesterov current->signal->nr_threads++; 152780628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 152880628ca0SOleg Nesterov atomic_inc(¤t->signal->sigcnt); 152980628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 153080628ca0SOleg Nesterov &p->group_leader->thread_group); 15310c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 15320c740d0aSOleg Nesterov &p->signal->thread_head); 15331da177e4SLinus Torvalds } 153481907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 15351da177e4SLinus Torvalds nr_threads++; 153673b9ebfeSOleg Nesterov } 153773b9ebfeSOleg Nesterov 15381da177e4SLinus Torvalds total_forks++; 15393f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 15404af4206bSOleg Nesterov syscall_tracepoint_update(p); 15411da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 15424af4206bSOleg Nesterov 1543c13cf856SAndrew Morton proc_fork_connector(p); 1544817929ecSPaul Menage cgroup_post_fork(p); 15454714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1546257058aeSTejun Heo threadgroup_change_end(current); 1547cdd6c482SIngo Molnar perf_event_fork(p); 154843d2b113SKAMEZAWA Hiroyuki 154943d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 15503ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 155143d2b113SKAMEZAWA Hiroyuki 15521da177e4SLinus Torvalds return p; 15531da177e4SLinus Torvalds 1554425fb2b4SPavel Emelyanov bad_fork_free_pid: 1555425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1556425fb2b4SPavel Emelyanov free_pid(pid); 1557fd0928dfSJens Axboe bad_fork_cleanup_io: 1558b69f2292SLouis Rilling if (p->io_context) 1559b69f2292SLouis Rilling exit_io_context(p); 1560ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1561444f378bSLinus Torvalds exit_task_namespaces(p); 15621da177e4SLinus Torvalds bad_fork_cleanup_mm: 1563c9f01245SDavid Rientjes if (p->mm) 15641da177e4SLinus Torvalds mmput(p->mm); 15651da177e4SLinus Torvalds bad_fork_cleanup_signal: 15664ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 15671c5354deSMike Galbraith free_signal_struct(p->signal); 15681da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1569a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 15701da177e4SLinus Torvalds bad_fork_cleanup_fs: 15711da177e4SLinus Torvalds exit_fs(p); /* blocking */ 15721da177e4SLinus Torvalds bad_fork_cleanup_files: 15731da177e4SLinus Torvalds exit_files(p); /* blocking */ 15741da177e4SLinus Torvalds bad_fork_cleanup_semundo: 15751da177e4SLinus Torvalds exit_sem(p); 15761da177e4SLinus Torvalds bad_fork_cleanup_audit: 15771da177e4SLinus Torvalds audit_free(p); 15786c72e350SPeter Zijlstra bad_fork_cleanup_perf: 1579cdd6c482SIngo Molnar perf_event_free_task(p); 15806c72e350SPeter Zijlstra bad_fork_cleanup_policy: 15811da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1582f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1583e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock: 15841da177e4SLinus Torvalds #endif 15854714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1586257058aeSTejun Heo threadgroup_change_end(current); 158735df17c5SShailabh Nagar delayacct_tsk_free(p); 1588a1261f54SAl Viro module_put(task_thread_info(p)->exec_domain->module); 15891da177e4SLinus Torvalds bad_fork_cleanup_count: 1590d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1591e0e81739SDavid Howells exit_creds(p); 15921da177e4SLinus Torvalds bad_fork_free: 15931da177e4SLinus Torvalds free_task(p); 1594fe7d37d1SOleg Nesterov fork_out: 1595fe7d37d1SOleg Nesterov return ERR_PTR(retval); 15961da177e4SLinus Torvalds } 15971da177e4SLinus Torvalds 1598f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1599f106eee1SOleg Nesterov { 1600f106eee1SOleg Nesterov enum pid_type type; 1601f106eee1SOleg Nesterov 1602f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1603f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1604f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1605f106eee1SOleg Nesterov } 1606f106eee1SOleg Nesterov } 1607f106eee1SOleg Nesterov 16080db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 16091da177e4SLinus Torvalds { 161036c8b586SIngo Molnar struct task_struct *task; 161162e791c1SAl Viro task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0); 1612f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1613f106eee1SOleg Nesterov init_idle_pids(task->pids); 16141da177e4SLinus Torvalds init_idle(task, cpu); 1615f106eee1SOleg Nesterov } 161673b9ebfeSOleg Nesterov 16171da177e4SLinus Torvalds return task; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds /* 16211da177e4SLinus Torvalds * Ok, this is the main fork-routine. 16221da177e4SLinus Torvalds * 16231da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 16241da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 16251da177e4SLinus Torvalds */ 16261da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 16271da177e4SLinus Torvalds unsigned long stack_start, 16281da177e4SLinus Torvalds unsigned long stack_size, 16291da177e4SLinus Torvalds int __user *parent_tidptr, 16301da177e4SLinus Torvalds int __user *child_tidptr) 16311da177e4SLinus Torvalds { 16321da177e4SLinus Torvalds struct task_struct *p; 16331da177e4SLinus Torvalds int trace = 0; 163492476d7fSEric W. Biederman long nr; 16351da177e4SLinus Torvalds 1636bdff746aSAndrew Morton /* 16374b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 16384b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 16394b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 16404b9d33e6STejun Heo * for the type of forking is enabled. 164109a05394SRoland McGrath */ 1642e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 16434b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 16444b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 16454b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 16464b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 16474b9d33e6STejun Heo else 16484b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 16494b9d33e6STejun Heo 16504b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 16514b9d33e6STejun Heo trace = 0; 16524b9d33e6STejun Heo } 16531da177e4SLinus Torvalds 165462e791c1SAl Viro p = copy_process(clone_flags, stack_start, stack_size, 165509a05394SRoland McGrath child_tidptr, NULL, trace); 16561da177e4SLinus Torvalds /* 16571da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 16581da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 16591da177e4SLinus Torvalds */ 16601da177e4SLinus Torvalds if (!IS_ERR(p)) { 16611da177e4SLinus Torvalds struct completion vfork; 16624e52365fSMatthew Dempsky struct pid *pid; 16631da177e4SLinus Torvalds 16640a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 16650a16b607SMathieu Desnoyers 16664e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 16674e52365fSMatthew Dempsky nr = pid_vnr(pid); 166830e49c26SPavel Emelyanov 166930e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 167030e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1671a6f5e063SSukadev Bhattiprolu 16721da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 16731da177e4SLinus Torvalds p->vfork_done = &vfork; 16741da177e4SLinus Torvalds init_completion(&vfork); 1675d68b46feSOleg Nesterov get_task_struct(p); 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 16783e51e3edSSamir Bellabes wake_up_new_task(p); 16791da177e4SLinus Torvalds 16804b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 16814b9d33e6STejun Heo if (unlikely(trace)) 16824e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 168309a05394SRoland McGrath 16841da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1685d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 16864e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 16879f59ce5dSChuck Ebbert } 16884e52365fSMatthew Dempsky 16894e52365fSMatthew Dempsky put_pid(pid); 16901da177e4SLinus Torvalds } else { 169192476d7fSEric W. Biederman nr = PTR_ERR(p); 16921da177e4SLinus Torvalds } 169392476d7fSEric W. Biederman return nr; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 16962aa3a7f8SAl Viro /* 16972aa3a7f8SAl Viro * Create a kernel thread. 16982aa3a7f8SAl Viro */ 16992aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 17002aa3a7f8SAl Viro { 1701e80d6661SAl Viro return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, 17022aa3a7f8SAl Viro (unsigned long)arg, NULL, NULL); 17032aa3a7f8SAl Viro } 17042aa3a7f8SAl Viro 1705d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 1706d2125043SAl Viro SYSCALL_DEFINE0(fork) 1707d2125043SAl Viro { 1708d2125043SAl Viro #ifdef CONFIG_MMU 1709e80d6661SAl Viro return do_fork(SIGCHLD, 0, 0, NULL, NULL); 1710d2125043SAl Viro #else 1711d2125043SAl Viro /* can not support in nommu mode */ 17125d59e182SDaeseok Youn return -EINVAL; 1713d2125043SAl Viro #endif 1714d2125043SAl Viro } 1715d2125043SAl Viro #endif 1716d2125043SAl Viro 1717d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 1718d2125043SAl Viro SYSCALL_DEFINE0(vfork) 1719d2125043SAl Viro { 1720e80d6661SAl Viro return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 1721d2125043SAl Viro 0, NULL, NULL); 1722d2125043SAl Viro } 1723d2125043SAl Viro #endif 1724d2125043SAl Viro 1725d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 1726d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 1727d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1728d2125043SAl Viro int __user *, parent_tidptr, 1729d2125043SAl Viro int, tls_val, 1730d2125043SAl Viro int __user *, child_tidptr) 1731d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 1732d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 1733d2125043SAl Viro int __user *, parent_tidptr, 1734d2125043SAl Viro int __user *, child_tidptr, 1735d2125043SAl Viro int, tls_val) 1736dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 1737dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 1738dfa9771aSMichal Simek int, stack_size, 1739dfa9771aSMichal Simek int __user *, parent_tidptr, 1740dfa9771aSMichal Simek int __user *, child_tidptr, 1741dfa9771aSMichal Simek int, tls_val) 1742d2125043SAl Viro #else 1743d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1744d2125043SAl Viro int __user *, parent_tidptr, 1745d2125043SAl Viro int __user *, child_tidptr, 1746d2125043SAl Viro int, tls_val) 1747d2125043SAl Viro #endif 1748d2125043SAl Viro { 17492cf09666SAl Viro return do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr); 1750d2125043SAl Viro } 1751d2125043SAl Viro #endif 1752d2125043SAl Viro 17535fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 17545fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 17555fd63b30SRavikiran G Thirumalai #endif 17565fd63b30SRavikiran G Thirumalai 175751cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 1758aa1757f9SOleg Nesterov { 1759aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1760aa1757f9SOleg Nesterov 1761aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1762b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 1763fba2afaaSDavide Libenzi } 1764aa1757f9SOleg Nesterov 17651da177e4SLinus Torvalds void __init proc_caches_init(void) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 17681da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 17692dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 17702dff4405SVegard Nossum SLAB_NOTRACK, sighand_ctor); 17711da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 17721da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 17732dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17741da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 17751da177e4SLinus Torvalds sizeof(struct files_struct), 0, 17762dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17771da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 17781da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 17792dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 17806345d24dSLinus Torvalds /* 17816345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 17826345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 17836345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 17846345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 17856345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 17866345d24dSLinus Torvalds */ 17871da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 17885fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 17892dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 179033e5d769SDavid Howells vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC); 17918feae131SDavid Howells mmap_init(); 179266577193SAl Viro nsproxy_cache_init(); 17931da177e4SLinus Torvalds } 1794cf2e340fSJANAK DESAI 1795cf2e340fSJANAK DESAI /* 17969bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 1797cf2e340fSJANAK DESAI */ 17989bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 1799cf2e340fSJANAK DESAI { 18009bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 18019bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 180250804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 1803b2e0d987SEric W. Biederman CLONE_NEWUSER|CLONE_NEWPID)) 1804cf2e340fSJANAK DESAI return -EINVAL; 18059bfb23fcSOleg Nesterov /* 18069bfb23fcSOleg Nesterov * Not implemented, but pretend it works if there is nothing to 18079bfb23fcSOleg Nesterov * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND 18089bfb23fcSOleg Nesterov * needs to unshare vm. 18099bfb23fcSOleg Nesterov */ 18109bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 18119bfb23fcSOleg Nesterov /* FIXME: get_task_mm() increments ->mm_users */ 18129bfb23fcSOleg Nesterov if (atomic_read(¤t->mm->mm_users) > 1) 18139bfb23fcSOleg Nesterov return -EINVAL; 18149bfb23fcSOleg Nesterov } 1815cf2e340fSJANAK DESAI 1816cf2e340fSJANAK DESAI return 0; 1817cf2e340fSJANAK DESAI } 1818cf2e340fSJANAK DESAI 1819cf2e340fSJANAK DESAI /* 182099d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1821cf2e340fSJANAK DESAI */ 1822cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1823cf2e340fSJANAK DESAI { 1824cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1825cf2e340fSJANAK DESAI 1826498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 1827498052bbSAl Viro return 0; 1828498052bbSAl Viro 1829498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 1830498052bbSAl Viro if (fs->users == 1) 1831498052bbSAl Viro return 0; 1832498052bbSAl Viro 1833498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 183499d1419dSJANAK DESAI if (!*new_fsp) 183599d1419dSJANAK DESAI return -ENOMEM; 1836cf2e340fSJANAK DESAI 1837cf2e340fSJANAK DESAI return 0; 1838cf2e340fSJANAK DESAI } 1839cf2e340fSJANAK DESAI 1840cf2e340fSJANAK DESAI /* 1841a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1842cf2e340fSJANAK DESAI */ 1843cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1844cf2e340fSJANAK DESAI { 1845cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1846a016f338SJANAK DESAI int error = 0; 1847cf2e340fSJANAK DESAI 1848cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1849a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1850a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1851a016f338SJANAK DESAI if (!*new_fdp) 1852a016f338SJANAK DESAI return error; 1853a016f338SJANAK DESAI } 1854cf2e340fSJANAK DESAI 1855cf2e340fSJANAK DESAI return 0; 1856cf2e340fSJANAK DESAI } 1857cf2e340fSJANAK DESAI 1858cf2e340fSJANAK DESAI /* 1859cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1860cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1861cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1862cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1863cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1864cf2e340fSJANAK DESAI * task_struct. 1865cf2e340fSJANAK DESAI */ 18666559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 1867cf2e340fSJANAK DESAI { 1868cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1869cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1870b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 1871cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 18729edff4abSManfred Spraul int do_sysvsem = 0; 18739bfb23fcSOleg Nesterov int err; 1874cf2e340fSJANAK DESAI 187550804fe3SEric W. Biederman /* 1876b2e0d987SEric W. Biederman * If unsharing a user namespace must also unshare the thread. 1877b2e0d987SEric W. Biederman */ 1878b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 1879e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 1880b2e0d987SEric W. Biederman /* 188150804fe3SEric W. Biederman * If unsharing a thread from a thread group, must also unshare vm. 188250804fe3SEric W. Biederman */ 188350804fe3SEric W. Biederman if (unshare_flags & CLONE_THREAD) 188450804fe3SEric W. Biederman unshare_flags |= CLONE_VM; 188550804fe3SEric W. Biederman /* 188650804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 188750804fe3SEric W. Biederman */ 188850804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 188950804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 18906013f67fSManfred Spraul /* 18919bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 18929bfb23fcSOleg Nesterov */ 18939bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 18949bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 189550804fe3SEric W. Biederman 189650804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 189750804fe3SEric W. Biederman if (err) 189850804fe3SEric W. Biederman goto bad_unshare_out; 18999bfb23fcSOleg Nesterov /* 19006013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 19016013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 19026013f67fSManfred Spraul * namespace are unreachable. 19036013f67fSManfred Spraul */ 19046013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 19059edff4abSManfred Spraul do_sysvsem = 1; 1906fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 1907fb0a685cSDaniel Rebelo de Oliveira if (err) 19089bfb23fcSOleg Nesterov goto bad_unshare_out; 1909fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 1910fb0a685cSDaniel Rebelo de Oliveira if (err) 19119bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 1912b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 1913fb0a685cSDaniel Rebelo de Oliveira if (err) 19149edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 1915b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 1916b2e0d987SEric W. Biederman new_cred, new_fs); 1917b2e0d987SEric W. Biederman if (err) 1918b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 1919cf2e340fSJANAK DESAI 1920b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 19219edff4abSManfred Spraul if (do_sysvsem) { 19229edff4abSManfred Spraul /* 19239edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 19249edff4abSManfred Spraul */ 19259edff4abSManfred Spraul exit_sem(current); 19269edff4abSManfred Spraul } 1927ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 1928ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 1929ab602f79SJack Miller exit_shm(current); 1930ab602f79SJack Miller shm_init_task(current); 1931ab602f79SJack Miller } 1932ab516013SSerge E. Hallyn 19336f977e6bSAlan Cox if (new_nsproxy) 1934cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 1935cf2e340fSJANAK DESAI 1936cf7b708cSPavel Emelyanov task_lock(current); 1937cf7b708cSPavel Emelyanov 1938cf2e340fSJANAK DESAI if (new_fs) { 1939cf2e340fSJANAK DESAI fs = current->fs; 19402a4419b5SNick Piggin spin_lock(&fs->lock); 1941cf2e340fSJANAK DESAI current->fs = new_fs; 1942498052bbSAl Viro if (--fs->users) 1943498052bbSAl Viro new_fs = NULL; 1944498052bbSAl Viro else 1945cf2e340fSJANAK DESAI new_fs = fs; 19462a4419b5SNick Piggin spin_unlock(&fs->lock); 1947cf2e340fSJANAK DESAI } 1948cf2e340fSJANAK DESAI 1949cf2e340fSJANAK DESAI if (new_fd) { 1950cf2e340fSJANAK DESAI fd = current->files; 1951cf2e340fSJANAK DESAI current->files = new_fd; 1952cf2e340fSJANAK DESAI new_fd = fd; 1953cf2e340fSJANAK DESAI } 1954cf2e340fSJANAK DESAI 1955cf2e340fSJANAK DESAI task_unlock(current); 1956b2e0d987SEric W. Biederman 1957b2e0d987SEric W. Biederman if (new_cred) { 1958b2e0d987SEric W. Biederman /* Install the new user namespace */ 1959b2e0d987SEric W. Biederman commit_creds(new_cred); 1960b2e0d987SEric W. Biederman new_cred = NULL; 1961b2e0d987SEric W. Biederman } 1962cf2e340fSJANAK DESAI } 1963cf2e340fSJANAK DESAI 1964b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 1965b2e0d987SEric W. Biederman if (new_cred) 1966b2e0d987SEric W. Biederman put_cred(new_cred); 1967cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 1968cf2e340fSJANAK DESAI if (new_fd) 1969cf2e340fSJANAK DESAI put_files_struct(new_fd); 1970cf2e340fSJANAK DESAI 1971cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 1972cf2e340fSJANAK DESAI if (new_fs) 1973498052bbSAl Viro free_fs_struct(new_fs); 1974cf2e340fSJANAK DESAI 1975cf2e340fSJANAK DESAI bad_unshare_out: 1976cf2e340fSJANAK DESAI return err; 1977cf2e340fSJANAK DESAI } 19783b125388SAl Viro 19793b125388SAl Viro /* 19803b125388SAl Viro * Helper to unshare the files of the current task. 19813b125388SAl Viro * We don't want to expose copy_files internals to 19823b125388SAl Viro * the exec layer of the kernel. 19833b125388SAl Viro */ 19843b125388SAl Viro 19853b125388SAl Viro int unshare_files(struct files_struct **displaced) 19863b125388SAl Viro { 19873b125388SAl Viro struct task_struct *task = current; 198850704516SAl Viro struct files_struct *copy = NULL; 19893b125388SAl Viro int error; 19903b125388SAl Viro 19913b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 19923b125388SAl Viro if (error || !copy) { 19933b125388SAl Viro *displaced = NULL; 19943b125388SAl Viro return error; 19953b125388SAl Viro } 19963b125388SAl Viro *displaced = task->files; 19973b125388SAl Viro task_lock(task); 19983b125388SAl Viro task->files = copy; 19993b125388SAl Viro task_unlock(task); 20003b125388SAl Viro return 0; 20013b125388SAl Viro } 2002