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> 45615d6e87SDavidlohr Bueso #include <linux/vmacache.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> 79fd0928dfSJens Axboe #include <linux/blkdev.h> 805ad4e53bSAl Viro #include <linux/fs_struct.h> 817c9f8861SEric Sandeen #include <linux/magic.h> 82cdd6c482SIngo Molnar #include <linux/perf_event.h> 8342c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 848e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 853d5992d2SYing Han #include <linux/oom.h> 86ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 87d80e731eSOleg Nesterov #include <linux/signalfd.h> 880326f5a9SSrikar Dronamraju #include <linux/uprobes.h> 89a27bb332SKent Overstreet #include <linux/aio.h> 9052f5684cSGideon Israel Dsouza #include <linux/compiler.h> 9116db3d3fSHeinrich Schuchardt #include <linux/sysctl.h> 925c9a8750SDmitry Vyukov #include <linux/kcov.h> 93d83a7cb3SJosh Poimboeuf #include <linux/livepatch.h> 9448ac3c18SMark Rutland #include <linux/thread_info.h> 95afaef01cSAlexander Popov #include <linux/stackleak.h> 96eafb149eSDaniel Axtens #include <linux/kasan.h> 97d08b9f0cSSami Tolvanen #include <linux/scs.h> 980f212204SJens Axboe #include <linux/io_uring.h> 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #include <asm/pgalloc.h> 1017c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 1021da177e4SLinus Torvalds #include <asm/mmu_context.h> 1031da177e4SLinus Torvalds #include <asm/cacheflush.h> 1041da177e4SLinus Torvalds #include <asm/tlbflush.h> 1051da177e4SLinus Torvalds 106ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 107ad8d75ffSSteven Rostedt 10843d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 10943d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 11043d2b113SKAMEZAWA Hiroyuki 1111da177e4SLinus Torvalds /* 112ac1b398dSHeinrich Schuchardt * Minimum number of threads to boot the kernel 113ac1b398dSHeinrich Schuchardt */ 114ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20 115ac1b398dSHeinrich Schuchardt 116ac1b398dSHeinrich Schuchardt /* 117ac1b398dSHeinrich Schuchardt * Maximum number of threads 118ac1b398dSHeinrich Schuchardt */ 119ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK 120ac1b398dSHeinrich Schuchardt 121ac1b398dSHeinrich Schuchardt /* 1221da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 1231da177e4SLinus Torvalds */ 1241da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 1251da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 1261da177e4SLinus Torvalds 1278856ae4dSKefeng Wang static int max_threads; /* tunable limit on nr_threads */ 1281da177e4SLinus Torvalds 1298495f7e6SSai Praneeth Prakhya #define NAMED_ARRAY_INDEX(x) [x] = __stringify(x) 1308495f7e6SSai Praneeth Prakhya 1318495f7e6SSai Praneeth Prakhya static const char * const resident_page_types[] = { 1328495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_FILEPAGES), 1338495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_ANONPAGES), 1348495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_SWAPENTS), 1358495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_SHMEMPAGES), 1368495f7e6SSai Praneeth Prakhya }; 1378495f7e6SSai Praneeth Prakhya 1381da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 141db1466b3SPaul E. McKenney 142db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 143db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 144db1466b3SPaul E. McKenney { 145db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 146db1466b3SPaul E. McKenney } 147db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 148db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds int nr_processes(void) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds int cpu; 1531da177e4SLinus Torvalds int total = 0; 1541da177e4SLinus Torvalds 1551d510750SIan Campbell for_each_possible_cpu(cpu) 1561da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds return total; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 161f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 162f19b9f74SAkinobu Mita { 163f19b9f74SAkinobu Mita } 164f19b9f74SAkinobu Mita 165f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 166e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 16741101809SThomas Gleixner 16841101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 16941101809SThomas Gleixner { 17041101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 17141101809SThomas Gleixner } 17241101809SThomas Gleixner 17341101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 17441101809SThomas Gleixner { 17541101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 17641101809SThomas Gleixner } 1771da177e4SLinus Torvalds #endif 1781da177e4SLinus Torvalds 179b235beeaSLinus Torvalds #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR 18041101809SThomas Gleixner 1810d15d74aSThomas Gleixner /* 1820d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1830d15d74aSThomas Gleixner * kmemcache based allocator. 1840d15d74aSThomas Gleixner */ 185ba14a194SAndy Lutomirski # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) 186ac496bf4SAndy Lutomirski 187ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 188ac496bf4SAndy Lutomirski /* 189ac496bf4SAndy Lutomirski * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 190ac496bf4SAndy Lutomirski * flush. Try to minimize the number of calls by caching stacks. 191ac496bf4SAndy Lutomirski */ 192ac496bf4SAndy Lutomirski #define NR_CACHED_STACKS 2 193ac496bf4SAndy Lutomirski static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 19419659c59SHoeun Ryu 19519659c59SHoeun Ryu static int free_vm_stack_cache(unsigned int cpu) 19619659c59SHoeun Ryu { 19719659c59SHoeun Ryu struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu); 19819659c59SHoeun Ryu int i; 19919659c59SHoeun Ryu 20019659c59SHoeun Ryu for (i = 0; i < NR_CACHED_STACKS; i++) { 20119659c59SHoeun Ryu struct vm_struct *vm_stack = cached_vm_stacks[i]; 20219659c59SHoeun Ryu 20319659c59SHoeun Ryu if (!vm_stack) 20419659c59SHoeun Ryu continue; 20519659c59SHoeun Ryu 20619659c59SHoeun Ryu vfree(vm_stack->addr); 20719659c59SHoeun Ryu cached_vm_stacks[i] = NULL; 20819659c59SHoeun Ryu } 20919659c59SHoeun Ryu 21019659c59SHoeun Ryu return 0; 21119659c59SHoeun Ryu } 212ac496bf4SAndy Lutomirski #endif 213ac496bf4SAndy Lutomirski 214ba14a194SAndy Lutomirski static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) 215b69c49b7SFUJITA Tomonori { 216ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 217ac496bf4SAndy Lutomirski void *stack; 218ac496bf4SAndy Lutomirski int i; 219ac496bf4SAndy Lutomirski 220ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 221112166f8SChristoph Lameter struct vm_struct *s; 222112166f8SChristoph Lameter 223112166f8SChristoph Lameter s = this_cpu_xchg(cached_stacks[i], NULL); 224ac496bf4SAndy Lutomirski 225ac496bf4SAndy Lutomirski if (!s) 226ac496bf4SAndy Lutomirski continue; 227ac496bf4SAndy Lutomirski 228eafb149eSDaniel Axtens /* Clear the KASAN shadow of the stack. */ 229eafb149eSDaniel Axtens kasan_unpoison_shadow(s->addr, THREAD_SIZE); 230eafb149eSDaniel Axtens 231ca182551SKonstantin Khlebnikov /* Clear stale pointers from reused stack. */ 232ca182551SKonstantin Khlebnikov memset(s->addr, 0, THREAD_SIZE); 233e01e8063SKees Cook 234ac496bf4SAndy Lutomirski tsk->stack_vm_area = s; 235ba4a4574SShakeel Butt tsk->stack = s->addr; 236ac496bf4SAndy Lutomirski return s->addr; 237ac496bf4SAndy Lutomirski } 238ac496bf4SAndy Lutomirski 2399b6f7e16SRoman Gushchin /* 2409b6f7e16SRoman Gushchin * Allocated stacks are cached and later reused by new threads, 2419b6f7e16SRoman Gushchin * so memcg accounting is performed manually on assigning/releasing 2429b6f7e16SRoman Gushchin * stacks to tasks. Drop __GFP_ACCOUNT. 2439b6f7e16SRoman Gushchin */ 24448ac3c18SMark Rutland stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, 245ba14a194SAndy Lutomirski VMALLOC_START, VMALLOC_END, 2469b6f7e16SRoman Gushchin THREADINFO_GFP & ~__GFP_ACCOUNT, 247ba14a194SAndy Lutomirski PAGE_KERNEL, 248ac496bf4SAndy Lutomirski 0, node, __builtin_return_address(0)); 249ba14a194SAndy Lutomirski 250ba14a194SAndy Lutomirski /* 251ba14a194SAndy Lutomirski * We can't call find_vm_area() in interrupt context, and 252ba14a194SAndy Lutomirski * free_thread_stack() can be called in interrupt context, 253ba14a194SAndy Lutomirski * so cache the vm_struct. 254ba14a194SAndy Lutomirski */ 2555eed6f1dSRik van Riel if (stack) { 256ba14a194SAndy Lutomirski tsk->stack_vm_area = find_vm_area(stack); 2575eed6f1dSRik van Riel tsk->stack = stack; 2585eed6f1dSRik van Riel } 259ba14a194SAndy Lutomirski return stack; 260ba14a194SAndy Lutomirski #else 2614949148aSVladimir Davydov struct page *page = alloc_pages_node(node, THREADINFO_GFP, 2622889f608SThomas Gleixner THREAD_SIZE_ORDER); 263b6a84016SEric Dumazet 2641bf4580eSAndrea Arcangeli if (likely(page)) { 2658dcc1d34SAndrey Konovalov tsk->stack = kasan_reset_tag(page_address(page)); 2661bf4580eSAndrea Arcangeli return tsk->stack; 2671bf4580eSAndrea Arcangeli } 2681bf4580eSAndrea Arcangeli return NULL; 269ba14a194SAndy Lutomirski #endif 270b69c49b7SFUJITA Tomonori } 271b69c49b7SFUJITA Tomonori 272ba14a194SAndy Lutomirski static inline void free_thread_stack(struct task_struct *tsk) 273b69c49b7SFUJITA Tomonori { 274ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 2759b6f7e16SRoman Gushchin struct vm_struct *vm = task_stack_vm_area(tsk); 2769b6f7e16SRoman Gushchin 2779b6f7e16SRoman Gushchin if (vm) { 278ac496bf4SAndy Lutomirski int i; 279ac496bf4SAndy Lutomirski 280991e7673SShakeel Butt for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) 281f4b00eabSRoman Gushchin memcg_kmem_uncharge_page(vm->pages[i], 0); 2829b6f7e16SRoman Gushchin 283ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 284112166f8SChristoph Lameter if (this_cpu_cmpxchg(cached_stacks[i], 285112166f8SChristoph Lameter NULL, tsk->stack_vm_area) != NULL) 286ac496bf4SAndy Lutomirski continue; 287ac496bf4SAndy Lutomirski 288ac496bf4SAndy Lutomirski return; 289ac496bf4SAndy Lutomirski } 290ac496bf4SAndy Lutomirski 2910f110a9bSAndrey Ryabinin vfree_atomic(tsk->stack); 292ac496bf4SAndy Lutomirski return; 293ac496bf4SAndy Lutomirski } 294ac496bf4SAndy Lutomirski #endif 295ac496bf4SAndy Lutomirski 296ba14a194SAndy Lutomirski __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); 297b69c49b7SFUJITA Tomonori } 2980d15d74aSThomas Gleixner # else 299b235beeaSLinus Torvalds static struct kmem_cache *thread_stack_cache; 3000d15d74aSThomas Gleixner 3019521d399SMichael Ellerman static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, 3020d15d74aSThomas Gleixner int node) 3030d15d74aSThomas Gleixner { 3045eed6f1dSRik van Riel unsigned long *stack; 3055eed6f1dSRik van Riel stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 3068dcc1d34SAndrey Konovalov stack = kasan_reset_tag(stack); 3075eed6f1dSRik van Riel tsk->stack = stack; 3085eed6f1dSRik van Riel return stack; 3090d15d74aSThomas Gleixner } 3100d15d74aSThomas Gleixner 311ba14a194SAndy Lutomirski static void free_thread_stack(struct task_struct *tsk) 3120d15d74aSThomas Gleixner { 313ba14a194SAndy Lutomirski kmem_cache_free(thread_stack_cache, tsk->stack); 3140d15d74aSThomas Gleixner } 3150d15d74aSThomas Gleixner 316b235beeaSLinus Torvalds void thread_stack_cache_init(void) 3170d15d74aSThomas Gleixner { 318f9d29946SDavid Windsor thread_stack_cache = kmem_cache_create_usercopy("thread_stack", 319f9d29946SDavid Windsor THREAD_SIZE, THREAD_SIZE, 0, 0, 320f9d29946SDavid Windsor THREAD_SIZE, NULL); 321b235beeaSLinus Torvalds BUG_ON(thread_stack_cache == NULL); 3220d15d74aSThomas Gleixner } 3230d15d74aSThomas Gleixner # endif 324b69c49b7SFUJITA Tomonori #endif 325b69c49b7SFUJITA Tomonori 3261da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 327e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 330e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 333e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 336e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 3393928d4f5SLinus Torvalds static struct kmem_cache *vm_area_cachep; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 342e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 3431da177e4SLinus Torvalds 344490fc053SLinus Torvalds struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) 3453928d4f5SLinus Torvalds { 346a670468fSAndrew Morton struct vm_area_struct *vma; 347490fc053SLinus Torvalds 348a670468fSAndrew Morton vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 349027232daSKirill A. Shutemov if (vma) 350027232daSKirill A. Shutemov vma_init(vma, mm); 351490fc053SLinus Torvalds return vma; 3523928d4f5SLinus Torvalds } 3533928d4f5SLinus Torvalds 3543928d4f5SLinus Torvalds struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) 3553928d4f5SLinus Torvalds { 35695faf699SLinus Torvalds struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 35795faf699SLinus Torvalds 35895faf699SLinus Torvalds if (new) { 359cda099b3SQian Cai ASSERT_EXCLUSIVE_WRITER(orig->vm_flags); 360cda099b3SQian Cai ASSERT_EXCLUSIVE_WRITER(orig->vm_file); 361cda099b3SQian Cai /* 362cda099b3SQian Cai * orig->shared.rb may be modified concurrently, but the clone 363cda099b3SQian Cai * will be reinitialized. 364cda099b3SQian Cai */ 365cda099b3SQian Cai *new = data_race(*orig); 36695faf699SLinus Torvalds INIT_LIST_HEAD(&new->anon_vma_chain); 367e39a4b33SLi Xinhai new->vm_next = new->vm_prev = NULL; 36895faf699SLinus Torvalds } 36995faf699SLinus Torvalds return new; 3703928d4f5SLinus Torvalds } 3713928d4f5SLinus Torvalds 3723928d4f5SLinus Torvalds void vm_area_free(struct vm_area_struct *vma) 3733928d4f5SLinus Torvalds { 3743928d4f5SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 3753928d4f5SLinus Torvalds } 3763928d4f5SLinus Torvalds 377ba14a194SAndy Lutomirski static void account_kernel_stack(struct task_struct *tsk, int account) 378c6a7f572SKOSAKI Motohiro { 379ba14a194SAndy Lutomirski void *stack = task_stack_page(tsk); 380ba14a194SAndy Lutomirski struct vm_struct *vm = task_stack_vm_area(tsk); 381ba14a194SAndy Lutomirski 382ba14a194SAndy Lutomirski 383991e7673SShakeel Butt /* All stack pages are in the same node. */ 384991e7673SShakeel Butt if (vm) 385991e7673SShakeel Butt mod_lruvec_page_state(vm->pages[0], NR_KERNEL_STACK_KB, 386efdc9490SAndy Lutomirski account * (THREAD_SIZE / 1024)); 387991e7673SShakeel Butt else 388*da3ceeffSMuchun Song mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB, 389991e7673SShakeel Butt account * (THREAD_SIZE / 1024)); 390ba14a194SAndy Lutomirski } 391c6a7f572SKOSAKI Motohiro 3929b6f7e16SRoman Gushchin static int memcg_charge_kernel_stack(struct task_struct *tsk) 3939b6f7e16SRoman Gushchin { 3949b6f7e16SRoman Gushchin #ifdef CONFIG_VMAP_STACK 3959b6f7e16SRoman Gushchin struct vm_struct *vm = task_stack_vm_area(tsk); 3969b6f7e16SRoman Gushchin int ret; 3979b6f7e16SRoman Gushchin 398991e7673SShakeel Butt BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); 399991e7673SShakeel Butt 4009b6f7e16SRoman Gushchin if (vm) { 4019b6f7e16SRoman Gushchin int i; 4029b6f7e16SRoman Gushchin 403991e7673SShakeel Butt BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); 404991e7673SShakeel Butt 4059b6f7e16SRoman Gushchin for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 4069b6f7e16SRoman Gushchin /* 407f4b00eabSRoman Gushchin * If memcg_kmem_charge_page() fails, page->mem_cgroup 408991e7673SShakeel Butt * pointer is NULL, and memcg_kmem_uncharge_page() in 409991e7673SShakeel Butt * free_thread_stack() will ignore this page. 4109b6f7e16SRoman Gushchin */ 411f4b00eabSRoman Gushchin ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL, 412f4b00eabSRoman Gushchin 0); 4139b6f7e16SRoman Gushchin if (ret) 4149b6f7e16SRoman Gushchin return ret; 4159b6f7e16SRoman Gushchin } 4169b6f7e16SRoman Gushchin } 4179b6f7e16SRoman Gushchin #endif 4189b6f7e16SRoman Gushchin return 0; 4199b6f7e16SRoman Gushchin } 4209b6f7e16SRoman Gushchin 42168f24b08SAndy Lutomirski static void release_task_stack(struct task_struct *tsk) 4221da177e4SLinus Torvalds { 423405c0759SAndy Lutomirski if (WARN_ON(tsk->state != TASK_DEAD)) 424405c0759SAndy Lutomirski return; /* Better to leak the stack than to free prematurely */ 425405c0759SAndy Lutomirski 426ba14a194SAndy Lutomirski account_kernel_stack(tsk, -1); 427ba14a194SAndy Lutomirski free_thread_stack(tsk); 42868f24b08SAndy Lutomirski tsk->stack = NULL; 42968f24b08SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 43068f24b08SAndy Lutomirski tsk->stack_vm_area = NULL; 43168f24b08SAndy Lutomirski #endif 43268f24b08SAndy Lutomirski } 43368f24b08SAndy Lutomirski 43468f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 43568f24b08SAndy Lutomirski void put_task_stack(struct task_struct *tsk) 43668f24b08SAndy Lutomirski { 437f0b89d39SElena Reshetova if (refcount_dec_and_test(&tsk->stack_refcount)) 43868f24b08SAndy Lutomirski release_task_stack(tsk); 43968f24b08SAndy Lutomirski } 44068f24b08SAndy Lutomirski #endif 44168f24b08SAndy Lutomirski 44268f24b08SAndy Lutomirski void free_task(struct task_struct *tsk) 44368f24b08SAndy Lutomirski { 444d08b9f0cSSami Tolvanen scs_release(tsk); 445d08b9f0cSSami Tolvanen 44668f24b08SAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK 44768f24b08SAndy Lutomirski /* 44868f24b08SAndy Lutomirski * The task is finally done with both the stack and thread_info, 44968f24b08SAndy Lutomirski * so free both. 45068f24b08SAndy Lutomirski */ 45168f24b08SAndy Lutomirski release_task_stack(tsk); 45268f24b08SAndy Lutomirski #else 45368f24b08SAndy Lutomirski /* 45468f24b08SAndy Lutomirski * If the task had a separate stack allocation, it should be gone 45568f24b08SAndy Lutomirski * by now. 45668f24b08SAndy Lutomirski */ 457f0b89d39SElena Reshetova WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0); 45868f24b08SAndy Lutomirski #endif 45923f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 460fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 461f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 4621da5c46fSOleg Nesterov if (tsk->flags & PF_KTHREAD) 4631da5c46fSOleg Nesterov free_kthread_struct(tsk); 4641da177e4SLinus Torvalds free_task_struct(tsk); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds #ifdef CONFIG_MMU 4690766f788SEmese Revfy static __latent_entropy int dup_mmap(struct mm_struct *mm, 4700766f788SEmese Revfy struct mm_struct *oldmm) 4711da177e4SLinus Torvalds { 472297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 4731da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 4741da177e4SLinus Torvalds int retval; 4751da177e4SLinus Torvalds unsigned long charge; 476893e26e6SPavel Emelyanov LIST_HEAD(uf); 4771da177e4SLinus Torvalds 47832cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 479d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(oldmm)) { 4807c051267SMichal Hocko retval = -EINTR; 4817c051267SMichal Hocko goto fail_uprobe_end; 4827c051267SMichal Hocko } 483ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 484f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 485ad339451SIngo Molnar /* 486ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 487ad339451SIngo Molnar */ 488aaa2cc56SMichel Lespinasse mmap_write_lock_nested(mm, SINGLE_DEPTH_NESTING); 4897ee78232SHugh Dickins 49090f31d0eSKonstantin Khlebnikov /* No ordering required: file already has been exposed. */ 49190f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 49290f31d0eSKonstantin Khlebnikov 4934f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 49484638335SKonstantin Khlebnikov mm->data_vm = oldmm->data_vm; 4954f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 4964f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 4974f7d4614SVladimir Davydov 4981da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 4991da177e4SLinus Torvalds rb_parent = NULL; 5001da177e4SLinus Torvalds pprev = &mm->mmap; 501f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 502f8af4da3SHugh Dickins if (retval) 503f8af4da3SHugh Dickins goto out; 504ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 505ba76149fSAndrea Arcangeli if (retval) 506ba76149fSAndrea Arcangeli goto out; 5071da177e4SLinus Torvalds 508297c5eeeSLinus Torvalds prev = NULL; 509fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 5101da177e4SLinus Torvalds struct file *file; 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 51384638335SKonstantin Khlebnikov vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); 5141da177e4SLinus Torvalds continue; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds charge = 0; 517655c79bbSTetsuo Handa /* 518655c79bbSTetsuo Handa * Don't duplicate many vmas if we've been oom-killed (for 519655c79bbSTetsuo Handa * example) 520655c79bbSTetsuo Handa */ 521655c79bbSTetsuo Handa if (fatal_signal_pending(current)) { 522655c79bbSTetsuo Handa retval = -EINTR; 523655c79bbSTetsuo Handa goto out; 524655c79bbSTetsuo Handa } 5251da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 526b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 527b2412b7fSHuang Shijie 528191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 5291da177e4SLinus Torvalds goto fail_nomem; 5301da177e4SLinus Torvalds charge = len; 5311da177e4SLinus Torvalds } 5323928d4f5SLinus Torvalds tmp = vm_area_dup(mpnt); 5331da177e4SLinus Torvalds if (!tmp) 5341da177e4SLinus Torvalds goto fail_nomem; 535ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 536ef0855d3SOleg Nesterov if (retval) 5371da177e4SLinus Torvalds goto fail_nomem_policy; 538a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 539893e26e6SPavel Emelyanov retval = dup_userfaultfd(tmp, &uf); 540893e26e6SPavel Emelyanov if (retval) 541893e26e6SPavel Emelyanov goto fail_nomem_anon_vma_fork; 542d2cd9edeSRik van Riel if (tmp->vm_flags & VM_WIPEONFORK) { 54393949bb2SLi Xinhai /* 54493949bb2SLi Xinhai * VM_WIPEONFORK gets a clean slate in the child. 54593949bb2SLi Xinhai * Don't prepare anon_vma until fault since we don't 54693949bb2SLi Xinhai * copy page for current vma. 54793949bb2SLi Xinhai */ 548d2cd9edeSRik van Riel tmp->anon_vma = NULL; 549d2cd9edeSRik van Riel } else if (anon_vma_fork(tmp, mpnt)) 5505beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 551893e26e6SPavel Emelyanov tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT); 5521da177e4SLinus Torvalds file = tmp->vm_file; 5531da177e4SLinus Torvalds if (file) { 554496ad9aaSAl Viro struct inode *inode = file_inode(file); 555b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 556b88ed205SHugh Dickins 5571da177e4SLinus Torvalds get_file(file); 5581da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 55973eb7f9aSMiaohe Lin put_write_access(inode); 56083cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 561b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 562cf508b58SMiaohe Lin mapping_allow_writable(mapping); 563b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 564b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 5659826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 5669826a516SMichel Lespinasse &mapping->i_mmap); 567b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 56883cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* 572a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 573a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 574a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 575a1e78772SMel Gorman */ 576a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 577a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 578a1e78772SMel Gorman 579a1e78772SMel Gorman /* 5807ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 5811da177e4SLinus Torvalds */ 5821da177e4SLinus Torvalds *pprev = tmp; 5831da177e4SLinus Torvalds pprev = &tmp->vm_next; 584297c5eeeSLinus Torvalds tmp->vm_prev = prev; 585297c5eeeSLinus Torvalds prev = tmp; 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 5881da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 5891da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds mm->map_count++; 592d2cd9edeSRik van Riel if (!(tmp->vm_flags & VM_WIPEONFORK)) 593c78f4636SPeter Xu retval = copy_page_range(tmp, mpnt); 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 5961da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (retval) 5991da177e4SLinus Torvalds goto out; 6001da177e4SLinus Torvalds } 601d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 6021ed0cc5aSNadav Amit retval = arch_dup_mmap(oldmm, mm); 6031da177e4SLinus Torvalds out: 604d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 605fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 606d8ed45c5SMichel Lespinasse mmap_write_unlock(oldmm); 607893e26e6SPavel Emelyanov dup_userfaultfd_complete(&uf); 6087c051267SMichal Hocko fail_uprobe_end: 60932cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 6101da177e4SLinus Torvalds return retval; 6115beb4930SRik van Riel fail_nomem_anon_vma_fork: 612ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 6131da177e4SLinus Torvalds fail_nomem_policy: 6143928d4f5SLinus Torvalds vm_area_free(tmp); 6151da177e4SLinus Torvalds fail_nomem: 6161da177e4SLinus Torvalds retval = -ENOMEM; 6171da177e4SLinus Torvalds vm_unacct_memory(charge); 6181da177e4SLinus Torvalds goto out; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 6221da177e4SLinus Torvalds { 6231da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 6241da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 6251da177e4SLinus Torvalds return -ENOMEM; 6261da177e4SLinus Torvalds return 0; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 6301da177e4SLinus Torvalds { 6315e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds #else 63490f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 63590f31d0eSKonstantin Khlebnikov { 636d8ed45c5SMichel Lespinasse mmap_write_lock(oldmm); 63790f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 638d8ed45c5SMichel Lespinasse mmap_write_unlock(oldmm); 63990f31d0eSKonstantin Khlebnikov return 0; 64090f31d0eSKonstantin Khlebnikov } 6411da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 6421da177e4SLinus Torvalds #define mm_free_pgd(mm) 6431da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 6441da177e4SLinus Torvalds 645d70f2a14SAndrew Morton static void check_mm(struct mm_struct *mm) 646d70f2a14SAndrew Morton { 647d70f2a14SAndrew Morton int i; 648d70f2a14SAndrew Morton 6498495f7e6SSai Praneeth Prakhya BUILD_BUG_ON_MSG(ARRAY_SIZE(resident_page_types) != NR_MM_COUNTERS, 6508495f7e6SSai Praneeth Prakhya "Please make sure 'struct resident_page_types[]' is updated as well"); 6518495f7e6SSai Praneeth Prakhya 652d70f2a14SAndrew Morton for (i = 0; i < NR_MM_COUNTERS; i++) { 653d70f2a14SAndrew Morton long x = atomic_long_read(&mm->rss_stat.count[i]); 654d70f2a14SAndrew Morton 655d70f2a14SAndrew Morton if (unlikely(x)) 6568495f7e6SSai Praneeth Prakhya pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n", 6578495f7e6SSai Praneeth Prakhya mm, resident_page_types[i], x); 658d70f2a14SAndrew Morton } 659d70f2a14SAndrew Morton 660d70f2a14SAndrew Morton if (mm_pgtables_bytes(mm)) 661d70f2a14SAndrew Morton pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", 662d70f2a14SAndrew Morton mm_pgtables_bytes(mm)); 663d70f2a14SAndrew Morton 664d70f2a14SAndrew Morton #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 665d70f2a14SAndrew Morton VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 666d70f2a14SAndrew Morton #endif 667d70f2a14SAndrew Morton } 6681da177e4SLinus Torvalds 669e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 6701da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 6711da177e4SLinus Torvalds 672d70f2a14SAndrew Morton /* 673d70f2a14SAndrew Morton * Called when the last reference to the mm 674d70f2a14SAndrew Morton * is dropped: either by a lazy thread or by 675d70f2a14SAndrew Morton * mmput. Free the page directory and the mm. 676d70f2a14SAndrew Morton */ 677d34bc48fSAndrew Morton void __mmdrop(struct mm_struct *mm) 678d70f2a14SAndrew Morton { 679d70f2a14SAndrew Morton BUG_ON(mm == &init_mm); 6803eda69c9SMark Rutland WARN_ON_ONCE(mm == current->mm); 6813eda69c9SMark Rutland WARN_ON_ONCE(mm == current->active_mm); 682d70f2a14SAndrew Morton mm_free_pgd(mm); 683d70f2a14SAndrew Morton destroy_context(mm); 684984cfe4eSJason Gunthorpe mmu_notifier_subscriptions_destroy(mm); 685d70f2a14SAndrew Morton check_mm(mm); 686d70f2a14SAndrew Morton put_user_ns(mm->user_ns); 687d70f2a14SAndrew Morton free_mm(mm); 688d70f2a14SAndrew Morton } 689d34bc48fSAndrew Morton EXPORT_SYMBOL_GPL(__mmdrop); 690d70f2a14SAndrew Morton 691d70f2a14SAndrew Morton static void mmdrop_async_fn(struct work_struct *work) 692d70f2a14SAndrew Morton { 693d70f2a14SAndrew Morton struct mm_struct *mm; 694d70f2a14SAndrew Morton 695d70f2a14SAndrew Morton mm = container_of(work, struct mm_struct, async_put_work); 696d70f2a14SAndrew Morton __mmdrop(mm); 697d70f2a14SAndrew Morton } 698d70f2a14SAndrew Morton 699d70f2a14SAndrew Morton static void mmdrop_async(struct mm_struct *mm) 700d70f2a14SAndrew Morton { 701d70f2a14SAndrew Morton if (unlikely(atomic_dec_and_test(&mm->mm_count))) { 702d70f2a14SAndrew Morton INIT_WORK(&mm->async_put_work, mmdrop_async_fn); 703d70f2a14SAndrew Morton schedule_work(&mm->async_put_work); 704d70f2a14SAndrew Morton } 705d70f2a14SAndrew Morton } 706d70f2a14SAndrew Morton 7071da177e4SLinus Torvalds static inline void free_signal_struct(struct signal_struct *sig) 7081da177e4SLinus Torvalds { 7091da177e4SLinus Torvalds taskstats_tgid_free(sig); 7101da177e4SLinus Torvalds sched_autogroup_exit(sig); 7111da177e4SLinus Torvalds /* 7121da177e4SLinus Torvalds * __mmdrop is not safe to call from softirq context on x86 due to 7131da177e4SLinus Torvalds * pgd_dtor so postpone it to the async context 7141da177e4SLinus Torvalds */ 7151da177e4SLinus Torvalds if (sig->oom_mm) 7161da177e4SLinus Torvalds mmdrop_async(sig->oom_mm); 7171da177e4SLinus Torvalds kmem_cache_free(signal_cachep, sig); 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds static inline void put_signal_struct(struct signal_struct *sig) 7211da177e4SLinus Torvalds { 72260d4de3fSElena Reshetova if (refcount_dec_and_test(&sig->sigcnt)) 7231da177e4SLinus Torvalds free_signal_struct(sig); 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds void __put_task_struct(struct task_struct *tsk) 7271da177e4SLinus Torvalds { 7281da177e4SLinus Torvalds WARN_ON(!tsk->exit_state); 729ec1d2819SElena Reshetova WARN_ON(refcount_read(&tsk->usage)); 7301da177e4SLinus Torvalds WARN_ON(tsk == current); 7311da177e4SLinus Torvalds 7320f212204SJens Axboe io_uring_free(tsk); 7331da177e4SLinus Torvalds cgroup_free(tsk); 73416d51a59SJann Horn task_numa_free(tsk, true); 7351da177e4SLinus Torvalds security_task_free(tsk); 7361da177e4SLinus Torvalds exit_creds(tsk); 7371da177e4SLinus Torvalds delayacct_tsk_free(tsk); 7381da177e4SLinus Torvalds put_signal_struct(tsk->signal); 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 7411da177e4SLinus Torvalds free_task(tsk); 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__put_task_struct); 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds void __init __weak arch_task_cache_init(void) { } 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds /* 7481da177e4SLinus Torvalds * set_max_threads 7491da177e4SLinus Torvalds */ 7501da177e4SLinus Torvalds static void set_max_threads(unsigned int max_threads_suggested) 7511da177e4SLinus Torvalds { 7521da177e4SLinus Torvalds u64 threads; 753ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds /* 7561da177e4SLinus Torvalds * The number of threads shall be limited such that the thread 7571da177e4SLinus Torvalds * structures may only consume a small part of the available memory. 7581da177e4SLinus Torvalds */ 7593d6357deSArun KS if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64) 7601da177e4SLinus Torvalds threads = MAX_THREADS; 7611da177e4SLinus Torvalds else 7623d6357deSArun KS threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE, 7631da177e4SLinus Torvalds (u64) THREAD_SIZE * 8UL); 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds if (threads > max_threads_suggested) 7661da177e4SLinus Torvalds threads = max_threads_suggested; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 7721da177e4SLinus Torvalds /* Initialized by the architecture: */ 7731da177e4SLinus Torvalds int arch_task_struct_size __read_mostly; 7741da177e4SLinus Torvalds #endif 7751da177e4SLinus Torvalds 7764189ff23SChristoph Hellwig #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 7775905429aSKees Cook static void task_struct_whitelist(unsigned long *offset, unsigned long *size) 7785905429aSKees Cook { 7795905429aSKees Cook /* Fetch thread_struct whitelist for the architecture. */ 7805905429aSKees Cook arch_thread_struct_whitelist(offset, size); 7815905429aSKees Cook 7825905429aSKees Cook /* 7835905429aSKees Cook * Handle zero-sized whitelist or empty thread_struct, otherwise 7845905429aSKees Cook * adjust offset to position of thread_struct in task_struct. 7855905429aSKees Cook */ 7865905429aSKees Cook if (unlikely(*size == 0)) 7875905429aSKees Cook *offset = 0; 7885905429aSKees Cook else 7895905429aSKees Cook *offset += offsetof(struct task_struct, thread); 7905905429aSKees Cook } 7914189ff23SChristoph Hellwig #endif /* CONFIG_ARCH_TASK_STRUCT_ALLOCATOR */ 7925905429aSKees Cook 7931da177e4SLinus Torvalds void __init fork_init(void) 7941da177e4SLinus Torvalds { 7951da177e4SLinus Torvalds int i; 7961da177e4SLinus Torvalds #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 7971da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 7981da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN 0 7991da177e4SLinus Torvalds #endif 8001da177e4SLinus Torvalds int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); 8015905429aSKees Cook unsigned long useroffset, usersize; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 8045905429aSKees Cook task_struct_whitelist(&useroffset, &usersize); 8055905429aSKees Cook task_struct_cachep = kmem_cache_create_usercopy("task_struct", 8061da177e4SLinus Torvalds arch_task_struct_size, align, 8075905429aSKees Cook SLAB_PANIC|SLAB_ACCOUNT, 8085905429aSKees Cook useroffset, usersize, NULL); 8091da177e4SLinus Torvalds #endif 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds /* do the arch specific task caches init */ 8121da177e4SLinus Torvalds arch_task_cache_init(); 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds set_max_threads(MAX_THREADS); 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 8171da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 8181da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 8191da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds for (i = 0; i < UCOUNT_COUNTS; i++) { 8221da177e4SLinus Torvalds init_user_ns.ucount_max[i] = max_threads/2; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds #ifdef CONFIG_VMAP_STACK 8261da177e4SLinus Torvalds cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", 8271da177e4SLinus Torvalds NULL, free_vm_stack_cache); 8281da177e4SLinus Torvalds #endif 8291da177e4SLinus Torvalds 830d08b9f0cSSami Tolvanen scs_init(); 831d08b9f0cSSami Tolvanen 8321da177e4SLinus Torvalds lockdep_init_task(&init_task); 833aad42dd4SNadav Amit uprobes_init(); 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds int __weak arch_dup_task_struct(struct task_struct *dst, 8371da177e4SLinus Torvalds struct task_struct *src) 8381da177e4SLinus Torvalds { 8391da177e4SLinus Torvalds *dst = *src; 8401da177e4SLinus Torvalds return 0; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds void set_task_stack_end_magic(struct task_struct *tsk) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds unsigned long *stackend; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds stackend = end_of_stack(tsk); 8481da177e4SLinus Torvalds *stackend = STACK_END_MAGIC; /* for overflow detection */ 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds struct task_struct *tsk; 8541da177e4SLinus Torvalds unsigned long *stack; 8550f4991e8SYueHaibing struct vm_struct *stack_vm_area __maybe_unused; 8561da177e4SLinus Torvalds int err; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds if (node == NUMA_NO_NODE) 8591da177e4SLinus Torvalds node = tsk_fork_get_node(orig); 8601da177e4SLinus Torvalds tsk = alloc_task_struct_node(node); 8611da177e4SLinus Torvalds if (!tsk) 8621da177e4SLinus Torvalds return NULL; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds stack = alloc_thread_stack_node(tsk, node); 8651da177e4SLinus Torvalds if (!stack) 8661da177e4SLinus Torvalds goto free_tsk; 8671da177e4SLinus Torvalds 8689b6f7e16SRoman Gushchin if (memcg_charge_kernel_stack(tsk)) 8699b6f7e16SRoman Gushchin goto free_stack; 8709b6f7e16SRoman Gushchin 8711da177e4SLinus Torvalds stack_vm_area = task_stack_vm_area(tsk); 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds err = arch_dup_task_struct(tsk, orig); 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds /* 8761da177e4SLinus Torvalds * arch_dup_task_struct() clobbers the stack-related fields. Make 8771da177e4SLinus Torvalds * sure they're properly initialized before using any stack-related 8781da177e4SLinus Torvalds * functions again. 8791da177e4SLinus Torvalds */ 8801da177e4SLinus Torvalds tsk->stack = stack; 8811da177e4SLinus Torvalds #ifdef CONFIG_VMAP_STACK 8821da177e4SLinus Torvalds tsk->stack_vm_area = stack_vm_area; 8831da177e4SLinus Torvalds #endif 8841da177e4SLinus Torvalds #ifdef CONFIG_THREAD_INFO_IN_TASK 885f0b89d39SElena Reshetova refcount_set(&tsk->stack_refcount, 1); 8861da177e4SLinus Torvalds #endif 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds if (err) 8891da177e4SLinus Torvalds goto free_stack; 8901da177e4SLinus Torvalds 891d08b9f0cSSami Tolvanen err = scs_prepare(tsk, node); 892d08b9f0cSSami Tolvanen if (err) 893d08b9f0cSSami Tolvanen goto free_stack; 894d08b9f0cSSami Tolvanen 8951da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 8961da177e4SLinus Torvalds /* 8971da177e4SLinus Torvalds * We must handle setting up seccomp filters once we're under 8981da177e4SLinus Torvalds * the sighand lock in case orig has changed between now and 8991da177e4SLinus Torvalds * then. Until then, filter must be NULL to avoid messing up 9001da177e4SLinus Torvalds * the usage counts on the error path calling free_task. 9011da177e4SLinus Torvalds */ 9021da177e4SLinus Torvalds tsk->seccomp.filter = NULL; 9031da177e4SLinus Torvalds #endif 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds setup_thread_stack(tsk, orig); 9061da177e4SLinus Torvalds clear_user_return_notifier(tsk); 9071da177e4SLinus Torvalds clear_tsk_need_resched(tsk); 9081da177e4SLinus Torvalds set_task_stack_end_magic(tsk); 9091da177e4SLinus Torvalds 910050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR 9111da177e4SLinus Torvalds tsk->stack_canary = get_random_canary(); 9121da177e4SLinus Torvalds #endif 9133bd37062SSebastian Andrzej Siewior if (orig->cpus_ptr == &orig->cpus_mask) 9143bd37062SSebastian Andrzej Siewior tsk->cpus_ptr = &tsk->cpus_mask; 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds /* 9170ff7b2cfSEric W. Biederman * One for the user space visible state that goes away when reaped. 9180ff7b2cfSEric W. Biederman * One for the scheduler. 9191da177e4SLinus Torvalds */ 9200ff7b2cfSEric W. Biederman refcount_set(&tsk->rcu_users, 2); 9210ff7b2cfSEric W. Biederman /* One for the rcu users */ 9220ff7b2cfSEric W. Biederman refcount_set(&tsk->usage, 1); 9231da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_IO_TRACE 9241da177e4SLinus Torvalds tsk->btrace_seq = 0; 9251da177e4SLinus Torvalds #endif 9261da177e4SLinus Torvalds tsk->splice_pipe = NULL; 9271da177e4SLinus Torvalds tsk->task_frag.page = NULL; 9281da177e4SLinus Torvalds tsk->wake_q.next = NULL; 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds account_kernel_stack(tsk, 1); 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds kcov_task_init(tsk); 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds #ifdef CONFIG_FAULT_INJECTION 9351da177e4SLinus Torvalds tsk->fail_nth = 0; 9361da177e4SLinus Torvalds #endif 9371da177e4SLinus Torvalds 9382c323017SJosef Bacik #ifdef CONFIG_BLK_CGROUP 9392c323017SJosef Bacik tsk->throttle_queue = NULL; 9402c323017SJosef Bacik tsk->use_memdelay = 0; 9412c323017SJosef Bacik #endif 9422c323017SJosef Bacik 943d46eb14bSShakeel Butt #ifdef CONFIG_MEMCG 944d46eb14bSShakeel Butt tsk->active_memcg = NULL; 945d46eb14bSShakeel Butt #endif 9461da177e4SLinus Torvalds return tsk; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds free_stack: 9491da177e4SLinus Torvalds free_thread_stack(tsk); 9501da177e4SLinus Torvalds free_tsk: 9511da177e4SLinus Torvalds free_task_struct(tsk); 9521da177e4SLinus Torvalds return NULL; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 9561da177e4SLinus Torvalds 9574cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 9584cb0e11bSHidehiro Kawai 9594cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 9604cb0e11bSHidehiro Kawai { 9614cb0e11bSHidehiro Kawai default_dump_filter = 9624cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 9634cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 9644cb0e11bSHidehiro Kawai return 1; 9654cb0e11bSHidehiro Kawai } 9664cb0e11bSHidehiro Kawai 9674cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 9684cb0e11bSHidehiro Kawai 9691da177e4SLinus Torvalds #include <linux/init_task.h> 9701da177e4SLinus Torvalds 971858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 972858f0993SAlexey Dobriyan { 973858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 974858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 975db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 976858f0993SAlexey Dobriyan #endif 977858f0993SAlexey Dobriyan } 978858f0993SAlexey Dobriyan 979c3f3ce04SAndrea Arcangeli static __always_inline void mm_clear_owner(struct mm_struct *mm, 980c3f3ce04SAndrea Arcangeli struct task_struct *p) 981c3f3ce04SAndrea Arcangeli { 982c3f3ce04SAndrea Arcangeli #ifdef CONFIG_MEMCG 983c3f3ce04SAndrea Arcangeli if (mm->owner == p) 984c3f3ce04SAndrea Arcangeli WRITE_ONCE(mm->owner, NULL); 985c3f3ce04SAndrea Arcangeli #endif 986c3f3ce04SAndrea Arcangeli } 987c3f3ce04SAndrea Arcangeli 98833144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 98933144e84SVladimir Davydov { 99033144e84SVladimir Davydov #ifdef CONFIG_MEMCG 99133144e84SVladimir Davydov mm->owner = p; 99233144e84SVladimir Davydov #endif 99333144e84SVladimir Davydov } 99433144e84SVladimir Davydov 995355627f5SEric Biggers static void mm_init_uprobes_state(struct mm_struct *mm) 996355627f5SEric Biggers { 997355627f5SEric Biggers #ifdef CONFIG_UPROBES 998355627f5SEric Biggers mm->uprobes_state.xol_area = NULL; 999355627f5SEric Biggers #endif 1000355627f5SEric Biggers } 1001355627f5SEric Biggers 1002bfedb589SEric W. Biederman static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, 1003bfedb589SEric W. Biederman struct user_namespace *user_ns) 10041da177e4SLinus Torvalds { 100541f727fdSVladimir Davydov mm->mmap = NULL; 100641f727fdSVladimir Davydov mm->mm_rb = RB_ROOT; 100741f727fdSVladimir Davydov mm->vmacache_seqnum = 0; 10081da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 10091da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 101057efa1feSJason Gunthorpe seqcount_init(&mm->write_protect_seq); 1011d8ed45c5SMichel Lespinasse mmap_init_lock(mm); 10121da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 1013999d9fc1SOleg Nesterov mm->core_state = NULL; 1014af5b0f6aSKirill A. Shutemov mm_pgtables_bytes_init(mm); 101541f727fdSVladimir Davydov mm->map_count = 0; 101641f727fdSVladimir Davydov mm->locked_vm = 0; 1017008cfe44SPeter Xu atomic_set(&mm->has_pinned, 0); 101870f8a3caSDavidlohr Bueso atomic64_set(&mm->pinned_vm, 0); 1019d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 10201da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 102188aa7cc6SYang Shi spin_lock_init(&mm->arg_lock); 102241f727fdSVladimir Davydov mm_init_cpumask(mm); 1023858f0993SAlexey Dobriyan mm_init_aio(mm); 1024cf475ad2SBalbir Singh mm_init_owner(mm, p); 10252b7e8665SEric Biggers RCU_INIT_POINTER(mm->exe_file, NULL); 1026984cfe4eSJason Gunthorpe mmu_notifier_subscriptions_init(mm); 102716af97dcSNadav Amit init_tlb_flush_pending(mm); 102841f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 102941f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 103041f727fdSVladimir Davydov #endif 1031355627f5SEric Biggers mm_init_uprobes_state(mm); 10321da177e4SLinus Torvalds 1033a0715cc2SAlex Thorlton if (current->mm) { 1034a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 1035a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 1036a0715cc2SAlex Thorlton } else { 1037a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 10381da177e4SLinus Torvalds mm->def_flags = 0; 1039a0715cc2SAlex Thorlton } 1040a0715cc2SAlex Thorlton 104141f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 104241f727fdSVladimir Davydov goto fail_nopgd; 104378fb7466SPavel Emelianov 104441f727fdSVladimir Davydov if (init_new_context(p, mm)) 104541f727fdSVladimir Davydov goto fail_nocontext; 104641f727fdSVladimir Davydov 1047bfedb589SEric W. Biederman mm->user_ns = get_user_ns(user_ns); 104841f727fdSVladimir Davydov return mm; 104941f727fdSVladimir Davydov 105041f727fdSVladimir Davydov fail_nocontext: 105141f727fdSVladimir Davydov mm_free_pgd(mm); 105241f727fdSVladimir Davydov fail_nopgd: 10531da177e4SLinus Torvalds free_mm(mm); 10541da177e4SLinus Torvalds return NULL; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* 10581da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 10611da177e4SLinus Torvalds { 10621da177e4SLinus Torvalds struct mm_struct *mm; 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds mm = allocate_mm(); 1065de03c72cSKOSAKI Motohiro if (!mm) 1066de03c72cSKOSAKI Motohiro return NULL; 1067de03c72cSKOSAKI Motohiro 10681da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 1069bfedb589SEric W. Biederman return mm_init(mm, current, current_user_ns()); 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 1072ec8d7c14SMichal Hocko static inline void __mmput(struct mm_struct *mm) 10731da177e4SLinus Torvalds { 1074ec8d7c14SMichal Hocko VM_BUG_ON(atomic_read(&mm->mm_users)); 10750ae26f1bSAndrew Morton 1076d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 10771da177e4SLinus Torvalds exit_aio(mm); 10781c2fb7a4SAndrea Arcangeli ksm_exit(mm); 1079ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 10801da177e4SLinus Torvalds exit_mmap(mm); 10816fcb52a5SAaron Lu mm_put_huge_zero_page(mm); 1082925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 10831da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 10841da177e4SLinus Torvalds spin_lock(&mmlist_lock); 10851da177e4SLinus Torvalds list_del(&mm->mmlist); 10861da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 10871da177e4SLinus Torvalds } 1088801460d0SHiroshi Shimamoto if (mm->binfmt) 1089801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 10901da177e4SLinus Torvalds mmdrop(mm); 10911da177e4SLinus Torvalds } 1092ec8d7c14SMichal Hocko 1093ec8d7c14SMichal Hocko /* 1094ec8d7c14SMichal Hocko * Decrement the use count and release all resources for an mm. 1095ec8d7c14SMichal Hocko */ 1096ec8d7c14SMichal Hocko void mmput(struct mm_struct *mm) 1097ec8d7c14SMichal Hocko { 1098ec8d7c14SMichal Hocko might_sleep(); 1099ec8d7c14SMichal Hocko 1100ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) 1101ec8d7c14SMichal Hocko __mmput(mm); 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 11041da177e4SLinus Torvalds 1105a1b2289cSSherry Yang #ifdef CONFIG_MMU 1106a1b2289cSSherry Yang static void mmput_async_fn(struct work_struct *work) 1107a1b2289cSSherry Yang { 1108a1b2289cSSherry Yang struct mm_struct *mm = container_of(work, struct mm_struct, 1109a1b2289cSSherry Yang async_put_work); 1110a1b2289cSSherry Yang 1111a1b2289cSSherry Yang __mmput(mm); 1112a1b2289cSSherry Yang } 1113a1b2289cSSherry Yang 1114a1b2289cSSherry Yang void mmput_async(struct mm_struct *mm) 1115a1b2289cSSherry Yang { 1116a1b2289cSSherry Yang if (atomic_dec_and_test(&mm->mm_users)) { 1117a1b2289cSSherry Yang INIT_WORK(&mm->async_put_work, mmput_async_fn); 1118a1b2289cSSherry Yang schedule_work(&mm->async_put_work); 1119a1b2289cSSherry Yang } 1120a1b2289cSSherry Yang } 1121a1b2289cSSherry Yang #endif 1122a1b2289cSSherry Yang 112390f31d0eSKonstantin Khlebnikov /** 112490f31d0eSKonstantin Khlebnikov * set_mm_exe_file - change a reference to the mm's executable file 112590f31d0eSKonstantin Khlebnikov * 112690f31d0eSKonstantin Khlebnikov * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 112790f31d0eSKonstantin Khlebnikov * 11286e399cd1SDavidlohr Bueso * Main users are mmput() and sys_execve(). Callers prevent concurrent 11296e399cd1SDavidlohr Bueso * invocations: in mmput() nobody alive left, in execve task is single 11306e399cd1SDavidlohr Bueso * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the 11316e399cd1SDavidlohr Bueso * mm->exe_file, but does so without using set_mm_exe_file() in order 11326e399cd1SDavidlohr Bueso * to do avoid the need for any locks. 113390f31d0eSKonstantin Khlebnikov */ 113438646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 113538646013SJiri Slaby { 11366e399cd1SDavidlohr Bueso struct file *old_exe_file; 11376e399cd1SDavidlohr Bueso 11386e399cd1SDavidlohr Bueso /* 11396e399cd1SDavidlohr Bueso * It is safe to dereference the exe_file without RCU as 11406e399cd1SDavidlohr Bueso * this function is only called if nobody else can access 11416e399cd1SDavidlohr Bueso * this mm -- see comment above for justification. 11426e399cd1SDavidlohr Bueso */ 11436e399cd1SDavidlohr Bueso old_exe_file = rcu_dereference_raw(mm->exe_file); 114490f31d0eSKonstantin Khlebnikov 114538646013SJiri Slaby if (new_exe_file) 114638646013SJiri Slaby get_file(new_exe_file); 114790f31d0eSKonstantin Khlebnikov rcu_assign_pointer(mm->exe_file, new_exe_file); 114890f31d0eSKonstantin Khlebnikov if (old_exe_file) 114990f31d0eSKonstantin Khlebnikov fput(old_exe_file); 115038646013SJiri Slaby } 115138646013SJiri Slaby 115290f31d0eSKonstantin Khlebnikov /** 115390f31d0eSKonstantin Khlebnikov * get_mm_exe_file - acquire a reference to the mm's executable file 115490f31d0eSKonstantin Khlebnikov * 115590f31d0eSKonstantin Khlebnikov * Returns %NULL if mm has no associated executable file. 115690f31d0eSKonstantin Khlebnikov * User must release file via fput(). 115790f31d0eSKonstantin Khlebnikov */ 115838646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 115938646013SJiri Slaby { 116038646013SJiri Slaby struct file *exe_file; 116138646013SJiri Slaby 116290f31d0eSKonstantin Khlebnikov rcu_read_lock(); 116390f31d0eSKonstantin Khlebnikov exe_file = rcu_dereference(mm->exe_file); 116490f31d0eSKonstantin Khlebnikov if (exe_file && !get_file_rcu(exe_file)) 116590f31d0eSKonstantin Khlebnikov exe_file = NULL; 116690f31d0eSKonstantin Khlebnikov rcu_read_unlock(); 116738646013SJiri Slaby return exe_file; 116838646013SJiri Slaby } 116911163348SDavidlohr Bueso EXPORT_SYMBOL(get_mm_exe_file); 117038646013SJiri Slaby 11711da177e4SLinus Torvalds /** 1172cd81a917SMateusz Guzik * get_task_exe_file - acquire a reference to the task's executable file 1173cd81a917SMateusz Guzik * 1174cd81a917SMateusz Guzik * Returns %NULL if task's mm (if any) has no associated executable file or 1175cd81a917SMateusz Guzik * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 1176cd81a917SMateusz Guzik * User must release file via fput(). 1177cd81a917SMateusz Guzik */ 1178cd81a917SMateusz Guzik struct file *get_task_exe_file(struct task_struct *task) 1179cd81a917SMateusz Guzik { 1180cd81a917SMateusz Guzik struct file *exe_file = NULL; 1181cd81a917SMateusz Guzik struct mm_struct *mm; 1182cd81a917SMateusz Guzik 1183cd81a917SMateusz Guzik task_lock(task); 1184cd81a917SMateusz Guzik mm = task->mm; 1185cd81a917SMateusz Guzik if (mm) { 1186cd81a917SMateusz Guzik if (!(task->flags & PF_KTHREAD)) 1187cd81a917SMateusz Guzik exe_file = get_mm_exe_file(mm); 1188cd81a917SMateusz Guzik } 1189cd81a917SMateusz Guzik task_unlock(task); 1190cd81a917SMateusz Guzik return exe_file; 1191cd81a917SMateusz Guzik } 1192cd81a917SMateusz Guzik EXPORT_SYMBOL(get_task_exe_file); 1193cd81a917SMateusz Guzik 1194cd81a917SMateusz Guzik /** 11951da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 11961da177e4SLinus Torvalds * 1197246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 11981da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 11991da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 12001da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 12011da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 12021da177e4SLinus Torvalds */ 12031da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 12041da177e4SLinus Torvalds { 12051da177e4SLinus Torvalds struct mm_struct *mm; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds task_lock(task); 12081da177e4SLinus Torvalds mm = task->mm; 12091da177e4SLinus Torvalds if (mm) { 1210246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 12111da177e4SLinus Torvalds mm = NULL; 12121da177e4SLinus Torvalds else 12133fce371bSVegard Nossum mmget(mm); 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds task_unlock(task); 12161da177e4SLinus Torvalds return mm; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 12191da177e4SLinus Torvalds 12208cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 12218cdb878dSChristopher Yeoh { 12228cdb878dSChristopher Yeoh struct mm_struct *mm; 12238cdb878dSChristopher Yeoh int err; 12248cdb878dSChristopher Yeoh 12253e74fabdSBernd Edlinger err = mutex_lock_killable(&task->signal->exec_update_mutex); 12268cdb878dSChristopher Yeoh if (err) 12278cdb878dSChristopher Yeoh return ERR_PTR(err); 12288cdb878dSChristopher Yeoh 12298cdb878dSChristopher Yeoh mm = get_task_mm(task); 12308cdb878dSChristopher Yeoh if (mm && mm != current->mm && 12318cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 12328cdb878dSChristopher Yeoh mmput(mm); 12338cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 12348cdb878dSChristopher Yeoh } 12353e74fabdSBernd Edlinger mutex_unlock(&task->signal->exec_update_mutex); 12368cdb878dSChristopher Yeoh 12378cdb878dSChristopher Yeoh return mm; 12388cdb878dSChristopher Yeoh } 12398cdb878dSChristopher Yeoh 124057b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 1241c415c3b4SOleg Nesterov { 1242d68b46feSOleg Nesterov struct completion *vfork; 1243c415c3b4SOleg Nesterov 1244d68b46feSOleg Nesterov task_lock(tsk); 1245d68b46feSOleg Nesterov vfork = tsk->vfork_done; 1246d68b46feSOleg Nesterov if (likely(vfork)) { 1247c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 1248d68b46feSOleg Nesterov complete(vfork); 1249d68b46feSOleg Nesterov } 1250d68b46feSOleg Nesterov task_unlock(tsk); 1251d68b46feSOleg Nesterov } 1252d68b46feSOleg Nesterov 1253d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 1254d68b46feSOleg Nesterov struct completion *vfork) 1255d68b46feSOleg Nesterov { 1256d68b46feSOleg Nesterov int killed; 1257d68b46feSOleg Nesterov 1258d68b46feSOleg Nesterov freezer_do_not_count(); 125976f969e8SRoman Gushchin cgroup_enter_frozen(); 1260d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 126176f969e8SRoman Gushchin cgroup_leave_frozen(false); 1262d68b46feSOleg Nesterov freezer_count(); 1263d68b46feSOleg Nesterov 1264d68b46feSOleg Nesterov if (killed) { 1265d68b46feSOleg Nesterov task_lock(child); 1266d68b46feSOleg Nesterov child->vfork_done = NULL; 1267d68b46feSOleg Nesterov task_unlock(child); 1268d68b46feSOleg Nesterov } 1269d68b46feSOleg Nesterov 1270d68b46feSOleg Nesterov put_task_struct(child); 1271d68b46feSOleg Nesterov return killed; 1272c415c3b4SOleg Nesterov } 1273c415c3b4SOleg Nesterov 12741da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 12751da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 12761da177e4SLinus Torvalds * error success whatever. 12771da177e4SLinus Torvalds * 12781da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 12791da177e4SLinus Torvalds * from the current process. 12801da177e4SLinus Torvalds * 12811da177e4SLinus Torvalds * This difference is important for error handling, when we 12821da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 12831da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 12841da177e4SLinus Torvalds * restoring the old one. . . 12851da177e4SLinus Torvalds * Eric Biederman 10 January 1998 12861da177e4SLinus Torvalds */ 12874610ba7aSThomas Gleixner static void mm_release(struct task_struct *tsk, struct mm_struct *mm) 12881da177e4SLinus Torvalds { 12890326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 12900326f5a9SSrikar Dronamraju 12911da177e4SLinus Torvalds /* Get rid of any cached register state */ 12921da177e4SLinus Torvalds deactivate_mm(tsk, mm); 12931da177e4SLinus Torvalds 1294fec1d011SRoland McGrath /* 1295735f2770SMichal Hocko * Signal userspace if we're not exiting with a core dump 1296735f2770SMichal Hocko * because we want to leave the value intact for debugging 1297735f2770SMichal Hocko * purposes. 1298fec1d011SRoland McGrath */ 12999c8a8228SEric Dumazet if (tsk->clear_child_tid) { 1300735f2770SMichal Hocko if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) && 13019c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 13021da177e4SLinus Torvalds /* 13031da177e4SLinus Torvalds * We don't check the error code - if userspace has 13041da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 13051da177e4SLinus Torvalds */ 13069c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 13072de0db99SDominik Brodowski do_futex(tsk->clear_child_tid, FUTEX_WAKE, 13082de0db99SDominik Brodowski 1, NULL, NULL, 0, 0); 13099c8a8228SEric Dumazet } 13109c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 13111da177e4SLinus Torvalds } 1312f7505d64SKonstantin Khlebnikov 1313f7505d64SKonstantin Khlebnikov /* 1314f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 1315f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 1316f7505d64SKonstantin Khlebnikov */ 1317f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 1318f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13214610ba7aSThomas Gleixner void exit_mm_release(struct task_struct *tsk, struct mm_struct *mm) 13224610ba7aSThomas Gleixner { 1323150d7158SThomas Gleixner futex_exit_release(tsk); 13244610ba7aSThomas Gleixner mm_release(tsk, mm); 13254610ba7aSThomas Gleixner } 13264610ba7aSThomas Gleixner 13274610ba7aSThomas Gleixner void exec_mm_release(struct task_struct *tsk, struct mm_struct *mm) 13284610ba7aSThomas Gleixner { 1329150d7158SThomas Gleixner futex_exec_release(tsk); 13304610ba7aSThomas Gleixner mm_release(tsk, mm); 13314610ba7aSThomas Gleixner } 13324610ba7aSThomas Gleixner 133313585fa0SNadav Amit /** 133413585fa0SNadav Amit * dup_mm() - duplicates an existing mm structure 133513585fa0SNadav Amit * @tsk: the task_struct with which the new mm will be associated. 133613585fa0SNadav Amit * @oldmm: the mm to duplicate. 133713585fa0SNadav Amit * 133813585fa0SNadav Amit * Allocates a new mm structure and duplicates the provided @oldmm structure 133913585fa0SNadav Amit * content into it. 134013585fa0SNadav Amit * 134113585fa0SNadav Amit * Return: the duplicated mm or NULL on failure. 1342a0a7ec30SJANAK DESAI */ 134313585fa0SNadav Amit static struct mm_struct *dup_mm(struct task_struct *tsk, 134413585fa0SNadav Amit struct mm_struct *oldmm) 1345a0a7ec30SJANAK DESAI { 134613585fa0SNadav Amit struct mm_struct *mm; 1347a0a7ec30SJANAK DESAI int err; 1348a0a7ec30SJANAK DESAI 1349a0a7ec30SJANAK DESAI mm = allocate_mm(); 1350a0a7ec30SJANAK DESAI if (!mm) 1351a0a7ec30SJANAK DESAI goto fail_nomem; 1352a0a7ec30SJANAK DESAI 1353a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 1354a0a7ec30SJANAK DESAI 1355bfedb589SEric W. Biederman if (!mm_init(mm, tsk, mm->user_ns)) 1356a0a7ec30SJANAK DESAI goto fail_nomem; 1357a0a7ec30SJANAK DESAI 1358a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 1359a0a7ec30SJANAK DESAI if (err) 1360a0a7ec30SJANAK DESAI goto free_pt; 1361a0a7ec30SJANAK DESAI 1362a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 1363a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 1364a0a7ec30SJANAK DESAI 1365801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1366801460d0SHiroshi Shimamoto goto free_pt; 1367801460d0SHiroshi Shimamoto 1368a0a7ec30SJANAK DESAI return mm; 1369a0a7ec30SJANAK DESAI 1370a0a7ec30SJANAK DESAI free_pt: 1371801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 1372801460d0SHiroshi Shimamoto mm->binfmt = NULL; 1373c3f3ce04SAndrea Arcangeli mm_init_owner(mm, NULL); 1374a0a7ec30SJANAK DESAI mmput(mm); 1375a0a7ec30SJANAK DESAI 1376a0a7ec30SJANAK DESAI fail_nomem: 1377a0a7ec30SJANAK DESAI return NULL; 1378a0a7ec30SJANAK DESAI } 1379a0a7ec30SJANAK DESAI 13801da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 13811da177e4SLinus Torvalds { 13821da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 13831da177e4SLinus Torvalds int retval; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 13861da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 138717406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 138817406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 1389a2e51445SDmitry Vyukov tsk->last_switch_time = 0; 139017406b82SMandeep Singh Baines #endif 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds tsk->mm = NULL; 13931da177e4SLinus Torvalds tsk->active_mm = NULL; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds /* 13961da177e4SLinus Torvalds * Are we cloning a kernel thread? 13971da177e4SLinus Torvalds * 13981da177e4SLinus Torvalds * We need to steal a active VM for that.. 13991da177e4SLinus Torvalds */ 14001da177e4SLinus Torvalds oldmm = current->mm; 14011da177e4SLinus Torvalds if (!oldmm) 14021da177e4SLinus Torvalds return 0; 14031da177e4SLinus Torvalds 1404615d6e87SDavidlohr Bueso /* initialize the new vmacache entries */ 1405615d6e87SDavidlohr Bueso vmacache_flush(tsk); 1406615d6e87SDavidlohr Bueso 14071da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 14083fce371bSVegard Nossum mmget(oldmm); 14091da177e4SLinus Torvalds mm = oldmm; 14101da177e4SLinus Torvalds goto good_mm; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds retval = -ENOMEM; 141413585fa0SNadav Amit mm = dup_mm(tsk, current->mm); 14151da177e4SLinus Torvalds if (!mm) 14161da177e4SLinus Torvalds goto fail_nomem; 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds good_mm: 14191da177e4SLinus Torvalds tsk->mm = mm; 14201da177e4SLinus Torvalds tsk->active_mm = mm; 14211da177e4SLinus Torvalds return 0; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds fail_nomem: 14241da177e4SLinus Torvalds return retval; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 1427a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 14281da177e4SLinus Torvalds { 1429498052bbSAl Viro struct fs_struct *fs = current->fs; 14301da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 1431498052bbSAl Viro /* tsk->fs is already what we want */ 14322a4419b5SNick Piggin spin_lock(&fs->lock); 1433498052bbSAl Viro if (fs->in_exec) { 14342a4419b5SNick Piggin spin_unlock(&fs->lock); 1435498052bbSAl Viro return -EAGAIN; 1436498052bbSAl Viro } 1437498052bbSAl Viro fs->users++; 14382a4419b5SNick Piggin spin_unlock(&fs->lock); 14391da177e4SLinus Torvalds return 0; 14401da177e4SLinus Torvalds } 1441498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 14421da177e4SLinus Torvalds if (!tsk->fs) 14431da177e4SLinus Torvalds return -ENOMEM; 14441da177e4SLinus Torvalds return 0; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 1447a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 1448a016f338SJANAK DESAI { 1449a016f338SJANAK DESAI struct files_struct *oldf, *newf; 1450a016f338SJANAK DESAI int error = 0; 1451a016f338SJANAK DESAI 1452a016f338SJANAK DESAI /* 1453a016f338SJANAK DESAI * A background process may not have any files ... 1454a016f338SJANAK DESAI */ 1455a016f338SJANAK DESAI oldf = current->files; 1456a016f338SJANAK DESAI if (!oldf) 1457a016f338SJANAK DESAI goto out; 1458a016f338SJANAK DESAI 1459a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 1460a016f338SJANAK DESAI atomic_inc(&oldf->count); 1461a016f338SJANAK DESAI goto out; 1462a016f338SJANAK DESAI } 1463a016f338SJANAK DESAI 146460997c3dSChristian Brauner newf = dup_fd(oldf, NR_OPEN_MAX, &error); 1465a016f338SJANAK DESAI if (!newf) 1466a016f338SJANAK DESAI goto out; 1467a016f338SJANAK DESAI 1468a016f338SJANAK DESAI tsk->files = newf; 1469a016f338SJANAK DESAI error = 0; 1470a016f338SJANAK DESAI out: 1471a016f338SJANAK DESAI return error; 1472a016f338SJANAK DESAI } 1473a016f338SJANAK DESAI 1474fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 1475fd0928dfSJens Axboe { 1476fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 1477fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 14786e736be7STejun Heo struct io_context *new_ioc; 1479fd0928dfSJens Axboe 1480fd0928dfSJens Axboe if (!ioc) 1481fd0928dfSJens Axboe return 0; 1482fadad878SJens Axboe /* 1483fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 1484fadad878SJens Axboe */ 1485fadad878SJens Axboe if (clone_flags & CLONE_IO) { 14863d48749dSTejun Heo ioc_task_link(ioc); 14873d48749dSTejun Heo tsk->io_context = ioc; 1488fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 14896e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 14906e736be7STejun Heo if (unlikely(!new_ioc)) 1491fd0928dfSJens Axboe return -ENOMEM; 1492fd0928dfSJens Axboe 14936e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 149411a3122fSTejun Heo put_io_context(new_ioc); 1495fd0928dfSJens Axboe } 1496fd0928dfSJens Axboe #endif 1497fd0928dfSJens Axboe return 0; 1498fd0928dfSJens Axboe } 1499fd0928dfSJens Axboe 1500a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 15011da177e4SLinus Torvalds { 15021da177e4SLinus Torvalds struct sighand_struct *sig; 15031da177e4SLinus Torvalds 150460348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 1505d036bda7SElena Reshetova refcount_inc(¤t->sighand->count); 15061da177e4SLinus Torvalds return 0; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 15090c282b06SMadhuparna Bhowmik RCU_INIT_POINTER(tsk->sighand, sig); 15101da177e4SLinus Torvalds if (!sig) 15111da177e4SLinus Torvalds return -ENOMEM; 15129d7fb042SPeter Zijlstra 1513d036bda7SElena Reshetova refcount_set(&sig->count, 1); 151406e62a46SJann Horn spin_lock_irq(¤t->sighand->siglock); 15151da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 151606e62a46SJann Horn spin_unlock_irq(¤t->sighand->siglock); 1517b612e5dfSChristian Brauner 1518b612e5dfSChristian Brauner /* Reset all signal handler not set to SIG_IGN to SIG_DFL. */ 1519b612e5dfSChristian Brauner if (clone_flags & CLONE_CLEAR_SIGHAND) 1520b612e5dfSChristian Brauner flush_signal_handlers(tsk, 0); 1521b612e5dfSChristian Brauner 15221da177e4SLinus Torvalds return 0; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 1525a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1526c81addc9SOleg Nesterov { 1527d036bda7SElena Reshetova if (refcount_dec_and_test(&sighand->count)) { 1528d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1529392809b2SOleg Nesterov /* 15305f0d5a3aSPaul E. McKenney * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it 1531392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1532392809b2SOleg Nesterov */ 1533c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1534c81addc9SOleg Nesterov } 1535d80e731eSOleg Nesterov } 1536c81addc9SOleg Nesterov 1537f06febc9SFrank Mayhar /* 1538f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1539f06febc9SFrank Mayhar */ 1540f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1541f06febc9SFrank Mayhar { 15422b69942fSThomas Gleixner struct posix_cputimers *pct = &sig->posix_cputimers; 154378d7d407SJiri Slaby unsigned long cpu_limit; 154478d7d407SJiri Slaby 1545316c1608SJason Low cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 15463a245c0fSThomas Gleixner posix_cputimers_group_init(pct, cpu_limit); 15476279a751SOleg Nesterov } 15486279a751SOleg Nesterov 1549a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 15501da177e4SLinus Torvalds { 15511da177e4SLinus Torvalds struct signal_struct *sig; 15521da177e4SLinus Torvalds 15534ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1554490dea45SPeter Zijlstra return 0; 15556279a751SOleg Nesterov 1556a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 15571da177e4SLinus Torvalds tsk->signal = sig; 15581da177e4SLinus Torvalds if (!sig) 15591da177e4SLinus Torvalds return -ENOMEM; 15601da177e4SLinus Torvalds 1561b3ac022cSOleg Nesterov sig->nr_threads = 1; 15621da177e4SLinus Torvalds atomic_set(&sig->live, 1); 156360d4de3fSElena Reshetova refcount_set(&sig->sigcnt, 1); 15640c740d0aSOleg Nesterov 15650c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 15660c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 15670c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 15680c740d0aSOleg Nesterov 15691da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1570db51aeccSOleg Nesterov sig->curr_target = tsk; 15711da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 1572c3ad2c3bSEric W. Biederman INIT_HLIST_HEAD(&sig->multiprocess); 1573e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 15749d7fb042SPeter Zijlstra prev_cputime_init(&sig->prev_cputime); 15751da177e4SLinus Torvalds 1576baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 1577b18b6a9cSNicolas Pitre INIT_LIST_HEAD(&sig->posix_timers); 1578c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 15791da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 1580baa73d9eSNicolas Pitre #endif 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds task_lock(current->group_leader); 15831da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 15841da177e4SLinus Torvalds task_unlock(current->group_leader); 15851da177e4SLinus Torvalds 15866279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 15876279a751SOleg Nesterov 1588522ed776SMiloslav Trmac tty_audit_fork(sig); 15895091faa4SMike Galbraith sched_autogroup_fork(sig); 1590522ed776SMiloslav Trmac 1591a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1592dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 159328b83c51SKOSAKI Motohiro 15949b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 1595eea96732SEric W. Biederman mutex_init(&sig->exec_update_mutex); 15969b1bf12dSKOSAKI Motohiro 15971da177e4SLinus Torvalds return 0; 15981da177e4SLinus Torvalds } 15991da177e4SLinus Torvalds 1600dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1601dbd95212SKees Cook { 1602dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1603dbd95212SKees Cook /* 1604dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1605dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1606dbd95212SKees Cook * needed because this new task is not yet running and cannot 1607dbd95212SKees Cook * be racing exec. 1608dbd95212SKees Cook */ 160969f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1610dbd95212SKees Cook 1611dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1612dbd95212SKees Cook get_seccomp_filter(current); 1613dbd95212SKees Cook p->seccomp = current->seccomp; 1614dbd95212SKees Cook 1615dbd95212SKees Cook /* 1616dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1617dbd95212SKees Cook * between the task_struct being duplicated and holding the 1618dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1619dbd95212SKees Cook */ 1620dbd95212SKees Cook if (task_no_new_privs(current)) 1621dbd95212SKees Cook task_set_no_new_privs(p); 1622dbd95212SKees Cook 1623dbd95212SKees Cook /* 1624dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1625dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1626dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1627dbd95212SKees Cook */ 1628dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1629dbd95212SKees Cook set_tsk_thread_flag(p, TIF_SECCOMP); 1630dbd95212SKees Cook #endif 1631dbd95212SKees Cook } 1632dbd95212SKees Cook 163317da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 16341da177e4SLinus Torvalds { 16351da177e4SLinus Torvalds current->clear_child_tid = tidptr; 16361da177e4SLinus Torvalds 1637b488893aSPavel Emelyanov return task_pid_vnr(current); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 1640a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 164123f78d4aSIngo Molnar { 16421d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1643e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1644a23ba907SDavidlohr Bueso p->pi_waiters = RB_ROOT_CACHED; 1645e96a7705SXunlei Pang p->pi_top_task = NULL; 164623f78d4aSIngo Molnar p->pi_blocked_on = NULL; 164723f78d4aSIngo Molnar #endif 164823f78d4aSIngo Molnar } 164923f78d4aSIngo Molnar 16502c470475SEric W. Biederman static inline void init_task_pid_links(struct task_struct *task) 16512c470475SEric W. Biederman { 16522c470475SEric W. Biederman enum pid_type type; 16532c470475SEric W. Biederman 16542c470475SEric W. Biederman for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 16552c470475SEric W. Biederman INIT_HLIST_NODE(&task->pid_links[type]); 16562c470475SEric W. Biederman } 16572c470475SEric W. Biederman } 16582c470475SEric W. Biederman 165981907739SOleg Nesterov static inline void 166081907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 166181907739SOleg Nesterov { 16622c470475SEric W. Biederman if (type == PIDTYPE_PID) 16632c470475SEric W. Biederman task->thread_pid = pid; 16642c470475SEric W. Biederman else 16652c470475SEric W. Biederman task->signal->pids[type] = pid; 166681907739SOleg Nesterov } 166781907739SOleg Nesterov 16686bfbaa51SIngo Molnar static inline void rcu_copy_process(struct task_struct *p) 16696bfbaa51SIngo Molnar { 16706bfbaa51SIngo Molnar #ifdef CONFIG_PREEMPT_RCU 16716bfbaa51SIngo Molnar p->rcu_read_lock_nesting = 0; 16726bfbaa51SIngo Molnar p->rcu_read_unlock_special.s = 0; 16736bfbaa51SIngo Molnar p->rcu_blocked_node = NULL; 16746bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_node_entry); 16756bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_PREEMPT_RCU */ 16766bfbaa51SIngo Molnar #ifdef CONFIG_TASKS_RCU 16776bfbaa51SIngo Molnar p->rcu_tasks_holdout = false; 16786bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); 16796bfbaa51SIngo Molnar p->rcu_tasks_idle_cpu = -1; 16806bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_TASKS_RCU */ 1681d5f177d3SPaul E. McKenney #ifdef CONFIG_TASKS_TRACE_RCU 1682d5f177d3SPaul E. McKenney p->trc_reader_nesting = 0; 1683276c4104SPaul E. McKenney p->trc_reader_special.s = 0; 1684d5f177d3SPaul E. McKenney INIT_LIST_HEAD(&p->trc_holdout_list); 1685d5f177d3SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */ 16866bfbaa51SIngo Molnar } 16876bfbaa51SIngo Molnar 16883695eae5SChristian Brauner struct pid *pidfd_pid(const struct file *file) 16893695eae5SChristian Brauner { 16903695eae5SChristian Brauner if (file->f_op == &pidfd_fops) 16913695eae5SChristian Brauner return file->private_data; 16923695eae5SChristian Brauner 16933695eae5SChristian Brauner return ERR_PTR(-EBADF); 16943695eae5SChristian Brauner } 16953695eae5SChristian Brauner 1696b3e58382SChristian Brauner static int pidfd_release(struct inode *inode, struct file *file) 1697b3e58382SChristian Brauner { 1698b3e58382SChristian Brauner struct pid *pid = file->private_data; 1699b3e58382SChristian Brauner 1700b3e58382SChristian Brauner file->private_data = NULL; 1701b3e58382SChristian Brauner put_pid(pid); 1702b3e58382SChristian Brauner return 0; 1703b3e58382SChristian Brauner } 1704b3e58382SChristian Brauner 1705b3e58382SChristian Brauner #ifdef CONFIG_PROC_FS 170615d42eb2SChristian Kellner /** 170715d42eb2SChristian Kellner * pidfd_show_fdinfo - print information about a pidfd 170815d42eb2SChristian Kellner * @m: proc fdinfo file 170915d42eb2SChristian Kellner * @f: file referencing a pidfd 171015d42eb2SChristian Kellner * 171115d42eb2SChristian Kellner * Pid: 171215d42eb2SChristian Kellner * This function will print the pid that a given pidfd refers to in the 171315d42eb2SChristian Kellner * pid namespace of the procfs instance. 171415d42eb2SChristian Kellner * If the pid namespace of the process is not a descendant of the pid 171515d42eb2SChristian Kellner * namespace of the procfs instance 0 will be shown as its pid. This is 171615d42eb2SChristian Kellner * similar to calling getppid() on a process whose parent is outside of 171715d42eb2SChristian Kellner * its pid namespace. 171815d42eb2SChristian Kellner * 171915d42eb2SChristian Kellner * NSpid: 172015d42eb2SChristian Kellner * If pid namespaces are supported then this function will also print 172115d42eb2SChristian Kellner * the pid of a given pidfd refers to for all descendant pid namespaces 172215d42eb2SChristian Kellner * starting from the current pid namespace of the instance, i.e. the 172315d42eb2SChristian Kellner * Pid field and the first entry in the NSpid field will be identical. 172415d42eb2SChristian Kellner * If the pid namespace of the process is not a descendant of the pid 172515d42eb2SChristian Kellner * namespace of the procfs instance 0 will be shown as its first NSpid 172615d42eb2SChristian Kellner * entry and no others will be shown. 172715d42eb2SChristian Kellner * Note that this differs from the Pid and NSpid fields in 172815d42eb2SChristian Kellner * /proc/<pid>/status where Pid and NSpid are always shown relative to 172915d42eb2SChristian Kellner * the pid namespace of the procfs instance. The difference becomes 173015d42eb2SChristian Kellner * obvious when sending around a pidfd between pid namespaces from a 173115d42eb2SChristian Kellner * different branch of the tree, i.e. where no ancestoral relation is 173215d42eb2SChristian Kellner * present between the pid namespaces: 173315d42eb2SChristian Kellner * - create two new pid namespaces ns1 and ns2 in the initial pid 173415d42eb2SChristian Kellner * namespace (also take care to create new mount namespaces in the 173515d42eb2SChristian Kellner * new pid namespace and mount procfs) 173615d42eb2SChristian Kellner * - create a process with a pidfd in ns1 173715d42eb2SChristian Kellner * - send pidfd from ns1 to ns2 173815d42eb2SChristian Kellner * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid 173915d42eb2SChristian Kellner * have exactly one entry, which is 0 174015d42eb2SChristian Kellner */ 1741b3e58382SChristian Brauner static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) 1742b3e58382SChristian Brauner { 1743b3e58382SChristian Brauner struct pid *pid = f->private_data; 17443d6d8da4SChristian Brauner struct pid_namespace *ns; 17453d6d8da4SChristian Brauner pid_t nr = -1; 1746b3e58382SChristian Brauner 17473d6d8da4SChristian Brauner if (likely(pid_has_task(pid, PIDTYPE_PID))) { 17489d78edeaSAlexey Gladkov ns = proc_pid_ns(file_inode(m->file)->i_sb); 17493d6d8da4SChristian Brauner nr = pid_nr_ns(pid, ns); 17503d6d8da4SChristian Brauner } 17513d6d8da4SChristian Brauner 17523d6d8da4SChristian Brauner seq_put_decimal_ll(m, "Pid:\t", nr); 175315d42eb2SChristian Kellner 175415d42eb2SChristian Kellner #ifdef CONFIG_PID_NS 17553d6d8da4SChristian Brauner seq_put_decimal_ll(m, "\nNSpid:\t", nr); 17563d6d8da4SChristian Brauner if (nr > 0) { 175715d42eb2SChristian Kellner int i; 175815d42eb2SChristian Kellner 175915d42eb2SChristian Kellner /* If nr is non-zero it means that 'pid' is valid and that 176015d42eb2SChristian Kellner * ns, i.e. the pid namespace associated with the procfs 176115d42eb2SChristian Kellner * instance, is in the pid namespace hierarchy of pid. 176215d42eb2SChristian Kellner * Start at one below the already printed level. 176315d42eb2SChristian Kellner */ 176415d42eb2SChristian Kellner for (i = ns->level + 1; i <= pid->level; i++) 17653d6d8da4SChristian Brauner seq_put_decimal_ll(m, "\t", pid->numbers[i].nr); 176615d42eb2SChristian Kellner } 176715d42eb2SChristian Kellner #endif 1768b3e58382SChristian Brauner seq_putc(m, '\n'); 1769b3e58382SChristian Brauner } 1770b3e58382SChristian Brauner #endif 1771b3e58382SChristian Brauner 1772b53b0b9dSJoel Fernandes (Google) /* 1773b53b0b9dSJoel Fernandes (Google) * Poll support for process exit notification. 1774b53b0b9dSJoel Fernandes (Google) */ 17759e77716aSLuc Van Oostenryck static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) 1776b53b0b9dSJoel Fernandes (Google) { 1777b53b0b9dSJoel Fernandes (Google) struct pid *pid = file->private_data; 17789e77716aSLuc Van Oostenryck __poll_t poll_flags = 0; 1779b53b0b9dSJoel Fernandes (Google) 1780b53b0b9dSJoel Fernandes (Google) poll_wait(file, &pid->wait_pidfd, pts); 1781b53b0b9dSJoel Fernandes (Google) 1782b53b0b9dSJoel Fernandes (Google) /* 1783b53b0b9dSJoel Fernandes (Google) * Inform pollers only when the whole thread group exits. 1784b53b0b9dSJoel Fernandes (Google) * If the thread group leader exits before all other threads in the 1785b53b0b9dSJoel Fernandes (Google) * group, then poll(2) should block, similar to the wait(2) family. 1786b53b0b9dSJoel Fernandes (Google) */ 178738fd525aSEric W. Biederman if (thread_group_exited(pid)) 17889e77716aSLuc Van Oostenryck poll_flags = EPOLLIN | EPOLLRDNORM; 1789b53b0b9dSJoel Fernandes (Google) 1790b53b0b9dSJoel Fernandes (Google) return poll_flags; 1791b53b0b9dSJoel Fernandes (Google) } 1792b53b0b9dSJoel Fernandes (Google) 1793b3e58382SChristian Brauner const struct file_operations pidfd_fops = { 1794b3e58382SChristian Brauner .release = pidfd_release, 1795b53b0b9dSJoel Fernandes (Google) .poll = pidfd_poll, 1796b3e58382SChristian Brauner #ifdef CONFIG_PROC_FS 1797b3e58382SChristian Brauner .show_fdinfo = pidfd_show_fdinfo, 1798b3e58382SChristian Brauner #endif 1799b3e58382SChristian Brauner }; 1800b3e58382SChristian Brauner 1801c3f3ce04SAndrea Arcangeli static void __delayed_free_task(struct rcu_head *rhp) 1802c3f3ce04SAndrea Arcangeli { 1803c3f3ce04SAndrea Arcangeli struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); 1804c3f3ce04SAndrea Arcangeli 1805c3f3ce04SAndrea Arcangeli free_task(tsk); 1806c3f3ce04SAndrea Arcangeli } 1807c3f3ce04SAndrea Arcangeli 1808c3f3ce04SAndrea Arcangeli static __always_inline void delayed_free_task(struct task_struct *tsk) 1809c3f3ce04SAndrea Arcangeli { 1810c3f3ce04SAndrea Arcangeli if (IS_ENABLED(CONFIG_MEMCG)) 1811c3f3ce04SAndrea Arcangeli call_rcu(&tsk->rcu, __delayed_free_task); 1812c3f3ce04SAndrea Arcangeli else 1813c3f3ce04SAndrea Arcangeli free_task(tsk); 1814c3f3ce04SAndrea Arcangeli } 1815c3f3ce04SAndrea Arcangeli 181667197a4fSSuren Baghdasaryan static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk) 181767197a4fSSuren Baghdasaryan { 181867197a4fSSuren Baghdasaryan /* Skip if kernel thread */ 181967197a4fSSuren Baghdasaryan if (!tsk->mm) 182067197a4fSSuren Baghdasaryan return; 182167197a4fSSuren Baghdasaryan 182267197a4fSSuren Baghdasaryan /* Skip if spawning a thread or using vfork */ 182367197a4fSSuren Baghdasaryan if ((clone_flags & (CLONE_VM | CLONE_THREAD | CLONE_VFORK)) != CLONE_VM) 182467197a4fSSuren Baghdasaryan return; 182567197a4fSSuren Baghdasaryan 182667197a4fSSuren Baghdasaryan /* We need to synchronize with __set_oom_adj */ 182767197a4fSSuren Baghdasaryan mutex_lock(&oom_adj_mutex); 182867197a4fSSuren Baghdasaryan set_bit(MMF_MULTIPROCESS, &tsk->mm->flags); 182967197a4fSSuren Baghdasaryan /* Update the values in case they were changed after copy_signal */ 183067197a4fSSuren Baghdasaryan tsk->signal->oom_score_adj = current->signal->oom_score_adj; 183167197a4fSSuren Baghdasaryan tsk->signal->oom_score_adj_min = current->signal->oom_score_adj_min; 183267197a4fSSuren Baghdasaryan mutex_unlock(&oom_adj_mutex); 183367197a4fSSuren Baghdasaryan } 183467197a4fSSuren Baghdasaryan 1835f06febc9SFrank Mayhar /* 18361da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 18371da177e4SLinus Torvalds * but does not actually start it yet. 18381da177e4SLinus Torvalds * 18391da177e4SLinus Torvalds * It copies the registers, and all the appropriate 18401da177e4SLinus Torvalds * parts of the process environment (as per the clone 18411da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 18421da177e4SLinus Torvalds */ 18430766f788SEmese Revfy static __latent_entropy struct task_struct *copy_process( 184409a05394SRoland McGrath struct pid *pid, 18453033f14aSJosh Triplett int trace, 18467f192e3cSChristian Brauner int node, 18477f192e3cSChristian Brauner struct kernel_clone_args *args) 18481da177e4SLinus Torvalds { 1849b3e58382SChristian Brauner int pidfd = -1, retval; 1850a24efe62SMariusz Kozlowski struct task_struct *p; 1851c3ad2c3bSEric W. Biederman struct multiprocess_signals delayed; 18526fd2fe49SAl Viro struct file *pidfile = NULL; 18537f192e3cSChristian Brauner u64 clone_flags = args->flags; 1854769071acSAndrei Vagin struct nsproxy *nsp = current->nsproxy; 18551da177e4SLinus Torvalds 1856667b6094SMarcos Paulo de Souza /* 1857667b6094SMarcos Paulo de Souza * Don't allow sharing the root directory with processes in a different 1858667b6094SMarcos Paulo de Souza * namespace 1859667b6094SMarcos Paulo de Souza */ 18601da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 18611da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 18621da177e4SLinus Torvalds 1863e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1864e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1865e66eded8SEric W. Biederman 18661da177e4SLinus Torvalds /* 18671da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 18681da177e4SLinus Torvalds * can only be started up within the thread group. 18691da177e4SLinus Torvalds */ 18701da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 18711da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds /* 18741da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 18751da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 18761da177e4SLinus Torvalds * for various simplifications in other code. 18771da177e4SLinus Torvalds */ 18781da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 18791da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 18801da177e4SLinus Torvalds 1881123be07bSSukadev Bhattiprolu /* 1882123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1883123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1884123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1885123be07bSSukadev Bhattiprolu * from creating siblings. 1886123be07bSSukadev Bhattiprolu */ 1887123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1888123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1889123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1890123be07bSSukadev Bhattiprolu 18918382fcacSEric W. Biederman /* 189240a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 1893faf00da5SEric W. Biederman * do not allow it to share a thread group with the forking task. 18948382fcacSEric W. Biederman */ 1895faf00da5SEric W. Biederman if (clone_flags & CLONE_THREAD) { 189640a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 1897769071acSAndrei Vagin (task_active_pid_ns(current) != nsp->pid_ns_for_children)) 1898769071acSAndrei Vagin return ERR_PTR(-EINVAL); 1899769071acSAndrei Vagin } 1900769071acSAndrei Vagin 1901769071acSAndrei Vagin /* 1902769071acSAndrei Vagin * If the new process will be in a different time namespace 1903769071acSAndrei Vagin * do not allow it to share VM or a thread group with the forking task. 1904769071acSAndrei Vagin */ 1905769071acSAndrei Vagin if (clone_flags & (CLONE_THREAD | CLONE_VM)) { 1906769071acSAndrei Vagin if (nsp->time_ns != nsp->time_ns_for_children) 19078382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 190840a0d32dSOleg Nesterov } 19098382fcacSEric W. Biederman 1910b3e58382SChristian Brauner if (clone_flags & CLONE_PIDFD) { 1911b3e58382SChristian Brauner /* 1912b3e58382SChristian Brauner * - CLONE_DETACHED is blocked so that we can potentially 1913b3e58382SChristian Brauner * reuse it later for CLONE_PIDFD. 1914b3e58382SChristian Brauner * - CLONE_THREAD is blocked until someone really needs it. 1915b3e58382SChristian Brauner */ 19167f192e3cSChristian Brauner if (clone_flags & (CLONE_DETACHED | CLONE_THREAD)) 1917b3e58382SChristian Brauner return ERR_PTR(-EINVAL); 1918b3e58382SChristian Brauner } 1919b3e58382SChristian Brauner 1920c3ad2c3bSEric W. Biederman /* 1921c3ad2c3bSEric W. Biederman * Force any signals received before this point to be delivered 1922c3ad2c3bSEric W. Biederman * before the fork happens. Collect up signals sent to multiple 1923c3ad2c3bSEric W. Biederman * processes that happen during the fork and delay them so that 1924c3ad2c3bSEric W. Biederman * they appear to happen after the fork. 1925c3ad2c3bSEric W. Biederman */ 1926c3ad2c3bSEric W. Biederman sigemptyset(&delayed.signal); 1927c3ad2c3bSEric W. Biederman INIT_HLIST_NODE(&delayed.node); 1928c3ad2c3bSEric W. Biederman 1929c3ad2c3bSEric W. Biederman spin_lock_irq(¤t->sighand->siglock); 1930c3ad2c3bSEric W. Biederman if (!(clone_flags & CLONE_THREAD)) 1931c3ad2c3bSEric W. Biederman hlist_add_head(&delayed.node, ¤t->signal->multiprocess); 1932c3ad2c3bSEric W. Biederman recalc_sigpending(); 1933c3ad2c3bSEric W. Biederman spin_unlock_irq(¤t->sighand->siglock); 1934c3ad2c3bSEric W. Biederman retval = -ERESTARTNOINTR; 1935c3ad2c3bSEric W. Biederman if (signal_pending(current)) 1936c3ad2c3bSEric W. Biederman goto fork_out; 1937c3ad2c3bSEric W. Biederman 19381da177e4SLinus Torvalds retval = -ENOMEM; 1939725fc629SAndi Kleen p = dup_task_struct(current, node); 19401da177e4SLinus Torvalds if (!p) 19411da177e4SLinus Torvalds goto fork_out; 19421da177e4SLinus Torvalds 19434d6501dcSVegard Nossum /* 19444d6501dcSVegard Nossum * This _must_ happen before we call free_task(), i.e. before we jump 19454d6501dcSVegard Nossum * to any of the bad_fork_* labels. This is to avoid freeing 19464d6501dcSVegard Nossum * p->set_child_tid which is (ab)used as a kthread's data pointer for 19474d6501dcSVegard Nossum * kernel threads (PF_KTHREAD). 19484d6501dcSVegard Nossum */ 19497f192e3cSChristian Brauner p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL; 19504d6501dcSVegard Nossum /* 19514d6501dcSVegard Nossum * Clear TID on mm_release()? 19524d6501dcSVegard Nossum */ 19537f192e3cSChristian Brauner p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL; 19544d6501dcSVegard Nossum 1955f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1956f7e8b616SSteven Rostedt 1957bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1958bea493a0SPeter Zijlstra 1959a21ee605SPeter Zijlstra lockdep_assert_irqs_enabled(); 1960d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1961de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1962de30a2b3SIngo Molnar #endif 19631da177e4SLinus Torvalds retval = -EAGAIN; 19643b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 196578d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1966b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1967b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 19681da177e4SLinus Torvalds goto bad_fork_free; 19691da177e4SLinus Torvalds } 197072fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 19711da177e4SLinus Torvalds 1972f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1973f1752eecSDavid Howells if (retval < 0) 1974f1752eecSDavid Howells goto bad_fork_free; 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds /* 19771da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 19781da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 19791da177e4SLinus Torvalds * to stop root fork bombs. 19801da177e4SLinus Torvalds */ 198104ec93feSLi Zefan retval = -EAGAIN; 1982c17d1a3aSWeilong Chen if (data_race(nr_threads >= max_threads)) 19831da177e4SLinus Torvalds goto bad_fork_cleanup_count; 19841da177e4SLinus Torvalds 1985ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1986c1de45caSPeter Zijlstra p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE); 1987514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 19881da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 19891da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1990f41d911fSPaul E. McKenney rcu_copy_process(p); 19911da177e4SLinus Torvalds p->vfork_done = NULL; 19921da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds init_sigpending(&p->pending); 19951da177e4SLinus Torvalds 199664861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 199740565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 199864861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 199940565b5aSStanislaw Gruszka #endif 20009d7fb042SPeter Zijlstra prev_cputime_init(&p->prev_cputime); 20019d7fb042SPeter Zijlstra 20026a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 2003bac5b6b6SFrederic Weisbecker seqcount_init(&p->vtime.seqcount); 2004bac5b6b6SFrederic Weisbecker p->vtime.starttime = 0; 2005bac5b6b6SFrederic Weisbecker p->vtime.state = VTIME_INACTIVE; 20066a61671bSFrederic Weisbecker #endif 20076a61671bSFrederic Weisbecker 20080f212204SJens Axboe #ifdef CONFIG_IO_URING 20090f212204SJens Axboe p->io_uring = NULL; 20100f212204SJens Axboe #endif 20110f212204SJens Axboe 2012a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 2013a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 2014a3a2e76cSKAMEZAWA Hiroyuki #endif 2015172ba844SBalbir Singh 20166976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 20176976675dSArjan van de Ven 2018eb414681SJohannes Weiner #ifdef CONFIG_PSI 2019eb414681SJohannes Weiner p->psi_flags = 0; 2020eb414681SJohannes Weiner #endif 2021eb414681SJohannes Weiner 20225995477aSAndrea Righi task_io_accounting_init(&p->ioac); 20231da177e4SLinus Torvalds acct_clear_integrals(p); 20241da177e4SLinus Torvalds 20253a245c0fSThomas Gleixner posix_cputimers_init(&p->posix_cputimers); 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds p->io_context = NULL; 2028c0b0ae8aSRichard Guy Briggs audit_set_context(p, NULL); 2029b4f48b63SPaul Menage cgroup_fork(p); 20301da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2031846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 20321da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 20331da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 20341da177e4SLinus Torvalds p->mempolicy = NULL; 2035e8604cb4SLi Zefan goto bad_fork_cleanup_threadgroup_lock; 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds #endif 2038778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 2039778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 2040778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 2041b7505861SAhmed S. Darwish seqcount_spinlock_init(&p->mems_allowed_seq, &p->alloc_lock); 2042778d3b0fSMichal Hocko #endif 2043de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 20440584df9cSMarco Elver memset(&p->irqtrace, 0, sizeof(p->irqtrace)); 20450584df9cSMarco Elver p->irqtrace.hardirq_disable_ip = _THIS_IP_; 20460584df9cSMarco Elver p->irqtrace.softirq_enable_ip = _THIS_IP_; 2047de30a2b3SIngo Molnar p->softirqs_enabled = 1; 2048de30a2b3SIngo Molnar p->softirq_context = 0; 2049de30a2b3SIngo Molnar #endif 20508bcbde54SDavid Hildenbrand 20518bcbde54SDavid Hildenbrand p->pagefault_disabled = 0; 20528bcbde54SDavid Hildenbrand 2053fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 2054b09be676SByungchul Park lockdep_init_task(p); 2055fbb9ce95SIngo Molnar #endif 20561da177e4SLinus Torvalds 2057408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 2058408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 2059408894eeSIngo Molnar #endif 2060cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 2061cafe5635SKent Overstreet p->sequential_io = 0; 2062cafe5635SKent Overstreet p->sequential_io_avg = 0; 2063cafe5635SKent Overstreet #endif 20640f481406SMarkus Metzger 20653c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 2066aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 2067aab03e05SDario Faggioli if (retval) 2068aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 20696ab423e0SPeter Zijlstra 2070cdd6c482SIngo Molnar retval = perf_event_init_task(p); 20716ab423e0SPeter Zijlstra if (retval) 20726ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 2073fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 2074fb0a685cSDaniel Rebelo de Oliveira if (retval) 20756c72e350SPeter Zijlstra goto bad_fork_cleanup_perf; 20761da177e4SLinus Torvalds /* copy all the process information */ 2077ab602f79SJack Miller shm_init_task(p); 2078e4e55b47STetsuo Handa retval = security_task_alloc(p, clone_flags); 2079fb0a685cSDaniel Rebelo de Oliveira if (retval) 20801da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 2081e4e55b47STetsuo Handa retval = copy_semundo(clone_flags, p); 2082e4e55b47STetsuo Handa if (retval) 2083e4e55b47STetsuo Handa goto bad_fork_cleanup_security; 2084fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 2085fb0a685cSDaniel Rebelo de Oliveira if (retval) 20861da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 2087fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 2088fb0a685cSDaniel Rebelo de Oliveira if (retval) 20891da177e4SLinus Torvalds goto bad_fork_cleanup_files; 2090fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 2091fb0a685cSDaniel Rebelo de Oliveira if (retval) 20921da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 2093fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 2094fb0a685cSDaniel Rebelo de Oliveira if (retval) 20951da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 2096fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 2097fb0a685cSDaniel Rebelo de Oliveira if (retval) 20981da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 2099fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 2100fb0a685cSDaniel Rebelo de Oliveira if (retval) 2101d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 2102fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 2103fb0a685cSDaniel Rebelo de Oliveira if (retval) 2104fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 2105714acdbdSChristian Brauner retval = copy_thread(clone_flags, args->stack, args->stack_size, p, args->tls); 21061da177e4SLinus Torvalds if (retval) 2107fd0928dfSJens Axboe goto bad_fork_cleanup_io; 21081da177e4SLinus Torvalds 2109afaef01cSAlexander Popov stackleak_task_init(p); 2110afaef01cSAlexander Popov 2111425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 211249cb2fc4SAdrian Reber pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid, 211349cb2fc4SAdrian Reber args->set_tid_size); 211435f71bc0SMichal Hocko if (IS_ERR(pid)) { 211535f71bc0SMichal Hocko retval = PTR_ERR(pid); 21160740aa5fSJiri Slaby goto bad_fork_cleanup_thread; 2117425fb2b4SPavel Emelyanov } 211835f71bc0SMichal Hocko } 2119425fb2b4SPavel Emelyanov 2120b3e58382SChristian Brauner /* 2121b3e58382SChristian Brauner * This has to happen after we've potentially unshared the file 2122b3e58382SChristian Brauner * descriptor table (so that the pidfd doesn't leak into the child 2123b3e58382SChristian Brauner * if the fd table isn't shared). 2124b3e58382SChristian Brauner */ 2125b3e58382SChristian Brauner if (clone_flags & CLONE_PIDFD) { 21266fd2fe49SAl Viro retval = get_unused_fd_flags(O_RDWR | O_CLOEXEC); 2127b3e58382SChristian Brauner if (retval < 0) 2128b3e58382SChristian Brauner goto bad_fork_free_pid; 2129b3e58382SChristian Brauner 2130b3e58382SChristian Brauner pidfd = retval; 21316fd2fe49SAl Viro 21326fd2fe49SAl Viro pidfile = anon_inode_getfile("[pidfd]", &pidfd_fops, pid, 21336fd2fe49SAl Viro O_RDWR | O_CLOEXEC); 21346fd2fe49SAl Viro if (IS_ERR(pidfile)) { 21356fd2fe49SAl Viro put_unused_fd(pidfd); 213628dd29c0SChristian Brauner retval = PTR_ERR(pidfile); 21376fd2fe49SAl Viro goto bad_fork_free_pid; 21386fd2fe49SAl Viro } 21396fd2fe49SAl Viro get_pid(pid); /* held by pidfile now */ 21406fd2fe49SAl Viro 21417f192e3cSChristian Brauner retval = put_user(pidfd, args->pidfd); 2142b3e58382SChristian Brauner if (retval) 2143b3e58382SChristian Brauner goto bad_fork_put_pidfd; 2144b3e58382SChristian Brauner } 2145b3e58382SChristian Brauner 214673c10101SJens Axboe #ifdef CONFIG_BLOCK 214773c10101SJens Axboe p->plug = NULL; 214873c10101SJens Axboe #endif 2149ba31c1a4SThomas Gleixner futex_init_task(p); 2150ba31c1a4SThomas Gleixner 21511da177e4SLinus Torvalds /* 2152f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 2153f9a3879aSGOTO Masanori */ 2154f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 21552a742138SStas Sergeev sas_ss_reset(p); 2156f9a3879aSGOTO Masanori 2157f9a3879aSGOTO Masanori /* 21586580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 21596580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 21601da177e4SLinus Torvalds */ 21616580807dSOleg Nesterov user_disable_single_step(p); 21621da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 2163ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 2164ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 2165ed75e8d5SLaurent Vivier #endif 2166e02c9b0dSLin Feng clear_tsk_latency_tracing(p); 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds /* ok, now we should be set up.. */ 216918c830dfSOleg Nesterov p->pid = pid_nr(pid); 217018c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 217118c830dfSOleg Nesterov p->group_leader = current->group_leader; 217218c830dfSOleg Nesterov p->tgid = current->tgid; 217318c830dfSOleg Nesterov } else { 217418c830dfSOleg Nesterov p->group_leader = p; 217518c830dfSOleg Nesterov p->tgid = p->pid; 217618c830dfSOleg Nesterov } 21775f8aadd8SOleg Nesterov 21789d823e8fSWu Fengguang p->nr_dirtied = 0; 21799d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 218083712358SWu Fengguang p->dirty_paused_when = 0; 21819d823e8fSWu Fengguang 2182bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 218347e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 2184158e1645SAl Viro p->task_works = NULL; 21851da177e4SLinus Torvalds 218618c830dfSOleg Nesterov /* 21877e47682eSAleksa Sarai * Ensure that the cgroup subsystem policies allow the new process to be 21887b7b8a2cSRandy Dunlap * forked. It should be noted that the new process's css_set can be changed 21897e47682eSAleksa Sarai * between here and cgroup_post_fork() if an organisation operation is in 21907e47682eSAleksa Sarai * progress. 21917e47682eSAleksa Sarai */ 2192ef2c41cfSChristian Brauner retval = cgroup_can_fork(p, args); 21937e47682eSAleksa Sarai if (retval) 21945a5cf5cbSChristian Brauner goto bad_fork_put_pidfd; 21957e47682eSAleksa Sarai 21967e47682eSAleksa Sarai /* 21977b558513SDavid Herrmann * From this point on we must avoid any synchronous user-space 21987b558513SDavid Herrmann * communication until we take the tasklist-lock. In particular, we do 21997b558513SDavid Herrmann * not want user-space to be able to predict the process start-time by 22007b558513SDavid Herrmann * stalling fork(2) after we recorded the start_time but before it is 22017b558513SDavid Herrmann * visible to the system. 22027b558513SDavid Herrmann */ 22037b558513SDavid Herrmann 22047b558513SDavid Herrmann p->start_time = ktime_get_ns(); 2205cf25e24dSPeter Zijlstra p->start_boottime = ktime_get_boottime_ns(); 22067b558513SDavid Herrmann 22077b558513SDavid Herrmann /* 220818c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 220918c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 221018c830dfSOleg Nesterov */ 22111da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 22142d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 22151da177e4SLinus Torvalds p->real_parent = current->real_parent; 22162d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 2217b4e00444SEddy Wu if (clone_flags & CLONE_THREAD) 2218b4e00444SEddy Wu p->exit_signal = -1; 2219b4e00444SEddy Wu else 2220b4e00444SEddy Wu p->exit_signal = current->group_leader->exit_signal; 22212d5516cbSOleg Nesterov } else { 22221da177e4SLinus Torvalds p->real_parent = current; 22232d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 2224b4e00444SEddy Wu p->exit_signal = args->exit_signal; 22252d5516cbSOleg Nesterov } 22261da177e4SLinus Torvalds 2227d83a7cb3SJosh Poimboeuf klp_copy_process(p); 2228d83a7cb3SJosh Poimboeuf 22291da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 22304a2c7a78SOleg Nesterov 22314a2c7a78SOleg Nesterov /* 2232dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 2233dbd95212SKees Cook * before holding sighand lock. 2234dbd95212SKees Cook */ 2235dbd95212SKees Cook copy_seccomp(p); 2236dbd95212SKees Cook 2237d7822b1eSMathieu Desnoyers rseq_fork(p, clone_flags); 2238d7822b1eSMathieu Desnoyers 22394ca1d3eeSEric W. Biederman /* Don't start children in a dying pid namespace */ 2240e8cfbc24SGargi Sharma if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { 22413fd37226SKirill Tkhai retval = -ENOMEM; 22423fd37226SKirill Tkhai goto bad_fork_cancel_cgroup; 22433fd37226SKirill Tkhai } 22444a2c7a78SOleg Nesterov 22457673bf55SEric W. Biederman /* Let kill terminate clone/fork in the middle */ 22467673bf55SEric W. Biederman if (fatal_signal_pending(current)) { 22477673bf55SEric W. Biederman retval = -EINTR; 22487673bf55SEric W. Biederman goto bad_fork_cancel_cgroup; 22497673bf55SEric W. Biederman } 22507673bf55SEric W. Biederman 22516fd2fe49SAl Viro /* past the last point of failure */ 22526fd2fe49SAl Viro if (pidfile) 22536fd2fe49SAl Viro fd_install(pidfd, pidfile); 22541da177e4SLinus Torvalds 22552c470475SEric W. Biederman init_task_pid_links(p); 225673b9ebfeSOleg Nesterov if (likely(p->pid)) { 22574b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 22581da177e4SLinus Torvalds 225981907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 22601da177e4SLinus Torvalds if (thread_group_leader(p)) { 22616883f81aSEric W. Biederman init_task_pid(p, PIDTYPE_TGID, pid); 226281907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 226381907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 226481907739SOleg Nesterov 22651c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 226617cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 22671c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 22681c4042c2SEric W. Biederman } 2269c3ad2c3bSEric W. Biederman p->signal->shared_pending.signal = delayed.signal; 22709c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 2271749860ceSPavel Tikhomirov /* 2272749860ceSPavel Tikhomirov * Inherit has_child_subreaper flag under the same 2273749860ceSPavel Tikhomirov * tasklist_lock with adding child to the process tree 2274749860ceSPavel Tikhomirov * for propagate_has_child_subreaper optimization. 2275749860ceSPavel Tikhomirov */ 2276749860ceSPavel Tikhomirov p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 2277749860ceSPavel Tikhomirov p->real_parent->signal->is_child_subreaper; 22789cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 22795e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 22806883f81aSEric W. Biederman attach_pid(p, PIDTYPE_TGID); 228181907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 228281907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 2283909ea964SChristoph Lameter __this_cpu_inc(process_counts); 228480628ca0SOleg Nesterov } else { 228580628ca0SOleg Nesterov current->signal->nr_threads++; 228680628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 228760d4de3fSElena Reshetova refcount_inc(¤t->signal->sigcnt); 2288924de3b8SEric W. Biederman task_join_group_stop(p); 228980628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 229080628ca0SOleg Nesterov &p->group_leader->thread_group); 22910c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 22920c740d0aSOleg Nesterov &p->signal->thread_head); 22931da177e4SLinus Torvalds } 229481907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 22951da177e4SLinus Torvalds nr_threads++; 229673b9ebfeSOleg Nesterov } 22971da177e4SLinus Torvalds total_forks++; 2298c3ad2c3bSEric W. Biederman hlist_del_init(&delayed.node); 22993f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 23004af4206bSOleg Nesterov syscall_tracepoint_update(p); 23011da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 23024af4206bSOleg Nesterov 2303c13cf856SAndrew Morton proc_fork_connector(p); 230413685c4aSQais Yousef sched_post_fork(p); 2305ef2c41cfSChristian Brauner cgroup_post_fork(p, args); 2306cdd6c482SIngo Molnar perf_event_fork(p); 230743d2b113SKAMEZAWA Hiroyuki 230843d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 23093ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 231043d2b113SKAMEZAWA Hiroyuki 231167197a4fSSuren Baghdasaryan copy_oom_score_adj(clone_flags, p); 231267197a4fSSuren Baghdasaryan 23131da177e4SLinus Torvalds return p; 23141da177e4SLinus Torvalds 23157e47682eSAleksa Sarai bad_fork_cancel_cgroup: 23163fd37226SKirill Tkhai spin_unlock(¤t->sighand->siglock); 23173fd37226SKirill Tkhai write_unlock_irq(&tasklist_lock); 2318ef2c41cfSChristian Brauner cgroup_cancel_fork(p, args); 2319b3e58382SChristian Brauner bad_fork_put_pidfd: 23206fd2fe49SAl Viro if (clone_flags & CLONE_PIDFD) { 23216fd2fe49SAl Viro fput(pidfile); 23226fd2fe49SAl Viro put_unused_fd(pidfd); 23236fd2fe49SAl Viro } 2324425fb2b4SPavel Emelyanov bad_fork_free_pid: 2325425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 2326425fb2b4SPavel Emelyanov free_pid(pid); 23270740aa5fSJiri Slaby bad_fork_cleanup_thread: 23280740aa5fSJiri Slaby exit_thread(p); 2329fd0928dfSJens Axboe bad_fork_cleanup_io: 2330b69f2292SLouis Rilling if (p->io_context) 2331b69f2292SLouis Rilling exit_io_context(p); 2332ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 2333444f378bSLinus Torvalds exit_task_namespaces(p); 23341da177e4SLinus Torvalds bad_fork_cleanup_mm: 2335c3f3ce04SAndrea Arcangeli if (p->mm) { 2336c3f3ce04SAndrea Arcangeli mm_clear_owner(p->mm, p); 23371da177e4SLinus Torvalds mmput(p->mm); 2338c3f3ce04SAndrea Arcangeli } 23391da177e4SLinus Torvalds bad_fork_cleanup_signal: 23404ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 23411c5354deSMike Galbraith free_signal_struct(p->signal); 23421da177e4SLinus Torvalds bad_fork_cleanup_sighand: 2343a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 23441da177e4SLinus Torvalds bad_fork_cleanup_fs: 23451da177e4SLinus Torvalds exit_fs(p); /* blocking */ 23461da177e4SLinus Torvalds bad_fork_cleanup_files: 23471da177e4SLinus Torvalds exit_files(p); /* blocking */ 23481da177e4SLinus Torvalds bad_fork_cleanup_semundo: 23491da177e4SLinus Torvalds exit_sem(p); 2350e4e55b47STetsuo Handa bad_fork_cleanup_security: 2351e4e55b47STetsuo Handa security_task_free(p); 23521da177e4SLinus Torvalds bad_fork_cleanup_audit: 23531da177e4SLinus Torvalds audit_free(p); 23546c72e350SPeter Zijlstra bad_fork_cleanup_perf: 2355cdd6c482SIngo Molnar perf_event_free_task(p); 23566c72e350SPeter Zijlstra bad_fork_cleanup_policy: 2357b09be676SByungchul Park lockdep_free_task(p); 23581da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2359f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 2360e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock: 23611da177e4SLinus Torvalds #endif 236235df17c5SShailabh Nagar delayacct_tsk_free(p); 23631da177e4SLinus Torvalds bad_fork_cleanup_count: 2364d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 2365e0e81739SDavid Howells exit_creds(p); 23661da177e4SLinus Torvalds bad_fork_free: 2367405c0759SAndy Lutomirski p->state = TASK_DEAD; 236868f24b08SAndy Lutomirski put_task_stack(p); 2369c3f3ce04SAndrea Arcangeli delayed_free_task(p); 2370fe7d37d1SOleg Nesterov fork_out: 2371c3ad2c3bSEric W. Biederman spin_lock_irq(¤t->sighand->siglock); 2372c3ad2c3bSEric W. Biederman hlist_del_init(&delayed.node); 2373c3ad2c3bSEric W. Biederman spin_unlock_irq(¤t->sighand->siglock); 2374fe7d37d1SOleg Nesterov return ERR_PTR(retval); 23751da177e4SLinus Torvalds } 23761da177e4SLinus Torvalds 23772c470475SEric W. Biederman static inline void init_idle_pids(struct task_struct *idle) 2378f106eee1SOleg Nesterov { 2379f106eee1SOleg Nesterov enum pid_type type; 2380f106eee1SOleg Nesterov 2381f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 23822c470475SEric W. Biederman INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */ 23832c470475SEric W. Biederman init_task_pid(idle, type, &init_struct_pid); 2384f106eee1SOleg Nesterov } 2385f106eee1SOleg Nesterov } 2386f106eee1SOleg Nesterov 23870db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 23881da177e4SLinus Torvalds { 238936c8b586SIngo Molnar struct task_struct *task; 23907f192e3cSChristian Brauner struct kernel_clone_args args = { 23917f192e3cSChristian Brauner .flags = CLONE_VM, 23927f192e3cSChristian Brauner }; 23937f192e3cSChristian Brauner 23947f192e3cSChristian Brauner task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args); 2395f106eee1SOleg Nesterov if (!IS_ERR(task)) { 23962c470475SEric W. Biederman init_idle_pids(task); 23971da177e4SLinus Torvalds init_idle(task, cpu); 2398f106eee1SOleg Nesterov } 239973b9ebfeSOleg Nesterov 24001da177e4SLinus Torvalds return task; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds 240313585fa0SNadav Amit struct mm_struct *copy_init_mm(void) 240413585fa0SNadav Amit { 240513585fa0SNadav Amit return dup_mm(NULL, &init_mm); 240613585fa0SNadav Amit } 240713585fa0SNadav Amit 24081da177e4SLinus Torvalds /* 24091da177e4SLinus Torvalds * Ok, this is the main fork-routine. 24101da177e4SLinus Torvalds * 24111da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 24121da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 2413a0eb9abdSEugene Syromiatnikov * 2414a0eb9abdSEugene Syromiatnikov * args->exit_signal is expected to be checked for sanity by the caller. 24151da177e4SLinus Torvalds */ 2416cad6967aSChristian Brauner pid_t kernel_clone(struct kernel_clone_args *args) 24171da177e4SLinus Torvalds { 24187f192e3cSChristian Brauner u64 clone_flags = args->flags; 24199f5325aaSMarcos Paulo de Souza struct completion vfork; 24209f5325aaSMarcos Paulo de Souza struct pid *pid; 24211da177e4SLinus Torvalds struct task_struct *p; 24221da177e4SLinus Torvalds int trace = 0; 2423cad6967aSChristian Brauner pid_t nr; 24241da177e4SLinus Torvalds 2425bdff746aSAndrew Morton /* 24263af8588cSChristian Brauner * For legacy clone() calls, CLONE_PIDFD uses the parent_tid argument 24273af8588cSChristian Brauner * to return the pidfd. Hence, CLONE_PIDFD and CLONE_PARENT_SETTID are 24283af8588cSChristian Brauner * mutually exclusive. With clone3() CLONE_PIDFD has grown a separate 24293af8588cSChristian Brauner * field in struct clone_args and it still doesn't make sense to have 24303af8588cSChristian Brauner * them both point at the same memory location. Performing this check 24313af8588cSChristian Brauner * here has the advantage that we don't need to have a separate helper 24323af8588cSChristian Brauner * to check for legacy clone(). 24333af8588cSChristian Brauner */ 24343af8588cSChristian Brauner if ((args->flags & CLONE_PIDFD) && 24353af8588cSChristian Brauner (args->flags & CLONE_PARENT_SETTID) && 24363af8588cSChristian Brauner (args->pidfd == args->parent_tid)) 24373af8588cSChristian Brauner return -EINVAL; 24383af8588cSChristian Brauner 24393af8588cSChristian Brauner /* 24404b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 24414b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 24424b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 24434b9d33e6STejun Heo * for the type of forking is enabled. 244409a05394SRoland McGrath */ 2445e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 24464b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 24474b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 24487f192e3cSChristian Brauner else if (args->exit_signal != SIGCHLD) 24494b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 24504b9d33e6STejun Heo else 24514b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 24524b9d33e6STejun Heo 24534b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 24544b9d33e6STejun Heo trace = 0; 24554b9d33e6STejun Heo } 24561da177e4SLinus Torvalds 24577f192e3cSChristian Brauner p = copy_process(NULL, trace, NUMA_NO_NODE, args); 245838addce8SEmese Revfy add_latent_entropy(); 24599f5325aaSMarcos Paulo de Souza 24609f5325aaSMarcos Paulo de Souza if (IS_ERR(p)) 24619f5325aaSMarcos Paulo de Souza return PTR_ERR(p); 24629f5325aaSMarcos Paulo de Souza 24631da177e4SLinus Torvalds /* 24641da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 24651da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 24661da177e4SLinus Torvalds */ 24670a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 24680a16b607SMathieu Desnoyers 24694e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 24704e52365fSMatthew Dempsky nr = pid_vnr(pid); 247130e49c26SPavel Emelyanov 247230e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 24737f192e3cSChristian Brauner put_user(nr, args->parent_tid); 2474a6f5e063SSukadev Bhattiprolu 24751da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 24761da177e4SLinus Torvalds p->vfork_done = &vfork; 24771da177e4SLinus Torvalds init_completion(&vfork); 2478d68b46feSOleg Nesterov get_task_struct(p); 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds 24813e51e3edSSamir Bellabes wake_up_new_task(p); 24821da177e4SLinus Torvalds 24834b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 24844b9d33e6STejun Heo if (unlikely(trace)) 24854e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 248609a05394SRoland McGrath 24871da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 2488d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 24894e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 24909f59ce5dSChuck Ebbert } 24914e52365fSMatthew Dempsky 24924e52365fSMatthew Dempsky put_pid(pid); 249392476d7fSEric W. Biederman return nr; 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds 24962aa3a7f8SAl Viro /* 24972aa3a7f8SAl Viro * Create a kernel thread. 24982aa3a7f8SAl Viro */ 24992aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 25002aa3a7f8SAl Viro { 25017f192e3cSChristian Brauner struct kernel_clone_args args = { 25023f2c788aSChristian Brauner .flags = ((lower_32_bits(flags) | CLONE_VM | 25033f2c788aSChristian Brauner CLONE_UNTRACED) & ~CSIGNAL), 25043f2c788aSChristian Brauner .exit_signal = (lower_32_bits(flags) & CSIGNAL), 25057f192e3cSChristian Brauner .stack = (unsigned long)fn, 25067f192e3cSChristian Brauner .stack_size = (unsigned long)arg, 25077f192e3cSChristian Brauner }; 25087f192e3cSChristian Brauner 2509cad6967aSChristian Brauner return kernel_clone(&args); 25102aa3a7f8SAl Viro } 25112aa3a7f8SAl Viro 2512d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 2513d2125043SAl Viro SYSCALL_DEFINE0(fork) 2514d2125043SAl Viro { 2515d2125043SAl Viro #ifdef CONFIG_MMU 25167f192e3cSChristian Brauner struct kernel_clone_args args = { 25177f192e3cSChristian Brauner .exit_signal = SIGCHLD, 25187f192e3cSChristian Brauner }; 25197f192e3cSChristian Brauner 2520cad6967aSChristian Brauner return kernel_clone(&args); 2521d2125043SAl Viro #else 2522d2125043SAl Viro /* can not support in nommu mode */ 25235d59e182SDaeseok Youn return -EINVAL; 2524d2125043SAl Viro #endif 2525d2125043SAl Viro } 2526d2125043SAl Viro #endif 2527d2125043SAl Viro 2528d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 2529d2125043SAl Viro SYSCALL_DEFINE0(vfork) 2530d2125043SAl Viro { 25317f192e3cSChristian Brauner struct kernel_clone_args args = { 25327f192e3cSChristian Brauner .flags = CLONE_VFORK | CLONE_VM, 25337f192e3cSChristian Brauner .exit_signal = SIGCHLD, 25347f192e3cSChristian Brauner }; 25357f192e3cSChristian Brauner 2536cad6967aSChristian Brauner return kernel_clone(&args); 2537d2125043SAl Viro } 2538d2125043SAl Viro #endif 2539d2125043SAl Viro 2540d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 2541d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 2542d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2543d2125043SAl Viro int __user *, parent_tidptr, 25443033f14aSJosh Triplett unsigned long, tls, 2545d2125043SAl Viro int __user *, child_tidptr) 2546d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 2547d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 2548d2125043SAl Viro int __user *, parent_tidptr, 2549d2125043SAl Viro int __user *, child_tidptr, 25503033f14aSJosh Triplett unsigned long, tls) 2551dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 2552dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 2553dfa9771aSMichal Simek int, stack_size, 2554dfa9771aSMichal Simek int __user *, parent_tidptr, 2555dfa9771aSMichal Simek int __user *, child_tidptr, 25563033f14aSJosh Triplett unsigned long, tls) 2557d2125043SAl Viro #else 2558d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2559d2125043SAl Viro int __user *, parent_tidptr, 2560d2125043SAl Viro int __user *, child_tidptr, 25613033f14aSJosh Triplett unsigned long, tls) 2562d2125043SAl Viro #endif 2563d2125043SAl Viro { 25647f192e3cSChristian Brauner struct kernel_clone_args args = { 25653f2c788aSChristian Brauner .flags = (lower_32_bits(clone_flags) & ~CSIGNAL), 25667f192e3cSChristian Brauner .pidfd = parent_tidptr, 25677f192e3cSChristian Brauner .child_tid = child_tidptr, 25687f192e3cSChristian Brauner .parent_tid = parent_tidptr, 25693f2c788aSChristian Brauner .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL), 25707f192e3cSChristian Brauner .stack = newsp, 25717f192e3cSChristian Brauner .tls = tls, 25727f192e3cSChristian Brauner }; 25737f192e3cSChristian Brauner 2574cad6967aSChristian Brauner return kernel_clone(&args); 25757f192e3cSChristian Brauner } 2576d68dbb0cSChristian Brauner #endif 25777f192e3cSChristian Brauner 2578d68dbb0cSChristian Brauner #ifdef __ARCH_WANT_SYS_CLONE3 2579dd499f7aSAmanieu d'Antras 25807f192e3cSChristian Brauner noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, 25817f192e3cSChristian Brauner struct clone_args __user *uargs, 2582f14c234bSAleksa Sarai size_t usize) 25837f192e3cSChristian Brauner { 2584f14c234bSAleksa Sarai int err; 25857f192e3cSChristian Brauner struct clone_args args; 258649cb2fc4SAdrian Reber pid_t *kset_tid = kargs->set_tid; 25877f192e3cSChristian Brauner 2588a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, tls) != 2589a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER0); 2590a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, set_tid_size) != 2591a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER1); 2592a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) != 2593a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER2); 2594a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2); 2595a966dcfeSEugene Syromiatnikov 2596f14c234bSAleksa Sarai if (unlikely(usize > PAGE_SIZE)) 25977f192e3cSChristian Brauner return -E2BIG; 2598f14c234bSAleksa Sarai if (unlikely(usize < CLONE_ARGS_SIZE_VER0)) 25997f192e3cSChristian Brauner return -EINVAL; 26007f192e3cSChristian Brauner 2601f14c234bSAleksa Sarai err = copy_struct_from_user(&args, sizeof(args), uargs, usize); 2602f14c234bSAleksa Sarai if (err) 2603f14c234bSAleksa Sarai return err; 26047f192e3cSChristian Brauner 260549cb2fc4SAdrian Reber if (unlikely(args.set_tid_size > MAX_PID_NS_LEVEL)) 260649cb2fc4SAdrian Reber return -EINVAL; 260749cb2fc4SAdrian Reber 260849cb2fc4SAdrian Reber if (unlikely(!args.set_tid && args.set_tid_size > 0)) 260949cb2fc4SAdrian Reber return -EINVAL; 261049cb2fc4SAdrian Reber 261149cb2fc4SAdrian Reber if (unlikely(args.set_tid && args.set_tid_size == 0)) 261249cb2fc4SAdrian Reber return -EINVAL; 261349cb2fc4SAdrian Reber 2614a0eb9abdSEugene Syromiatnikov /* 2615a0eb9abdSEugene Syromiatnikov * Verify that higher 32bits of exit_signal are unset and that 2616a0eb9abdSEugene Syromiatnikov * it is a valid signal 2617a0eb9abdSEugene Syromiatnikov */ 2618a0eb9abdSEugene Syromiatnikov if (unlikely((args.exit_signal & ~((u64)CSIGNAL)) || 2619a0eb9abdSEugene Syromiatnikov !valid_signal(args.exit_signal))) 2620a0eb9abdSEugene Syromiatnikov return -EINVAL; 2621a0eb9abdSEugene Syromiatnikov 262262173872SEugene Syromiatnikov if ((args.flags & CLONE_INTO_CGROUP) && 262362173872SEugene Syromiatnikov (args.cgroup > INT_MAX || usize < CLONE_ARGS_SIZE_VER2)) 2624ef2c41cfSChristian Brauner return -EINVAL; 2625ef2c41cfSChristian Brauner 26267f192e3cSChristian Brauner *kargs = (struct kernel_clone_args){ 26277f192e3cSChristian Brauner .flags = args.flags, 26287f192e3cSChristian Brauner .pidfd = u64_to_user_ptr(args.pidfd), 26297f192e3cSChristian Brauner .child_tid = u64_to_user_ptr(args.child_tid), 26307f192e3cSChristian Brauner .parent_tid = u64_to_user_ptr(args.parent_tid), 26317f192e3cSChristian Brauner .exit_signal = args.exit_signal, 26327f192e3cSChristian Brauner .stack = args.stack, 26337f192e3cSChristian Brauner .stack_size = args.stack_size, 26347f192e3cSChristian Brauner .tls = args.tls, 263549cb2fc4SAdrian Reber .set_tid_size = args.set_tid_size, 2636ef2c41cfSChristian Brauner .cgroup = args.cgroup, 26377f192e3cSChristian Brauner }; 26387f192e3cSChristian Brauner 263949cb2fc4SAdrian Reber if (args.set_tid && 264049cb2fc4SAdrian Reber copy_from_user(kset_tid, u64_to_user_ptr(args.set_tid), 264149cb2fc4SAdrian Reber (kargs->set_tid_size * sizeof(pid_t)))) 264249cb2fc4SAdrian Reber return -EFAULT; 264349cb2fc4SAdrian Reber 264449cb2fc4SAdrian Reber kargs->set_tid = kset_tid; 264549cb2fc4SAdrian Reber 26467f192e3cSChristian Brauner return 0; 26477f192e3cSChristian Brauner } 26487f192e3cSChristian Brauner 2649fa729c4dSChristian Brauner /** 2650fa729c4dSChristian Brauner * clone3_stack_valid - check and prepare stack 2651fa729c4dSChristian Brauner * @kargs: kernel clone args 2652fa729c4dSChristian Brauner * 2653fa729c4dSChristian Brauner * Verify that the stack arguments userspace gave us are sane. 2654fa729c4dSChristian Brauner * In addition, set the stack direction for userspace since it's easy for us to 2655fa729c4dSChristian Brauner * determine. 2656fa729c4dSChristian Brauner */ 2657fa729c4dSChristian Brauner static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) 2658fa729c4dSChristian Brauner { 2659fa729c4dSChristian Brauner if (kargs->stack == 0) { 2660fa729c4dSChristian Brauner if (kargs->stack_size > 0) 2661fa729c4dSChristian Brauner return false; 2662fa729c4dSChristian Brauner } else { 2663fa729c4dSChristian Brauner if (kargs->stack_size == 0) 2664fa729c4dSChristian Brauner return false; 2665fa729c4dSChristian Brauner 2666fa729c4dSChristian Brauner if (!access_ok((void __user *)kargs->stack, kargs->stack_size)) 2667fa729c4dSChristian Brauner return false; 2668fa729c4dSChristian Brauner 2669fa729c4dSChristian Brauner #if !defined(CONFIG_STACK_GROWSUP) && !defined(CONFIG_IA64) 2670fa729c4dSChristian Brauner kargs->stack += kargs->stack_size; 2671fa729c4dSChristian Brauner #endif 2672fa729c4dSChristian Brauner } 2673fa729c4dSChristian Brauner 2674fa729c4dSChristian Brauner return true; 2675fa729c4dSChristian Brauner } 2676fa729c4dSChristian Brauner 2677fa729c4dSChristian Brauner static bool clone3_args_valid(struct kernel_clone_args *kargs) 26787f192e3cSChristian Brauner { 2679b612e5dfSChristian Brauner /* Verify that no unknown flags are passed along. */ 2680ef2c41cfSChristian Brauner if (kargs->flags & 2681ef2c41cfSChristian Brauner ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP)) 26827f192e3cSChristian Brauner return false; 26837f192e3cSChristian Brauner 26847f192e3cSChristian Brauner /* 26857f192e3cSChristian Brauner * - make the CLONE_DETACHED bit reuseable for clone3 26867f192e3cSChristian Brauner * - make the CSIGNAL bits reuseable for clone3 26877f192e3cSChristian Brauner */ 26887f192e3cSChristian Brauner if (kargs->flags & (CLONE_DETACHED | CSIGNAL)) 26897f192e3cSChristian Brauner return false; 26907f192e3cSChristian Brauner 2691b612e5dfSChristian Brauner if ((kargs->flags & (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) == 2692b612e5dfSChristian Brauner (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) 2693b612e5dfSChristian Brauner return false; 2694b612e5dfSChristian Brauner 26957f192e3cSChristian Brauner if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) && 26967f192e3cSChristian Brauner kargs->exit_signal) 26977f192e3cSChristian Brauner return false; 26987f192e3cSChristian Brauner 2699fa729c4dSChristian Brauner if (!clone3_stack_valid(kargs)) 2700fa729c4dSChristian Brauner return false; 2701fa729c4dSChristian Brauner 27027f192e3cSChristian Brauner return true; 27037f192e3cSChristian Brauner } 27047f192e3cSChristian Brauner 2705501bd016SChristian Brauner /** 2706501bd016SChristian Brauner * clone3 - create a new process with specific properties 2707501bd016SChristian Brauner * @uargs: argument structure 2708501bd016SChristian Brauner * @size: size of @uargs 2709501bd016SChristian Brauner * 2710501bd016SChristian Brauner * clone3() is the extensible successor to clone()/clone2(). 2711501bd016SChristian Brauner * It takes a struct as argument that is versioned by its size. 2712501bd016SChristian Brauner * 2713501bd016SChristian Brauner * Return: On success, a positive PID for the child process. 2714501bd016SChristian Brauner * On error, a negative errno number. 2715501bd016SChristian Brauner */ 27167f192e3cSChristian Brauner SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size) 27177f192e3cSChristian Brauner { 27187f192e3cSChristian Brauner int err; 27197f192e3cSChristian Brauner 27207f192e3cSChristian Brauner struct kernel_clone_args kargs; 272149cb2fc4SAdrian Reber pid_t set_tid[MAX_PID_NS_LEVEL]; 272249cb2fc4SAdrian Reber 272349cb2fc4SAdrian Reber kargs.set_tid = set_tid; 27247f192e3cSChristian Brauner 27257f192e3cSChristian Brauner err = copy_clone_args_from_user(&kargs, uargs, size); 27267f192e3cSChristian Brauner if (err) 27277f192e3cSChristian Brauner return err; 27287f192e3cSChristian Brauner 27297f192e3cSChristian Brauner if (!clone3_args_valid(&kargs)) 27307f192e3cSChristian Brauner return -EINVAL; 27317f192e3cSChristian Brauner 2732cad6967aSChristian Brauner return kernel_clone(&kargs); 2733d2125043SAl Viro } 2734d2125043SAl Viro #endif 2735d2125043SAl Viro 27360f1b92cbSOleg Nesterov void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) 27370f1b92cbSOleg Nesterov { 27380f1b92cbSOleg Nesterov struct task_struct *leader, *parent, *child; 27390f1b92cbSOleg Nesterov int res; 27400f1b92cbSOleg Nesterov 27410f1b92cbSOleg Nesterov read_lock(&tasklist_lock); 27420f1b92cbSOleg Nesterov leader = top = top->group_leader; 27430f1b92cbSOleg Nesterov down: 27440f1b92cbSOleg Nesterov for_each_thread(leader, parent) { 27450f1b92cbSOleg Nesterov list_for_each_entry(child, &parent->children, sibling) { 27460f1b92cbSOleg Nesterov res = visitor(child, data); 27470f1b92cbSOleg Nesterov if (res) { 27480f1b92cbSOleg Nesterov if (res < 0) 27490f1b92cbSOleg Nesterov goto out; 27500f1b92cbSOleg Nesterov leader = child; 27510f1b92cbSOleg Nesterov goto down; 27520f1b92cbSOleg Nesterov } 27530f1b92cbSOleg Nesterov up: 27540f1b92cbSOleg Nesterov ; 27550f1b92cbSOleg Nesterov } 27560f1b92cbSOleg Nesterov } 27570f1b92cbSOleg Nesterov 27580f1b92cbSOleg Nesterov if (leader != top) { 27590f1b92cbSOleg Nesterov child = leader; 27600f1b92cbSOleg Nesterov parent = child->real_parent; 27610f1b92cbSOleg Nesterov leader = parent->group_leader; 27620f1b92cbSOleg Nesterov goto up; 27630f1b92cbSOleg Nesterov } 27640f1b92cbSOleg Nesterov out: 27650f1b92cbSOleg Nesterov read_unlock(&tasklist_lock); 27660f1b92cbSOleg Nesterov } 27670f1b92cbSOleg Nesterov 27685fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 27695fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 27705fd63b30SRavikiran G Thirumalai #endif 27715fd63b30SRavikiran G Thirumalai 277251cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 2773aa1757f9SOleg Nesterov { 2774aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 2775aa1757f9SOleg Nesterov 2776aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 2777b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 2778fba2afaaSDavide Libenzi } 2779aa1757f9SOleg Nesterov 27801da177e4SLinus Torvalds void __init proc_caches_init(void) 27811da177e4SLinus Torvalds { 2782c1a2f7f0SRik van Riel unsigned int mm_size; 2783c1a2f7f0SRik van Riel 27841da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 27851da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 27865f0d5a3aSPaul E. McKenney SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| 278775f296d9SLevin, Alexander (Sasha Levin) SLAB_ACCOUNT, sighand_ctor); 27881da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 27891da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 279075f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 27915d097056SVladimir Davydov NULL); 27921da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 27931da177e4SLinus Torvalds sizeof(struct files_struct), 0, 279475f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 27955d097056SVladimir Davydov NULL); 27961da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 27971da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 279875f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 27995d097056SVladimir Davydov NULL); 2800c1a2f7f0SRik van Riel 28016345d24dSLinus Torvalds /* 2802c1a2f7f0SRik van Riel * The mm_cpumask is located at the end of mm_struct, and is 2803c1a2f7f0SRik van Riel * dynamically sized based on the maximum CPU number this system 2804c1a2f7f0SRik van Riel * can have, taking hotplug into account (nr_cpu_ids). 28056345d24dSLinus Torvalds */ 2806c1a2f7f0SRik van Riel mm_size = sizeof(struct mm_struct) + cpumask_size(); 2807c1a2f7f0SRik van Riel 280807dcd7feSDavid Windsor mm_cachep = kmem_cache_create_usercopy("mm_struct", 2809c1a2f7f0SRik van Riel mm_size, ARCH_MIN_MMSTRUCT_ALIGN, 281075f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 281107dcd7feSDavid Windsor offsetof(struct mm_struct, saved_auxv), 281207dcd7feSDavid Windsor sizeof_field(struct mm_struct, saved_auxv), 28135d097056SVladimir Davydov NULL); 28145d097056SVladimir Davydov vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 28158feae131SDavid Howells mmap_init(); 281666577193SAl Viro nsproxy_cache_init(); 28171da177e4SLinus Torvalds } 2818cf2e340fSJANAK DESAI 2819cf2e340fSJANAK DESAI /* 28209bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 2821cf2e340fSJANAK DESAI */ 28229bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 2823cf2e340fSJANAK DESAI { 28249bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 28259bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 282650804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 2827769071acSAndrei Vagin CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP| 2828769071acSAndrei Vagin CLONE_NEWTIME)) 2829cf2e340fSJANAK DESAI return -EINVAL; 28309bfb23fcSOleg Nesterov /* 283112c641abSEric W. Biederman * Not implemented, but pretend it works if there is nothing 283212c641abSEric W. Biederman * to unshare. Note that unsharing the address space or the 283312c641abSEric W. Biederman * signal handlers also need to unshare the signal queues (aka 283412c641abSEric W. Biederman * CLONE_THREAD). 28359bfb23fcSOleg Nesterov */ 28369bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 283712c641abSEric W. Biederman if (!thread_group_empty(current)) 283812c641abSEric W. Biederman return -EINVAL; 283912c641abSEric W. Biederman } 284012c641abSEric W. Biederman if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 2841d036bda7SElena Reshetova if (refcount_read(¤t->sighand->count) > 1) 284212c641abSEric W. Biederman return -EINVAL; 284312c641abSEric W. Biederman } 284412c641abSEric W. Biederman if (unshare_flags & CLONE_VM) { 284512c641abSEric W. Biederman if (!current_is_single_threaded()) 28469bfb23fcSOleg Nesterov return -EINVAL; 28479bfb23fcSOleg Nesterov } 2848cf2e340fSJANAK DESAI 2849cf2e340fSJANAK DESAI return 0; 2850cf2e340fSJANAK DESAI } 2851cf2e340fSJANAK DESAI 2852cf2e340fSJANAK DESAI /* 285399d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 2854cf2e340fSJANAK DESAI */ 2855cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 2856cf2e340fSJANAK DESAI { 2857cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 2858cf2e340fSJANAK DESAI 2859498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 2860498052bbSAl Viro return 0; 2861498052bbSAl Viro 2862498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 2863498052bbSAl Viro if (fs->users == 1) 2864498052bbSAl Viro return 0; 2865498052bbSAl Viro 2866498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 286799d1419dSJANAK DESAI if (!*new_fsp) 286899d1419dSJANAK DESAI return -ENOMEM; 2869cf2e340fSJANAK DESAI 2870cf2e340fSJANAK DESAI return 0; 2871cf2e340fSJANAK DESAI } 2872cf2e340fSJANAK DESAI 2873cf2e340fSJANAK DESAI /* 2874a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 2875cf2e340fSJANAK DESAI */ 287660997c3dSChristian Brauner int unshare_fd(unsigned long unshare_flags, unsigned int max_fds, 287760997c3dSChristian Brauner struct files_struct **new_fdp) 2878cf2e340fSJANAK DESAI { 2879cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 2880a016f338SJANAK DESAI int error = 0; 2881cf2e340fSJANAK DESAI 2882cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 2883a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 288460997c3dSChristian Brauner *new_fdp = dup_fd(fd, max_fds, &error); 2885a016f338SJANAK DESAI if (!*new_fdp) 2886a016f338SJANAK DESAI return error; 2887a016f338SJANAK DESAI } 2888cf2e340fSJANAK DESAI 2889cf2e340fSJANAK DESAI return 0; 2890cf2e340fSJANAK DESAI } 2891cf2e340fSJANAK DESAI 2892cf2e340fSJANAK DESAI /* 2893cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 2894cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 2895cad6967aSChristian Brauner * functions used by kernel_clone() cannot be used here directly 2896cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 2897cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 2898cf2e340fSJANAK DESAI * task_struct. 2899cf2e340fSJANAK DESAI */ 29009b32105eSDominik Brodowski int ksys_unshare(unsigned long unshare_flags) 2901cf2e340fSJANAK DESAI { 2902cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 2903cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 2904b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 2905cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 29069edff4abSManfred Spraul int do_sysvsem = 0; 29079bfb23fcSOleg Nesterov int err; 2908cf2e340fSJANAK DESAI 290950804fe3SEric W. Biederman /* 2910faf00da5SEric W. Biederman * If unsharing a user namespace must also unshare the thread group 2911faf00da5SEric W. Biederman * and unshare the filesystem root and working directories. 2912b2e0d987SEric W. Biederman */ 2913b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 2914e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 2915b2e0d987SEric W. Biederman /* 291650804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 291750804fe3SEric W. Biederman */ 291850804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 291950804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 29206013f67fSManfred Spraul /* 292112c641abSEric W. Biederman * If unsharing a signal handlers, must also unshare the signal queues. 292212c641abSEric W. Biederman */ 292312c641abSEric W. Biederman if (unshare_flags & CLONE_SIGHAND) 292412c641abSEric W. Biederman unshare_flags |= CLONE_THREAD; 292512c641abSEric W. Biederman /* 29269bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 29279bfb23fcSOleg Nesterov */ 29289bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 29299bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 293050804fe3SEric W. Biederman 293150804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 293250804fe3SEric W. Biederman if (err) 293350804fe3SEric W. Biederman goto bad_unshare_out; 29349bfb23fcSOleg Nesterov /* 29356013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 29366013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 29376013f67fSManfred Spraul * namespace are unreachable. 29386013f67fSManfred Spraul */ 29396013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 29409edff4abSManfred Spraul do_sysvsem = 1; 2941fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 2942fb0a685cSDaniel Rebelo de Oliveira if (err) 29439bfb23fcSOleg Nesterov goto bad_unshare_out; 294460997c3dSChristian Brauner err = unshare_fd(unshare_flags, NR_OPEN_MAX, &new_fd); 2945fb0a685cSDaniel Rebelo de Oliveira if (err) 29469bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 2947b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 2948fb0a685cSDaniel Rebelo de Oliveira if (err) 29499edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 2950b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 2951b2e0d987SEric W. Biederman new_cred, new_fs); 2952b2e0d987SEric W. Biederman if (err) 2953b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 2954cf2e340fSJANAK DESAI 2955b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 29569edff4abSManfred Spraul if (do_sysvsem) { 29579edff4abSManfred Spraul /* 29589edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 29599edff4abSManfred Spraul */ 29609edff4abSManfred Spraul exit_sem(current); 29619edff4abSManfred Spraul } 2962ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 2963ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 2964ab602f79SJack Miller exit_shm(current); 2965ab602f79SJack Miller shm_init_task(current); 2966ab602f79SJack Miller } 2967ab516013SSerge E. Hallyn 29686f977e6bSAlan Cox if (new_nsproxy) 2969cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 2970cf2e340fSJANAK DESAI 2971cf7b708cSPavel Emelyanov task_lock(current); 2972cf7b708cSPavel Emelyanov 2973cf2e340fSJANAK DESAI if (new_fs) { 2974cf2e340fSJANAK DESAI fs = current->fs; 29752a4419b5SNick Piggin spin_lock(&fs->lock); 2976cf2e340fSJANAK DESAI current->fs = new_fs; 2977498052bbSAl Viro if (--fs->users) 2978498052bbSAl Viro new_fs = NULL; 2979498052bbSAl Viro else 2980cf2e340fSJANAK DESAI new_fs = fs; 29812a4419b5SNick Piggin spin_unlock(&fs->lock); 2982cf2e340fSJANAK DESAI } 2983cf2e340fSJANAK DESAI 2984cf2e340fSJANAK DESAI if (new_fd) { 2985cf2e340fSJANAK DESAI fd = current->files; 2986cf2e340fSJANAK DESAI current->files = new_fd; 2987cf2e340fSJANAK DESAI new_fd = fd; 2988cf2e340fSJANAK DESAI } 2989cf2e340fSJANAK DESAI 2990cf2e340fSJANAK DESAI task_unlock(current); 2991b2e0d987SEric W. Biederman 2992b2e0d987SEric W. Biederman if (new_cred) { 2993b2e0d987SEric W. Biederman /* Install the new user namespace */ 2994b2e0d987SEric W. Biederman commit_creds(new_cred); 2995b2e0d987SEric W. Biederman new_cred = NULL; 2996b2e0d987SEric W. Biederman } 2997cf2e340fSJANAK DESAI } 2998cf2e340fSJANAK DESAI 2999e4222673SHari Bathini perf_event_namespaces(current); 3000e4222673SHari Bathini 3001b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 3002b2e0d987SEric W. Biederman if (new_cred) 3003b2e0d987SEric W. Biederman put_cred(new_cred); 3004cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 3005cf2e340fSJANAK DESAI if (new_fd) 3006cf2e340fSJANAK DESAI put_files_struct(new_fd); 3007cf2e340fSJANAK DESAI 3008cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 3009cf2e340fSJANAK DESAI if (new_fs) 3010498052bbSAl Viro free_fs_struct(new_fs); 3011cf2e340fSJANAK DESAI 3012cf2e340fSJANAK DESAI bad_unshare_out: 3013cf2e340fSJANAK DESAI return err; 3014cf2e340fSJANAK DESAI } 30153b125388SAl Viro 30169b32105eSDominik Brodowski SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 30179b32105eSDominik Brodowski { 30189b32105eSDominik Brodowski return ksys_unshare(unshare_flags); 30199b32105eSDominik Brodowski } 30209b32105eSDominik Brodowski 30213b125388SAl Viro /* 30223b125388SAl Viro * Helper to unshare the files of the current task. 30233b125388SAl Viro * We don't want to expose copy_files internals to 30243b125388SAl Viro * the exec layer of the kernel. 30253b125388SAl Viro */ 30263b125388SAl Viro 30273b125388SAl Viro int unshare_files(struct files_struct **displaced) 30283b125388SAl Viro { 30293b125388SAl Viro struct task_struct *task = current; 303050704516SAl Viro struct files_struct *copy = NULL; 30313b125388SAl Viro int error; 30323b125388SAl Viro 303360997c3dSChristian Brauner error = unshare_fd(CLONE_FILES, NR_OPEN_MAX, ©); 30343b125388SAl Viro if (error || !copy) { 30353b125388SAl Viro *displaced = NULL; 30363b125388SAl Viro return error; 30373b125388SAl Viro } 30383b125388SAl Viro *displaced = task->files; 30393b125388SAl Viro task_lock(task); 30403b125388SAl Viro task->files = copy; 30413b125388SAl Viro task_unlock(task); 30423b125388SAl Viro return 0; 30433b125388SAl Viro } 304416db3d3fSHeinrich Schuchardt 304516db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write, 3046b0daa2c7STobias Klauser void *buffer, size_t *lenp, loff_t *ppos) 304716db3d3fSHeinrich Schuchardt { 304816db3d3fSHeinrich Schuchardt struct ctl_table t; 304916db3d3fSHeinrich Schuchardt int ret; 305016db3d3fSHeinrich Schuchardt int threads = max_threads; 3051b0f53dbcSMichal Hocko int min = 1; 305216db3d3fSHeinrich Schuchardt int max = MAX_THREADS; 305316db3d3fSHeinrich Schuchardt 305416db3d3fSHeinrich Schuchardt t = *table; 305516db3d3fSHeinrich Schuchardt t.data = &threads; 305616db3d3fSHeinrich Schuchardt t.extra1 = &min; 305716db3d3fSHeinrich Schuchardt t.extra2 = &max; 305816db3d3fSHeinrich Schuchardt 305916db3d3fSHeinrich Schuchardt ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 306016db3d3fSHeinrich Schuchardt if (ret || !write) 306116db3d3fSHeinrich Schuchardt return ret; 306216db3d3fSHeinrich Schuchardt 3063b0f53dbcSMichal Hocko max_threads = threads; 306416db3d3fSHeinrich Schuchardt 306516db3d3fSHeinrich Schuchardt return 0; 306616db3d3fSHeinrich Schuchardt } 3067