1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/kernel/fork.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * 'fork.c' contains the help-routines for the 'fork' system call 101da177e4SLinus Torvalds * (see also entry.S and others). 111da177e4SLinus Torvalds * Fork is rather simple, once you get the hang of it, but the memory 121da177e4SLinus Torvalds * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 15b3e58382SChristian Brauner #include <linux/anon_inodes.h> 161da177e4SLinus Torvalds #include <linux/slab.h> 174eb5aaa3SIngo Molnar #include <linux/sched/autogroup.h> 186e84f315SIngo Molnar #include <linux/sched/mm.h> 19f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 208703e8a4SIngo Molnar #include <linux/sched/user.h> 216a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 2203441a34SIngo Molnar #include <linux/sched/stat.h> 2329930025SIngo Molnar #include <linux/sched/task.h> 2468db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 2532ef5517SIngo Molnar #include <linux/sched/cputime.h> 26b3e58382SChristian Brauner #include <linux/seq_file.h> 27037741a6SIngo Molnar #include <linux/rtmutex.h> 281da177e4SLinus Torvalds #include <linux/init.h> 291da177e4SLinus Torvalds #include <linux/unistd.h> 301da177e4SLinus Torvalds #include <linux/module.h> 311da177e4SLinus Torvalds #include <linux/vmalloc.h> 321da177e4SLinus Torvalds #include <linux/completion.h> 331da177e4SLinus Torvalds #include <linux/personality.h> 341da177e4SLinus Torvalds #include <linux/mempolicy.h> 351da177e4SLinus Torvalds #include <linux/sem.h> 361da177e4SLinus Torvalds #include <linux/file.h> 379f3acc31SAl Viro #include <linux/fdtable.h> 38da9cbc87SJens Axboe #include <linux/iocontext.h> 391da177e4SLinus Torvalds #include <linux/key.h> 4050b5e49cSAlexander Potapenko #include <linux/kmsan.h> 411da177e4SLinus Torvalds #include <linux/binfmts.h> 421da177e4SLinus Torvalds #include <linux/mman.h> 43cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 441da177e4SLinus Torvalds #include <linux/fs.h> 45615d6e87SDavidlohr Bueso #include <linux/mm.h> 4617fca131SArnd Bergmann #include <linux/mm_inline.h> 47ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 48c59ede7bSRandy.Dunlap #include <linux/capability.h> 491da177e4SLinus Torvalds #include <linux/cpu.h> 50b4f48b63SPaul Menage #include <linux/cgroup.h> 511da177e4SLinus Torvalds #include <linux/security.h> 52a1e78772SMel Gorman #include <linux/hugetlb.h> 53e2cfabdfSWill Drewry #include <linux/seccomp.h> 541da177e4SLinus Torvalds #include <linux/swap.h> 551da177e4SLinus Torvalds #include <linux/syscalls.h> 561da177e4SLinus Torvalds #include <linux/jiffies.h> 571da177e4SLinus Torvalds #include <linux/futex.h> 588141c7f3SLinus Torvalds #include <linux/compat.h> 59207205a2SEric Dumazet #include <linux/kthread.h> 607c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 61ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 621da177e4SLinus Torvalds #include <linux/ptrace.h> 631da177e4SLinus Torvalds #include <linux/mount.h> 641da177e4SLinus Torvalds #include <linux/audit.h> 6578fb7466SPavel Emelianov #include <linux/memcontrol.h> 66f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 675e2bf014SMike Galbraith #include <linux/proc_fs.h> 681da177e4SLinus Torvalds #include <linux/profile.h> 691da177e4SLinus Torvalds #include <linux/rmap.h> 70f8af4da3SHugh Dickins #include <linux/ksm.h> 711da177e4SLinus Torvalds #include <linux/acct.h> 72893e26e6SPavel Emelyanov #include <linux/userfaultfd_k.h> 738f0ab514SJay Lan #include <linux/tsacct_kern.h> 749f46080cSMatt Helsley #include <linux/cn_proc.h> 75ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 76ca74e92bSShailabh Nagar #include <linux/delayacct.h> 77ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 78522ed776SMiloslav Trmac #include <linux/tty.h> 795ad4e53bSAl Viro #include <linux/fs_struct.h> 807c9f8861SEric Sandeen #include <linux/magic.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> 94afaef01cSAlexander Popov #include <linux/stackleak.h> 95eafb149eSDaniel Axtens #include <linux/kasan.h> 96d08b9f0cSSami Tolvanen #include <linux/scs.h> 970f212204SJens Axboe #include <linux/io_uring.h> 98a10787e6SSong Liu #include <linux/bpf.h> 99b3883a9aSJason A. Donenfeld #include <linux/stackprotector.h> 100fd593511SBeau Belgrave #include <linux/user_events.h> 101cd389115SJacob Pan #include <linux/iommu.h> 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds #include <asm/pgalloc.h> 1047c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 1051da177e4SLinus Torvalds #include <asm/mmu_context.h> 1061da177e4SLinus Torvalds #include <asm/cacheflush.h> 1071da177e4SLinus Torvalds #include <asm/tlbflush.h> 1081da177e4SLinus Torvalds 109ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 110ad8d75ffSSteven Rostedt 11143d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 11243d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 11343d2b113SKAMEZAWA Hiroyuki 1141da177e4SLinus Torvalds /* 115ac1b398dSHeinrich Schuchardt * Minimum number of threads to boot the kernel 116ac1b398dSHeinrich Schuchardt */ 117ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20 118ac1b398dSHeinrich Schuchardt 119ac1b398dSHeinrich Schuchardt /* 120ac1b398dSHeinrich Schuchardt * Maximum number of threads 121ac1b398dSHeinrich Schuchardt */ 122ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK 123ac1b398dSHeinrich Schuchardt 124ac1b398dSHeinrich Schuchardt /* 1251da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 1281da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 1291da177e4SLinus Torvalds 1308856ae4dSKefeng Wang static int max_threads; /* tunable limit on nr_threads */ 1311da177e4SLinus Torvalds 1328495f7e6SSai Praneeth Prakhya #define NAMED_ARRAY_INDEX(x) [x] = __stringify(x) 1338495f7e6SSai Praneeth Prakhya 1348495f7e6SSai Praneeth Prakhya static const char * const resident_page_types[] = { 1358495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_FILEPAGES), 1368495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_ANONPAGES), 1378495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_SWAPENTS), 1388495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_SHMEMPAGES), 1398495f7e6SSai Praneeth Prakhya }; 1408495f7e6SSai Praneeth Prakhya 1411da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 144db1466b3SPaul E. McKenney 145db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 146db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 147db1466b3SPaul E. McKenney { 148db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 149db1466b3SPaul E. McKenney } 150db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 151db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds int nr_processes(void) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds int cpu; 1561da177e4SLinus Torvalds int total = 0; 1571da177e4SLinus Torvalds 1581d510750SIan Campbell for_each_possible_cpu(cpu) 1591da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds return total; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 164f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 165f19b9f74SAkinobu Mita { 166f19b9f74SAkinobu Mita } 167f19b9f74SAkinobu Mita 168f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 169e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 17041101809SThomas Gleixner 17141101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 17241101809SThomas Gleixner { 17341101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 17441101809SThomas Gleixner } 17541101809SThomas Gleixner 17641101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 17741101809SThomas Gleixner { 17841101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 17941101809SThomas Gleixner } 1801da177e4SLinus Torvalds #endif 1811da177e4SLinus Torvalds 182b235beeaSLinus Torvalds #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR 18341101809SThomas Gleixner 1840d15d74aSThomas Gleixner /* 1850d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1860d15d74aSThomas Gleixner * kmemcache based allocator. 1870d15d74aSThomas Gleixner */ 188ba14a194SAndy Lutomirski # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) 189ac496bf4SAndy Lutomirski 190ac496bf4SAndy Lutomirski # ifdef CONFIG_VMAP_STACK 191ac496bf4SAndy Lutomirski /* 192ac496bf4SAndy Lutomirski * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 193ac496bf4SAndy Lutomirski * flush. Try to minimize the number of calls by caching stacks. 194ac496bf4SAndy Lutomirski */ 195ac496bf4SAndy Lutomirski #define NR_CACHED_STACKS 2 196ac496bf4SAndy Lutomirski static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 19719659c59SHoeun Ryu 198e540bf31SSebastian Andrzej Siewior struct vm_stack { 199e540bf31SSebastian Andrzej Siewior struct rcu_head rcu; 200e540bf31SSebastian Andrzej Siewior struct vm_struct *stack_vm_area; 201e540bf31SSebastian Andrzej Siewior }; 202e540bf31SSebastian Andrzej Siewior 203e540bf31SSebastian Andrzej Siewior static bool try_release_thread_stack_to_cache(struct vm_struct *vm) 204e540bf31SSebastian Andrzej Siewior { 205e540bf31SSebastian Andrzej Siewior unsigned int i; 206e540bf31SSebastian Andrzej Siewior 207e540bf31SSebastian Andrzej Siewior for (i = 0; i < NR_CACHED_STACKS; i++) { 208e540bf31SSebastian Andrzej Siewior if (this_cpu_cmpxchg(cached_stacks[i], NULL, vm) != NULL) 209e540bf31SSebastian Andrzej Siewior continue; 210e540bf31SSebastian Andrzej Siewior return true; 211e540bf31SSebastian Andrzej Siewior } 212e540bf31SSebastian Andrzej Siewior return false; 213e540bf31SSebastian Andrzej Siewior } 214e540bf31SSebastian Andrzej Siewior 215e540bf31SSebastian Andrzej Siewior static void thread_stack_free_rcu(struct rcu_head *rh) 216e540bf31SSebastian Andrzej Siewior { 217e540bf31SSebastian Andrzej Siewior struct vm_stack *vm_stack = container_of(rh, struct vm_stack, rcu); 218e540bf31SSebastian Andrzej Siewior 219e540bf31SSebastian Andrzej Siewior if (try_release_thread_stack_to_cache(vm_stack->stack_vm_area)) 220e540bf31SSebastian Andrzej Siewior return; 221e540bf31SSebastian Andrzej Siewior 222e540bf31SSebastian Andrzej Siewior vfree(vm_stack); 223e540bf31SSebastian Andrzej Siewior } 224e540bf31SSebastian Andrzej Siewior 225e540bf31SSebastian Andrzej Siewior static void thread_stack_delayed_free(struct task_struct *tsk) 226e540bf31SSebastian Andrzej Siewior { 227e540bf31SSebastian Andrzej Siewior struct vm_stack *vm_stack = tsk->stack; 228e540bf31SSebastian Andrzej Siewior 229e540bf31SSebastian Andrzej Siewior vm_stack->stack_vm_area = tsk->stack_vm_area; 230e540bf31SSebastian Andrzej Siewior call_rcu(&vm_stack->rcu, thread_stack_free_rcu); 231e540bf31SSebastian Andrzej Siewior } 232e540bf31SSebastian Andrzej Siewior 23319659c59SHoeun Ryu static int free_vm_stack_cache(unsigned int cpu) 23419659c59SHoeun Ryu { 23519659c59SHoeun Ryu struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu); 23619659c59SHoeun Ryu int i; 23719659c59SHoeun Ryu 23819659c59SHoeun Ryu for (i = 0; i < NR_CACHED_STACKS; i++) { 23919659c59SHoeun Ryu struct vm_struct *vm_stack = cached_vm_stacks[i]; 24019659c59SHoeun Ryu 24119659c59SHoeun Ryu if (!vm_stack) 24219659c59SHoeun Ryu continue; 24319659c59SHoeun Ryu 24419659c59SHoeun Ryu vfree(vm_stack->addr); 24519659c59SHoeun Ryu cached_vm_stacks[i] = NULL; 24619659c59SHoeun Ryu } 24719659c59SHoeun Ryu 24819659c59SHoeun Ryu return 0; 24919659c59SHoeun Ryu } 250ac496bf4SAndy Lutomirski 2511a03d3f1SSebastian Andrzej Siewior static int memcg_charge_kernel_stack(struct vm_struct *vm) 252b69c49b7SFUJITA Tomonori { 253f1c1a9eeSSebastian Andrzej Siewior int i; 254f1c1a9eeSSebastian Andrzej Siewior int ret; 2554e2f6342SHaifeng Xu int nr_charged = 0; 256f1c1a9eeSSebastian Andrzej Siewior 257f1c1a9eeSSebastian Andrzej Siewior BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); 258f1c1a9eeSSebastian Andrzej Siewior 259f1c1a9eeSSebastian Andrzej Siewior for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 260f1c1a9eeSSebastian Andrzej Siewior ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL, 0); 261f1c1a9eeSSebastian Andrzej Siewior if (ret) 262f1c1a9eeSSebastian Andrzej Siewior goto err; 2634e2f6342SHaifeng Xu nr_charged++; 264f1c1a9eeSSebastian Andrzej Siewior } 265f1c1a9eeSSebastian Andrzej Siewior return 0; 266f1c1a9eeSSebastian Andrzej Siewior err: 2674e2f6342SHaifeng Xu for (i = 0; i < nr_charged; i++) 268f1c1a9eeSSebastian Andrzej Siewior memcg_kmem_uncharge_page(vm->pages[i], 0); 269f1c1a9eeSSebastian Andrzej Siewior return ret; 270f1c1a9eeSSebastian Andrzej Siewior } 271f1c1a9eeSSebastian Andrzej Siewior 2727865aba3SSebastian Andrzej Siewior static int alloc_thread_stack_node(struct task_struct *tsk, int node) 273b69c49b7SFUJITA Tomonori { 2741a03d3f1SSebastian Andrzej Siewior struct vm_struct *vm; 275ac496bf4SAndy Lutomirski void *stack; 276ac496bf4SAndy Lutomirski int i; 277ac496bf4SAndy Lutomirski 278ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 279112166f8SChristoph Lameter struct vm_struct *s; 280112166f8SChristoph Lameter 281112166f8SChristoph Lameter s = this_cpu_xchg(cached_stacks[i], NULL); 282ac496bf4SAndy Lutomirski 283ac496bf4SAndy Lutomirski if (!s) 284ac496bf4SAndy Lutomirski continue; 285ac496bf4SAndy Lutomirski 28651fb34deSAndrey Konovalov /* Reset stack metadata. */ 287cebd0eb2SAndrey Konovalov kasan_unpoison_range(s->addr, THREAD_SIZE); 288eafb149eSDaniel Axtens 28951fb34deSAndrey Konovalov stack = kasan_reset_tag(s->addr); 29051fb34deSAndrey Konovalov 291ca182551SKonstantin Khlebnikov /* Clear stale pointers from reused stack. */ 29251fb34deSAndrey Konovalov memset(stack, 0, THREAD_SIZE); 293e01e8063SKees Cook 2941a03d3f1SSebastian Andrzej Siewior if (memcg_charge_kernel_stack(s)) { 295f1c1a9eeSSebastian Andrzej Siewior vfree(s->addr); 296f1c1a9eeSSebastian Andrzej Siewior return -ENOMEM; 297f1c1a9eeSSebastian Andrzej Siewior } 298f1c1a9eeSSebastian Andrzej Siewior 299ac496bf4SAndy Lutomirski tsk->stack_vm_area = s; 30051fb34deSAndrey Konovalov tsk->stack = stack; 3017865aba3SSebastian Andrzej Siewior return 0; 302ac496bf4SAndy Lutomirski } 303ac496bf4SAndy Lutomirski 3049b6f7e16SRoman Gushchin /* 3059b6f7e16SRoman Gushchin * Allocated stacks are cached and later reused by new threads, 3069b6f7e16SRoman Gushchin * so memcg accounting is performed manually on assigning/releasing 3079b6f7e16SRoman Gushchin * stacks to tasks. Drop __GFP_ACCOUNT. 3089b6f7e16SRoman Gushchin */ 30948ac3c18SMark Rutland stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, 310ba14a194SAndy Lutomirski VMALLOC_START, VMALLOC_END, 3119b6f7e16SRoman Gushchin THREADINFO_GFP & ~__GFP_ACCOUNT, 312ba14a194SAndy Lutomirski PAGE_KERNEL, 313ac496bf4SAndy Lutomirski 0, node, __builtin_return_address(0)); 3147865aba3SSebastian Andrzej Siewior if (!stack) 3157865aba3SSebastian Andrzej Siewior return -ENOMEM; 316ba14a194SAndy Lutomirski 3171a03d3f1SSebastian Andrzej Siewior vm = find_vm_area(stack); 3181a03d3f1SSebastian Andrzej Siewior if (memcg_charge_kernel_stack(vm)) { 319f1c1a9eeSSebastian Andrzej Siewior vfree(stack); 320f1c1a9eeSSebastian Andrzej Siewior return -ENOMEM; 321f1c1a9eeSSebastian Andrzej Siewior } 322ba14a194SAndy Lutomirski /* 323ba14a194SAndy Lutomirski * We can't call find_vm_area() in interrupt context, and 324ba14a194SAndy Lutomirski * free_thread_stack() can be called in interrupt context, 325ba14a194SAndy Lutomirski * so cache the vm_struct. 326ba14a194SAndy Lutomirski */ 3271a03d3f1SSebastian Andrzej Siewior tsk->stack_vm_area = vm; 328c08e6a12SAndrey Konovalov stack = kasan_reset_tag(stack); 3295eed6f1dSRik van Riel tsk->stack = stack; 3307865aba3SSebastian Andrzej Siewior return 0; 3315eed6f1dSRik van Riel } 332be9a2277SSebastian Andrzej Siewior 333be9a2277SSebastian Andrzej Siewior static void free_thread_stack(struct task_struct *tsk) 334be9a2277SSebastian Andrzej Siewior { 335e540bf31SSebastian Andrzej Siewior if (!try_release_thread_stack_to_cache(tsk->stack_vm_area)) 336e540bf31SSebastian Andrzej Siewior thread_stack_delayed_free(tsk); 337be9a2277SSebastian Andrzej Siewior 338be9a2277SSebastian Andrzej Siewior tsk->stack = NULL; 339be9a2277SSebastian Andrzej Siewior tsk->stack_vm_area = NULL; 340be9a2277SSebastian Andrzej Siewior } 341be9a2277SSebastian Andrzej Siewior 342be9a2277SSebastian Andrzej Siewior # else /* !CONFIG_VMAP_STACK */ 343be9a2277SSebastian Andrzej Siewior 344e540bf31SSebastian Andrzej Siewior static void thread_stack_free_rcu(struct rcu_head *rh) 345e540bf31SSebastian Andrzej Siewior { 346e540bf31SSebastian Andrzej Siewior __free_pages(virt_to_page(rh), THREAD_SIZE_ORDER); 347e540bf31SSebastian Andrzej Siewior } 348e540bf31SSebastian Andrzej Siewior 349e540bf31SSebastian Andrzej Siewior static void thread_stack_delayed_free(struct task_struct *tsk) 350e540bf31SSebastian Andrzej Siewior { 351e540bf31SSebastian Andrzej Siewior struct rcu_head *rh = tsk->stack; 352e540bf31SSebastian Andrzej Siewior 353e540bf31SSebastian Andrzej Siewior call_rcu(rh, thread_stack_free_rcu); 354e540bf31SSebastian Andrzej Siewior } 355e540bf31SSebastian Andrzej Siewior 3567865aba3SSebastian Andrzej Siewior static int alloc_thread_stack_node(struct task_struct *tsk, int node) 357be9a2277SSebastian Andrzej Siewior { 3584949148aSVladimir Davydov struct page *page = alloc_pages_node(node, THREADINFO_GFP, 3592889f608SThomas Gleixner THREAD_SIZE_ORDER); 360b6a84016SEric Dumazet 3611bf4580eSAndrea Arcangeli if (likely(page)) { 3628dcc1d34SAndrey Konovalov tsk->stack = kasan_reset_tag(page_address(page)); 3637865aba3SSebastian Andrzej Siewior return 0; 3641bf4580eSAndrea Arcangeli } 3657865aba3SSebastian Andrzej Siewior return -ENOMEM; 366b69c49b7SFUJITA Tomonori } 367b69c49b7SFUJITA Tomonori 368be9a2277SSebastian Andrzej Siewior static void free_thread_stack(struct task_struct *tsk) 369b69c49b7SFUJITA Tomonori { 370e540bf31SSebastian Andrzej Siewior thread_stack_delayed_free(tsk); 371be9a2277SSebastian Andrzej Siewior tsk->stack = NULL; 372ac496bf4SAndy Lutomirski } 373ac496bf4SAndy Lutomirski 374be9a2277SSebastian Andrzej Siewior # endif /* CONFIG_VMAP_STACK */ 375be9a2277SSebastian Andrzej Siewior # else /* !(THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)) */ 376ac496bf4SAndy Lutomirski 377b235beeaSLinus Torvalds static struct kmem_cache *thread_stack_cache; 3780d15d74aSThomas Gleixner 379e540bf31SSebastian Andrzej Siewior static void thread_stack_free_rcu(struct rcu_head *rh) 380e540bf31SSebastian Andrzej Siewior { 381e540bf31SSebastian Andrzej Siewior kmem_cache_free(thread_stack_cache, rh); 382e540bf31SSebastian Andrzej Siewior } 383e540bf31SSebastian Andrzej Siewior 384e540bf31SSebastian Andrzej Siewior static void thread_stack_delayed_free(struct task_struct *tsk) 385e540bf31SSebastian Andrzej Siewior { 386e540bf31SSebastian Andrzej Siewior struct rcu_head *rh = tsk->stack; 387e540bf31SSebastian Andrzej Siewior 388e540bf31SSebastian Andrzej Siewior call_rcu(rh, thread_stack_free_rcu); 389e540bf31SSebastian Andrzej Siewior } 390e540bf31SSebastian Andrzej Siewior 3917865aba3SSebastian Andrzej Siewior static int alloc_thread_stack_node(struct task_struct *tsk, int node) 3920d15d74aSThomas Gleixner { 3935eed6f1dSRik van Riel unsigned long *stack; 3945eed6f1dSRik van Riel stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 3958dcc1d34SAndrey Konovalov stack = kasan_reset_tag(stack); 3965eed6f1dSRik van Riel tsk->stack = stack; 3977865aba3SSebastian Andrzej Siewior return stack ? 0 : -ENOMEM; 3980d15d74aSThomas Gleixner } 3990d15d74aSThomas Gleixner 400ba14a194SAndy Lutomirski static void free_thread_stack(struct task_struct *tsk) 4010d15d74aSThomas Gleixner { 402e540bf31SSebastian Andrzej Siewior thread_stack_delayed_free(tsk); 403be9a2277SSebastian Andrzej Siewior tsk->stack = NULL; 4040d15d74aSThomas Gleixner } 4050d15d74aSThomas Gleixner 406b235beeaSLinus Torvalds void thread_stack_cache_init(void) 4070d15d74aSThomas Gleixner { 408f9d29946SDavid Windsor thread_stack_cache = kmem_cache_create_usercopy("thread_stack", 409f9d29946SDavid Windsor THREAD_SIZE, THREAD_SIZE, 0, 0, 410f9d29946SDavid Windsor THREAD_SIZE, NULL); 411b235beeaSLinus Torvalds BUG_ON(thread_stack_cache == NULL); 4120d15d74aSThomas Gleixner } 413be9a2277SSebastian Andrzej Siewior 414be9a2277SSebastian Andrzej Siewior # endif /* THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) */ 4152bb0529cSSebastian Andrzej Siewior #else /* CONFIG_ARCH_THREAD_STACK_ALLOCATOR */ 4162bb0529cSSebastian Andrzej Siewior 4177865aba3SSebastian Andrzej Siewior static int alloc_thread_stack_node(struct task_struct *tsk, int node) 4182bb0529cSSebastian Andrzej Siewior { 4192bb0529cSSebastian Andrzej Siewior unsigned long *stack; 4202bb0529cSSebastian Andrzej Siewior 4212bb0529cSSebastian Andrzej Siewior stack = arch_alloc_thread_stack_node(tsk, node); 4222bb0529cSSebastian Andrzej Siewior tsk->stack = stack; 4237865aba3SSebastian Andrzej Siewior return stack ? 0 : -ENOMEM; 4242bb0529cSSebastian Andrzej Siewior } 4252bb0529cSSebastian Andrzej Siewior 4262bb0529cSSebastian Andrzej Siewior static void free_thread_stack(struct task_struct *tsk) 4272bb0529cSSebastian Andrzej Siewior { 4282bb0529cSSebastian Andrzej Siewior arch_free_thread_stack(tsk); 4292bb0529cSSebastian Andrzej Siewior tsk->stack = NULL; 4302bb0529cSSebastian Andrzej Siewior } 4312bb0529cSSebastian Andrzej Siewior 432be9a2277SSebastian Andrzej Siewior #endif /* !CONFIG_ARCH_THREAD_STACK_ALLOCATOR */ 433b69c49b7SFUJITA Tomonori 4341da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 435e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 438e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 441e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 444e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 4473928d4f5SLinus Torvalds static struct kmem_cache *vm_area_cachep; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 450e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 4511da177e4SLinus Torvalds 452c7f8f31cSSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 453c7f8f31cSSuren Baghdasaryan 454c7f8f31cSSuren Baghdasaryan /* SLAB cache for vm_area_struct.lock */ 455c7f8f31cSSuren Baghdasaryan static struct kmem_cache *vma_lock_cachep; 456c7f8f31cSSuren Baghdasaryan 457c7f8f31cSSuren Baghdasaryan static bool vma_lock_alloc(struct vm_area_struct *vma) 458c7f8f31cSSuren Baghdasaryan { 459c7f8f31cSSuren Baghdasaryan vma->vm_lock = kmem_cache_alloc(vma_lock_cachep, GFP_KERNEL); 460c7f8f31cSSuren Baghdasaryan if (!vma->vm_lock) 461c7f8f31cSSuren Baghdasaryan return false; 462c7f8f31cSSuren Baghdasaryan 463c7f8f31cSSuren Baghdasaryan init_rwsem(&vma->vm_lock->lock); 464c7f8f31cSSuren Baghdasaryan vma->vm_lock_seq = -1; 465c7f8f31cSSuren Baghdasaryan 466c7f8f31cSSuren Baghdasaryan return true; 467c7f8f31cSSuren Baghdasaryan } 468c7f8f31cSSuren Baghdasaryan 469c7f8f31cSSuren Baghdasaryan static inline void vma_lock_free(struct vm_area_struct *vma) 470c7f8f31cSSuren Baghdasaryan { 471c7f8f31cSSuren Baghdasaryan kmem_cache_free(vma_lock_cachep, vma->vm_lock); 472c7f8f31cSSuren Baghdasaryan } 473c7f8f31cSSuren Baghdasaryan 474c7f8f31cSSuren Baghdasaryan #else /* CONFIG_PER_VMA_LOCK */ 475c7f8f31cSSuren Baghdasaryan 476c7f8f31cSSuren Baghdasaryan static inline bool vma_lock_alloc(struct vm_area_struct *vma) { return true; } 477c7f8f31cSSuren Baghdasaryan static inline void vma_lock_free(struct vm_area_struct *vma) {} 478c7f8f31cSSuren Baghdasaryan 479c7f8f31cSSuren Baghdasaryan #endif /* CONFIG_PER_VMA_LOCK */ 480c7f8f31cSSuren Baghdasaryan 481490fc053SLinus Torvalds struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) 4823928d4f5SLinus Torvalds { 483a670468fSAndrew Morton struct vm_area_struct *vma; 484490fc053SLinus Torvalds 485a670468fSAndrew Morton vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 486c7f8f31cSSuren Baghdasaryan if (!vma) 487c7f8f31cSSuren Baghdasaryan return NULL; 488c7f8f31cSSuren Baghdasaryan 489027232daSKirill A. Shutemov vma_init(vma, mm); 490c7f8f31cSSuren Baghdasaryan if (!vma_lock_alloc(vma)) { 491c7f8f31cSSuren Baghdasaryan kmem_cache_free(vm_area_cachep, vma); 492c7f8f31cSSuren Baghdasaryan return NULL; 493c7f8f31cSSuren Baghdasaryan } 494c7f8f31cSSuren Baghdasaryan 495490fc053SLinus Torvalds return vma; 4963928d4f5SLinus Torvalds } 4973928d4f5SLinus Torvalds 4983928d4f5SLinus Torvalds struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) 4993928d4f5SLinus Torvalds { 50095faf699SLinus Torvalds struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 50195faf699SLinus Torvalds 502c7f8f31cSSuren Baghdasaryan if (!new) 503c7f8f31cSSuren Baghdasaryan return NULL; 504c7f8f31cSSuren Baghdasaryan 505cda099b3SQian Cai ASSERT_EXCLUSIVE_WRITER(orig->vm_flags); 506cda099b3SQian Cai ASSERT_EXCLUSIVE_WRITER(orig->vm_file); 507cda099b3SQian Cai /* 508cda099b3SQian Cai * orig->shared.rb may be modified concurrently, but the clone 509cda099b3SQian Cai * will be reinitialized. 510cda099b3SQian Cai */ 51106e78b61SSuren Baghdasaryan data_race(memcpy(new, orig, sizeof(*new))); 512c7f8f31cSSuren Baghdasaryan if (!vma_lock_alloc(new)) { 513c7f8f31cSSuren Baghdasaryan kmem_cache_free(vm_area_cachep, new); 514c7f8f31cSSuren Baghdasaryan return NULL; 51595faf699SLinus Torvalds } 516c7f8f31cSSuren Baghdasaryan INIT_LIST_HEAD(&new->anon_vma_chain); 517ef6a22b7SMel Gorman vma_numab_state_init(new); 518c7f8f31cSSuren Baghdasaryan dup_anon_vma_name(orig, new); 519c7f8f31cSSuren Baghdasaryan 52095faf699SLinus Torvalds return new; 5213928d4f5SLinus Torvalds } 5223928d4f5SLinus Torvalds 5230d2ebf9cSSuren Baghdasaryan void __vm_area_free(struct vm_area_struct *vma) 5243928d4f5SLinus Torvalds { 525ef6a22b7SMel Gorman vma_numab_state_free(vma); 5265c26f6acSSuren Baghdasaryan free_anon_vma_name(vma); 527c7f8f31cSSuren Baghdasaryan vma_lock_free(vma); 5283928d4f5SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 5293928d4f5SLinus Torvalds } 5303928d4f5SLinus Torvalds 53120cce633SMichel Lespinasse #ifdef CONFIG_PER_VMA_LOCK 53220cce633SMichel Lespinasse static void vm_area_free_rcu_cb(struct rcu_head *head) 53320cce633SMichel Lespinasse { 53420cce633SMichel Lespinasse struct vm_area_struct *vma = container_of(head, struct vm_area_struct, 53520cce633SMichel Lespinasse vm_rcu); 536f2e13784SSuren Baghdasaryan 537f2e13784SSuren Baghdasaryan /* The vma should not be locked while being destroyed. */ 538c7f8f31cSSuren Baghdasaryan VM_BUG_ON_VMA(rwsem_is_locked(&vma->vm_lock->lock), vma); 53920cce633SMichel Lespinasse __vm_area_free(vma); 54020cce633SMichel Lespinasse } 54120cce633SMichel Lespinasse #endif 54220cce633SMichel Lespinasse 5431da177e4SLinus Torvalds void vm_area_free(struct vm_area_struct *vma) 5441da177e4SLinus Torvalds { 54520cce633SMichel Lespinasse #ifdef CONFIG_PER_VMA_LOCK 54620cce633SMichel Lespinasse call_rcu(&vma->vm_rcu, vm_area_free_rcu_cb); 54720cce633SMichel Lespinasse #else 54820cce633SMichel Lespinasse __vm_area_free(vma); 54920cce633SMichel Lespinasse #endif 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 552ba14a194SAndy Lutomirski static void account_kernel_stack(struct task_struct *tsk, int account) 553c6a7f572SKOSAKI Motohiro { 5540ce055f8SSebastian Andrzej Siewior if (IS_ENABLED(CONFIG_VMAP_STACK)) { 555ba14a194SAndy Lutomirski struct vm_struct *vm = task_stack_vm_area(tsk); 55627faca83SMuchun Song int i; 557ba14a194SAndy Lutomirski 55827faca83SMuchun Song for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) 55927faca83SMuchun Song mod_lruvec_page_state(vm->pages[i], NR_KERNEL_STACK_KB, 56027faca83SMuchun Song account * (PAGE_SIZE / 1024)); 56127faca83SMuchun Song } else { 5620ce055f8SSebastian Andrzej Siewior void *stack = task_stack_page(tsk); 5630ce055f8SSebastian Andrzej Siewior 564991e7673SShakeel Butt /* All stack pages are in the same node. */ 565da3ceeffSMuchun Song mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB, 566991e7673SShakeel Butt account * (THREAD_SIZE / 1024)); 567ba14a194SAndy Lutomirski } 56827faca83SMuchun Song } 569c6a7f572SKOSAKI Motohiro 5701a03d3f1SSebastian Andrzej Siewior void exit_task_stack_account(struct task_struct *tsk) 5719b6f7e16SRoman Gushchin { 5721a03d3f1SSebastian Andrzej Siewior account_kernel_stack(tsk, -1); 5739b6f7e16SRoman Gushchin 5741a03d3f1SSebastian Andrzej Siewior if (IS_ENABLED(CONFIG_VMAP_STACK)) { 5751a03d3f1SSebastian Andrzej Siewior struct vm_struct *vm; 5769b6f7e16SRoman Gushchin int i; 5779b6f7e16SRoman Gushchin 5781a03d3f1SSebastian Andrzej Siewior vm = task_stack_vm_area(tsk); 5791a03d3f1SSebastian Andrzej Siewior for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) 5801a03d3f1SSebastian Andrzej Siewior memcg_kmem_uncharge_page(vm->pages[i], 0); 5819b6f7e16SRoman Gushchin } 5829b6f7e16SRoman Gushchin } 5839b6f7e16SRoman Gushchin 58468f24b08SAndy Lutomirski static void release_task_stack(struct task_struct *tsk) 5851da177e4SLinus Torvalds { 5862f064a59SPeter Zijlstra if (WARN_ON(READ_ONCE(tsk->__state) != TASK_DEAD)) 587405c0759SAndy Lutomirski return; /* Better to leak the stack than to free prematurely */ 588405c0759SAndy Lutomirski 589ba14a194SAndy Lutomirski free_thread_stack(tsk); 59068f24b08SAndy Lutomirski } 59168f24b08SAndy Lutomirski 59268f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 59368f24b08SAndy Lutomirski void put_task_stack(struct task_struct *tsk) 59468f24b08SAndy Lutomirski { 595f0b89d39SElena Reshetova if (refcount_dec_and_test(&tsk->stack_refcount)) 59668f24b08SAndy Lutomirski release_task_stack(tsk); 59768f24b08SAndy Lutomirski } 59868f24b08SAndy Lutomirski #endif 59968f24b08SAndy Lutomirski 60068f24b08SAndy Lutomirski void free_task(struct task_struct *tsk) 60168f24b08SAndy Lutomirski { 602a1140cb2SKuniyuki Iwashima #ifdef CONFIG_SECCOMP 603a1140cb2SKuniyuki Iwashima WARN_ON_ONCE(tsk->seccomp.filter); 604a1140cb2SKuniyuki Iwashima #endif 605b90ca8baSWill Deacon release_user_cpus_ptr(tsk); 606d08b9f0cSSami Tolvanen scs_release(tsk); 607d08b9f0cSSami Tolvanen 60868f24b08SAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK 60968f24b08SAndy Lutomirski /* 61068f24b08SAndy Lutomirski * The task is finally done with both the stack and thread_info, 61168f24b08SAndy Lutomirski * so free both. 61268f24b08SAndy Lutomirski */ 61368f24b08SAndy Lutomirski release_task_stack(tsk); 61468f24b08SAndy Lutomirski #else 61568f24b08SAndy Lutomirski /* 61668f24b08SAndy Lutomirski * If the task had a separate stack allocation, it should be gone 61768f24b08SAndy Lutomirski * by now. 61868f24b08SAndy Lutomirski */ 619f0b89d39SElena Reshetova WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0); 62068f24b08SAndy Lutomirski #endif 62123f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 622fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 623f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 6241da5c46fSOleg Nesterov if (tsk->flags & PF_KTHREAD) 6251da5c46fSOleg Nesterov free_kthread_struct(tsk); 626b0fd1852SKP Singh bpf_task_storage_free(tsk); 6271da177e4SLinus Torvalds free_task_struct(tsk); 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 6301da177e4SLinus Torvalds 631fe69d560SDavid Hildenbrand static void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm) 632fe69d560SDavid Hildenbrand { 633fe69d560SDavid Hildenbrand struct file *exe_file; 634fe69d560SDavid Hildenbrand 635fe69d560SDavid Hildenbrand exe_file = get_mm_exe_file(oldmm); 636fe69d560SDavid Hildenbrand RCU_INIT_POINTER(mm->exe_file, exe_file); 637fe69d560SDavid Hildenbrand /* 638fe69d560SDavid Hildenbrand * We depend on the oldmm having properly denied write access to the 639fe69d560SDavid Hildenbrand * exe_file already. 640fe69d560SDavid Hildenbrand */ 641fe69d560SDavid Hildenbrand if (exe_file && deny_write_access(exe_file)) 642fe69d560SDavid Hildenbrand pr_warn_once("deny_write_access() failed in %s\n", __func__); 643fe69d560SDavid Hildenbrand } 644fe69d560SDavid Hildenbrand 6451da177e4SLinus Torvalds #ifdef CONFIG_MMU 6460766f788SEmese Revfy static __latent_entropy int dup_mmap(struct mm_struct *mm, 6470766f788SEmese Revfy struct mm_struct *oldmm) 6481da177e4SLinus Torvalds { 649763ecb03SLiam R. Howlett struct vm_area_struct *mpnt, *tmp; 6501da177e4SLinus Torvalds int retval; 651c9dbe82cSLiam R. Howlett unsigned long charge = 0; 652893e26e6SPavel Emelyanov LIST_HEAD(uf); 6533b9dbd5eSLiam R. Howlett VMA_ITERATOR(old_vmi, oldmm, 0); 6543b9dbd5eSLiam R. Howlett VMA_ITERATOR(vmi, mm, 0); 6551da177e4SLinus Torvalds 65632cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 657d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(oldmm)) { 6587c051267SMichal Hocko retval = -EINTR; 6597c051267SMichal Hocko goto fail_uprobe_end; 6607c051267SMichal Hocko } 661ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 662f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 663ad339451SIngo Molnar /* 664ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 665ad339451SIngo Molnar */ 666aaa2cc56SMichel Lespinasse mmap_write_lock_nested(mm, SINGLE_DEPTH_NESTING); 6677ee78232SHugh Dickins 66890f31d0eSKonstantin Khlebnikov /* No ordering required: file already has been exposed. */ 669fe69d560SDavid Hildenbrand dup_mm_exe_file(mm, oldmm); 67090f31d0eSKonstantin Khlebnikov 6714f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 67284638335SKonstantin Khlebnikov mm->data_vm = oldmm->data_vm; 6734f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 6744f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 6754f7d4614SVladimir Davydov 676f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 677f8af4da3SHugh Dickins if (retval) 678f8af4da3SHugh Dickins goto out; 679d2081b2bSYang Shi khugepaged_fork(mm, oldmm); 6801da177e4SLinus Torvalds 6813b9dbd5eSLiam R. Howlett retval = vma_iter_bulk_alloc(&vmi, oldmm->map_count); 682d4af56c5SLiam R. Howlett if (retval) 683d4af56c5SLiam R. Howlett goto out; 684d4af56c5SLiam R. Howlett 6853dd44325SLiam R. Howlett mt_clear_in_rcu(vmi.mas.tree); 6863b9dbd5eSLiam R. Howlett for_each_vma(old_vmi, mpnt) { 6871da177e4SLinus Torvalds struct file *file; 6881da177e4SLinus Torvalds 689fb49c455SSuren Baghdasaryan vma_start_write(mpnt); 6901da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 69184638335SKonstantin Khlebnikov vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); 6921da177e4SLinus Torvalds continue; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds charge = 0; 695655c79bbSTetsuo Handa /* 696655c79bbSTetsuo Handa * Don't duplicate many vmas if we've been oom-killed (for 697655c79bbSTetsuo Handa * example) 698655c79bbSTetsuo Handa */ 699655c79bbSTetsuo Handa if (fatal_signal_pending(current)) { 700655c79bbSTetsuo Handa retval = -EINTR; 701d4af56c5SLiam R. Howlett goto loop_out; 702655c79bbSTetsuo Handa } 7031da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 704b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 705b2412b7fSHuang Shijie 706191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 7071da177e4SLinus Torvalds goto fail_nomem; 7081da177e4SLinus Torvalds charge = len; 7091da177e4SLinus Torvalds } 7103928d4f5SLinus Torvalds tmp = vm_area_dup(mpnt); 7111da177e4SLinus Torvalds if (!tmp) 7121da177e4SLinus Torvalds goto fail_nomem; 713ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 714ef0855d3SOleg Nesterov if (retval) 7151da177e4SLinus Torvalds goto fail_nomem_policy; 716a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 717893e26e6SPavel Emelyanov retval = dup_userfaultfd(tmp, &uf); 718893e26e6SPavel Emelyanov if (retval) 719893e26e6SPavel Emelyanov goto fail_nomem_anon_vma_fork; 720d2cd9edeSRik van Riel if (tmp->vm_flags & VM_WIPEONFORK) { 72193949bb2SLi Xinhai /* 72293949bb2SLi Xinhai * VM_WIPEONFORK gets a clean slate in the child. 72393949bb2SLi Xinhai * Don't prepare anon_vma until fault since we don't 72493949bb2SLi Xinhai * copy page for current vma. 72593949bb2SLi Xinhai */ 726d2cd9edeSRik van Riel tmp->anon_vma = NULL; 727d2cd9edeSRik van Riel } else if (anon_vma_fork(tmp, mpnt)) 7285beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 729e430a95aSSuren Baghdasaryan vm_flags_clear(tmp, VM_LOCKED_MASK); 7301da177e4SLinus Torvalds file = tmp->vm_file; 7311da177e4SLinus Torvalds if (file) { 732b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 733b88ed205SHugh Dickins 7341da177e4SLinus Torvalds get_file(file); 73583cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 736b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 737cf508b58SMiaohe Lin mapping_allow_writable(mapping); 738b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 739b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 7409826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 7419826a516SMichel Lespinasse &mapping->i_mmap); 742b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 74383cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* 7478d9bfb26SMike Kravetz * Copy/update hugetlb private vma information. 748a1e78772SMel Gorman */ 749a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 7508d9bfb26SMike Kravetz hugetlb_dup_vma_private(tmp); 751a1e78772SMel Gorman 752d4af56c5SLiam R. Howlett /* Link the vma into the MT */ 7533b9dbd5eSLiam R. Howlett if (vma_iter_bulk_store(&vmi, tmp)) 7543b9dbd5eSLiam R. Howlett goto fail_nomem_vmi_store; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds mm->map_count++; 757d2cd9edeSRik van Riel if (!(tmp->vm_flags & VM_WIPEONFORK)) 758c78f4636SPeter Xu retval = copy_page_range(tmp, mpnt); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 7611da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds if (retval) 764d4af56c5SLiam R. Howlett goto loop_out; 7651da177e4SLinus Torvalds } 766d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 7671ed0cc5aSNadav Amit retval = arch_dup_mmap(oldmm, mm); 768d4af56c5SLiam R. Howlett loop_out: 7693b9dbd5eSLiam R. Howlett vma_iter_free(&vmi); 7703dd44325SLiam R. Howlett if (!retval) 7713dd44325SLiam R. Howlett mt_set_in_rcu(vmi.mas.tree); 7721da177e4SLinus Torvalds out: 773d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 774fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 775d8ed45c5SMichel Lespinasse mmap_write_unlock(oldmm); 776893e26e6SPavel Emelyanov dup_userfaultfd_complete(&uf); 7777c051267SMichal Hocko fail_uprobe_end: 77832cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 7791da177e4SLinus Torvalds return retval; 780c9dbe82cSLiam R. Howlett 7813b9dbd5eSLiam R. Howlett fail_nomem_vmi_store: 782c9dbe82cSLiam R. Howlett unlink_anon_vmas(tmp); 7835beb4930SRik van Riel fail_nomem_anon_vma_fork: 784ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 7851da177e4SLinus Torvalds fail_nomem_policy: 7863928d4f5SLinus Torvalds vm_area_free(tmp); 7871da177e4SLinus Torvalds fail_nomem: 7881da177e4SLinus Torvalds retval = -ENOMEM; 7891da177e4SLinus Torvalds vm_unacct_memory(charge); 790d4af56c5SLiam R. Howlett goto loop_out; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 7941da177e4SLinus Torvalds { 7951da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 7961da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 7971da177e4SLinus Torvalds return -ENOMEM; 7981da177e4SLinus Torvalds return 0; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 8021da177e4SLinus Torvalds { 8035e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds #else 80690f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 80790f31d0eSKonstantin Khlebnikov { 808d8ed45c5SMichel Lespinasse mmap_write_lock(oldmm); 809fe69d560SDavid Hildenbrand dup_mm_exe_file(mm, oldmm); 810d8ed45c5SMichel Lespinasse mmap_write_unlock(oldmm); 81190f31d0eSKonstantin Khlebnikov return 0; 81290f31d0eSKonstantin Khlebnikov } 8131da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 8141da177e4SLinus Torvalds #define mm_free_pgd(mm) 8151da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 8161da177e4SLinus Torvalds 817d70f2a14SAndrew Morton static void check_mm(struct mm_struct *mm) 818d70f2a14SAndrew Morton { 819d70f2a14SAndrew Morton int i; 820d70f2a14SAndrew Morton 8218495f7e6SSai Praneeth Prakhya BUILD_BUG_ON_MSG(ARRAY_SIZE(resident_page_types) != NR_MM_COUNTERS, 8228495f7e6SSai Praneeth Prakhya "Please make sure 'struct resident_page_types[]' is updated as well"); 8238495f7e6SSai Praneeth Prakhya 824d70f2a14SAndrew Morton for (i = 0; i < NR_MM_COUNTERS; i++) { 825f1a79412SShakeel Butt long x = percpu_counter_sum(&mm->rss_stat[i]); 826d70f2a14SAndrew Morton 827d70f2a14SAndrew Morton if (unlikely(x)) 8288495f7e6SSai Praneeth Prakhya pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n", 8298495f7e6SSai Praneeth Prakhya mm, resident_page_types[i], x); 830d70f2a14SAndrew Morton } 831d70f2a14SAndrew Morton 832d70f2a14SAndrew Morton if (mm_pgtables_bytes(mm)) 833d70f2a14SAndrew Morton pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", 834d70f2a14SAndrew Morton mm_pgtables_bytes(mm)); 835d70f2a14SAndrew Morton 836d70f2a14SAndrew Morton #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 837d70f2a14SAndrew Morton VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 838d70f2a14SAndrew Morton #endif 839d70f2a14SAndrew Morton } 8401da177e4SLinus Torvalds 841e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 8421da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 8431da177e4SLinus Torvalds 8442655421aSNicholas Piggin static void do_check_lazy_tlb(void *arg) 8452655421aSNicholas Piggin { 8462655421aSNicholas Piggin struct mm_struct *mm = arg; 8472655421aSNicholas Piggin 8482655421aSNicholas Piggin WARN_ON_ONCE(current->active_mm == mm); 8492655421aSNicholas Piggin } 8502655421aSNicholas Piggin 8512655421aSNicholas Piggin static void do_shoot_lazy_tlb(void *arg) 8522655421aSNicholas Piggin { 8532655421aSNicholas Piggin struct mm_struct *mm = arg; 8542655421aSNicholas Piggin 8552655421aSNicholas Piggin if (current->active_mm == mm) { 8562655421aSNicholas Piggin WARN_ON_ONCE(current->mm); 8572655421aSNicholas Piggin current->active_mm = &init_mm; 8582655421aSNicholas Piggin switch_mm(mm, &init_mm, current); 8592655421aSNicholas Piggin } 8602655421aSNicholas Piggin } 8612655421aSNicholas Piggin 8622655421aSNicholas Piggin static void cleanup_lazy_tlbs(struct mm_struct *mm) 8632655421aSNicholas Piggin { 8642655421aSNicholas Piggin if (!IS_ENABLED(CONFIG_MMU_LAZY_TLB_SHOOTDOWN)) { 8652655421aSNicholas Piggin /* 8662655421aSNicholas Piggin * In this case, lazy tlb mms are refounted and would not reach 8672655421aSNicholas Piggin * __mmdrop until all CPUs have switched away and mmdrop()ed. 8682655421aSNicholas Piggin */ 8692655421aSNicholas Piggin return; 8702655421aSNicholas Piggin } 8712655421aSNicholas Piggin 8722655421aSNicholas Piggin /* 8732655421aSNicholas Piggin * Lazy mm shootdown does not refcount "lazy tlb mm" usage, rather it 8742655421aSNicholas Piggin * requires lazy mm users to switch to another mm when the refcount 8752655421aSNicholas Piggin * drops to zero, before the mm is freed. This requires IPIs here to 8762655421aSNicholas Piggin * switch kernel threads to init_mm. 8772655421aSNicholas Piggin * 8782655421aSNicholas Piggin * archs that use IPIs to flush TLBs can piggy-back that lazy tlb mm 8792655421aSNicholas Piggin * switch with the final userspace teardown TLB flush which leaves the 8802655421aSNicholas Piggin * mm lazy on this CPU but no others, reducing the need for additional 8812655421aSNicholas Piggin * IPIs here. There are cases where a final IPI is still required here, 8822655421aSNicholas Piggin * such as the final mmdrop being performed on a different CPU than the 8832655421aSNicholas Piggin * one exiting, or kernel threads using the mm when userspace exits. 8842655421aSNicholas Piggin * 8852655421aSNicholas Piggin * IPI overheads have not found to be expensive, but they could be 8862655421aSNicholas Piggin * reduced in a number of possible ways, for example (roughly 8872655421aSNicholas Piggin * increasing order of complexity): 8882655421aSNicholas Piggin * - The last lazy reference created by exit_mm() could instead switch 8892655421aSNicholas Piggin * to init_mm, however it's probable this will run on the same CPU 8902655421aSNicholas Piggin * immediately afterwards, so this may not reduce IPIs much. 8912655421aSNicholas Piggin * - A batch of mms requiring IPIs could be gathered and freed at once. 8922655421aSNicholas Piggin * - CPUs store active_mm where it can be remotely checked without a 8932655421aSNicholas Piggin * lock, to filter out false-positives in the cpumask. 8942655421aSNicholas Piggin * - After mm_users or mm_count reaches zero, switching away from the 8952655421aSNicholas Piggin * mm could clear mm_cpumask to reduce some IPIs, perhaps together 8962655421aSNicholas Piggin * with some batching or delaying of the final IPIs. 8972655421aSNicholas Piggin * - A delayed freeing and RCU-like quiescing sequence based on mm 8982655421aSNicholas Piggin * switching to avoid IPIs completely. 8992655421aSNicholas Piggin */ 9002655421aSNicholas Piggin on_each_cpu_mask(mm_cpumask(mm), do_shoot_lazy_tlb, (void *)mm, 1); 9012655421aSNicholas Piggin if (IS_ENABLED(CONFIG_DEBUG_VM_SHOOT_LAZIES)) 9022655421aSNicholas Piggin on_each_cpu(do_check_lazy_tlb, (void *)mm, 1); 9032655421aSNicholas Piggin } 9042655421aSNicholas Piggin 905d70f2a14SAndrew Morton /* 906d70f2a14SAndrew Morton * Called when the last reference to the mm 907d70f2a14SAndrew Morton * is dropped: either by a lazy thread or by 908d70f2a14SAndrew Morton * mmput. Free the page directory and the mm. 909d70f2a14SAndrew Morton */ 910d34bc48fSAndrew Morton void __mmdrop(struct mm_struct *mm) 911d70f2a14SAndrew Morton { 912f1a79412SShakeel Butt int i; 913f1a79412SShakeel Butt 914d70f2a14SAndrew Morton BUG_ON(mm == &init_mm); 9153eda69c9SMark Rutland WARN_ON_ONCE(mm == current->mm); 9162655421aSNicholas Piggin 9172655421aSNicholas Piggin /* Ensure no CPUs are using this as their lazy tlb mm */ 9182655421aSNicholas Piggin cleanup_lazy_tlbs(mm); 9192655421aSNicholas Piggin 9203eda69c9SMark Rutland WARN_ON_ONCE(mm == current->active_mm); 921d70f2a14SAndrew Morton mm_free_pgd(mm); 922d70f2a14SAndrew Morton destroy_context(mm); 923984cfe4eSJason Gunthorpe mmu_notifier_subscriptions_destroy(mm); 924d70f2a14SAndrew Morton check_mm(mm); 925d70f2a14SAndrew Morton put_user_ns(mm->user_ns); 9262667ed10SFenghua Yu mm_pasid_drop(mm); 927223baf9dSMathieu Desnoyers mm_destroy_cid(mm); 928f1a79412SShakeel Butt 929f1a79412SShakeel Butt for (i = 0; i < NR_MM_COUNTERS; i++) 930f1a79412SShakeel Butt percpu_counter_destroy(&mm->rss_stat[i]); 931d70f2a14SAndrew Morton free_mm(mm); 932d70f2a14SAndrew Morton } 933d34bc48fSAndrew Morton EXPORT_SYMBOL_GPL(__mmdrop); 934d70f2a14SAndrew Morton 935d70f2a14SAndrew Morton static void mmdrop_async_fn(struct work_struct *work) 936d70f2a14SAndrew Morton { 937d70f2a14SAndrew Morton struct mm_struct *mm; 938d70f2a14SAndrew Morton 939d70f2a14SAndrew Morton mm = container_of(work, struct mm_struct, async_put_work); 940d70f2a14SAndrew Morton __mmdrop(mm); 941d70f2a14SAndrew Morton } 942d70f2a14SAndrew Morton 943d70f2a14SAndrew Morton static void mmdrop_async(struct mm_struct *mm) 944d70f2a14SAndrew Morton { 945d70f2a14SAndrew Morton if (unlikely(atomic_dec_and_test(&mm->mm_count))) { 946d70f2a14SAndrew Morton INIT_WORK(&mm->async_put_work, mmdrop_async_fn); 947d70f2a14SAndrew Morton schedule_work(&mm->async_put_work); 948d70f2a14SAndrew Morton } 949d70f2a14SAndrew Morton } 950d70f2a14SAndrew Morton 9511da177e4SLinus Torvalds static inline void free_signal_struct(struct signal_struct *sig) 9521da177e4SLinus Torvalds { 9531da177e4SLinus Torvalds taskstats_tgid_free(sig); 9541da177e4SLinus Torvalds sched_autogroup_exit(sig); 9551da177e4SLinus Torvalds /* 9561da177e4SLinus Torvalds * __mmdrop is not safe to call from softirq context on x86 due to 9571da177e4SLinus Torvalds * pgd_dtor so postpone it to the async context 9581da177e4SLinus Torvalds */ 9591da177e4SLinus Torvalds if (sig->oom_mm) 9601da177e4SLinus Torvalds mmdrop_async(sig->oom_mm); 9611da177e4SLinus Torvalds kmem_cache_free(signal_cachep, sig); 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds static inline void put_signal_struct(struct signal_struct *sig) 9651da177e4SLinus Torvalds { 96660d4de3fSElena Reshetova if (refcount_dec_and_test(&sig->sigcnt)) 9671da177e4SLinus Torvalds free_signal_struct(sig); 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds void __put_task_struct(struct task_struct *tsk) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds WARN_ON(!tsk->exit_state); 973ec1d2819SElena Reshetova WARN_ON(refcount_read(&tsk->usage)); 9741da177e4SLinus Torvalds WARN_ON(tsk == current); 9751da177e4SLinus Torvalds 9760f212204SJens Axboe io_uring_free(tsk); 9771da177e4SLinus Torvalds cgroup_free(tsk); 97816d51a59SJann Horn task_numa_free(tsk, true); 9791da177e4SLinus Torvalds security_task_free(tsk); 9801da177e4SLinus Torvalds exit_creds(tsk); 9811da177e4SLinus Torvalds delayacct_tsk_free(tsk); 9821da177e4SLinus Torvalds put_signal_struct(tsk->signal); 9836e33cad0SPeter Zijlstra sched_core_free(tsk); 9841da177e4SLinus Torvalds free_task(tsk); 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__put_task_struct); 9871da177e4SLinus Torvalds 988*d243b344SWander Lairson Costa void __put_task_struct_rcu_cb(struct rcu_head *rhp) 989*d243b344SWander Lairson Costa { 990*d243b344SWander Lairson Costa struct task_struct *task = container_of(rhp, struct task_struct, rcu); 991*d243b344SWander Lairson Costa 992*d243b344SWander Lairson Costa __put_task_struct(task); 993*d243b344SWander Lairson Costa } 994*d243b344SWander Lairson Costa EXPORT_SYMBOL_GPL(__put_task_struct_rcu_cb); 995*d243b344SWander Lairson Costa 9961da177e4SLinus Torvalds void __init __weak arch_task_cache_init(void) { } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds /* 9991da177e4SLinus Torvalds * set_max_threads 10001da177e4SLinus Torvalds */ 10011da177e4SLinus Torvalds static void set_max_threads(unsigned int max_threads_suggested) 10021da177e4SLinus Torvalds { 10031da177e4SLinus Torvalds u64 threads; 1004ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /* 10071da177e4SLinus Torvalds * The number of threads shall be limited such that the thread 10081da177e4SLinus Torvalds * structures may only consume a small part of the available memory. 10091da177e4SLinus Torvalds */ 10103d6357deSArun KS if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64) 10111da177e4SLinus Torvalds threads = MAX_THREADS; 10121da177e4SLinus Torvalds else 10133d6357deSArun KS threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE, 10141da177e4SLinus Torvalds (u64) THREAD_SIZE * 8UL); 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds if (threads > max_threads_suggested) 10171da177e4SLinus Torvalds threads = max_threads_suggested; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 10231da177e4SLinus Torvalds /* Initialized by the architecture: */ 10241da177e4SLinus Torvalds int arch_task_struct_size __read_mostly; 10251da177e4SLinus Torvalds #endif 10261da177e4SLinus Torvalds 10274189ff23SChristoph Hellwig #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 10285905429aSKees Cook static void task_struct_whitelist(unsigned long *offset, unsigned long *size) 10295905429aSKees Cook { 10305905429aSKees Cook /* Fetch thread_struct whitelist for the architecture. */ 10315905429aSKees Cook arch_thread_struct_whitelist(offset, size); 10325905429aSKees Cook 10335905429aSKees Cook /* 10345905429aSKees Cook * Handle zero-sized whitelist or empty thread_struct, otherwise 10355905429aSKees Cook * adjust offset to position of thread_struct in task_struct. 10365905429aSKees Cook */ 10375905429aSKees Cook if (unlikely(*size == 0)) 10385905429aSKees Cook *offset = 0; 10395905429aSKees Cook else 10405905429aSKees Cook *offset += offsetof(struct task_struct, thread); 10415905429aSKees Cook } 10424189ff23SChristoph Hellwig #endif /* CONFIG_ARCH_TASK_STRUCT_ALLOCATOR */ 10435905429aSKees Cook 10441da177e4SLinus Torvalds void __init fork_init(void) 10451da177e4SLinus Torvalds { 10461da177e4SLinus Torvalds int i; 10471da177e4SLinus Torvalds #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 10481da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 10491da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN 0 10501da177e4SLinus Torvalds #endif 10511da177e4SLinus Torvalds int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); 10525905429aSKees Cook unsigned long useroffset, usersize; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 10555905429aSKees Cook task_struct_whitelist(&useroffset, &usersize); 10565905429aSKees Cook task_struct_cachep = kmem_cache_create_usercopy("task_struct", 10571da177e4SLinus Torvalds arch_task_struct_size, align, 10585905429aSKees Cook SLAB_PANIC|SLAB_ACCOUNT, 10595905429aSKees Cook useroffset, usersize, NULL); 10601da177e4SLinus Torvalds #endif 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds /* do the arch specific task caches init */ 10631da177e4SLinus Torvalds arch_task_cache_init(); 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds set_max_threads(MAX_THREADS); 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 10681da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 10691da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 10701da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 10711da177e4SLinus Torvalds 1072de399236SAlexey Gladkov for (i = 0; i < UCOUNT_COUNTS; i++) 10731da177e4SLinus Torvalds init_user_ns.ucount_max[i] = max_threads/2; 10741da177e4SLinus Torvalds 1075de399236SAlexey Gladkov set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_NPROC, RLIM_INFINITY); 1076de399236SAlexey Gladkov set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_MSGQUEUE, RLIM_INFINITY); 1077de399236SAlexey Gladkov set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_SIGPENDING, RLIM_INFINITY); 1078de399236SAlexey Gladkov set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_MEMLOCK, RLIM_INFINITY); 107921d1c5e3SAlexey Gladkov 10801da177e4SLinus Torvalds #ifdef CONFIG_VMAP_STACK 10811da177e4SLinus Torvalds cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", 10821da177e4SLinus Torvalds NULL, free_vm_stack_cache); 10831da177e4SLinus Torvalds #endif 10841da177e4SLinus Torvalds 1085d08b9f0cSSami Tolvanen scs_init(); 1086d08b9f0cSSami Tolvanen 10871da177e4SLinus Torvalds lockdep_init_task(&init_task); 1088aad42dd4SNadav Amit uprobes_init(); 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds int __weak arch_dup_task_struct(struct task_struct *dst, 10921da177e4SLinus Torvalds struct task_struct *src) 10931da177e4SLinus Torvalds { 10941da177e4SLinus Torvalds *dst = *src; 10951da177e4SLinus Torvalds return 0; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds void set_task_stack_end_magic(struct task_struct *tsk) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds unsigned long *stackend; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds stackend = end_of_stack(tsk); 11031da177e4SLinus Torvalds *stackend = STACK_END_MAGIC; /* for overflow detection */ 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 11071da177e4SLinus Torvalds { 11081da177e4SLinus Torvalds struct task_struct *tsk; 11091da177e4SLinus Torvalds int err; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds if (node == NUMA_NO_NODE) 11121da177e4SLinus Torvalds node = tsk_fork_get_node(orig); 11131da177e4SLinus Torvalds tsk = alloc_task_struct_node(node); 11141da177e4SLinus Torvalds if (!tsk) 11151da177e4SLinus Torvalds return NULL; 11161da177e4SLinus Torvalds 1117546c42b2SSebastian Andrzej Siewior err = arch_dup_task_struct(tsk, orig); 1118546c42b2SSebastian Andrzej Siewior if (err) 11191da177e4SLinus Torvalds goto free_tsk; 11201da177e4SLinus Torvalds 11217865aba3SSebastian Andrzej Siewior err = alloc_thread_stack_node(tsk, node); 11227865aba3SSebastian Andrzej Siewior if (err) 11231da177e4SLinus Torvalds goto free_tsk; 11249b6f7e16SRoman Gushchin 11251da177e4SLinus Torvalds #ifdef CONFIG_THREAD_INFO_IN_TASK 1126f0b89d39SElena Reshetova refcount_set(&tsk->stack_refcount, 1); 11271da177e4SLinus Torvalds #endif 11281a03d3f1SSebastian Andrzej Siewior account_kernel_stack(tsk, 1); 11291da177e4SLinus Torvalds 1130d08b9f0cSSami Tolvanen err = scs_prepare(tsk, node); 1131d08b9f0cSSami Tolvanen if (err) 1132d08b9f0cSSami Tolvanen goto free_stack; 1133d08b9f0cSSami Tolvanen 11341da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 11351da177e4SLinus Torvalds /* 11361da177e4SLinus Torvalds * We must handle setting up seccomp filters once we're under 11371da177e4SLinus Torvalds * the sighand lock in case orig has changed between now and 11381da177e4SLinus Torvalds * then. Until then, filter must be NULL to avoid messing up 11391da177e4SLinus Torvalds * the usage counts on the error path calling free_task. 11401da177e4SLinus Torvalds */ 11411da177e4SLinus Torvalds tsk->seccomp.filter = NULL; 11421da177e4SLinus Torvalds #endif 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds setup_thread_stack(tsk, orig); 11451da177e4SLinus Torvalds clear_user_return_notifier(tsk); 11461da177e4SLinus Torvalds clear_tsk_need_resched(tsk); 11471da177e4SLinus Torvalds set_task_stack_end_magic(tsk); 11481446e1dfSGabriel Krisman Bertazi clear_syscall_work_syscall_user_dispatch(tsk); 11491da177e4SLinus Torvalds 1150050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR 11511da177e4SLinus Torvalds tsk->stack_canary = get_random_canary(); 11521da177e4SLinus Torvalds #endif 11533bd37062SSebastian Andrzej Siewior if (orig->cpus_ptr == &orig->cpus_mask) 11543bd37062SSebastian Andrzej Siewior tsk->cpus_ptr = &tsk->cpus_mask; 1155b90ca8baSWill Deacon dup_user_cpus_ptr(tsk, orig, node); 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /* 11580ff7b2cfSEric W. Biederman * One for the user space visible state that goes away when reaped. 11590ff7b2cfSEric W. Biederman * One for the scheduler. 11601da177e4SLinus Torvalds */ 11610ff7b2cfSEric W. Biederman refcount_set(&tsk->rcu_users, 2); 11620ff7b2cfSEric W. Biederman /* One for the rcu users */ 11630ff7b2cfSEric W. Biederman refcount_set(&tsk->usage, 1); 11641da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_IO_TRACE 11651da177e4SLinus Torvalds tsk->btrace_seq = 0; 11661da177e4SLinus Torvalds #endif 11671da177e4SLinus Torvalds tsk->splice_pipe = NULL; 11681da177e4SLinus Torvalds tsk->task_frag.page = NULL; 11691da177e4SLinus Torvalds tsk->wake_q.next = NULL; 1170e32cf5dfSEric W. Biederman tsk->worker_private = NULL; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds kcov_task_init(tsk); 117350b5e49cSAlexander Potapenko kmsan_task_create(tsk); 11745fbda3ecSThomas Gleixner kmap_local_fork(tsk); 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds #ifdef CONFIG_FAULT_INJECTION 11771da177e4SLinus Torvalds tsk->fail_nth = 0; 11781da177e4SLinus Torvalds #endif 11791da177e4SLinus Torvalds 11802c323017SJosef Bacik #ifdef CONFIG_BLK_CGROUP 1181f05837edSChristoph Hellwig tsk->throttle_disk = NULL; 11822c323017SJosef Bacik tsk->use_memdelay = 0; 11832c323017SJosef Bacik #endif 11842c323017SJosef Bacik 1185a3d29e82SPeter Zijlstra #ifdef CONFIG_IOMMU_SVA 1186a3d29e82SPeter Zijlstra tsk->pasid_activated = 0; 1187a3d29e82SPeter Zijlstra #endif 1188a3d29e82SPeter Zijlstra 1189d46eb14bSShakeel Butt #ifdef CONFIG_MEMCG 1190d46eb14bSShakeel Butt tsk->active_memcg = NULL; 1191d46eb14bSShakeel Butt #endif 1192b041b525STony Luck 1193b041b525STony Luck #ifdef CONFIG_CPU_SUP_INTEL 1194b041b525STony Luck tsk->reported_split_lock = 0; 1195b041b525STony Luck #endif 1196b041b525STony Luck 1197af7f588dSMathieu Desnoyers #ifdef CONFIG_SCHED_MM_CID 1198af7f588dSMathieu Desnoyers tsk->mm_cid = -1; 1199223baf9dSMathieu Desnoyers tsk->last_mm_cid = -1; 1200af7f588dSMathieu Desnoyers tsk->mm_cid_active = 0; 1201223baf9dSMathieu Desnoyers tsk->migrate_from_cpu = -1; 1202af7f588dSMathieu Desnoyers #endif 12031da177e4SLinus Torvalds return tsk; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds free_stack: 12061a03d3f1SSebastian Andrzej Siewior exit_task_stack_account(tsk); 12071da177e4SLinus Torvalds free_thread_stack(tsk); 12081da177e4SLinus Torvalds free_tsk: 12091da177e4SLinus Torvalds free_task_struct(tsk); 12101da177e4SLinus Torvalds return NULL; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 12141da177e4SLinus Torvalds 12154cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 12164cb0e11bSHidehiro Kawai 12174cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 12184cb0e11bSHidehiro Kawai { 12194cb0e11bSHidehiro Kawai default_dump_filter = 12204cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 12214cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 12224cb0e11bSHidehiro Kawai return 1; 12234cb0e11bSHidehiro Kawai } 12244cb0e11bSHidehiro Kawai 12254cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 12264cb0e11bSHidehiro Kawai 12271da177e4SLinus Torvalds #include <linux/init_task.h> 12281da177e4SLinus Torvalds 1229858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 1230858f0993SAlexey Dobriyan { 1231858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 1232858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 1233db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 1234858f0993SAlexey Dobriyan #endif 1235858f0993SAlexey Dobriyan } 1236858f0993SAlexey Dobriyan 1237c3f3ce04SAndrea Arcangeli static __always_inline void mm_clear_owner(struct mm_struct *mm, 1238c3f3ce04SAndrea Arcangeli struct task_struct *p) 1239c3f3ce04SAndrea Arcangeli { 1240c3f3ce04SAndrea Arcangeli #ifdef CONFIG_MEMCG 1241c3f3ce04SAndrea Arcangeli if (mm->owner == p) 1242c3f3ce04SAndrea Arcangeli WRITE_ONCE(mm->owner, NULL); 1243c3f3ce04SAndrea Arcangeli #endif 1244c3f3ce04SAndrea Arcangeli } 1245c3f3ce04SAndrea Arcangeli 124633144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 124733144e84SVladimir Davydov { 124833144e84SVladimir Davydov #ifdef CONFIG_MEMCG 124933144e84SVladimir Davydov mm->owner = p; 125033144e84SVladimir Davydov #endif 125133144e84SVladimir Davydov } 125233144e84SVladimir Davydov 1253355627f5SEric Biggers static void mm_init_uprobes_state(struct mm_struct *mm) 1254355627f5SEric Biggers { 1255355627f5SEric Biggers #ifdef CONFIG_UPROBES 1256355627f5SEric Biggers mm->uprobes_state.xol_area = NULL; 1257355627f5SEric Biggers #endif 1258355627f5SEric Biggers } 1259355627f5SEric Biggers 1260bfedb589SEric W. Biederman static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, 1261bfedb589SEric W. Biederman struct user_namespace *user_ns) 12621da177e4SLinus Torvalds { 1263f1a79412SShakeel Butt int i; 1264f1a79412SShakeel Butt 1265d4af56c5SLiam R. Howlett mt_init_flags(&mm->mm_mt, MM_MT_FLAGS); 1266d4af56c5SLiam R. Howlett mt_set_external_lock(&mm->mm_mt, &mm->mmap_lock); 12671da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 12681da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 126957efa1feSJason Gunthorpe seqcount_init(&mm->write_protect_seq); 1270d8ed45c5SMichel Lespinasse mmap_init_lock(mm); 12711da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 12725e31275cSSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 12735e31275cSSuren Baghdasaryan mm->mm_lock_seq = 0; 12745e31275cSSuren Baghdasaryan #endif 1275af5b0f6aSKirill A. Shutemov mm_pgtables_bytes_init(mm); 127641f727fdSVladimir Davydov mm->map_count = 0; 127741f727fdSVladimir Davydov mm->locked_vm = 0; 127870f8a3caSDavidlohr Bueso atomic64_set(&mm->pinned_vm, 0); 1279d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 12801da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 128188aa7cc6SYang Shi spin_lock_init(&mm->arg_lock); 128241f727fdSVladimir Davydov mm_init_cpumask(mm); 1283858f0993SAlexey Dobriyan mm_init_aio(mm); 1284cf475ad2SBalbir Singh mm_init_owner(mm, p); 1285a6cbd440SFenghua Yu mm_pasid_init(mm); 12862b7e8665SEric Biggers RCU_INIT_POINTER(mm->exe_file, NULL); 1287984cfe4eSJason Gunthorpe mmu_notifier_subscriptions_init(mm); 128816af97dcSNadav Amit init_tlb_flush_pending(mm); 128941f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 129041f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 129141f727fdSVladimir Davydov #endif 1292355627f5SEric Biggers mm_init_uprobes_state(mm); 129313db8c50SLiu Zixian hugetlb_count_init(mm); 12941da177e4SLinus Torvalds 1295a0715cc2SAlex Thorlton if (current->mm) { 1296a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 1297a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 1298a0715cc2SAlex Thorlton } else { 1299a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 13001da177e4SLinus Torvalds mm->def_flags = 0; 1301a0715cc2SAlex Thorlton } 1302a0715cc2SAlex Thorlton 130341f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 130441f727fdSVladimir Davydov goto fail_nopgd; 130578fb7466SPavel Emelianov 130641f727fdSVladimir Davydov if (init_new_context(p, mm)) 130741f727fdSVladimir Davydov goto fail_nocontext; 130841f727fdSVladimir Davydov 1309223baf9dSMathieu Desnoyers if (mm_alloc_cid(mm)) 1310223baf9dSMathieu Desnoyers goto fail_cid; 1311223baf9dSMathieu Desnoyers 1312f1a79412SShakeel Butt for (i = 0; i < NR_MM_COUNTERS; i++) 1313f1a79412SShakeel Butt if (percpu_counter_init(&mm->rss_stat[i], 0, GFP_KERNEL_ACCOUNT)) 1314f1a79412SShakeel Butt goto fail_pcpu; 1315f1a79412SShakeel Butt 1316bfedb589SEric W. Biederman mm->user_ns = get_user_ns(user_ns); 1317bd74fdaeSYu Zhao lru_gen_init_mm(mm); 131841f727fdSVladimir Davydov return mm; 131941f727fdSVladimir Davydov 1320f1a79412SShakeel Butt fail_pcpu: 1321f1a79412SShakeel Butt while (i > 0) 1322f1a79412SShakeel Butt percpu_counter_destroy(&mm->rss_stat[--i]); 1323223baf9dSMathieu Desnoyers mm_destroy_cid(mm); 1324223baf9dSMathieu Desnoyers fail_cid: 1325b20b0368SMathieu Desnoyers destroy_context(mm); 132641f727fdSVladimir Davydov fail_nocontext: 132741f727fdSVladimir Davydov mm_free_pgd(mm); 132841f727fdSVladimir Davydov fail_nopgd: 13291da177e4SLinus Torvalds free_mm(mm); 13301da177e4SLinus Torvalds return NULL; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds /* 13341da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 13351da177e4SLinus Torvalds */ 13361da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 13371da177e4SLinus Torvalds { 13381da177e4SLinus Torvalds struct mm_struct *mm; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds mm = allocate_mm(); 1341de03c72cSKOSAKI Motohiro if (!mm) 1342de03c72cSKOSAKI Motohiro return NULL; 1343de03c72cSKOSAKI Motohiro 13441da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 1345bfedb589SEric W. Biederman return mm_init(mm, current, current_user_ns()); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 1348ec8d7c14SMichal Hocko static inline void __mmput(struct mm_struct *mm) 13491da177e4SLinus Torvalds { 1350ec8d7c14SMichal Hocko VM_BUG_ON(atomic_read(&mm->mm_users)); 13510ae26f1bSAndrew Morton 1352d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 13531da177e4SLinus Torvalds exit_aio(mm); 13541c2fb7a4SAndrea Arcangeli ksm_exit(mm); 1355ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 13561da177e4SLinus Torvalds exit_mmap(mm); 13576fcb52a5SAaron Lu mm_put_huge_zero_page(mm); 1358925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 13591da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 13601da177e4SLinus Torvalds spin_lock(&mmlist_lock); 13611da177e4SLinus Torvalds list_del(&mm->mmlist); 13621da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 13631da177e4SLinus Torvalds } 1364801460d0SHiroshi Shimamoto if (mm->binfmt) 1365801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 1366bd74fdaeSYu Zhao lru_gen_del_mm(mm); 13671da177e4SLinus Torvalds mmdrop(mm); 13681da177e4SLinus Torvalds } 1369ec8d7c14SMichal Hocko 1370ec8d7c14SMichal Hocko /* 1371ec8d7c14SMichal Hocko * Decrement the use count and release all resources for an mm. 1372ec8d7c14SMichal Hocko */ 1373ec8d7c14SMichal Hocko void mmput(struct mm_struct *mm) 1374ec8d7c14SMichal Hocko { 1375ec8d7c14SMichal Hocko might_sleep(); 1376ec8d7c14SMichal Hocko 1377ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) 1378ec8d7c14SMichal Hocko __mmput(mm); 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 13811da177e4SLinus Torvalds 1382a1b2289cSSherry Yang #ifdef CONFIG_MMU 1383a1b2289cSSherry Yang static void mmput_async_fn(struct work_struct *work) 1384a1b2289cSSherry Yang { 1385a1b2289cSSherry Yang struct mm_struct *mm = container_of(work, struct mm_struct, 1386a1b2289cSSherry Yang async_put_work); 1387a1b2289cSSherry Yang 1388a1b2289cSSherry Yang __mmput(mm); 1389a1b2289cSSherry Yang } 1390a1b2289cSSherry Yang 1391a1b2289cSSherry Yang void mmput_async(struct mm_struct *mm) 1392a1b2289cSSherry Yang { 1393a1b2289cSSherry Yang if (atomic_dec_and_test(&mm->mm_users)) { 1394a1b2289cSSherry Yang INIT_WORK(&mm->async_put_work, mmput_async_fn); 1395a1b2289cSSherry Yang schedule_work(&mm->async_put_work); 1396a1b2289cSSherry Yang } 1397a1b2289cSSherry Yang } 139885eaeb50SYishai Hadas EXPORT_SYMBOL_GPL(mmput_async); 1399a1b2289cSSherry Yang #endif 1400a1b2289cSSherry Yang 140190f31d0eSKonstantin Khlebnikov /** 140290f31d0eSKonstantin Khlebnikov * set_mm_exe_file - change a reference to the mm's executable file 140390f31d0eSKonstantin Khlebnikov * 140490f31d0eSKonstantin Khlebnikov * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 140590f31d0eSKonstantin Khlebnikov * 14066e399cd1SDavidlohr Bueso * Main users are mmput() and sys_execve(). Callers prevent concurrent 14076e399cd1SDavidlohr Bueso * invocations: in mmput() nobody alive left, in execve task is single 140835d7bdc8SDavid Hildenbrand * threaded. 1409fe69d560SDavid Hildenbrand * 1410fe69d560SDavid Hildenbrand * Can only fail if new_exe_file != NULL. 141190f31d0eSKonstantin Khlebnikov */ 1412fe69d560SDavid Hildenbrand int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 141338646013SJiri Slaby { 14146e399cd1SDavidlohr Bueso struct file *old_exe_file; 14156e399cd1SDavidlohr Bueso 14166e399cd1SDavidlohr Bueso /* 14176e399cd1SDavidlohr Bueso * It is safe to dereference the exe_file without RCU as 14186e399cd1SDavidlohr Bueso * this function is only called if nobody else can access 14196e399cd1SDavidlohr Bueso * this mm -- see comment above for justification. 14206e399cd1SDavidlohr Bueso */ 14216e399cd1SDavidlohr Bueso old_exe_file = rcu_dereference_raw(mm->exe_file); 142290f31d0eSKonstantin Khlebnikov 1423fe69d560SDavid Hildenbrand if (new_exe_file) { 1424fe69d560SDavid Hildenbrand /* 1425fe69d560SDavid Hildenbrand * We expect the caller (i.e., sys_execve) to already denied 1426fe69d560SDavid Hildenbrand * write access, so this is unlikely to fail. 1427fe69d560SDavid Hildenbrand */ 1428fe69d560SDavid Hildenbrand if (unlikely(deny_write_access(new_exe_file))) 1429fe69d560SDavid Hildenbrand return -EACCES; 143038646013SJiri Slaby get_file(new_exe_file); 1431fe69d560SDavid Hildenbrand } 143290f31d0eSKonstantin Khlebnikov rcu_assign_pointer(mm->exe_file, new_exe_file); 1433fe69d560SDavid Hildenbrand if (old_exe_file) { 1434fe69d560SDavid Hildenbrand allow_write_access(old_exe_file); 143590f31d0eSKonstantin Khlebnikov fput(old_exe_file); 143638646013SJiri Slaby } 1437fe69d560SDavid Hildenbrand return 0; 1438fe69d560SDavid Hildenbrand } 143938646013SJiri Slaby 144090f31d0eSKonstantin Khlebnikov /** 144135d7bdc8SDavid Hildenbrand * replace_mm_exe_file - replace a reference to the mm's executable file 144235d7bdc8SDavid Hildenbrand * 144335d7bdc8SDavid Hildenbrand * This changes mm's executable file (shown as symlink /proc/[pid]/exe), 144435d7bdc8SDavid Hildenbrand * dealing with concurrent invocation and without grabbing the mmap lock in 144535d7bdc8SDavid Hildenbrand * write mode. 144635d7bdc8SDavid Hildenbrand * 144735d7bdc8SDavid Hildenbrand * Main user is sys_prctl(PR_SET_MM_MAP/EXE_FILE). 144835d7bdc8SDavid Hildenbrand */ 144935d7bdc8SDavid Hildenbrand int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 145035d7bdc8SDavid Hildenbrand { 145135d7bdc8SDavid Hildenbrand struct vm_area_struct *vma; 145235d7bdc8SDavid Hildenbrand struct file *old_exe_file; 145335d7bdc8SDavid Hildenbrand int ret = 0; 145435d7bdc8SDavid Hildenbrand 145535d7bdc8SDavid Hildenbrand /* Forbid mm->exe_file change if old file still mapped. */ 145635d7bdc8SDavid Hildenbrand old_exe_file = get_mm_exe_file(mm); 145735d7bdc8SDavid Hildenbrand if (old_exe_file) { 1458fa5e5876SMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, 0); 145935d7bdc8SDavid Hildenbrand mmap_read_lock(mm); 1460fa5e5876SMatthew Wilcox (Oracle) for_each_vma(vmi, vma) { 146135d7bdc8SDavid Hildenbrand if (!vma->vm_file) 146235d7bdc8SDavid Hildenbrand continue; 146335d7bdc8SDavid Hildenbrand if (path_equal(&vma->vm_file->f_path, 1464fa5e5876SMatthew Wilcox (Oracle) &old_exe_file->f_path)) { 146535d7bdc8SDavid Hildenbrand ret = -EBUSY; 1466fa5e5876SMatthew Wilcox (Oracle) break; 1467fa5e5876SMatthew Wilcox (Oracle) } 146835d7bdc8SDavid Hildenbrand } 146935d7bdc8SDavid Hildenbrand mmap_read_unlock(mm); 147035d7bdc8SDavid Hildenbrand fput(old_exe_file); 147135d7bdc8SDavid Hildenbrand if (ret) 147235d7bdc8SDavid Hildenbrand return ret; 147335d7bdc8SDavid Hildenbrand } 147435d7bdc8SDavid Hildenbrand 147535d7bdc8SDavid Hildenbrand /* set the new file, lockless */ 1476fe69d560SDavid Hildenbrand ret = deny_write_access(new_exe_file); 1477fe69d560SDavid Hildenbrand if (ret) 1478fe69d560SDavid Hildenbrand return -EACCES; 147935d7bdc8SDavid Hildenbrand get_file(new_exe_file); 1480fe69d560SDavid Hildenbrand 148135d7bdc8SDavid Hildenbrand old_exe_file = xchg(&mm->exe_file, new_exe_file); 1482fe69d560SDavid Hildenbrand if (old_exe_file) { 1483fe69d560SDavid Hildenbrand /* 1484fe69d560SDavid Hildenbrand * Don't race with dup_mmap() getting the file and disallowing 1485fe69d560SDavid Hildenbrand * write access while someone might open the file writable. 1486fe69d560SDavid Hildenbrand */ 1487fe69d560SDavid Hildenbrand mmap_read_lock(mm); 1488fe69d560SDavid Hildenbrand allow_write_access(old_exe_file); 148935d7bdc8SDavid Hildenbrand fput(old_exe_file); 1490fe69d560SDavid Hildenbrand mmap_read_unlock(mm); 1491fe69d560SDavid Hildenbrand } 149235d7bdc8SDavid Hildenbrand return 0; 149335d7bdc8SDavid Hildenbrand } 149438646013SJiri Slaby 149590f31d0eSKonstantin Khlebnikov /** 149690f31d0eSKonstantin Khlebnikov * get_mm_exe_file - acquire a reference to the mm's executable file 149790f31d0eSKonstantin Khlebnikov * 149890f31d0eSKonstantin Khlebnikov * Returns %NULL if mm has no associated executable file. 149990f31d0eSKonstantin Khlebnikov * User must release file via fput(). 150090f31d0eSKonstantin Khlebnikov */ 150138646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 150238646013SJiri Slaby { 150338646013SJiri Slaby struct file *exe_file; 150438646013SJiri Slaby 150590f31d0eSKonstantin Khlebnikov rcu_read_lock(); 150690f31d0eSKonstantin Khlebnikov exe_file = rcu_dereference(mm->exe_file); 150790f31d0eSKonstantin Khlebnikov if (exe_file && !get_file_rcu(exe_file)) 150890f31d0eSKonstantin Khlebnikov exe_file = NULL; 150990f31d0eSKonstantin Khlebnikov rcu_read_unlock(); 151038646013SJiri Slaby return exe_file; 151138646013SJiri Slaby } 151238646013SJiri Slaby 15131da177e4SLinus Torvalds /** 1514cd81a917SMateusz Guzik * get_task_exe_file - acquire a reference to the task's executable file 1515cd81a917SMateusz Guzik * 1516cd81a917SMateusz Guzik * Returns %NULL if task's mm (if any) has no associated executable file or 1517cd81a917SMateusz Guzik * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 1518cd81a917SMateusz Guzik * User must release file via fput(). 1519cd81a917SMateusz Guzik */ 1520cd81a917SMateusz Guzik struct file *get_task_exe_file(struct task_struct *task) 1521cd81a917SMateusz Guzik { 1522cd81a917SMateusz Guzik struct file *exe_file = NULL; 1523cd81a917SMateusz Guzik struct mm_struct *mm; 1524cd81a917SMateusz Guzik 1525cd81a917SMateusz Guzik task_lock(task); 1526cd81a917SMateusz Guzik mm = task->mm; 1527cd81a917SMateusz Guzik if (mm) { 1528cd81a917SMateusz Guzik if (!(task->flags & PF_KTHREAD)) 1529cd81a917SMateusz Guzik exe_file = get_mm_exe_file(mm); 1530cd81a917SMateusz Guzik } 1531cd81a917SMateusz Guzik task_unlock(task); 1532cd81a917SMateusz Guzik return exe_file; 1533cd81a917SMateusz Guzik } 1534cd81a917SMateusz Guzik 1535cd81a917SMateusz Guzik /** 15361da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 15371da177e4SLinus Torvalds * 1538246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 15391da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 15401da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 15411da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 15421da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 15431da177e4SLinus Torvalds */ 15441da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 15451da177e4SLinus Torvalds { 15461da177e4SLinus Torvalds struct mm_struct *mm; 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds task_lock(task); 15491da177e4SLinus Torvalds mm = task->mm; 15501da177e4SLinus Torvalds if (mm) { 1551246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 15521da177e4SLinus Torvalds mm = NULL; 15531da177e4SLinus Torvalds else 15543fce371bSVegard Nossum mmget(mm); 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds task_unlock(task); 15571da177e4SLinus Torvalds return mm; 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 15601da177e4SLinus Torvalds 15618cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 15628cdb878dSChristopher Yeoh { 15638cdb878dSChristopher Yeoh struct mm_struct *mm; 15648cdb878dSChristopher Yeoh int err; 15658cdb878dSChristopher Yeoh 1566f7cfd871SEric W. Biederman err = down_read_killable(&task->signal->exec_update_lock); 15678cdb878dSChristopher Yeoh if (err) 15688cdb878dSChristopher Yeoh return ERR_PTR(err); 15698cdb878dSChristopher Yeoh 15708cdb878dSChristopher Yeoh mm = get_task_mm(task); 15718cdb878dSChristopher Yeoh if (mm && mm != current->mm && 15728cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 15738cdb878dSChristopher Yeoh mmput(mm); 15748cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 15758cdb878dSChristopher Yeoh } 1576f7cfd871SEric W. Biederman up_read(&task->signal->exec_update_lock); 15778cdb878dSChristopher Yeoh 15788cdb878dSChristopher Yeoh return mm; 15798cdb878dSChristopher Yeoh } 15808cdb878dSChristopher Yeoh 158157b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 1582c415c3b4SOleg Nesterov { 1583d68b46feSOleg Nesterov struct completion *vfork; 1584c415c3b4SOleg Nesterov 1585d68b46feSOleg Nesterov task_lock(tsk); 1586d68b46feSOleg Nesterov vfork = tsk->vfork_done; 1587d68b46feSOleg Nesterov if (likely(vfork)) { 1588c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 1589d68b46feSOleg Nesterov complete(vfork); 1590d68b46feSOleg Nesterov } 1591d68b46feSOleg Nesterov task_unlock(tsk); 1592d68b46feSOleg Nesterov } 1593d68b46feSOleg Nesterov 1594d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 1595d68b46feSOleg Nesterov struct completion *vfork) 1596d68b46feSOleg Nesterov { 1597f5d39b02SPeter Zijlstra unsigned int state = TASK_UNINTERRUPTIBLE|TASK_KILLABLE|TASK_FREEZABLE; 1598d68b46feSOleg Nesterov int killed; 1599d68b46feSOleg Nesterov 160076f969e8SRoman Gushchin cgroup_enter_frozen(); 1601f5d39b02SPeter Zijlstra killed = wait_for_completion_state(vfork, state); 160276f969e8SRoman Gushchin cgroup_leave_frozen(false); 1603d68b46feSOleg Nesterov 1604d68b46feSOleg Nesterov if (killed) { 1605d68b46feSOleg Nesterov task_lock(child); 1606d68b46feSOleg Nesterov child->vfork_done = NULL; 1607d68b46feSOleg Nesterov task_unlock(child); 1608d68b46feSOleg Nesterov } 1609d68b46feSOleg Nesterov 1610d68b46feSOleg Nesterov put_task_struct(child); 1611d68b46feSOleg Nesterov return killed; 1612c415c3b4SOleg Nesterov } 1613c415c3b4SOleg Nesterov 16141da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 16151da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 16161da177e4SLinus Torvalds * error success whatever. 16171da177e4SLinus Torvalds * 16181da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 16191da177e4SLinus Torvalds * from the current process. 16201da177e4SLinus Torvalds * 16211da177e4SLinus Torvalds * This difference is important for error handling, when we 16221da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 16231da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 16241da177e4SLinus Torvalds * restoring the old one. . . 16251da177e4SLinus Torvalds * Eric Biederman 10 January 1998 16261da177e4SLinus Torvalds */ 16274610ba7aSThomas Gleixner static void mm_release(struct task_struct *tsk, struct mm_struct *mm) 16281da177e4SLinus Torvalds { 16290326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 16300326f5a9SSrikar Dronamraju 16311da177e4SLinus Torvalds /* Get rid of any cached register state */ 16321da177e4SLinus Torvalds deactivate_mm(tsk, mm); 16331da177e4SLinus Torvalds 1634fec1d011SRoland McGrath /* 1635735f2770SMichal Hocko * Signal userspace if we're not exiting with a core dump 1636735f2770SMichal Hocko * because we want to leave the value intact for debugging 1637735f2770SMichal Hocko * purposes. 1638fec1d011SRoland McGrath */ 16399c8a8228SEric Dumazet if (tsk->clear_child_tid) { 164092307383SEric W. Biederman if (atomic_read(&mm->mm_users) > 1) { 16411da177e4SLinus Torvalds /* 16421da177e4SLinus Torvalds * We don't check the error code - if userspace has 16431da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 16441da177e4SLinus Torvalds */ 16459c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 16462de0db99SDominik Brodowski do_futex(tsk->clear_child_tid, FUTEX_WAKE, 16472de0db99SDominik Brodowski 1, NULL, NULL, 0, 0); 16489c8a8228SEric Dumazet } 16499c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 16501da177e4SLinus Torvalds } 1651f7505d64SKonstantin Khlebnikov 1652f7505d64SKonstantin Khlebnikov /* 1653f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 1654f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 1655f7505d64SKonstantin Khlebnikov */ 1656f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 1657f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 16581da177e4SLinus Torvalds } 16591da177e4SLinus Torvalds 16604610ba7aSThomas Gleixner void exit_mm_release(struct task_struct *tsk, struct mm_struct *mm) 16614610ba7aSThomas Gleixner { 1662150d7158SThomas Gleixner futex_exit_release(tsk); 16634610ba7aSThomas Gleixner mm_release(tsk, mm); 16644610ba7aSThomas Gleixner } 16654610ba7aSThomas Gleixner 16664610ba7aSThomas Gleixner void exec_mm_release(struct task_struct *tsk, struct mm_struct *mm) 16674610ba7aSThomas Gleixner { 1668150d7158SThomas Gleixner futex_exec_release(tsk); 16694610ba7aSThomas Gleixner mm_release(tsk, mm); 16704610ba7aSThomas Gleixner } 16714610ba7aSThomas Gleixner 167213585fa0SNadav Amit /** 167313585fa0SNadav Amit * dup_mm() - duplicates an existing mm structure 167413585fa0SNadav Amit * @tsk: the task_struct with which the new mm will be associated. 167513585fa0SNadav Amit * @oldmm: the mm to duplicate. 167613585fa0SNadav Amit * 167713585fa0SNadav Amit * Allocates a new mm structure and duplicates the provided @oldmm structure 167813585fa0SNadav Amit * content into it. 167913585fa0SNadav Amit * 168013585fa0SNadav Amit * Return: the duplicated mm or NULL on failure. 1681a0a7ec30SJANAK DESAI */ 168213585fa0SNadav Amit static struct mm_struct *dup_mm(struct task_struct *tsk, 168313585fa0SNadav Amit struct mm_struct *oldmm) 1684a0a7ec30SJANAK DESAI { 168513585fa0SNadav Amit struct mm_struct *mm; 1686a0a7ec30SJANAK DESAI int err; 1687a0a7ec30SJANAK DESAI 1688a0a7ec30SJANAK DESAI mm = allocate_mm(); 1689a0a7ec30SJANAK DESAI if (!mm) 1690a0a7ec30SJANAK DESAI goto fail_nomem; 1691a0a7ec30SJANAK DESAI 1692a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 1693a0a7ec30SJANAK DESAI 1694bfedb589SEric W. Biederman if (!mm_init(mm, tsk, mm->user_ns)) 1695a0a7ec30SJANAK DESAI goto fail_nomem; 1696a0a7ec30SJANAK DESAI 1697a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 1698a0a7ec30SJANAK DESAI if (err) 1699a0a7ec30SJANAK DESAI goto free_pt; 1700a0a7ec30SJANAK DESAI 1701a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 1702a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 1703a0a7ec30SJANAK DESAI 1704801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1705801460d0SHiroshi Shimamoto goto free_pt; 1706801460d0SHiroshi Shimamoto 1707a0a7ec30SJANAK DESAI return mm; 1708a0a7ec30SJANAK DESAI 1709a0a7ec30SJANAK DESAI free_pt: 1710801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 1711801460d0SHiroshi Shimamoto mm->binfmt = NULL; 1712c3f3ce04SAndrea Arcangeli mm_init_owner(mm, NULL); 1713a0a7ec30SJANAK DESAI mmput(mm); 1714a0a7ec30SJANAK DESAI 1715a0a7ec30SJANAK DESAI fail_nomem: 1716a0a7ec30SJANAK DESAI return NULL; 1717a0a7ec30SJANAK DESAI } 1718a0a7ec30SJANAK DESAI 17191da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 17241da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 172517406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 172617406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 1727a2e51445SDmitry Vyukov tsk->last_switch_time = 0; 172817406b82SMandeep Singh Baines #endif 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds tsk->mm = NULL; 17311da177e4SLinus Torvalds tsk->active_mm = NULL; 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds /* 17341da177e4SLinus Torvalds * Are we cloning a kernel thread? 17351da177e4SLinus Torvalds * 17361da177e4SLinus Torvalds * We need to steal a active VM for that.. 17371da177e4SLinus Torvalds */ 17381da177e4SLinus Torvalds oldmm = current->mm; 17391da177e4SLinus Torvalds if (!oldmm) 17401da177e4SLinus Torvalds return 0; 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 17433fce371bSVegard Nossum mmget(oldmm); 17441da177e4SLinus Torvalds mm = oldmm; 1745a6895399SRolf Eike Beer } else { 174613585fa0SNadav Amit mm = dup_mm(tsk, current->mm); 17471da177e4SLinus Torvalds if (!mm) 1748a6895399SRolf Eike Beer return -ENOMEM; 1749a6895399SRolf Eike Beer } 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds tsk->mm = mm; 17521da177e4SLinus Torvalds tsk->active_mm = mm; 1753af7f588dSMathieu Desnoyers sched_mm_cid_fork(tsk); 17541da177e4SLinus Torvalds return 0; 17551da177e4SLinus Torvalds } 17561da177e4SLinus Torvalds 1757a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 17581da177e4SLinus Torvalds { 1759498052bbSAl Viro struct fs_struct *fs = current->fs; 17601da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 1761498052bbSAl Viro /* tsk->fs is already what we want */ 17622a4419b5SNick Piggin spin_lock(&fs->lock); 1763498052bbSAl Viro if (fs->in_exec) { 17642a4419b5SNick Piggin spin_unlock(&fs->lock); 1765498052bbSAl Viro return -EAGAIN; 1766498052bbSAl Viro } 1767498052bbSAl Viro fs->users++; 17682a4419b5SNick Piggin spin_unlock(&fs->lock); 17691da177e4SLinus Torvalds return 0; 17701da177e4SLinus Torvalds } 1771498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 17721da177e4SLinus Torvalds if (!tsk->fs) 17731da177e4SLinus Torvalds return -ENOMEM; 17741da177e4SLinus Torvalds return 0; 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 177711f3f500SMike Christie static int copy_files(unsigned long clone_flags, struct task_struct *tsk, 177811f3f500SMike Christie int no_files) 1779a016f338SJANAK DESAI { 1780a016f338SJANAK DESAI struct files_struct *oldf, *newf; 1781a016f338SJANAK DESAI int error = 0; 1782a016f338SJANAK DESAI 1783a016f338SJANAK DESAI /* 1784a016f338SJANAK DESAI * A background process may not have any files ... 1785a016f338SJANAK DESAI */ 1786a016f338SJANAK DESAI oldf = current->files; 1787a016f338SJANAK DESAI if (!oldf) 1788a016f338SJANAK DESAI goto out; 1789a016f338SJANAK DESAI 179011f3f500SMike Christie if (no_files) { 179111f3f500SMike Christie tsk->files = NULL; 179211f3f500SMike Christie goto out; 179311f3f500SMike Christie } 179411f3f500SMike Christie 1795a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 1796a016f338SJANAK DESAI atomic_inc(&oldf->count); 1797a016f338SJANAK DESAI goto out; 1798a016f338SJANAK DESAI } 1799a016f338SJANAK DESAI 180060997c3dSChristian Brauner newf = dup_fd(oldf, NR_OPEN_MAX, &error); 1801a016f338SJANAK DESAI if (!newf) 1802a016f338SJANAK DESAI goto out; 1803a016f338SJANAK DESAI 1804a016f338SJANAK DESAI tsk->files = newf; 1805a016f338SJANAK DESAI error = 0; 1806a016f338SJANAK DESAI out: 1807a016f338SJANAK DESAI return error; 1808a016f338SJANAK DESAI } 1809a016f338SJANAK DESAI 1810a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 18111da177e4SLinus Torvalds { 18121da177e4SLinus Torvalds struct sighand_struct *sig; 18131da177e4SLinus Torvalds 181460348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 1815d036bda7SElena Reshetova refcount_inc(¤t->sighand->count); 18161da177e4SLinus Torvalds return 0; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 18190c282b06SMadhuparna Bhowmik RCU_INIT_POINTER(tsk->sighand, sig); 18201da177e4SLinus Torvalds if (!sig) 18211da177e4SLinus Torvalds return -ENOMEM; 18229d7fb042SPeter Zijlstra 1823d036bda7SElena Reshetova refcount_set(&sig->count, 1); 182406e62a46SJann Horn spin_lock_irq(¤t->sighand->siglock); 18251da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 182606e62a46SJann Horn spin_unlock_irq(¤t->sighand->siglock); 1827b612e5dfSChristian Brauner 1828b612e5dfSChristian Brauner /* Reset all signal handler not set to SIG_IGN to SIG_DFL. */ 1829b612e5dfSChristian Brauner if (clone_flags & CLONE_CLEAR_SIGHAND) 1830b612e5dfSChristian Brauner flush_signal_handlers(tsk, 0); 1831b612e5dfSChristian Brauner 18321da177e4SLinus Torvalds return 0; 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 1835a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1836c81addc9SOleg Nesterov { 1837d036bda7SElena Reshetova if (refcount_dec_and_test(&sighand->count)) { 1838d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1839392809b2SOleg Nesterov /* 18405f0d5a3aSPaul E. McKenney * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it 1841392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1842392809b2SOleg Nesterov */ 1843c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1844c81addc9SOleg Nesterov } 1845d80e731eSOleg Nesterov } 1846c81addc9SOleg Nesterov 1847f06febc9SFrank Mayhar /* 1848f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1849f06febc9SFrank Mayhar */ 1850f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1851f06febc9SFrank Mayhar { 18522b69942fSThomas Gleixner struct posix_cputimers *pct = &sig->posix_cputimers; 185378d7d407SJiri Slaby unsigned long cpu_limit; 185478d7d407SJiri Slaby 1855316c1608SJason Low cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 18563a245c0fSThomas Gleixner posix_cputimers_group_init(pct, cpu_limit); 18576279a751SOleg Nesterov } 18586279a751SOleg Nesterov 1859a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 18601da177e4SLinus Torvalds { 18611da177e4SLinus Torvalds struct signal_struct *sig; 18621da177e4SLinus Torvalds 18634ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1864490dea45SPeter Zijlstra return 0; 18656279a751SOleg Nesterov 1866a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 18671da177e4SLinus Torvalds tsk->signal = sig; 18681da177e4SLinus Torvalds if (!sig) 18691da177e4SLinus Torvalds return -ENOMEM; 18701da177e4SLinus Torvalds 1871b3ac022cSOleg Nesterov sig->nr_threads = 1; 1872d80f7d7bSEric W. Biederman sig->quick_threads = 1; 18731da177e4SLinus Torvalds atomic_set(&sig->live, 1); 187460d4de3fSElena Reshetova refcount_set(&sig->sigcnt, 1); 18750c740d0aSOleg Nesterov 18760c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 18770c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 18780c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 18790c740d0aSOleg Nesterov 18801da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1881db51aeccSOleg Nesterov sig->curr_target = tsk; 18821da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 1883c3ad2c3bSEric W. Biederman INIT_HLIST_HEAD(&sig->multiprocess); 1884e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 18859d7fb042SPeter Zijlstra prev_cputime_init(&sig->prev_cputime); 18861da177e4SLinus Torvalds 1887baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 1888b18b6a9cSNicolas Pitre INIT_LIST_HEAD(&sig->posix_timers); 1889c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 18901da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 1891baa73d9eSNicolas Pitre #endif 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds task_lock(current->group_leader); 18941da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 18951da177e4SLinus Torvalds task_unlock(current->group_leader); 18961da177e4SLinus Torvalds 18976279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 18986279a751SOleg Nesterov 1899522ed776SMiloslav Trmac tty_audit_fork(sig); 19005091faa4SMike Galbraith sched_autogroup_fork(sig); 1901522ed776SMiloslav Trmac 1902a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1903dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 190428b83c51SKOSAKI Motohiro 19059b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 1906f7cfd871SEric W. Biederman init_rwsem(&sig->exec_update_lock); 19079b1bf12dSKOSAKI Motohiro 19081da177e4SLinus Torvalds return 0; 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds 1911dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1912dbd95212SKees Cook { 1913dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1914dbd95212SKees Cook /* 1915dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1916dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1917dbd95212SKees Cook * needed because this new task is not yet running and cannot 1918dbd95212SKees Cook * be racing exec. 1919dbd95212SKees Cook */ 192069f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1921dbd95212SKees Cook 1922dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1923dbd95212SKees Cook get_seccomp_filter(current); 1924dbd95212SKees Cook p->seccomp = current->seccomp; 1925dbd95212SKees Cook 1926dbd95212SKees Cook /* 1927dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1928dbd95212SKees Cook * between the task_struct being duplicated and holding the 1929dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1930dbd95212SKees Cook */ 1931dbd95212SKees Cook if (task_no_new_privs(current)) 1932dbd95212SKees Cook task_set_no_new_privs(p); 1933dbd95212SKees Cook 1934dbd95212SKees Cook /* 1935dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1936dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1937dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1938dbd95212SKees Cook */ 1939dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 194023d67a54SGabriel Krisman Bertazi set_task_syscall_work(p, SECCOMP); 1941dbd95212SKees Cook #endif 1942dbd95212SKees Cook } 1943dbd95212SKees Cook 194417da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 19451da177e4SLinus Torvalds { 19461da177e4SLinus Torvalds current->clear_child_tid = tidptr; 19471da177e4SLinus Torvalds 1948b488893aSPavel Emelyanov return task_pid_vnr(current); 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 1951a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 195223f78d4aSIngo Molnar { 19531d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1954e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1955a23ba907SDavidlohr Bueso p->pi_waiters = RB_ROOT_CACHED; 1956e96a7705SXunlei Pang p->pi_top_task = NULL; 195723f78d4aSIngo Molnar p->pi_blocked_on = NULL; 195823f78d4aSIngo Molnar #endif 195923f78d4aSIngo Molnar } 196023f78d4aSIngo Molnar 19612c470475SEric W. Biederman static inline void init_task_pid_links(struct task_struct *task) 19622c470475SEric W. Biederman { 19632c470475SEric W. Biederman enum pid_type type; 19642c470475SEric W. Biederman 196596e1e984SAlexander Guril for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) 19662c470475SEric W. Biederman INIT_HLIST_NODE(&task->pid_links[type]); 19672c470475SEric W. Biederman } 19682c470475SEric W. Biederman 196981907739SOleg Nesterov static inline void 197081907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 197181907739SOleg Nesterov { 19722c470475SEric W. Biederman if (type == PIDTYPE_PID) 19732c470475SEric W. Biederman task->thread_pid = pid; 19742c470475SEric W. Biederman else 19752c470475SEric W. Biederman task->signal->pids[type] = pid; 197681907739SOleg Nesterov } 197781907739SOleg Nesterov 19786bfbaa51SIngo Molnar static inline void rcu_copy_process(struct task_struct *p) 19796bfbaa51SIngo Molnar { 19806bfbaa51SIngo Molnar #ifdef CONFIG_PREEMPT_RCU 19816bfbaa51SIngo Molnar p->rcu_read_lock_nesting = 0; 19826bfbaa51SIngo Molnar p->rcu_read_unlock_special.s = 0; 19836bfbaa51SIngo Molnar p->rcu_blocked_node = NULL; 19846bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_node_entry); 19856bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_PREEMPT_RCU */ 19866bfbaa51SIngo Molnar #ifdef CONFIG_TASKS_RCU 19876bfbaa51SIngo Molnar p->rcu_tasks_holdout = false; 19886bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); 19896bfbaa51SIngo Molnar p->rcu_tasks_idle_cpu = -1; 19906bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_TASKS_RCU */ 1991d5f177d3SPaul E. McKenney #ifdef CONFIG_TASKS_TRACE_RCU 1992d5f177d3SPaul E. McKenney p->trc_reader_nesting = 0; 1993276c4104SPaul E. McKenney p->trc_reader_special.s = 0; 1994d5f177d3SPaul E. McKenney INIT_LIST_HEAD(&p->trc_holdout_list); 1995434c9eefSPaul E. McKenney INIT_LIST_HEAD(&p->trc_blkd_node); 1996d5f177d3SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */ 19976bfbaa51SIngo Molnar } 19986bfbaa51SIngo Molnar 19993695eae5SChristian Brauner struct pid *pidfd_pid(const struct file *file) 20003695eae5SChristian Brauner { 20013695eae5SChristian Brauner if (file->f_op == &pidfd_fops) 20023695eae5SChristian Brauner return file->private_data; 20033695eae5SChristian Brauner 20043695eae5SChristian Brauner return ERR_PTR(-EBADF); 20053695eae5SChristian Brauner } 20063695eae5SChristian Brauner 2007b3e58382SChristian Brauner static int pidfd_release(struct inode *inode, struct file *file) 2008b3e58382SChristian Brauner { 2009b3e58382SChristian Brauner struct pid *pid = file->private_data; 2010b3e58382SChristian Brauner 2011b3e58382SChristian Brauner file->private_data = NULL; 2012b3e58382SChristian Brauner put_pid(pid); 2013b3e58382SChristian Brauner return 0; 2014b3e58382SChristian Brauner } 2015b3e58382SChristian Brauner 2016b3e58382SChristian Brauner #ifdef CONFIG_PROC_FS 201715d42eb2SChristian Kellner /** 201815d42eb2SChristian Kellner * pidfd_show_fdinfo - print information about a pidfd 201915d42eb2SChristian Kellner * @m: proc fdinfo file 202015d42eb2SChristian Kellner * @f: file referencing a pidfd 202115d42eb2SChristian Kellner * 202215d42eb2SChristian Kellner * Pid: 202315d42eb2SChristian Kellner * This function will print the pid that a given pidfd refers to in the 202415d42eb2SChristian Kellner * pid namespace of the procfs instance. 202515d42eb2SChristian Kellner * If the pid namespace of the process is not a descendant of the pid 202615d42eb2SChristian Kellner * namespace of the procfs instance 0 will be shown as its pid. This is 202715d42eb2SChristian Kellner * similar to calling getppid() on a process whose parent is outside of 202815d42eb2SChristian Kellner * its pid namespace. 202915d42eb2SChristian Kellner * 203015d42eb2SChristian Kellner * NSpid: 203115d42eb2SChristian Kellner * If pid namespaces are supported then this function will also print 203215d42eb2SChristian Kellner * the pid of a given pidfd refers to for all descendant pid namespaces 203315d42eb2SChristian Kellner * starting from the current pid namespace of the instance, i.e. the 203415d42eb2SChristian Kellner * Pid field and the first entry in the NSpid field will be identical. 203515d42eb2SChristian Kellner * If the pid namespace of the process is not a descendant of the pid 203615d42eb2SChristian Kellner * namespace of the procfs instance 0 will be shown as its first NSpid 203715d42eb2SChristian Kellner * entry and no others will be shown. 203815d42eb2SChristian Kellner * Note that this differs from the Pid and NSpid fields in 203915d42eb2SChristian Kellner * /proc/<pid>/status where Pid and NSpid are always shown relative to 204015d42eb2SChristian Kellner * the pid namespace of the procfs instance. The difference becomes 204115d42eb2SChristian Kellner * obvious when sending around a pidfd between pid namespaces from a 2042a8ca6b13SXiaofeng Cao * different branch of the tree, i.e. where no ancestral relation is 204315d42eb2SChristian Kellner * present between the pid namespaces: 204415d42eb2SChristian Kellner * - create two new pid namespaces ns1 and ns2 in the initial pid 204515d42eb2SChristian Kellner * namespace (also take care to create new mount namespaces in the 204615d42eb2SChristian Kellner * new pid namespace and mount procfs) 204715d42eb2SChristian Kellner * - create a process with a pidfd in ns1 204815d42eb2SChristian Kellner * - send pidfd from ns1 to ns2 204915d42eb2SChristian Kellner * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid 205015d42eb2SChristian Kellner * have exactly one entry, which is 0 205115d42eb2SChristian Kellner */ 2052b3e58382SChristian Brauner static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) 2053b3e58382SChristian Brauner { 2054b3e58382SChristian Brauner struct pid *pid = f->private_data; 20553d6d8da4SChristian Brauner struct pid_namespace *ns; 20563d6d8da4SChristian Brauner pid_t nr = -1; 2057b3e58382SChristian Brauner 20583d6d8da4SChristian Brauner if (likely(pid_has_task(pid, PIDTYPE_PID))) { 20599d78edeaSAlexey Gladkov ns = proc_pid_ns(file_inode(m->file)->i_sb); 20603d6d8da4SChristian Brauner nr = pid_nr_ns(pid, ns); 20613d6d8da4SChristian Brauner } 20623d6d8da4SChristian Brauner 20633d6d8da4SChristian Brauner seq_put_decimal_ll(m, "Pid:\t", nr); 206415d42eb2SChristian Kellner 206515d42eb2SChristian Kellner #ifdef CONFIG_PID_NS 20663d6d8da4SChristian Brauner seq_put_decimal_ll(m, "\nNSpid:\t", nr); 20673d6d8da4SChristian Brauner if (nr > 0) { 206815d42eb2SChristian Kellner int i; 206915d42eb2SChristian Kellner 207015d42eb2SChristian Kellner /* If nr is non-zero it means that 'pid' is valid and that 207115d42eb2SChristian Kellner * ns, i.e. the pid namespace associated with the procfs 207215d42eb2SChristian Kellner * instance, is in the pid namespace hierarchy of pid. 207315d42eb2SChristian Kellner * Start at one below the already printed level. 207415d42eb2SChristian Kellner */ 207515d42eb2SChristian Kellner for (i = ns->level + 1; i <= pid->level; i++) 20763d6d8da4SChristian Brauner seq_put_decimal_ll(m, "\t", pid->numbers[i].nr); 207715d42eb2SChristian Kellner } 207815d42eb2SChristian Kellner #endif 2079b3e58382SChristian Brauner seq_putc(m, '\n'); 2080b3e58382SChristian Brauner } 2081b3e58382SChristian Brauner #endif 2082b3e58382SChristian Brauner 2083b53b0b9dSJoel Fernandes (Google) /* 2084b53b0b9dSJoel Fernandes (Google) * Poll support for process exit notification. 2085b53b0b9dSJoel Fernandes (Google) */ 20869e77716aSLuc Van Oostenryck static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) 2087b53b0b9dSJoel Fernandes (Google) { 2088b53b0b9dSJoel Fernandes (Google) struct pid *pid = file->private_data; 20899e77716aSLuc Van Oostenryck __poll_t poll_flags = 0; 2090b53b0b9dSJoel Fernandes (Google) 2091b53b0b9dSJoel Fernandes (Google) poll_wait(file, &pid->wait_pidfd, pts); 2092b53b0b9dSJoel Fernandes (Google) 2093b53b0b9dSJoel Fernandes (Google) /* 2094b53b0b9dSJoel Fernandes (Google) * Inform pollers only when the whole thread group exits. 2095b53b0b9dSJoel Fernandes (Google) * If the thread group leader exits before all other threads in the 2096b53b0b9dSJoel Fernandes (Google) * group, then poll(2) should block, similar to the wait(2) family. 2097b53b0b9dSJoel Fernandes (Google) */ 209838fd525aSEric W. Biederman if (thread_group_exited(pid)) 20999e77716aSLuc Van Oostenryck poll_flags = EPOLLIN | EPOLLRDNORM; 2100b53b0b9dSJoel Fernandes (Google) 2101b53b0b9dSJoel Fernandes (Google) return poll_flags; 2102b53b0b9dSJoel Fernandes (Google) } 2103b53b0b9dSJoel Fernandes (Google) 2104b3e58382SChristian Brauner const struct file_operations pidfd_fops = { 2105b3e58382SChristian Brauner .release = pidfd_release, 2106b53b0b9dSJoel Fernandes (Google) .poll = pidfd_poll, 2107b3e58382SChristian Brauner #ifdef CONFIG_PROC_FS 2108b3e58382SChristian Brauner .show_fdinfo = pidfd_show_fdinfo, 2109b3e58382SChristian Brauner #endif 2110b3e58382SChristian Brauner }; 2111b3e58382SChristian Brauner 21126ae930d9SChristian Brauner /** 21136ae930d9SChristian Brauner * __pidfd_prepare - allocate a new pidfd_file and reserve a pidfd 21146ae930d9SChristian Brauner * @pid: the struct pid for which to create a pidfd 21156ae930d9SChristian Brauner * @flags: flags of the new @pidfd 21166ae930d9SChristian Brauner * @pidfd: the pidfd to return 21176ae930d9SChristian Brauner * 21186ae930d9SChristian Brauner * Allocate a new file that stashes @pid and reserve a new pidfd number in the 21196ae930d9SChristian Brauner * caller's file descriptor table. The pidfd is reserved but not installed yet. 21206ae930d9SChristian Brauner 21216ae930d9SChristian Brauner * The helper doesn't perform checks on @pid which makes it useful for pidfds 21226ae930d9SChristian Brauner * created via CLONE_PIDFD where @pid has no task attached when the pidfd and 21236ae930d9SChristian Brauner * pidfd file are prepared. 21246ae930d9SChristian Brauner * 21256ae930d9SChristian Brauner * If this function returns successfully the caller is responsible to either 21266ae930d9SChristian Brauner * call fd_install() passing the returned pidfd and pidfd file as arguments in 21276ae930d9SChristian Brauner * order to install the pidfd into its file descriptor table or they must use 21286ae930d9SChristian Brauner * put_unused_fd() and fput() on the returned pidfd and pidfd file 21296ae930d9SChristian Brauner * respectively. 21306ae930d9SChristian Brauner * 21316ae930d9SChristian Brauner * This function is useful when a pidfd must already be reserved but there 21326ae930d9SChristian Brauner * might still be points of failure afterwards and the caller wants to ensure 21336ae930d9SChristian Brauner * that no pidfd is leaked into its file descriptor table. 21346ae930d9SChristian Brauner * 21356ae930d9SChristian Brauner * Return: On success, a reserved pidfd is returned from the function and a new 21366ae930d9SChristian Brauner * pidfd file is returned in the last argument to the function. On 21376ae930d9SChristian Brauner * error, a negative error code is returned from the function and the 21386ae930d9SChristian Brauner * last argument remains unchanged. 21396ae930d9SChristian Brauner */ 21406ae930d9SChristian Brauner static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret) 21416ae930d9SChristian Brauner { 21426ae930d9SChristian Brauner int pidfd; 21436ae930d9SChristian Brauner struct file *pidfd_file; 21446ae930d9SChristian Brauner 21456ae930d9SChristian Brauner if (flags & ~(O_NONBLOCK | O_RDWR | O_CLOEXEC)) 21466ae930d9SChristian Brauner return -EINVAL; 21476ae930d9SChristian Brauner 21486ae930d9SChristian Brauner pidfd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); 21496ae930d9SChristian Brauner if (pidfd < 0) 21506ae930d9SChristian Brauner return pidfd; 21516ae930d9SChristian Brauner 21526ae930d9SChristian Brauner pidfd_file = anon_inode_getfile("[pidfd]", &pidfd_fops, pid, 21536ae930d9SChristian Brauner flags | O_RDWR | O_CLOEXEC); 21546ae930d9SChristian Brauner if (IS_ERR(pidfd_file)) { 21556ae930d9SChristian Brauner put_unused_fd(pidfd); 21566ae930d9SChristian Brauner return PTR_ERR(pidfd_file); 21576ae930d9SChristian Brauner } 21586ae930d9SChristian Brauner get_pid(pid); /* held by pidfd_file now */ 21596ae930d9SChristian Brauner *ret = pidfd_file; 21606ae930d9SChristian Brauner return pidfd; 21616ae930d9SChristian Brauner } 21626ae930d9SChristian Brauner 21636ae930d9SChristian Brauner /** 21646ae930d9SChristian Brauner * pidfd_prepare - allocate a new pidfd_file and reserve a pidfd 21656ae930d9SChristian Brauner * @pid: the struct pid for which to create a pidfd 21666ae930d9SChristian Brauner * @flags: flags of the new @pidfd 21676ae930d9SChristian Brauner * @pidfd: the pidfd to return 21686ae930d9SChristian Brauner * 21696ae930d9SChristian Brauner * Allocate a new file that stashes @pid and reserve a new pidfd number in the 21706ae930d9SChristian Brauner * caller's file descriptor table. The pidfd is reserved but not installed yet. 21716ae930d9SChristian Brauner * 21726ae930d9SChristian Brauner * The helper verifies that @pid is used as a thread group leader. 21736ae930d9SChristian Brauner * 21746ae930d9SChristian Brauner * If this function returns successfully the caller is responsible to either 21756ae930d9SChristian Brauner * call fd_install() passing the returned pidfd and pidfd file as arguments in 21766ae930d9SChristian Brauner * order to install the pidfd into its file descriptor table or they must use 21776ae930d9SChristian Brauner * put_unused_fd() and fput() on the returned pidfd and pidfd file 21786ae930d9SChristian Brauner * respectively. 21796ae930d9SChristian Brauner * 21806ae930d9SChristian Brauner * This function is useful when a pidfd must already be reserved but there 21816ae930d9SChristian Brauner * might still be points of failure afterwards and the caller wants to ensure 21826ae930d9SChristian Brauner * that no pidfd is leaked into its file descriptor table. 21836ae930d9SChristian Brauner * 21846ae930d9SChristian Brauner * Return: On success, a reserved pidfd is returned from the function and a new 21856ae930d9SChristian Brauner * pidfd file is returned in the last argument to the function. On 21866ae930d9SChristian Brauner * error, a negative error code is returned from the function and the 21876ae930d9SChristian Brauner * last argument remains unchanged. 21886ae930d9SChristian Brauner */ 21896ae930d9SChristian Brauner int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret) 21906ae930d9SChristian Brauner { 21916ae930d9SChristian Brauner if (!pid || !pid_has_task(pid, PIDTYPE_TGID)) 21926ae930d9SChristian Brauner return -EINVAL; 21936ae930d9SChristian Brauner 21946ae930d9SChristian Brauner return __pidfd_prepare(pid, flags, ret); 21956ae930d9SChristian Brauner } 21966ae930d9SChristian Brauner 2197c3f3ce04SAndrea Arcangeli static void __delayed_free_task(struct rcu_head *rhp) 2198c3f3ce04SAndrea Arcangeli { 2199c3f3ce04SAndrea Arcangeli struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); 2200c3f3ce04SAndrea Arcangeli 2201c3f3ce04SAndrea Arcangeli free_task(tsk); 2202c3f3ce04SAndrea Arcangeli } 2203c3f3ce04SAndrea Arcangeli 2204c3f3ce04SAndrea Arcangeli static __always_inline void delayed_free_task(struct task_struct *tsk) 2205c3f3ce04SAndrea Arcangeli { 2206c3f3ce04SAndrea Arcangeli if (IS_ENABLED(CONFIG_MEMCG)) 2207c3f3ce04SAndrea Arcangeli call_rcu(&tsk->rcu, __delayed_free_task); 2208c3f3ce04SAndrea Arcangeli else 2209c3f3ce04SAndrea Arcangeli free_task(tsk); 2210c3f3ce04SAndrea Arcangeli } 2211c3f3ce04SAndrea Arcangeli 221267197a4fSSuren Baghdasaryan static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk) 221367197a4fSSuren Baghdasaryan { 221467197a4fSSuren Baghdasaryan /* Skip if kernel thread */ 221567197a4fSSuren Baghdasaryan if (!tsk->mm) 221667197a4fSSuren Baghdasaryan return; 221767197a4fSSuren Baghdasaryan 221867197a4fSSuren Baghdasaryan /* Skip if spawning a thread or using vfork */ 221967197a4fSSuren Baghdasaryan if ((clone_flags & (CLONE_VM | CLONE_THREAD | CLONE_VFORK)) != CLONE_VM) 222067197a4fSSuren Baghdasaryan return; 222167197a4fSSuren Baghdasaryan 222267197a4fSSuren Baghdasaryan /* We need to synchronize with __set_oom_adj */ 222367197a4fSSuren Baghdasaryan mutex_lock(&oom_adj_mutex); 222467197a4fSSuren Baghdasaryan set_bit(MMF_MULTIPROCESS, &tsk->mm->flags); 222567197a4fSSuren Baghdasaryan /* Update the values in case they were changed after copy_signal */ 222667197a4fSSuren Baghdasaryan tsk->signal->oom_score_adj = current->signal->oom_score_adj; 222767197a4fSSuren Baghdasaryan tsk->signal->oom_score_adj_min = current->signal->oom_score_adj_min; 222867197a4fSSuren Baghdasaryan mutex_unlock(&oom_adj_mutex); 222967197a4fSSuren Baghdasaryan } 223067197a4fSSuren Baghdasaryan 223179257534SDaniel Bristot de Oliveira #ifdef CONFIG_RV 223279257534SDaniel Bristot de Oliveira static void rv_task_fork(struct task_struct *p) 223379257534SDaniel Bristot de Oliveira { 223479257534SDaniel Bristot de Oliveira int i; 223579257534SDaniel Bristot de Oliveira 223679257534SDaniel Bristot de Oliveira for (i = 0; i < RV_PER_TASK_MONITORS; i++) 223779257534SDaniel Bristot de Oliveira p->rv[i].da_mon.monitoring = false; 223879257534SDaniel Bristot de Oliveira } 223979257534SDaniel Bristot de Oliveira #else 224079257534SDaniel Bristot de Oliveira #define rv_task_fork(p) do {} while (0) 224179257534SDaniel Bristot de Oliveira #endif 224279257534SDaniel Bristot de Oliveira 2243f06febc9SFrank Mayhar /* 22441da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 22451da177e4SLinus Torvalds * but does not actually start it yet. 22461da177e4SLinus Torvalds * 22471da177e4SLinus Torvalds * It copies the registers, and all the appropriate 22481da177e4SLinus Torvalds * parts of the process environment (as per the clone 22491da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 22501da177e4SLinus Torvalds */ 225189c8e98dSMike Christie __latent_entropy struct task_struct *copy_process( 225209a05394SRoland McGrath struct pid *pid, 22533033f14aSJosh Triplett int trace, 22547f192e3cSChristian Brauner int node, 22557f192e3cSChristian Brauner struct kernel_clone_args *args) 22561da177e4SLinus Torvalds { 2257b3e58382SChristian Brauner int pidfd = -1, retval; 2258a24efe62SMariusz Kozlowski struct task_struct *p; 2259c3ad2c3bSEric W. Biederman struct multiprocess_signals delayed; 22606fd2fe49SAl Viro struct file *pidfile = NULL; 2261c5febea0SEric W. Biederman const u64 clone_flags = args->flags; 2262769071acSAndrei Vagin struct nsproxy *nsp = current->nsproxy; 22631da177e4SLinus Torvalds 2264667b6094SMarcos Paulo de Souza /* 2265667b6094SMarcos Paulo de Souza * Don't allow sharing the root directory with processes in a different 2266667b6094SMarcos Paulo de Souza * namespace 2267667b6094SMarcos Paulo de Souza */ 22681da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 22691da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 22701da177e4SLinus Torvalds 2271e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 2272e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 2273e66eded8SEric W. Biederman 22741da177e4SLinus Torvalds /* 22751da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 22761da177e4SLinus Torvalds * can only be started up within the thread group. 22771da177e4SLinus Torvalds */ 22781da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 22791da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds /* 22821da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 22831da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 22841da177e4SLinus Torvalds * for various simplifications in other code. 22851da177e4SLinus Torvalds */ 22861da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 22871da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 22881da177e4SLinus Torvalds 2289123be07bSSukadev Bhattiprolu /* 2290123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 2291123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 2292123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 2293123be07bSSukadev Bhattiprolu * from creating siblings. 2294123be07bSSukadev Bhattiprolu */ 2295123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 2296123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 2297123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 2298123be07bSSukadev Bhattiprolu 22998382fcacSEric W. Biederman /* 230040a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 2301faf00da5SEric W. Biederman * do not allow it to share a thread group with the forking task. 23028382fcacSEric W. Biederman */ 2303faf00da5SEric W. Biederman if (clone_flags & CLONE_THREAD) { 230440a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 2305769071acSAndrei Vagin (task_active_pid_ns(current) != nsp->pid_ns_for_children)) 2306769071acSAndrei Vagin return ERR_PTR(-EINVAL); 2307769071acSAndrei Vagin } 2308769071acSAndrei Vagin 2309b3e58382SChristian Brauner if (clone_flags & CLONE_PIDFD) { 2310b3e58382SChristian Brauner /* 2311b3e58382SChristian Brauner * - CLONE_DETACHED is blocked so that we can potentially 2312b3e58382SChristian Brauner * reuse it later for CLONE_PIDFD. 2313b3e58382SChristian Brauner * - CLONE_THREAD is blocked until someone really needs it. 2314b3e58382SChristian Brauner */ 23157f192e3cSChristian Brauner if (clone_flags & (CLONE_DETACHED | CLONE_THREAD)) 2316b3e58382SChristian Brauner return ERR_PTR(-EINVAL); 2317b3e58382SChristian Brauner } 2318b3e58382SChristian Brauner 2319c3ad2c3bSEric W. Biederman /* 2320c3ad2c3bSEric W. Biederman * Force any signals received before this point to be delivered 2321c3ad2c3bSEric W. Biederman * before the fork happens. Collect up signals sent to multiple 2322c3ad2c3bSEric W. Biederman * processes that happen during the fork and delay them so that 2323c3ad2c3bSEric W. Biederman * they appear to happen after the fork. 2324c3ad2c3bSEric W. Biederman */ 2325c3ad2c3bSEric W. Biederman sigemptyset(&delayed.signal); 2326c3ad2c3bSEric W. Biederman INIT_HLIST_NODE(&delayed.node); 2327c3ad2c3bSEric W. Biederman 2328c3ad2c3bSEric W. Biederman spin_lock_irq(¤t->sighand->siglock); 2329c3ad2c3bSEric W. Biederman if (!(clone_flags & CLONE_THREAD)) 2330c3ad2c3bSEric W. Biederman hlist_add_head(&delayed.node, ¤t->signal->multiprocess); 2331c3ad2c3bSEric W. Biederman recalc_sigpending(); 2332c3ad2c3bSEric W. Biederman spin_unlock_irq(¤t->sighand->siglock); 2333c3ad2c3bSEric W. Biederman retval = -ERESTARTNOINTR; 233466ae0d1eSJens Axboe if (task_sigpending(current)) 2335c3ad2c3bSEric W. Biederman goto fork_out; 2336c3ad2c3bSEric W. Biederman 23371da177e4SLinus Torvalds retval = -ENOMEM; 2338725fc629SAndi Kleen p = dup_task_struct(current, node); 23391da177e4SLinus Torvalds if (!p) 23401da177e4SLinus Torvalds goto fork_out; 2341753550ebSEric W. Biederman p->flags &= ~PF_KTHREAD; 2342753550ebSEric W. Biederman if (args->kthread) 2343753550ebSEric W. Biederman p->flags |= PF_KTHREAD; 2344f9010dbdSMike Christie if (args->user_worker) { 2345b16b3855SJens Axboe /* 2346f9010dbdSMike Christie * Mark us a user worker, and block any signal that isn't 2347b16b3855SJens Axboe * fatal or STOP 2348b16b3855SJens Axboe */ 2349f9010dbdSMike Christie p->flags |= PF_USER_WORKER; 2350b16b3855SJens Axboe siginitsetinv(&p->blocked, sigmask(SIGKILL)|sigmask(SIGSTOP)); 2351b16b3855SJens Axboe } 2352f9010dbdSMike Christie if (args->io_thread) 2353f9010dbdSMike Christie p->flags |= PF_IO_WORKER; 23541da177e4SLinus Torvalds 2355cf587db2SMike Christie if (args->name) 2356cf587db2SMike Christie strscpy_pad(p->comm, args->name, sizeof(p->comm)); 2357cf587db2SMike Christie 23587f192e3cSChristian Brauner p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL; 23594d6501dcSVegard Nossum /* 23604d6501dcSVegard Nossum * Clear TID on mm_release()? 23614d6501dcSVegard Nossum */ 23627f192e3cSChristian Brauner p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL; 23634d6501dcSVegard Nossum 2364f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 2365f7e8b616SSteven Rostedt 2366bea493a0SPeter Zijlstra rt_mutex_init_task(p); 2367bea493a0SPeter Zijlstra 2368a21ee605SPeter Zijlstra lockdep_assert_irqs_enabled(); 2369d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 2370de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 2371de30a2b3SIngo Molnar #endif 23728f2f9c4dSEric W. Biederman retval = copy_creds(p, clone_flags); 23738f2f9c4dSEric W. Biederman if (retval < 0) 23748f2f9c4dSEric W. Biederman goto bad_fork_free; 23758f2f9c4dSEric W. Biederman 23761da177e4SLinus Torvalds retval = -EAGAIN; 2377de399236SAlexey Gladkov if (is_rlimit_overlimit(task_ucounts(p), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) { 2378b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 2379b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 23808f2f9c4dSEric W. Biederman goto bad_fork_cleanup_count; 23811da177e4SLinus Torvalds } 238272fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 23831da177e4SLinus Torvalds 23841da177e4SLinus Torvalds /* 23851da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 23861da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 23871da177e4SLinus Torvalds * to stop root fork bombs. 23881da177e4SLinus Torvalds */ 238904ec93feSLi Zefan retval = -EAGAIN; 2390c17d1a3aSWeilong Chen if (data_race(nr_threads >= max_threads)) 23911da177e4SLinus Torvalds goto bad_fork_cleanup_count; 23921da177e4SLinus Torvalds 2393ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 2394a8ea6fc9SFrederic Weisbecker p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE | PF_NO_SETAFFINITY); 2395514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 23961da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 23971da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 2398f41d911fSPaul E. McKenney rcu_copy_process(p); 23991da177e4SLinus Torvalds p->vfork_done = NULL; 24001da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 24011da177e4SLinus Torvalds 24021da177e4SLinus Torvalds init_sigpending(&p->pending); 24031da177e4SLinus Torvalds 240464861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 240540565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 240664861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 240740565b5aSStanislaw Gruszka #endif 24089d7fb042SPeter Zijlstra prev_cputime_init(&p->prev_cputime); 24099d7fb042SPeter Zijlstra 24106a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 2411bac5b6b6SFrederic Weisbecker seqcount_init(&p->vtime.seqcount); 2412bac5b6b6SFrederic Weisbecker p->vtime.starttime = 0; 2413bac5b6b6SFrederic Weisbecker p->vtime.state = VTIME_INACTIVE; 24146a61671bSFrederic Weisbecker #endif 24156a61671bSFrederic Weisbecker 24160f212204SJens Axboe #ifdef CONFIG_IO_URING 24170f212204SJens Axboe p->io_uring = NULL; 24180f212204SJens Axboe #endif 24190f212204SJens Axboe 2420a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 2421a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 2422a3a2e76cSKAMEZAWA Hiroyuki #endif 2423172ba844SBalbir Singh 24246976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 24256976675dSArjan van de Ven 2426eb414681SJohannes Weiner #ifdef CONFIG_PSI 2427eb414681SJohannes Weiner p->psi_flags = 0; 2428eb414681SJohannes Weiner #endif 2429eb414681SJohannes Weiner 24305995477aSAndrea Righi task_io_accounting_init(&p->ioac); 24311da177e4SLinus Torvalds acct_clear_integrals(p); 24321da177e4SLinus Torvalds 24333a245c0fSThomas Gleixner posix_cputimers_init(&p->posix_cputimers); 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds p->io_context = NULL; 2436c0b0ae8aSRichard Guy Briggs audit_set_context(p, NULL); 2437b4f48b63SPaul Menage cgroup_fork(p); 2438343f4c49SEric W. Biederman if (args->kthread) { 243940966e31SEric W. Biederman if (!set_kthread_struct(p)) 2440ff8288ffSEric W. Biederman goto bad_fork_cleanup_delayacct; 244140966e31SEric W. Biederman } 24421da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2443846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 24441da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 24451da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 24461da177e4SLinus Torvalds p->mempolicy = NULL; 2447ff8288ffSEric W. Biederman goto bad_fork_cleanup_delayacct; 24481da177e4SLinus Torvalds } 24491da177e4SLinus Torvalds #endif 2450778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 2451778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 2452778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 2453b7505861SAhmed S. Darwish seqcount_spinlock_init(&p->mems_allowed_seq, &p->alloc_lock); 2454778d3b0fSMichal Hocko #endif 2455de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 24560584df9cSMarco Elver memset(&p->irqtrace, 0, sizeof(p->irqtrace)); 24570584df9cSMarco Elver p->irqtrace.hardirq_disable_ip = _THIS_IP_; 24580584df9cSMarco Elver p->irqtrace.softirq_enable_ip = _THIS_IP_; 2459de30a2b3SIngo Molnar p->softirqs_enabled = 1; 2460de30a2b3SIngo Molnar p->softirq_context = 0; 2461de30a2b3SIngo Molnar #endif 24628bcbde54SDavid Hildenbrand 24638bcbde54SDavid Hildenbrand p->pagefault_disabled = 0; 24648bcbde54SDavid Hildenbrand 2465fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 2466b09be676SByungchul Park lockdep_init_task(p); 2467fbb9ce95SIngo Molnar #endif 24681da177e4SLinus Torvalds 2469408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 2470408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 2471408894eeSIngo Molnar #endif 2472cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 2473cafe5635SKent Overstreet p->sequential_io = 0; 2474cafe5635SKent Overstreet p->sequential_io_avg = 0; 2475cafe5635SKent Overstreet #endif 2476a10787e6SSong Liu #ifdef CONFIG_BPF_SYSCALL 2477a10787e6SSong Liu RCU_INIT_POINTER(p->bpf_storage, NULL); 2478c7603cfaSAndrii Nakryiko p->bpf_ctx = NULL; 2479a10787e6SSong Liu #endif 24800f481406SMarkus Metzger 24813c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 2482aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 2483aab03e05SDario Faggioli if (retval) 2484aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 24856ab423e0SPeter Zijlstra 24862b26f0aaSMarco Elver retval = perf_event_init_task(p, clone_flags); 24876ab423e0SPeter Zijlstra if (retval) 24886ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 2489fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 2490fb0a685cSDaniel Rebelo de Oliveira if (retval) 24916c72e350SPeter Zijlstra goto bad_fork_cleanup_perf; 24921da177e4SLinus Torvalds /* copy all the process information */ 2493ab602f79SJack Miller shm_init_task(p); 2494e4e55b47STetsuo Handa retval = security_task_alloc(p, clone_flags); 2495fb0a685cSDaniel Rebelo de Oliveira if (retval) 24961da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 2497e4e55b47STetsuo Handa retval = copy_semundo(clone_flags, p); 2498e4e55b47STetsuo Handa if (retval) 2499e4e55b47STetsuo Handa goto bad_fork_cleanup_security; 250011f3f500SMike Christie retval = copy_files(clone_flags, p, args->no_files); 2501fb0a685cSDaniel Rebelo de Oliveira if (retval) 25021da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 2503fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 2504fb0a685cSDaniel Rebelo de Oliveira if (retval) 25051da177e4SLinus Torvalds goto bad_fork_cleanup_files; 2506fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 2507fb0a685cSDaniel Rebelo de Oliveira if (retval) 25081da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 2509fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 2510fb0a685cSDaniel Rebelo de Oliveira if (retval) 25111da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 2512fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 2513fb0a685cSDaniel Rebelo de Oliveira if (retval) 25141da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 2515fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 2516fb0a685cSDaniel Rebelo de Oliveira if (retval) 2517d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 2518fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 2519fb0a685cSDaniel Rebelo de Oliveira if (retval) 2520fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 2521c5febea0SEric W. Biederman retval = copy_thread(p, args); 25221da177e4SLinus Torvalds if (retval) 2523fd0928dfSJens Axboe goto bad_fork_cleanup_io; 25241da177e4SLinus Torvalds 2525afaef01cSAlexander Popov stackleak_task_init(p); 2526afaef01cSAlexander Popov 2527425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 252849cb2fc4SAdrian Reber pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid, 252949cb2fc4SAdrian Reber args->set_tid_size); 253035f71bc0SMichal Hocko if (IS_ERR(pid)) { 253135f71bc0SMichal Hocko retval = PTR_ERR(pid); 25320740aa5fSJiri Slaby goto bad_fork_cleanup_thread; 2533425fb2b4SPavel Emelyanov } 253435f71bc0SMichal Hocko } 2535425fb2b4SPavel Emelyanov 2536b3e58382SChristian Brauner /* 2537b3e58382SChristian Brauner * This has to happen after we've potentially unshared the file 2538b3e58382SChristian Brauner * descriptor table (so that the pidfd doesn't leak into the child 2539b3e58382SChristian Brauner * if the fd table isn't shared). 2540b3e58382SChristian Brauner */ 2541b3e58382SChristian Brauner if (clone_flags & CLONE_PIDFD) { 2542ca7707f5SChristian Brauner /* Note that no task has been attached to @pid yet. */ 2543ca7707f5SChristian Brauner retval = __pidfd_prepare(pid, O_RDWR | O_CLOEXEC, &pidfile); 2544b3e58382SChristian Brauner if (retval < 0) 2545b3e58382SChristian Brauner goto bad_fork_free_pid; 2546b3e58382SChristian Brauner pidfd = retval; 25476fd2fe49SAl Viro 25487f192e3cSChristian Brauner retval = put_user(pidfd, args->pidfd); 2549b3e58382SChristian Brauner if (retval) 2550b3e58382SChristian Brauner goto bad_fork_put_pidfd; 2551b3e58382SChristian Brauner } 2552b3e58382SChristian Brauner 255373c10101SJens Axboe #ifdef CONFIG_BLOCK 255473c10101SJens Axboe p->plug = NULL; 255573c10101SJens Axboe #endif 2556ba31c1a4SThomas Gleixner futex_init_task(p); 2557ba31c1a4SThomas Gleixner 25581da177e4SLinus Torvalds /* 2559f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 2560f9a3879aSGOTO Masanori */ 2561f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 25622a742138SStas Sergeev sas_ss_reset(p); 2563f9a3879aSGOTO Masanori 2564f9a3879aSGOTO Masanori /* 25656580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 25666580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 25671da177e4SLinus Torvalds */ 25686580807dSOleg Nesterov user_disable_single_step(p); 256964c19ba2SGabriel Krisman Bertazi clear_task_syscall_work(p, SYSCALL_TRACE); 257064eb35f7SGabriel Krisman Bertazi #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 257164eb35f7SGabriel Krisman Bertazi clear_task_syscall_work(p, SYSCALL_EMU); 2572ed75e8d5SLaurent Vivier #endif 2573e02c9b0dSLin Feng clear_tsk_latency_tracing(p); 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds /* ok, now we should be set up.. */ 257618c830dfSOleg Nesterov p->pid = pid_nr(pid); 257718c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 257818c830dfSOleg Nesterov p->group_leader = current->group_leader; 257918c830dfSOleg Nesterov p->tgid = current->tgid; 258018c830dfSOleg Nesterov } else { 258118c830dfSOleg Nesterov p->group_leader = p; 258218c830dfSOleg Nesterov p->tgid = p->pid; 258318c830dfSOleg Nesterov } 25845f8aadd8SOleg Nesterov 25859d823e8fSWu Fengguang p->nr_dirtied = 0; 25869d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 258783712358SWu Fengguang p->dirty_paused_when = 0; 25889d823e8fSWu Fengguang 2589bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 259047e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 2591158e1645SAl Viro p->task_works = NULL; 2592ca7752caSMichael Pratt clear_posix_cputimers_work(p); 25931da177e4SLinus Torvalds 2594d741bf41SPeter Zijlstra #ifdef CONFIG_KRETPROBES 2595d741bf41SPeter Zijlstra p->kretprobe_instances.first = NULL; 2596d741bf41SPeter Zijlstra #endif 259754ecbe6fSMasami Hiramatsu #ifdef CONFIG_RETHOOK 259854ecbe6fSMasami Hiramatsu p->rethooks.first = NULL; 259954ecbe6fSMasami Hiramatsu #endif 2600d741bf41SPeter Zijlstra 260118c830dfSOleg Nesterov /* 26027e47682eSAleksa Sarai * Ensure that the cgroup subsystem policies allow the new process to be 26037b7b8a2cSRandy Dunlap * forked. It should be noted that the new process's css_set can be changed 26047e47682eSAleksa Sarai * between here and cgroup_post_fork() if an organisation operation is in 26057e47682eSAleksa Sarai * progress. 26067e47682eSAleksa Sarai */ 2607ef2c41cfSChristian Brauner retval = cgroup_can_fork(p, args); 26087e47682eSAleksa Sarai if (retval) 26095a5cf5cbSChristian Brauner goto bad_fork_put_pidfd; 26107e47682eSAleksa Sarai 26117e47682eSAleksa Sarai /* 2612b1e82065SPeter Zijlstra * Now that the cgroups are pinned, re-clone the parent cgroup and put 2613b1e82065SPeter Zijlstra * the new task on the correct runqueue. All this *before* the task 2614b1e82065SPeter Zijlstra * becomes visible. 2615b1e82065SPeter Zijlstra * 2616b1e82065SPeter Zijlstra * This isn't part of ->can_fork() because while the re-cloning is 2617b1e82065SPeter Zijlstra * cgroup specific, it unconditionally needs to place the task on a 2618b1e82065SPeter Zijlstra * runqueue. 2619b1e82065SPeter Zijlstra */ 2620b1e82065SPeter Zijlstra sched_cgroup_fork(p, args); 2621b1e82065SPeter Zijlstra 2622b1e82065SPeter Zijlstra /* 26237b558513SDavid Herrmann * From this point on we must avoid any synchronous user-space 26247b558513SDavid Herrmann * communication until we take the tasklist-lock. In particular, we do 26257b558513SDavid Herrmann * not want user-space to be able to predict the process start-time by 26267b558513SDavid Herrmann * stalling fork(2) after we recorded the start_time but before it is 26277b558513SDavid Herrmann * visible to the system. 26287b558513SDavid Herrmann */ 26297b558513SDavid Herrmann 26307b558513SDavid Herrmann p->start_time = ktime_get_ns(); 2631cf25e24dSPeter Zijlstra p->start_boottime = ktime_get_boottime_ns(); 26327b558513SDavid Herrmann 26337b558513SDavid Herrmann /* 263418c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 263518c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 263618c830dfSOleg Nesterov */ 26371da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 26381da177e4SLinus Torvalds 26391da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 26402d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 26411da177e4SLinus Torvalds p->real_parent = current->real_parent; 26422d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 2643b4e00444SEddy Wu if (clone_flags & CLONE_THREAD) 2644b4e00444SEddy Wu p->exit_signal = -1; 2645b4e00444SEddy Wu else 2646b4e00444SEddy Wu p->exit_signal = current->group_leader->exit_signal; 26472d5516cbSOleg Nesterov } else { 26481da177e4SLinus Torvalds p->real_parent = current; 26492d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 2650b4e00444SEddy Wu p->exit_signal = args->exit_signal; 26512d5516cbSOleg Nesterov } 26521da177e4SLinus Torvalds 2653d83a7cb3SJosh Poimboeuf klp_copy_process(p); 2654d83a7cb3SJosh Poimboeuf 265585dd3f61SPeter Zijlstra sched_core_fork(p); 265685dd3f61SPeter Zijlstra 26571da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 26584a2c7a78SOleg Nesterov 265979257534SDaniel Bristot de Oliveira rv_task_fork(p); 266079257534SDaniel Bristot de Oliveira 2661d7822b1eSMathieu Desnoyers rseq_fork(p, clone_flags); 2662d7822b1eSMathieu Desnoyers 26634ca1d3eeSEric W. Biederman /* Don't start children in a dying pid namespace */ 2664e8cfbc24SGargi Sharma if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { 26653fd37226SKirill Tkhai retval = -ENOMEM; 26663fd37226SKirill Tkhai goto bad_fork_cancel_cgroup; 26673fd37226SKirill Tkhai } 26684a2c7a78SOleg Nesterov 26697673bf55SEric W. Biederman /* Let kill terminate clone/fork in the middle */ 26707673bf55SEric W. Biederman if (fatal_signal_pending(current)) { 26717673bf55SEric W. Biederman retval = -EINTR; 26727673bf55SEric W. Biederman goto bad_fork_cancel_cgroup; 26737673bf55SEric W. Biederman } 26747673bf55SEric W. Biederman 2675a1140cb2SKuniyuki Iwashima /* No more failure paths after this point. */ 2676a1140cb2SKuniyuki Iwashima 2677a1140cb2SKuniyuki Iwashima /* 2678a1140cb2SKuniyuki Iwashima * Copy seccomp details explicitly here, in case they were changed 2679a1140cb2SKuniyuki Iwashima * before holding sighand lock. 2680a1140cb2SKuniyuki Iwashima */ 2681a1140cb2SKuniyuki Iwashima copy_seccomp(p); 2682a1140cb2SKuniyuki Iwashima 26832c470475SEric W. Biederman init_task_pid_links(p); 268473b9ebfeSOleg Nesterov if (likely(p->pid)) { 26854b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 26861da177e4SLinus Torvalds 268781907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 26881da177e4SLinus Torvalds if (thread_group_leader(p)) { 26896883f81aSEric W. Biederman init_task_pid(p, PIDTYPE_TGID, pid); 269081907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 269181907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 269281907739SOleg Nesterov 26931c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 269417cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 26951c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 26961c4042c2SEric W. Biederman } 2697c3ad2c3bSEric W. Biederman p->signal->shared_pending.signal = delayed.signal; 26989c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 2699749860ceSPavel Tikhomirov /* 2700749860ceSPavel Tikhomirov * Inherit has_child_subreaper flag under the same 2701749860ceSPavel Tikhomirov * tasklist_lock with adding child to the process tree 2702749860ceSPavel Tikhomirov * for propagate_has_child_subreaper optimization. 2703749860ceSPavel Tikhomirov */ 2704749860ceSPavel Tikhomirov p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 2705749860ceSPavel Tikhomirov p->real_parent->signal->is_child_subreaper; 27069cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 27075e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 27086883f81aSEric W. Biederman attach_pid(p, PIDTYPE_TGID); 270981907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 271081907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 2711909ea964SChristoph Lameter __this_cpu_inc(process_counts); 271280628ca0SOleg Nesterov } else { 271380628ca0SOleg Nesterov current->signal->nr_threads++; 2714d80f7d7bSEric W. Biederman current->signal->quick_threads++; 271580628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 271660d4de3fSElena Reshetova refcount_inc(¤t->signal->sigcnt); 2717924de3b8SEric W. Biederman task_join_group_stop(p); 271880628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 271980628ca0SOleg Nesterov &p->group_leader->thread_group); 27200c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 27210c740d0aSOleg Nesterov &p->signal->thread_head); 27221da177e4SLinus Torvalds } 272381907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 27241da177e4SLinus Torvalds nr_threads++; 272573b9ebfeSOleg Nesterov } 27261da177e4SLinus Torvalds total_forks++; 2727c3ad2c3bSEric W. Biederman hlist_del_init(&delayed.node); 27283f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 27294af4206bSOleg Nesterov syscall_tracepoint_update(p); 27301da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 27314af4206bSOleg Nesterov 2732ddc204b5SWaiman Long if (pidfile) 2733ddc204b5SWaiman Long fd_install(pidfd, pidfile); 2734ddc204b5SWaiman Long 2735c13cf856SAndrew Morton proc_fork_connector(p); 2736b1e82065SPeter Zijlstra sched_post_fork(p); 2737ef2c41cfSChristian Brauner cgroup_post_fork(p, args); 2738cdd6c482SIngo Molnar perf_event_fork(p); 273943d2b113SKAMEZAWA Hiroyuki 274043d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 27413ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 2742fd593511SBeau Belgrave user_events_fork(p, clone_flags); 274343d2b113SKAMEZAWA Hiroyuki 274467197a4fSSuren Baghdasaryan copy_oom_score_adj(clone_flags, p); 274567197a4fSSuren Baghdasaryan 27461da177e4SLinus Torvalds return p; 27471da177e4SLinus Torvalds 27487e47682eSAleksa Sarai bad_fork_cancel_cgroup: 274985dd3f61SPeter Zijlstra sched_core_free(p); 27503fd37226SKirill Tkhai spin_unlock(¤t->sighand->siglock); 27513fd37226SKirill Tkhai write_unlock_irq(&tasklist_lock); 2752ef2c41cfSChristian Brauner cgroup_cancel_fork(p, args); 2753b3e58382SChristian Brauner bad_fork_put_pidfd: 27546fd2fe49SAl Viro if (clone_flags & CLONE_PIDFD) { 27556fd2fe49SAl Viro fput(pidfile); 27566fd2fe49SAl Viro put_unused_fd(pidfd); 27576fd2fe49SAl Viro } 2758425fb2b4SPavel Emelyanov bad_fork_free_pid: 2759425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 2760425fb2b4SPavel Emelyanov free_pid(pid); 27610740aa5fSJiri Slaby bad_fork_cleanup_thread: 27620740aa5fSJiri Slaby exit_thread(p); 2763fd0928dfSJens Axboe bad_fork_cleanup_io: 2764b69f2292SLouis Rilling if (p->io_context) 2765b69f2292SLouis Rilling exit_io_context(p); 2766ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 2767444f378bSLinus Torvalds exit_task_namespaces(p); 27681da177e4SLinus Torvalds bad_fork_cleanup_mm: 2769c3f3ce04SAndrea Arcangeli if (p->mm) { 2770c3f3ce04SAndrea Arcangeli mm_clear_owner(p->mm, p); 27711da177e4SLinus Torvalds mmput(p->mm); 2772c3f3ce04SAndrea Arcangeli } 27731da177e4SLinus Torvalds bad_fork_cleanup_signal: 27744ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 27751c5354deSMike Galbraith free_signal_struct(p->signal); 27761da177e4SLinus Torvalds bad_fork_cleanup_sighand: 2777a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 27781da177e4SLinus Torvalds bad_fork_cleanup_fs: 27791da177e4SLinus Torvalds exit_fs(p); /* blocking */ 27801da177e4SLinus Torvalds bad_fork_cleanup_files: 27811da177e4SLinus Torvalds exit_files(p); /* blocking */ 27821da177e4SLinus Torvalds bad_fork_cleanup_semundo: 27831da177e4SLinus Torvalds exit_sem(p); 2784e4e55b47STetsuo Handa bad_fork_cleanup_security: 2785e4e55b47STetsuo Handa security_task_free(p); 27861da177e4SLinus Torvalds bad_fork_cleanup_audit: 27871da177e4SLinus Torvalds audit_free(p); 27886c72e350SPeter Zijlstra bad_fork_cleanup_perf: 2789cdd6c482SIngo Molnar perf_event_free_task(p); 27906c72e350SPeter Zijlstra bad_fork_cleanup_policy: 2791b09be676SByungchul Park lockdep_free_task(p); 27921da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2793f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 27941da177e4SLinus Torvalds #endif 2795ff8288ffSEric W. Biederman bad_fork_cleanup_delayacct: 279635df17c5SShailabh Nagar delayacct_tsk_free(p); 27971da177e4SLinus Torvalds bad_fork_cleanup_count: 279821d1c5e3SAlexey Gladkov dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1); 2799e0e81739SDavid Howells exit_creds(p); 28001da177e4SLinus Torvalds bad_fork_free: 28012f064a59SPeter Zijlstra WRITE_ONCE(p->__state, TASK_DEAD); 28021a03d3f1SSebastian Andrzej Siewior exit_task_stack_account(p); 280368f24b08SAndy Lutomirski put_task_stack(p); 2804c3f3ce04SAndrea Arcangeli delayed_free_task(p); 2805fe7d37d1SOleg Nesterov fork_out: 2806c3ad2c3bSEric W. Biederman spin_lock_irq(¤t->sighand->siglock); 2807c3ad2c3bSEric W. Biederman hlist_del_init(&delayed.node); 2808c3ad2c3bSEric W. Biederman spin_unlock_irq(¤t->sighand->siglock); 2809fe7d37d1SOleg Nesterov return ERR_PTR(retval); 28101da177e4SLinus Torvalds } 28111da177e4SLinus Torvalds 28122c470475SEric W. Biederman static inline void init_idle_pids(struct task_struct *idle) 2813f106eee1SOleg Nesterov { 2814f106eee1SOleg Nesterov enum pid_type type; 2815f106eee1SOleg Nesterov 2816f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 28172c470475SEric W. Biederman INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */ 28182c470475SEric W. Biederman init_task_pid(idle, type, &init_struct_pid); 2819f106eee1SOleg Nesterov } 2820f106eee1SOleg Nesterov } 2821f106eee1SOleg Nesterov 282236cb0e1cSEric W. Biederman static int idle_dummy(void *dummy) 282336cb0e1cSEric W. Biederman { 282436cb0e1cSEric W. Biederman /* This function is never called */ 282536cb0e1cSEric W. Biederman return 0; 282636cb0e1cSEric W. Biederman } 282736cb0e1cSEric W. Biederman 2828f1a0a376SValentin Schneider struct task_struct * __init fork_idle(int cpu) 28291da177e4SLinus Torvalds { 283036c8b586SIngo Molnar struct task_struct *task; 28317f192e3cSChristian Brauner struct kernel_clone_args args = { 28327f192e3cSChristian Brauner .flags = CLONE_VM, 28335bd2e97cSEric W. Biederman .fn = &idle_dummy, 28345bd2e97cSEric W. Biederman .fn_arg = NULL, 2835343f4c49SEric W. Biederman .kthread = 1, 283636cb0e1cSEric W. Biederman .idle = 1, 28377f192e3cSChristian Brauner }; 28387f192e3cSChristian Brauner 28397f192e3cSChristian Brauner task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args); 2840f106eee1SOleg Nesterov if (!IS_ERR(task)) { 28412c470475SEric W. Biederman init_idle_pids(task); 28421da177e4SLinus Torvalds init_idle(task, cpu); 2843f106eee1SOleg Nesterov } 284473b9ebfeSOleg Nesterov 28451da177e4SLinus Torvalds return task; 28461da177e4SLinus Torvalds } 28471da177e4SLinus Torvalds 28481da177e4SLinus Torvalds /* 2849cc440e87SJens Axboe * This is like kernel_clone(), but shaved down and tailored to just 2850cc440e87SJens Axboe * creating io_uring workers. It returns a created task, or an error pointer. 2851cc440e87SJens Axboe * The returned task is inactive, and the caller must fire it up through 2852cc440e87SJens Axboe * wake_up_new_task(p). All signals are blocked in the created task. 2853cc440e87SJens Axboe */ 2854cc440e87SJens Axboe struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node) 2855cc440e87SJens Axboe { 2856cc440e87SJens Axboe unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD| 2857cc440e87SJens Axboe CLONE_IO; 2858cc440e87SJens Axboe struct kernel_clone_args args = { 2859cc440e87SJens Axboe .flags = ((lower_32_bits(flags) | CLONE_VM | 2860cc440e87SJens Axboe CLONE_UNTRACED) & ~CSIGNAL), 2861cc440e87SJens Axboe .exit_signal = (lower_32_bits(flags) & CSIGNAL), 28625bd2e97cSEric W. Biederman .fn = fn, 28635bd2e97cSEric W. Biederman .fn_arg = arg, 2864cc440e87SJens Axboe .io_thread = 1, 286554e6842dSMike Christie .user_worker = 1, 2866cc440e87SJens Axboe }; 2867cc440e87SJens Axboe 2868b16b3855SJens Axboe return copy_process(NULL, 0, node, &args); 2869cc440e87SJens Axboe } 2870cc440e87SJens Axboe 2871cc440e87SJens Axboe /* 28721da177e4SLinus Torvalds * Ok, this is the main fork-routine. 28731da177e4SLinus Torvalds * 28741da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 28751da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 2876a0eb9abdSEugene Syromiatnikov * 2877a0eb9abdSEugene Syromiatnikov * args->exit_signal is expected to be checked for sanity by the caller. 28781da177e4SLinus Torvalds */ 2879cad6967aSChristian Brauner pid_t kernel_clone(struct kernel_clone_args *args) 28801da177e4SLinus Torvalds { 28817f192e3cSChristian Brauner u64 clone_flags = args->flags; 28829f5325aaSMarcos Paulo de Souza struct completion vfork; 28839f5325aaSMarcos Paulo de Souza struct pid *pid; 28841da177e4SLinus Torvalds struct task_struct *p; 28851da177e4SLinus Torvalds int trace = 0; 2886cad6967aSChristian Brauner pid_t nr; 28871da177e4SLinus Torvalds 2888bdff746aSAndrew Morton /* 28893af8588cSChristian Brauner * For legacy clone() calls, CLONE_PIDFD uses the parent_tid argument 28903af8588cSChristian Brauner * to return the pidfd. Hence, CLONE_PIDFD and CLONE_PARENT_SETTID are 28913af8588cSChristian Brauner * mutually exclusive. With clone3() CLONE_PIDFD has grown a separate 28923af8588cSChristian Brauner * field in struct clone_args and it still doesn't make sense to have 28933af8588cSChristian Brauner * them both point at the same memory location. Performing this check 28943af8588cSChristian Brauner * here has the advantage that we don't need to have a separate helper 28953af8588cSChristian Brauner * to check for legacy clone(). 28963af8588cSChristian Brauner */ 28973af8588cSChristian Brauner if ((args->flags & CLONE_PIDFD) && 28983af8588cSChristian Brauner (args->flags & CLONE_PARENT_SETTID) && 28993af8588cSChristian Brauner (args->pidfd == args->parent_tid)) 29003af8588cSChristian Brauner return -EINVAL; 29013af8588cSChristian Brauner 29023af8588cSChristian Brauner /* 29034b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 29044b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 29054b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 29064b9d33e6STejun Heo * for the type of forking is enabled. 290709a05394SRoland McGrath */ 2908e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 29094b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 29104b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 29117f192e3cSChristian Brauner else if (args->exit_signal != SIGCHLD) 29124b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 29134b9d33e6STejun Heo else 29144b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 29154b9d33e6STejun Heo 29164b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 29174b9d33e6STejun Heo trace = 0; 29184b9d33e6STejun Heo } 29191da177e4SLinus Torvalds 29207f192e3cSChristian Brauner p = copy_process(NULL, trace, NUMA_NO_NODE, args); 292138addce8SEmese Revfy add_latent_entropy(); 29229f5325aaSMarcos Paulo de Souza 29239f5325aaSMarcos Paulo de Souza if (IS_ERR(p)) 29249f5325aaSMarcos Paulo de Souza return PTR_ERR(p); 29259f5325aaSMarcos Paulo de Souza 29261da177e4SLinus Torvalds /* 29271da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 29281da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 29291da177e4SLinus Torvalds */ 29300a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 29310a16b607SMathieu Desnoyers 29324e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 29334e52365fSMatthew Dempsky nr = pid_vnr(pid); 293430e49c26SPavel Emelyanov 293530e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 29367f192e3cSChristian Brauner put_user(nr, args->parent_tid); 2937a6f5e063SSukadev Bhattiprolu 29381da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 29391da177e4SLinus Torvalds p->vfork_done = &vfork; 29401da177e4SLinus Torvalds init_completion(&vfork); 2941d68b46feSOleg Nesterov get_task_struct(p); 29421da177e4SLinus Torvalds } 29431da177e4SLinus Torvalds 2944bd74fdaeSYu Zhao if (IS_ENABLED(CONFIG_LRU_GEN) && !(clone_flags & CLONE_VM)) { 2945bd74fdaeSYu Zhao /* lock the task to synchronize with memcg migration */ 2946bd74fdaeSYu Zhao task_lock(p); 2947bd74fdaeSYu Zhao lru_gen_add_mm(p->mm); 2948bd74fdaeSYu Zhao task_unlock(p); 2949bd74fdaeSYu Zhao } 2950bd74fdaeSYu Zhao 29513e51e3edSSamir Bellabes wake_up_new_task(p); 29521da177e4SLinus Torvalds 29534b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 29544b9d33e6STejun Heo if (unlikely(trace)) 29554e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 295609a05394SRoland McGrath 29571da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 2958d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 29594e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 29609f59ce5dSChuck Ebbert } 29614e52365fSMatthew Dempsky 29624e52365fSMatthew Dempsky put_pid(pid); 296392476d7fSEric W. Biederman return nr; 29641da177e4SLinus Torvalds } 29651da177e4SLinus Torvalds 29662aa3a7f8SAl Viro /* 29672aa3a7f8SAl Viro * Create a kernel thread. 29682aa3a7f8SAl Viro */ 2969cf587db2SMike Christie pid_t kernel_thread(int (*fn)(void *), void *arg, const char *name, 2970cf587db2SMike Christie unsigned long flags) 29712aa3a7f8SAl Viro { 29727f192e3cSChristian Brauner struct kernel_clone_args args = { 29733f2c788aSChristian Brauner .flags = ((lower_32_bits(flags) | CLONE_VM | 29743f2c788aSChristian Brauner CLONE_UNTRACED) & ~CSIGNAL), 29753f2c788aSChristian Brauner .exit_signal = (lower_32_bits(flags) & CSIGNAL), 29765bd2e97cSEric W. Biederman .fn = fn, 29775bd2e97cSEric W. Biederman .fn_arg = arg, 2978cf587db2SMike Christie .name = name, 2979343f4c49SEric W. Biederman .kthread = 1, 2980343f4c49SEric W. Biederman }; 2981343f4c49SEric W. Biederman 2982343f4c49SEric W. Biederman return kernel_clone(&args); 2983343f4c49SEric W. Biederman } 2984343f4c49SEric W. Biederman 2985343f4c49SEric W. Biederman /* 2986343f4c49SEric W. Biederman * Create a user mode thread. 2987343f4c49SEric W. Biederman */ 2988343f4c49SEric W. Biederman pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags) 2989343f4c49SEric W. Biederman { 2990343f4c49SEric W. Biederman struct kernel_clone_args args = { 2991343f4c49SEric W. Biederman .flags = ((lower_32_bits(flags) | CLONE_VM | 2992343f4c49SEric W. Biederman CLONE_UNTRACED) & ~CSIGNAL), 2993343f4c49SEric W. Biederman .exit_signal = (lower_32_bits(flags) & CSIGNAL), 29945bd2e97cSEric W. Biederman .fn = fn, 29955bd2e97cSEric W. Biederman .fn_arg = arg, 29967f192e3cSChristian Brauner }; 29977f192e3cSChristian Brauner 2998cad6967aSChristian Brauner return kernel_clone(&args); 29992aa3a7f8SAl Viro } 30002aa3a7f8SAl Viro 3001d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 3002d2125043SAl Viro SYSCALL_DEFINE0(fork) 3003d2125043SAl Viro { 3004d2125043SAl Viro #ifdef CONFIG_MMU 30057f192e3cSChristian Brauner struct kernel_clone_args args = { 30067f192e3cSChristian Brauner .exit_signal = SIGCHLD, 30077f192e3cSChristian Brauner }; 30087f192e3cSChristian Brauner 3009cad6967aSChristian Brauner return kernel_clone(&args); 3010d2125043SAl Viro #else 3011d2125043SAl Viro /* can not support in nommu mode */ 30125d59e182SDaeseok Youn return -EINVAL; 3013d2125043SAl Viro #endif 3014d2125043SAl Viro } 3015d2125043SAl Viro #endif 3016d2125043SAl Viro 3017d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 3018d2125043SAl Viro SYSCALL_DEFINE0(vfork) 3019d2125043SAl Viro { 30207f192e3cSChristian Brauner struct kernel_clone_args args = { 30217f192e3cSChristian Brauner .flags = CLONE_VFORK | CLONE_VM, 30227f192e3cSChristian Brauner .exit_signal = SIGCHLD, 30237f192e3cSChristian Brauner }; 30247f192e3cSChristian Brauner 3025cad6967aSChristian Brauner return kernel_clone(&args); 3026d2125043SAl Viro } 3027d2125043SAl Viro #endif 3028d2125043SAl Viro 3029d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 3030d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 3031d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 3032d2125043SAl Viro int __user *, parent_tidptr, 30333033f14aSJosh Triplett unsigned long, tls, 3034d2125043SAl Viro int __user *, child_tidptr) 3035d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 3036d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 3037d2125043SAl Viro int __user *, parent_tidptr, 3038d2125043SAl Viro int __user *, child_tidptr, 30393033f14aSJosh Triplett unsigned long, tls) 3040dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 3041dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 3042dfa9771aSMichal Simek int, stack_size, 3043dfa9771aSMichal Simek int __user *, parent_tidptr, 3044dfa9771aSMichal Simek int __user *, child_tidptr, 30453033f14aSJosh Triplett unsigned long, tls) 3046d2125043SAl Viro #else 3047d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 3048d2125043SAl Viro int __user *, parent_tidptr, 3049d2125043SAl Viro int __user *, child_tidptr, 30503033f14aSJosh Triplett unsigned long, tls) 3051d2125043SAl Viro #endif 3052d2125043SAl Viro { 30537f192e3cSChristian Brauner struct kernel_clone_args args = { 30543f2c788aSChristian Brauner .flags = (lower_32_bits(clone_flags) & ~CSIGNAL), 30557f192e3cSChristian Brauner .pidfd = parent_tidptr, 30567f192e3cSChristian Brauner .child_tid = child_tidptr, 30577f192e3cSChristian Brauner .parent_tid = parent_tidptr, 30583f2c788aSChristian Brauner .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL), 30597f192e3cSChristian Brauner .stack = newsp, 30607f192e3cSChristian Brauner .tls = tls, 30617f192e3cSChristian Brauner }; 30627f192e3cSChristian Brauner 3063cad6967aSChristian Brauner return kernel_clone(&args); 30647f192e3cSChristian Brauner } 3065d68dbb0cSChristian Brauner #endif 30667f192e3cSChristian Brauner 3067d68dbb0cSChristian Brauner #ifdef __ARCH_WANT_SYS_CLONE3 3068dd499f7aSAmanieu d'Antras 30697f192e3cSChristian Brauner noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, 30707f192e3cSChristian Brauner struct clone_args __user *uargs, 3071f14c234bSAleksa Sarai size_t usize) 30727f192e3cSChristian Brauner { 3073f14c234bSAleksa Sarai int err; 30747f192e3cSChristian Brauner struct clone_args args; 307549cb2fc4SAdrian Reber pid_t *kset_tid = kargs->set_tid; 30767f192e3cSChristian Brauner 3077a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, tls) != 3078a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER0); 3079a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, set_tid_size) != 3080a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER1); 3081a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) != 3082a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER2); 3083a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2); 3084a966dcfeSEugene Syromiatnikov 3085f14c234bSAleksa Sarai if (unlikely(usize > PAGE_SIZE)) 30867f192e3cSChristian Brauner return -E2BIG; 3087f14c234bSAleksa Sarai if (unlikely(usize < CLONE_ARGS_SIZE_VER0)) 30887f192e3cSChristian Brauner return -EINVAL; 30897f192e3cSChristian Brauner 3090f14c234bSAleksa Sarai err = copy_struct_from_user(&args, sizeof(args), uargs, usize); 3091f14c234bSAleksa Sarai if (err) 3092f14c234bSAleksa Sarai return err; 30937f192e3cSChristian Brauner 309449cb2fc4SAdrian Reber if (unlikely(args.set_tid_size > MAX_PID_NS_LEVEL)) 309549cb2fc4SAdrian Reber return -EINVAL; 309649cb2fc4SAdrian Reber 309749cb2fc4SAdrian Reber if (unlikely(!args.set_tid && args.set_tid_size > 0)) 309849cb2fc4SAdrian Reber return -EINVAL; 309949cb2fc4SAdrian Reber 310049cb2fc4SAdrian Reber if (unlikely(args.set_tid && args.set_tid_size == 0)) 310149cb2fc4SAdrian Reber return -EINVAL; 310249cb2fc4SAdrian Reber 3103a0eb9abdSEugene Syromiatnikov /* 3104a0eb9abdSEugene Syromiatnikov * Verify that higher 32bits of exit_signal are unset and that 3105a0eb9abdSEugene Syromiatnikov * it is a valid signal 3106a0eb9abdSEugene Syromiatnikov */ 3107a0eb9abdSEugene Syromiatnikov if (unlikely((args.exit_signal & ~((u64)CSIGNAL)) || 3108a0eb9abdSEugene Syromiatnikov !valid_signal(args.exit_signal))) 3109a0eb9abdSEugene Syromiatnikov return -EINVAL; 3110a0eb9abdSEugene Syromiatnikov 311162173872SEugene Syromiatnikov if ((args.flags & CLONE_INTO_CGROUP) && 311262173872SEugene Syromiatnikov (args.cgroup > INT_MAX || usize < CLONE_ARGS_SIZE_VER2)) 3113ef2c41cfSChristian Brauner return -EINVAL; 3114ef2c41cfSChristian Brauner 31157f192e3cSChristian Brauner *kargs = (struct kernel_clone_args){ 31167f192e3cSChristian Brauner .flags = args.flags, 31177f192e3cSChristian Brauner .pidfd = u64_to_user_ptr(args.pidfd), 31187f192e3cSChristian Brauner .child_tid = u64_to_user_ptr(args.child_tid), 31197f192e3cSChristian Brauner .parent_tid = u64_to_user_ptr(args.parent_tid), 31207f192e3cSChristian Brauner .exit_signal = args.exit_signal, 31217f192e3cSChristian Brauner .stack = args.stack, 31227f192e3cSChristian Brauner .stack_size = args.stack_size, 31237f192e3cSChristian Brauner .tls = args.tls, 312449cb2fc4SAdrian Reber .set_tid_size = args.set_tid_size, 3125ef2c41cfSChristian Brauner .cgroup = args.cgroup, 31267f192e3cSChristian Brauner }; 31277f192e3cSChristian Brauner 312849cb2fc4SAdrian Reber if (args.set_tid && 312949cb2fc4SAdrian Reber copy_from_user(kset_tid, u64_to_user_ptr(args.set_tid), 313049cb2fc4SAdrian Reber (kargs->set_tid_size * sizeof(pid_t)))) 313149cb2fc4SAdrian Reber return -EFAULT; 313249cb2fc4SAdrian Reber 313349cb2fc4SAdrian Reber kargs->set_tid = kset_tid; 313449cb2fc4SAdrian Reber 31357f192e3cSChristian Brauner return 0; 31367f192e3cSChristian Brauner } 31377f192e3cSChristian Brauner 3138fa729c4dSChristian Brauner /** 3139fa729c4dSChristian Brauner * clone3_stack_valid - check and prepare stack 3140fa729c4dSChristian Brauner * @kargs: kernel clone args 3141fa729c4dSChristian Brauner * 3142fa729c4dSChristian Brauner * Verify that the stack arguments userspace gave us are sane. 3143fa729c4dSChristian Brauner * In addition, set the stack direction for userspace since it's easy for us to 3144fa729c4dSChristian Brauner * determine. 3145fa729c4dSChristian Brauner */ 3146fa729c4dSChristian Brauner static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) 3147fa729c4dSChristian Brauner { 3148fa729c4dSChristian Brauner if (kargs->stack == 0) { 3149fa729c4dSChristian Brauner if (kargs->stack_size > 0) 3150fa729c4dSChristian Brauner return false; 3151fa729c4dSChristian Brauner } else { 3152fa729c4dSChristian Brauner if (kargs->stack_size == 0) 3153fa729c4dSChristian Brauner return false; 3154fa729c4dSChristian Brauner 3155fa729c4dSChristian Brauner if (!access_ok((void __user *)kargs->stack, kargs->stack_size)) 3156fa729c4dSChristian Brauner return false; 3157fa729c4dSChristian Brauner 3158fa729c4dSChristian Brauner #if !defined(CONFIG_STACK_GROWSUP) && !defined(CONFIG_IA64) 3159fa729c4dSChristian Brauner kargs->stack += kargs->stack_size; 3160fa729c4dSChristian Brauner #endif 3161fa729c4dSChristian Brauner } 3162fa729c4dSChristian Brauner 3163fa729c4dSChristian Brauner return true; 3164fa729c4dSChristian Brauner } 3165fa729c4dSChristian Brauner 3166fa729c4dSChristian Brauner static bool clone3_args_valid(struct kernel_clone_args *kargs) 31677f192e3cSChristian Brauner { 3168b612e5dfSChristian Brauner /* Verify that no unknown flags are passed along. */ 3169ef2c41cfSChristian Brauner if (kargs->flags & 3170ef2c41cfSChristian Brauner ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP)) 31717f192e3cSChristian Brauner return false; 31727f192e3cSChristian Brauner 31737f192e3cSChristian Brauner /* 3174a8ca6b13SXiaofeng Cao * - make the CLONE_DETACHED bit reusable for clone3 3175a8ca6b13SXiaofeng Cao * - make the CSIGNAL bits reusable for clone3 31767f192e3cSChristian Brauner */ 3177a402f1e3STobias Klauser if (kargs->flags & (CLONE_DETACHED | (CSIGNAL & (~CLONE_NEWTIME)))) 31787f192e3cSChristian Brauner return false; 31797f192e3cSChristian Brauner 3180b612e5dfSChristian Brauner if ((kargs->flags & (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) == 3181b612e5dfSChristian Brauner (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) 3182b612e5dfSChristian Brauner return false; 3183b612e5dfSChristian Brauner 31847f192e3cSChristian Brauner if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) && 31857f192e3cSChristian Brauner kargs->exit_signal) 31867f192e3cSChristian Brauner return false; 31877f192e3cSChristian Brauner 3188fa729c4dSChristian Brauner if (!clone3_stack_valid(kargs)) 3189fa729c4dSChristian Brauner return false; 3190fa729c4dSChristian Brauner 31917f192e3cSChristian Brauner return true; 31927f192e3cSChristian Brauner } 31937f192e3cSChristian Brauner 3194501bd016SChristian Brauner /** 3195501bd016SChristian Brauner * clone3 - create a new process with specific properties 3196501bd016SChristian Brauner * @uargs: argument structure 3197501bd016SChristian Brauner * @size: size of @uargs 3198501bd016SChristian Brauner * 3199501bd016SChristian Brauner * clone3() is the extensible successor to clone()/clone2(). 3200501bd016SChristian Brauner * It takes a struct as argument that is versioned by its size. 3201501bd016SChristian Brauner * 3202501bd016SChristian Brauner * Return: On success, a positive PID for the child process. 3203501bd016SChristian Brauner * On error, a negative errno number. 3204501bd016SChristian Brauner */ 32057f192e3cSChristian Brauner SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size) 32067f192e3cSChristian Brauner { 32077f192e3cSChristian Brauner int err; 32087f192e3cSChristian Brauner 32097f192e3cSChristian Brauner struct kernel_clone_args kargs; 321049cb2fc4SAdrian Reber pid_t set_tid[MAX_PID_NS_LEVEL]; 321149cb2fc4SAdrian Reber 321249cb2fc4SAdrian Reber kargs.set_tid = set_tid; 32137f192e3cSChristian Brauner 32147f192e3cSChristian Brauner err = copy_clone_args_from_user(&kargs, uargs, size); 32157f192e3cSChristian Brauner if (err) 32167f192e3cSChristian Brauner return err; 32177f192e3cSChristian Brauner 32187f192e3cSChristian Brauner if (!clone3_args_valid(&kargs)) 32197f192e3cSChristian Brauner return -EINVAL; 32207f192e3cSChristian Brauner 3221cad6967aSChristian Brauner return kernel_clone(&kargs); 3222d2125043SAl Viro } 3223d2125043SAl Viro #endif 3224d2125043SAl Viro 32250f1b92cbSOleg Nesterov void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) 32260f1b92cbSOleg Nesterov { 32270f1b92cbSOleg Nesterov struct task_struct *leader, *parent, *child; 32280f1b92cbSOleg Nesterov int res; 32290f1b92cbSOleg Nesterov 32300f1b92cbSOleg Nesterov read_lock(&tasklist_lock); 32310f1b92cbSOleg Nesterov leader = top = top->group_leader; 32320f1b92cbSOleg Nesterov down: 32330f1b92cbSOleg Nesterov for_each_thread(leader, parent) { 32340f1b92cbSOleg Nesterov list_for_each_entry(child, &parent->children, sibling) { 32350f1b92cbSOleg Nesterov res = visitor(child, data); 32360f1b92cbSOleg Nesterov if (res) { 32370f1b92cbSOleg Nesterov if (res < 0) 32380f1b92cbSOleg Nesterov goto out; 32390f1b92cbSOleg Nesterov leader = child; 32400f1b92cbSOleg Nesterov goto down; 32410f1b92cbSOleg Nesterov } 32420f1b92cbSOleg Nesterov up: 32430f1b92cbSOleg Nesterov ; 32440f1b92cbSOleg Nesterov } 32450f1b92cbSOleg Nesterov } 32460f1b92cbSOleg Nesterov 32470f1b92cbSOleg Nesterov if (leader != top) { 32480f1b92cbSOleg Nesterov child = leader; 32490f1b92cbSOleg Nesterov parent = child->real_parent; 32500f1b92cbSOleg Nesterov leader = parent->group_leader; 32510f1b92cbSOleg Nesterov goto up; 32520f1b92cbSOleg Nesterov } 32530f1b92cbSOleg Nesterov out: 32540f1b92cbSOleg Nesterov read_unlock(&tasklist_lock); 32550f1b92cbSOleg Nesterov } 32560f1b92cbSOleg Nesterov 32575fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 32585fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 32595fd63b30SRavikiran G Thirumalai #endif 32605fd63b30SRavikiran G Thirumalai 326151cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 3262aa1757f9SOleg Nesterov { 3263aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 3264aa1757f9SOleg Nesterov 3265aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 3266b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 3267fba2afaaSDavide Libenzi } 3268aa1757f9SOleg Nesterov 3269af806027SPeter Zijlstra void __init mm_cache_init(void) 32701da177e4SLinus Torvalds { 3271c1a2f7f0SRik van Riel unsigned int mm_size; 3272c1a2f7f0SRik van Riel 3273af806027SPeter Zijlstra /* 3274af806027SPeter Zijlstra * The mm_cpumask is located at the end of mm_struct, and is 3275af806027SPeter Zijlstra * dynamically sized based on the maximum CPU number this system 3276af806027SPeter Zijlstra * can have, taking hotplug into account (nr_cpu_ids). 3277af806027SPeter Zijlstra */ 3278af7f588dSMathieu Desnoyers mm_size = sizeof(struct mm_struct) + cpumask_size() + mm_cid_size(); 3279af806027SPeter Zijlstra 3280af806027SPeter Zijlstra mm_cachep = kmem_cache_create_usercopy("mm_struct", 3281af806027SPeter Zijlstra mm_size, ARCH_MIN_MMSTRUCT_ALIGN, 3282af806027SPeter Zijlstra SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 3283af806027SPeter Zijlstra offsetof(struct mm_struct, saved_auxv), 3284af806027SPeter Zijlstra sizeof_field(struct mm_struct, saved_auxv), 3285af806027SPeter Zijlstra NULL); 3286af806027SPeter Zijlstra } 3287af806027SPeter Zijlstra 3288af806027SPeter Zijlstra void __init proc_caches_init(void) 3289af806027SPeter Zijlstra { 32901da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 32911da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 32925f0d5a3aSPaul E. McKenney SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| 329375f296d9SLevin, Alexander (Sasha Levin) SLAB_ACCOUNT, sighand_ctor); 32941da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 32951da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 329675f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 32975d097056SVladimir Davydov NULL); 32981da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 32991da177e4SLinus Torvalds sizeof(struct files_struct), 0, 330075f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 33015d097056SVladimir Davydov NULL); 33021da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 33031da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 330475f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 33055d097056SVladimir Davydov NULL); 3306c1a2f7f0SRik van Riel 33075d097056SVladimir Davydov vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 3308c7f8f31cSSuren Baghdasaryan #ifdef CONFIG_PER_VMA_LOCK 3309c7f8f31cSSuren Baghdasaryan vma_lock_cachep = KMEM_CACHE(vma_lock, SLAB_PANIC|SLAB_ACCOUNT); 3310c7f8f31cSSuren Baghdasaryan #endif 33118feae131SDavid Howells mmap_init(); 331266577193SAl Viro nsproxy_cache_init(); 33131da177e4SLinus Torvalds } 3314cf2e340fSJANAK DESAI 3315cf2e340fSJANAK DESAI /* 33169bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 3317cf2e340fSJANAK DESAI */ 33189bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 3319cf2e340fSJANAK DESAI { 33209bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 33219bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 332250804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 3323769071acSAndrei Vagin CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP| 3324769071acSAndrei Vagin CLONE_NEWTIME)) 3325cf2e340fSJANAK DESAI return -EINVAL; 33269bfb23fcSOleg Nesterov /* 332712c641abSEric W. Biederman * Not implemented, but pretend it works if there is nothing 332812c641abSEric W. Biederman * to unshare. Note that unsharing the address space or the 332912c641abSEric W. Biederman * signal handlers also need to unshare the signal queues (aka 333012c641abSEric W. Biederman * CLONE_THREAD). 33319bfb23fcSOleg Nesterov */ 33329bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 333312c641abSEric W. Biederman if (!thread_group_empty(current)) 333412c641abSEric W. Biederman return -EINVAL; 333512c641abSEric W. Biederman } 333612c641abSEric W. Biederman if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 3337d036bda7SElena Reshetova if (refcount_read(¤t->sighand->count) > 1) 333812c641abSEric W. Biederman return -EINVAL; 333912c641abSEric W. Biederman } 334012c641abSEric W. Biederman if (unshare_flags & CLONE_VM) { 334112c641abSEric W. Biederman if (!current_is_single_threaded()) 33429bfb23fcSOleg Nesterov return -EINVAL; 33439bfb23fcSOleg Nesterov } 3344cf2e340fSJANAK DESAI 3345cf2e340fSJANAK DESAI return 0; 3346cf2e340fSJANAK DESAI } 3347cf2e340fSJANAK DESAI 3348cf2e340fSJANAK DESAI /* 334999d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 3350cf2e340fSJANAK DESAI */ 3351cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 3352cf2e340fSJANAK DESAI { 3353cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 3354cf2e340fSJANAK DESAI 3355498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 3356498052bbSAl Viro return 0; 3357498052bbSAl Viro 3358498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 3359498052bbSAl Viro if (fs->users == 1) 3360498052bbSAl Viro return 0; 3361498052bbSAl Viro 3362498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 336399d1419dSJANAK DESAI if (!*new_fsp) 336499d1419dSJANAK DESAI return -ENOMEM; 3365cf2e340fSJANAK DESAI 3366cf2e340fSJANAK DESAI return 0; 3367cf2e340fSJANAK DESAI } 3368cf2e340fSJANAK DESAI 3369cf2e340fSJANAK DESAI /* 3370a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 3371cf2e340fSJANAK DESAI */ 337260997c3dSChristian Brauner int unshare_fd(unsigned long unshare_flags, unsigned int max_fds, 337360997c3dSChristian Brauner struct files_struct **new_fdp) 3374cf2e340fSJANAK DESAI { 3375cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 3376a016f338SJANAK DESAI int error = 0; 3377cf2e340fSJANAK DESAI 3378cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 3379a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 338060997c3dSChristian Brauner *new_fdp = dup_fd(fd, max_fds, &error); 3381a016f338SJANAK DESAI if (!*new_fdp) 3382a016f338SJANAK DESAI return error; 3383a016f338SJANAK DESAI } 3384cf2e340fSJANAK DESAI 3385cf2e340fSJANAK DESAI return 0; 3386cf2e340fSJANAK DESAI } 3387cf2e340fSJANAK DESAI 3388cf2e340fSJANAK DESAI /* 3389cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 3390cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 3391cad6967aSChristian Brauner * functions used by kernel_clone() cannot be used here directly 3392cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 3393cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 3394cf2e340fSJANAK DESAI * task_struct. 3395cf2e340fSJANAK DESAI */ 33969b32105eSDominik Brodowski int ksys_unshare(unsigned long unshare_flags) 3397cf2e340fSJANAK DESAI { 3398cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 3399ba1f70ddSRan Xiaokai struct files_struct *new_fd = NULL; 3400b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 3401cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 34029edff4abSManfred Spraul int do_sysvsem = 0; 34039bfb23fcSOleg Nesterov int err; 3404cf2e340fSJANAK DESAI 340550804fe3SEric W. Biederman /* 3406faf00da5SEric W. Biederman * If unsharing a user namespace must also unshare the thread group 3407faf00da5SEric W. Biederman * and unshare the filesystem root and working directories. 3408b2e0d987SEric W. Biederman */ 3409b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 3410e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 3411b2e0d987SEric W. Biederman /* 341250804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 341350804fe3SEric W. Biederman */ 341450804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 341550804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 34166013f67fSManfred Spraul /* 341712c641abSEric W. Biederman * If unsharing a signal handlers, must also unshare the signal queues. 341812c641abSEric W. Biederman */ 341912c641abSEric W. Biederman if (unshare_flags & CLONE_SIGHAND) 342012c641abSEric W. Biederman unshare_flags |= CLONE_THREAD; 342112c641abSEric W. Biederman /* 34229bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 34239bfb23fcSOleg Nesterov */ 34249bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 34259bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 342650804fe3SEric W. Biederman 342750804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 342850804fe3SEric W. Biederman if (err) 342950804fe3SEric W. Biederman goto bad_unshare_out; 34309bfb23fcSOleg Nesterov /* 34316013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 34326013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 34336013f67fSManfred Spraul * namespace are unreachable. 34346013f67fSManfred Spraul */ 34356013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 34369edff4abSManfred Spraul do_sysvsem = 1; 3437fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 3438fb0a685cSDaniel Rebelo de Oliveira if (err) 34399bfb23fcSOleg Nesterov goto bad_unshare_out; 344060997c3dSChristian Brauner err = unshare_fd(unshare_flags, NR_OPEN_MAX, &new_fd); 3441fb0a685cSDaniel Rebelo de Oliveira if (err) 34429bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 3443b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 3444fb0a685cSDaniel Rebelo de Oliveira if (err) 34459edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 3446b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 3447b2e0d987SEric W. Biederman new_cred, new_fs); 3448b2e0d987SEric W. Biederman if (err) 3449b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 3450cf2e340fSJANAK DESAI 3451905ae01cSAlexey Gladkov if (new_cred) { 3452905ae01cSAlexey Gladkov err = set_cred_ucounts(new_cred); 3453905ae01cSAlexey Gladkov if (err) 3454905ae01cSAlexey Gladkov goto bad_unshare_cleanup_cred; 3455905ae01cSAlexey Gladkov } 3456905ae01cSAlexey Gladkov 3457b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 34589edff4abSManfred Spraul if (do_sysvsem) { 34599edff4abSManfred Spraul /* 34609edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 34619edff4abSManfred Spraul */ 34629edff4abSManfred Spraul exit_sem(current); 34639edff4abSManfred Spraul } 3464ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 3465ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 3466ab602f79SJack Miller exit_shm(current); 3467ab602f79SJack Miller shm_init_task(current); 3468ab602f79SJack Miller } 3469ab516013SSerge E. Hallyn 34706f977e6bSAlan Cox if (new_nsproxy) 3471cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 3472cf2e340fSJANAK DESAI 3473cf7b708cSPavel Emelyanov task_lock(current); 3474cf7b708cSPavel Emelyanov 3475cf2e340fSJANAK DESAI if (new_fs) { 3476cf2e340fSJANAK DESAI fs = current->fs; 34772a4419b5SNick Piggin spin_lock(&fs->lock); 3478cf2e340fSJANAK DESAI current->fs = new_fs; 3479498052bbSAl Viro if (--fs->users) 3480498052bbSAl Viro new_fs = NULL; 3481498052bbSAl Viro else 3482cf2e340fSJANAK DESAI new_fs = fs; 34832a4419b5SNick Piggin spin_unlock(&fs->lock); 3484cf2e340fSJANAK DESAI } 3485cf2e340fSJANAK DESAI 3486ba1f70ddSRan Xiaokai if (new_fd) 3487ba1f70ddSRan Xiaokai swap(current->files, new_fd); 3488cf2e340fSJANAK DESAI 3489cf2e340fSJANAK DESAI task_unlock(current); 3490b2e0d987SEric W. Biederman 3491b2e0d987SEric W. Biederman if (new_cred) { 3492b2e0d987SEric W. Biederman /* Install the new user namespace */ 3493b2e0d987SEric W. Biederman commit_creds(new_cred); 3494b2e0d987SEric W. Biederman new_cred = NULL; 3495b2e0d987SEric W. Biederman } 3496cf2e340fSJANAK DESAI } 3497cf2e340fSJANAK DESAI 3498e4222673SHari Bathini perf_event_namespaces(current); 3499e4222673SHari Bathini 3500b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 3501b2e0d987SEric W. Biederman if (new_cred) 3502b2e0d987SEric W. Biederman put_cred(new_cred); 3503cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 3504cf2e340fSJANAK DESAI if (new_fd) 3505cf2e340fSJANAK DESAI put_files_struct(new_fd); 3506cf2e340fSJANAK DESAI 3507cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 3508cf2e340fSJANAK DESAI if (new_fs) 3509498052bbSAl Viro free_fs_struct(new_fs); 3510cf2e340fSJANAK DESAI 3511cf2e340fSJANAK DESAI bad_unshare_out: 3512cf2e340fSJANAK DESAI return err; 3513cf2e340fSJANAK DESAI } 35143b125388SAl Viro 35159b32105eSDominik Brodowski SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 35169b32105eSDominik Brodowski { 35179b32105eSDominik Brodowski return ksys_unshare(unshare_flags); 35189b32105eSDominik Brodowski } 35199b32105eSDominik Brodowski 35203b125388SAl Viro /* 35213b125388SAl Viro * Helper to unshare the files of the current task. 35223b125388SAl Viro * We don't want to expose copy_files internals to 35233b125388SAl Viro * the exec layer of the kernel. 35243b125388SAl Viro */ 35253b125388SAl Viro 35261f702603SEric W. Biederman int unshare_files(void) 35273b125388SAl Viro { 35283b125388SAl Viro struct task_struct *task = current; 35291f702603SEric W. Biederman struct files_struct *old, *copy = NULL; 35303b125388SAl Viro int error; 35313b125388SAl Viro 353260997c3dSChristian Brauner error = unshare_fd(CLONE_FILES, NR_OPEN_MAX, ©); 35331f702603SEric W. Biederman if (error || !copy) 35343b125388SAl Viro return error; 35351f702603SEric W. Biederman 35361f702603SEric W. Biederman old = task->files; 35373b125388SAl Viro task_lock(task); 35383b125388SAl Viro task->files = copy; 35393b125388SAl Viro task_unlock(task); 35401f702603SEric W. Biederman put_files_struct(old); 35413b125388SAl Viro return 0; 35423b125388SAl Viro } 354316db3d3fSHeinrich Schuchardt 354416db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write, 3545b0daa2c7STobias Klauser void *buffer, size_t *lenp, loff_t *ppos) 354616db3d3fSHeinrich Schuchardt { 354716db3d3fSHeinrich Schuchardt struct ctl_table t; 354816db3d3fSHeinrich Schuchardt int ret; 354916db3d3fSHeinrich Schuchardt int threads = max_threads; 3550b0f53dbcSMichal Hocko int min = 1; 355116db3d3fSHeinrich Schuchardt int max = MAX_THREADS; 355216db3d3fSHeinrich Schuchardt 355316db3d3fSHeinrich Schuchardt t = *table; 355416db3d3fSHeinrich Schuchardt t.data = &threads; 355516db3d3fSHeinrich Schuchardt t.extra1 = &min; 355616db3d3fSHeinrich Schuchardt t.extra2 = &max; 355716db3d3fSHeinrich Schuchardt 355816db3d3fSHeinrich Schuchardt ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 355916db3d3fSHeinrich Schuchardt if (ret || !write) 356016db3d3fSHeinrich Schuchardt return ret; 356116db3d3fSHeinrich Schuchardt 3562b0f53dbcSMichal Hocko max_threads = threads; 356316db3d3fSHeinrich Schuchardt 356416db3d3fSHeinrich Schuchardt return 0; 356516db3d3fSHeinrich Schuchardt } 3566