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> 401da177e4SLinus Torvalds #include <linux/binfmts.h> 411da177e4SLinus Torvalds #include <linux/mman.h> 42cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 431da177e4SLinus Torvalds #include <linux/fs.h> 44615d6e87SDavidlohr Bueso #include <linux/mm.h> 4517fca131SArnd Bergmann #include <linux/mm_inline.h> 46ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 47c59ede7bSRandy.Dunlap #include <linux/capability.h> 481da177e4SLinus Torvalds #include <linux/cpu.h> 49b4f48b63SPaul Menage #include <linux/cgroup.h> 501da177e4SLinus Torvalds #include <linux/security.h> 51a1e78772SMel Gorman #include <linux/hugetlb.h> 52e2cfabdfSWill Drewry #include <linux/seccomp.h> 531da177e4SLinus Torvalds #include <linux/swap.h> 541da177e4SLinus Torvalds #include <linux/syscalls.h> 551da177e4SLinus Torvalds #include <linux/jiffies.h> 561da177e4SLinus Torvalds #include <linux/futex.h> 578141c7f3SLinus Torvalds #include <linux/compat.h> 58207205a2SEric Dumazet #include <linux/kthread.h> 597c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 60ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 611da177e4SLinus Torvalds #include <linux/ptrace.h> 621da177e4SLinus Torvalds #include <linux/mount.h> 631da177e4SLinus Torvalds #include <linux/audit.h> 6478fb7466SPavel Emelianov #include <linux/memcontrol.h> 65f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 665e2bf014SMike Galbraith #include <linux/proc_fs.h> 671da177e4SLinus Torvalds #include <linux/profile.h> 681da177e4SLinus Torvalds #include <linux/rmap.h> 69f8af4da3SHugh Dickins #include <linux/ksm.h> 701da177e4SLinus Torvalds #include <linux/acct.h> 71893e26e6SPavel Emelyanov #include <linux/userfaultfd_k.h> 728f0ab514SJay Lan #include <linux/tsacct_kern.h> 739f46080cSMatt Helsley #include <linux/cn_proc.h> 74ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 75ca74e92bSShailabh Nagar #include <linux/delayacct.h> 76ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 770a425405SArjan van de Ven #include <linux/random.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> 99a6cbd440SFenghua Yu #include <linux/sched/mm.h> 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds #include <asm/pgalloc.h> 1027c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 1031da177e4SLinus Torvalds #include <asm/mmu_context.h> 1041da177e4SLinus Torvalds #include <asm/cacheflush.h> 1051da177e4SLinus Torvalds #include <asm/tlbflush.h> 1061da177e4SLinus Torvalds 107ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 108ad8d75ffSSteven Rostedt 10943d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 11043d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 11143d2b113SKAMEZAWA Hiroyuki 1121da177e4SLinus Torvalds /* 113ac1b398dSHeinrich Schuchardt * Minimum number of threads to boot the kernel 114ac1b398dSHeinrich Schuchardt */ 115ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20 116ac1b398dSHeinrich Schuchardt 117ac1b398dSHeinrich Schuchardt /* 118ac1b398dSHeinrich Schuchardt * Maximum number of threads 119ac1b398dSHeinrich Schuchardt */ 120ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK 121ac1b398dSHeinrich Schuchardt 122ac1b398dSHeinrich Schuchardt /* 1231da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 1241da177e4SLinus Torvalds */ 1251da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 1261da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 1271da177e4SLinus Torvalds 1288856ae4dSKefeng Wang static int max_threads; /* tunable limit on nr_threads */ 1291da177e4SLinus Torvalds 1308495f7e6SSai Praneeth Prakhya #define NAMED_ARRAY_INDEX(x) [x] = __stringify(x) 1318495f7e6SSai Praneeth Prakhya 1328495f7e6SSai Praneeth Prakhya static const char * const resident_page_types[] = { 1338495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_FILEPAGES), 1348495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_ANONPAGES), 1358495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_SWAPENTS), 1368495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_SHMEMPAGES), 1378495f7e6SSai Praneeth Prakhya }; 1388495f7e6SSai Praneeth Prakhya 1391da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 142db1466b3SPaul E. McKenney 143db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 144db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 145db1466b3SPaul E. McKenney { 146db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 147db1466b3SPaul E. McKenney } 148db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 149db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds int nr_processes(void) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds int cpu; 1541da177e4SLinus Torvalds int total = 0; 1551da177e4SLinus Torvalds 1561d510750SIan Campbell for_each_possible_cpu(cpu) 1571da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds return total; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 162f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 163f19b9f74SAkinobu Mita { 164f19b9f74SAkinobu Mita } 165f19b9f74SAkinobu Mita 166f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 167e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 16841101809SThomas Gleixner 16941101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 17041101809SThomas Gleixner { 17141101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 17241101809SThomas Gleixner } 17341101809SThomas Gleixner 17441101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 17541101809SThomas Gleixner { 17641101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 17741101809SThomas Gleixner } 1781da177e4SLinus Torvalds #endif 1791da177e4SLinus Torvalds 180b235beeaSLinus Torvalds #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR 18141101809SThomas Gleixner 1820d15d74aSThomas Gleixner /* 1830d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1840d15d74aSThomas Gleixner * kmemcache based allocator. 1850d15d74aSThomas Gleixner */ 186ba14a194SAndy Lutomirski # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) 187ac496bf4SAndy Lutomirski 188ac496bf4SAndy Lutomirski # ifdef CONFIG_VMAP_STACK 189ac496bf4SAndy Lutomirski /* 190ac496bf4SAndy Lutomirski * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 191ac496bf4SAndy Lutomirski * flush. Try to minimize the number of calls by caching stacks. 192ac496bf4SAndy Lutomirski */ 193ac496bf4SAndy Lutomirski #define NR_CACHED_STACKS 2 194ac496bf4SAndy Lutomirski static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 19519659c59SHoeun Ryu 196e540bf31SSebastian Andrzej Siewior struct vm_stack { 197e540bf31SSebastian Andrzej Siewior struct rcu_head rcu; 198e540bf31SSebastian Andrzej Siewior struct vm_struct *stack_vm_area; 199e540bf31SSebastian Andrzej Siewior }; 200e540bf31SSebastian Andrzej Siewior 201e540bf31SSebastian Andrzej Siewior static bool try_release_thread_stack_to_cache(struct vm_struct *vm) 202e540bf31SSebastian Andrzej Siewior { 203e540bf31SSebastian Andrzej Siewior unsigned int i; 204e540bf31SSebastian Andrzej Siewior 205e540bf31SSebastian Andrzej Siewior for (i = 0; i < NR_CACHED_STACKS; i++) { 206e540bf31SSebastian Andrzej Siewior if (this_cpu_cmpxchg(cached_stacks[i], NULL, vm) != NULL) 207e540bf31SSebastian Andrzej Siewior continue; 208e540bf31SSebastian Andrzej Siewior return true; 209e540bf31SSebastian Andrzej Siewior } 210e540bf31SSebastian Andrzej Siewior return false; 211e540bf31SSebastian Andrzej Siewior } 212e540bf31SSebastian Andrzej Siewior 213e540bf31SSebastian Andrzej Siewior static void thread_stack_free_rcu(struct rcu_head *rh) 214e540bf31SSebastian Andrzej Siewior { 215e540bf31SSebastian Andrzej Siewior struct vm_stack *vm_stack = container_of(rh, struct vm_stack, rcu); 216e540bf31SSebastian Andrzej Siewior 217e540bf31SSebastian Andrzej Siewior if (try_release_thread_stack_to_cache(vm_stack->stack_vm_area)) 218e540bf31SSebastian Andrzej Siewior return; 219e540bf31SSebastian Andrzej Siewior 220e540bf31SSebastian Andrzej Siewior vfree(vm_stack); 221e540bf31SSebastian Andrzej Siewior } 222e540bf31SSebastian Andrzej Siewior 223e540bf31SSebastian Andrzej Siewior static void thread_stack_delayed_free(struct task_struct *tsk) 224e540bf31SSebastian Andrzej Siewior { 225e540bf31SSebastian Andrzej Siewior struct vm_stack *vm_stack = tsk->stack; 226e540bf31SSebastian Andrzej Siewior 227e540bf31SSebastian Andrzej Siewior vm_stack->stack_vm_area = tsk->stack_vm_area; 228e540bf31SSebastian Andrzej Siewior call_rcu(&vm_stack->rcu, thread_stack_free_rcu); 229e540bf31SSebastian Andrzej Siewior } 230e540bf31SSebastian Andrzej Siewior 23119659c59SHoeun Ryu static int free_vm_stack_cache(unsigned int cpu) 23219659c59SHoeun Ryu { 23319659c59SHoeun Ryu struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu); 23419659c59SHoeun Ryu int i; 23519659c59SHoeun Ryu 23619659c59SHoeun Ryu for (i = 0; i < NR_CACHED_STACKS; i++) { 23719659c59SHoeun Ryu struct vm_struct *vm_stack = cached_vm_stacks[i]; 23819659c59SHoeun Ryu 23919659c59SHoeun Ryu if (!vm_stack) 24019659c59SHoeun Ryu continue; 24119659c59SHoeun Ryu 24219659c59SHoeun Ryu vfree(vm_stack->addr); 24319659c59SHoeun Ryu cached_vm_stacks[i] = NULL; 24419659c59SHoeun Ryu } 24519659c59SHoeun Ryu 24619659c59SHoeun Ryu return 0; 24719659c59SHoeun Ryu } 248ac496bf4SAndy Lutomirski 2491a03d3f1SSebastian Andrzej Siewior static int memcg_charge_kernel_stack(struct vm_struct *vm) 250b69c49b7SFUJITA Tomonori { 251f1c1a9eeSSebastian Andrzej Siewior int i; 252f1c1a9eeSSebastian Andrzej Siewior int ret; 253f1c1a9eeSSebastian Andrzej Siewior 254f1c1a9eeSSebastian Andrzej Siewior BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); 255f1c1a9eeSSebastian Andrzej Siewior BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); 256f1c1a9eeSSebastian Andrzej Siewior 257f1c1a9eeSSebastian Andrzej Siewior for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 258f1c1a9eeSSebastian Andrzej Siewior ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL, 0); 259f1c1a9eeSSebastian Andrzej Siewior if (ret) 260f1c1a9eeSSebastian Andrzej Siewior goto err; 261f1c1a9eeSSebastian Andrzej Siewior } 262f1c1a9eeSSebastian Andrzej Siewior return 0; 263f1c1a9eeSSebastian Andrzej Siewior err: 264f1c1a9eeSSebastian Andrzej Siewior /* 265f1c1a9eeSSebastian Andrzej Siewior * If memcg_kmem_charge_page() fails, page's memory cgroup pointer is 266f1c1a9eeSSebastian Andrzej Siewior * NULL, and memcg_kmem_uncharge_page() in free_thread_stack() will 267f1c1a9eeSSebastian Andrzej Siewior * ignore this page. 268f1c1a9eeSSebastian Andrzej Siewior */ 269f1c1a9eeSSebastian Andrzej Siewior for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) 270f1c1a9eeSSebastian Andrzej Siewior memcg_kmem_uncharge_page(vm->pages[i], 0); 271f1c1a9eeSSebastian Andrzej Siewior return ret; 272f1c1a9eeSSebastian Andrzej Siewior } 273f1c1a9eeSSebastian Andrzej Siewior 2747865aba3SSebastian Andrzej Siewior static int alloc_thread_stack_node(struct task_struct *tsk, int node) 275b69c49b7SFUJITA Tomonori { 2761a03d3f1SSebastian Andrzej Siewior struct vm_struct *vm; 277ac496bf4SAndy Lutomirski void *stack; 278ac496bf4SAndy Lutomirski int i; 279ac496bf4SAndy Lutomirski 280ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 281112166f8SChristoph Lameter struct vm_struct *s; 282112166f8SChristoph Lameter 283112166f8SChristoph Lameter s = this_cpu_xchg(cached_stacks[i], NULL); 284ac496bf4SAndy Lutomirski 285ac496bf4SAndy Lutomirski if (!s) 286ac496bf4SAndy Lutomirski continue; 287ac496bf4SAndy Lutomirski 28851fb34deSAndrey Konovalov /* Reset stack metadata. */ 289cebd0eb2SAndrey Konovalov kasan_unpoison_range(s->addr, THREAD_SIZE); 290eafb149eSDaniel Axtens 29151fb34deSAndrey Konovalov stack = kasan_reset_tag(s->addr); 29251fb34deSAndrey Konovalov 293ca182551SKonstantin Khlebnikov /* Clear stale pointers from reused stack. */ 29451fb34deSAndrey Konovalov memset(stack, 0, THREAD_SIZE); 295e01e8063SKees Cook 2961a03d3f1SSebastian Andrzej Siewior if (memcg_charge_kernel_stack(s)) { 297f1c1a9eeSSebastian Andrzej Siewior vfree(s->addr); 298f1c1a9eeSSebastian Andrzej Siewior return -ENOMEM; 299f1c1a9eeSSebastian Andrzej Siewior } 300f1c1a9eeSSebastian Andrzej Siewior 301ac496bf4SAndy Lutomirski tsk->stack_vm_area = s; 30251fb34deSAndrey Konovalov tsk->stack = stack; 3037865aba3SSebastian Andrzej Siewior return 0; 304ac496bf4SAndy Lutomirski } 305ac496bf4SAndy Lutomirski 3069b6f7e16SRoman Gushchin /* 3079b6f7e16SRoman Gushchin * Allocated stacks are cached and later reused by new threads, 3089b6f7e16SRoman Gushchin * so memcg accounting is performed manually on assigning/releasing 3099b6f7e16SRoman Gushchin * stacks to tasks. Drop __GFP_ACCOUNT. 3109b6f7e16SRoman Gushchin */ 31148ac3c18SMark Rutland stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, 312ba14a194SAndy Lutomirski VMALLOC_START, VMALLOC_END, 3139b6f7e16SRoman Gushchin THREADINFO_GFP & ~__GFP_ACCOUNT, 314ba14a194SAndy Lutomirski PAGE_KERNEL, 315ac496bf4SAndy Lutomirski 0, node, __builtin_return_address(0)); 3167865aba3SSebastian Andrzej Siewior if (!stack) 3177865aba3SSebastian Andrzej Siewior return -ENOMEM; 318ba14a194SAndy Lutomirski 3191a03d3f1SSebastian Andrzej Siewior vm = find_vm_area(stack); 3201a03d3f1SSebastian Andrzej Siewior if (memcg_charge_kernel_stack(vm)) { 321f1c1a9eeSSebastian Andrzej Siewior vfree(stack); 322f1c1a9eeSSebastian Andrzej Siewior return -ENOMEM; 323f1c1a9eeSSebastian Andrzej Siewior } 324ba14a194SAndy Lutomirski /* 325ba14a194SAndy Lutomirski * We can't call find_vm_area() in interrupt context, and 326ba14a194SAndy Lutomirski * free_thread_stack() can be called in interrupt context, 327ba14a194SAndy Lutomirski * so cache the vm_struct. 328ba14a194SAndy Lutomirski */ 3291a03d3f1SSebastian Andrzej Siewior tsk->stack_vm_area = vm; 330c08e6a12SAndrey Konovalov stack = kasan_reset_tag(stack); 3315eed6f1dSRik van Riel tsk->stack = stack; 3327865aba3SSebastian Andrzej Siewior return 0; 3335eed6f1dSRik van Riel } 334be9a2277SSebastian Andrzej Siewior 335be9a2277SSebastian Andrzej Siewior static void free_thread_stack(struct task_struct *tsk) 336be9a2277SSebastian Andrzej Siewior { 337e540bf31SSebastian Andrzej Siewior if (!try_release_thread_stack_to_cache(tsk->stack_vm_area)) 338e540bf31SSebastian Andrzej Siewior thread_stack_delayed_free(tsk); 339be9a2277SSebastian Andrzej Siewior 340be9a2277SSebastian Andrzej Siewior tsk->stack = NULL; 341be9a2277SSebastian Andrzej Siewior tsk->stack_vm_area = NULL; 342be9a2277SSebastian Andrzej Siewior } 343be9a2277SSebastian Andrzej Siewior 344be9a2277SSebastian Andrzej Siewior # else /* !CONFIG_VMAP_STACK */ 345be9a2277SSebastian Andrzej Siewior 346e540bf31SSebastian Andrzej Siewior static void thread_stack_free_rcu(struct rcu_head *rh) 347e540bf31SSebastian Andrzej Siewior { 348e540bf31SSebastian Andrzej Siewior __free_pages(virt_to_page(rh), THREAD_SIZE_ORDER); 349e540bf31SSebastian Andrzej Siewior } 350e540bf31SSebastian Andrzej Siewior 351e540bf31SSebastian Andrzej Siewior static void thread_stack_delayed_free(struct task_struct *tsk) 352e540bf31SSebastian Andrzej Siewior { 353e540bf31SSebastian Andrzej Siewior struct rcu_head *rh = tsk->stack; 354e540bf31SSebastian Andrzej Siewior 355e540bf31SSebastian Andrzej Siewior call_rcu(rh, thread_stack_free_rcu); 356e540bf31SSebastian Andrzej Siewior } 357e540bf31SSebastian Andrzej Siewior 3587865aba3SSebastian Andrzej Siewior static int alloc_thread_stack_node(struct task_struct *tsk, int node) 359be9a2277SSebastian Andrzej Siewior { 3604949148aSVladimir Davydov struct page *page = alloc_pages_node(node, THREADINFO_GFP, 3612889f608SThomas Gleixner THREAD_SIZE_ORDER); 362b6a84016SEric Dumazet 3631bf4580eSAndrea Arcangeli if (likely(page)) { 3648dcc1d34SAndrey Konovalov tsk->stack = kasan_reset_tag(page_address(page)); 3657865aba3SSebastian Andrzej Siewior return 0; 3661bf4580eSAndrea Arcangeli } 3677865aba3SSebastian Andrzej Siewior return -ENOMEM; 368b69c49b7SFUJITA Tomonori } 369b69c49b7SFUJITA Tomonori 370be9a2277SSebastian Andrzej Siewior static void free_thread_stack(struct task_struct *tsk) 371b69c49b7SFUJITA Tomonori { 372e540bf31SSebastian Andrzej Siewior thread_stack_delayed_free(tsk); 373be9a2277SSebastian Andrzej Siewior tsk->stack = NULL; 374ac496bf4SAndy Lutomirski } 375ac496bf4SAndy Lutomirski 376be9a2277SSebastian Andrzej Siewior # endif /* CONFIG_VMAP_STACK */ 377be9a2277SSebastian Andrzej Siewior # else /* !(THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)) */ 378ac496bf4SAndy Lutomirski 379b235beeaSLinus Torvalds static struct kmem_cache *thread_stack_cache; 3800d15d74aSThomas Gleixner 381e540bf31SSebastian Andrzej Siewior static void thread_stack_free_rcu(struct rcu_head *rh) 382e540bf31SSebastian Andrzej Siewior { 383e540bf31SSebastian Andrzej Siewior kmem_cache_free(thread_stack_cache, rh); 384e540bf31SSebastian Andrzej Siewior } 385e540bf31SSebastian Andrzej Siewior 386e540bf31SSebastian Andrzej Siewior static void thread_stack_delayed_free(struct task_struct *tsk) 387e540bf31SSebastian Andrzej Siewior { 388e540bf31SSebastian Andrzej Siewior struct rcu_head *rh = tsk->stack; 389e540bf31SSebastian Andrzej Siewior 390e540bf31SSebastian Andrzej Siewior call_rcu(rh, thread_stack_free_rcu); 391e540bf31SSebastian Andrzej Siewior } 392e540bf31SSebastian Andrzej Siewior 3937865aba3SSebastian Andrzej Siewior static int alloc_thread_stack_node(struct task_struct *tsk, int node) 3940d15d74aSThomas Gleixner { 3955eed6f1dSRik van Riel unsigned long *stack; 3965eed6f1dSRik van Riel stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 3978dcc1d34SAndrey Konovalov stack = kasan_reset_tag(stack); 3985eed6f1dSRik van Riel tsk->stack = stack; 3997865aba3SSebastian Andrzej Siewior return stack ? 0 : -ENOMEM; 4000d15d74aSThomas Gleixner } 4010d15d74aSThomas Gleixner 402ba14a194SAndy Lutomirski static void free_thread_stack(struct task_struct *tsk) 4030d15d74aSThomas Gleixner { 404e540bf31SSebastian Andrzej Siewior thread_stack_delayed_free(tsk); 405be9a2277SSebastian Andrzej Siewior tsk->stack = NULL; 4060d15d74aSThomas Gleixner } 4070d15d74aSThomas Gleixner 408b235beeaSLinus Torvalds void thread_stack_cache_init(void) 4090d15d74aSThomas Gleixner { 410f9d29946SDavid Windsor thread_stack_cache = kmem_cache_create_usercopy("thread_stack", 411f9d29946SDavid Windsor THREAD_SIZE, THREAD_SIZE, 0, 0, 412f9d29946SDavid Windsor THREAD_SIZE, NULL); 413b235beeaSLinus Torvalds BUG_ON(thread_stack_cache == NULL); 4140d15d74aSThomas Gleixner } 415be9a2277SSebastian Andrzej Siewior 416be9a2277SSebastian Andrzej Siewior # endif /* THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) */ 4172bb0529cSSebastian Andrzej Siewior #else /* CONFIG_ARCH_THREAD_STACK_ALLOCATOR */ 4182bb0529cSSebastian Andrzej Siewior 4197865aba3SSebastian Andrzej Siewior static int alloc_thread_stack_node(struct task_struct *tsk, int node) 4202bb0529cSSebastian Andrzej Siewior { 4212bb0529cSSebastian Andrzej Siewior unsigned long *stack; 4222bb0529cSSebastian Andrzej Siewior 4232bb0529cSSebastian Andrzej Siewior stack = arch_alloc_thread_stack_node(tsk, node); 4242bb0529cSSebastian Andrzej Siewior tsk->stack = stack; 4257865aba3SSebastian Andrzej Siewior return stack ? 0 : -ENOMEM; 4262bb0529cSSebastian Andrzej Siewior } 4272bb0529cSSebastian Andrzej Siewior 4282bb0529cSSebastian Andrzej Siewior static void free_thread_stack(struct task_struct *tsk) 4292bb0529cSSebastian Andrzej Siewior { 4302bb0529cSSebastian Andrzej Siewior arch_free_thread_stack(tsk); 4312bb0529cSSebastian Andrzej Siewior tsk->stack = NULL; 4322bb0529cSSebastian Andrzej Siewior } 4332bb0529cSSebastian Andrzej Siewior 434be9a2277SSebastian Andrzej Siewior #endif /* !CONFIG_ARCH_THREAD_STACK_ALLOCATOR */ 435b69c49b7SFUJITA Tomonori 4361da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 437e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 440e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 443e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 446e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 4493928d4f5SLinus Torvalds static struct kmem_cache *vm_area_cachep; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 452e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 4531da177e4SLinus Torvalds 454490fc053SLinus Torvalds struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) 4553928d4f5SLinus Torvalds { 456a670468fSAndrew Morton struct vm_area_struct *vma; 457490fc053SLinus Torvalds 458a670468fSAndrew Morton vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 459027232daSKirill A. Shutemov if (vma) 460027232daSKirill A. Shutemov vma_init(vma, mm); 461490fc053SLinus Torvalds return vma; 4623928d4f5SLinus Torvalds } 4633928d4f5SLinus Torvalds 4643928d4f5SLinus Torvalds struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) 4653928d4f5SLinus Torvalds { 46695faf699SLinus Torvalds struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 46795faf699SLinus Torvalds 46895faf699SLinus Torvalds if (new) { 469cda099b3SQian Cai ASSERT_EXCLUSIVE_WRITER(orig->vm_flags); 470cda099b3SQian Cai ASSERT_EXCLUSIVE_WRITER(orig->vm_file); 471cda099b3SQian Cai /* 472cda099b3SQian Cai * orig->shared.rb may be modified concurrently, but the clone 473cda099b3SQian Cai * will be reinitialized. 474cda099b3SQian Cai */ 475cda099b3SQian Cai *new = data_race(*orig); 47695faf699SLinus Torvalds INIT_LIST_HEAD(&new->anon_vma_chain); 4775c26f6acSSuren Baghdasaryan dup_anon_vma_name(orig, new); 47895faf699SLinus Torvalds } 47995faf699SLinus Torvalds return new; 4803928d4f5SLinus Torvalds } 4813928d4f5SLinus Torvalds 4823928d4f5SLinus Torvalds void vm_area_free(struct vm_area_struct *vma) 4833928d4f5SLinus Torvalds { 4845c26f6acSSuren Baghdasaryan free_anon_vma_name(vma); 4853928d4f5SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 4863928d4f5SLinus Torvalds } 4873928d4f5SLinus Torvalds 488ba14a194SAndy Lutomirski static void account_kernel_stack(struct task_struct *tsk, int account) 489c6a7f572SKOSAKI Motohiro { 4900ce055f8SSebastian Andrzej Siewior if (IS_ENABLED(CONFIG_VMAP_STACK)) { 491ba14a194SAndy Lutomirski struct vm_struct *vm = task_stack_vm_area(tsk); 49227faca83SMuchun Song int i; 493ba14a194SAndy Lutomirski 49427faca83SMuchun Song for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) 49527faca83SMuchun Song mod_lruvec_page_state(vm->pages[i], NR_KERNEL_STACK_KB, 49627faca83SMuchun Song account * (PAGE_SIZE / 1024)); 49727faca83SMuchun Song } else { 4980ce055f8SSebastian Andrzej Siewior void *stack = task_stack_page(tsk); 4990ce055f8SSebastian Andrzej Siewior 500991e7673SShakeel Butt /* All stack pages are in the same node. */ 501da3ceeffSMuchun Song mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB, 502991e7673SShakeel Butt account * (THREAD_SIZE / 1024)); 503ba14a194SAndy Lutomirski } 50427faca83SMuchun Song } 505c6a7f572SKOSAKI Motohiro 5061a03d3f1SSebastian Andrzej Siewior void exit_task_stack_account(struct task_struct *tsk) 5079b6f7e16SRoman Gushchin { 5081a03d3f1SSebastian Andrzej Siewior account_kernel_stack(tsk, -1); 5099b6f7e16SRoman Gushchin 5101a03d3f1SSebastian Andrzej Siewior if (IS_ENABLED(CONFIG_VMAP_STACK)) { 5111a03d3f1SSebastian Andrzej Siewior struct vm_struct *vm; 5129b6f7e16SRoman Gushchin int i; 5139b6f7e16SRoman Gushchin 5141a03d3f1SSebastian Andrzej Siewior vm = task_stack_vm_area(tsk); 5151a03d3f1SSebastian Andrzej Siewior for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) 5161a03d3f1SSebastian Andrzej Siewior memcg_kmem_uncharge_page(vm->pages[i], 0); 5179b6f7e16SRoman Gushchin } 5189b6f7e16SRoman Gushchin } 5199b6f7e16SRoman Gushchin 52068f24b08SAndy Lutomirski static void release_task_stack(struct task_struct *tsk) 5211da177e4SLinus Torvalds { 5222f064a59SPeter Zijlstra if (WARN_ON(READ_ONCE(tsk->__state) != TASK_DEAD)) 523405c0759SAndy Lutomirski return; /* Better to leak the stack than to free prematurely */ 524405c0759SAndy Lutomirski 525ba14a194SAndy Lutomirski free_thread_stack(tsk); 52668f24b08SAndy Lutomirski } 52768f24b08SAndy Lutomirski 52868f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 52968f24b08SAndy Lutomirski void put_task_stack(struct task_struct *tsk) 53068f24b08SAndy Lutomirski { 531f0b89d39SElena Reshetova if (refcount_dec_and_test(&tsk->stack_refcount)) 53268f24b08SAndy Lutomirski release_task_stack(tsk); 53368f24b08SAndy Lutomirski } 53468f24b08SAndy Lutomirski #endif 53568f24b08SAndy Lutomirski 53668f24b08SAndy Lutomirski void free_task(struct task_struct *tsk) 53768f24b08SAndy Lutomirski { 538b90ca8baSWill Deacon release_user_cpus_ptr(tsk); 539d08b9f0cSSami Tolvanen scs_release(tsk); 540d08b9f0cSSami Tolvanen 54168f24b08SAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK 54268f24b08SAndy Lutomirski /* 54368f24b08SAndy Lutomirski * The task is finally done with both the stack and thread_info, 54468f24b08SAndy Lutomirski * so free both. 54568f24b08SAndy Lutomirski */ 54668f24b08SAndy Lutomirski release_task_stack(tsk); 54768f24b08SAndy Lutomirski #else 54868f24b08SAndy Lutomirski /* 54968f24b08SAndy Lutomirski * If the task had a separate stack allocation, it should be gone 55068f24b08SAndy Lutomirski * by now. 55168f24b08SAndy Lutomirski */ 552f0b89d39SElena Reshetova WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0); 55368f24b08SAndy Lutomirski #endif 55423f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 555fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 556f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 5571da5c46fSOleg Nesterov if (tsk->flags & PF_KTHREAD) 5581da5c46fSOleg Nesterov free_kthread_struct(tsk); 5591da177e4SLinus Torvalds free_task_struct(tsk); 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 5621da177e4SLinus Torvalds 563fe69d560SDavid Hildenbrand static void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm) 564fe69d560SDavid Hildenbrand { 565fe69d560SDavid Hildenbrand struct file *exe_file; 566fe69d560SDavid Hildenbrand 567fe69d560SDavid Hildenbrand exe_file = get_mm_exe_file(oldmm); 568fe69d560SDavid Hildenbrand RCU_INIT_POINTER(mm->exe_file, exe_file); 569fe69d560SDavid Hildenbrand /* 570fe69d560SDavid Hildenbrand * We depend on the oldmm having properly denied write access to the 571fe69d560SDavid Hildenbrand * exe_file already. 572fe69d560SDavid Hildenbrand */ 573fe69d560SDavid Hildenbrand if (exe_file && deny_write_access(exe_file)) 574fe69d560SDavid Hildenbrand pr_warn_once("deny_write_access() failed in %s\n", __func__); 575fe69d560SDavid Hildenbrand } 576fe69d560SDavid Hildenbrand 5771da177e4SLinus Torvalds #ifdef CONFIG_MMU 5780766f788SEmese Revfy static __latent_entropy int dup_mmap(struct mm_struct *mm, 5790766f788SEmese Revfy struct mm_struct *oldmm) 5801da177e4SLinus Torvalds { 581*763ecb03SLiam R. Howlett struct vm_area_struct *mpnt, *tmp; 5821da177e4SLinus Torvalds int retval; 583c9dbe82cSLiam R. Howlett unsigned long charge = 0; 584893e26e6SPavel Emelyanov LIST_HEAD(uf); 585c9dbe82cSLiam R. Howlett MA_STATE(old_mas, &oldmm->mm_mt, 0, 0); 586d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 5871da177e4SLinus Torvalds 58832cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 589d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(oldmm)) { 5907c051267SMichal Hocko retval = -EINTR; 5917c051267SMichal Hocko goto fail_uprobe_end; 5927c051267SMichal Hocko } 593ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 594f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 595ad339451SIngo Molnar /* 596ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 597ad339451SIngo Molnar */ 598aaa2cc56SMichel Lespinasse mmap_write_lock_nested(mm, SINGLE_DEPTH_NESTING); 5997ee78232SHugh Dickins 60090f31d0eSKonstantin Khlebnikov /* No ordering required: file already has been exposed. */ 601fe69d560SDavid Hildenbrand dup_mm_exe_file(mm, oldmm); 60290f31d0eSKonstantin Khlebnikov 6034f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 60484638335SKonstantin Khlebnikov mm->data_vm = oldmm->data_vm; 6054f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 6064f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 6074f7d4614SVladimir Davydov 608f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 609f8af4da3SHugh Dickins if (retval) 610f8af4da3SHugh Dickins goto out; 611d2081b2bSYang Shi khugepaged_fork(mm, oldmm); 6121da177e4SLinus Torvalds 613d4af56c5SLiam R. Howlett retval = mas_expected_entries(&mas, oldmm->map_count); 614d4af56c5SLiam R. Howlett if (retval) 615d4af56c5SLiam R. Howlett goto out; 616d4af56c5SLiam R. Howlett 617c9dbe82cSLiam R. Howlett mas_for_each(&old_mas, mpnt, ULONG_MAX) { 6181da177e4SLinus Torvalds struct file *file; 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 62184638335SKonstantin Khlebnikov vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); 6221da177e4SLinus Torvalds continue; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds charge = 0; 625655c79bbSTetsuo Handa /* 626655c79bbSTetsuo Handa * Don't duplicate many vmas if we've been oom-killed (for 627655c79bbSTetsuo Handa * example) 628655c79bbSTetsuo Handa */ 629655c79bbSTetsuo Handa if (fatal_signal_pending(current)) { 630655c79bbSTetsuo Handa retval = -EINTR; 631d4af56c5SLiam R. Howlett goto loop_out; 632655c79bbSTetsuo Handa } 6331da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 634b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 635b2412b7fSHuang Shijie 636191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 6371da177e4SLinus Torvalds goto fail_nomem; 6381da177e4SLinus Torvalds charge = len; 6391da177e4SLinus Torvalds } 6403928d4f5SLinus Torvalds tmp = vm_area_dup(mpnt); 6411da177e4SLinus Torvalds if (!tmp) 6421da177e4SLinus Torvalds goto fail_nomem; 643ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 644ef0855d3SOleg Nesterov if (retval) 6451da177e4SLinus Torvalds goto fail_nomem_policy; 646a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 647893e26e6SPavel Emelyanov retval = dup_userfaultfd(tmp, &uf); 648893e26e6SPavel Emelyanov if (retval) 649893e26e6SPavel Emelyanov goto fail_nomem_anon_vma_fork; 650d2cd9edeSRik van Riel if (tmp->vm_flags & VM_WIPEONFORK) { 65193949bb2SLi Xinhai /* 65293949bb2SLi Xinhai * VM_WIPEONFORK gets a clean slate in the child. 65393949bb2SLi Xinhai * Don't prepare anon_vma until fault since we don't 65493949bb2SLi Xinhai * copy page for current vma. 65593949bb2SLi Xinhai */ 656d2cd9edeSRik van Riel tmp->anon_vma = NULL; 657d2cd9edeSRik van Riel } else if (anon_vma_fork(tmp, mpnt)) 6585beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 659893e26e6SPavel Emelyanov tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT); 6601da177e4SLinus Torvalds file = tmp->vm_file; 6611da177e4SLinus Torvalds if (file) { 662b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 663b88ed205SHugh Dickins 6641da177e4SLinus Torvalds get_file(file); 66583cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 666b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 667cf508b58SMiaohe Lin mapping_allow_writable(mapping); 668b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 669b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 6709826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 6719826a516SMichel Lespinasse &mapping->i_mmap); 672b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 67383cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 677a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 678a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 679a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 680a1e78772SMel Gorman */ 681a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 682a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 683a1e78772SMel Gorman 684d4af56c5SLiam R. Howlett /* Link the vma into the MT */ 685d4af56c5SLiam R. Howlett mas.index = tmp->vm_start; 686d4af56c5SLiam R. Howlett mas.last = tmp->vm_end - 1; 687d4af56c5SLiam R. Howlett mas_store(&mas, tmp); 688c9dbe82cSLiam R. Howlett if (mas_is_err(&mas)) 689c9dbe82cSLiam R. Howlett goto fail_nomem_mas_store; 690d4af56c5SLiam R. Howlett 6911da177e4SLinus Torvalds mm->map_count++; 692d2cd9edeSRik van Riel if (!(tmp->vm_flags & VM_WIPEONFORK)) 693c78f4636SPeter Xu retval = copy_page_range(tmp, mpnt); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 6961da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds if (retval) 699d4af56c5SLiam R. Howlett goto loop_out; 7001da177e4SLinus Torvalds } 701d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 7021ed0cc5aSNadav Amit retval = arch_dup_mmap(oldmm, mm); 703d4af56c5SLiam R. Howlett loop_out: 704d4af56c5SLiam R. Howlett mas_destroy(&mas); 7051da177e4SLinus Torvalds out: 706d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 707fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 708d8ed45c5SMichel Lespinasse mmap_write_unlock(oldmm); 709893e26e6SPavel Emelyanov dup_userfaultfd_complete(&uf); 7107c051267SMichal Hocko fail_uprobe_end: 71132cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 7121da177e4SLinus Torvalds return retval; 713c9dbe82cSLiam R. Howlett 714c9dbe82cSLiam R. Howlett fail_nomem_mas_store: 715c9dbe82cSLiam R. Howlett unlink_anon_vmas(tmp); 7165beb4930SRik van Riel fail_nomem_anon_vma_fork: 717ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 7181da177e4SLinus Torvalds fail_nomem_policy: 7193928d4f5SLinus Torvalds vm_area_free(tmp); 7201da177e4SLinus Torvalds fail_nomem: 7211da177e4SLinus Torvalds retval = -ENOMEM; 7221da177e4SLinus Torvalds vm_unacct_memory(charge); 723d4af56c5SLiam R. Howlett goto loop_out; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 7271da177e4SLinus Torvalds { 7281da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 7291da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 7301da177e4SLinus Torvalds return -ENOMEM; 7311da177e4SLinus Torvalds return 0; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 7351da177e4SLinus Torvalds { 7365e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds #else 73990f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 74090f31d0eSKonstantin Khlebnikov { 741d8ed45c5SMichel Lespinasse mmap_write_lock(oldmm); 742fe69d560SDavid Hildenbrand dup_mm_exe_file(mm, oldmm); 743d8ed45c5SMichel Lespinasse mmap_write_unlock(oldmm); 74490f31d0eSKonstantin Khlebnikov return 0; 74590f31d0eSKonstantin Khlebnikov } 7461da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 7471da177e4SLinus Torvalds #define mm_free_pgd(mm) 7481da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 7491da177e4SLinus Torvalds 750d70f2a14SAndrew Morton static void check_mm(struct mm_struct *mm) 751d70f2a14SAndrew Morton { 752d70f2a14SAndrew Morton int i; 753d70f2a14SAndrew Morton 7548495f7e6SSai Praneeth Prakhya BUILD_BUG_ON_MSG(ARRAY_SIZE(resident_page_types) != NR_MM_COUNTERS, 7558495f7e6SSai Praneeth Prakhya "Please make sure 'struct resident_page_types[]' is updated as well"); 7568495f7e6SSai Praneeth Prakhya 757d70f2a14SAndrew Morton for (i = 0; i < NR_MM_COUNTERS; i++) { 758d70f2a14SAndrew Morton long x = atomic_long_read(&mm->rss_stat.count[i]); 759d70f2a14SAndrew Morton 760d70f2a14SAndrew Morton if (unlikely(x)) 7618495f7e6SSai Praneeth Prakhya pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n", 7628495f7e6SSai Praneeth Prakhya mm, resident_page_types[i], x); 763d70f2a14SAndrew Morton } 764d70f2a14SAndrew Morton 765d70f2a14SAndrew Morton if (mm_pgtables_bytes(mm)) 766d70f2a14SAndrew Morton pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", 767d70f2a14SAndrew Morton mm_pgtables_bytes(mm)); 768d70f2a14SAndrew Morton 769d70f2a14SAndrew Morton #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 770d70f2a14SAndrew Morton VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 771d70f2a14SAndrew Morton #endif 772d70f2a14SAndrew Morton } 7731da177e4SLinus Torvalds 774e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 7751da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 7761da177e4SLinus Torvalds 777d70f2a14SAndrew Morton /* 778d70f2a14SAndrew Morton * Called when the last reference to the mm 779d70f2a14SAndrew Morton * is dropped: either by a lazy thread or by 780d70f2a14SAndrew Morton * mmput. Free the page directory and the mm. 781d70f2a14SAndrew Morton */ 782d34bc48fSAndrew Morton void __mmdrop(struct mm_struct *mm) 783d70f2a14SAndrew Morton { 784d70f2a14SAndrew Morton BUG_ON(mm == &init_mm); 7853eda69c9SMark Rutland WARN_ON_ONCE(mm == current->mm); 7863eda69c9SMark Rutland WARN_ON_ONCE(mm == current->active_mm); 787d70f2a14SAndrew Morton mm_free_pgd(mm); 788d70f2a14SAndrew Morton destroy_context(mm); 789984cfe4eSJason Gunthorpe mmu_notifier_subscriptions_destroy(mm); 790d70f2a14SAndrew Morton check_mm(mm); 791d70f2a14SAndrew Morton put_user_ns(mm->user_ns); 7922667ed10SFenghua Yu mm_pasid_drop(mm); 793d70f2a14SAndrew Morton free_mm(mm); 794d70f2a14SAndrew Morton } 795d34bc48fSAndrew Morton EXPORT_SYMBOL_GPL(__mmdrop); 796d70f2a14SAndrew Morton 797d70f2a14SAndrew Morton static void mmdrop_async_fn(struct work_struct *work) 798d70f2a14SAndrew Morton { 799d70f2a14SAndrew Morton struct mm_struct *mm; 800d70f2a14SAndrew Morton 801d70f2a14SAndrew Morton mm = container_of(work, struct mm_struct, async_put_work); 802d70f2a14SAndrew Morton __mmdrop(mm); 803d70f2a14SAndrew Morton } 804d70f2a14SAndrew Morton 805d70f2a14SAndrew Morton static void mmdrop_async(struct mm_struct *mm) 806d70f2a14SAndrew Morton { 807d70f2a14SAndrew Morton if (unlikely(atomic_dec_and_test(&mm->mm_count))) { 808d70f2a14SAndrew Morton INIT_WORK(&mm->async_put_work, mmdrop_async_fn); 809d70f2a14SAndrew Morton schedule_work(&mm->async_put_work); 810d70f2a14SAndrew Morton } 811d70f2a14SAndrew Morton } 812d70f2a14SAndrew Morton 8131da177e4SLinus Torvalds static inline void free_signal_struct(struct signal_struct *sig) 8141da177e4SLinus Torvalds { 8151da177e4SLinus Torvalds taskstats_tgid_free(sig); 8161da177e4SLinus Torvalds sched_autogroup_exit(sig); 8171da177e4SLinus Torvalds /* 8181da177e4SLinus Torvalds * __mmdrop is not safe to call from softirq context on x86 due to 8191da177e4SLinus Torvalds * pgd_dtor so postpone it to the async context 8201da177e4SLinus Torvalds */ 8211da177e4SLinus Torvalds if (sig->oom_mm) 8221da177e4SLinus Torvalds mmdrop_async(sig->oom_mm); 8231da177e4SLinus Torvalds kmem_cache_free(signal_cachep, sig); 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds static inline void put_signal_struct(struct signal_struct *sig) 8271da177e4SLinus Torvalds { 82860d4de3fSElena Reshetova if (refcount_dec_and_test(&sig->sigcnt)) 8291da177e4SLinus Torvalds free_signal_struct(sig); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds void __put_task_struct(struct task_struct *tsk) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds WARN_ON(!tsk->exit_state); 835ec1d2819SElena Reshetova WARN_ON(refcount_read(&tsk->usage)); 8361da177e4SLinus Torvalds WARN_ON(tsk == current); 8371da177e4SLinus Torvalds 8380f212204SJens Axboe io_uring_free(tsk); 8391da177e4SLinus Torvalds cgroup_free(tsk); 84016d51a59SJann Horn task_numa_free(tsk, true); 8411da177e4SLinus Torvalds security_task_free(tsk); 842a10787e6SSong Liu bpf_task_storage_free(tsk); 8431da177e4SLinus Torvalds exit_creds(tsk); 8441da177e4SLinus Torvalds delayacct_tsk_free(tsk); 8451da177e4SLinus Torvalds put_signal_struct(tsk->signal); 8466e33cad0SPeter Zijlstra sched_core_free(tsk); 8471da177e4SLinus Torvalds free_task(tsk); 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__put_task_struct); 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds void __init __weak arch_task_cache_init(void) { } 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds /* 8541da177e4SLinus Torvalds * set_max_threads 8551da177e4SLinus Torvalds */ 8561da177e4SLinus Torvalds static void set_max_threads(unsigned int max_threads_suggested) 8571da177e4SLinus Torvalds { 8581da177e4SLinus Torvalds u64 threads; 859ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds /* 8621da177e4SLinus Torvalds * The number of threads shall be limited such that the thread 8631da177e4SLinus Torvalds * structures may only consume a small part of the available memory. 8641da177e4SLinus Torvalds */ 8653d6357deSArun KS if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64) 8661da177e4SLinus Torvalds threads = MAX_THREADS; 8671da177e4SLinus Torvalds else 8683d6357deSArun KS threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE, 8691da177e4SLinus Torvalds (u64) THREAD_SIZE * 8UL); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds if (threads > max_threads_suggested) 8721da177e4SLinus Torvalds threads = max_threads_suggested; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 8781da177e4SLinus Torvalds /* Initialized by the architecture: */ 8791da177e4SLinus Torvalds int arch_task_struct_size __read_mostly; 8801da177e4SLinus Torvalds #endif 8811da177e4SLinus Torvalds 8824189ff23SChristoph Hellwig #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 8835905429aSKees Cook static void task_struct_whitelist(unsigned long *offset, unsigned long *size) 8845905429aSKees Cook { 8855905429aSKees Cook /* Fetch thread_struct whitelist for the architecture. */ 8865905429aSKees Cook arch_thread_struct_whitelist(offset, size); 8875905429aSKees Cook 8885905429aSKees Cook /* 8895905429aSKees Cook * Handle zero-sized whitelist or empty thread_struct, otherwise 8905905429aSKees Cook * adjust offset to position of thread_struct in task_struct. 8915905429aSKees Cook */ 8925905429aSKees Cook if (unlikely(*size == 0)) 8935905429aSKees Cook *offset = 0; 8945905429aSKees Cook else 8955905429aSKees Cook *offset += offsetof(struct task_struct, thread); 8965905429aSKees Cook } 8974189ff23SChristoph Hellwig #endif /* CONFIG_ARCH_TASK_STRUCT_ALLOCATOR */ 8985905429aSKees Cook 8991da177e4SLinus Torvalds void __init fork_init(void) 9001da177e4SLinus Torvalds { 9011da177e4SLinus Torvalds int i; 9021da177e4SLinus Torvalds #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 9031da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 9041da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN 0 9051da177e4SLinus Torvalds #endif 9061da177e4SLinus Torvalds int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); 9075905429aSKees Cook unsigned long useroffset, usersize; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 9105905429aSKees Cook task_struct_whitelist(&useroffset, &usersize); 9115905429aSKees Cook task_struct_cachep = kmem_cache_create_usercopy("task_struct", 9121da177e4SLinus Torvalds arch_task_struct_size, align, 9135905429aSKees Cook SLAB_PANIC|SLAB_ACCOUNT, 9145905429aSKees Cook useroffset, usersize, NULL); 9151da177e4SLinus Torvalds #endif 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds /* do the arch specific task caches init */ 9181da177e4SLinus Torvalds arch_task_cache_init(); 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds set_max_threads(MAX_THREADS); 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 9231da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 9241da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 9251da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 9261da177e4SLinus Torvalds 92721d1c5e3SAlexey Gladkov for (i = 0; i < MAX_PER_NAMESPACE_UCOUNTS; i++) 9281da177e4SLinus Torvalds init_user_ns.ucount_max[i] = max_threads/2; 9291da177e4SLinus Torvalds 9305ddf994fSEric W. Biederman set_rlimit_ucount_max(&init_user_ns, UCOUNT_RLIMIT_NPROC, RLIM_INFINITY); 9315ddf994fSEric W. Biederman set_rlimit_ucount_max(&init_user_ns, UCOUNT_RLIMIT_MSGQUEUE, RLIM_INFINITY); 9325ddf994fSEric W. Biederman set_rlimit_ucount_max(&init_user_ns, UCOUNT_RLIMIT_SIGPENDING, RLIM_INFINITY); 9335ddf994fSEric W. Biederman set_rlimit_ucount_max(&init_user_ns, UCOUNT_RLIMIT_MEMLOCK, RLIM_INFINITY); 93421d1c5e3SAlexey Gladkov 9351da177e4SLinus Torvalds #ifdef CONFIG_VMAP_STACK 9361da177e4SLinus Torvalds cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", 9371da177e4SLinus Torvalds NULL, free_vm_stack_cache); 9381da177e4SLinus Torvalds #endif 9391da177e4SLinus Torvalds 940d08b9f0cSSami Tolvanen scs_init(); 941d08b9f0cSSami Tolvanen 9421da177e4SLinus Torvalds lockdep_init_task(&init_task); 943aad42dd4SNadav Amit uprobes_init(); 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds int __weak arch_dup_task_struct(struct task_struct *dst, 9471da177e4SLinus Torvalds struct task_struct *src) 9481da177e4SLinus Torvalds { 9491da177e4SLinus Torvalds *dst = *src; 9501da177e4SLinus Torvalds return 0; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds void set_task_stack_end_magic(struct task_struct *tsk) 9541da177e4SLinus Torvalds { 9551da177e4SLinus Torvalds unsigned long *stackend; 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds stackend = end_of_stack(tsk); 9581da177e4SLinus Torvalds *stackend = STACK_END_MAGIC; /* for overflow detection */ 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 9621da177e4SLinus Torvalds { 9631da177e4SLinus Torvalds struct task_struct *tsk; 9641da177e4SLinus Torvalds int err; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds if (node == NUMA_NO_NODE) 9671da177e4SLinus Torvalds node = tsk_fork_get_node(orig); 9681da177e4SLinus Torvalds tsk = alloc_task_struct_node(node); 9691da177e4SLinus Torvalds if (!tsk) 9701da177e4SLinus Torvalds return NULL; 9711da177e4SLinus Torvalds 972546c42b2SSebastian Andrzej Siewior err = arch_dup_task_struct(tsk, orig); 973546c42b2SSebastian Andrzej Siewior if (err) 9741da177e4SLinus Torvalds goto free_tsk; 9751da177e4SLinus Torvalds 9767865aba3SSebastian Andrzej Siewior err = alloc_thread_stack_node(tsk, node); 9777865aba3SSebastian Andrzej Siewior if (err) 9781da177e4SLinus Torvalds goto free_tsk; 9799b6f7e16SRoman Gushchin 9801da177e4SLinus Torvalds #ifdef CONFIG_THREAD_INFO_IN_TASK 981f0b89d39SElena Reshetova refcount_set(&tsk->stack_refcount, 1); 9821da177e4SLinus Torvalds #endif 9831a03d3f1SSebastian Andrzej Siewior account_kernel_stack(tsk, 1); 9841da177e4SLinus Torvalds 985d08b9f0cSSami Tolvanen err = scs_prepare(tsk, node); 986d08b9f0cSSami Tolvanen if (err) 987d08b9f0cSSami Tolvanen goto free_stack; 988d08b9f0cSSami Tolvanen 9891da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 9901da177e4SLinus Torvalds /* 9911da177e4SLinus Torvalds * We must handle setting up seccomp filters once we're under 9921da177e4SLinus Torvalds * the sighand lock in case orig has changed between now and 9931da177e4SLinus Torvalds * then. Until then, filter must be NULL to avoid messing up 9941da177e4SLinus Torvalds * the usage counts on the error path calling free_task. 9951da177e4SLinus Torvalds */ 9961da177e4SLinus Torvalds tsk->seccomp.filter = NULL; 9971da177e4SLinus Torvalds #endif 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds setup_thread_stack(tsk, orig); 10001da177e4SLinus Torvalds clear_user_return_notifier(tsk); 10011da177e4SLinus Torvalds clear_tsk_need_resched(tsk); 10021da177e4SLinus Torvalds set_task_stack_end_magic(tsk); 10031446e1dfSGabriel Krisman Bertazi clear_syscall_work_syscall_user_dispatch(tsk); 10041da177e4SLinus Torvalds 1005050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR 10061da177e4SLinus Torvalds tsk->stack_canary = get_random_canary(); 10071da177e4SLinus Torvalds #endif 10083bd37062SSebastian Andrzej Siewior if (orig->cpus_ptr == &orig->cpus_mask) 10093bd37062SSebastian Andrzej Siewior tsk->cpus_ptr = &tsk->cpus_mask; 1010b90ca8baSWill Deacon dup_user_cpus_ptr(tsk, orig, node); 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds /* 10130ff7b2cfSEric W. Biederman * One for the user space visible state that goes away when reaped. 10140ff7b2cfSEric W. Biederman * One for the scheduler. 10151da177e4SLinus Torvalds */ 10160ff7b2cfSEric W. Biederman refcount_set(&tsk->rcu_users, 2); 10170ff7b2cfSEric W. Biederman /* One for the rcu users */ 10180ff7b2cfSEric W. Biederman refcount_set(&tsk->usage, 1); 10191da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_IO_TRACE 10201da177e4SLinus Torvalds tsk->btrace_seq = 0; 10211da177e4SLinus Torvalds #endif 10221da177e4SLinus Torvalds tsk->splice_pipe = NULL; 10231da177e4SLinus Torvalds tsk->task_frag.page = NULL; 10241da177e4SLinus Torvalds tsk->wake_q.next = NULL; 1025e32cf5dfSEric W. Biederman tsk->worker_private = NULL; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds kcov_task_init(tsk); 10285fbda3ecSThomas Gleixner kmap_local_fork(tsk); 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds #ifdef CONFIG_FAULT_INJECTION 10311da177e4SLinus Torvalds tsk->fail_nth = 0; 10321da177e4SLinus Torvalds #endif 10331da177e4SLinus Torvalds 10342c323017SJosef Bacik #ifdef CONFIG_BLK_CGROUP 10352c323017SJosef Bacik tsk->throttle_queue = NULL; 10362c323017SJosef Bacik tsk->use_memdelay = 0; 10372c323017SJosef Bacik #endif 10382c323017SJosef Bacik 1039a3d29e82SPeter Zijlstra #ifdef CONFIG_IOMMU_SVA 1040a3d29e82SPeter Zijlstra tsk->pasid_activated = 0; 1041a3d29e82SPeter Zijlstra #endif 1042a3d29e82SPeter Zijlstra 1043d46eb14bSShakeel Butt #ifdef CONFIG_MEMCG 1044d46eb14bSShakeel Butt tsk->active_memcg = NULL; 1045d46eb14bSShakeel Butt #endif 1046b041b525STony Luck 1047b041b525STony Luck #ifdef CONFIG_CPU_SUP_INTEL 1048b041b525STony Luck tsk->reported_split_lock = 0; 1049b041b525STony Luck #endif 1050b041b525STony Luck 10511da177e4SLinus Torvalds return tsk; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds free_stack: 10541a03d3f1SSebastian Andrzej Siewior exit_task_stack_account(tsk); 10551da177e4SLinus Torvalds free_thread_stack(tsk); 10561da177e4SLinus Torvalds free_tsk: 10571da177e4SLinus Torvalds free_task_struct(tsk); 10581da177e4SLinus Torvalds return NULL; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 10621da177e4SLinus Torvalds 10634cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 10644cb0e11bSHidehiro Kawai 10654cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 10664cb0e11bSHidehiro Kawai { 10674cb0e11bSHidehiro Kawai default_dump_filter = 10684cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 10694cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 10704cb0e11bSHidehiro Kawai return 1; 10714cb0e11bSHidehiro Kawai } 10724cb0e11bSHidehiro Kawai 10734cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 10744cb0e11bSHidehiro Kawai 10751da177e4SLinus Torvalds #include <linux/init_task.h> 10761da177e4SLinus Torvalds 1077858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 1078858f0993SAlexey Dobriyan { 1079858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 1080858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 1081db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 1082858f0993SAlexey Dobriyan #endif 1083858f0993SAlexey Dobriyan } 1084858f0993SAlexey Dobriyan 1085c3f3ce04SAndrea Arcangeli static __always_inline void mm_clear_owner(struct mm_struct *mm, 1086c3f3ce04SAndrea Arcangeli struct task_struct *p) 1087c3f3ce04SAndrea Arcangeli { 1088c3f3ce04SAndrea Arcangeli #ifdef CONFIG_MEMCG 1089c3f3ce04SAndrea Arcangeli if (mm->owner == p) 1090c3f3ce04SAndrea Arcangeli WRITE_ONCE(mm->owner, NULL); 1091c3f3ce04SAndrea Arcangeli #endif 1092c3f3ce04SAndrea Arcangeli } 1093c3f3ce04SAndrea Arcangeli 109433144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 109533144e84SVladimir Davydov { 109633144e84SVladimir Davydov #ifdef CONFIG_MEMCG 109733144e84SVladimir Davydov mm->owner = p; 109833144e84SVladimir Davydov #endif 109933144e84SVladimir Davydov } 110033144e84SVladimir Davydov 1101355627f5SEric Biggers static void mm_init_uprobes_state(struct mm_struct *mm) 1102355627f5SEric Biggers { 1103355627f5SEric Biggers #ifdef CONFIG_UPROBES 1104355627f5SEric Biggers mm->uprobes_state.xol_area = NULL; 1105355627f5SEric Biggers #endif 1106355627f5SEric Biggers } 1107355627f5SEric Biggers 1108bfedb589SEric W. Biederman static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, 1109bfedb589SEric W. Biederman struct user_namespace *user_ns) 11101da177e4SLinus Torvalds { 1111d4af56c5SLiam R. Howlett mt_init_flags(&mm->mm_mt, MM_MT_FLAGS); 1112d4af56c5SLiam R. Howlett mt_set_external_lock(&mm->mm_mt, &mm->mmap_lock); 11131da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 11141da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 111557efa1feSJason Gunthorpe seqcount_init(&mm->write_protect_seq); 1116d8ed45c5SMichel Lespinasse mmap_init_lock(mm); 11171da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 1118af5b0f6aSKirill A. Shutemov mm_pgtables_bytes_init(mm); 111941f727fdSVladimir Davydov mm->map_count = 0; 112041f727fdSVladimir Davydov mm->locked_vm = 0; 112170f8a3caSDavidlohr Bueso atomic64_set(&mm->pinned_vm, 0); 1122d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 11231da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 112488aa7cc6SYang Shi spin_lock_init(&mm->arg_lock); 112541f727fdSVladimir Davydov mm_init_cpumask(mm); 1126858f0993SAlexey Dobriyan mm_init_aio(mm); 1127cf475ad2SBalbir Singh mm_init_owner(mm, p); 1128a6cbd440SFenghua Yu mm_pasid_init(mm); 11292b7e8665SEric Biggers RCU_INIT_POINTER(mm->exe_file, NULL); 1130984cfe4eSJason Gunthorpe mmu_notifier_subscriptions_init(mm); 113116af97dcSNadav Amit init_tlb_flush_pending(mm); 113241f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 113341f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 113441f727fdSVladimir Davydov #endif 1135355627f5SEric Biggers mm_init_uprobes_state(mm); 113613db8c50SLiu Zixian hugetlb_count_init(mm); 11371da177e4SLinus Torvalds 1138a0715cc2SAlex Thorlton if (current->mm) { 1139a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 1140a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 1141a0715cc2SAlex Thorlton } else { 1142a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 11431da177e4SLinus Torvalds mm->def_flags = 0; 1144a0715cc2SAlex Thorlton } 1145a0715cc2SAlex Thorlton 114641f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 114741f727fdSVladimir Davydov goto fail_nopgd; 114878fb7466SPavel Emelianov 114941f727fdSVladimir Davydov if (init_new_context(p, mm)) 115041f727fdSVladimir Davydov goto fail_nocontext; 115141f727fdSVladimir Davydov 1152bfedb589SEric W. Biederman mm->user_ns = get_user_ns(user_ns); 1153bd74fdaeSYu Zhao lru_gen_init_mm(mm); 115441f727fdSVladimir Davydov return mm; 115541f727fdSVladimir Davydov 115641f727fdSVladimir Davydov fail_nocontext: 115741f727fdSVladimir Davydov mm_free_pgd(mm); 115841f727fdSVladimir Davydov fail_nopgd: 11591da177e4SLinus Torvalds free_mm(mm); 11601da177e4SLinus Torvalds return NULL; 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds /* 11641da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 11651da177e4SLinus Torvalds */ 11661da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 11671da177e4SLinus Torvalds { 11681da177e4SLinus Torvalds struct mm_struct *mm; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds mm = allocate_mm(); 1171de03c72cSKOSAKI Motohiro if (!mm) 1172de03c72cSKOSAKI Motohiro return NULL; 1173de03c72cSKOSAKI Motohiro 11741da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 1175bfedb589SEric W. Biederman return mm_init(mm, current, current_user_ns()); 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 1178ec8d7c14SMichal Hocko static inline void __mmput(struct mm_struct *mm) 11791da177e4SLinus Torvalds { 1180ec8d7c14SMichal Hocko VM_BUG_ON(atomic_read(&mm->mm_users)); 11810ae26f1bSAndrew Morton 1182d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 11831da177e4SLinus Torvalds exit_aio(mm); 11841c2fb7a4SAndrea Arcangeli ksm_exit(mm); 1185ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 11861da177e4SLinus Torvalds exit_mmap(mm); 11876fcb52a5SAaron Lu mm_put_huge_zero_page(mm); 1188925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 11891da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 11901da177e4SLinus Torvalds spin_lock(&mmlist_lock); 11911da177e4SLinus Torvalds list_del(&mm->mmlist); 11921da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 11931da177e4SLinus Torvalds } 1194801460d0SHiroshi Shimamoto if (mm->binfmt) 1195801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 1196bd74fdaeSYu Zhao lru_gen_del_mm(mm); 11971da177e4SLinus Torvalds mmdrop(mm); 11981da177e4SLinus Torvalds } 1199ec8d7c14SMichal Hocko 1200ec8d7c14SMichal Hocko /* 1201ec8d7c14SMichal Hocko * Decrement the use count and release all resources for an mm. 1202ec8d7c14SMichal Hocko */ 1203ec8d7c14SMichal Hocko void mmput(struct mm_struct *mm) 1204ec8d7c14SMichal Hocko { 1205ec8d7c14SMichal Hocko might_sleep(); 1206ec8d7c14SMichal Hocko 1207ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) 1208ec8d7c14SMichal Hocko __mmput(mm); 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 12111da177e4SLinus Torvalds 1212a1b2289cSSherry Yang #ifdef CONFIG_MMU 1213a1b2289cSSherry Yang static void mmput_async_fn(struct work_struct *work) 1214a1b2289cSSherry Yang { 1215a1b2289cSSherry Yang struct mm_struct *mm = container_of(work, struct mm_struct, 1216a1b2289cSSherry Yang async_put_work); 1217a1b2289cSSherry Yang 1218a1b2289cSSherry Yang __mmput(mm); 1219a1b2289cSSherry Yang } 1220a1b2289cSSherry Yang 1221a1b2289cSSherry Yang void mmput_async(struct mm_struct *mm) 1222a1b2289cSSherry Yang { 1223a1b2289cSSherry Yang if (atomic_dec_and_test(&mm->mm_users)) { 1224a1b2289cSSherry Yang INIT_WORK(&mm->async_put_work, mmput_async_fn); 1225a1b2289cSSherry Yang schedule_work(&mm->async_put_work); 1226a1b2289cSSherry Yang } 1227a1b2289cSSherry Yang } 1228a1b2289cSSherry Yang #endif 1229a1b2289cSSherry Yang 123090f31d0eSKonstantin Khlebnikov /** 123190f31d0eSKonstantin Khlebnikov * set_mm_exe_file - change a reference to the mm's executable file 123290f31d0eSKonstantin Khlebnikov * 123390f31d0eSKonstantin Khlebnikov * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 123490f31d0eSKonstantin Khlebnikov * 12356e399cd1SDavidlohr Bueso * Main users are mmput() and sys_execve(). Callers prevent concurrent 12366e399cd1SDavidlohr Bueso * invocations: in mmput() nobody alive left, in execve task is single 123735d7bdc8SDavid Hildenbrand * threaded. 1238fe69d560SDavid Hildenbrand * 1239fe69d560SDavid Hildenbrand * Can only fail if new_exe_file != NULL. 124090f31d0eSKonstantin Khlebnikov */ 1241fe69d560SDavid Hildenbrand int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 124238646013SJiri Slaby { 12436e399cd1SDavidlohr Bueso struct file *old_exe_file; 12446e399cd1SDavidlohr Bueso 12456e399cd1SDavidlohr Bueso /* 12466e399cd1SDavidlohr Bueso * It is safe to dereference the exe_file without RCU as 12476e399cd1SDavidlohr Bueso * this function is only called if nobody else can access 12486e399cd1SDavidlohr Bueso * this mm -- see comment above for justification. 12496e399cd1SDavidlohr Bueso */ 12506e399cd1SDavidlohr Bueso old_exe_file = rcu_dereference_raw(mm->exe_file); 125190f31d0eSKonstantin Khlebnikov 1252fe69d560SDavid Hildenbrand if (new_exe_file) { 1253fe69d560SDavid Hildenbrand /* 1254fe69d560SDavid Hildenbrand * We expect the caller (i.e., sys_execve) to already denied 1255fe69d560SDavid Hildenbrand * write access, so this is unlikely to fail. 1256fe69d560SDavid Hildenbrand */ 1257fe69d560SDavid Hildenbrand if (unlikely(deny_write_access(new_exe_file))) 1258fe69d560SDavid Hildenbrand return -EACCES; 125938646013SJiri Slaby get_file(new_exe_file); 1260fe69d560SDavid Hildenbrand } 126190f31d0eSKonstantin Khlebnikov rcu_assign_pointer(mm->exe_file, new_exe_file); 1262fe69d560SDavid Hildenbrand if (old_exe_file) { 1263fe69d560SDavid Hildenbrand allow_write_access(old_exe_file); 126490f31d0eSKonstantin Khlebnikov fput(old_exe_file); 126538646013SJiri Slaby } 1266fe69d560SDavid Hildenbrand return 0; 1267fe69d560SDavid Hildenbrand } 126838646013SJiri Slaby 126990f31d0eSKonstantin Khlebnikov /** 127035d7bdc8SDavid Hildenbrand * replace_mm_exe_file - replace a reference to the mm's executable file 127135d7bdc8SDavid Hildenbrand * 127235d7bdc8SDavid Hildenbrand * This changes mm's executable file (shown as symlink /proc/[pid]/exe), 127335d7bdc8SDavid Hildenbrand * dealing with concurrent invocation and without grabbing the mmap lock in 127435d7bdc8SDavid Hildenbrand * write mode. 127535d7bdc8SDavid Hildenbrand * 127635d7bdc8SDavid Hildenbrand * Main user is sys_prctl(PR_SET_MM_MAP/EXE_FILE). 127735d7bdc8SDavid Hildenbrand */ 127835d7bdc8SDavid Hildenbrand int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 127935d7bdc8SDavid Hildenbrand { 128035d7bdc8SDavid Hildenbrand struct vm_area_struct *vma; 128135d7bdc8SDavid Hildenbrand struct file *old_exe_file; 128235d7bdc8SDavid Hildenbrand int ret = 0; 128335d7bdc8SDavid Hildenbrand 128435d7bdc8SDavid Hildenbrand /* Forbid mm->exe_file change if old file still mapped. */ 128535d7bdc8SDavid Hildenbrand old_exe_file = get_mm_exe_file(mm); 128635d7bdc8SDavid Hildenbrand if (old_exe_file) { 1287fa5e5876SMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, 0); 128835d7bdc8SDavid Hildenbrand mmap_read_lock(mm); 1289fa5e5876SMatthew Wilcox (Oracle) for_each_vma(vmi, vma) { 129035d7bdc8SDavid Hildenbrand if (!vma->vm_file) 129135d7bdc8SDavid Hildenbrand continue; 129235d7bdc8SDavid Hildenbrand if (path_equal(&vma->vm_file->f_path, 1293fa5e5876SMatthew Wilcox (Oracle) &old_exe_file->f_path)) { 129435d7bdc8SDavid Hildenbrand ret = -EBUSY; 1295fa5e5876SMatthew Wilcox (Oracle) break; 1296fa5e5876SMatthew Wilcox (Oracle) } 129735d7bdc8SDavid Hildenbrand } 129835d7bdc8SDavid Hildenbrand mmap_read_unlock(mm); 129935d7bdc8SDavid Hildenbrand fput(old_exe_file); 130035d7bdc8SDavid Hildenbrand if (ret) 130135d7bdc8SDavid Hildenbrand return ret; 130235d7bdc8SDavid Hildenbrand } 130335d7bdc8SDavid Hildenbrand 130435d7bdc8SDavid Hildenbrand /* set the new file, lockless */ 1305fe69d560SDavid Hildenbrand ret = deny_write_access(new_exe_file); 1306fe69d560SDavid Hildenbrand if (ret) 1307fe69d560SDavid Hildenbrand return -EACCES; 130835d7bdc8SDavid Hildenbrand get_file(new_exe_file); 1309fe69d560SDavid Hildenbrand 131035d7bdc8SDavid Hildenbrand old_exe_file = xchg(&mm->exe_file, new_exe_file); 1311fe69d560SDavid Hildenbrand if (old_exe_file) { 1312fe69d560SDavid Hildenbrand /* 1313fe69d560SDavid Hildenbrand * Don't race with dup_mmap() getting the file and disallowing 1314fe69d560SDavid Hildenbrand * write access while someone might open the file writable. 1315fe69d560SDavid Hildenbrand */ 1316fe69d560SDavid Hildenbrand mmap_read_lock(mm); 1317fe69d560SDavid Hildenbrand allow_write_access(old_exe_file); 131835d7bdc8SDavid Hildenbrand fput(old_exe_file); 1319fe69d560SDavid Hildenbrand mmap_read_unlock(mm); 1320fe69d560SDavid Hildenbrand } 132135d7bdc8SDavid Hildenbrand return 0; 132235d7bdc8SDavid Hildenbrand } 132338646013SJiri Slaby 132490f31d0eSKonstantin Khlebnikov /** 132590f31d0eSKonstantin Khlebnikov * get_mm_exe_file - acquire a reference to the mm's executable file 132690f31d0eSKonstantin Khlebnikov * 132790f31d0eSKonstantin Khlebnikov * Returns %NULL if mm has no associated executable file. 132890f31d0eSKonstantin Khlebnikov * User must release file via fput(). 132990f31d0eSKonstantin Khlebnikov */ 133038646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 133138646013SJiri Slaby { 133238646013SJiri Slaby struct file *exe_file; 133338646013SJiri Slaby 133490f31d0eSKonstantin Khlebnikov rcu_read_lock(); 133590f31d0eSKonstantin Khlebnikov exe_file = rcu_dereference(mm->exe_file); 133690f31d0eSKonstantin Khlebnikov if (exe_file && !get_file_rcu(exe_file)) 133790f31d0eSKonstantin Khlebnikov exe_file = NULL; 133890f31d0eSKonstantin Khlebnikov rcu_read_unlock(); 133938646013SJiri Slaby return exe_file; 134038646013SJiri Slaby } 134138646013SJiri Slaby 13421da177e4SLinus Torvalds /** 1343cd81a917SMateusz Guzik * get_task_exe_file - acquire a reference to the task's executable file 1344cd81a917SMateusz Guzik * 1345cd81a917SMateusz Guzik * Returns %NULL if task's mm (if any) has no associated executable file or 1346cd81a917SMateusz Guzik * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 1347cd81a917SMateusz Guzik * User must release file via fput(). 1348cd81a917SMateusz Guzik */ 1349cd81a917SMateusz Guzik struct file *get_task_exe_file(struct task_struct *task) 1350cd81a917SMateusz Guzik { 1351cd81a917SMateusz Guzik struct file *exe_file = NULL; 1352cd81a917SMateusz Guzik struct mm_struct *mm; 1353cd81a917SMateusz Guzik 1354cd81a917SMateusz Guzik task_lock(task); 1355cd81a917SMateusz Guzik mm = task->mm; 1356cd81a917SMateusz Guzik if (mm) { 1357cd81a917SMateusz Guzik if (!(task->flags & PF_KTHREAD)) 1358cd81a917SMateusz Guzik exe_file = get_mm_exe_file(mm); 1359cd81a917SMateusz Guzik } 1360cd81a917SMateusz Guzik task_unlock(task); 1361cd81a917SMateusz Guzik return exe_file; 1362cd81a917SMateusz Guzik } 1363cd81a917SMateusz Guzik 1364cd81a917SMateusz Guzik /** 13651da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 13661da177e4SLinus Torvalds * 1367246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 13681da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 13691da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 13701da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 13711da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 13721da177e4SLinus Torvalds */ 13731da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds struct mm_struct *mm; 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds task_lock(task); 13781da177e4SLinus Torvalds mm = task->mm; 13791da177e4SLinus Torvalds if (mm) { 1380246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 13811da177e4SLinus Torvalds mm = NULL; 13821da177e4SLinus Torvalds else 13833fce371bSVegard Nossum mmget(mm); 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds task_unlock(task); 13861da177e4SLinus Torvalds return mm; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 13891da177e4SLinus Torvalds 13908cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 13918cdb878dSChristopher Yeoh { 13928cdb878dSChristopher Yeoh struct mm_struct *mm; 13938cdb878dSChristopher Yeoh int err; 13948cdb878dSChristopher Yeoh 1395f7cfd871SEric W. Biederman err = down_read_killable(&task->signal->exec_update_lock); 13968cdb878dSChristopher Yeoh if (err) 13978cdb878dSChristopher Yeoh return ERR_PTR(err); 13988cdb878dSChristopher Yeoh 13998cdb878dSChristopher Yeoh mm = get_task_mm(task); 14008cdb878dSChristopher Yeoh if (mm && mm != current->mm && 14018cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 14028cdb878dSChristopher Yeoh mmput(mm); 14038cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 14048cdb878dSChristopher Yeoh } 1405f7cfd871SEric W. Biederman up_read(&task->signal->exec_update_lock); 14068cdb878dSChristopher Yeoh 14078cdb878dSChristopher Yeoh return mm; 14088cdb878dSChristopher Yeoh } 14098cdb878dSChristopher Yeoh 141057b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 1411c415c3b4SOleg Nesterov { 1412d68b46feSOleg Nesterov struct completion *vfork; 1413c415c3b4SOleg Nesterov 1414d68b46feSOleg Nesterov task_lock(tsk); 1415d68b46feSOleg Nesterov vfork = tsk->vfork_done; 1416d68b46feSOleg Nesterov if (likely(vfork)) { 1417c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 1418d68b46feSOleg Nesterov complete(vfork); 1419d68b46feSOleg Nesterov } 1420d68b46feSOleg Nesterov task_unlock(tsk); 1421d68b46feSOleg Nesterov } 1422d68b46feSOleg Nesterov 1423d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 1424d68b46feSOleg Nesterov struct completion *vfork) 1425d68b46feSOleg Nesterov { 1426d68b46feSOleg Nesterov int killed; 1427d68b46feSOleg Nesterov 1428d68b46feSOleg Nesterov freezer_do_not_count(); 142976f969e8SRoman Gushchin cgroup_enter_frozen(); 1430d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 143176f969e8SRoman Gushchin cgroup_leave_frozen(false); 1432d68b46feSOleg Nesterov freezer_count(); 1433d68b46feSOleg Nesterov 1434d68b46feSOleg Nesterov if (killed) { 1435d68b46feSOleg Nesterov task_lock(child); 1436d68b46feSOleg Nesterov child->vfork_done = NULL; 1437d68b46feSOleg Nesterov task_unlock(child); 1438d68b46feSOleg Nesterov } 1439d68b46feSOleg Nesterov 1440d68b46feSOleg Nesterov put_task_struct(child); 1441d68b46feSOleg Nesterov return killed; 1442c415c3b4SOleg Nesterov } 1443c415c3b4SOleg Nesterov 14441da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 14451da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 14461da177e4SLinus Torvalds * error success whatever. 14471da177e4SLinus Torvalds * 14481da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 14491da177e4SLinus Torvalds * from the current process. 14501da177e4SLinus Torvalds * 14511da177e4SLinus Torvalds * This difference is important for error handling, when we 14521da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 14531da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 14541da177e4SLinus Torvalds * restoring the old one. . . 14551da177e4SLinus Torvalds * Eric Biederman 10 January 1998 14561da177e4SLinus Torvalds */ 14574610ba7aSThomas Gleixner static void mm_release(struct task_struct *tsk, struct mm_struct *mm) 14581da177e4SLinus Torvalds { 14590326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 14600326f5a9SSrikar Dronamraju 14611da177e4SLinus Torvalds /* Get rid of any cached register state */ 14621da177e4SLinus Torvalds deactivate_mm(tsk, mm); 14631da177e4SLinus Torvalds 1464fec1d011SRoland McGrath /* 1465735f2770SMichal Hocko * Signal userspace if we're not exiting with a core dump 1466735f2770SMichal Hocko * because we want to leave the value intact for debugging 1467735f2770SMichal Hocko * purposes. 1468fec1d011SRoland McGrath */ 14699c8a8228SEric Dumazet if (tsk->clear_child_tid) { 147092307383SEric W. Biederman if (atomic_read(&mm->mm_users) > 1) { 14711da177e4SLinus Torvalds /* 14721da177e4SLinus Torvalds * We don't check the error code - if userspace has 14731da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 14741da177e4SLinus Torvalds */ 14759c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 14762de0db99SDominik Brodowski do_futex(tsk->clear_child_tid, FUTEX_WAKE, 14772de0db99SDominik Brodowski 1, NULL, NULL, 0, 0); 14789c8a8228SEric Dumazet } 14799c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 14801da177e4SLinus Torvalds } 1481f7505d64SKonstantin Khlebnikov 1482f7505d64SKonstantin Khlebnikov /* 1483f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 1484f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 1485f7505d64SKonstantin Khlebnikov */ 1486f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 1487f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 14904610ba7aSThomas Gleixner void exit_mm_release(struct task_struct *tsk, struct mm_struct *mm) 14914610ba7aSThomas Gleixner { 1492150d7158SThomas Gleixner futex_exit_release(tsk); 14934610ba7aSThomas Gleixner mm_release(tsk, mm); 14944610ba7aSThomas Gleixner } 14954610ba7aSThomas Gleixner 14964610ba7aSThomas Gleixner void exec_mm_release(struct task_struct *tsk, struct mm_struct *mm) 14974610ba7aSThomas Gleixner { 1498150d7158SThomas Gleixner futex_exec_release(tsk); 14994610ba7aSThomas Gleixner mm_release(tsk, mm); 15004610ba7aSThomas Gleixner } 15014610ba7aSThomas Gleixner 150213585fa0SNadav Amit /** 150313585fa0SNadav Amit * dup_mm() - duplicates an existing mm structure 150413585fa0SNadav Amit * @tsk: the task_struct with which the new mm will be associated. 150513585fa0SNadav Amit * @oldmm: the mm to duplicate. 150613585fa0SNadav Amit * 150713585fa0SNadav Amit * Allocates a new mm structure and duplicates the provided @oldmm structure 150813585fa0SNadav Amit * content into it. 150913585fa0SNadav Amit * 151013585fa0SNadav Amit * Return: the duplicated mm or NULL on failure. 1511a0a7ec30SJANAK DESAI */ 151213585fa0SNadav Amit static struct mm_struct *dup_mm(struct task_struct *tsk, 151313585fa0SNadav Amit struct mm_struct *oldmm) 1514a0a7ec30SJANAK DESAI { 151513585fa0SNadav Amit struct mm_struct *mm; 1516a0a7ec30SJANAK DESAI int err; 1517a0a7ec30SJANAK DESAI 1518a0a7ec30SJANAK DESAI mm = allocate_mm(); 1519a0a7ec30SJANAK DESAI if (!mm) 1520a0a7ec30SJANAK DESAI goto fail_nomem; 1521a0a7ec30SJANAK DESAI 1522a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 1523a0a7ec30SJANAK DESAI 1524bfedb589SEric W. Biederman if (!mm_init(mm, tsk, mm->user_ns)) 1525a0a7ec30SJANAK DESAI goto fail_nomem; 1526a0a7ec30SJANAK DESAI 1527a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 1528a0a7ec30SJANAK DESAI if (err) 1529a0a7ec30SJANAK DESAI goto free_pt; 1530a0a7ec30SJANAK DESAI 1531a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 1532a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 1533a0a7ec30SJANAK DESAI 1534801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1535801460d0SHiroshi Shimamoto goto free_pt; 1536801460d0SHiroshi Shimamoto 1537a0a7ec30SJANAK DESAI return mm; 1538a0a7ec30SJANAK DESAI 1539a0a7ec30SJANAK DESAI free_pt: 1540801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 1541801460d0SHiroshi Shimamoto mm->binfmt = NULL; 1542c3f3ce04SAndrea Arcangeli mm_init_owner(mm, NULL); 1543a0a7ec30SJANAK DESAI mmput(mm); 1544a0a7ec30SJANAK DESAI 1545a0a7ec30SJANAK DESAI fail_nomem: 1546a0a7ec30SJANAK DESAI return NULL; 1547a0a7ec30SJANAK DESAI } 1548a0a7ec30SJANAK DESAI 15491da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 15501da177e4SLinus Torvalds { 15511da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 15541da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 155517406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 155617406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 1557a2e51445SDmitry Vyukov tsk->last_switch_time = 0; 155817406b82SMandeep Singh Baines #endif 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds tsk->mm = NULL; 15611da177e4SLinus Torvalds tsk->active_mm = NULL; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* 15641da177e4SLinus Torvalds * Are we cloning a kernel thread? 15651da177e4SLinus Torvalds * 15661da177e4SLinus Torvalds * We need to steal a active VM for that.. 15671da177e4SLinus Torvalds */ 15681da177e4SLinus Torvalds oldmm = current->mm; 15691da177e4SLinus Torvalds if (!oldmm) 15701da177e4SLinus Torvalds return 0; 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 15733fce371bSVegard Nossum mmget(oldmm); 15741da177e4SLinus Torvalds mm = oldmm; 1575a6895399SRolf Eike Beer } else { 157613585fa0SNadav Amit mm = dup_mm(tsk, current->mm); 15771da177e4SLinus Torvalds if (!mm) 1578a6895399SRolf Eike Beer return -ENOMEM; 1579a6895399SRolf Eike Beer } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds tsk->mm = mm; 15821da177e4SLinus Torvalds tsk->active_mm = mm; 15831da177e4SLinus Torvalds return 0; 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 1586a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 15871da177e4SLinus Torvalds { 1588498052bbSAl Viro struct fs_struct *fs = current->fs; 15891da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 1590498052bbSAl Viro /* tsk->fs is already what we want */ 15912a4419b5SNick Piggin spin_lock(&fs->lock); 1592498052bbSAl Viro if (fs->in_exec) { 15932a4419b5SNick Piggin spin_unlock(&fs->lock); 1594498052bbSAl Viro return -EAGAIN; 1595498052bbSAl Viro } 1596498052bbSAl Viro fs->users++; 15972a4419b5SNick Piggin spin_unlock(&fs->lock); 15981da177e4SLinus Torvalds return 0; 15991da177e4SLinus Torvalds } 1600498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 16011da177e4SLinus Torvalds if (!tsk->fs) 16021da177e4SLinus Torvalds return -ENOMEM; 16031da177e4SLinus Torvalds return 0; 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 1606a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 1607a016f338SJANAK DESAI { 1608a016f338SJANAK DESAI struct files_struct *oldf, *newf; 1609a016f338SJANAK DESAI int error = 0; 1610a016f338SJANAK DESAI 1611a016f338SJANAK DESAI /* 1612a016f338SJANAK DESAI * A background process may not have any files ... 1613a016f338SJANAK DESAI */ 1614a016f338SJANAK DESAI oldf = current->files; 1615a016f338SJANAK DESAI if (!oldf) 1616a016f338SJANAK DESAI goto out; 1617a016f338SJANAK DESAI 1618a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 1619a016f338SJANAK DESAI atomic_inc(&oldf->count); 1620a016f338SJANAK DESAI goto out; 1621a016f338SJANAK DESAI } 1622a016f338SJANAK DESAI 162360997c3dSChristian Brauner newf = dup_fd(oldf, NR_OPEN_MAX, &error); 1624a016f338SJANAK DESAI if (!newf) 1625a016f338SJANAK DESAI goto out; 1626a016f338SJANAK DESAI 1627a016f338SJANAK DESAI tsk->files = newf; 1628a016f338SJANAK DESAI error = 0; 1629a016f338SJANAK DESAI out: 1630a016f338SJANAK DESAI return error; 1631a016f338SJANAK DESAI } 1632a016f338SJANAK DESAI 1633a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 16341da177e4SLinus Torvalds { 16351da177e4SLinus Torvalds struct sighand_struct *sig; 16361da177e4SLinus Torvalds 163760348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 1638d036bda7SElena Reshetova refcount_inc(¤t->sighand->count); 16391da177e4SLinus Torvalds return 0; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 16420c282b06SMadhuparna Bhowmik RCU_INIT_POINTER(tsk->sighand, sig); 16431da177e4SLinus Torvalds if (!sig) 16441da177e4SLinus Torvalds return -ENOMEM; 16459d7fb042SPeter Zijlstra 1646d036bda7SElena Reshetova refcount_set(&sig->count, 1); 164706e62a46SJann Horn spin_lock_irq(¤t->sighand->siglock); 16481da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 164906e62a46SJann Horn spin_unlock_irq(¤t->sighand->siglock); 1650b612e5dfSChristian Brauner 1651b612e5dfSChristian Brauner /* Reset all signal handler not set to SIG_IGN to SIG_DFL. */ 1652b612e5dfSChristian Brauner if (clone_flags & CLONE_CLEAR_SIGHAND) 1653b612e5dfSChristian Brauner flush_signal_handlers(tsk, 0); 1654b612e5dfSChristian Brauner 16551da177e4SLinus Torvalds return 0; 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 1658a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1659c81addc9SOleg Nesterov { 1660d036bda7SElena Reshetova if (refcount_dec_and_test(&sighand->count)) { 1661d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1662392809b2SOleg Nesterov /* 16635f0d5a3aSPaul E. McKenney * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it 1664392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1665392809b2SOleg Nesterov */ 1666c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1667c81addc9SOleg Nesterov } 1668d80e731eSOleg Nesterov } 1669c81addc9SOleg Nesterov 1670f06febc9SFrank Mayhar /* 1671f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1672f06febc9SFrank Mayhar */ 1673f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1674f06febc9SFrank Mayhar { 16752b69942fSThomas Gleixner struct posix_cputimers *pct = &sig->posix_cputimers; 167678d7d407SJiri Slaby unsigned long cpu_limit; 167778d7d407SJiri Slaby 1678316c1608SJason Low cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 16793a245c0fSThomas Gleixner posix_cputimers_group_init(pct, cpu_limit); 16806279a751SOleg Nesterov } 16816279a751SOleg Nesterov 1682a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 16831da177e4SLinus Torvalds { 16841da177e4SLinus Torvalds struct signal_struct *sig; 16851da177e4SLinus Torvalds 16864ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1687490dea45SPeter Zijlstra return 0; 16886279a751SOleg Nesterov 1689a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 16901da177e4SLinus Torvalds tsk->signal = sig; 16911da177e4SLinus Torvalds if (!sig) 16921da177e4SLinus Torvalds return -ENOMEM; 16931da177e4SLinus Torvalds 1694b3ac022cSOleg Nesterov sig->nr_threads = 1; 16951da177e4SLinus Torvalds atomic_set(&sig->live, 1); 169660d4de3fSElena Reshetova refcount_set(&sig->sigcnt, 1); 16970c740d0aSOleg Nesterov 16980c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 16990c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 17000c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 17010c740d0aSOleg Nesterov 17021da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1703db51aeccSOleg Nesterov sig->curr_target = tsk; 17041da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 1705c3ad2c3bSEric W. Biederman INIT_HLIST_HEAD(&sig->multiprocess); 1706e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 17079d7fb042SPeter Zijlstra prev_cputime_init(&sig->prev_cputime); 17081da177e4SLinus Torvalds 1709baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 1710b18b6a9cSNicolas Pitre INIT_LIST_HEAD(&sig->posix_timers); 1711c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 17121da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 1713baa73d9eSNicolas Pitre #endif 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds task_lock(current->group_leader); 17161da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 17171da177e4SLinus Torvalds task_unlock(current->group_leader); 17181da177e4SLinus Torvalds 17196279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 17206279a751SOleg Nesterov 1721522ed776SMiloslav Trmac tty_audit_fork(sig); 17225091faa4SMike Galbraith sched_autogroup_fork(sig); 1723522ed776SMiloslav Trmac 1724a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1725dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 172628b83c51SKOSAKI Motohiro 17279b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 1728f7cfd871SEric W. Biederman init_rwsem(&sig->exec_update_lock); 17299b1bf12dSKOSAKI Motohiro 17301da177e4SLinus Torvalds return 0; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 1733dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1734dbd95212SKees Cook { 1735dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1736dbd95212SKees Cook /* 1737dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1738dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1739dbd95212SKees Cook * needed because this new task is not yet running and cannot 1740dbd95212SKees Cook * be racing exec. 1741dbd95212SKees Cook */ 174269f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1743dbd95212SKees Cook 1744dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1745dbd95212SKees Cook get_seccomp_filter(current); 1746dbd95212SKees Cook p->seccomp = current->seccomp; 1747dbd95212SKees Cook 1748dbd95212SKees Cook /* 1749dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1750dbd95212SKees Cook * between the task_struct being duplicated and holding the 1751dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1752dbd95212SKees Cook */ 1753dbd95212SKees Cook if (task_no_new_privs(current)) 1754dbd95212SKees Cook task_set_no_new_privs(p); 1755dbd95212SKees Cook 1756dbd95212SKees Cook /* 1757dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1758dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1759dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1760dbd95212SKees Cook */ 1761dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 176223d67a54SGabriel Krisman Bertazi set_task_syscall_work(p, SECCOMP); 1763dbd95212SKees Cook #endif 1764dbd95212SKees Cook } 1765dbd95212SKees Cook 176617da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 17671da177e4SLinus Torvalds { 17681da177e4SLinus Torvalds current->clear_child_tid = tidptr; 17691da177e4SLinus Torvalds 1770b488893aSPavel Emelyanov return task_pid_vnr(current); 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds 1773a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 177423f78d4aSIngo Molnar { 17751d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1776e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1777a23ba907SDavidlohr Bueso p->pi_waiters = RB_ROOT_CACHED; 1778e96a7705SXunlei Pang p->pi_top_task = NULL; 177923f78d4aSIngo Molnar p->pi_blocked_on = NULL; 178023f78d4aSIngo Molnar #endif 178123f78d4aSIngo Molnar } 178223f78d4aSIngo Molnar 17832c470475SEric W. Biederman static inline void init_task_pid_links(struct task_struct *task) 17842c470475SEric W. Biederman { 17852c470475SEric W. Biederman enum pid_type type; 17862c470475SEric W. Biederman 178796e1e984SAlexander Guril for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) 17882c470475SEric W. Biederman INIT_HLIST_NODE(&task->pid_links[type]); 17892c470475SEric W. Biederman } 17902c470475SEric W. Biederman 179181907739SOleg Nesterov static inline void 179281907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 179381907739SOleg Nesterov { 17942c470475SEric W. Biederman if (type == PIDTYPE_PID) 17952c470475SEric W. Biederman task->thread_pid = pid; 17962c470475SEric W. Biederman else 17972c470475SEric W. Biederman task->signal->pids[type] = pid; 179881907739SOleg Nesterov } 179981907739SOleg Nesterov 18006bfbaa51SIngo Molnar static inline void rcu_copy_process(struct task_struct *p) 18016bfbaa51SIngo Molnar { 18026bfbaa51SIngo Molnar #ifdef CONFIG_PREEMPT_RCU 18036bfbaa51SIngo Molnar p->rcu_read_lock_nesting = 0; 18046bfbaa51SIngo Molnar p->rcu_read_unlock_special.s = 0; 18056bfbaa51SIngo Molnar p->rcu_blocked_node = NULL; 18066bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_node_entry); 18076bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_PREEMPT_RCU */ 18086bfbaa51SIngo Molnar #ifdef CONFIG_TASKS_RCU 18096bfbaa51SIngo Molnar p->rcu_tasks_holdout = false; 18106bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); 18116bfbaa51SIngo Molnar p->rcu_tasks_idle_cpu = -1; 18126bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_TASKS_RCU */ 1813d5f177d3SPaul E. McKenney #ifdef CONFIG_TASKS_TRACE_RCU 1814d5f177d3SPaul E. McKenney p->trc_reader_nesting = 0; 1815276c4104SPaul E. McKenney p->trc_reader_special.s = 0; 1816d5f177d3SPaul E. McKenney INIT_LIST_HEAD(&p->trc_holdout_list); 1817434c9eefSPaul E. McKenney INIT_LIST_HEAD(&p->trc_blkd_node); 1818d5f177d3SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */ 18196bfbaa51SIngo Molnar } 18206bfbaa51SIngo Molnar 18213695eae5SChristian Brauner struct pid *pidfd_pid(const struct file *file) 18223695eae5SChristian Brauner { 18233695eae5SChristian Brauner if (file->f_op == &pidfd_fops) 18243695eae5SChristian Brauner return file->private_data; 18253695eae5SChristian Brauner 18263695eae5SChristian Brauner return ERR_PTR(-EBADF); 18273695eae5SChristian Brauner } 18283695eae5SChristian Brauner 1829b3e58382SChristian Brauner static int pidfd_release(struct inode *inode, struct file *file) 1830b3e58382SChristian Brauner { 1831b3e58382SChristian Brauner struct pid *pid = file->private_data; 1832b3e58382SChristian Brauner 1833b3e58382SChristian Brauner file->private_data = NULL; 1834b3e58382SChristian Brauner put_pid(pid); 1835b3e58382SChristian Brauner return 0; 1836b3e58382SChristian Brauner } 1837b3e58382SChristian Brauner 1838b3e58382SChristian Brauner #ifdef CONFIG_PROC_FS 183915d42eb2SChristian Kellner /** 184015d42eb2SChristian Kellner * pidfd_show_fdinfo - print information about a pidfd 184115d42eb2SChristian Kellner * @m: proc fdinfo file 184215d42eb2SChristian Kellner * @f: file referencing a pidfd 184315d42eb2SChristian Kellner * 184415d42eb2SChristian Kellner * Pid: 184515d42eb2SChristian Kellner * This function will print the pid that a given pidfd refers to in the 184615d42eb2SChristian Kellner * pid namespace of the procfs instance. 184715d42eb2SChristian Kellner * If the pid namespace of the process is not a descendant of the pid 184815d42eb2SChristian Kellner * namespace of the procfs instance 0 will be shown as its pid. This is 184915d42eb2SChristian Kellner * similar to calling getppid() on a process whose parent is outside of 185015d42eb2SChristian Kellner * its pid namespace. 185115d42eb2SChristian Kellner * 185215d42eb2SChristian Kellner * NSpid: 185315d42eb2SChristian Kellner * If pid namespaces are supported then this function will also print 185415d42eb2SChristian Kellner * the pid of a given pidfd refers to for all descendant pid namespaces 185515d42eb2SChristian Kellner * starting from the current pid namespace of the instance, i.e. the 185615d42eb2SChristian Kellner * Pid field and the first entry in the NSpid field will be identical. 185715d42eb2SChristian Kellner * If the pid namespace of the process is not a descendant of the pid 185815d42eb2SChristian Kellner * namespace of the procfs instance 0 will be shown as its first NSpid 185915d42eb2SChristian Kellner * entry and no others will be shown. 186015d42eb2SChristian Kellner * Note that this differs from the Pid and NSpid fields in 186115d42eb2SChristian Kellner * /proc/<pid>/status where Pid and NSpid are always shown relative to 186215d42eb2SChristian Kellner * the pid namespace of the procfs instance. The difference becomes 186315d42eb2SChristian Kellner * obvious when sending around a pidfd between pid namespaces from a 1864a8ca6b13SXiaofeng Cao * different branch of the tree, i.e. where no ancestral relation is 186515d42eb2SChristian Kellner * present between the pid namespaces: 186615d42eb2SChristian Kellner * - create two new pid namespaces ns1 and ns2 in the initial pid 186715d42eb2SChristian Kellner * namespace (also take care to create new mount namespaces in the 186815d42eb2SChristian Kellner * new pid namespace and mount procfs) 186915d42eb2SChristian Kellner * - create a process with a pidfd in ns1 187015d42eb2SChristian Kellner * - send pidfd from ns1 to ns2 187115d42eb2SChristian Kellner * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid 187215d42eb2SChristian Kellner * have exactly one entry, which is 0 187315d42eb2SChristian Kellner */ 1874b3e58382SChristian Brauner static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) 1875b3e58382SChristian Brauner { 1876b3e58382SChristian Brauner struct pid *pid = f->private_data; 18773d6d8da4SChristian Brauner struct pid_namespace *ns; 18783d6d8da4SChristian Brauner pid_t nr = -1; 1879b3e58382SChristian Brauner 18803d6d8da4SChristian Brauner if (likely(pid_has_task(pid, PIDTYPE_PID))) { 18819d78edeaSAlexey Gladkov ns = proc_pid_ns(file_inode(m->file)->i_sb); 18823d6d8da4SChristian Brauner nr = pid_nr_ns(pid, ns); 18833d6d8da4SChristian Brauner } 18843d6d8da4SChristian Brauner 18853d6d8da4SChristian Brauner seq_put_decimal_ll(m, "Pid:\t", nr); 188615d42eb2SChristian Kellner 188715d42eb2SChristian Kellner #ifdef CONFIG_PID_NS 18883d6d8da4SChristian Brauner seq_put_decimal_ll(m, "\nNSpid:\t", nr); 18893d6d8da4SChristian Brauner if (nr > 0) { 189015d42eb2SChristian Kellner int i; 189115d42eb2SChristian Kellner 189215d42eb2SChristian Kellner /* If nr is non-zero it means that 'pid' is valid and that 189315d42eb2SChristian Kellner * ns, i.e. the pid namespace associated with the procfs 189415d42eb2SChristian Kellner * instance, is in the pid namespace hierarchy of pid. 189515d42eb2SChristian Kellner * Start at one below the already printed level. 189615d42eb2SChristian Kellner */ 189715d42eb2SChristian Kellner for (i = ns->level + 1; i <= pid->level; i++) 18983d6d8da4SChristian Brauner seq_put_decimal_ll(m, "\t", pid->numbers[i].nr); 189915d42eb2SChristian Kellner } 190015d42eb2SChristian Kellner #endif 1901b3e58382SChristian Brauner seq_putc(m, '\n'); 1902b3e58382SChristian Brauner } 1903b3e58382SChristian Brauner #endif 1904b3e58382SChristian Brauner 1905b53b0b9dSJoel Fernandes (Google) /* 1906b53b0b9dSJoel Fernandes (Google) * Poll support for process exit notification. 1907b53b0b9dSJoel Fernandes (Google) */ 19089e77716aSLuc Van Oostenryck static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) 1909b53b0b9dSJoel Fernandes (Google) { 1910b53b0b9dSJoel Fernandes (Google) struct pid *pid = file->private_data; 19119e77716aSLuc Van Oostenryck __poll_t poll_flags = 0; 1912b53b0b9dSJoel Fernandes (Google) 1913b53b0b9dSJoel Fernandes (Google) poll_wait(file, &pid->wait_pidfd, pts); 1914b53b0b9dSJoel Fernandes (Google) 1915b53b0b9dSJoel Fernandes (Google) /* 1916b53b0b9dSJoel Fernandes (Google) * Inform pollers only when the whole thread group exits. 1917b53b0b9dSJoel Fernandes (Google) * If the thread group leader exits before all other threads in the 1918b53b0b9dSJoel Fernandes (Google) * group, then poll(2) should block, similar to the wait(2) family. 1919b53b0b9dSJoel Fernandes (Google) */ 192038fd525aSEric W. Biederman if (thread_group_exited(pid)) 19219e77716aSLuc Van Oostenryck poll_flags = EPOLLIN | EPOLLRDNORM; 1922b53b0b9dSJoel Fernandes (Google) 1923b53b0b9dSJoel Fernandes (Google) return poll_flags; 1924b53b0b9dSJoel Fernandes (Google) } 1925b53b0b9dSJoel Fernandes (Google) 1926b3e58382SChristian Brauner const struct file_operations pidfd_fops = { 1927b3e58382SChristian Brauner .release = pidfd_release, 1928b53b0b9dSJoel Fernandes (Google) .poll = pidfd_poll, 1929b3e58382SChristian Brauner #ifdef CONFIG_PROC_FS 1930b3e58382SChristian Brauner .show_fdinfo = pidfd_show_fdinfo, 1931b3e58382SChristian Brauner #endif 1932b3e58382SChristian Brauner }; 1933b3e58382SChristian Brauner 1934c3f3ce04SAndrea Arcangeli static void __delayed_free_task(struct rcu_head *rhp) 1935c3f3ce04SAndrea Arcangeli { 1936c3f3ce04SAndrea Arcangeli struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); 1937c3f3ce04SAndrea Arcangeli 1938c3f3ce04SAndrea Arcangeli free_task(tsk); 1939c3f3ce04SAndrea Arcangeli } 1940c3f3ce04SAndrea Arcangeli 1941c3f3ce04SAndrea Arcangeli static __always_inline void delayed_free_task(struct task_struct *tsk) 1942c3f3ce04SAndrea Arcangeli { 1943c3f3ce04SAndrea Arcangeli if (IS_ENABLED(CONFIG_MEMCG)) 1944c3f3ce04SAndrea Arcangeli call_rcu(&tsk->rcu, __delayed_free_task); 1945c3f3ce04SAndrea Arcangeli else 1946c3f3ce04SAndrea Arcangeli free_task(tsk); 1947c3f3ce04SAndrea Arcangeli } 1948c3f3ce04SAndrea Arcangeli 194967197a4fSSuren Baghdasaryan static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk) 195067197a4fSSuren Baghdasaryan { 195167197a4fSSuren Baghdasaryan /* Skip if kernel thread */ 195267197a4fSSuren Baghdasaryan if (!tsk->mm) 195367197a4fSSuren Baghdasaryan return; 195467197a4fSSuren Baghdasaryan 195567197a4fSSuren Baghdasaryan /* Skip if spawning a thread or using vfork */ 195667197a4fSSuren Baghdasaryan if ((clone_flags & (CLONE_VM | CLONE_THREAD | CLONE_VFORK)) != CLONE_VM) 195767197a4fSSuren Baghdasaryan return; 195867197a4fSSuren Baghdasaryan 195967197a4fSSuren Baghdasaryan /* We need to synchronize with __set_oom_adj */ 196067197a4fSSuren Baghdasaryan mutex_lock(&oom_adj_mutex); 196167197a4fSSuren Baghdasaryan set_bit(MMF_MULTIPROCESS, &tsk->mm->flags); 196267197a4fSSuren Baghdasaryan /* Update the values in case they were changed after copy_signal */ 196367197a4fSSuren Baghdasaryan tsk->signal->oom_score_adj = current->signal->oom_score_adj; 196467197a4fSSuren Baghdasaryan tsk->signal->oom_score_adj_min = current->signal->oom_score_adj_min; 196567197a4fSSuren Baghdasaryan mutex_unlock(&oom_adj_mutex); 196667197a4fSSuren Baghdasaryan } 196767197a4fSSuren Baghdasaryan 196879257534SDaniel Bristot de Oliveira #ifdef CONFIG_RV 196979257534SDaniel Bristot de Oliveira static void rv_task_fork(struct task_struct *p) 197079257534SDaniel Bristot de Oliveira { 197179257534SDaniel Bristot de Oliveira int i; 197279257534SDaniel Bristot de Oliveira 197379257534SDaniel Bristot de Oliveira for (i = 0; i < RV_PER_TASK_MONITORS; i++) 197479257534SDaniel Bristot de Oliveira p->rv[i].da_mon.monitoring = false; 197579257534SDaniel Bristot de Oliveira } 197679257534SDaniel Bristot de Oliveira #else 197779257534SDaniel Bristot de Oliveira #define rv_task_fork(p) do {} while (0) 197879257534SDaniel Bristot de Oliveira #endif 197979257534SDaniel Bristot de Oliveira 1980f06febc9SFrank Mayhar /* 19811da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 19821da177e4SLinus Torvalds * but does not actually start it yet. 19831da177e4SLinus Torvalds * 19841da177e4SLinus Torvalds * It copies the registers, and all the appropriate 19851da177e4SLinus Torvalds * parts of the process environment (as per the clone 19861da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 19871da177e4SLinus Torvalds */ 19880766f788SEmese Revfy static __latent_entropy struct task_struct *copy_process( 198909a05394SRoland McGrath struct pid *pid, 19903033f14aSJosh Triplett int trace, 19917f192e3cSChristian Brauner int node, 19927f192e3cSChristian Brauner struct kernel_clone_args *args) 19931da177e4SLinus Torvalds { 1994b3e58382SChristian Brauner int pidfd = -1, retval; 1995a24efe62SMariusz Kozlowski struct task_struct *p; 1996c3ad2c3bSEric W. Biederman struct multiprocess_signals delayed; 19976fd2fe49SAl Viro struct file *pidfile = NULL; 1998c5febea0SEric W. Biederman const u64 clone_flags = args->flags; 1999769071acSAndrei Vagin struct nsproxy *nsp = current->nsproxy; 20001da177e4SLinus Torvalds 2001667b6094SMarcos Paulo de Souza /* 2002667b6094SMarcos Paulo de Souza * Don't allow sharing the root directory with processes in a different 2003667b6094SMarcos Paulo de Souza * namespace 2004667b6094SMarcos Paulo de Souza */ 20051da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 20061da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 20071da177e4SLinus Torvalds 2008e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 2009e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 2010e66eded8SEric W. Biederman 20111da177e4SLinus Torvalds /* 20121da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 20131da177e4SLinus Torvalds * can only be started up within the thread group. 20141da177e4SLinus Torvalds */ 20151da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 20161da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds /* 20191da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 20201da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 20211da177e4SLinus Torvalds * for various simplifications in other code. 20221da177e4SLinus Torvalds */ 20231da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 20241da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 20251da177e4SLinus Torvalds 2026123be07bSSukadev Bhattiprolu /* 2027123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 2028123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 2029123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 2030123be07bSSukadev Bhattiprolu * from creating siblings. 2031123be07bSSukadev Bhattiprolu */ 2032123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 2033123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 2034123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 2035123be07bSSukadev Bhattiprolu 20368382fcacSEric W. Biederman /* 203740a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 2038faf00da5SEric W. Biederman * do not allow it to share a thread group with the forking task. 20398382fcacSEric W. Biederman */ 2040faf00da5SEric W. Biederman if (clone_flags & CLONE_THREAD) { 204140a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 2042769071acSAndrei Vagin (task_active_pid_ns(current) != nsp->pid_ns_for_children)) 2043769071acSAndrei Vagin return ERR_PTR(-EINVAL); 2044769071acSAndrei Vagin } 2045769071acSAndrei Vagin 2046769071acSAndrei Vagin /* 2047769071acSAndrei Vagin * If the new process will be in a different time namespace 2048769071acSAndrei Vagin * do not allow it to share VM or a thread group with the forking task. 2049133e2d3eSAndrei Vagin * 2050133e2d3eSAndrei Vagin * On vfork, the child process enters the target time namespace only 2051133e2d3eSAndrei Vagin * after exec. 2052769071acSAndrei Vagin */ 2053133e2d3eSAndrei Vagin if ((clone_flags & (CLONE_VM | CLONE_VFORK)) == CLONE_VM) { 2054769071acSAndrei Vagin if (nsp->time_ns != nsp->time_ns_for_children) 20558382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 205640a0d32dSOleg Nesterov } 20578382fcacSEric W. Biederman 2058b3e58382SChristian Brauner if (clone_flags & CLONE_PIDFD) { 2059b3e58382SChristian Brauner /* 2060b3e58382SChristian Brauner * - CLONE_DETACHED is blocked so that we can potentially 2061b3e58382SChristian Brauner * reuse it later for CLONE_PIDFD. 2062b3e58382SChristian Brauner * - CLONE_THREAD is blocked until someone really needs it. 2063b3e58382SChristian Brauner */ 20647f192e3cSChristian Brauner if (clone_flags & (CLONE_DETACHED | CLONE_THREAD)) 2065b3e58382SChristian Brauner return ERR_PTR(-EINVAL); 2066b3e58382SChristian Brauner } 2067b3e58382SChristian Brauner 2068c3ad2c3bSEric W. Biederman /* 2069c3ad2c3bSEric W. Biederman * Force any signals received before this point to be delivered 2070c3ad2c3bSEric W. Biederman * before the fork happens. Collect up signals sent to multiple 2071c3ad2c3bSEric W. Biederman * processes that happen during the fork and delay them so that 2072c3ad2c3bSEric W. Biederman * they appear to happen after the fork. 2073c3ad2c3bSEric W. Biederman */ 2074c3ad2c3bSEric W. Biederman sigemptyset(&delayed.signal); 2075c3ad2c3bSEric W. Biederman INIT_HLIST_NODE(&delayed.node); 2076c3ad2c3bSEric W. Biederman 2077c3ad2c3bSEric W. Biederman spin_lock_irq(¤t->sighand->siglock); 2078c3ad2c3bSEric W. Biederman if (!(clone_flags & CLONE_THREAD)) 2079c3ad2c3bSEric W. Biederman hlist_add_head(&delayed.node, ¤t->signal->multiprocess); 2080c3ad2c3bSEric W. Biederman recalc_sigpending(); 2081c3ad2c3bSEric W. Biederman spin_unlock_irq(¤t->sighand->siglock); 2082c3ad2c3bSEric W. Biederman retval = -ERESTARTNOINTR; 208366ae0d1eSJens Axboe if (task_sigpending(current)) 2084c3ad2c3bSEric W. Biederman goto fork_out; 2085c3ad2c3bSEric W. Biederman 20861da177e4SLinus Torvalds retval = -ENOMEM; 2087725fc629SAndi Kleen p = dup_task_struct(current, node); 20881da177e4SLinus Torvalds if (!p) 20891da177e4SLinus Torvalds goto fork_out; 2090753550ebSEric W. Biederman p->flags &= ~PF_KTHREAD; 2091753550ebSEric W. Biederman if (args->kthread) 2092753550ebSEric W. Biederman p->flags |= PF_KTHREAD; 2093b16b3855SJens Axboe if (args->io_thread) { 2094b16b3855SJens Axboe /* 2095b16b3855SJens Axboe * Mark us an IO worker, and block any signal that isn't 2096b16b3855SJens Axboe * fatal or STOP 2097b16b3855SJens Axboe */ 2098cc440e87SJens Axboe p->flags |= PF_IO_WORKER; 2099b16b3855SJens Axboe siginitsetinv(&p->blocked, sigmask(SIGKILL)|sigmask(SIGSTOP)); 2100b16b3855SJens Axboe } 21011da177e4SLinus Torvalds 21027f192e3cSChristian Brauner p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL; 21034d6501dcSVegard Nossum /* 21044d6501dcSVegard Nossum * Clear TID on mm_release()? 21054d6501dcSVegard Nossum */ 21067f192e3cSChristian Brauner p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL; 21074d6501dcSVegard Nossum 2108f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 2109f7e8b616SSteven Rostedt 2110bea493a0SPeter Zijlstra rt_mutex_init_task(p); 2111bea493a0SPeter Zijlstra 2112a21ee605SPeter Zijlstra lockdep_assert_irqs_enabled(); 2113d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 2114de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 2115de30a2b3SIngo Molnar #endif 21168f2f9c4dSEric W. Biederman retval = copy_creds(p, clone_flags); 21178f2f9c4dSEric W. Biederman if (retval < 0) 21188f2f9c4dSEric W. Biederman goto bad_fork_free; 21198f2f9c4dSEric W. Biederman 21201da177e4SLinus Torvalds retval = -EAGAIN; 212121d1c5e3SAlexey Gladkov if (is_ucounts_overlimit(task_ucounts(p), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) { 2122b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 2123b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 21248f2f9c4dSEric W. Biederman goto bad_fork_cleanup_count; 21251da177e4SLinus Torvalds } 212672fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds /* 21291da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 21301da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 21311da177e4SLinus Torvalds * to stop root fork bombs. 21321da177e4SLinus Torvalds */ 213304ec93feSLi Zefan retval = -EAGAIN; 2134c17d1a3aSWeilong Chen if (data_race(nr_threads >= max_threads)) 21351da177e4SLinus Torvalds goto bad_fork_cleanup_count; 21361da177e4SLinus Torvalds 2137ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 2138a8ea6fc9SFrederic Weisbecker p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE | PF_NO_SETAFFINITY); 2139514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 21401da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 21411da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 2142f41d911fSPaul E. McKenney rcu_copy_process(p); 21431da177e4SLinus Torvalds p->vfork_done = NULL; 21441da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds init_sigpending(&p->pending); 21471da177e4SLinus Torvalds 214864861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 214940565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 215064861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 215140565b5aSStanislaw Gruszka #endif 21529d7fb042SPeter Zijlstra prev_cputime_init(&p->prev_cputime); 21539d7fb042SPeter Zijlstra 21546a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 2155bac5b6b6SFrederic Weisbecker seqcount_init(&p->vtime.seqcount); 2156bac5b6b6SFrederic Weisbecker p->vtime.starttime = 0; 2157bac5b6b6SFrederic Weisbecker p->vtime.state = VTIME_INACTIVE; 21586a61671bSFrederic Weisbecker #endif 21596a61671bSFrederic Weisbecker 21600f212204SJens Axboe #ifdef CONFIG_IO_URING 21610f212204SJens Axboe p->io_uring = NULL; 21620f212204SJens Axboe #endif 21630f212204SJens Axboe 2164a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 2165a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 2166a3a2e76cSKAMEZAWA Hiroyuki #endif 2167172ba844SBalbir Singh 21686976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 21696976675dSArjan van de Ven 2170eb414681SJohannes Weiner #ifdef CONFIG_PSI 2171eb414681SJohannes Weiner p->psi_flags = 0; 2172eb414681SJohannes Weiner #endif 2173eb414681SJohannes Weiner 21745995477aSAndrea Righi task_io_accounting_init(&p->ioac); 21751da177e4SLinus Torvalds acct_clear_integrals(p); 21761da177e4SLinus Torvalds 21773a245c0fSThomas Gleixner posix_cputimers_init(&p->posix_cputimers); 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds p->io_context = NULL; 2180c0b0ae8aSRichard Guy Briggs audit_set_context(p, NULL); 2181b4f48b63SPaul Menage cgroup_fork(p); 2182343f4c49SEric W. Biederman if (args->kthread) { 218340966e31SEric W. Biederman if (!set_kthread_struct(p)) 2184ff8288ffSEric W. Biederman goto bad_fork_cleanup_delayacct; 218540966e31SEric W. Biederman } 21861da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2187846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 21881da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 21891da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 21901da177e4SLinus Torvalds p->mempolicy = NULL; 2191ff8288ffSEric W. Biederman goto bad_fork_cleanup_delayacct; 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds #endif 2194778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 2195778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 2196778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 2197b7505861SAhmed S. Darwish seqcount_spinlock_init(&p->mems_allowed_seq, &p->alloc_lock); 2198778d3b0fSMichal Hocko #endif 2199de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 22000584df9cSMarco Elver memset(&p->irqtrace, 0, sizeof(p->irqtrace)); 22010584df9cSMarco Elver p->irqtrace.hardirq_disable_ip = _THIS_IP_; 22020584df9cSMarco Elver p->irqtrace.softirq_enable_ip = _THIS_IP_; 2203de30a2b3SIngo Molnar p->softirqs_enabled = 1; 2204de30a2b3SIngo Molnar p->softirq_context = 0; 2205de30a2b3SIngo Molnar #endif 22068bcbde54SDavid Hildenbrand 22078bcbde54SDavid Hildenbrand p->pagefault_disabled = 0; 22088bcbde54SDavid Hildenbrand 2209fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 2210b09be676SByungchul Park lockdep_init_task(p); 2211fbb9ce95SIngo Molnar #endif 22121da177e4SLinus Torvalds 2213408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 2214408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 2215408894eeSIngo Molnar #endif 2216cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 2217cafe5635SKent Overstreet p->sequential_io = 0; 2218cafe5635SKent Overstreet p->sequential_io_avg = 0; 2219cafe5635SKent Overstreet #endif 2220a10787e6SSong Liu #ifdef CONFIG_BPF_SYSCALL 2221a10787e6SSong Liu RCU_INIT_POINTER(p->bpf_storage, NULL); 2222c7603cfaSAndrii Nakryiko p->bpf_ctx = NULL; 2223a10787e6SSong Liu #endif 22240f481406SMarkus Metzger 22253c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 2226aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 2227aab03e05SDario Faggioli if (retval) 2228aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 22296ab423e0SPeter Zijlstra 22302b26f0aaSMarco Elver retval = perf_event_init_task(p, clone_flags); 22316ab423e0SPeter Zijlstra if (retval) 22326ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 2233fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 2234fb0a685cSDaniel Rebelo de Oliveira if (retval) 22356c72e350SPeter Zijlstra goto bad_fork_cleanup_perf; 22361da177e4SLinus Torvalds /* copy all the process information */ 2237ab602f79SJack Miller shm_init_task(p); 2238e4e55b47STetsuo Handa retval = security_task_alloc(p, clone_flags); 2239fb0a685cSDaniel Rebelo de Oliveira if (retval) 22401da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 2241e4e55b47STetsuo Handa retval = copy_semundo(clone_flags, p); 2242e4e55b47STetsuo Handa if (retval) 2243e4e55b47STetsuo Handa goto bad_fork_cleanup_security; 2244fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 2245fb0a685cSDaniel Rebelo de Oliveira if (retval) 22461da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 2247fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 2248fb0a685cSDaniel Rebelo de Oliveira if (retval) 22491da177e4SLinus Torvalds goto bad_fork_cleanup_files; 2250fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 2251fb0a685cSDaniel Rebelo de Oliveira if (retval) 22521da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 2253fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 2254fb0a685cSDaniel Rebelo de Oliveira if (retval) 22551da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 2256fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 2257fb0a685cSDaniel Rebelo de Oliveira if (retval) 22581da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 2259fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 2260fb0a685cSDaniel Rebelo de Oliveira if (retval) 2261d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 2262fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 2263fb0a685cSDaniel Rebelo de Oliveira if (retval) 2264fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 2265c5febea0SEric W. Biederman retval = copy_thread(p, args); 22661da177e4SLinus Torvalds if (retval) 2267fd0928dfSJens Axboe goto bad_fork_cleanup_io; 22681da177e4SLinus Torvalds 2269afaef01cSAlexander Popov stackleak_task_init(p); 2270afaef01cSAlexander Popov 2271425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 227249cb2fc4SAdrian Reber pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid, 227349cb2fc4SAdrian Reber args->set_tid_size); 227435f71bc0SMichal Hocko if (IS_ERR(pid)) { 227535f71bc0SMichal Hocko retval = PTR_ERR(pid); 22760740aa5fSJiri Slaby goto bad_fork_cleanup_thread; 2277425fb2b4SPavel Emelyanov } 227835f71bc0SMichal Hocko } 2279425fb2b4SPavel Emelyanov 2280b3e58382SChristian Brauner /* 2281b3e58382SChristian Brauner * This has to happen after we've potentially unshared the file 2282b3e58382SChristian Brauner * descriptor table (so that the pidfd doesn't leak into the child 2283b3e58382SChristian Brauner * if the fd table isn't shared). 2284b3e58382SChristian Brauner */ 2285b3e58382SChristian Brauner if (clone_flags & CLONE_PIDFD) { 22866fd2fe49SAl Viro retval = get_unused_fd_flags(O_RDWR | O_CLOEXEC); 2287b3e58382SChristian Brauner if (retval < 0) 2288b3e58382SChristian Brauner goto bad_fork_free_pid; 2289b3e58382SChristian Brauner 2290b3e58382SChristian Brauner pidfd = retval; 22916fd2fe49SAl Viro 22926fd2fe49SAl Viro pidfile = anon_inode_getfile("[pidfd]", &pidfd_fops, pid, 22936fd2fe49SAl Viro O_RDWR | O_CLOEXEC); 22946fd2fe49SAl Viro if (IS_ERR(pidfile)) { 22956fd2fe49SAl Viro put_unused_fd(pidfd); 229628dd29c0SChristian Brauner retval = PTR_ERR(pidfile); 22976fd2fe49SAl Viro goto bad_fork_free_pid; 22986fd2fe49SAl Viro } 22996fd2fe49SAl Viro get_pid(pid); /* held by pidfile now */ 23006fd2fe49SAl Viro 23017f192e3cSChristian Brauner retval = put_user(pidfd, args->pidfd); 2302b3e58382SChristian Brauner if (retval) 2303b3e58382SChristian Brauner goto bad_fork_put_pidfd; 2304b3e58382SChristian Brauner } 2305b3e58382SChristian Brauner 230673c10101SJens Axboe #ifdef CONFIG_BLOCK 230773c10101SJens Axboe p->plug = NULL; 230873c10101SJens Axboe #endif 2309ba31c1a4SThomas Gleixner futex_init_task(p); 2310ba31c1a4SThomas Gleixner 23111da177e4SLinus Torvalds /* 2312f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 2313f9a3879aSGOTO Masanori */ 2314f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 23152a742138SStas Sergeev sas_ss_reset(p); 2316f9a3879aSGOTO Masanori 2317f9a3879aSGOTO Masanori /* 23186580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 23196580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 23201da177e4SLinus Torvalds */ 23216580807dSOleg Nesterov user_disable_single_step(p); 232264c19ba2SGabriel Krisman Bertazi clear_task_syscall_work(p, SYSCALL_TRACE); 232364eb35f7SGabriel Krisman Bertazi #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 232464eb35f7SGabriel Krisman Bertazi clear_task_syscall_work(p, SYSCALL_EMU); 2325ed75e8d5SLaurent Vivier #endif 2326e02c9b0dSLin Feng clear_tsk_latency_tracing(p); 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds /* ok, now we should be set up.. */ 232918c830dfSOleg Nesterov p->pid = pid_nr(pid); 233018c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 233118c830dfSOleg Nesterov p->group_leader = current->group_leader; 233218c830dfSOleg Nesterov p->tgid = current->tgid; 233318c830dfSOleg Nesterov } else { 233418c830dfSOleg Nesterov p->group_leader = p; 233518c830dfSOleg Nesterov p->tgid = p->pid; 233618c830dfSOleg Nesterov } 23375f8aadd8SOleg Nesterov 23389d823e8fSWu Fengguang p->nr_dirtied = 0; 23399d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 234083712358SWu Fengguang p->dirty_paused_when = 0; 23419d823e8fSWu Fengguang 2342bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 234347e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 2344158e1645SAl Viro p->task_works = NULL; 2345ca7752caSMichael Pratt clear_posix_cputimers_work(p); 23461da177e4SLinus Torvalds 2347d741bf41SPeter Zijlstra #ifdef CONFIG_KRETPROBES 2348d741bf41SPeter Zijlstra p->kretprobe_instances.first = NULL; 2349d741bf41SPeter Zijlstra #endif 235054ecbe6fSMasami Hiramatsu #ifdef CONFIG_RETHOOK 235154ecbe6fSMasami Hiramatsu p->rethooks.first = NULL; 235254ecbe6fSMasami Hiramatsu #endif 2353d741bf41SPeter Zijlstra 235418c830dfSOleg Nesterov /* 23557e47682eSAleksa Sarai * Ensure that the cgroup subsystem policies allow the new process to be 23567b7b8a2cSRandy Dunlap * forked. It should be noted that the new process's css_set can be changed 23577e47682eSAleksa Sarai * between here and cgroup_post_fork() if an organisation operation is in 23587e47682eSAleksa Sarai * progress. 23597e47682eSAleksa Sarai */ 2360ef2c41cfSChristian Brauner retval = cgroup_can_fork(p, args); 23617e47682eSAleksa Sarai if (retval) 23625a5cf5cbSChristian Brauner goto bad_fork_put_pidfd; 23637e47682eSAleksa Sarai 23647e47682eSAleksa Sarai /* 2365b1e82065SPeter Zijlstra * Now that the cgroups are pinned, re-clone the parent cgroup and put 2366b1e82065SPeter Zijlstra * the new task on the correct runqueue. All this *before* the task 2367b1e82065SPeter Zijlstra * becomes visible. 2368b1e82065SPeter Zijlstra * 2369b1e82065SPeter Zijlstra * This isn't part of ->can_fork() because while the re-cloning is 2370b1e82065SPeter Zijlstra * cgroup specific, it unconditionally needs to place the task on a 2371b1e82065SPeter Zijlstra * runqueue. 2372b1e82065SPeter Zijlstra */ 2373b1e82065SPeter Zijlstra sched_cgroup_fork(p, args); 2374b1e82065SPeter Zijlstra 2375b1e82065SPeter Zijlstra /* 23767b558513SDavid Herrmann * From this point on we must avoid any synchronous user-space 23777b558513SDavid Herrmann * communication until we take the tasklist-lock. In particular, we do 23787b558513SDavid Herrmann * not want user-space to be able to predict the process start-time by 23797b558513SDavid Herrmann * stalling fork(2) after we recorded the start_time but before it is 23807b558513SDavid Herrmann * visible to the system. 23817b558513SDavid Herrmann */ 23827b558513SDavid Herrmann 23837b558513SDavid Herrmann p->start_time = ktime_get_ns(); 2384cf25e24dSPeter Zijlstra p->start_boottime = ktime_get_boottime_ns(); 23857b558513SDavid Herrmann 23867b558513SDavid Herrmann /* 238718c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 238818c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 238918c830dfSOleg Nesterov */ 23901da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 23932d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 23941da177e4SLinus Torvalds p->real_parent = current->real_parent; 23952d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 2396b4e00444SEddy Wu if (clone_flags & CLONE_THREAD) 2397b4e00444SEddy Wu p->exit_signal = -1; 2398b4e00444SEddy Wu else 2399b4e00444SEddy Wu p->exit_signal = current->group_leader->exit_signal; 24002d5516cbSOleg Nesterov } else { 24011da177e4SLinus Torvalds p->real_parent = current; 24022d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 2403b4e00444SEddy Wu p->exit_signal = args->exit_signal; 24042d5516cbSOleg Nesterov } 24051da177e4SLinus Torvalds 2406d83a7cb3SJosh Poimboeuf klp_copy_process(p); 2407d83a7cb3SJosh Poimboeuf 240885dd3f61SPeter Zijlstra sched_core_fork(p); 240985dd3f61SPeter Zijlstra 24101da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 24114a2c7a78SOleg Nesterov 24124a2c7a78SOleg Nesterov /* 2413dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 2414dbd95212SKees Cook * before holding sighand lock. 2415dbd95212SKees Cook */ 2416dbd95212SKees Cook copy_seccomp(p); 2417dbd95212SKees Cook 241879257534SDaniel Bristot de Oliveira rv_task_fork(p); 241979257534SDaniel Bristot de Oliveira 2420d7822b1eSMathieu Desnoyers rseq_fork(p, clone_flags); 2421d7822b1eSMathieu Desnoyers 24224ca1d3eeSEric W. Biederman /* Don't start children in a dying pid namespace */ 2423e8cfbc24SGargi Sharma if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { 24243fd37226SKirill Tkhai retval = -ENOMEM; 24253fd37226SKirill Tkhai goto bad_fork_cancel_cgroup; 24263fd37226SKirill Tkhai } 24274a2c7a78SOleg Nesterov 24287673bf55SEric W. Biederman /* Let kill terminate clone/fork in the middle */ 24297673bf55SEric W. Biederman if (fatal_signal_pending(current)) { 24307673bf55SEric W. Biederman retval = -EINTR; 24317673bf55SEric W. Biederman goto bad_fork_cancel_cgroup; 24327673bf55SEric W. Biederman } 24337673bf55SEric W. Biederman 24342c470475SEric W. Biederman init_task_pid_links(p); 243573b9ebfeSOleg Nesterov if (likely(p->pid)) { 24364b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 24371da177e4SLinus Torvalds 243881907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 24391da177e4SLinus Torvalds if (thread_group_leader(p)) { 24406883f81aSEric W. Biederman init_task_pid(p, PIDTYPE_TGID, pid); 244181907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 244281907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 244381907739SOleg Nesterov 24441c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 244517cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 24461c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 24471c4042c2SEric W. Biederman } 2448c3ad2c3bSEric W. Biederman p->signal->shared_pending.signal = delayed.signal; 24499c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 2450749860ceSPavel Tikhomirov /* 2451749860ceSPavel Tikhomirov * Inherit has_child_subreaper flag under the same 2452749860ceSPavel Tikhomirov * tasklist_lock with adding child to the process tree 2453749860ceSPavel Tikhomirov * for propagate_has_child_subreaper optimization. 2454749860ceSPavel Tikhomirov */ 2455749860ceSPavel Tikhomirov p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 2456749860ceSPavel Tikhomirov p->real_parent->signal->is_child_subreaper; 24579cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 24585e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 24596883f81aSEric W. Biederman attach_pid(p, PIDTYPE_TGID); 246081907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 246181907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 2462909ea964SChristoph Lameter __this_cpu_inc(process_counts); 246380628ca0SOleg Nesterov } else { 246480628ca0SOleg Nesterov current->signal->nr_threads++; 246580628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 246660d4de3fSElena Reshetova refcount_inc(¤t->signal->sigcnt); 2467924de3b8SEric W. Biederman task_join_group_stop(p); 246880628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 246980628ca0SOleg Nesterov &p->group_leader->thread_group); 24700c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 24710c740d0aSOleg Nesterov &p->signal->thread_head); 24721da177e4SLinus Torvalds } 247381907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 24741da177e4SLinus Torvalds nr_threads++; 247573b9ebfeSOleg Nesterov } 24761da177e4SLinus Torvalds total_forks++; 2477c3ad2c3bSEric W. Biederman hlist_del_init(&delayed.node); 24783f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 24794af4206bSOleg Nesterov syscall_tracepoint_update(p); 24801da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 24814af4206bSOleg Nesterov 2482ddc204b5SWaiman Long if (pidfile) 2483ddc204b5SWaiman Long fd_install(pidfd, pidfile); 2484ddc204b5SWaiman Long 2485c13cf856SAndrew Morton proc_fork_connector(p); 2486b1e82065SPeter Zijlstra sched_post_fork(p); 2487ef2c41cfSChristian Brauner cgroup_post_fork(p, args); 2488cdd6c482SIngo Molnar perf_event_fork(p); 248943d2b113SKAMEZAWA Hiroyuki 249043d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 24913ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 249243d2b113SKAMEZAWA Hiroyuki 249367197a4fSSuren Baghdasaryan copy_oom_score_adj(clone_flags, p); 249467197a4fSSuren Baghdasaryan 24951da177e4SLinus Torvalds return p; 24961da177e4SLinus Torvalds 24977e47682eSAleksa Sarai bad_fork_cancel_cgroup: 249885dd3f61SPeter Zijlstra sched_core_free(p); 24993fd37226SKirill Tkhai spin_unlock(¤t->sighand->siglock); 25003fd37226SKirill Tkhai write_unlock_irq(&tasklist_lock); 2501ef2c41cfSChristian Brauner cgroup_cancel_fork(p, args); 2502b3e58382SChristian Brauner bad_fork_put_pidfd: 25036fd2fe49SAl Viro if (clone_flags & CLONE_PIDFD) { 25046fd2fe49SAl Viro fput(pidfile); 25056fd2fe49SAl Viro put_unused_fd(pidfd); 25066fd2fe49SAl Viro } 2507425fb2b4SPavel Emelyanov bad_fork_free_pid: 2508425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 2509425fb2b4SPavel Emelyanov free_pid(pid); 25100740aa5fSJiri Slaby bad_fork_cleanup_thread: 25110740aa5fSJiri Slaby exit_thread(p); 2512fd0928dfSJens Axboe bad_fork_cleanup_io: 2513b69f2292SLouis Rilling if (p->io_context) 2514b69f2292SLouis Rilling exit_io_context(p); 2515ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 2516444f378bSLinus Torvalds exit_task_namespaces(p); 25171da177e4SLinus Torvalds bad_fork_cleanup_mm: 2518c3f3ce04SAndrea Arcangeli if (p->mm) { 2519c3f3ce04SAndrea Arcangeli mm_clear_owner(p->mm, p); 25201da177e4SLinus Torvalds mmput(p->mm); 2521c3f3ce04SAndrea Arcangeli } 25221da177e4SLinus Torvalds bad_fork_cleanup_signal: 25234ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 25241c5354deSMike Galbraith free_signal_struct(p->signal); 25251da177e4SLinus Torvalds bad_fork_cleanup_sighand: 2526a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 25271da177e4SLinus Torvalds bad_fork_cleanup_fs: 25281da177e4SLinus Torvalds exit_fs(p); /* blocking */ 25291da177e4SLinus Torvalds bad_fork_cleanup_files: 25301da177e4SLinus Torvalds exit_files(p); /* blocking */ 25311da177e4SLinus Torvalds bad_fork_cleanup_semundo: 25321da177e4SLinus Torvalds exit_sem(p); 2533e4e55b47STetsuo Handa bad_fork_cleanup_security: 2534e4e55b47STetsuo Handa security_task_free(p); 25351da177e4SLinus Torvalds bad_fork_cleanup_audit: 25361da177e4SLinus Torvalds audit_free(p); 25376c72e350SPeter Zijlstra bad_fork_cleanup_perf: 2538cdd6c482SIngo Molnar perf_event_free_task(p); 25396c72e350SPeter Zijlstra bad_fork_cleanup_policy: 2540b09be676SByungchul Park lockdep_free_task(p); 25411da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2542f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 25431da177e4SLinus Torvalds #endif 2544ff8288ffSEric W. Biederman bad_fork_cleanup_delayacct: 254535df17c5SShailabh Nagar delayacct_tsk_free(p); 25461da177e4SLinus Torvalds bad_fork_cleanup_count: 254721d1c5e3SAlexey Gladkov dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1); 2548e0e81739SDavid Howells exit_creds(p); 25491da177e4SLinus Torvalds bad_fork_free: 25502f064a59SPeter Zijlstra WRITE_ONCE(p->__state, TASK_DEAD); 25511a03d3f1SSebastian Andrzej Siewior exit_task_stack_account(p); 255268f24b08SAndy Lutomirski put_task_stack(p); 2553c3f3ce04SAndrea Arcangeli delayed_free_task(p); 2554fe7d37d1SOleg Nesterov fork_out: 2555c3ad2c3bSEric W. Biederman spin_lock_irq(¤t->sighand->siglock); 2556c3ad2c3bSEric W. Biederman hlist_del_init(&delayed.node); 2557c3ad2c3bSEric W. Biederman spin_unlock_irq(¤t->sighand->siglock); 2558fe7d37d1SOleg Nesterov return ERR_PTR(retval); 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds 25612c470475SEric W. Biederman static inline void init_idle_pids(struct task_struct *idle) 2562f106eee1SOleg Nesterov { 2563f106eee1SOleg Nesterov enum pid_type type; 2564f106eee1SOleg Nesterov 2565f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 25662c470475SEric W. Biederman INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */ 25672c470475SEric W. Biederman init_task_pid(idle, type, &init_struct_pid); 2568f106eee1SOleg Nesterov } 2569f106eee1SOleg Nesterov } 2570f106eee1SOleg Nesterov 257136cb0e1cSEric W. Biederman static int idle_dummy(void *dummy) 257236cb0e1cSEric W. Biederman { 257336cb0e1cSEric W. Biederman /* This function is never called */ 257436cb0e1cSEric W. Biederman return 0; 257536cb0e1cSEric W. Biederman } 257636cb0e1cSEric W. Biederman 2577f1a0a376SValentin Schneider struct task_struct * __init fork_idle(int cpu) 25781da177e4SLinus Torvalds { 257936c8b586SIngo Molnar struct task_struct *task; 25807f192e3cSChristian Brauner struct kernel_clone_args args = { 25817f192e3cSChristian Brauner .flags = CLONE_VM, 25825bd2e97cSEric W. Biederman .fn = &idle_dummy, 25835bd2e97cSEric W. Biederman .fn_arg = NULL, 2584343f4c49SEric W. Biederman .kthread = 1, 258536cb0e1cSEric W. Biederman .idle = 1, 25867f192e3cSChristian Brauner }; 25877f192e3cSChristian Brauner 25887f192e3cSChristian Brauner task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args); 2589f106eee1SOleg Nesterov if (!IS_ERR(task)) { 25902c470475SEric W. Biederman init_idle_pids(task); 25911da177e4SLinus Torvalds init_idle(task, cpu); 2592f106eee1SOleg Nesterov } 259373b9ebfeSOleg Nesterov 25941da177e4SLinus Torvalds return task; 25951da177e4SLinus Torvalds } 25961da177e4SLinus Torvalds 259713585fa0SNadav Amit struct mm_struct *copy_init_mm(void) 259813585fa0SNadav Amit { 259913585fa0SNadav Amit return dup_mm(NULL, &init_mm); 260013585fa0SNadav Amit } 260113585fa0SNadav Amit 26021da177e4SLinus Torvalds /* 2603cc440e87SJens Axboe * This is like kernel_clone(), but shaved down and tailored to just 2604cc440e87SJens Axboe * creating io_uring workers. It returns a created task, or an error pointer. 2605cc440e87SJens Axboe * The returned task is inactive, and the caller must fire it up through 2606cc440e87SJens Axboe * wake_up_new_task(p). All signals are blocked in the created task. 2607cc440e87SJens Axboe */ 2608cc440e87SJens Axboe struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node) 2609cc440e87SJens Axboe { 2610cc440e87SJens Axboe unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD| 2611cc440e87SJens Axboe CLONE_IO; 2612cc440e87SJens Axboe struct kernel_clone_args args = { 2613cc440e87SJens Axboe .flags = ((lower_32_bits(flags) | CLONE_VM | 2614cc440e87SJens Axboe CLONE_UNTRACED) & ~CSIGNAL), 2615cc440e87SJens Axboe .exit_signal = (lower_32_bits(flags) & CSIGNAL), 26165bd2e97cSEric W. Biederman .fn = fn, 26175bd2e97cSEric W. Biederman .fn_arg = arg, 2618cc440e87SJens Axboe .io_thread = 1, 2619cc440e87SJens Axboe }; 2620cc440e87SJens Axboe 2621b16b3855SJens Axboe return copy_process(NULL, 0, node, &args); 2622cc440e87SJens Axboe } 2623cc440e87SJens Axboe 2624cc440e87SJens Axboe /* 26251da177e4SLinus Torvalds * Ok, this is the main fork-routine. 26261da177e4SLinus Torvalds * 26271da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 26281da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 2629a0eb9abdSEugene Syromiatnikov * 2630a0eb9abdSEugene Syromiatnikov * args->exit_signal is expected to be checked for sanity by the caller. 26311da177e4SLinus Torvalds */ 2632cad6967aSChristian Brauner pid_t kernel_clone(struct kernel_clone_args *args) 26331da177e4SLinus Torvalds { 26347f192e3cSChristian Brauner u64 clone_flags = args->flags; 26359f5325aaSMarcos Paulo de Souza struct completion vfork; 26369f5325aaSMarcos Paulo de Souza struct pid *pid; 26371da177e4SLinus Torvalds struct task_struct *p; 26381da177e4SLinus Torvalds int trace = 0; 2639cad6967aSChristian Brauner pid_t nr; 26401da177e4SLinus Torvalds 2641bdff746aSAndrew Morton /* 26423af8588cSChristian Brauner * For legacy clone() calls, CLONE_PIDFD uses the parent_tid argument 26433af8588cSChristian Brauner * to return the pidfd. Hence, CLONE_PIDFD and CLONE_PARENT_SETTID are 26443af8588cSChristian Brauner * mutually exclusive. With clone3() CLONE_PIDFD has grown a separate 26453af8588cSChristian Brauner * field in struct clone_args and it still doesn't make sense to have 26463af8588cSChristian Brauner * them both point at the same memory location. Performing this check 26473af8588cSChristian Brauner * here has the advantage that we don't need to have a separate helper 26483af8588cSChristian Brauner * to check for legacy clone(). 26493af8588cSChristian Brauner */ 26503af8588cSChristian Brauner if ((args->flags & CLONE_PIDFD) && 26513af8588cSChristian Brauner (args->flags & CLONE_PARENT_SETTID) && 26523af8588cSChristian Brauner (args->pidfd == args->parent_tid)) 26533af8588cSChristian Brauner return -EINVAL; 26543af8588cSChristian Brauner 26553af8588cSChristian Brauner /* 26564b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 26574b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 26584b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 26594b9d33e6STejun Heo * for the type of forking is enabled. 266009a05394SRoland McGrath */ 2661e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 26624b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 26634b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 26647f192e3cSChristian Brauner else if (args->exit_signal != SIGCHLD) 26654b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 26664b9d33e6STejun Heo else 26674b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 26684b9d33e6STejun Heo 26694b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 26704b9d33e6STejun Heo trace = 0; 26714b9d33e6STejun Heo } 26721da177e4SLinus Torvalds 26737f192e3cSChristian Brauner p = copy_process(NULL, trace, NUMA_NO_NODE, args); 267438addce8SEmese Revfy add_latent_entropy(); 26759f5325aaSMarcos Paulo de Souza 26769f5325aaSMarcos Paulo de Souza if (IS_ERR(p)) 26779f5325aaSMarcos Paulo de Souza return PTR_ERR(p); 26789f5325aaSMarcos Paulo de Souza 26791da177e4SLinus Torvalds /* 26801da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 26811da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 26821da177e4SLinus Torvalds */ 26830a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 26840a16b607SMathieu Desnoyers 26854e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 26864e52365fSMatthew Dempsky nr = pid_vnr(pid); 268730e49c26SPavel Emelyanov 268830e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 26897f192e3cSChristian Brauner put_user(nr, args->parent_tid); 2690a6f5e063SSukadev Bhattiprolu 26911da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 26921da177e4SLinus Torvalds p->vfork_done = &vfork; 26931da177e4SLinus Torvalds init_completion(&vfork); 2694d68b46feSOleg Nesterov get_task_struct(p); 26951da177e4SLinus Torvalds } 26961da177e4SLinus Torvalds 2697bd74fdaeSYu Zhao if (IS_ENABLED(CONFIG_LRU_GEN) && !(clone_flags & CLONE_VM)) { 2698bd74fdaeSYu Zhao /* lock the task to synchronize with memcg migration */ 2699bd74fdaeSYu Zhao task_lock(p); 2700bd74fdaeSYu Zhao lru_gen_add_mm(p->mm); 2701bd74fdaeSYu Zhao task_unlock(p); 2702bd74fdaeSYu Zhao } 2703bd74fdaeSYu Zhao 27043e51e3edSSamir Bellabes wake_up_new_task(p); 27051da177e4SLinus Torvalds 27064b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 27074b9d33e6STejun Heo if (unlikely(trace)) 27084e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 270909a05394SRoland McGrath 27101da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 2711d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 27124e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 27139f59ce5dSChuck Ebbert } 27144e52365fSMatthew Dempsky 27154e52365fSMatthew Dempsky put_pid(pid); 271692476d7fSEric W. Biederman return nr; 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 27192aa3a7f8SAl Viro /* 27202aa3a7f8SAl Viro * Create a kernel thread. 27212aa3a7f8SAl Viro */ 27222aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 27232aa3a7f8SAl Viro { 27247f192e3cSChristian Brauner struct kernel_clone_args args = { 27253f2c788aSChristian Brauner .flags = ((lower_32_bits(flags) | CLONE_VM | 27263f2c788aSChristian Brauner CLONE_UNTRACED) & ~CSIGNAL), 27273f2c788aSChristian Brauner .exit_signal = (lower_32_bits(flags) & CSIGNAL), 27285bd2e97cSEric W. Biederman .fn = fn, 27295bd2e97cSEric W. Biederman .fn_arg = arg, 2730343f4c49SEric W. Biederman .kthread = 1, 2731343f4c49SEric W. Biederman }; 2732343f4c49SEric W. Biederman 2733343f4c49SEric W. Biederman return kernel_clone(&args); 2734343f4c49SEric W. Biederman } 2735343f4c49SEric W. Biederman 2736343f4c49SEric W. Biederman /* 2737343f4c49SEric W. Biederman * Create a user mode thread. 2738343f4c49SEric W. Biederman */ 2739343f4c49SEric W. Biederman pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags) 2740343f4c49SEric W. Biederman { 2741343f4c49SEric W. Biederman struct kernel_clone_args args = { 2742343f4c49SEric W. Biederman .flags = ((lower_32_bits(flags) | CLONE_VM | 2743343f4c49SEric W. Biederman CLONE_UNTRACED) & ~CSIGNAL), 2744343f4c49SEric W. Biederman .exit_signal = (lower_32_bits(flags) & CSIGNAL), 27455bd2e97cSEric W. Biederman .fn = fn, 27465bd2e97cSEric W. Biederman .fn_arg = arg, 27477f192e3cSChristian Brauner }; 27487f192e3cSChristian Brauner 2749cad6967aSChristian Brauner return kernel_clone(&args); 27502aa3a7f8SAl Viro } 27512aa3a7f8SAl Viro 2752d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 2753d2125043SAl Viro SYSCALL_DEFINE0(fork) 2754d2125043SAl Viro { 2755d2125043SAl Viro #ifdef CONFIG_MMU 27567f192e3cSChristian Brauner struct kernel_clone_args args = { 27577f192e3cSChristian Brauner .exit_signal = SIGCHLD, 27587f192e3cSChristian Brauner }; 27597f192e3cSChristian Brauner 2760cad6967aSChristian Brauner return kernel_clone(&args); 2761d2125043SAl Viro #else 2762d2125043SAl Viro /* can not support in nommu mode */ 27635d59e182SDaeseok Youn return -EINVAL; 2764d2125043SAl Viro #endif 2765d2125043SAl Viro } 2766d2125043SAl Viro #endif 2767d2125043SAl Viro 2768d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 2769d2125043SAl Viro SYSCALL_DEFINE0(vfork) 2770d2125043SAl Viro { 27717f192e3cSChristian Brauner struct kernel_clone_args args = { 27727f192e3cSChristian Brauner .flags = CLONE_VFORK | CLONE_VM, 27737f192e3cSChristian Brauner .exit_signal = SIGCHLD, 27747f192e3cSChristian Brauner }; 27757f192e3cSChristian Brauner 2776cad6967aSChristian Brauner return kernel_clone(&args); 2777d2125043SAl Viro } 2778d2125043SAl Viro #endif 2779d2125043SAl Viro 2780d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 2781d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 2782d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2783d2125043SAl Viro int __user *, parent_tidptr, 27843033f14aSJosh Triplett unsigned long, tls, 2785d2125043SAl Viro int __user *, child_tidptr) 2786d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 2787d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 2788d2125043SAl Viro int __user *, parent_tidptr, 2789d2125043SAl Viro int __user *, child_tidptr, 27903033f14aSJosh Triplett unsigned long, tls) 2791dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 2792dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 2793dfa9771aSMichal Simek int, stack_size, 2794dfa9771aSMichal Simek int __user *, parent_tidptr, 2795dfa9771aSMichal Simek int __user *, child_tidptr, 27963033f14aSJosh Triplett unsigned long, tls) 2797d2125043SAl Viro #else 2798d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2799d2125043SAl Viro int __user *, parent_tidptr, 2800d2125043SAl Viro int __user *, child_tidptr, 28013033f14aSJosh Triplett unsigned long, tls) 2802d2125043SAl Viro #endif 2803d2125043SAl Viro { 28047f192e3cSChristian Brauner struct kernel_clone_args args = { 28053f2c788aSChristian Brauner .flags = (lower_32_bits(clone_flags) & ~CSIGNAL), 28067f192e3cSChristian Brauner .pidfd = parent_tidptr, 28077f192e3cSChristian Brauner .child_tid = child_tidptr, 28087f192e3cSChristian Brauner .parent_tid = parent_tidptr, 28093f2c788aSChristian Brauner .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL), 28107f192e3cSChristian Brauner .stack = newsp, 28117f192e3cSChristian Brauner .tls = tls, 28127f192e3cSChristian Brauner }; 28137f192e3cSChristian Brauner 2814cad6967aSChristian Brauner return kernel_clone(&args); 28157f192e3cSChristian Brauner } 2816d68dbb0cSChristian Brauner #endif 28177f192e3cSChristian Brauner 2818d68dbb0cSChristian Brauner #ifdef __ARCH_WANT_SYS_CLONE3 2819dd499f7aSAmanieu d'Antras 28207f192e3cSChristian Brauner noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, 28217f192e3cSChristian Brauner struct clone_args __user *uargs, 2822f14c234bSAleksa Sarai size_t usize) 28237f192e3cSChristian Brauner { 2824f14c234bSAleksa Sarai int err; 28257f192e3cSChristian Brauner struct clone_args args; 282649cb2fc4SAdrian Reber pid_t *kset_tid = kargs->set_tid; 28277f192e3cSChristian Brauner 2828a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, tls) != 2829a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER0); 2830a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, set_tid_size) != 2831a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER1); 2832a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) != 2833a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER2); 2834a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2); 2835a966dcfeSEugene Syromiatnikov 2836f14c234bSAleksa Sarai if (unlikely(usize > PAGE_SIZE)) 28377f192e3cSChristian Brauner return -E2BIG; 2838f14c234bSAleksa Sarai if (unlikely(usize < CLONE_ARGS_SIZE_VER0)) 28397f192e3cSChristian Brauner return -EINVAL; 28407f192e3cSChristian Brauner 2841f14c234bSAleksa Sarai err = copy_struct_from_user(&args, sizeof(args), uargs, usize); 2842f14c234bSAleksa Sarai if (err) 2843f14c234bSAleksa Sarai return err; 28447f192e3cSChristian Brauner 284549cb2fc4SAdrian Reber if (unlikely(args.set_tid_size > MAX_PID_NS_LEVEL)) 284649cb2fc4SAdrian Reber return -EINVAL; 284749cb2fc4SAdrian Reber 284849cb2fc4SAdrian Reber if (unlikely(!args.set_tid && args.set_tid_size > 0)) 284949cb2fc4SAdrian Reber return -EINVAL; 285049cb2fc4SAdrian Reber 285149cb2fc4SAdrian Reber if (unlikely(args.set_tid && args.set_tid_size == 0)) 285249cb2fc4SAdrian Reber return -EINVAL; 285349cb2fc4SAdrian Reber 2854a0eb9abdSEugene Syromiatnikov /* 2855a0eb9abdSEugene Syromiatnikov * Verify that higher 32bits of exit_signal are unset and that 2856a0eb9abdSEugene Syromiatnikov * it is a valid signal 2857a0eb9abdSEugene Syromiatnikov */ 2858a0eb9abdSEugene Syromiatnikov if (unlikely((args.exit_signal & ~((u64)CSIGNAL)) || 2859a0eb9abdSEugene Syromiatnikov !valid_signal(args.exit_signal))) 2860a0eb9abdSEugene Syromiatnikov return -EINVAL; 2861a0eb9abdSEugene Syromiatnikov 286262173872SEugene Syromiatnikov if ((args.flags & CLONE_INTO_CGROUP) && 286362173872SEugene Syromiatnikov (args.cgroup > INT_MAX || usize < CLONE_ARGS_SIZE_VER2)) 2864ef2c41cfSChristian Brauner return -EINVAL; 2865ef2c41cfSChristian Brauner 28667f192e3cSChristian Brauner *kargs = (struct kernel_clone_args){ 28677f192e3cSChristian Brauner .flags = args.flags, 28687f192e3cSChristian Brauner .pidfd = u64_to_user_ptr(args.pidfd), 28697f192e3cSChristian Brauner .child_tid = u64_to_user_ptr(args.child_tid), 28707f192e3cSChristian Brauner .parent_tid = u64_to_user_ptr(args.parent_tid), 28717f192e3cSChristian Brauner .exit_signal = args.exit_signal, 28727f192e3cSChristian Brauner .stack = args.stack, 28737f192e3cSChristian Brauner .stack_size = args.stack_size, 28747f192e3cSChristian Brauner .tls = args.tls, 287549cb2fc4SAdrian Reber .set_tid_size = args.set_tid_size, 2876ef2c41cfSChristian Brauner .cgroup = args.cgroup, 28777f192e3cSChristian Brauner }; 28787f192e3cSChristian Brauner 287949cb2fc4SAdrian Reber if (args.set_tid && 288049cb2fc4SAdrian Reber copy_from_user(kset_tid, u64_to_user_ptr(args.set_tid), 288149cb2fc4SAdrian Reber (kargs->set_tid_size * sizeof(pid_t)))) 288249cb2fc4SAdrian Reber return -EFAULT; 288349cb2fc4SAdrian Reber 288449cb2fc4SAdrian Reber kargs->set_tid = kset_tid; 288549cb2fc4SAdrian Reber 28867f192e3cSChristian Brauner return 0; 28877f192e3cSChristian Brauner } 28887f192e3cSChristian Brauner 2889fa729c4dSChristian Brauner /** 2890fa729c4dSChristian Brauner * clone3_stack_valid - check and prepare stack 2891fa729c4dSChristian Brauner * @kargs: kernel clone args 2892fa729c4dSChristian Brauner * 2893fa729c4dSChristian Brauner * Verify that the stack arguments userspace gave us are sane. 2894fa729c4dSChristian Brauner * In addition, set the stack direction for userspace since it's easy for us to 2895fa729c4dSChristian Brauner * determine. 2896fa729c4dSChristian Brauner */ 2897fa729c4dSChristian Brauner static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) 2898fa729c4dSChristian Brauner { 2899fa729c4dSChristian Brauner if (kargs->stack == 0) { 2900fa729c4dSChristian Brauner if (kargs->stack_size > 0) 2901fa729c4dSChristian Brauner return false; 2902fa729c4dSChristian Brauner } else { 2903fa729c4dSChristian Brauner if (kargs->stack_size == 0) 2904fa729c4dSChristian Brauner return false; 2905fa729c4dSChristian Brauner 2906fa729c4dSChristian Brauner if (!access_ok((void __user *)kargs->stack, kargs->stack_size)) 2907fa729c4dSChristian Brauner return false; 2908fa729c4dSChristian Brauner 2909fa729c4dSChristian Brauner #if !defined(CONFIG_STACK_GROWSUP) && !defined(CONFIG_IA64) 2910fa729c4dSChristian Brauner kargs->stack += kargs->stack_size; 2911fa729c4dSChristian Brauner #endif 2912fa729c4dSChristian Brauner } 2913fa729c4dSChristian Brauner 2914fa729c4dSChristian Brauner return true; 2915fa729c4dSChristian Brauner } 2916fa729c4dSChristian Brauner 2917fa729c4dSChristian Brauner static bool clone3_args_valid(struct kernel_clone_args *kargs) 29187f192e3cSChristian Brauner { 2919b612e5dfSChristian Brauner /* Verify that no unknown flags are passed along. */ 2920ef2c41cfSChristian Brauner if (kargs->flags & 2921ef2c41cfSChristian Brauner ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP)) 29227f192e3cSChristian Brauner return false; 29237f192e3cSChristian Brauner 29247f192e3cSChristian Brauner /* 2925a8ca6b13SXiaofeng Cao * - make the CLONE_DETACHED bit reusable for clone3 2926a8ca6b13SXiaofeng Cao * - make the CSIGNAL bits reusable for clone3 29277f192e3cSChristian Brauner */ 29287f192e3cSChristian Brauner if (kargs->flags & (CLONE_DETACHED | CSIGNAL)) 29297f192e3cSChristian Brauner return false; 29307f192e3cSChristian Brauner 2931b612e5dfSChristian Brauner if ((kargs->flags & (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) == 2932b612e5dfSChristian Brauner (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) 2933b612e5dfSChristian Brauner return false; 2934b612e5dfSChristian Brauner 29357f192e3cSChristian Brauner if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) && 29367f192e3cSChristian Brauner kargs->exit_signal) 29377f192e3cSChristian Brauner return false; 29387f192e3cSChristian Brauner 2939fa729c4dSChristian Brauner if (!clone3_stack_valid(kargs)) 2940fa729c4dSChristian Brauner return false; 2941fa729c4dSChristian Brauner 29427f192e3cSChristian Brauner return true; 29437f192e3cSChristian Brauner } 29447f192e3cSChristian Brauner 2945501bd016SChristian Brauner /** 2946501bd016SChristian Brauner * clone3 - create a new process with specific properties 2947501bd016SChristian Brauner * @uargs: argument structure 2948501bd016SChristian Brauner * @size: size of @uargs 2949501bd016SChristian Brauner * 2950501bd016SChristian Brauner * clone3() is the extensible successor to clone()/clone2(). 2951501bd016SChristian Brauner * It takes a struct as argument that is versioned by its size. 2952501bd016SChristian Brauner * 2953501bd016SChristian Brauner * Return: On success, a positive PID for the child process. 2954501bd016SChristian Brauner * On error, a negative errno number. 2955501bd016SChristian Brauner */ 29567f192e3cSChristian Brauner SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size) 29577f192e3cSChristian Brauner { 29587f192e3cSChristian Brauner int err; 29597f192e3cSChristian Brauner 29607f192e3cSChristian Brauner struct kernel_clone_args kargs; 296149cb2fc4SAdrian Reber pid_t set_tid[MAX_PID_NS_LEVEL]; 296249cb2fc4SAdrian Reber 296349cb2fc4SAdrian Reber kargs.set_tid = set_tid; 29647f192e3cSChristian Brauner 29657f192e3cSChristian Brauner err = copy_clone_args_from_user(&kargs, uargs, size); 29667f192e3cSChristian Brauner if (err) 29677f192e3cSChristian Brauner return err; 29687f192e3cSChristian Brauner 29697f192e3cSChristian Brauner if (!clone3_args_valid(&kargs)) 29707f192e3cSChristian Brauner return -EINVAL; 29717f192e3cSChristian Brauner 2972cad6967aSChristian Brauner return kernel_clone(&kargs); 2973d2125043SAl Viro } 2974d2125043SAl Viro #endif 2975d2125043SAl Viro 29760f1b92cbSOleg Nesterov void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) 29770f1b92cbSOleg Nesterov { 29780f1b92cbSOleg Nesterov struct task_struct *leader, *parent, *child; 29790f1b92cbSOleg Nesterov int res; 29800f1b92cbSOleg Nesterov 29810f1b92cbSOleg Nesterov read_lock(&tasklist_lock); 29820f1b92cbSOleg Nesterov leader = top = top->group_leader; 29830f1b92cbSOleg Nesterov down: 29840f1b92cbSOleg Nesterov for_each_thread(leader, parent) { 29850f1b92cbSOleg Nesterov list_for_each_entry(child, &parent->children, sibling) { 29860f1b92cbSOleg Nesterov res = visitor(child, data); 29870f1b92cbSOleg Nesterov if (res) { 29880f1b92cbSOleg Nesterov if (res < 0) 29890f1b92cbSOleg Nesterov goto out; 29900f1b92cbSOleg Nesterov leader = child; 29910f1b92cbSOleg Nesterov goto down; 29920f1b92cbSOleg Nesterov } 29930f1b92cbSOleg Nesterov up: 29940f1b92cbSOleg Nesterov ; 29950f1b92cbSOleg Nesterov } 29960f1b92cbSOleg Nesterov } 29970f1b92cbSOleg Nesterov 29980f1b92cbSOleg Nesterov if (leader != top) { 29990f1b92cbSOleg Nesterov child = leader; 30000f1b92cbSOleg Nesterov parent = child->real_parent; 30010f1b92cbSOleg Nesterov leader = parent->group_leader; 30020f1b92cbSOleg Nesterov goto up; 30030f1b92cbSOleg Nesterov } 30040f1b92cbSOleg Nesterov out: 30050f1b92cbSOleg Nesterov read_unlock(&tasklist_lock); 30060f1b92cbSOleg Nesterov } 30070f1b92cbSOleg Nesterov 30085fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 30095fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 30105fd63b30SRavikiran G Thirumalai #endif 30115fd63b30SRavikiran G Thirumalai 301251cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 3013aa1757f9SOleg Nesterov { 3014aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 3015aa1757f9SOleg Nesterov 3016aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 3017b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 3018fba2afaaSDavide Libenzi } 3019aa1757f9SOleg Nesterov 30201da177e4SLinus Torvalds void __init proc_caches_init(void) 30211da177e4SLinus Torvalds { 3022c1a2f7f0SRik van Riel unsigned int mm_size; 3023c1a2f7f0SRik van Riel 30241da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 30251da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 30265f0d5a3aSPaul E. McKenney SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| 302775f296d9SLevin, Alexander (Sasha Levin) SLAB_ACCOUNT, sighand_ctor); 30281da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 30291da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 303075f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 30315d097056SVladimir Davydov NULL); 30321da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 30331da177e4SLinus Torvalds sizeof(struct files_struct), 0, 303475f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 30355d097056SVladimir Davydov NULL); 30361da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 30371da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 303875f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 30395d097056SVladimir Davydov NULL); 3040c1a2f7f0SRik van Riel 30416345d24dSLinus Torvalds /* 3042c1a2f7f0SRik van Riel * The mm_cpumask is located at the end of mm_struct, and is 3043c1a2f7f0SRik van Riel * dynamically sized based on the maximum CPU number this system 3044c1a2f7f0SRik van Riel * can have, taking hotplug into account (nr_cpu_ids). 30456345d24dSLinus Torvalds */ 3046c1a2f7f0SRik van Riel mm_size = sizeof(struct mm_struct) + cpumask_size(); 3047c1a2f7f0SRik van Riel 304807dcd7feSDavid Windsor mm_cachep = kmem_cache_create_usercopy("mm_struct", 3049c1a2f7f0SRik van Riel mm_size, ARCH_MIN_MMSTRUCT_ALIGN, 305075f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 305107dcd7feSDavid Windsor offsetof(struct mm_struct, saved_auxv), 305207dcd7feSDavid Windsor sizeof_field(struct mm_struct, saved_auxv), 30535d097056SVladimir Davydov NULL); 30545d097056SVladimir Davydov vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 30558feae131SDavid Howells mmap_init(); 305666577193SAl Viro nsproxy_cache_init(); 30571da177e4SLinus Torvalds } 3058cf2e340fSJANAK DESAI 3059cf2e340fSJANAK DESAI /* 30609bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 3061cf2e340fSJANAK DESAI */ 30629bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 3063cf2e340fSJANAK DESAI { 30649bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 30659bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 306650804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 3067769071acSAndrei Vagin CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP| 3068769071acSAndrei Vagin CLONE_NEWTIME)) 3069cf2e340fSJANAK DESAI return -EINVAL; 30709bfb23fcSOleg Nesterov /* 307112c641abSEric W. Biederman * Not implemented, but pretend it works if there is nothing 307212c641abSEric W. Biederman * to unshare. Note that unsharing the address space or the 307312c641abSEric W. Biederman * signal handlers also need to unshare the signal queues (aka 307412c641abSEric W. Biederman * CLONE_THREAD). 30759bfb23fcSOleg Nesterov */ 30769bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 307712c641abSEric W. Biederman if (!thread_group_empty(current)) 307812c641abSEric W. Biederman return -EINVAL; 307912c641abSEric W. Biederman } 308012c641abSEric W. Biederman if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 3081d036bda7SElena Reshetova if (refcount_read(¤t->sighand->count) > 1) 308212c641abSEric W. Biederman return -EINVAL; 308312c641abSEric W. Biederman } 308412c641abSEric W. Biederman if (unshare_flags & CLONE_VM) { 308512c641abSEric W. Biederman if (!current_is_single_threaded()) 30869bfb23fcSOleg Nesterov return -EINVAL; 30879bfb23fcSOleg Nesterov } 3088cf2e340fSJANAK DESAI 3089cf2e340fSJANAK DESAI return 0; 3090cf2e340fSJANAK DESAI } 3091cf2e340fSJANAK DESAI 3092cf2e340fSJANAK DESAI /* 309399d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 3094cf2e340fSJANAK DESAI */ 3095cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 3096cf2e340fSJANAK DESAI { 3097cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 3098cf2e340fSJANAK DESAI 3099498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 3100498052bbSAl Viro return 0; 3101498052bbSAl Viro 3102498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 3103498052bbSAl Viro if (fs->users == 1) 3104498052bbSAl Viro return 0; 3105498052bbSAl Viro 3106498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 310799d1419dSJANAK DESAI if (!*new_fsp) 310899d1419dSJANAK DESAI return -ENOMEM; 3109cf2e340fSJANAK DESAI 3110cf2e340fSJANAK DESAI return 0; 3111cf2e340fSJANAK DESAI } 3112cf2e340fSJANAK DESAI 3113cf2e340fSJANAK DESAI /* 3114a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 3115cf2e340fSJANAK DESAI */ 311660997c3dSChristian Brauner int unshare_fd(unsigned long unshare_flags, unsigned int max_fds, 311760997c3dSChristian Brauner struct files_struct **new_fdp) 3118cf2e340fSJANAK DESAI { 3119cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 3120a016f338SJANAK DESAI int error = 0; 3121cf2e340fSJANAK DESAI 3122cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 3123a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 312460997c3dSChristian Brauner *new_fdp = dup_fd(fd, max_fds, &error); 3125a016f338SJANAK DESAI if (!*new_fdp) 3126a016f338SJANAK DESAI return error; 3127a016f338SJANAK DESAI } 3128cf2e340fSJANAK DESAI 3129cf2e340fSJANAK DESAI return 0; 3130cf2e340fSJANAK DESAI } 3131cf2e340fSJANAK DESAI 3132cf2e340fSJANAK DESAI /* 3133cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 3134cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 3135cad6967aSChristian Brauner * functions used by kernel_clone() cannot be used here directly 3136cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 3137cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 3138cf2e340fSJANAK DESAI * task_struct. 3139cf2e340fSJANAK DESAI */ 31409b32105eSDominik Brodowski int ksys_unshare(unsigned long unshare_flags) 3141cf2e340fSJANAK DESAI { 3142cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 3143ba1f70ddSRan Xiaokai struct files_struct *new_fd = NULL; 3144b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 3145cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 31469edff4abSManfred Spraul int do_sysvsem = 0; 31479bfb23fcSOleg Nesterov int err; 3148cf2e340fSJANAK DESAI 314950804fe3SEric W. Biederman /* 3150faf00da5SEric W. Biederman * If unsharing a user namespace must also unshare the thread group 3151faf00da5SEric W. Biederman * and unshare the filesystem root and working directories. 3152b2e0d987SEric W. Biederman */ 3153b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 3154e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 3155b2e0d987SEric W. Biederman /* 315650804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 315750804fe3SEric W. Biederman */ 315850804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 315950804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 31606013f67fSManfred Spraul /* 316112c641abSEric W. Biederman * If unsharing a signal handlers, must also unshare the signal queues. 316212c641abSEric W. Biederman */ 316312c641abSEric W. Biederman if (unshare_flags & CLONE_SIGHAND) 316412c641abSEric W. Biederman unshare_flags |= CLONE_THREAD; 316512c641abSEric W. Biederman /* 31669bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 31679bfb23fcSOleg Nesterov */ 31689bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 31699bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 317050804fe3SEric W. Biederman 317150804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 317250804fe3SEric W. Biederman if (err) 317350804fe3SEric W. Biederman goto bad_unshare_out; 31749bfb23fcSOleg Nesterov /* 31756013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 31766013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 31776013f67fSManfred Spraul * namespace are unreachable. 31786013f67fSManfred Spraul */ 31796013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 31809edff4abSManfred Spraul do_sysvsem = 1; 3181fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 3182fb0a685cSDaniel Rebelo de Oliveira if (err) 31839bfb23fcSOleg Nesterov goto bad_unshare_out; 318460997c3dSChristian Brauner err = unshare_fd(unshare_flags, NR_OPEN_MAX, &new_fd); 3185fb0a685cSDaniel Rebelo de Oliveira if (err) 31869bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 3187b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 3188fb0a685cSDaniel Rebelo de Oliveira if (err) 31899edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 3190b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 3191b2e0d987SEric W. Biederman new_cred, new_fs); 3192b2e0d987SEric W. Biederman if (err) 3193b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 3194cf2e340fSJANAK DESAI 3195905ae01cSAlexey Gladkov if (new_cred) { 3196905ae01cSAlexey Gladkov err = set_cred_ucounts(new_cred); 3197905ae01cSAlexey Gladkov if (err) 3198905ae01cSAlexey Gladkov goto bad_unshare_cleanup_cred; 3199905ae01cSAlexey Gladkov } 3200905ae01cSAlexey Gladkov 3201b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 32029edff4abSManfred Spraul if (do_sysvsem) { 32039edff4abSManfred Spraul /* 32049edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 32059edff4abSManfred Spraul */ 32069edff4abSManfred Spraul exit_sem(current); 32079edff4abSManfred Spraul } 3208ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 3209ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 3210ab602f79SJack Miller exit_shm(current); 3211ab602f79SJack Miller shm_init_task(current); 3212ab602f79SJack Miller } 3213ab516013SSerge E. Hallyn 32146f977e6bSAlan Cox if (new_nsproxy) 3215cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 3216cf2e340fSJANAK DESAI 3217cf7b708cSPavel Emelyanov task_lock(current); 3218cf7b708cSPavel Emelyanov 3219cf2e340fSJANAK DESAI if (new_fs) { 3220cf2e340fSJANAK DESAI fs = current->fs; 32212a4419b5SNick Piggin spin_lock(&fs->lock); 3222cf2e340fSJANAK DESAI current->fs = new_fs; 3223498052bbSAl Viro if (--fs->users) 3224498052bbSAl Viro new_fs = NULL; 3225498052bbSAl Viro else 3226cf2e340fSJANAK DESAI new_fs = fs; 32272a4419b5SNick Piggin spin_unlock(&fs->lock); 3228cf2e340fSJANAK DESAI } 3229cf2e340fSJANAK DESAI 3230ba1f70ddSRan Xiaokai if (new_fd) 3231ba1f70ddSRan Xiaokai swap(current->files, new_fd); 3232cf2e340fSJANAK DESAI 3233cf2e340fSJANAK DESAI task_unlock(current); 3234b2e0d987SEric W. Biederman 3235b2e0d987SEric W. Biederman if (new_cred) { 3236b2e0d987SEric W. Biederman /* Install the new user namespace */ 3237b2e0d987SEric W. Biederman commit_creds(new_cred); 3238b2e0d987SEric W. Biederman new_cred = NULL; 3239b2e0d987SEric W. Biederman } 3240cf2e340fSJANAK DESAI } 3241cf2e340fSJANAK DESAI 3242e4222673SHari Bathini perf_event_namespaces(current); 3243e4222673SHari Bathini 3244b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 3245b2e0d987SEric W. Biederman if (new_cred) 3246b2e0d987SEric W. Biederman put_cred(new_cred); 3247cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 3248cf2e340fSJANAK DESAI if (new_fd) 3249cf2e340fSJANAK DESAI put_files_struct(new_fd); 3250cf2e340fSJANAK DESAI 3251cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 3252cf2e340fSJANAK DESAI if (new_fs) 3253498052bbSAl Viro free_fs_struct(new_fs); 3254cf2e340fSJANAK DESAI 3255cf2e340fSJANAK DESAI bad_unshare_out: 3256cf2e340fSJANAK DESAI return err; 3257cf2e340fSJANAK DESAI } 32583b125388SAl Viro 32599b32105eSDominik Brodowski SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 32609b32105eSDominik Brodowski { 32619b32105eSDominik Brodowski return ksys_unshare(unshare_flags); 32629b32105eSDominik Brodowski } 32639b32105eSDominik Brodowski 32643b125388SAl Viro /* 32653b125388SAl Viro * Helper to unshare the files of the current task. 32663b125388SAl Viro * We don't want to expose copy_files internals to 32673b125388SAl Viro * the exec layer of the kernel. 32683b125388SAl Viro */ 32693b125388SAl Viro 32701f702603SEric W. Biederman int unshare_files(void) 32713b125388SAl Viro { 32723b125388SAl Viro struct task_struct *task = current; 32731f702603SEric W. Biederman struct files_struct *old, *copy = NULL; 32743b125388SAl Viro int error; 32753b125388SAl Viro 327660997c3dSChristian Brauner error = unshare_fd(CLONE_FILES, NR_OPEN_MAX, ©); 32771f702603SEric W. Biederman if (error || !copy) 32783b125388SAl Viro return error; 32791f702603SEric W. Biederman 32801f702603SEric W. Biederman old = task->files; 32813b125388SAl Viro task_lock(task); 32823b125388SAl Viro task->files = copy; 32833b125388SAl Viro task_unlock(task); 32841f702603SEric W. Biederman put_files_struct(old); 32853b125388SAl Viro return 0; 32863b125388SAl Viro } 328716db3d3fSHeinrich Schuchardt 328816db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write, 3289b0daa2c7STobias Klauser void *buffer, size_t *lenp, loff_t *ppos) 329016db3d3fSHeinrich Schuchardt { 329116db3d3fSHeinrich Schuchardt struct ctl_table t; 329216db3d3fSHeinrich Schuchardt int ret; 329316db3d3fSHeinrich Schuchardt int threads = max_threads; 3294b0f53dbcSMichal Hocko int min = 1; 329516db3d3fSHeinrich Schuchardt int max = MAX_THREADS; 329616db3d3fSHeinrich Schuchardt 329716db3d3fSHeinrich Schuchardt t = *table; 329816db3d3fSHeinrich Schuchardt t.data = &threads; 329916db3d3fSHeinrich Schuchardt t.extra1 = &min; 330016db3d3fSHeinrich Schuchardt t.extra2 = &max; 330116db3d3fSHeinrich Schuchardt 330216db3d3fSHeinrich Schuchardt ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 330316db3d3fSHeinrich Schuchardt if (ret || !write) 330416db3d3fSHeinrich Schuchardt return ret; 330516db3d3fSHeinrich Schuchardt 3306b0f53dbcSMichal Hocko max_threads = threads; 330716db3d3fSHeinrich Schuchardt 330816db3d3fSHeinrich Schuchardt return 0; 330916db3d3fSHeinrich Schuchardt } 3310