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> 154eb5aaa3SIngo Molnar #include <linux/sched/autogroup.h> 166e84f315SIngo Molnar #include <linux/sched/mm.h> 17f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 188703e8a4SIngo Molnar #include <linux/sched/user.h> 196a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 2003441a34SIngo Molnar #include <linux/sched/stat.h> 2129930025SIngo Molnar #include <linux/sched/task.h> 2268db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 2332ef5517SIngo Molnar #include <linux/sched/cputime.h> 24037741a6SIngo Molnar #include <linux/rtmutex.h> 251da177e4SLinus Torvalds #include <linux/init.h> 261da177e4SLinus Torvalds #include <linux/unistd.h> 271da177e4SLinus Torvalds #include <linux/module.h> 281da177e4SLinus Torvalds #include <linux/vmalloc.h> 291da177e4SLinus Torvalds #include <linux/completion.h> 301da177e4SLinus Torvalds #include <linux/personality.h> 311da177e4SLinus Torvalds #include <linux/mempolicy.h> 321da177e4SLinus Torvalds #include <linux/sem.h> 331da177e4SLinus Torvalds #include <linux/file.h> 349f3acc31SAl Viro #include <linux/fdtable.h> 35da9cbc87SJens Axboe #include <linux/iocontext.h> 361da177e4SLinus Torvalds #include <linux/key.h> 371da177e4SLinus Torvalds #include <linux/binfmts.h> 381da177e4SLinus Torvalds #include <linux/mman.h> 39cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 40133ff0eaSJérôme Glisse #include <linux/hmm.h> 411da177e4SLinus Torvalds #include <linux/fs.h> 42615d6e87SDavidlohr Bueso #include <linux/mm.h> 43615d6e87SDavidlohr Bueso #include <linux/vmacache.h> 44ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 45c59ede7bSRandy.Dunlap #include <linux/capability.h> 461da177e4SLinus Torvalds #include <linux/cpu.h> 47b4f48b63SPaul Menage #include <linux/cgroup.h> 481da177e4SLinus Torvalds #include <linux/security.h> 49a1e78772SMel Gorman #include <linux/hugetlb.h> 50e2cfabdfSWill Drewry #include <linux/seccomp.h> 511da177e4SLinus Torvalds #include <linux/swap.h> 521da177e4SLinus Torvalds #include <linux/syscalls.h> 531da177e4SLinus Torvalds #include <linux/jiffies.h> 541da177e4SLinus Torvalds #include <linux/futex.h> 558141c7f3SLinus Torvalds #include <linux/compat.h> 56207205a2SEric Dumazet #include <linux/kthread.h> 577c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 58ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 591da177e4SLinus Torvalds #include <linux/ptrace.h> 601da177e4SLinus Torvalds #include <linux/mount.h> 611da177e4SLinus Torvalds #include <linux/audit.h> 6278fb7466SPavel Emelianov #include <linux/memcontrol.h> 63f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 645e2bf014SMike Galbraith #include <linux/proc_fs.h> 651da177e4SLinus Torvalds #include <linux/profile.h> 661da177e4SLinus Torvalds #include <linux/rmap.h> 67f8af4da3SHugh Dickins #include <linux/ksm.h> 681da177e4SLinus Torvalds #include <linux/acct.h> 69893e26e6SPavel Emelyanov #include <linux/userfaultfd_k.h> 708f0ab514SJay Lan #include <linux/tsacct_kern.h> 719f46080cSMatt Helsley #include <linux/cn_proc.h> 72ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 73ca74e92bSShailabh Nagar #include <linux/delayacct.h> 74ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 750a425405SArjan van de Ven #include <linux/random.h> 76522ed776SMiloslav Trmac #include <linux/tty.h> 77fd0928dfSJens Axboe #include <linux/blkdev.h> 785ad4e53bSAl Viro #include <linux/fs_struct.h> 797c9f8861SEric Sandeen #include <linux/magic.h> 80d70f2a14SAndrew Morton #include <linux/sched/mm.h> 81cdd6c482SIngo Molnar #include <linux/perf_event.h> 8242c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 838e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 843d5992d2SYing Han #include <linux/oom.h> 85ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 86d80e731eSOleg Nesterov #include <linux/signalfd.h> 870326f5a9SSrikar Dronamraju #include <linux/uprobes.h> 88a27bb332SKent Overstreet #include <linux/aio.h> 8952f5684cSGideon Israel Dsouza #include <linux/compiler.h> 9016db3d3fSHeinrich Schuchardt #include <linux/sysctl.h> 915c9a8750SDmitry Vyukov #include <linux/kcov.h> 92d83a7cb3SJosh Poimboeuf #include <linux/livepatch.h> 9348ac3c18SMark Rutland #include <linux/thread_info.h> 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds #include <asm/pgtable.h> 961da177e4SLinus Torvalds #include <asm/pgalloc.h> 977c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 981da177e4SLinus Torvalds #include <asm/mmu_context.h> 991da177e4SLinus Torvalds #include <asm/cacheflush.h> 1001da177e4SLinus Torvalds #include <asm/tlbflush.h> 1011da177e4SLinus Torvalds 102ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 103ad8d75ffSSteven Rostedt 10443d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 10543d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 10643d2b113SKAMEZAWA Hiroyuki 1071da177e4SLinus Torvalds /* 108ac1b398dSHeinrich Schuchardt * Minimum number of threads to boot the kernel 109ac1b398dSHeinrich Schuchardt */ 110ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20 111ac1b398dSHeinrich Schuchardt 112ac1b398dSHeinrich Schuchardt /* 113ac1b398dSHeinrich Schuchardt * Maximum number of threads 114ac1b398dSHeinrich Schuchardt */ 115ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK 116ac1b398dSHeinrich Schuchardt 117ac1b398dSHeinrich Schuchardt /* 1181da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 1191da177e4SLinus Torvalds */ 1201da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 1211da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 128db1466b3SPaul E. McKenney 129db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 130db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 131db1466b3SPaul E. McKenney { 132db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 133db1466b3SPaul E. McKenney } 134db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 135db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds int nr_processes(void) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds int cpu; 1401da177e4SLinus Torvalds int total = 0; 1411da177e4SLinus Torvalds 1421d510750SIan Campbell for_each_possible_cpu(cpu) 1431da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds return total; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 148f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 149f19b9f74SAkinobu Mita { 150f19b9f74SAkinobu Mita } 151f19b9f74SAkinobu Mita 152f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 153e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 15441101809SThomas Gleixner 15541101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 15641101809SThomas Gleixner { 15741101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 15841101809SThomas Gleixner } 15941101809SThomas Gleixner 16041101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 16141101809SThomas Gleixner { 16241101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 16341101809SThomas Gleixner } 1641da177e4SLinus Torvalds #endif 1651da177e4SLinus Torvalds 166b235beeaSLinus Torvalds void __weak arch_release_thread_stack(unsigned long *stack) 167f19b9f74SAkinobu Mita { 168f19b9f74SAkinobu Mita } 169f19b9f74SAkinobu Mita 170b235beeaSLinus Torvalds #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR 17141101809SThomas Gleixner 1720d15d74aSThomas Gleixner /* 1730d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1740d15d74aSThomas Gleixner * kmemcache based allocator. 1750d15d74aSThomas Gleixner */ 176ba14a194SAndy Lutomirski # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) 177ac496bf4SAndy Lutomirski 178ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 179ac496bf4SAndy Lutomirski /* 180ac496bf4SAndy Lutomirski * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 181ac496bf4SAndy Lutomirski * flush. Try to minimize the number of calls by caching stacks. 182ac496bf4SAndy Lutomirski */ 183ac496bf4SAndy Lutomirski #define NR_CACHED_STACKS 2 184ac496bf4SAndy Lutomirski static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 18519659c59SHoeun Ryu 18619659c59SHoeun Ryu static int free_vm_stack_cache(unsigned int cpu) 18719659c59SHoeun Ryu { 18819659c59SHoeun Ryu struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu); 18919659c59SHoeun Ryu int i; 19019659c59SHoeun Ryu 19119659c59SHoeun Ryu for (i = 0; i < NR_CACHED_STACKS; i++) { 19219659c59SHoeun Ryu struct vm_struct *vm_stack = cached_vm_stacks[i]; 19319659c59SHoeun Ryu 19419659c59SHoeun Ryu if (!vm_stack) 19519659c59SHoeun Ryu continue; 19619659c59SHoeun Ryu 19719659c59SHoeun Ryu vfree(vm_stack->addr); 19819659c59SHoeun Ryu cached_vm_stacks[i] = NULL; 19919659c59SHoeun Ryu } 20019659c59SHoeun Ryu 20119659c59SHoeun Ryu return 0; 20219659c59SHoeun Ryu } 203ac496bf4SAndy Lutomirski #endif 204ac496bf4SAndy Lutomirski 205ba14a194SAndy Lutomirski static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) 206b69c49b7SFUJITA Tomonori { 207ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 208ac496bf4SAndy Lutomirski void *stack; 209ac496bf4SAndy Lutomirski int i; 210ac496bf4SAndy Lutomirski 211ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 212112166f8SChristoph Lameter struct vm_struct *s; 213112166f8SChristoph Lameter 214112166f8SChristoph Lameter s = this_cpu_xchg(cached_stacks[i], NULL); 215ac496bf4SAndy Lutomirski 216ac496bf4SAndy Lutomirski if (!s) 217ac496bf4SAndy Lutomirski continue; 218ac496bf4SAndy Lutomirski 219ca182551SKonstantin Khlebnikov #ifdef CONFIG_DEBUG_KMEMLEAK 220ca182551SKonstantin Khlebnikov /* Clear stale pointers from reused stack. */ 221ca182551SKonstantin Khlebnikov memset(s->addr, 0, THREAD_SIZE); 222ca182551SKonstantin Khlebnikov #endif 223ac496bf4SAndy Lutomirski tsk->stack_vm_area = s; 224ac496bf4SAndy Lutomirski return s->addr; 225ac496bf4SAndy Lutomirski } 226ac496bf4SAndy Lutomirski 22748ac3c18SMark Rutland stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, 228ba14a194SAndy Lutomirski VMALLOC_START, VMALLOC_END, 22919809c2dSMichal Hocko THREADINFO_GFP, 230ba14a194SAndy Lutomirski PAGE_KERNEL, 231ac496bf4SAndy Lutomirski 0, node, __builtin_return_address(0)); 232ba14a194SAndy Lutomirski 233ba14a194SAndy Lutomirski /* 234ba14a194SAndy Lutomirski * We can't call find_vm_area() in interrupt context, and 235ba14a194SAndy Lutomirski * free_thread_stack() can be called in interrupt context, 236ba14a194SAndy Lutomirski * so cache the vm_struct. 237ba14a194SAndy Lutomirski */ 238ba14a194SAndy Lutomirski if (stack) 239ba14a194SAndy Lutomirski tsk->stack_vm_area = find_vm_area(stack); 240ba14a194SAndy Lutomirski return stack; 241ba14a194SAndy Lutomirski #else 2424949148aSVladimir Davydov struct page *page = alloc_pages_node(node, THREADINFO_GFP, 2432889f608SThomas Gleixner THREAD_SIZE_ORDER); 244b6a84016SEric Dumazet 245b6a84016SEric Dumazet return page ? page_address(page) : NULL; 246ba14a194SAndy Lutomirski #endif 247b69c49b7SFUJITA Tomonori } 248b69c49b7SFUJITA Tomonori 249ba14a194SAndy Lutomirski static inline void free_thread_stack(struct task_struct *tsk) 250b69c49b7SFUJITA Tomonori { 251ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 252ac496bf4SAndy Lutomirski if (task_stack_vm_area(tsk)) { 253ac496bf4SAndy Lutomirski int i; 254ac496bf4SAndy Lutomirski 255ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 256112166f8SChristoph Lameter if (this_cpu_cmpxchg(cached_stacks[i], 257112166f8SChristoph Lameter NULL, tsk->stack_vm_area) != NULL) 258ac496bf4SAndy Lutomirski continue; 259ac496bf4SAndy Lutomirski 260ac496bf4SAndy Lutomirski return; 261ac496bf4SAndy Lutomirski } 262ac496bf4SAndy Lutomirski 2630f110a9bSAndrey Ryabinin vfree_atomic(tsk->stack); 264ac496bf4SAndy Lutomirski return; 265ac496bf4SAndy Lutomirski } 266ac496bf4SAndy Lutomirski #endif 267ac496bf4SAndy Lutomirski 268ba14a194SAndy Lutomirski __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); 269b69c49b7SFUJITA Tomonori } 2700d15d74aSThomas Gleixner # else 271b235beeaSLinus Torvalds static struct kmem_cache *thread_stack_cache; 2720d15d74aSThomas Gleixner 2739521d399SMichael Ellerman static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, 2740d15d74aSThomas Gleixner int node) 2750d15d74aSThomas Gleixner { 276b235beeaSLinus Torvalds return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 2770d15d74aSThomas Gleixner } 2780d15d74aSThomas Gleixner 279ba14a194SAndy Lutomirski static void free_thread_stack(struct task_struct *tsk) 2800d15d74aSThomas Gleixner { 281ba14a194SAndy Lutomirski kmem_cache_free(thread_stack_cache, tsk->stack); 2820d15d74aSThomas Gleixner } 2830d15d74aSThomas Gleixner 284b235beeaSLinus Torvalds void thread_stack_cache_init(void) 2850d15d74aSThomas Gleixner { 286f9d29946SDavid Windsor thread_stack_cache = kmem_cache_create_usercopy("thread_stack", 287f9d29946SDavid Windsor THREAD_SIZE, THREAD_SIZE, 0, 0, 288f9d29946SDavid Windsor THREAD_SIZE, NULL); 289b235beeaSLinus Torvalds BUG_ON(thread_stack_cache == NULL); 2900d15d74aSThomas Gleixner } 2910d15d74aSThomas Gleixner # endif 292b69c49b7SFUJITA Tomonori #endif 293b69c49b7SFUJITA Tomonori 2941da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 295e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 298e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 301e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 304e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 307e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 310e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 3111da177e4SLinus Torvalds 312ba14a194SAndy Lutomirski static void account_kernel_stack(struct task_struct *tsk, int account) 313c6a7f572SKOSAKI Motohiro { 314ba14a194SAndy Lutomirski void *stack = task_stack_page(tsk); 315ba14a194SAndy Lutomirski struct vm_struct *vm = task_stack_vm_area(tsk); 316ba14a194SAndy Lutomirski 317ba14a194SAndy Lutomirski BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); 318ba14a194SAndy Lutomirski 319ba14a194SAndy Lutomirski if (vm) { 320ba14a194SAndy Lutomirski int i; 321ba14a194SAndy Lutomirski 322ba14a194SAndy Lutomirski BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); 323ba14a194SAndy Lutomirski 324ba14a194SAndy Lutomirski for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 325ba14a194SAndy Lutomirski mod_zone_page_state(page_zone(vm->pages[i]), 326ba14a194SAndy Lutomirski NR_KERNEL_STACK_KB, 327ba14a194SAndy Lutomirski PAGE_SIZE / 1024 * account); 328ba14a194SAndy Lutomirski } 329ba14a194SAndy Lutomirski 330ba14a194SAndy Lutomirski /* All stack pages belong to the same memcg. */ 331ed52be7bSJohannes Weiner mod_memcg_page_state(vm->pages[0], MEMCG_KERNEL_STACK_KB, 332ba14a194SAndy Lutomirski account * (THREAD_SIZE / 1024)); 333ba14a194SAndy Lutomirski } else { 334ba14a194SAndy Lutomirski /* 335ba14a194SAndy Lutomirski * All stack pages are in the same zone and belong to the 336ba14a194SAndy Lutomirski * same memcg. 337ba14a194SAndy Lutomirski */ 338efdc9490SAndy Lutomirski struct page *first_page = virt_to_page(stack); 339c6a7f572SKOSAKI Motohiro 340efdc9490SAndy Lutomirski mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB, 341d30dd8beSAndy Lutomirski THREAD_SIZE / 1024 * account); 342efdc9490SAndy Lutomirski 343ed52be7bSJohannes Weiner mod_memcg_page_state(first_page, MEMCG_KERNEL_STACK_KB, 344efdc9490SAndy Lutomirski account * (THREAD_SIZE / 1024)); 345c6a7f572SKOSAKI Motohiro } 346ba14a194SAndy Lutomirski } 347c6a7f572SKOSAKI Motohiro 34868f24b08SAndy Lutomirski static void release_task_stack(struct task_struct *tsk) 3491da177e4SLinus Torvalds { 350405c0759SAndy Lutomirski if (WARN_ON(tsk->state != TASK_DEAD)) 351405c0759SAndy Lutomirski return; /* Better to leak the stack than to free prematurely */ 352405c0759SAndy Lutomirski 353ba14a194SAndy Lutomirski account_kernel_stack(tsk, -1); 354b235beeaSLinus Torvalds arch_release_thread_stack(tsk->stack); 355ba14a194SAndy Lutomirski free_thread_stack(tsk); 35668f24b08SAndy Lutomirski tsk->stack = NULL; 35768f24b08SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 35868f24b08SAndy Lutomirski tsk->stack_vm_area = NULL; 35968f24b08SAndy Lutomirski #endif 36068f24b08SAndy Lutomirski } 36168f24b08SAndy Lutomirski 36268f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 36368f24b08SAndy Lutomirski void put_task_stack(struct task_struct *tsk) 36468f24b08SAndy Lutomirski { 36568f24b08SAndy Lutomirski if (atomic_dec_and_test(&tsk->stack_refcount)) 36668f24b08SAndy Lutomirski release_task_stack(tsk); 36768f24b08SAndy Lutomirski } 36868f24b08SAndy Lutomirski #endif 36968f24b08SAndy Lutomirski 37068f24b08SAndy Lutomirski void free_task(struct task_struct *tsk) 37168f24b08SAndy Lutomirski { 37268f24b08SAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK 37368f24b08SAndy Lutomirski /* 37468f24b08SAndy Lutomirski * The task is finally done with both the stack and thread_info, 37568f24b08SAndy Lutomirski * so free both. 37668f24b08SAndy Lutomirski */ 37768f24b08SAndy Lutomirski release_task_stack(tsk); 37868f24b08SAndy Lutomirski #else 37968f24b08SAndy Lutomirski /* 38068f24b08SAndy Lutomirski * If the task had a separate stack allocation, it should be gone 38168f24b08SAndy Lutomirski * by now. 38268f24b08SAndy Lutomirski */ 38368f24b08SAndy Lutomirski WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0); 38468f24b08SAndy Lutomirski #endif 38523f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 386fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 387e2cfabdfSWill Drewry put_seccomp_filter(tsk); 388f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 3891da5c46fSOleg Nesterov if (tsk->flags & PF_KTHREAD) 3901da5c46fSOleg Nesterov free_kthread_struct(tsk); 3911da177e4SLinus Torvalds free_task_struct(tsk); 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds #ifdef CONFIG_MMU 3960766f788SEmese Revfy static __latent_entropy int dup_mmap(struct mm_struct *mm, 3970766f788SEmese Revfy struct mm_struct *oldmm) 3981da177e4SLinus Torvalds { 399297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 4001da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 4011da177e4SLinus Torvalds int retval; 4021da177e4SLinus Torvalds unsigned long charge; 403893e26e6SPavel Emelyanov LIST_HEAD(uf); 4041da177e4SLinus Torvalds 40532cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 4067c051267SMichal Hocko if (down_write_killable(&oldmm->mmap_sem)) { 4077c051267SMichal Hocko retval = -EINTR; 4087c051267SMichal Hocko goto fail_uprobe_end; 4097c051267SMichal Hocko } 410ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 411f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 412ad339451SIngo Molnar /* 413ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 414ad339451SIngo Molnar */ 415ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 4167ee78232SHugh Dickins 41790f31d0eSKonstantin Khlebnikov /* No ordering required: file already has been exposed. */ 41890f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 41990f31d0eSKonstantin Khlebnikov 4204f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 42184638335SKonstantin Khlebnikov mm->data_vm = oldmm->data_vm; 4224f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 4234f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 4244f7d4614SVladimir Davydov 4251da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 4261da177e4SLinus Torvalds rb_parent = NULL; 4271da177e4SLinus Torvalds pprev = &mm->mmap; 428f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 429f8af4da3SHugh Dickins if (retval) 430f8af4da3SHugh Dickins goto out; 431ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 432ba76149fSAndrea Arcangeli if (retval) 433ba76149fSAndrea Arcangeli goto out; 4341da177e4SLinus Torvalds 435297c5eeeSLinus Torvalds prev = NULL; 436fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 4371da177e4SLinus Torvalds struct file *file; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 44084638335SKonstantin Khlebnikov vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); 4411da177e4SLinus Torvalds continue; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds charge = 0; 4441da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 445b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 446b2412b7fSHuang Shijie 447191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 4481da177e4SLinus Torvalds goto fail_nomem; 4491da177e4SLinus Torvalds charge = len; 4501da177e4SLinus Torvalds } 451e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 4521da177e4SLinus Torvalds if (!tmp) 4531da177e4SLinus Torvalds goto fail_nomem; 4541da177e4SLinus Torvalds *tmp = *mpnt; 4555beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 456ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 457ef0855d3SOleg Nesterov if (retval) 4581da177e4SLinus Torvalds goto fail_nomem_policy; 459a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 460893e26e6SPavel Emelyanov retval = dup_userfaultfd(tmp, &uf); 461893e26e6SPavel Emelyanov if (retval) 462893e26e6SPavel Emelyanov goto fail_nomem_anon_vma_fork; 463d2cd9edeSRik van Riel if (tmp->vm_flags & VM_WIPEONFORK) { 464d2cd9edeSRik van Riel /* VM_WIPEONFORK gets a clean slate in the child. */ 465d2cd9edeSRik van Riel tmp->anon_vma = NULL; 466d2cd9edeSRik van Riel if (anon_vma_prepare(tmp)) 467d2cd9edeSRik van Riel goto fail_nomem_anon_vma_fork; 468d2cd9edeSRik van Riel } else if (anon_vma_fork(tmp, mpnt)) 4695beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 470893e26e6SPavel Emelyanov tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT); 471297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 4721da177e4SLinus Torvalds file = tmp->vm_file; 4731da177e4SLinus Torvalds if (file) { 474496ad9aaSAl Viro struct inode *inode = file_inode(file); 475b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 476b88ed205SHugh Dickins 4771da177e4SLinus Torvalds get_file(file); 4781da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 4791da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 48083cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 481b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 4824bb5f5d9SDavid Herrmann atomic_inc(&mapping->i_mmap_writable); 483b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 484b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 4859826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 4869826a516SMichel Lespinasse &mapping->i_mmap); 487b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 48883cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* 492a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 493a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 494a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 495a1e78772SMel Gorman */ 496a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 497a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 498a1e78772SMel Gorman 499a1e78772SMel Gorman /* 5007ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 5011da177e4SLinus Torvalds */ 5021da177e4SLinus Torvalds *pprev = tmp; 5031da177e4SLinus Torvalds pprev = &tmp->vm_next; 504297c5eeeSLinus Torvalds tmp->vm_prev = prev; 505297c5eeeSLinus Torvalds prev = tmp; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 5081da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 5091da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds mm->map_count++; 512d2cd9edeSRik van Riel if (!(tmp->vm_flags & VM_WIPEONFORK)) 5130b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 5161da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds if (retval) 5191da177e4SLinus Torvalds goto out; 5201da177e4SLinus Torvalds } 521d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 522d70f2a14SAndrew Morton arch_dup_mmap(oldmm, mm); 523d70f2a14SAndrew Morton retval = 0; 5241da177e4SLinus Torvalds out: 5257ee78232SHugh Dickins up_write(&mm->mmap_sem); 526fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 5271da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 528893e26e6SPavel Emelyanov dup_userfaultfd_complete(&uf); 5297c051267SMichal Hocko fail_uprobe_end: 53032cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 5311da177e4SLinus Torvalds return retval; 5325beb4930SRik van Riel fail_nomem_anon_vma_fork: 533ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 5341da177e4SLinus Torvalds fail_nomem_policy: 5351da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 5361da177e4SLinus Torvalds fail_nomem: 5371da177e4SLinus Torvalds retval = -ENOMEM; 5381da177e4SLinus Torvalds vm_unacct_memory(charge); 5391da177e4SLinus Torvalds goto out; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 5431da177e4SLinus Torvalds { 5441da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 5451da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 5461da177e4SLinus Torvalds return -ENOMEM; 5471da177e4SLinus Torvalds return 0; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 5511da177e4SLinus Torvalds { 5525e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds #else 55590f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 55690f31d0eSKonstantin Khlebnikov { 55790f31d0eSKonstantin Khlebnikov down_write(&oldmm->mmap_sem); 55890f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 55990f31d0eSKonstantin Khlebnikov up_write(&oldmm->mmap_sem); 56090f31d0eSKonstantin Khlebnikov return 0; 56190f31d0eSKonstantin Khlebnikov } 5621da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 5631da177e4SLinus Torvalds #define mm_free_pgd(mm) 5641da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 5651da177e4SLinus Torvalds 566d70f2a14SAndrew Morton static void check_mm(struct mm_struct *mm) 567d70f2a14SAndrew Morton { 568d70f2a14SAndrew Morton int i; 569d70f2a14SAndrew Morton 570d70f2a14SAndrew Morton for (i = 0; i < NR_MM_COUNTERS; i++) { 571d70f2a14SAndrew Morton long x = atomic_long_read(&mm->rss_stat.count[i]); 572d70f2a14SAndrew Morton 573d70f2a14SAndrew Morton if (unlikely(x)) 574d70f2a14SAndrew Morton printk(KERN_ALERT "BUG: Bad rss-counter state " 575d70f2a14SAndrew Morton "mm:%p idx:%d val:%ld\n", mm, i, x); 576d70f2a14SAndrew Morton } 577d70f2a14SAndrew Morton 578d70f2a14SAndrew Morton if (mm_pgtables_bytes(mm)) 579d70f2a14SAndrew Morton pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", 580d70f2a14SAndrew Morton mm_pgtables_bytes(mm)); 581d70f2a14SAndrew Morton 582d70f2a14SAndrew Morton #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 583d70f2a14SAndrew Morton VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 584d70f2a14SAndrew Morton #endif 585d70f2a14SAndrew Morton } 5861da177e4SLinus Torvalds 587e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 5881da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 5891da177e4SLinus Torvalds 590d70f2a14SAndrew Morton /* 591d70f2a14SAndrew Morton * Called when the last reference to the mm 592d70f2a14SAndrew Morton * is dropped: either by a lazy thread or by 593d70f2a14SAndrew Morton * mmput. Free the page directory and the mm. 594d70f2a14SAndrew Morton */ 595d34bc48fSAndrew Morton void __mmdrop(struct mm_struct *mm) 596d70f2a14SAndrew Morton { 597d70f2a14SAndrew Morton BUG_ON(mm == &init_mm); 598d70f2a14SAndrew Morton mm_free_pgd(mm); 599d70f2a14SAndrew Morton destroy_context(mm); 600d70f2a14SAndrew Morton hmm_mm_destroy(mm); 601d70f2a14SAndrew Morton mmu_notifier_mm_destroy(mm); 602d70f2a14SAndrew Morton check_mm(mm); 603d70f2a14SAndrew Morton put_user_ns(mm->user_ns); 604d70f2a14SAndrew Morton free_mm(mm); 605d70f2a14SAndrew Morton } 606d34bc48fSAndrew Morton EXPORT_SYMBOL_GPL(__mmdrop); 607d70f2a14SAndrew Morton 608d70f2a14SAndrew Morton static void mmdrop_async_fn(struct work_struct *work) 609d70f2a14SAndrew Morton { 610d70f2a14SAndrew Morton struct mm_struct *mm; 611d70f2a14SAndrew Morton 612d70f2a14SAndrew Morton mm = container_of(work, struct mm_struct, async_put_work); 613d70f2a14SAndrew Morton __mmdrop(mm); 614d70f2a14SAndrew Morton } 615d70f2a14SAndrew Morton 616d70f2a14SAndrew Morton static void mmdrop_async(struct mm_struct *mm) 617d70f2a14SAndrew Morton { 618d70f2a14SAndrew Morton if (unlikely(atomic_dec_and_test(&mm->mm_count))) { 619d70f2a14SAndrew Morton INIT_WORK(&mm->async_put_work, mmdrop_async_fn); 620d70f2a14SAndrew Morton schedule_work(&mm->async_put_work); 621d70f2a14SAndrew Morton } 622d70f2a14SAndrew Morton } 623d70f2a14SAndrew Morton 6241da177e4SLinus Torvalds static inline void free_signal_struct(struct signal_struct *sig) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds taskstats_tgid_free(sig); 6271da177e4SLinus Torvalds sched_autogroup_exit(sig); 6281da177e4SLinus Torvalds /* 6291da177e4SLinus Torvalds * __mmdrop is not safe to call from softirq context on x86 due to 6301da177e4SLinus Torvalds * pgd_dtor so postpone it to the async context 6311da177e4SLinus Torvalds */ 6321da177e4SLinus Torvalds if (sig->oom_mm) 6331da177e4SLinus Torvalds mmdrop_async(sig->oom_mm); 6341da177e4SLinus Torvalds kmem_cache_free(signal_cachep, sig); 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds static inline void put_signal_struct(struct signal_struct *sig) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds if (atomic_dec_and_test(&sig->sigcnt)) 6401da177e4SLinus Torvalds free_signal_struct(sig); 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds void __put_task_struct(struct task_struct *tsk) 6441da177e4SLinus Torvalds { 6451da177e4SLinus Torvalds WARN_ON(!tsk->exit_state); 6461da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 6471da177e4SLinus Torvalds WARN_ON(tsk == current); 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds cgroup_free(tsk); 6501da177e4SLinus Torvalds task_numa_free(tsk); 6511da177e4SLinus Torvalds security_task_free(tsk); 6521da177e4SLinus Torvalds exit_creds(tsk); 6531da177e4SLinus Torvalds delayacct_tsk_free(tsk); 6541da177e4SLinus Torvalds put_signal_struct(tsk->signal); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 6571da177e4SLinus Torvalds free_task(tsk); 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__put_task_struct); 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds void __init __weak arch_task_cache_init(void) { } 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds /* 6641da177e4SLinus Torvalds * set_max_threads 6651da177e4SLinus Torvalds */ 6661da177e4SLinus Torvalds static void set_max_threads(unsigned int max_threads_suggested) 6671da177e4SLinus Torvalds { 6681da177e4SLinus Torvalds u64 threads; 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds /* 6711da177e4SLinus Torvalds * The number of threads shall be limited such that the thread 6721da177e4SLinus Torvalds * structures may only consume a small part of the available memory. 6731da177e4SLinus Torvalds */ 6741da177e4SLinus Torvalds if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64) 6751da177e4SLinus Torvalds threads = MAX_THREADS; 6761da177e4SLinus Torvalds else 6771da177e4SLinus Torvalds threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, 6781da177e4SLinus Torvalds (u64) THREAD_SIZE * 8UL); 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds if (threads > max_threads_suggested) 6811da177e4SLinus Torvalds threads = max_threads_suggested; 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 6871da177e4SLinus Torvalds /* Initialized by the architecture: */ 6881da177e4SLinus Torvalds int arch_task_struct_size __read_mostly; 6891da177e4SLinus Torvalds #endif 6901da177e4SLinus Torvalds 6915905429aSKees Cook static void task_struct_whitelist(unsigned long *offset, unsigned long *size) 6925905429aSKees Cook { 6935905429aSKees Cook /* Fetch thread_struct whitelist for the architecture. */ 6945905429aSKees Cook arch_thread_struct_whitelist(offset, size); 6955905429aSKees Cook 6965905429aSKees Cook /* 6975905429aSKees Cook * Handle zero-sized whitelist or empty thread_struct, otherwise 6985905429aSKees Cook * adjust offset to position of thread_struct in task_struct. 6995905429aSKees Cook */ 7005905429aSKees Cook if (unlikely(*size == 0)) 7015905429aSKees Cook *offset = 0; 7025905429aSKees Cook else 7035905429aSKees Cook *offset += offsetof(struct task_struct, thread); 7045905429aSKees Cook } 7055905429aSKees Cook 7061da177e4SLinus Torvalds void __init fork_init(void) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds int i; 7091da177e4SLinus Torvalds #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 7101da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 7111da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN 0 7121da177e4SLinus Torvalds #endif 7131da177e4SLinus Torvalds int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); 7145905429aSKees Cook unsigned long useroffset, usersize; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 7175905429aSKees Cook task_struct_whitelist(&useroffset, &usersize); 7185905429aSKees Cook task_struct_cachep = kmem_cache_create_usercopy("task_struct", 7191da177e4SLinus Torvalds arch_task_struct_size, align, 7205905429aSKees Cook SLAB_PANIC|SLAB_ACCOUNT, 7215905429aSKees Cook useroffset, usersize, NULL); 7221da177e4SLinus Torvalds #endif 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds /* do the arch specific task caches init */ 7251da177e4SLinus Torvalds arch_task_cache_init(); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds set_max_threads(MAX_THREADS); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 7301da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 7311da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 7321da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds for (i = 0; i < UCOUNT_COUNTS; i++) { 7351da177e4SLinus Torvalds init_user_ns.ucount_max[i] = max_threads/2; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds #ifdef CONFIG_VMAP_STACK 7391da177e4SLinus Torvalds cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", 7401da177e4SLinus Torvalds NULL, free_vm_stack_cache); 7411da177e4SLinus Torvalds #endif 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds lockdep_init_task(&init_task); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds int __weak arch_dup_task_struct(struct task_struct *dst, 7471da177e4SLinus Torvalds struct task_struct *src) 7481da177e4SLinus Torvalds { 7491da177e4SLinus Torvalds *dst = *src; 7501da177e4SLinus Torvalds return 0; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds void set_task_stack_end_magic(struct task_struct *tsk) 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds unsigned long *stackend; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds stackend = end_of_stack(tsk); 7581da177e4SLinus Torvalds *stackend = STACK_END_MAGIC; /* for overflow detection */ 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds struct task_struct *tsk; 7641da177e4SLinus Torvalds unsigned long *stack; 7651da177e4SLinus Torvalds struct vm_struct *stack_vm_area; 7661da177e4SLinus Torvalds int err; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds if (node == NUMA_NO_NODE) 7691da177e4SLinus Torvalds node = tsk_fork_get_node(orig); 7701da177e4SLinus Torvalds tsk = alloc_task_struct_node(node); 7711da177e4SLinus Torvalds if (!tsk) 7721da177e4SLinus Torvalds return NULL; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds stack = alloc_thread_stack_node(tsk, node); 7751da177e4SLinus Torvalds if (!stack) 7761da177e4SLinus Torvalds goto free_tsk; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds stack_vm_area = task_stack_vm_area(tsk); 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds err = arch_dup_task_struct(tsk, orig); 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* 7831da177e4SLinus Torvalds * arch_dup_task_struct() clobbers the stack-related fields. Make 7841da177e4SLinus Torvalds * sure they're properly initialized before using any stack-related 7851da177e4SLinus Torvalds * functions again. 7861da177e4SLinus Torvalds */ 7871da177e4SLinus Torvalds tsk->stack = stack; 7881da177e4SLinus Torvalds #ifdef CONFIG_VMAP_STACK 7891da177e4SLinus Torvalds tsk->stack_vm_area = stack_vm_area; 7901da177e4SLinus Torvalds #endif 7911da177e4SLinus Torvalds #ifdef CONFIG_THREAD_INFO_IN_TASK 7921da177e4SLinus Torvalds atomic_set(&tsk->stack_refcount, 1); 7931da177e4SLinus Torvalds #endif 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds if (err) 7961da177e4SLinus Torvalds goto free_stack; 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 7991da177e4SLinus Torvalds /* 8001da177e4SLinus Torvalds * We must handle setting up seccomp filters once we're under 8011da177e4SLinus Torvalds * the sighand lock in case orig has changed between now and 8021da177e4SLinus Torvalds * then. Until then, filter must be NULL to avoid messing up 8031da177e4SLinus Torvalds * the usage counts on the error path calling free_task. 8041da177e4SLinus Torvalds */ 8051da177e4SLinus Torvalds tsk->seccomp.filter = NULL; 8061da177e4SLinus Torvalds #endif 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds setup_thread_stack(tsk, orig); 8091da177e4SLinus Torvalds clear_user_return_notifier(tsk); 8101da177e4SLinus Torvalds clear_tsk_need_resched(tsk); 8111da177e4SLinus Torvalds set_task_stack_end_magic(tsk); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds #ifdef CONFIG_CC_STACKPROTECTOR 8141da177e4SLinus Torvalds tsk->stack_canary = get_random_canary(); 8151da177e4SLinus Torvalds #endif 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds /* 8181da177e4SLinus Torvalds * One for us, one for whoever does the "release_task()" (usually 8191da177e4SLinus Torvalds * parent) 8201da177e4SLinus Torvalds */ 8211da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 8221da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_IO_TRACE 8231da177e4SLinus Torvalds tsk->btrace_seq = 0; 8241da177e4SLinus Torvalds #endif 8251da177e4SLinus Torvalds tsk->splice_pipe = NULL; 8261da177e4SLinus Torvalds tsk->task_frag.page = NULL; 8271da177e4SLinus Torvalds tsk->wake_q.next = NULL; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds account_kernel_stack(tsk, 1); 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds kcov_task_init(tsk); 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds #ifdef CONFIG_FAULT_INJECTION 8341da177e4SLinus Torvalds tsk->fail_nth = 0; 8351da177e4SLinus Torvalds #endif 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds return tsk; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds free_stack: 8401da177e4SLinus Torvalds free_thread_stack(tsk); 8411da177e4SLinus Torvalds free_tsk: 8421da177e4SLinus Torvalds free_task_struct(tsk); 8431da177e4SLinus Torvalds return NULL; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 8471da177e4SLinus Torvalds 8484cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 8494cb0e11bSHidehiro Kawai 8504cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 8514cb0e11bSHidehiro Kawai { 8524cb0e11bSHidehiro Kawai default_dump_filter = 8534cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 8544cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 8554cb0e11bSHidehiro Kawai return 1; 8564cb0e11bSHidehiro Kawai } 8574cb0e11bSHidehiro Kawai 8584cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 8594cb0e11bSHidehiro Kawai 8601da177e4SLinus Torvalds #include <linux/init_task.h> 8611da177e4SLinus Torvalds 862858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 863858f0993SAlexey Dobriyan { 864858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 865858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 866db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 867858f0993SAlexey Dobriyan #endif 868858f0993SAlexey Dobriyan } 869858f0993SAlexey Dobriyan 87033144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 87133144e84SVladimir Davydov { 87233144e84SVladimir Davydov #ifdef CONFIG_MEMCG 87333144e84SVladimir Davydov mm->owner = p; 87433144e84SVladimir Davydov #endif 87533144e84SVladimir Davydov } 87633144e84SVladimir Davydov 877355627f5SEric Biggers static void mm_init_uprobes_state(struct mm_struct *mm) 878355627f5SEric Biggers { 879355627f5SEric Biggers #ifdef CONFIG_UPROBES 880355627f5SEric Biggers mm->uprobes_state.xol_area = NULL; 881355627f5SEric Biggers #endif 882355627f5SEric Biggers } 883355627f5SEric Biggers 884bfedb589SEric W. Biederman static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, 885bfedb589SEric W. Biederman struct user_namespace *user_ns) 8861da177e4SLinus Torvalds { 88741f727fdSVladimir Davydov mm->mmap = NULL; 88841f727fdSVladimir Davydov mm->mm_rb = RB_ROOT; 88941f727fdSVladimir Davydov mm->vmacache_seqnum = 0; 8901da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 8911da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 8921da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 8931da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 894999d9fc1SOleg Nesterov mm->core_state = NULL; 895af5b0f6aSKirill A. Shutemov mm_pgtables_bytes_init(mm); 89641f727fdSVladimir Davydov mm->map_count = 0; 89741f727fdSVladimir Davydov mm->locked_vm = 0; 898ce65cefaSVladimir Davydov mm->pinned_vm = 0; 899d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 9001da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 90141f727fdSVladimir Davydov mm_init_cpumask(mm); 902858f0993SAlexey Dobriyan mm_init_aio(mm); 903cf475ad2SBalbir Singh mm_init_owner(mm, p); 9042b7e8665SEric Biggers RCU_INIT_POINTER(mm->exe_file, NULL); 90541f727fdSVladimir Davydov mmu_notifier_mm_init(mm); 906133ff0eaSJérôme Glisse hmm_mm_init(mm); 90716af97dcSNadav Amit init_tlb_flush_pending(mm); 90841f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 90941f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 91041f727fdSVladimir Davydov #endif 911355627f5SEric Biggers mm_init_uprobes_state(mm); 9121da177e4SLinus Torvalds 913a0715cc2SAlex Thorlton if (current->mm) { 914a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 915a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 916a0715cc2SAlex Thorlton } else { 917a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 9181da177e4SLinus Torvalds mm->def_flags = 0; 919a0715cc2SAlex Thorlton } 920a0715cc2SAlex Thorlton 92141f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 92241f727fdSVladimir Davydov goto fail_nopgd; 92378fb7466SPavel Emelianov 92441f727fdSVladimir Davydov if (init_new_context(p, mm)) 92541f727fdSVladimir Davydov goto fail_nocontext; 92641f727fdSVladimir Davydov 927bfedb589SEric W. Biederman mm->user_ns = get_user_ns(user_ns); 92841f727fdSVladimir Davydov return mm; 92941f727fdSVladimir Davydov 93041f727fdSVladimir Davydov fail_nocontext: 93141f727fdSVladimir Davydov mm_free_pgd(mm); 93241f727fdSVladimir Davydov fail_nopgd: 9331da177e4SLinus Torvalds free_mm(mm); 9341da177e4SLinus Torvalds return NULL; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* 9381da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 9391da177e4SLinus Torvalds */ 9401da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 9411da177e4SLinus Torvalds { 9421da177e4SLinus Torvalds struct mm_struct *mm; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds mm = allocate_mm(); 945de03c72cSKOSAKI Motohiro if (!mm) 946de03c72cSKOSAKI Motohiro return NULL; 947de03c72cSKOSAKI Motohiro 9481da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 949bfedb589SEric W. Biederman return mm_init(mm, current, current_user_ns()); 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 952ec8d7c14SMichal Hocko static inline void __mmput(struct mm_struct *mm) 9531da177e4SLinus Torvalds { 954ec8d7c14SMichal Hocko VM_BUG_ON(atomic_read(&mm->mm_users)); 9550ae26f1bSAndrew Morton 956d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 9571da177e4SLinus Torvalds exit_aio(mm); 9581c2fb7a4SAndrea Arcangeli ksm_exit(mm); 959ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 9601da177e4SLinus Torvalds exit_mmap(mm); 9616fcb52a5SAaron Lu mm_put_huge_zero_page(mm); 962925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 9631da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 9641da177e4SLinus Torvalds spin_lock(&mmlist_lock); 9651da177e4SLinus Torvalds list_del(&mm->mmlist); 9661da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 9671da177e4SLinus Torvalds } 968801460d0SHiroshi Shimamoto if (mm->binfmt) 969801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 9701da177e4SLinus Torvalds mmdrop(mm); 9711da177e4SLinus Torvalds } 972ec8d7c14SMichal Hocko 973ec8d7c14SMichal Hocko /* 974ec8d7c14SMichal Hocko * Decrement the use count and release all resources for an mm. 975ec8d7c14SMichal Hocko */ 976ec8d7c14SMichal Hocko void mmput(struct mm_struct *mm) 977ec8d7c14SMichal Hocko { 978ec8d7c14SMichal Hocko might_sleep(); 979ec8d7c14SMichal Hocko 980ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) 981ec8d7c14SMichal Hocko __mmput(mm); 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 9841da177e4SLinus Torvalds 985a1b2289cSSherry Yang #ifdef CONFIG_MMU 986a1b2289cSSherry Yang static void mmput_async_fn(struct work_struct *work) 987a1b2289cSSherry Yang { 988a1b2289cSSherry Yang struct mm_struct *mm = container_of(work, struct mm_struct, 989a1b2289cSSherry Yang async_put_work); 990a1b2289cSSherry Yang 991a1b2289cSSherry Yang __mmput(mm); 992a1b2289cSSherry Yang } 993a1b2289cSSherry Yang 994a1b2289cSSherry Yang void mmput_async(struct mm_struct *mm) 995a1b2289cSSherry Yang { 996a1b2289cSSherry Yang if (atomic_dec_and_test(&mm->mm_users)) { 997a1b2289cSSherry Yang INIT_WORK(&mm->async_put_work, mmput_async_fn); 998a1b2289cSSherry Yang schedule_work(&mm->async_put_work); 999a1b2289cSSherry Yang } 1000a1b2289cSSherry Yang } 1001a1b2289cSSherry Yang #endif 1002a1b2289cSSherry Yang 100390f31d0eSKonstantin Khlebnikov /** 100490f31d0eSKonstantin Khlebnikov * set_mm_exe_file - change a reference to the mm's executable file 100590f31d0eSKonstantin Khlebnikov * 100690f31d0eSKonstantin Khlebnikov * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 100790f31d0eSKonstantin Khlebnikov * 10086e399cd1SDavidlohr Bueso * Main users are mmput() and sys_execve(). Callers prevent concurrent 10096e399cd1SDavidlohr Bueso * invocations: in mmput() nobody alive left, in execve task is single 10106e399cd1SDavidlohr Bueso * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the 10116e399cd1SDavidlohr Bueso * mm->exe_file, but does so without using set_mm_exe_file() in order 10126e399cd1SDavidlohr Bueso * to do avoid the need for any locks. 101390f31d0eSKonstantin Khlebnikov */ 101438646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 101538646013SJiri Slaby { 10166e399cd1SDavidlohr Bueso struct file *old_exe_file; 10176e399cd1SDavidlohr Bueso 10186e399cd1SDavidlohr Bueso /* 10196e399cd1SDavidlohr Bueso * It is safe to dereference the exe_file without RCU as 10206e399cd1SDavidlohr Bueso * this function is only called if nobody else can access 10216e399cd1SDavidlohr Bueso * this mm -- see comment above for justification. 10226e399cd1SDavidlohr Bueso */ 10236e399cd1SDavidlohr Bueso old_exe_file = rcu_dereference_raw(mm->exe_file); 102490f31d0eSKonstantin Khlebnikov 102538646013SJiri Slaby if (new_exe_file) 102638646013SJiri Slaby get_file(new_exe_file); 102790f31d0eSKonstantin Khlebnikov rcu_assign_pointer(mm->exe_file, new_exe_file); 102890f31d0eSKonstantin Khlebnikov if (old_exe_file) 102990f31d0eSKonstantin Khlebnikov fput(old_exe_file); 103038646013SJiri Slaby } 103138646013SJiri Slaby 103290f31d0eSKonstantin Khlebnikov /** 103390f31d0eSKonstantin Khlebnikov * get_mm_exe_file - acquire a reference to the mm's executable file 103490f31d0eSKonstantin Khlebnikov * 103590f31d0eSKonstantin Khlebnikov * Returns %NULL if mm has no associated executable file. 103690f31d0eSKonstantin Khlebnikov * User must release file via fput(). 103790f31d0eSKonstantin Khlebnikov */ 103838646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 103938646013SJiri Slaby { 104038646013SJiri Slaby struct file *exe_file; 104138646013SJiri Slaby 104290f31d0eSKonstantin Khlebnikov rcu_read_lock(); 104390f31d0eSKonstantin Khlebnikov exe_file = rcu_dereference(mm->exe_file); 104490f31d0eSKonstantin Khlebnikov if (exe_file && !get_file_rcu(exe_file)) 104590f31d0eSKonstantin Khlebnikov exe_file = NULL; 104690f31d0eSKonstantin Khlebnikov rcu_read_unlock(); 104738646013SJiri Slaby return exe_file; 104838646013SJiri Slaby } 104911163348SDavidlohr Bueso EXPORT_SYMBOL(get_mm_exe_file); 105038646013SJiri Slaby 10511da177e4SLinus Torvalds /** 1052cd81a917SMateusz Guzik * get_task_exe_file - acquire a reference to the task's executable file 1053cd81a917SMateusz Guzik * 1054cd81a917SMateusz Guzik * Returns %NULL if task's mm (if any) has no associated executable file or 1055cd81a917SMateusz Guzik * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 1056cd81a917SMateusz Guzik * User must release file via fput(). 1057cd81a917SMateusz Guzik */ 1058cd81a917SMateusz Guzik struct file *get_task_exe_file(struct task_struct *task) 1059cd81a917SMateusz Guzik { 1060cd81a917SMateusz Guzik struct file *exe_file = NULL; 1061cd81a917SMateusz Guzik struct mm_struct *mm; 1062cd81a917SMateusz Guzik 1063cd81a917SMateusz Guzik task_lock(task); 1064cd81a917SMateusz Guzik mm = task->mm; 1065cd81a917SMateusz Guzik if (mm) { 1066cd81a917SMateusz Guzik if (!(task->flags & PF_KTHREAD)) 1067cd81a917SMateusz Guzik exe_file = get_mm_exe_file(mm); 1068cd81a917SMateusz Guzik } 1069cd81a917SMateusz Guzik task_unlock(task); 1070cd81a917SMateusz Guzik return exe_file; 1071cd81a917SMateusz Guzik } 1072cd81a917SMateusz Guzik EXPORT_SYMBOL(get_task_exe_file); 1073cd81a917SMateusz Guzik 1074cd81a917SMateusz Guzik /** 10751da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 10761da177e4SLinus Torvalds * 1077246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 10781da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 10791da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 10801da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 10811da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 10821da177e4SLinus Torvalds */ 10831da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds struct mm_struct *mm; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds task_lock(task); 10881da177e4SLinus Torvalds mm = task->mm; 10891da177e4SLinus Torvalds if (mm) { 1090246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 10911da177e4SLinus Torvalds mm = NULL; 10921da177e4SLinus Torvalds else 10933fce371bSVegard Nossum mmget(mm); 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds task_unlock(task); 10961da177e4SLinus Torvalds return mm; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 10991da177e4SLinus Torvalds 11008cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 11018cdb878dSChristopher Yeoh { 11028cdb878dSChristopher Yeoh struct mm_struct *mm; 11038cdb878dSChristopher Yeoh int err; 11048cdb878dSChristopher Yeoh 11058cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 11068cdb878dSChristopher Yeoh if (err) 11078cdb878dSChristopher Yeoh return ERR_PTR(err); 11088cdb878dSChristopher Yeoh 11098cdb878dSChristopher Yeoh mm = get_task_mm(task); 11108cdb878dSChristopher Yeoh if (mm && mm != current->mm && 11118cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 11128cdb878dSChristopher Yeoh mmput(mm); 11138cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 11148cdb878dSChristopher Yeoh } 11158cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 11168cdb878dSChristopher Yeoh 11178cdb878dSChristopher Yeoh return mm; 11188cdb878dSChristopher Yeoh } 11198cdb878dSChristopher Yeoh 112057b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 1121c415c3b4SOleg Nesterov { 1122d68b46feSOleg Nesterov struct completion *vfork; 1123c415c3b4SOleg Nesterov 1124d68b46feSOleg Nesterov task_lock(tsk); 1125d68b46feSOleg Nesterov vfork = tsk->vfork_done; 1126d68b46feSOleg Nesterov if (likely(vfork)) { 1127c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 1128d68b46feSOleg Nesterov complete(vfork); 1129d68b46feSOleg Nesterov } 1130d68b46feSOleg Nesterov task_unlock(tsk); 1131d68b46feSOleg Nesterov } 1132d68b46feSOleg Nesterov 1133d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 1134d68b46feSOleg Nesterov struct completion *vfork) 1135d68b46feSOleg Nesterov { 1136d68b46feSOleg Nesterov int killed; 1137d68b46feSOleg Nesterov 1138d68b46feSOleg Nesterov freezer_do_not_count(); 1139d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 1140d68b46feSOleg Nesterov freezer_count(); 1141d68b46feSOleg Nesterov 1142d68b46feSOleg Nesterov if (killed) { 1143d68b46feSOleg Nesterov task_lock(child); 1144d68b46feSOleg Nesterov child->vfork_done = NULL; 1145d68b46feSOleg Nesterov task_unlock(child); 1146d68b46feSOleg Nesterov } 1147d68b46feSOleg Nesterov 1148d68b46feSOleg Nesterov put_task_struct(child); 1149d68b46feSOleg Nesterov return killed; 1150c415c3b4SOleg Nesterov } 1151c415c3b4SOleg Nesterov 11521da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 11531da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 11541da177e4SLinus Torvalds * error success whatever. 11551da177e4SLinus Torvalds * 11561da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 11571da177e4SLinus Torvalds * from the current process. 11581da177e4SLinus Torvalds * 11591da177e4SLinus Torvalds * This difference is important for error handling, when we 11601da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 11611da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 11621da177e4SLinus Torvalds * restoring the old one. . . 11631da177e4SLinus Torvalds * Eric Biederman 10 January 1998 11641da177e4SLinus Torvalds */ 11651da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 11661da177e4SLinus Torvalds { 11678141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 11688141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 1169fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 11708141c7f3SLinus Torvalds exit_robust_list(tsk); 1171fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 1172fc6b177dSPeter Zijlstra } 11738141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 1174fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 11758141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 1176fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 1177fc6b177dSPeter Zijlstra } 11788141c7f3SLinus Torvalds #endif 1179322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 1180322a2c10SThomas Gleixner exit_pi_state_list(tsk); 11818141c7f3SLinus Torvalds #endif 11828141c7f3SLinus Torvalds 11830326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 11840326f5a9SSrikar Dronamraju 11851da177e4SLinus Torvalds /* Get rid of any cached register state */ 11861da177e4SLinus Torvalds deactivate_mm(tsk, mm); 11871da177e4SLinus Torvalds 1188fec1d011SRoland McGrath /* 1189735f2770SMichal Hocko * Signal userspace if we're not exiting with a core dump 1190735f2770SMichal Hocko * because we want to leave the value intact for debugging 1191735f2770SMichal Hocko * purposes. 1192fec1d011SRoland McGrath */ 11939c8a8228SEric Dumazet if (tsk->clear_child_tid) { 1194735f2770SMichal Hocko if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) && 11959c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 11961da177e4SLinus Torvalds /* 11971da177e4SLinus Torvalds * We don't check the error code - if userspace has 11981da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 11991da177e4SLinus Torvalds */ 12009c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 12012de0db99SDominik Brodowski do_futex(tsk->clear_child_tid, FUTEX_WAKE, 12022de0db99SDominik Brodowski 1, NULL, NULL, 0, 0); 12039c8a8228SEric Dumazet } 12049c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 12051da177e4SLinus Torvalds } 1206f7505d64SKonstantin Khlebnikov 1207f7505d64SKonstantin Khlebnikov /* 1208f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 1209f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 1210f7505d64SKonstantin Khlebnikov */ 1211f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 1212f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 1215a0a7ec30SJANAK DESAI /* 1216a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 1217a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 1218a0a7ec30SJANAK DESAI */ 1219ff252c1fSDaeSeok Youn static struct mm_struct *dup_mm(struct task_struct *tsk) 1220a0a7ec30SJANAK DESAI { 1221a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 1222a0a7ec30SJANAK DESAI int err; 1223a0a7ec30SJANAK DESAI 1224a0a7ec30SJANAK DESAI mm = allocate_mm(); 1225a0a7ec30SJANAK DESAI if (!mm) 1226a0a7ec30SJANAK DESAI goto fail_nomem; 1227a0a7ec30SJANAK DESAI 1228a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 1229a0a7ec30SJANAK DESAI 1230bfedb589SEric W. Biederman if (!mm_init(mm, tsk, mm->user_ns)) 1231a0a7ec30SJANAK DESAI goto fail_nomem; 1232a0a7ec30SJANAK DESAI 1233a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 1234a0a7ec30SJANAK DESAI if (err) 1235a0a7ec30SJANAK DESAI goto free_pt; 1236a0a7ec30SJANAK DESAI 1237a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 1238a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 1239a0a7ec30SJANAK DESAI 1240801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1241801460d0SHiroshi Shimamoto goto free_pt; 1242801460d0SHiroshi Shimamoto 1243a0a7ec30SJANAK DESAI return mm; 1244a0a7ec30SJANAK DESAI 1245a0a7ec30SJANAK DESAI free_pt: 1246801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 1247801460d0SHiroshi Shimamoto mm->binfmt = NULL; 1248a0a7ec30SJANAK DESAI mmput(mm); 1249a0a7ec30SJANAK DESAI 1250a0a7ec30SJANAK DESAI fail_nomem: 1251a0a7ec30SJANAK DESAI return NULL; 1252a0a7ec30SJANAK DESAI } 1253a0a7ec30SJANAK DESAI 12541da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 12551da177e4SLinus Torvalds { 12561da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 12571da177e4SLinus Torvalds int retval; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 12601da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 126117406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 126217406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 126317406b82SMandeep Singh Baines #endif 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds tsk->mm = NULL; 12661da177e4SLinus Torvalds tsk->active_mm = NULL; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds /* 12691da177e4SLinus Torvalds * Are we cloning a kernel thread? 12701da177e4SLinus Torvalds * 12711da177e4SLinus Torvalds * We need to steal a active VM for that.. 12721da177e4SLinus Torvalds */ 12731da177e4SLinus Torvalds oldmm = current->mm; 12741da177e4SLinus Torvalds if (!oldmm) 12751da177e4SLinus Torvalds return 0; 12761da177e4SLinus Torvalds 1277615d6e87SDavidlohr Bueso /* initialize the new vmacache entries */ 1278615d6e87SDavidlohr Bueso vmacache_flush(tsk); 1279615d6e87SDavidlohr Bueso 12801da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 12813fce371bSVegard Nossum mmget(oldmm); 12821da177e4SLinus Torvalds mm = oldmm; 12831da177e4SLinus Torvalds goto good_mm; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds retval = -ENOMEM; 1287a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 12881da177e4SLinus Torvalds if (!mm) 12891da177e4SLinus Torvalds goto fail_nomem; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds good_mm: 12921da177e4SLinus Torvalds tsk->mm = mm; 12931da177e4SLinus Torvalds tsk->active_mm = mm; 12941da177e4SLinus Torvalds return 0; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds fail_nomem: 12971da177e4SLinus Torvalds return retval; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 1300a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 13011da177e4SLinus Torvalds { 1302498052bbSAl Viro struct fs_struct *fs = current->fs; 13031da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 1304498052bbSAl Viro /* tsk->fs is already what we want */ 13052a4419b5SNick Piggin spin_lock(&fs->lock); 1306498052bbSAl Viro if (fs->in_exec) { 13072a4419b5SNick Piggin spin_unlock(&fs->lock); 1308498052bbSAl Viro return -EAGAIN; 1309498052bbSAl Viro } 1310498052bbSAl Viro fs->users++; 13112a4419b5SNick Piggin spin_unlock(&fs->lock); 13121da177e4SLinus Torvalds return 0; 13131da177e4SLinus Torvalds } 1314498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 13151da177e4SLinus Torvalds if (!tsk->fs) 13161da177e4SLinus Torvalds return -ENOMEM; 13171da177e4SLinus Torvalds return 0; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 1320a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 1321a016f338SJANAK DESAI { 1322a016f338SJANAK DESAI struct files_struct *oldf, *newf; 1323a016f338SJANAK DESAI int error = 0; 1324a016f338SJANAK DESAI 1325a016f338SJANAK DESAI /* 1326a016f338SJANAK DESAI * A background process may not have any files ... 1327a016f338SJANAK DESAI */ 1328a016f338SJANAK DESAI oldf = current->files; 1329a016f338SJANAK DESAI if (!oldf) 1330a016f338SJANAK DESAI goto out; 1331a016f338SJANAK DESAI 1332a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 1333a016f338SJANAK DESAI atomic_inc(&oldf->count); 1334a016f338SJANAK DESAI goto out; 1335a016f338SJANAK DESAI } 1336a016f338SJANAK DESAI 1337a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 1338a016f338SJANAK DESAI if (!newf) 1339a016f338SJANAK DESAI goto out; 1340a016f338SJANAK DESAI 1341a016f338SJANAK DESAI tsk->files = newf; 1342a016f338SJANAK DESAI error = 0; 1343a016f338SJANAK DESAI out: 1344a016f338SJANAK DESAI return error; 1345a016f338SJANAK DESAI } 1346a016f338SJANAK DESAI 1347fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 1348fd0928dfSJens Axboe { 1349fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 1350fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 13516e736be7STejun Heo struct io_context *new_ioc; 1352fd0928dfSJens Axboe 1353fd0928dfSJens Axboe if (!ioc) 1354fd0928dfSJens Axboe return 0; 1355fadad878SJens Axboe /* 1356fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 1357fadad878SJens Axboe */ 1358fadad878SJens Axboe if (clone_flags & CLONE_IO) { 13593d48749dSTejun Heo ioc_task_link(ioc); 13603d48749dSTejun Heo tsk->io_context = ioc; 1361fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 13626e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 13636e736be7STejun Heo if (unlikely(!new_ioc)) 1364fd0928dfSJens Axboe return -ENOMEM; 1365fd0928dfSJens Axboe 13666e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 136711a3122fSTejun Heo put_io_context(new_ioc); 1368fd0928dfSJens Axboe } 1369fd0928dfSJens Axboe #endif 1370fd0928dfSJens Axboe return 0; 1371fd0928dfSJens Axboe } 1372fd0928dfSJens Axboe 1373a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds struct sighand_struct *sig; 13761da177e4SLinus Torvalds 137760348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 13781da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 13791da177e4SLinus Torvalds return 0; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 1382e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 13831da177e4SLinus Torvalds if (!sig) 13841da177e4SLinus Torvalds return -ENOMEM; 13859d7fb042SPeter Zijlstra 13861da177e4SLinus Torvalds atomic_set(&sig->count, 1); 13871da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 13881da177e4SLinus Torvalds return 0; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 1391a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1392c81addc9SOleg Nesterov { 1393d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 1394d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1395392809b2SOleg Nesterov /* 13965f0d5a3aSPaul E. McKenney * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it 1397392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1398392809b2SOleg Nesterov */ 1399c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1400c81addc9SOleg Nesterov } 1401d80e731eSOleg Nesterov } 1402c81addc9SOleg Nesterov 1403b18b6a9cSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 1404f06febc9SFrank Mayhar /* 1405f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1406f06febc9SFrank Mayhar */ 1407f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1408f06febc9SFrank Mayhar { 140978d7d407SJiri Slaby unsigned long cpu_limit; 141078d7d407SJiri Slaby 1411316c1608SJason Low cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 141278d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 1413ebd7e7fcSFrederic Weisbecker sig->cputime_expires.prof_exp = cpu_limit * NSEC_PER_SEC; 1414d5c373ebSJason Low sig->cputimer.running = true; 14156279a751SOleg Nesterov } 14166279a751SOleg Nesterov 1417f06febc9SFrank Mayhar /* The timer lists. */ 1418f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1419f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1420f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1421f06febc9SFrank Mayhar } 1422b18b6a9cSNicolas Pitre #else 1423b18b6a9cSNicolas Pitre static inline void posix_cpu_timers_init_group(struct signal_struct *sig) { } 1424b18b6a9cSNicolas Pitre #endif 1425f06febc9SFrank Mayhar 1426a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 14271da177e4SLinus Torvalds { 14281da177e4SLinus Torvalds struct signal_struct *sig; 14291da177e4SLinus Torvalds 14304ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1431490dea45SPeter Zijlstra return 0; 14326279a751SOleg Nesterov 1433a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 14341da177e4SLinus Torvalds tsk->signal = sig; 14351da177e4SLinus Torvalds if (!sig) 14361da177e4SLinus Torvalds return -ENOMEM; 14371da177e4SLinus Torvalds 1438b3ac022cSOleg Nesterov sig->nr_threads = 1; 14391da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1440b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 14410c740d0aSOleg Nesterov 14420c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 14430c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 14440c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 14450c740d0aSOleg Nesterov 14461da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1447db51aeccSOleg Nesterov sig->curr_target = tsk; 14481da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 1449e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 14509d7fb042SPeter Zijlstra prev_cputime_init(&sig->prev_cputime); 14511da177e4SLinus Torvalds 1452baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 1453b18b6a9cSNicolas Pitre INIT_LIST_HEAD(&sig->posix_timers); 1454c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 14551da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 1456baa73d9eSNicolas Pitre #endif 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds task_lock(current->group_leader); 14591da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 14601da177e4SLinus Torvalds task_unlock(current->group_leader); 14611da177e4SLinus Torvalds 14626279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 14636279a751SOleg Nesterov 1464522ed776SMiloslav Trmac tty_audit_fork(sig); 14655091faa4SMike Galbraith sched_autogroup_fork(sig); 1466522ed776SMiloslav Trmac 1467a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1468dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 146928b83c51SKOSAKI Motohiro 14709b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 14719b1bf12dSKOSAKI Motohiro 14721da177e4SLinus Torvalds return 0; 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds 1475dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1476dbd95212SKees Cook { 1477dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1478dbd95212SKees Cook /* 1479dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1480dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1481dbd95212SKees Cook * needed because this new task is not yet running and cannot 1482dbd95212SKees Cook * be racing exec. 1483dbd95212SKees Cook */ 148469f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1485dbd95212SKees Cook 1486dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1487dbd95212SKees Cook get_seccomp_filter(current); 1488dbd95212SKees Cook p->seccomp = current->seccomp; 1489dbd95212SKees Cook 1490dbd95212SKees Cook /* 1491dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1492dbd95212SKees Cook * between the task_struct being duplicated and holding the 1493dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1494dbd95212SKees Cook */ 1495dbd95212SKees Cook if (task_no_new_privs(current)) 1496dbd95212SKees Cook task_set_no_new_privs(p); 1497dbd95212SKees Cook 1498dbd95212SKees Cook /* 1499dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1500dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1501dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1502dbd95212SKees Cook */ 1503dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1504dbd95212SKees Cook set_tsk_thread_flag(p, TIF_SECCOMP); 1505dbd95212SKees Cook #endif 1506dbd95212SKees Cook } 1507dbd95212SKees Cook 150817da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 15091da177e4SLinus Torvalds { 15101da177e4SLinus Torvalds current->clear_child_tid = tidptr; 15111da177e4SLinus Torvalds 1512b488893aSPavel Emelyanov return task_pid_vnr(current); 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds 1515a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 151623f78d4aSIngo Molnar { 15171d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1518e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1519a23ba907SDavidlohr Bueso p->pi_waiters = RB_ROOT_CACHED; 1520e96a7705SXunlei Pang p->pi_top_task = NULL; 152123f78d4aSIngo Molnar p->pi_blocked_on = NULL; 152223f78d4aSIngo Molnar #endif 152323f78d4aSIngo Molnar } 152423f78d4aSIngo Molnar 1525b18b6a9cSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 15261da177e4SLinus Torvalds /* 1527f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1528f06febc9SFrank Mayhar */ 1529f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1530f06febc9SFrank Mayhar { 153164861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 153264861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1533f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1534f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1535f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1536f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1537f06febc9SFrank Mayhar } 1538b18b6a9cSNicolas Pitre #else 1539b18b6a9cSNicolas Pitre static inline void posix_cpu_timers_init(struct task_struct *tsk) { } 1540b18b6a9cSNicolas Pitre #endif 1541f06febc9SFrank Mayhar 154281907739SOleg Nesterov static inline void 154381907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 154481907739SOleg Nesterov { 154581907739SOleg Nesterov task->pids[type].pid = pid; 154681907739SOleg Nesterov } 154781907739SOleg Nesterov 15486bfbaa51SIngo Molnar static inline void rcu_copy_process(struct task_struct *p) 15496bfbaa51SIngo Molnar { 15506bfbaa51SIngo Molnar #ifdef CONFIG_PREEMPT_RCU 15516bfbaa51SIngo Molnar p->rcu_read_lock_nesting = 0; 15526bfbaa51SIngo Molnar p->rcu_read_unlock_special.s = 0; 15536bfbaa51SIngo Molnar p->rcu_blocked_node = NULL; 15546bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_node_entry); 15556bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_PREEMPT_RCU */ 15566bfbaa51SIngo Molnar #ifdef CONFIG_TASKS_RCU 15576bfbaa51SIngo Molnar p->rcu_tasks_holdout = false; 15586bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); 15596bfbaa51SIngo Molnar p->rcu_tasks_idle_cpu = -1; 15606bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_TASKS_RCU */ 15616bfbaa51SIngo Molnar } 15626bfbaa51SIngo Molnar 1563f06febc9SFrank Mayhar /* 15641da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 15651da177e4SLinus Torvalds * but does not actually start it yet. 15661da177e4SLinus Torvalds * 15671da177e4SLinus Torvalds * It copies the registers, and all the appropriate 15681da177e4SLinus Torvalds * parts of the process environment (as per the clone 15691da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 15701da177e4SLinus Torvalds */ 15710766f788SEmese Revfy static __latent_entropy struct task_struct *copy_process( 15720766f788SEmese Revfy unsigned long clone_flags, 15731da177e4SLinus Torvalds unsigned long stack_start, 15741da177e4SLinus Torvalds unsigned long stack_size, 15751da177e4SLinus Torvalds int __user *child_tidptr, 157609a05394SRoland McGrath struct pid *pid, 15773033f14aSJosh Triplett int trace, 1578725fc629SAndi Kleen unsigned long tls, 1579725fc629SAndi Kleen int node) 15801da177e4SLinus Torvalds { 15811da177e4SLinus Torvalds int retval; 1582a24efe62SMariusz Kozlowski struct task_struct *p; 15831da177e4SLinus Torvalds 1584667b6094SMarcos Paulo de Souza /* 1585667b6094SMarcos Paulo de Souza * Don't allow sharing the root directory with processes in a different 1586667b6094SMarcos Paulo de Souza * namespace 1587667b6094SMarcos Paulo de Souza */ 15881da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 15891da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 15901da177e4SLinus Torvalds 1591e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1592e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1593e66eded8SEric W. Biederman 15941da177e4SLinus Torvalds /* 15951da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 15961da177e4SLinus Torvalds * can only be started up within the thread group. 15971da177e4SLinus Torvalds */ 15981da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 15991da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds /* 16021da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 16031da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 16041da177e4SLinus Torvalds * for various simplifications in other code. 16051da177e4SLinus Torvalds */ 16061da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 16071da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 16081da177e4SLinus Torvalds 1609123be07bSSukadev Bhattiprolu /* 1610123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1611123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1612123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1613123be07bSSukadev Bhattiprolu * from creating siblings. 1614123be07bSSukadev Bhattiprolu */ 1615123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1616123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1617123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1618123be07bSSukadev Bhattiprolu 16198382fcacSEric W. Biederman /* 162040a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 1621faf00da5SEric W. Biederman * do not allow it to share a thread group with the forking task. 16228382fcacSEric W. Biederman */ 1623faf00da5SEric W. Biederman if (clone_flags & CLONE_THREAD) { 162440a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 162540a0d32dSOleg Nesterov (task_active_pid_ns(current) != 1626c2b1df2eSAndy Lutomirski current->nsproxy->pid_ns_for_children)) 16278382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 162840a0d32dSOleg Nesterov } 16298382fcacSEric W. Biederman 16301da177e4SLinus Torvalds retval = -ENOMEM; 1631725fc629SAndi Kleen p = dup_task_struct(current, node); 16321da177e4SLinus Torvalds if (!p) 16331da177e4SLinus Torvalds goto fork_out; 16341da177e4SLinus Torvalds 16354d6501dcSVegard Nossum /* 16364d6501dcSVegard Nossum * This _must_ happen before we call free_task(), i.e. before we jump 16374d6501dcSVegard Nossum * to any of the bad_fork_* labels. This is to avoid freeing 16384d6501dcSVegard Nossum * p->set_child_tid which is (ab)used as a kthread's data pointer for 16394d6501dcSVegard Nossum * kernel threads (PF_KTHREAD). 16404d6501dcSVegard Nossum */ 16414d6501dcSVegard Nossum p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 16424d6501dcSVegard Nossum /* 16434d6501dcSVegard Nossum * Clear TID on mm_release()? 16444d6501dcSVegard Nossum */ 16454d6501dcSVegard Nossum p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 16464d6501dcSVegard Nossum 1647f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1648f7e8b616SSteven Rostedt 1649bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1650bea493a0SPeter Zijlstra 1651d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1652de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1653de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1654de30a2b3SIngo Molnar #endif 16551da177e4SLinus Torvalds retval = -EAGAIN; 16563b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 165778d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1658b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1659b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 16601da177e4SLinus Torvalds goto bad_fork_free; 16611da177e4SLinus Torvalds } 166272fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 16631da177e4SLinus Torvalds 1664f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1665f1752eecSDavid Howells if (retval < 0) 1666f1752eecSDavid Howells goto bad_fork_free; 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds /* 16691da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 16701da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 16711da177e4SLinus Torvalds * to stop root fork bombs. 16721da177e4SLinus Torvalds */ 167304ec93feSLi Zefan retval = -EAGAIN; 16741da177e4SLinus Torvalds if (nr_threads >= max_threads) 16751da177e4SLinus Torvalds goto bad_fork_cleanup_count; 16761da177e4SLinus Torvalds 1677ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1678c1de45caSPeter Zijlstra p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE); 1679514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 16801da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 16811da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1682f41d911fSPaul E. McKenney rcu_copy_process(p); 16831da177e4SLinus Torvalds p->vfork_done = NULL; 16841da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds init_sigpending(&p->pending); 16871da177e4SLinus Torvalds 168864861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 168940565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 169064861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 169140565b5aSStanislaw Gruszka #endif 16929d7fb042SPeter Zijlstra prev_cputime_init(&p->prev_cputime); 16939d7fb042SPeter Zijlstra 16946a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 1695bac5b6b6SFrederic Weisbecker seqcount_init(&p->vtime.seqcount); 1696bac5b6b6SFrederic Weisbecker p->vtime.starttime = 0; 1697bac5b6b6SFrederic Weisbecker p->vtime.state = VTIME_INACTIVE; 16986a61671bSFrederic Weisbecker #endif 16996a61671bSFrederic Weisbecker 1700a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1701a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1702a3a2e76cSKAMEZAWA Hiroyuki #endif 1703172ba844SBalbir Singh 17046976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 17056976675dSArjan van de Ven 17065995477aSAndrea Righi task_io_accounting_init(&p->ioac); 17071da177e4SLinus Torvalds acct_clear_integrals(p); 17081da177e4SLinus Torvalds 1709f06febc9SFrank Mayhar posix_cpu_timers_init(p); 17101da177e4SLinus Torvalds 1711ccbf62d8SThomas Gleixner p->start_time = ktime_get_ns(); 171257e0be04SThomas Gleixner p->real_start_time = ktime_get_boot_ns(); 17131da177e4SLinus Torvalds p->io_context = NULL; 17141da177e4SLinus Torvalds p->audit_context = NULL; 1715b4f48b63SPaul Menage cgroup_fork(p); 17161da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1717846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 17181da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 17191da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 17201da177e4SLinus Torvalds p->mempolicy = NULL; 1721e8604cb4SLi Zefan goto bad_fork_cleanup_threadgroup_lock; 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds #endif 1724778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1725778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1726778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1727cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1728778d3b0fSMichal Hocko #endif 1729de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1730de30a2b3SIngo Molnar p->irq_events = 0; 1731de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1732de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1733de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1734de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1735de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1736de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1737de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1738de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1739de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1740de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1741de30a2b3SIngo Molnar p->hardirq_context = 0; 1742de30a2b3SIngo Molnar p->softirq_context = 0; 1743de30a2b3SIngo Molnar #endif 17448bcbde54SDavid Hildenbrand 17458bcbde54SDavid Hildenbrand p->pagefault_disabled = 0; 17468bcbde54SDavid Hildenbrand 1747fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1748fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1749fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1750fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1751b09be676SByungchul Park lockdep_init_task(p); 1752fbb9ce95SIngo Molnar #endif 17531da177e4SLinus Torvalds 1754408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1755408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1756408894eeSIngo Molnar #endif 1757cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 1758cafe5635SKent Overstreet p->sequential_io = 0; 1759cafe5635SKent Overstreet p->sequential_io_avg = 0; 1760cafe5635SKent Overstreet #endif 17610f481406SMarkus Metzger 17623c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 1763aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 1764aab03e05SDario Faggioli if (retval) 1765aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 17666ab423e0SPeter Zijlstra 1767cdd6c482SIngo Molnar retval = perf_event_init_task(p); 17686ab423e0SPeter Zijlstra if (retval) 17696ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1770fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1771fb0a685cSDaniel Rebelo de Oliveira if (retval) 17726c72e350SPeter Zijlstra goto bad_fork_cleanup_perf; 17731da177e4SLinus Torvalds /* copy all the process information */ 1774ab602f79SJack Miller shm_init_task(p); 1775e4e55b47STetsuo Handa retval = security_task_alloc(p, clone_flags); 1776fb0a685cSDaniel Rebelo de Oliveira if (retval) 17771da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1778e4e55b47STetsuo Handa retval = copy_semundo(clone_flags, p); 1779e4e55b47STetsuo Handa if (retval) 1780e4e55b47STetsuo Handa goto bad_fork_cleanup_security; 1781fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1782fb0a685cSDaniel Rebelo de Oliveira if (retval) 17831da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1784fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1785fb0a685cSDaniel Rebelo de Oliveira if (retval) 17861da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1787fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1788fb0a685cSDaniel Rebelo de Oliveira if (retval) 17891da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1790fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1791fb0a685cSDaniel Rebelo de Oliveira if (retval) 17921da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1793fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1794fb0a685cSDaniel Rebelo de Oliveira if (retval) 17951da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1796fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1797fb0a685cSDaniel Rebelo de Oliveira if (retval) 1798d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1799fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1800fb0a685cSDaniel Rebelo de Oliveira if (retval) 1801fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 18023033f14aSJosh Triplett retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls); 18031da177e4SLinus Torvalds if (retval) 1804fd0928dfSJens Axboe goto bad_fork_cleanup_io; 18051da177e4SLinus Torvalds 1806425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1807c2b1df2eSAndy Lutomirski pid = alloc_pid(p->nsproxy->pid_ns_for_children); 180835f71bc0SMichal Hocko if (IS_ERR(pid)) { 180935f71bc0SMichal Hocko retval = PTR_ERR(pid); 18100740aa5fSJiri Slaby goto bad_fork_cleanup_thread; 1811425fb2b4SPavel Emelyanov } 181235f71bc0SMichal Hocko } 1813425fb2b4SPavel Emelyanov 181473c10101SJens Axboe #ifdef CONFIG_BLOCK 181573c10101SJens Axboe p->plug = NULL; 181673c10101SJens Axboe #endif 181742b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 18188f17d3a5SIngo Molnar p->robust_list = NULL; 18198f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 18208f17d3a5SIngo Molnar p->compat_robust_list = NULL; 18218f17d3a5SIngo Molnar #endif 1822c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1823c87e2837SIngo Molnar p->pi_state_cache = NULL; 182442b2dd0aSAlexey Dobriyan #endif 18251da177e4SLinus Torvalds /* 1826f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1827f9a3879aSGOTO Masanori */ 1828f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 18292a742138SStas Sergeev sas_ss_reset(p); 1830f9a3879aSGOTO Masanori 1831f9a3879aSGOTO Masanori /* 18326580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 18336580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 18341da177e4SLinus Torvalds */ 18356580807dSOleg Nesterov user_disable_single_step(p); 18361da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1837ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1838ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1839ed75e8d5SLaurent Vivier #endif 18409745512cSArjan van de Ven clear_all_latency_tracing(p); 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds /* ok, now we should be set up.. */ 184318c830dfSOleg Nesterov p->pid = pid_nr(pid); 184418c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 18455f8aadd8SOleg Nesterov p->exit_signal = -1; 184618c830dfSOleg Nesterov p->group_leader = current->group_leader; 184718c830dfSOleg Nesterov p->tgid = current->tgid; 184818c830dfSOleg Nesterov } else { 184918c830dfSOleg Nesterov if (clone_flags & CLONE_PARENT) 18505f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 18515f8aadd8SOleg Nesterov else 18525f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 185318c830dfSOleg Nesterov p->group_leader = p; 185418c830dfSOleg Nesterov p->tgid = p->pid; 185518c830dfSOleg Nesterov } 18565f8aadd8SOleg Nesterov 18579d823e8fSWu Fengguang p->nr_dirtied = 0; 18589d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 185983712358SWu Fengguang p->dirty_paused_when = 0; 18609d823e8fSWu Fengguang 1861bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 186247e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 1863158e1645SAl Viro p->task_works = NULL; 18641da177e4SLinus Torvalds 1865780de9ddSIngo Molnar cgroup_threadgroup_change_begin(current); 186618c830dfSOleg Nesterov /* 18677e47682eSAleksa Sarai * Ensure that the cgroup subsystem policies allow the new process to be 18687e47682eSAleksa Sarai * forked. It should be noted the the new process's css_set can be changed 18697e47682eSAleksa Sarai * between here and cgroup_post_fork() if an organisation operation is in 18707e47682eSAleksa Sarai * progress. 18717e47682eSAleksa Sarai */ 1872b53202e6SOleg Nesterov retval = cgroup_can_fork(p); 18737e47682eSAleksa Sarai if (retval) 18747e47682eSAleksa Sarai goto bad_fork_free_pid; 18757e47682eSAleksa Sarai 18767e47682eSAleksa Sarai /* 187718c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 187818c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 187918c830dfSOleg Nesterov */ 18801da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 18832d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 18841da177e4SLinus Torvalds p->real_parent = current->real_parent; 18852d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 18862d5516cbSOleg Nesterov } else { 18871da177e4SLinus Torvalds p->real_parent = current; 18882d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 18892d5516cbSOleg Nesterov } 18901da177e4SLinus Torvalds 1891d83a7cb3SJosh Poimboeuf klp_copy_process(p); 1892d83a7cb3SJosh Poimboeuf 18931da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 18944a2c7a78SOleg Nesterov 18954a2c7a78SOleg Nesterov /* 1896dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 1897dbd95212SKees Cook * before holding sighand lock. 1898dbd95212SKees Cook */ 1899dbd95212SKees Cook copy_seccomp(p); 1900dbd95212SKees Cook 1901dbd95212SKees Cook /* 19024a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 19034a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 19044a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 19054a2c7a78SOleg Nesterov * it's process group. 19064a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 19074a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 19084a2c7a78SOleg Nesterov */ 19094a2c7a78SOleg Nesterov recalc_sigpending(); 19104a2c7a78SOleg Nesterov if (signal_pending(current)) { 19114a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 19127e47682eSAleksa Sarai goto bad_fork_cancel_cgroup; 19134a2c7a78SOleg Nesterov } 1914e8cfbc24SGargi Sharma if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { 19153fd37226SKirill Tkhai retval = -ENOMEM; 19163fd37226SKirill Tkhai goto bad_fork_cancel_cgroup; 19173fd37226SKirill Tkhai } 19184a2c7a78SOleg Nesterov 191973b9ebfeSOleg Nesterov if (likely(p->pid)) { 19204b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 19211da177e4SLinus Torvalds 192281907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 19231da177e4SLinus Torvalds if (thread_group_leader(p)) { 192481907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 192581907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 192681907739SOleg Nesterov 19271c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 192817cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 19291c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 19301c4042c2SEric W. Biederman } 19315cd17569SEric W. Biederman 1932fea9d175SOleg Nesterov p->signal->leader_pid = pid; 19339c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 1934749860ceSPavel Tikhomirov /* 1935749860ceSPavel Tikhomirov * Inherit has_child_subreaper flag under the same 1936749860ceSPavel Tikhomirov * tasklist_lock with adding child to the process tree 1937749860ceSPavel Tikhomirov * for propagate_has_child_subreaper optimization. 1938749860ceSPavel Tikhomirov */ 1939749860ceSPavel Tikhomirov p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 1940749860ceSPavel Tikhomirov p->real_parent->signal->is_child_subreaper; 19419cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 19425e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 194381907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 194481907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 1945909ea964SChristoph Lameter __this_cpu_inc(process_counts); 194680628ca0SOleg Nesterov } else { 194780628ca0SOleg Nesterov current->signal->nr_threads++; 194880628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 194980628ca0SOleg Nesterov atomic_inc(¤t->signal->sigcnt); 195080628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 195180628ca0SOleg Nesterov &p->group_leader->thread_group); 19520c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 19530c740d0aSOleg Nesterov &p->signal->thread_head); 19541da177e4SLinus Torvalds } 195581907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 19561da177e4SLinus Torvalds nr_threads++; 195773b9ebfeSOleg Nesterov } 195873b9ebfeSOleg Nesterov 19591da177e4SLinus Torvalds total_forks++; 19603f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 19614af4206bSOleg Nesterov syscall_tracepoint_update(p); 19621da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 19634af4206bSOleg Nesterov 1964c13cf856SAndrew Morton proc_fork_connector(p); 1965b53202e6SOleg Nesterov cgroup_post_fork(p); 1966780de9ddSIngo Molnar cgroup_threadgroup_change_end(current); 1967cdd6c482SIngo Molnar perf_event_fork(p); 196843d2b113SKAMEZAWA Hiroyuki 196943d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 19703ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 197143d2b113SKAMEZAWA Hiroyuki 19721da177e4SLinus Torvalds return p; 19731da177e4SLinus Torvalds 19747e47682eSAleksa Sarai bad_fork_cancel_cgroup: 19753fd37226SKirill Tkhai spin_unlock(¤t->sighand->siglock); 19763fd37226SKirill Tkhai write_unlock_irq(&tasklist_lock); 1977b53202e6SOleg Nesterov cgroup_cancel_fork(p); 1978425fb2b4SPavel Emelyanov bad_fork_free_pid: 1979780de9ddSIngo Molnar cgroup_threadgroup_change_end(current); 1980425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1981425fb2b4SPavel Emelyanov free_pid(pid); 19820740aa5fSJiri Slaby bad_fork_cleanup_thread: 19830740aa5fSJiri Slaby exit_thread(p); 1984fd0928dfSJens Axboe bad_fork_cleanup_io: 1985b69f2292SLouis Rilling if (p->io_context) 1986b69f2292SLouis Rilling exit_io_context(p); 1987ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1988444f378bSLinus Torvalds exit_task_namespaces(p); 19891da177e4SLinus Torvalds bad_fork_cleanup_mm: 1990c9f01245SDavid Rientjes if (p->mm) 19911da177e4SLinus Torvalds mmput(p->mm); 19921da177e4SLinus Torvalds bad_fork_cleanup_signal: 19934ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 19941c5354deSMike Galbraith free_signal_struct(p->signal); 19951da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1996a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 19971da177e4SLinus Torvalds bad_fork_cleanup_fs: 19981da177e4SLinus Torvalds exit_fs(p); /* blocking */ 19991da177e4SLinus Torvalds bad_fork_cleanup_files: 20001da177e4SLinus Torvalds exit_files(p); /* blocking */ 20011da177e4SLinus Torvalds bad_fork_cleanup_semundo: 20021da177e4SLinus Torvalds exit_sem(p); 2003e4e55b47STetsuo Handa bad_fork_cleanup_security: 2004e4e55b47STetsuo Handa security_task_free(p); 20051da177e4SLinus Torvalds bad_fork_cleanup_audit: 20061da177e4SLinus Torvalds audit_free(p); 20076c72e350SPeter Zijlstra bad_fork_cleanup_perf: 2008cdd6c482SIngo Molnar perf_event_free_task(p); 20096c72e350SPeter Zijlstra bad_fork_cleanup_policy: 2010b09be676SByungchul Park lockdep_free_task(p); 20111da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2012f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 2013e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock: 20141da177e4SLinus Torvalds #endif 201535df17c5SShailabh Nagar delayacct_tsk_free(p); 20161da177e4SLinus Torvalds bad_fork_cleanup_count: 2017d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 2018e0e81739SDavid Howells exit_creds(p); 20191da177e4SLinus Torvalds bad_fork_free: 2020405c0759SAndy Lutomirski p->state = TASK_DEAD; 202168f24b08SAndy Lutomirski put_task_stack(p); 20221da177e4SLinus Torvalds free_task(p); 2023fe7d37d1SOleg Nesterov fork_out: 2024fe7d37d1SOleg Nesterov return ERR_PTR(retval); 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 2027f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 2028f106eee1SOleg Nesterov { 2029f106eee1SOleg Nesterov enum pid_type type; 2030f106eee1SOleg Nesterov 2031f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 2032f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 2033f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 2034f106eee1SOleg Nesterov } 2035f106eee1SOleg Nesterov } 2036f106eee1SOleg Nesterov 20370db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 20381da177e4SLinus Torvalds { 203936c8b586SIngo Molnar struct task_struct *task; 2040725fc629SAndi Kleen task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0, 2041725fc629SAndi Kleen cpu_to_node(cpu)); 2042f106eee1SOleg Nesterov if (!IS_ERR(task)) { 2043f106eee1SOleg Nesterov init_idle_pids(task->pids); 20441da177e4SLinus Torvalds init_idle(task, cpu); 2045f106eee1SOleg Nesterov } 204673b9ebfeSOleg Nesterov 20471da177e4SLinus Torvalds return task; 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds /* 20511da177e4SLinus Torvalds * Ok, this is the main fork-routine. 20521da177e4SLinus Torvalds * 20531da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 20541da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 20551da177e4SLinus Torvalds */ 20563033f14aSJosh Triplett long _do_fork(unsigned long clone_flags, 20571da177e4SLinus Torvalds unsigned long stack_start, 20581da177e4SLinus Torvalds unsigned long stack_size, 20591da177e4SLinus Torvalds int __user *parent_tidptr, 20603033f14aSJosh Triplett int __user *child_tidptr, 20613033f14aSJosh Triplett unsigned long tls) 20621da177e4SLinus Torvalds { 20639f5325aaSMarcos Paulo de Souza struct completion vfork; 20649f5325aaSMarcos Paulo de Souza struct pid *pid; 20651da177e4SLinus Torvalds struct task_struct *p; 20661da177e4SLinus Torvalds int trace = 0; 206792476d7fSEric W. Biederman long nr; 20681da177e4SLinus Torvalds 2069bdff746aSAndrew Morton /* 20704b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 20714b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 20724b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 20734b9d33e6STejun Heo * for the type of forking is enabled. 207409a05394SRoland McGrath */ 2075e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 20764b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 20774b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 20784b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 20794b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 20804b9d33e6STejun Heo else 20814b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 20824b9d33e6STejun Heo 20834b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 20844b9d33e6STejun Heo trace = 0; 20854b9d33e6STejun Heo } 20861da177e4SLinus Torvalds 208762e791c1SAl Viro p = copy_process(clone_flags, stack_start, stack_size, 2088725fc629SAndi Kleen child_tidptr, NULL, trace, tls, NUMA_NO_NODE); 208938addce8SEmese Revfy add_latent_entropy(); 20909f5325aaSMarcos Paulo de Souza 20919f5325aaSMarcos Paulo de Souza if (IS_ERR(p)) 20929f5325aaSMarcos Paulo de Souza return PTR_ERR(p); 20939f5325aaSMarcos Paulo de Souza 20941da177e4SLinus Torvalds /* 20951da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 20961da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 20971da177e4SLinus Torvalds */ 20980a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 20990a16b607SMathieu Desnoyers 21004e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 21014e52365fSMatthew Dempsky nr = pid_vnr(pid); 210230e49c26SPavel Emelyanov 210330e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 210430e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 2105a6f5e063SSukadev Bhattiprolu 21061da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 21071da177e4SLinus Torvalds p->vfork_done = &vfork; 21081da177e4SLinus Torvalds init_completion(&vfork); 2109d68b46feSOleg Nesterov get_task_struct(p); 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds 21123e51e3edSSamir Bellabes wake_up_new_task(p); 21131da177e4SLinus Torvalds 21144b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 21154b9d33e6STejun Heo if (unlikely(trace)) 21164e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 211709a05394SRoland McGrath 21181da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 2119d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 21204e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 21219f59ce5dSChuck Ebbert } 21224e52365fSMatthew Dempsky 21234e52365fSMatthew Dempsky put_pid(pid); 212492476d7fSEric W. Biederman return nr; 21251da177e4SLinus Torvalds } 21261da177e4SLinus Torvalds 21273033f14aSJosh Triplett #ifndef CONFIG_HAVE_COPY_THREAD_TLS 21283033f14aSJosh Triplett /* For compatibility with architectures that call do_fork directly rather than 21293033f14aSJosh Triplett * using the syscall entry points below. */ 21303033f14aSJosh Triplett long do_fork(unsigned long clone_flags, 21313033f14aSJosh Triplett unsigned long stack_start, 21323033f14aSJosh Triplett unsigned long stack_size, 21333033f14aSJosh Triplett int __user *parent_tidptr, 21343033f14aSJosh Triplett int __user *child_tidptr) 21353033f14aSJosh Triplett { 21363033f14aSJosh Triplett return _do_fork(clone_flags, stack_start, stack_size, 21373033f14aSJosh Triplett parent_tidptr, child_tidptr, 0); 21383033f14aSJosh Triplett } 21393033f14aSJosh Triplett #endif 21403033f14aSJosh Triplett 21412aa3a7f8SAl Viro /* 21422aa3a7f8SAl Viro * Create a kernel thread. 21432aa3a7f8SAl Viro */ 21442aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 21452aa3a7f8SAl Viro { 21463033f14aSJosh Triplett return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, 21473033f14aSJosh Triplett (unsigned long)arg, NULL, NULL, 0); 21482aa3a7f8SAl Viro } 21492aa3a7f8SAl Viro 2150d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 2151d2125043SAl Viro SYSCALL_DEFINE0(fork) 2152d2125043SAl Viro { 2153d2125043SAl Viro #ifdef CONFIG_MMU 21543033f14aSJosh Triplett return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0); 2155d2125043SAl Viro #else 2156d2125043SAl Viro /* can not support in nommu mode */ 21575d59e182SDaeseok Youn return -EINVAL; 2158d2125043SAl Viro #endif 2159d2125043SAl Viro } 2160d2125043SAl Viro #endif 2161d2125043SAl Viro 2162d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 2163d2125043SAl Viro SYSCALL_DEFINE0(vfork) 2164d2125043SAl Viro { 21653033f14aSJosh Triplett return _do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 21663033f14aSJosh Triplett 0, NULL, NULL, 0); 2167d2125043SAl Viro } 2168d2125043SAl Viro #endif 2169d2125043SAl Viro 2170d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 2171d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 2172d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2173d2125043SAl Viro int __user *, parent_tidptr, 21743033f14aSJosh Triplett unsigned long, tls, 2175d2125043SAl Viro int __user *, child_tidptr) 2176d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 2177d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 2178d2125043SAl Viro int __user *, parent_tidptr, 2179d2125043SAl Viro int __user *, child_tidptr, 21803033f14aSJosh Triplett unsigned long, tls) 2181dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 2182dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 2183dfa9771aSMichal Simek int, stack_size, 2184dfa9771aSMichal Simek int __user *, parent_tidptr, 2185dfa9771aSMichal Simek int __user *, child_tidptr, 21863033f14aSJosh Triplett unsigned long, tls) 2187d2125043SAl Viro #else 2188d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2189d2125043SAl Viro int __user *, parent_tidptr, 2190d2125043SAl Viro int __user *, child_tidptr, 21913033f14aSJosh Triplett unsigned long, tls) 2192d2125043SAl Viro #endif 2193d2125043SAl Viro { 21943033f14aSJosh Triplett return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls); 2195d2125043SAl Viro } 2196d2125043SAl Viro #endif 2197d2125043SAl Viro 21980f1b92cbSOleg Nesterov void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) 21990f1b92cbSOleg Nesterov { 22000f1b92cbSOleg Nesterov struct task_struct *leader, *parent, *child; 22010f1b92cbSOleg Nesterov int res; 22020f1b92cbSOleg Nesterov 22030f1b92cbSOleg Nesterov read_lock(&tasklist_lock); 22040f1b92cbSOleg Nesterov leader = top = top->group_leader; 22050f1b92cbSOleg Nesterov down: 22060f1b92cbSOleg Nesterov for_each_thread(leader, parent) { 22070f1b92cbSOleg Nesterov list_for_each_entry(child, &parent->children, sibling) { 22080f1b92cbSOleg Nesterov res = visitor(child, data); 22090f1b92cbSOleg Nesterov if (res) { 22100f1b92cbSOleg Nesterov if (res < 0) 22110f1b92cbSOleg Nesterov goto out; 22120f1b92cbSOleg Nesterov leader = child; 22130f1b92cbSOleg Nesterov goto down; 22140f1b92cbSOleg Nesterov } 22150f1b92cbSOleg Nesterov up: 22160f1b92cbSOleg Nesterov ; 22170f1b92cbSOleg Nesterov } 22180f1b92cbSOleg Nesterov } 22190f1b92cbSOleg Nesterov 22200f1b92cbSOleg Nesterov if (leader != top) { 22210f1b92cbSOleg Nesterov child = leader; 22220f1b92cbSOleg Nesterov parent = child->real_parent; 22230f1b92cbSOleg Nesterov leader = parent->group_leader; 22240f1b92cbSOleg Nesterov goto up; 22250f1b92cbSOleg Nesterov } 22260f1b92cbSOleg Nesterov out: 22270f1b92cbSOleg Nesterov read_unlock(&tasklist_lock); 22280f1b92cbSOleg Nesterov } 22290f1b92cbSOleg Nesterov 22305fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 22315fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 22325fd63b30SRavikiran G Thirumalai #endif 22335fd63b30SRavikiran G Thirumalai 223451cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 2235aa1757f9SOleg Nesterov { 2236aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 2237aa1757f9SOleg Nesterov 2238aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 2239b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 2240fba2afaaSDavide Libenzi } 2241aa1757f9SOleg Nesterov 22421da177e4SLinus Torvalds void __init proc_caches_init(void) 22431da177e4SLinus Torvalds { 22441da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 22451da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 22465f0d5a3aSPaul E. McKenney SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| 224775f296d9SLevin, Alexander (Sasha Levin) SLAB_ACCOUNT, sighand_ctor); 22481da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 22491da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 225075f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 22515d097056SVladimir Davydov NULL); 22521da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 22531da177e4SLinus Torvalds sizeof(struct files_struct), 0, 225475f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 22555d097056SVladimir Davydov NULL); 22561da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 22571da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 225875f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 22595d097056SVladimir Davydov NULL); 22606345d24dSLinus Torvalds /* 22616345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 22626345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 22636345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 22646345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 22656345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 22666345d24dSLinus Torvalds */ 226707dcd7feSDavid Windsor mm_cachep = kmem_cache_create_usercopy("mm_struct", 22685fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 226975f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 227007dcd7feSDavid Windsor offsetof(struct mm_struct, saved_auxv), 227107dcd7feSDavid Windsor sizeof_field(struct mm_struct, saved_auxv), 22725d097056SVladimir Davydov NULL); 22735d097056SVladimir Davydov vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 22748feae131SDavid Howells mmap_init(); 227566577193SAl Viro nsproxy_cache_init(); 22761da177e4SLinus Torvalds } 2277cf2e340fSJANAK DESAI 2278cf2e340fSJANAK DESAI /* 22799bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 2280cf2e340fSJANAK DESAI */ 22819bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 2282cf2e340fSJANAK DESAI { 22839bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 22849bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 228550804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 2286a79a908fSAditya Kali CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP)) 2287cf2e340fSJANAK DESAI return -EINVAL; 22889bfb23fcSOleg Nesterov /* 228912c641abSEric W. Biederman * Not implemented, but pretend it works if there is nothing 229012c641abSEric W. Biederman * to unshare. Note that unsharing the address space or the 229112c641abSEric W. Biederman * signal handlers also need to unshare the signal queues (aka 229212c641abSEric W. Biederman * CLONE_THREAD). 22939bfb23fcSOleg Nesterov */ 22949bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 229512c641abSEric W. Biederman if (!thread_group_empty(current)) 229612c641abSEric W. Biederman return -EINVAL; 229712c641abSEric W. Biederman } 229812c641abSEric W. Biederman if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 229912c641abSEric W. Biederman if (atomic_read(¤t->sighand->count) > 1) 230012c641abSEric W. Biederman return -EINVAL; 230112c641abSEric W. Biederman } 230212c641abSEric W. Biederman if (unshare_flags & CLONE_VM) { 230312c641abSEric W. Biederman if (!current_is_single_threaded()) 23049bfb23fcSOleg Nesterov return -EINVAL; 23059bfb23fcSOleg Nesterov } 2306cf2e340fSJANAK DESAI 2307cf2e340fSJANAK DESAI return 0; 2308cf2e340fSJANAK DESAI } 2309cf2e340fSJANAK DESAI 2310cf2e340fSJANAK DESAI /* 231199d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 2312cf2e340fSJANAK DESAI */ 2313cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 2314cf2e340fSJANAK DESAI { 2315cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 2316cf2e340fSJANAK DESAI 2317498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 2318498052bbSAl Viro return 0; 2319498052bbSAl Viro 2320498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 2321498052bbSAl Viro if (fs->users == 1) 2322498052bbSAl Viro return 0; 2323498052bbSAl Viro 2324498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 232599d1419dSJANAK DESAI if (!*new_fsp) 232699d1419dSJANAK DESAI return -ENOMEM; 2327cf2e340fSJANAK DESAI 2328cf2e340fSJANAK DESAI return 0; 2329cf2e340fSJANAK DESAI } 2330cf2e340fSJANAK DESAI 2331cf2e340fSJANAK DESAI /* 2332a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 2333cf2e340fSJANAK DESAI */ 2334cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 2335cf2e340fSJANAK DESAI { 2336cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 2337a016f338SJANAK DESAI int error = 0; 2338cf2e340fSJANAK DESAI 2339cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 2340a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 2341a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 2342a016f338SJANAK DESAI if (!*new_fdp) 2343a016f338SJANAK DESAI return error; 2344a016f338SJANAK DESAI } 2345cf2e340fSJANAK DESAI 2346cf2e340fSJANAK DESAI return 0; 2347cf2e340fSJANAK DESAI } 2348cf2e340fSJANAK DESAI 2349cf2e340fSJANAK DESAI /* 2350cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 2351cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 2352cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 2353cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 2354cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 2355cf2e340fSJANAK DESAI * task_struct. 2356cf2e340fSJANAK DESAI */ 2357*9b32105eSDominik Brodowski int ksys_unshare(unsigned long unshare_flags) 2358cf2e340fSJANAK DESAI { 2359cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 2360cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 2361b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 2362cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 23639edff4abSManfred Spraul int do_sysvsem = 0; 23649bfb23fcSOleg Nesterov int err; 2365cf2e340fSJANAK DESAI 236650804fe3SEric W. Biederman /* 2367faf00da5SEric W. Biederman * If unsharing a user namespace must also unshare the thread group 2368faf00da5SEric W. Biederman * and unshare the filesystem root and working directories. 2369b2e0d987SEric W. Biederman */ 2370b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 2371e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 2372b2e0d987SEric W. Biederman /* 237350804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 237450804fe3SEric W. Biederman */ 237550804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 237650804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 23776013f67fSManfred Spraul /* 237812c641abSEric W. Biederman * If unsharing a signal handlers, must also unshare the signal queues. 237912c641abSEric W. Biederman */ 238012c641abSEric W. Biederman if (unshare_flags & CLONE_SIGHAND) 238112c641abSEric W. Biederman unshare_flags |= CLONE_THREAD; 238212c641abSEric W. Biederman /* 23839bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 23849bfb23fcSOleg Nesterov */ 23859bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 23869bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 238750804fe3SEric W. Biederman 238850804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 238950804fe3SEric W. Biederman if (err) 239050804fe3SEric W. Biederman goto bad_unshare_out; 23919bfb23fcSOleg Nesterov /* 23926013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 23936013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 23946013f67fSManfred Spraul * namespace are unreachable. 23956013f67fSManfred Spraul */ 23966013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 23979edff4abSManfred Spraul do_sysvsem = 1; 2398fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 2399fb0a685cSDaniel Rebelo de Oliveira if (err) 24009bfb23fcSOleg Nesterov goto bad_unshare_out; 2401fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 2402fb0a685cSDaniel Rebelo de Oliveira if (err) 24039bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 2404b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 2405fb0a685cSDaniel Rebelo de Oliveira if (err) 24069edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 2407b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 2408b2e0d987SEric W. Biederman new_cred, new_fs); 2409b2e0d987SEric W. Biederman if (err) 2410b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 2411cf2e340fSJANAK DESAI 2412b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 24139edff4abSManfred Spraul if (do_sysvsem) { 24149edff4abSManfred Spraul /* 24159edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 24169edff4abSManfred Spraul */ 24179edff4abSManfred Spraul exit_sem(current); 24189edff4abSManfred Spraul } 2419ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 2420ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 2421ab602f79SJack Miller exit_shm(current); 2422ab602f79SJack Miller shm_init_task(current); 2423ab602f79SJack Miller } 2424ab516013SSerge E. Hallyn 24256f977e6bSAlan Cox if (new_nsproxy) 2426cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 2427cf2e340fSJANAK DESAI 2428cf7b708cSPavel Emelyanov task_lock(current); 2429cf7b708cSPavel Emelyanov 2430cf2e340fSJANAK DESAI if (new_fs) { 2431cf2e340fSJANAK DESAI fs = current->fs; 24322a4419b5SNick Piggin spin_lock(&fs->lock); 2433cf2e340fSJANAK DESAI current->fs = new_fs; 2434498052bbSAl Viro if (--fs->users) 2435498052bbSAl Viro new_fs = NULL; 2436498052bbSAl Viro else 2437cf2e340fSJANAK DESAI new_fs = fs; 24382a4419b5SNick Piggin spin_unlock(&fs->lock); 2439cf2e340fSJANAK DESAI } 2440cf2e340fSJANAK DESAI 2441cf2e340fSJANAK DESAI if (new_fd) { 2442cf2e340fSJANAK DESAI fd = current->files; 2443cf2e340fSJANAK DESAI current->files = new_fd; 2444cf2e340fSJANAK DESAI new_fd = fd; 2445cf2e340fSJANAK DESAI } 2446cf2e340fSJANAK DESAI 2447cf2e340fSJANAK DESAI task_unlock(current); 2448b2e0d987SEric W. Biederman 2449b2e0d987SEric W. Biederman if (new_cred) { 2450b2e0d987SEric W. Biederman /* Install the new user namespace */ 2451b2e0d987SEric W. Biederman commit_creds(new_cred); 2452b2e0d987SEric W. Biederman new_cred = NULL; 2453b2e0d987SEric W. Biederman } 2454cf2e340fSJANAK DESAI } 2455cf2e340fSJANAK DESAI 2456e4222673SHari Bathini perf_event_namespaces(current); 2457e4222673SHari Bathini 2458b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 2459b2e0d987SEric W. Biederman if (new_cred) 2460b2e0d987SEric W. Biederman put_cred(new_cred); 2461cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 2462cf2e340fSJANAK DESAI if (new_fd) 2463cf2e340fSJANAK DESAI put_files_struct(new_fd); 2464cf2e340fSJANAK DESAI 2465cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 2466cf2e340fSJANAK DESAI if (new_fs) 2467498052bbSAl Viro free_fs_struct(new_fs); 2468cf2e340fSJANAK DESAI 2469cf2e340fSJANAK DESAI bad_unshare_out: 2470cf2e340fSJANAK DESAI return err; 2471cf2e340fSJANAK DESAI } 24723b125388SAl Viro 2473*9b32105eSDominik Brodowski SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 2474*9b32105eSDominik Brodowski { 2475*9b32105eSDominik Brodowski return ksys_unshare(unshare_flags); 2476*9b32105eSDominik Brodowski } 2477*9b32105eSDominik Brodowski 24783b125388SAl Viro /* 24793b125388SAl Viro * Helper to unshare the files of the current task. 24803b125388SAl Viro * We don't want to expose copy_files internals to 24813b125388SAl Viro * the exec layer of the kernel. 24823b125388SAl Viro */ 24833b125388SAl Viro 24843b125388SAl Viro int unshare_files(struct files_struct **displaced) 24853b125388SAl Viro { 24863b125388SAl Viro struct task_struct *task = current; 248750704516SAl Viro struct files_struct *copy = NULL; 24883b125388SAl Viro int error; 24893b125388SAl Viro 24903b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 24913b125388SAl Viro if (error || !copy) { 24923b125388SAl Viro *displaced = NULL; 24933b125388SAl Viro return error; 24943b125388SAl Viro } 24953b125388SAl Viro *displaced = task->files; 24963b125388SAl Viro task_lock(task); 24973b125388SAl Viro task->files = copy; 24983b125388SAl Viro task_unlock(task); 24993b125388SAl Viro return 0; 25003b125388SAl Viro } 250116db3d3fSHeinrich Schuchardt 250216db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write, 250316db3d3fSHeinrich Schuchardt void __user *buffer, size_t *lenp, loff_t *ppos) 250416db3d3fSHeinrich Schuchardt { 250516db3d3fSHeinrich Schuchardt struct ctl_table t; 250616db3d3fSHeinrich Schuchardt int ret; 250716db3d3fSHeinrich Schuchardt int threads = max_threads; 250816db3d3fSHeinrich Schuchardt int min = MIN_THREADS; 250916db3d3fSHeinrich Schuchardt int max = MAX_THREADS; 251016db3d3fSHeinrich Schuchardt 251116db3d3fSHeinrich Schuchardt t = *table; 251216db3d3fSHeinrich Schuchardt t.data = &threads; 251316db3d3fSHeinrich Schuchardt t.extra1 = &min; 251416db3d3fSHeinrich Schuchardt t.extra2 = &max; 251516db3d3fSHeinrich Schuchardt 251616db3d3fSHeinrich Schuchardt ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 251716db3d3fSHeinrich Schuchardt if (ret || !write) 251816db3d3fSHeinrich Schuchardt return ret; 251916db3d3fSHeinrich Schuchardt 252016db3d3fSHeinrich Schuchardt set_max_threads(threads); 252116db3d3fSHeinrich Schuchardt 252216db3d3fSHeinrich Schuchardt return 0; 252316db3d3fSHeinrich Schuchardt } 2524