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> 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #include <asm/pgalloc.h> 1007c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 1011da177e4SLinus Torvalds #include <asm/mmu_context.h> 1021da177e4SLinus Torvalds #include <asm/cacheflush.h> 1031da177e4SLinus Torvalds #include <asm/tlbflush.h> 1041da177e4SLinus Torvalds 105ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 106ad8d75ffSSteven Rostedt 10743d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 10843d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 10943d2b113SKAMEZAWA Hiroyuki 1101da177e4SLinus Torvalds /* 111ac1b398dSHeinrich Schuchardt * Minimum number of threads to boot the kernel 112ac1b398dSHeinrich Schuchardt */ 113ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20 114ac1b398dSHeinrich Schuchardt 115ac1b398dSHeinrich Schuchardt /* 116ac1b398dSHeinrich Schuchardt * Maximum number of threads 117ac1b398dSHeinrich Schuchardt */ 118ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK 119ac1b398dSHeinrich Schuchardt 120ac1b398dSHeinrich Schuchardt /* 1211da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 1221da177e4SLinus Torvalds */ 1231da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 1241da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 1251da177e4SLinus Torvalds 1268856ae4dSKefeng Wang static int max_threads; /* tunable limit on nr_threads */ 1271da177e4SLinus Torvalds 1288495f7e6SSai Praneeth Prakhya #define NAMED_ARRAY_INDEX(x) [x] = __stringify(x) 1298495f7e6SSai Praneeth Prakhya 1308495f7e6SSai Praneeth Prakhya static const char * const resident_page_types[] = { 1318495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_FILEPAGES), 1328495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_ANONPAGES), 1338495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_SWAPENTS), 1348495f7e6SSai Praneeth Prakhya NAMED_ARRAY_INDEX(MM_SHMEMPAGES), 1358495f7e6SSai Praneeth Prakhya }; 1368495f7e6SSai Praneeth Prakhya 1371da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 140db1466b3SPaul E. McKenney 141db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 142db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 143db1466b3SPaul E. McKenney { 144db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 145db1466b3SPaul E. McKenney } 146db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 147db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds int nr_processes(void) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds int cpu; 1521da177e4SLinus Torvalds int total = 0; 1531da177e4SLinus Torvalds 1541d510750SIan Campbell for_each_possible_cpu(cpu) 1551da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds return total; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 161f19b9f74SAkinobu Mita { 162f19b9f74SAkinobu Mita } 163f19b9f74SAkinobu Mita 164f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 165e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 16641101809SThomas Gleixner 16741101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 16841101809SThomas Gleixner { 16941101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 17041101809SThomas Gleixner } 17141101809SThomas Gleixner 17241101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 17341101809SThomas Gleixner { 17441101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 17541101809SThomas Gleixner } 1761da177e4SLinus Torvalds #endif 1771da177e4SLinus Torvalds 178b235beeaSLinus Torvalds #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR 17941101809SThomas Gleixner 1800d15d74aSThomas Gleixner /* 1810d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1820d15d74aSThomas Gleixner * kmemcache based allocator. 1830d15d74aSThomas Gleixner */ 184ba14a194SAndy Lutomirski # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) 185ac496bf4SAndy Lutomirski 186ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 187ac496bf4SAndy Lutomirski /* 188ac496bf4SAndy Lutomirski * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 189ac496bf4SAndy Lutomirski * flush. Try to minimize the number of calls by caching stacks. 190ac496bf4SAndy Lutomirski */ 191ac496bf4SAndy Lutomirski #define NR_CACHED_STACKS 2 192ac496bf4SAndy Lutomirski static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 19319659c59SHoeun Ryu 19419659c59SHoeun Ryu static int free_vm_stack_cache(unsigned int cpu) 19519659c59SHoeun Ryu { 19619659c59SHoeun Ryu struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu); 19719659c59SHoeun Ryu int i; 19819659c59SHoeun Ryu 19919659c59SHoeun Ryu for (i = 0; i < NR_CACHED_STACKS; i++) { 20019659c59SHoeun Ryu struct vm_struct *vm_stack = cached_vm_stacks[i]; 20119659c59SHoeun Ryu 20219659c59SHoeun Ryu if (!vm_stack) 20319659c59SHoeun Ryu continue; 20419659c59SHoeun Ryu 20519659c59SHoeun Ryu vfree(vm_stack->addr); 20619659c59SHoeun Ryu cached_vm_stacks[i] = NULL; 20719659c59SHoeun Ryu } 20819659c59SHoeun Ryu 20919659c59SHoeun Ryu return 0; 21019659c59SHoeun Ryu } 211ac496bf4SAndy Lutomirski #endif 212ac496bf4SAndy Lutomirski 213ba14a194SAndy Lutomirski static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) 214b69c49b7SFUJITA Tomonori { 215ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 216ac496bf4SAndy Lutomirski void *stack; 217ac496bf4SAndy Lutomirski int i; 218ac496bf4SAndy Lutomirski 219ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 220112166f8SChristoph Lameter struct vm_struct *s; 221112166f8SChristoph Lameter 222112166f8SChristoph Lameter s = this_cpu_xchg(cached_stacks[i], NULL); 223ac496bf4SAndy Lutomirski 224ac496bf4SAndy Lutomirski if (!s) 225ac496bf4SAndy Lutomirski continue; 226ac496bf4SAndy Lutomirski 227eafb149eSDaniel Axtens /* Clear the KASAN shadow of the stack. */ 228eafb149eSDaniel Axtens kasan_unpoison_shadow(s->addr, THREAD_SIZE); 229eafb149eSDaniel Axtens 230ca182551SKonstantin Khlebnikov /* Clear stale pointers from reused stack. */ 231ca182551SKonstantin Khlebnikov memset(s->addr, 0, THREAD_SIZE); 232e01e8063SKees Cook 233ac496bf4SAndy Lutomirski tsk->stack_vm_area = s; 234ba4a4574SShakeel Butt tsk->stack = s->addr; 235ac496bf4SAndy Lutomirski return s->addr; 236ac496bf4SAndy Lutomirski } 237ac496bf4SAndy Lutomirski 2389b6f7e16SRoman Gushchin /* 2399b6f7e16SRoman Gushchin * Allocated stacks are cached and later reused by new threads, 2409b6f7e16SRoman Gushchin * so memcg accounting is performed manually on assigning/releasing 2419b6f7e16SRoman Gushchin * stacks to tasks. Drop __GFP_ACCOUNT. 2429b6f7e16SRoman Gushchin */ 24348ac3c18SMark Rutland stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, 244ba14a194SAndy Lutomirski VMALLOC_START, VMALLOC_END, 2459b6f7e16SRoman Gushchin THREADINFO_GFP & ~__GFP_ACCOUNT, 246ba14a194SAndy Lutomirski PAGE_KERNEL, 247ac496bf4SAndy Lutomirski 0, node, __builtin_return_address(0)); 248ba14a194SAndy Lutomirski 249ba14a194SAndy Lutomirski /* 250ba14a194SAndy Lutomirski * We can't call find_vm_area() in interrupt context, and 251ba14a194SAndy Lutomirski * free_thread_stack() can be called in interrupt context, 252ba14a194SAndy Lutomirski * so cache the vm_struct. 253ba14a194SAndy Lutomirski */ 2545eed6f1dSRik van Riel if (stack) { 255ba14a194SAndy Lutomirski tsk->stack_vm_area = find_vm_area(stack); 2565eed6f1dSRik van Riel tsk->stack = stack; 2575eed6f1dSRik van Riel } 258ba14a194SAndy Lutomirski return stack; 259ba14a194SAndy Lutomirski #else 2604949148aSVladimir Davydov struct page *page = alloc_pages_node(node, THREADINFO_GFP, 2612889f608SThomas Gleixner THREAD_SIZE_ORDER); 262b6a84016SEric Dumazet 2631bf4580eSAndrea Arcangeli if (likely(page)) { 2641bf4580eSAndrea Arcangeli tsk->stack = page_address(page); 2651bf4580eSAndrea Arcangeli return tsk->stack; 2661bf4580eSAndrea Arcangeli } 2671bf4580eSAndrea Arcangeli return NULL; 268ba14a194SAndy Lutomirski #endif 269b69c49b7SFUJITA Tomonori } 270b69c49b7SFUJITA Tomonori 271ba14a194SAndy Lutomirski static inline void free_thread_stack(struct task_struct *tsk) 272b69c49b7SFUJITA Tomonori { 273ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 2749b6f7e16SRoman Gushchin struct vm_struct *vm = task_stack_vm_area(tsk); 2759b6f7e16SRoman Gushchin 2769b6f7e16SRoman Gushchin if (vm) { 277ac496bf4SAndy Lutomirski int i; 278ac496bf4SAndy Lutomirski 2799b6f7e16SRoman Gushchin for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 2809b6f7e16SRoman Gushchin mod_memcg_page_state(vm->pages[i], 2819b6f7e16SRoman Gushchin MEMCG_KERNEL_STACK_KB, 2829b6f7e16SRoman Gushchin -(int)(PAGE_SIZE / 1024)); 2839b6f7e16SRoman Gushchin 284f4b00eabSRoman Gushchin memcg_kmem_uncharge_page(vm->pages[i], 0); 2859b6f7e16SRoman Gushchin } 2869b6f7e16SRoman Gushchin 287ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 288112166f8SChristoph Lameter if (this_cpu_cmpxchg(cached_stacks[i], 289112166f8SChristoph Lameter NULL, tsk->stack_vm_area) != NULL) 290ac496bf4SAndy Lutomirski continue; 291ac496bf4SAndy Lutomirski 292ac496bf4SAndy Lutomirski return; 293ac496bf4SAndy Lutomirski } 294ac496bf4SAndy Lutomirski 2950f110a9bSAndrey Ryabinin vfree_atomic(tsk->stack); 296ac496bf4SAndy Lutomirski return; 297ac496bf4SAndy Lutomirski } 298ac496bf4SAndy Lutomirski #endif 299ac496bf4SAndy Lutomirski 300ba14a194SAndy Lutomirski __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); 301b69c49b7SFUJITA Tomonori } 3020d15d74aSThomas Gleixner # else 303b235beeaSLinus Torvalds static struct kmem_cache *thread_stack_cache; 3040d15d74aSThomas Gleixner 3059521d399SMichael Ellerman static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, 3060d15d74aSThomas Gleixner int node) 3070d15d74aSThomas Gleixner { 3085eed6f1dSRik van Riel unsigned long *stack; 3095eed6f1dSRik van Riel stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 3105eed6f1dSRik van Riel tsk->stack = stack; 3115eed6f1dSRik van Riel return stack; 3120d15d74aSThomas Gleixner } 3130d15d74aSThomas Gleixner 314ba14a194SAndy Lutomirski static void free_thread_stack(struct task_struct *tsk) 3150d15d74aSThomas Gleixner { 316ba14a194SAndy Lutomirski kmem_cache_free(thread_stack_cache, tsk->stack); 3170d15d74aSThomas Gleixner } 3180d15d74aSThomas Gleixner 319b235beeaSLinus Torvalds void thread_stack_cache_init(void) 3200d15d74aSThomas Gleixner { 321f9d29946SDavid Windsor thread_stack_cache = kmem_cache_create_usercopy("thread_stack", 322f9d29946SDavid Windsor THREAD_SIZE, THREAD_SIZE, 0, 0, 323f9d29946SDavid Windsor THREAD_SIZE, NULL); 324b235beeaSLinus Torvalds BUG_ON(thread_stack_cache == NULL); 3250d15d74aSThomas Gleixner } 3260d15d74aSThomas Gleixner # endif 327b69c49b7SFUJITA Tomonori #endif 328b69c49b7SFUJITA Tomonori 3291da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 330e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 333e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 336e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 339e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 3423928d4f5SLinus Torvalds static struct kmem_cache *vm_area_cachep; 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 345e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 3461da177e4SLinus Torvalds 347490fc053SLinus Torvalds struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) 3483928d4f5SLinus Torvalds { 349a670468fSAndrew Morton struct vm_area_struct *vma; 350490fc053SLinus Torvalds 351a670468fSAndrew Morton vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 352027232daSKirill A. Shutemov if (vma) 353027232daSKirill A. Shutemov vma_init(vma, mm); 354490fc053SLinus Torvalds return vma; 3553928d4f5SLinus Torvalds } 3563928d4f5SLinus Torvalds 3573928d4f5SLinus Torvalds struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) 3583928d4f5SLinus Torvalds { 35995faf699SLinus Torvalds struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 36095faf699SLinus Torvalds 36195faf699SLinus Torvalds if (new) { 36295faf699SLinus Torvalds *new = *orig; 36395faf699SLinus Torvalds INIT_LIST_HEAD(&new->anon_vma_chain); 364e39a4b33SLi Xinhai new->vm_next = new->vm_prev = NULL; 36595faf699SLinus Torvalds } 36695faf699SLinus Torvalds return new; 3673928d4f5SLinus Torvalds } 3683928d4f5SLinus Torvalds 3693928d4f5SLinus Torvalds void vm_area_free(struct vm_area_struct *vma) 3703928d4f5SLinus Torvalds { 3713928d4f5SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 3723928d4f5SLinus Torvalds } 3733928d4f5SLinus Torvalds 374ba14a194SAndy Lutomirski static void account_kernel_stack(struct task_struct *tsk, int account) 375c6a7f572SKOSAKI Motohiro { 376ba14a194SAndy Lutomirski void *stack = task_stack_page(tsk); 377ba14a194SAndy Lutomirski struct vm_struct *vm = task_stack_vm_area(tsk); 378ba14a194SAndy Lutomirski 379ba14a194SAndy Lutomirski BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); 380ba14a194SAndy Lutomirski 381ba14a194SAndy Lutomirski if (vm) { 382ba14a194SAndy Lutomirski int i; 383ba14a194SAndy Lutomirski 384ba14a194SAndy Lutomirski BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); 385ba14a194SAndy Lutomirski 386ba14a194SAndy Lutomirski for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 387ba14a194SAndy Lutomirski mod_zone_page_state(page_zone(vm->pages[i]), 388ba14a194SAndy Lutomirski NR_KERNEL_STACK_KB, 389ba14a194SAndy Lutomirski PAGE_SIZE / 1024 * account); 390ba14a194SAndy Lutomirski } 391ba14a194SAndy Lutomirski } else { 392ba14a194SAndy Lutomirski /* 393ba14a194SAndy Lutomirski * All stack pages are in the same zone and belong to the 394ba14a194SAndy Lutomirski * same memcg. 395ba14a194SAndy Lutomirski */ 396efdc9490SAndy Lutomirski struct page *first_page = virt_to_page(stack); 397c6a7f572SKOSAKI Motohiro 398efdc9490SAndy Lutomirski mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB, 399d30dd8beSAndy Lutomirski THREAD_SIZE / 1024 * account); 400efdc9490SAndy Lutomirski 4018380ce47SRoman Gushchin mod_memcg_obj_state(stack, MEMCG_KERNEL_STACK_KB, 402efdc9490SAndy Lutomirski account * (THREAD_SIZE / 1024)); 403c6a7f572SKOSAKI Motohiro } 404ba14a194SAndy Lutomirski } 405c6a7f572SKOSAKI Motohiro 4069b6f7e16SRoman Gushchin static int memcg_charge_kernel_stack(struct task_struct *tsk) 4079b6f7e16SRoman Gushchin { 4089b6f7e16SRoman Gushchin #ifdef CONFIG_VMAP_STACK 4099b6f7e16SRoman Gushchin struct vm_struct *vm = task_stack_vm_area(tsk); 4109b6f7e16SRoman Gushchin int ret; 4119b6f7e16SRoman Gushchin 4129b6f7e16SRoman Gushchin if (vm) { 4139b6f7e16SRoman Gushchin int i; 4149b6f7e16SRoman Gushchin 4159b6f7e16SRoman Gushchin for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 4169b6f7e16SRoman Gushchin /* 417f4b00eabSRoman Gushchin * If memcg_kmem_charge_page() fails, page->mem_cgroup 418f4b00eabSRoman Gushchin * pointer is NULL, and both memcg_kmem_uncharge_page() 4199b6f7e16SRoman Gushchin * and mod_memcg_page_state() in free_thread_stack() 4209b6f7e16SRoman Gushchin * will ignore this page. So it's safe. 4219b6f7e16SRoman Gushchin */ 422f4b00eabSRoman Gushchin ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL, 423f4b00eabSRoman Gushchin 0); 4249b6f7e16SRoman Gushchin if (ret) 4259b6f7e16SRoman Gushchin return ret; 4269b6f7e16SRoman Gushchin 4279b6f7e16SRoman Gushchin mod_memcg_page_state(vm->pages[i], 4289b6f7e16SRoman Gushchin MEMCG_KERNEL_STACK_KB, 4299b6f7e16SRoman Gushchin PAGE_SIZE / 1024); 4309b6f7e16SRoman Gushchin } 4319b6f7e16SRoman Gushchin } 4329b6f7e16SRoman Gushchin #endif 4339b6f7e16SRoman Gushchin return 0; 4349b6f7e16SRoman Gushchin } 4359b6f7e16SRoman Gushchin 43668f24b08SAndy Lutomirski static void release_task_stack(struct task_struct *tsk) 4371da177e4SLinus Torvalds { 438405c0759SAndy Lutomirski if (WARN_ON(tsk->state != TASK_DEAD)) 439405c0759SAndy Lutomirski return; /* Better to leak the stack than to free prematurely */ 440405c0759SAndy Lutomirski 441ba14a194SAndy Lutomirski account_kernel_stack(tsk, -1); 442ba14a194SAndy Lutomirski free_thread_stack(tsk); 44368f24b08SAndy Lutomirski tsk->stack = NULL; 44468f24b08SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 44568f24b08SAndy Lutomirski tsk->stack_vm_area = NULL; 44668f24b08SAndy Lutomirski #endif 44768f24b08SAndy Lutomirski } 44868f24b08SAndy Lutomirski 44968f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 45068f24b08SAndy Lutomirski void put_task_stack(struct task_struct *tsk) 45168f24b08SAndy Lutomirski { 452f0b89d39SElena Reshetova if (refcount_dec_and_test(&tsk->stack_refcount)) 45368f24b08SAndy Lutomirski release_task_stack(tsk); 45468f24b08SAndy Lutomirski } 45568f24b08SAndy Lutomirski #endif 45668f24b08SAndy Lutomirski 45768f24b08SAndy Lutomirski void free_task(struct task_struct *tsk) 45868f24b08SAndy Lutomirski { 459d08b9f0cSSami Tolvanen scs_release(tsk); 460d08b9f0cSSami Tolvanen 46168f24b08SAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK 46268f24b08SAndy Lutomirski /* 46368f24b08SAndy Lutomirski * The task is finally done with both the stack and thread_info, 46468f24b08SAndy Lutomirski * so free both. 46568f24b08SAndy Lutomirski */ 46668f24b08SAndy Lutomirski release_task_stack(tsk); 46768f24b08SAndy Lutomirski #else 46868f24b08SAndy Lutomirski /* 46968f24b08SAndy Lutomirski * If the task had a separate stack allocation, it should be gone 47068f24b08SAndy Lutomirski * by now. 47168f24b08SAndy Lutomirski */ 472f0b89d39SElena Reshetova WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0); 47368f24b08SAndy Lutomirski #endif 47423f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 475fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 476e2cfabdfSWill Drewry put_seccomp_filter(tsk); 477f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 4781da5c46fSOleg Nesterov if (tsk->flags & PF_KTHREAD) 4791da5c46fSOleg Nesterov free_kthread_struct(tsk); 4801da177e4SLinus Torvalds free_task_struct(tsk); 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds #ifdef CONFIG_MMU 4850766f788SEmese Revfy static __latent_entropy int dup_mmap(struct mm_struct *mm, 4860766f788SEmese Revfy struct mm_struct *oldmm) 4871da177e4SLinus Torvalds { 488297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 4891da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 4901da177e4SLinus Torvalds int retval; 4911da177e4SLinus Torvalds unsigned long charge; 492893e26e6SPavel Emelyanov LIST_HEAD(uf); 4931da177e4SLinus Torvalds 49432cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 495d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(oldmm)) { 4967c051267SMichal Hocko retval = -EINTR; 4977c051267SMichal Hocko goto fail_uprobe_end; 4987c051267SMichal Hocko } 499ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 500f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 501ad339451SIngo Molnar /* 502ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 503ad339451SIngo Molnar */ 504aaa2cc56SMichel Lespinasse mmap_write_lock_nested(mm, SINGLE_DEPTH_NESTING); 5057ee78232SHugh Dickins 50690f31d0eSKonstantin Khlebnikov /* No ordering required: file already has been exposed. */ 50790f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 50890f31d0eSKonstantin Khlebnikov 5094f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 51084638335SKonstantin Khlebnikov mm->data_vm = oldmm->data_vm; 5114f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 5124f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 5134f7d4614SVladimir Davydov 5141da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 5151da177e4SLinus Torvalds rb_parent = NULL; 5161da177e4SLinus Torvalds pprev = &mm->mmap; 517f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 518f8af4da3SHugh Dickins if (retval) 519f8af4da3SHugh Dickins goto out; 520ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 521ba76149fSAndrea Arcangeli if (retval) 522ba76149fSAndrea Arcangeli goto out; 5231da177e4SLinus Torvalds 524297c5eeeSLinus Torvalds prev = NULL; 525fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 5261da177e4SLinus Torvalds struct file *file; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 52984638335SKonstantin Khlebnikov vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); 5301da177e4SLinus Torvalds continue; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds charge = 0; 533655c79bbSTetsuo Handa /* 534655c79bbSTetsuo Handa * Don't duplicate many vmas if we've been oom-killed (for 535655c79bbSTetsuo Handa * example) 536655c79bbSTetsuo Handa */ 537655c79bbSTetsuo Handa if (fatal_signal_pending(current)) { 538655c79bbSTetsuo Handa retval = -EINTR; 539655c79bbSTetsuo Handa goto out; 540655c79bbSTetsuo Handa } 5411da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 542b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 543b2412b7fSHuang Shijie 544191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 5451da177e4SLinus Torvalds goto fail_nomem; 5461da177e4SLinus Torvalds charge = len; 5471da177e4SLinus Torvalds } 5483928d4f5SLinus Torvalds tmp = vm_area_dup(mpnt); 5491da177e4SLinus Torvalds if (!tmp) 5501da177e4SLinus Torvalds goto fail_nomem; 551ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 552ef0855d3SOleg Nesterov if (retval) 5531da177e4SLinus Torvalds goto fail_nomem_policy; 554a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 555893e26e6SPavel Emelyanov retval = dup_userfaultfd(tmp, &uf); 556893e26e6SPavel Emelyanov if (retval) 557893e26e6SPavel Emelyanov goto fail_nomem_anon_vma_fork; 558d2cd9edeSRik van Riel if (tmp->vm_flags & VM_WIPEONFORK) { 55993949bb2SLi Xinhai /* 56093949bb2SLi Xinhai * VM_WIPEONFORK gets a clean slate in the child. 56193949bb2SLi Xinhai * Don't prepare anon_vma until fault since we don't 56293949bb2SLi Xinhai * copy page for current vma. 56393949bb2SLi Xinhai */ 564d2cd9edeSRik van Riel tmp->anon_vma = NULL; 565d2cd9edeSRik van Riel } else if (anon_vma_fork(tmp, mpnt)) 5665beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 567893e26e6SPavel Emelyanov tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT); 5681da177e4SLinus Torvalds file = tmp->vm_file; 5691da177e4SLinus Torvalds if (file) { 570496ad9aaSAl Viro struct inode *inode = file_inode(file); 571b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 572b88ed205SHugh Dickins 5731da177e4SLinus Torvalds get_file(file); 5741da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 5751da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 57683cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 577b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 5784bb5f5d9SDavid Herrmann atomic_inc(&mapping->i_mmap_writable); 579b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 580b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 5819826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 5829826a516SMichel Lespinasse &mapping->i_mmap); 583b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 58483cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds /* 588a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 589a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 590a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 591a1e78772SMel Gorman */ 592a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 593a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 594a1e78772SMel Gorman 595a1e78772SMel Gorman /* 5967ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 5971da177e4SLinus Torvalds */ 5981da177e4SLinus Torvalds *pprev = tmp; 5991da177e4SLinus Torvalds pprev = &tmp->vm_next; 600297c5eeeSLinus Torvalds tmp->vm_prev = prev; 601297c5eeeSLinus Torvalds prev = tmp; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 6041da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 6051da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds mm->map_count++; 608d2cd9edeSRik van Riel if (!(tmp->vm_flags & VM_WIPEONFORK)) 6090b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 6121da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds if (retval) 6151da177e4SLinus Torvalds goto out; 6161da177e4SLinus Torvalds } 617d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 6181ed0cc5aSNadav Amit retval = arch_dup_mmap(oldmm, mm); 6191da177e4SLinus Torvalds out: 620d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 621fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 622d8ed45c5SMichel Lespinasse mmap_write_unlock(oldmm); 623893e26e6SPavel Emelyanov dup_userfaultfd_complete(&uf); 6247c051267SMichal Hocko fail_uprobe_end: 62532cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 6261da177e4SLinus Torvalds return retval; 6275beb4930SRik van Riel fail_nomem_anon_vma_fork: 628ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 6291da177e4SLinus Torvalds fail_nomem_policy: 6303928d4f5SLinus Torvalds vm_area_free(tmp); 6311da177e4SLinus Torvalds fail_nomem: 6321da177e4SLinus Torvalds retval = -ENOMEM; 6331da177e4SLinus Torvalds vm_unacct_memory(charge); 6341da177e4SLinus Torvalds goto out; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 6401da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 6411da177e4SLinus Torvalds return -ENOMEM; 6421da177e4SLinus Torvalds return 0; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 6461da177e4SLinus Torvalds { 6475e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds #else 65090f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 65190f31d0eSKonstantin Khlebnikov { 652d8ed45c5SMichel Lespinasse mmap_write_lock(oldmm); 65390f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 654d8ed45c5SMichel Lespinasse mmap_write_unlock(oldmm); 65590f31d0eSKonstantin Khlebnikov return 0; 65690f31d0eSKonstantin Khlebnikov } 6571da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 6581da177e4SLinus Torvalds #define mm_free_pgd(mm) 6591da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 6601da177e4SLinus Torvalds 661d70f2a14SAndrew Morton static void check_mm(struct mm_struct *mm) 662d70f2a14SAndrew Morton { 663d70f2a14SAndrew Morton int i; 664d70f2a14SAndrew Morton 6658495f7e6SSai Praneeth Prakhya BUILD_BUG_ON_MSG(ARRAY_SIZE(resident_page_types) != NR_MM_COUNTERS, 6668495f7e6SSai Praneeth Prakhya "Please make sure 'struct resident_page_types[]' is updated as well"); 6678495f7e6SSai Praneeth Prakhya 668d70f2a14SAndrew Morton for (i = 0; i < NR_MM_COUNTERS; i++) { 669d70f2a14SAndrew Morton long x = atomic_long_read(&mm->rss_stat.count[i]); 670d70f2a14SAndrew Morton 671d70f2a14SAndrew Morton if (unlikely(x)) 6728495f7e6SSai Praneeth Prakhya pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n", 6738495f7e6SSai Praneeth Prakhya mm, resident_page_types[i], x); 674d70f2a14SAndrew Morton } 675d70f2a14SAndrew Morton 676d70f2a14SAndrew Morton if (mm_pgtables_bytes(mm)) 677d70f2a14SAndrew Morton pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", 678d70f2a14SAndrew Morton mm_pgtables_bytes(mm)); 679d70f2a14SAndrew Morton 680d70f2a14SAndrew Morton #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 681d70f2a14SAndrew Morton VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 682d70f2a14SAndrew Morton #endif 683d70f2a14SAndrew Morton } 6841da177e4SLinus Torvalds 685e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 6861da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 6871da177e4SLinus Torvalds 688d70f2a14SAndrew Morton /* 689d70f2a14SAndrew Morton * Called when the last reference to the mm 690d70f2a14SAndrew Morton * is dropped: either by a lazy thread or by 691d70f2a14SAndrew Morton * mmput. Free the page directory and the mm. 692d70f2a14SAndrew Morton */ 693d34bc48fSAndrew Morton void __mmdrop(struct mm_struct *mm) 694d70f2a14SAndrew Morton { 695d70f2a14SAndrew Morton BUG_ON(mm == &init_mm); 6963eda69c9SMark Rutland WARN_ON_ONCE(mm == current->mm); 6973eda69c9SMark Rutland WARN_ON_ONCE(mm == current->active_mm); 698d70f2a14SAndrew Morton mm_free_pgd(mm); 699d70f2a14SAndrew Morton destroy_context(mm); 700984cfe4eSJason Gunthorpe mmu_notifier_subscriptions_destroy(mm); 701d70f2a14SAndrew Morton check_mm(mm); 702d70f2a14SAndrew Morton put_user_ns(mm->user_ns); 703d70f2a14SAndrew Morton free_mm(mm); 704d70f2a14SAndrew Morton } 705d34bc48fSAndrew Morton EXPORT_SYMBOL_GPL(__mmdrop); 706d70f2a14SAndrew Morton 707d70f2a14SAndrew Morton static void mmdrop_async_fn(struct work_struct *work) 708d70f2a14SAndrew Morton { 709d70f2a14SAndrew Morton struct mm_struct *mm; 710d70f2a14SAndrew Morton 711d70f2a14SAndrew Morton mm = container_of(work, struct mm_struct, async_put_work); 712d70f2a14SAndrew Morton __mmdrop(mm); 713d70f2a14SAndrew Morton } 714d70f2a14SAndrew Morton 715d70f2a14SAndrew Morton static void mmdrop_async(struct mm_struct *mm) 716d70f2a14SAndrew Morton { 717d70f2a14SAndrew Morton if (unlikely(atomic_dec_and_test(&mm->mm_count))) { 718d70f2a14SAndrew Morton INIT_WORK(&mm->async_put_work, mmdrop_async_fn); 719d70f2a14SAndrew Morton schedule_work(&mm->async_put_work); 720d70f2a14SAndrew Morton } 721d70f2a14SAndrew Morton } 722d70f2a14SAndrew Morton 7231da177e4SLinus Torvalds static inline void free_signal_struct(struct signal_struct *sig) 7241da177e4SLinus Torvalds { 7251da177e4SLinus Torvalds taskstats_tgid_free(sig); 7261da177e4SLinus Torvalds sched_autogroup_exit(sig); 7271da177e4SLinus Torvalds /* 7281da177e4SLinus Torvalds * __mmdrop is not safe to call from softirq context on x86 due to 7291da177e4SLinus Torvalds * pgd_dtor so postpone it to the async context 7301da177e4SLinus Torvalds */ 7311da177e4SLinus Torvalds if (sig->oom_mm) 7321da177e4SLinus Torvalds mmdrop_async(sig->oom_mm); 7331da177e4SLinus Torvalds kmem_cache_free(signal_cachep, sig); 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds static inline void put_signal_struct(struct signal_struct *sig) 7371da177e4SLinus Torvalds { 73860d4de3fSElena Reshetova if (refcount_dec_and_test(&sig->sigcnt)) 7391da177e4SLinus Torvalds free_signal_struct(sig); 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds void __put_task_struct(struct task_struct *tsk) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds WARN_ON(!tsk->exit_state); 745ec1d2819SElena Reshetova WARN_ON(refcount_read(&tsk->usage)); 7461da177e4SLinus Torvalds WARN_ON(tsk == current); 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds cgroup_free(tsk); 74916d51a59SJann Horn task_numa_free(tsk, true); 7501da177e4SLinus Torvalds security_task_free(tsk); 7511da177e4SLinus Torvalds exit_creds(tsk); 7521da177e4SLinus Torvalds delayacct_tsk_free(tsk); 7531da177e4SLinus Torvalds put_signal_struct(tsk->signal); 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 7561da177e4SLinus Torvalds free_task(tsk); 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__put_task_struct); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds void __init __weak arch_task_cache_init(void) { } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds /* 7631da177e4SLinus Torvalds * set_max_threads 7641da177e4SLinus Torvalds */ 7651da177e4SLinus Torvalds static void set_max_threads(unsigned int max_threads_suggested) 7661da177e4SLinus Torvalds { 7671da177e4SLinus Torvalds u64 threads; 768ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds /* 7711da177e4SLinus Torvalds * The number of threads shall be limited such that the thread 7721da177e4SLinus Torvalds * structures may only consume a small part of the available memory. 7731da177e4SLinus Torvalds */ 7743d6357deSArun KS if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64) 7751da177e4SLinus Torvalds threads = MAX_THREADS; 7761da177e4SLinus Torvalds else 7773d6357deSArun KS threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE, 7781da177e4SLinus Torvalds (u64) THREAD_SIZE * 8UL); 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (threads > max_threads_suggested) 7811da177e4SLinus Torvalds threads = max_threads_suggested; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 7871da177e4SLinus Torvalds /* Initialized by the architecture: */ 7881da177e4SLinus Torvalds int arch_task_struct_size __read_mostly; 7891da177e4SLinus Torvalds #endif 7901da177e4SLinus Torvalds 7914189ff23SChristoph Hellwig #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 7925905429aSKees Cook static void task_struct_whitelist(unsigned long *offset, unsigned long *size) 7935905429aSKees Cook { 7945905429aSKees Cook /* Fetch thread_struct whitelist for the architecture. */ 7955905429aSKees Cook arch_thread_struct_whitelist(offset, size); 7965905429aSKees Cook 7975905429aSKees Cook /* 7985905429aSKees Cook * Handle zero-sized whitelist or empty thread_struct, otherwise 7995905429aSKees Cook * adjust offset to position of thread_struct in task_struct. 8005905429aSKees Cook */ 8015905429aSKees Cook if (unlikely(*size == 0)) 8025905429aSKees Cook *offset = 0; 8035905429aSKees Cook else 8045905429aSKees Cook *offset += offsetof(struct task_struct, thread); 8055905429aSKees Cook } 8064189ff23SChristoph Hellwig #endif /* CONFIG_ARCH_TASK_STRUCT_ALLOCATOR */ 8075905429aSKees Cook 8081da177e4SLinus Torvalds void __init fork_init(void) 8091da177e4SLinus Torvalds { 8101da177e4SLinus Torvalds int i; 8111da177e4SLinus Torvalds #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 8121da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 8131da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN 0 8141da177e4SLinus Torvalds #endif 8151da177e4SLinus Torvalds int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); 8165905429aSKees Cook unsigned long useroffset, usersize; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 8195905429aSKees Cook task_struct_whitelist(&useroffset, &usersize); 8205905429aSKees Cook task_struct_cachep = kmem_cache_create_usercopy("task_struct", 8211da177e4SLinus Torvalds arch_task_struct_size, align, 8225905429aSKees Cook SLAB_PANIC|SLAB_ACCOUNT, 8235905429aSKees Cook useroffset, usersize, NULL); 8241da177e4SLinus Torvalds #endif 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* do the arch specific task caches init */ 8271da177e4SLinus Torvalds arch_task_cache_init(); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds set_max_threads(MAX_THREADS); 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 8321da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 8331da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 8341da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds for (i = 0; i < UCOUNT_COUNTS; i++) { 8371da177e4SLinus Torvalds init_user_ns.ucount_max[i] = max_threads/2; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds #ifdef CONFIG_VMAP_STACK 8411da177e4SLinus Torvalds cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", 8421da177e4SLinus Torvalds NULL, free_vm_stack_cache); 8431da177e4SLinus Torvalds #endif 8441da177e4SLinus Torvalds 845d08b9f0cSSami Tolvanen scs_init(); 846d08b9f0cSSami Tolvanen 8471da177e4SLinus Torvalds lockdep_init_task(&init_task); 848aad42dd4SNadav Amit uprobes_init(); 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds int __weak arch_dup_task_struct(struct task_struct *dst, 8521da177e4SLinus Torvalds struct task_struct *src) 8531da177e4SLinus Torvalds { 8541da177e4SLinus Torvalds *dst = *src; 8551da177e4SLinus Torvalds return 0; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds void set_task_stack_end_magic(struct task_struct *tsk) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds unsigned long *stackend; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds stackend = end_of_stack(tsk); 8631da177e4SLinus Torvalds *stackend = STACK_END_MAGIC; /* for overflow detection */ 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 8671da177e4SLinus Torvalds { 8681da177e4SLinus Torvalds struct task_struct *tsk; 8691da177e4SLinus Torvalds unsigned long *stack; 8700f4991e8SYueHaibing struct vm_struct *stack_vm_area __maybe_unused; 8711da177e4SLinus Torvalds int err; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds if (node == NUMA_NO_NODE) 8741da177e4SLinus Torvalds node = tsk_fork_get_node(orig); 8751da177e4SLinus Torvalds tsk = alloc_task_struct_node(node); 8761da177e4SLinus Torvalds if (!tsk) 8771da177e4SLinus Torvalds return NULL; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds stack = alloc_thread_stack_node(tsk, node); 8801da177e4SLinus Torvalds if (!stack) 8811da177e4SLinus Torvalds goto free_tsk; 8821da177e4SLinus Torvalds 8839b6f7e16SRoman Gushchin if (memcg_charge_kernel_stack(tsk)) 8849b6f7e16SRoman Gushchin goto free_stack; 8859b6f7e16SRoman Gushchin 8861da177e4SLinus Torvalds stack_vm_area = task_stack_vm_area(tsk); 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds err = arch_dup_task_struct(tsk, orig); 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds /* 8911da177e4SLinus Torvalds * arch_dup_task_struct() clobbers the stack-related fields. Make 8921da177e4SLinus Torvalds * sure they're properly initialized before using any stack-related 8931da177e4SLinus Torvalds * functions again. 8941da177e4SLinus Torvalds */ 8951da177e4SLinus Torvalds tsk->stack = stack; 8961da177e4SLinus Torvalds #ifdef CONFIG_VMAP_STACK 8971da177e4SLinus Torvalds tsk->stack_vm_area = stack_vm_area; 8981da177e4SLinus Torvalds #endif 8991da177e4SLinus Torvalds #ifdef CONFIG_THREAD_INFO_IN_TASK 900f0b89d39SElena Reshetova refcount_set(&tsk->stack_refcount, 1); 9011da177e4SLinus Torvalds #endif 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds if (err) 9041da177e4SLinus Torvalds goto free_stack; 9051da177e4SLinus Torvalds 906d08b9f0cSSami Tolvanen err = scs_prepare(tsk, node); 907d08b9f0cSSami Tolvanen if (err) 908d08b9f0cSSami Tolvanen goto free_stack; 909d08b9f0cSSami Tolvanen 9101da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 9111da177e4SLinus Torvalds /* 9121da177e4SLinus Torvalds * We must handle setting up seccomp filters once we're under 9131da177e4SLinus Torvalds * the sighand lock in case orig has changed between now and 9141da177e4SLinus Torvalds * then. Until then, filter must be NULL to avoid messing up 9151da177e4SLinus Torvalds * the usage counts on the error path calling free_task. 9161da177e4SLinus Torvalds */ 9171da177e4SLinus Torvalds tsk->seccomp.filter = NULL; 9181da177e4SLinus Torvalds #endif 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds setup_thread_stack(tsk, orig); 9211da177e4SLinus Torvalds clear_user_return_notifier(tsk); 9221da177e4SLinus Torvalds clear_tsk_need_resched(tsk); 9231da177e4SLinus Torvalds set_task_stack_end_magic(tsk); 9241da177e4SLinus Torvalds 925050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR 9261da177e4SLinus Torvalds tsk->stack_canary = get_random_canary(); 9271da177e4SLinus Torvalds #endif 9283bd37062SSebastian Andrzej Siewior if (orig->cpus_ptr == &orig->cpus_mask) 9293bd37062SSebastian Andrzej Siewior tsk->cpus_ptr = &tsk->cpus_mask; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds /* 9320ff7b2cfSEric W. Biederman * One for the user space visible state that goes away when reaped. 9330ff7b2cfSEric W. Biederman * One for the scheduler. 9341da177e4SLinus Torvalds */ 9350ff7b2cfSEric W. Biederman refcount_set(&tsk->rcu_users, 2); 9360ff7b2cfSEric W. Biederman /* One for the rcu users */ 9370ff7b2cfSEric W. Biederman refcount_set(&tsk->usage, 1); 9381da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_IO_TRACE 9391da177e4SLinus Torvalds tsk->btrace_seq = 0; 9401da177e4SLinus Torvalds #endif 9411da177e4SLinus Torvalds tsk->splice_pipe = NULL; 9421da177e4SLinus Torvalds tsk->task_frag.page = NULL; 9431da177e4SLinus Torvalds tsk->wake_q.next = NULL; 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds account_kernel_stack(tsk, 1); 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds kcov_task_init(tsk); 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds #ifdef CONFIG_FAULT_INJECTION 9501da177e4SLinus Torvalds tsk->fail_nth = 0; 9511da177e4SLinus Torvalds #endif 9521da177e4SLinus Torvalds 9532c323017SJosef Bacik #ifdef CONFIG_BLK_CGROUP 9542c323017SJosef Bacik tsk->throttle_queue = NULL; 9552c323017SJosef Bacik tsk->use_memdelay = 0; 9562c323017SJosef Bacik #endif 9572c323017SJosef Bacik 958d46eb14bSShakeel Butt #ifdef CONFIG_MEMCG 959d46eb14bSShakeel Butt tsk->active_memcg = NULL; 960d46eb14bSShakeel Butt #endif 9611da177e4SLinus Torvalds return tsk; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds free_stack: 9641da177e4SLinus Torvalds free_thread_stack(tsk); 9651da177e4SLinus Torvalds free_tsk: 9661da177e4SLinus Torvalds free_task_struct(tsk); 9671da177e4SLinus Torvalds return NULL; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 9711da177e4SLinus Torvalds 9724cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 9734cb0e11bSHidehiro Kawai 9744cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 9754cb0e11bSHidehiro Kawai { 9764cb0e11bSHidehiro Kawai default_dump_filter = 9774cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 9784cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 9794cb0e11bSHidehiro Kawai return 1; 9804cb0e11bSHidehiro Kawai } 9814cb0e11bSHidehiro Kawai 9824cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 9834cb0e11bSHidehiro Kawai 9841da177e4SLinus Torvalds #include <linux/init_task.h> 9851da177e4SLinus Torvalds 986858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 987858f0993SAlexey Dobriyan { 988858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 989858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 990db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 991858f0993SAlexey Dobriyan #endif 992858f0993SAlexey Dobriyan } 993858f0993SAlexey Dobriyan 994c3f3ce04SAndrea Arcangeli static __always_inline void mm_clear_owner(struct mm_struct *mm, 995c3f3ce04SAndrea Arcangeli struct task_struct *p) 996c3f3ce04SAndrea Arcangeli { 997c3f3ce04SAndrea Arcangeli #ifdef CONFIG_MEMCG 998c3f3ce04SAndrea Arcangeli if (mm->owner == p) 999c3f3ce04SAndrea Arcangeli WRITE_ONCE(mm->owner, NULL); 1000c3f3ce04SAndrea Arcangeli #endif 1001c3f3ce04SAndrea Arcangeli } 1002c3f3ce04SAndrea Arcangeli 100333144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 100433144e84SVladimir Davydov { 100533144e84SVladimir Davydov #ifdef CONFIG_MEMCG 100633144e84SVladimir Davydov mm->owner = p; 100733144e84SVladimir Davydov #endif 100833144e84SVladimir Davydov } 100933144e84SVladimir Davydov 1010355627f5SEric Biggers static void mm_init_uprobes_state(struct mm_struct *mm) 1011355627f5SEric Biggers { 1012355627f5SEric Biggers #ifdef CONFIG_UPROBES 1013355627f5SEric Biggers mm->uprobes_state.xol_area = NULL; 1014355627f5SEric Biggers #endif 1015355627f5SEric Biggers } 1016355627f5SEric Biggers 1017bfedb589SEric W. Biederman static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, 1018bfedb589SEric W. Biederman struct user_namespace *user_ns) 10191da177e4SLinus Torvalds { 102041f727fdSVladimir Davydov mm->mmap = NULL; 102141f727fdSVladimir Davydov mm->mm_rb = RB_ROOT; 102241f727fdSVladimir Davydov mm->vmacache_seqnum = 0; 10231da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 10241da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 1025d8ed45c5SMichel Lespinasse mmap_init_lock(mm); 10261da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 1027999d9fc1SOleg Nesterov mm->core_state = NULL; 1028af5b0f6aSKirill A. Shutemov mm_pgtables_bytes_init(mm); 102941f727fdSVladimir Davydov mm->map_count = 0; 103041f727fdSVladimir Davydov mm->locked_vm = 0; 103170f8a3caSDavidlohr Bueso atomic64_set(&mm->pinned_vm, 0); 1032d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 10331da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 103488aa7cc6SYang Shi spin_lock_init(&mm->arg_lock); 103541f727fdSVladimir Davydov mm_init_cpumask(mm); 1036858f0993SAlexey Dobriyan mm_init_aio(mm); 1037cf475ad2SBalbir Singh mm_init_owner(mm, p); 10382b7e8665SEric Biggers RCU_INIT_POINTER(mm->exe_file, NULL); 1039984cfe4eSJason Gunthorpe mmu_notifier_subscriptions_init(mm); 104016af97dcSNadav Amit init_tlb_flush_pending(mm); 104141f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 104241f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 104341f727fdSVladimir Davydov #endif 1044355627f5SEric Biggers mm_init_uprobes_state(mm); 10451da177e4SLinus Torvalds 1046a0715cc2SAlex Thorlton if (current->mm) { 1047a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 1048a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 1049a0715cc2SAlex Thorlton } else { 1050a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 10511da177e4SLinus Torvalds mm->def_flags = 0; 1052a0715cc2SAlex Thorlton } 1053a0715cc2SAlex Thorlton 105441f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 105541f727fdSVladimir Davydov goto fail_nopgd; 105678fb7466SPavel Emelianov 105741f727fdSVladimir Davydov if (init_new_context(p, mm)) 105841f727fdSVladimir Davydov goto fail_nocontext; 105941f727fdSVladimir Davydov 1060bfedb589SEric W. Biederman mm->user_ns = get_user_ns(user_ns); 106141f727fdSVladimir Davydov return mm; 106241f727fdSVladimir Davydov 106341f727fdSVladimir Davydov fail_nocontext: 106441f727fdSVladimir Davydov mm_free_pgd(mm); 106541f727fdSVladimir Davydov fail_nopgd: 10661da177e4SLinus Torvalds free_mm(mm); 10671da177e4SLinus Torvalds return NULL; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds /* 10711da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 10721da177e4SLinus Torvalds */ 10731da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 10741da177e4SLinus Torvalds { 10751da177e4SLinus Torvalds struct mm_struct *mm; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds mm = allocate_mm(); 1078de03c72cSKOSAKI Motohiro if (!mm) 1079de03c72cSKOSAKI Motohiro return NULL; 1080de03c72cSKOSAKI Motohiro 10811da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 1082bfedb589SEric W. Biederman return mm_init(mm, current, current_user_ns()); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 1085ec8d7c14SMichal Hocko static inline void __mmput(struct mm_struct *mm) 10861da177e4SLinus Torvalds { 1087ec8d7c14SMichal Hocko VM_BUG_ON(atomic_read(&mm->mm_users)); 10880ae26f1bSAndrew Morton 1089d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 10901da177e4SLinus Torvalds exit_aio(mm); 10911c2fb7a4SAndrea Arcangeli ksm_exit(mm); 1092ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 10931da177e4SLinus Torvalds exit_mmap(mm); 10946fcb52a5SAaron Lu mm_put_huge_zero_page(mm); 1095925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 10961da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 10971da177e4SLinus Torvalds spin_lock(&mmlist_lock); 10981da177e4SLinus Torvalds list_del(&mm->mmlist); 10991da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 11001da177e4SLinus Torvalds } 1101801460d0SHiroshi Shimamoto if (mm->binfmt) 1102801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 11031da177e4SLinus Torvalds mmdrop(mm); 11041da177e4SLinus Torvalds } 1105ec8d7c14SMichal Hocko 1106ec8d7c14SMichal Hocko /* 1107ec8d7c14SMichal Hocko * Decrement the use count and release all resources for an mm. 1108ec8d7c14SMichal Hocko */ 1109ec8d7c14SMichal Hocko void mmput(struct mm_struct *mm) 1110ec8d7c14SMichal Hocko { 1111ec8d7c14SMichal Hocko might_sleep(); 1112ec8d7c14SMichal Hocko 1113ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) 1114ec8d7c14SMichal Hocko __mmput(mm); 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 11171da177e4SLinus Torvalds 1118a1b2289cSSherry Yang #ifdef CONFIG_MMU 1119a1b2289cSSherry Yang static void mmput_async_fn(struct work_struct *work) 1120a1b2289cSSherry Yang { 1121a1b2289cSSherry Yang struct mm_struct *mm = container_of(work, struct mm_struct, 1122a1b2289cSSherry Yang async_put_work); 1123a1b2289cSSherry Yang 1124a1b2289cSSherry Yang __mmput(mm); 1125a1b2289cSSherry Yang } 1126a1b2289cSSherry Yang 1127a1b2289cSSherry Yang void mmput_async(struct mm_struct *mm) 1128a1b2289cSSherry Yang { 1129a1b2289cSSherry Yang if (atomic_dec_and_test(&mm->mm_users)) { 1130a1b2289cSSherry Yang INIT_WORK(&mm->async_put_work, mmput_async_fn); 1131a1b2289cSSherry Yang schedule_work(&mm->async_put_work); 1132a1b2289cSSherry Yang } 1133a1b2289cSSherry Yang } 1134a1b2289cSSherry Yang #endif 1135a1b2289cSSherry Yang 113690f31d0eSKonstantin Khlebnikov /** 113790f31d0eSKonstantin Khlebnikov * set_mm_exe_file - change a reference to the mm's executable file 113890f31d0eSKonstantin Khlebnikov * 113990f31d0eSKonstantin Khlebnikov * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 114090f31d0eSKonstantin Khlebnikov * 11416e399cd1SDavidlohr Bueso * Main users are mmput() and sys_execve(). Callers prevent concurrent 11426e399cd1SDavidlohr Bueso * invocations: in mmput() nobody alive left, in execve task is single 11436e399cd1SDavidlohr Bueso * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the 11446e399cd1SDavidlohr Bueso * mm->exe_file, but does so without using set_mm_exe_file() in order 11456e399cd1SDavidlohr Bueso * to do avoid the need for any locks. 114690f31d0eSKonstantin Khlebnikov */ 114738646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 114838646013SJiri Slaby { 11496e399cd1SDavidlohr Bueso struct file *old_exe_file; 11506e399cd1SDavidlohr Bueso 11516e399cd1SDavidlohr Bueso /* 11526e399cd1SDavidlohr Bueso * It is safe to dereference the exe_file without RCU as 11536e399cd1SDavidlohr Bueso * this function is only called if nobody else can access 11546e399cd1SDavidlohr Bueso * this mm -- see comment above for justification. 11556e399cd1SDavidlohr Bueso */ 11566e399cd1SDavidlohr Bueso old_exe_file = rcu_dereference_raw(mm->exe_file); 115790f31d0eSKonstantin Khlebnikov 115838646013SJiri Slaby if (new_exe_file) 115938646013SJiri Slaby get_file(new_exe_file); 116090f31d0eSKonstantin Khlebnikov rcu_assign_pointer(mm->exe_file, new_exe_file); 116190f31d0eSKonstantin Khlebnikov if (old_exe_file) 116290f31d0eSKonstantin Khlebnikov fput(old_exe_file); 116338646013SJiri Slaby } 116438646013SJiri Slaby 116590f31d0eSKonstantin Khlebnikov /** 116690f31d0eSKonstantin Khlebnikov * get_mm_exe_file - acquire a reference to the mm's executable file 116790f31d0eSKonstantin Khlebnikov * 116890f31d0eSKonstantin Khlebnikov * Returns %NULL if mm has no associated executable file. 116990f31d0eSKonstantin Khlebnikov * User must release file via fput(). 117090f31d0eSKonstantin Khlebnikov */ 117138646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 117238646013SJiri Slaby { 117338646013SJiri Slaby struct file *exe_file; 117438646013SJiri Slaby 117590f31d0eSKonstantin Khlebnikov rcu_read_lock(); 117690f31d0eSKonstantin Khlebnikov exe_file = rcu_dereference(mm->exe_file); 117790f31d0eSKonstantin Khlebnikov if (exe_file && !get_file_rcu(exe_file)) 117890f31d0eSKonstantin Khlebnikov exe_file = NULL; 117990f31d0eSKonstantin Khlebnikov rcu_read_unlock(); 118038646013SJiri Slaby return exe_file; 118138646013SJiri Slaby } 118211163348SDavidlohr Bueso EXPORT_SYMBOL(get_mm_exe_file); 118338646013SJiri Slaby 11841da177e4SLinus Torvalds /** 1185cd81a917SMateusz Guzik * get_task_exe_file - acquire a reference to the task's executable file 1186cd81a917SMateusz Guzik * 1187cd81a917SMateusz Guzik * Returns %NULL if task's mm (if any) has no associated executable file or 1188cd81a917SMateusz Guzik * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 1189cd81a917SMateusz Guzik * User must release file via fput(). 1190cd81a917SMateusz Guzik */ 1191cd81a917SMateusz Guzik struct file *get_task_exe_file(struct task_struct *task) 1192cd81a917SMateusz Guzik { 1193cd81a917SMateusz Guzik struct file *exe_file = NULL; 1194cd81a917SMateusz Guzik struct mm_struct *mm; 1195cd81a917SMateusz Guzik 1196cd81a917SMateusz Guzik task_lock(task); 1197cd81a917SMateusz Guzik mm = task->mm; 1198cd81a917SMateusz Guzik if (mm) { 1199cd81a917SMateusz Guzik if (!(task->flags & PF_KTHREAD)) 1200cd81a917SMateusz Guzik exe_file = get_mm_exe_file(mm); 1201cd81a917SMateusz Guzik } 1202cd81a917SMateusz Guzik task_unlock(task); 1203cd81a917SMateusz Guzik return exe_file; 1204cd81a917SMateusz Guzik } 1205cd81a917SMateusz Guzik EXPORT_SYMBOL(get_task_exe_file); 1206cd81a917SMateusz Guzik 1207cd81a917SMateusz Guzik /** 12081da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 12091da177e4SLinus Torvalds * 1210246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 12111da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 12121da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 12131da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 12141da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 12151da177e4SLinus Torvalds */ 12161da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 12171da177e4SLinus Torvalds { 12181da177e4SLinus Torvalds struct mm_struct *mm; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds task_lock(task); 12211da177e4SLinus Torvalds mm = task->mm; 12221da177e4SLinus Torvalds if (mm) { 1223246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 12241da177e4SLinus Torvalds mm = NULL; 12251da177e4SLinus Torvalds else 12263fce371bSVegard Nossum mmget(mm); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds task_unlock(task); 12291da177e4SLinus Torvalds return mm; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 12321da177e4SLinus Torvalds 12338cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 12348cdb878dSChristopher Yeoh { 12358cdb878dSChristopher Yeoh struct mm_struct *mm; 12368cdb878dSChristopher Yeoh int err; 12378cdb878dSChristopher Yeoh 12383e74fabdSBernd Edlinger err = mutex_lock_killable(&task->signal->exec_update_mutex); 12398cdb878dSChristopher Yeoh if (err) 12408cdb878dSChristopher Yeoh return ERR_PTR(err); 12418cdb878dSChristopher Yeoh 12428cdb878dSChristopher Yeoh mm = get_task_mm(task); 12438cdb878dSChristopher Yeoh if (mm && mm != current->mm && 12448cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 12458cdb878dSChristopher Yeoh mmput(mm); 12468cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 12478cdb878dSChristopher Yeoh } 12483e74fabdSBernd Edlinger mutex_unlock(&task->signal->exec_update_mutex); 12498cdb878dSChristopher Yeoh 12508cdb878dSChristopher Yeoh return mm; 12518cdb878dSChristopher Yeoh } 12528cdb878dSChristopher Yeoh 125357b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 1254c415c3b4SOleg Nesterov { 1255d68b46feSOleg Nesterov struct completion *vfork; 1256c415c3b4SOleg Nesterov 1257d68b46feSOleg Nesterov task_lock(tsk); 1258d68b46feSOleg Nesterov vfork = tsk->vfork_done; 1259d68b46feSOleg Nesterov if (likely(vfork)) { 1260c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 1261d68b46feSOleg Nesterov complete(vfork); 1262d68b46feSOleg Nesterov } 1263d68b46feSOleg Nesterov task_unlock(tsk); 1264d68b46feSOleg Nesterov } 1265d68b46feSOleg Nesterov 1266d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 1267d68b46feSOleg Nesterov struct completion *vfork) 1268d68b46feSOleg Nesterov { 1269d68b46feSOleg Nesterov int killed; 1270d68b46feSOleg Nesterov 1271d68b46feSOleg Nesterov freezer_do_not_count(); 127276f969e8SRoman Gushchin cgroup_enter_frozen(); 1273d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 127476f969e8SRoman Gushchin cgroup_leave_frozen(false); 1275d68b46feSOleg Nesterov freezer_count(); 1276d68b46feSOleg Nesterov 1277d68b46feSOleg Nesterov if (killed) { 1278d68b46feSOleg Nesterov task_lock(child); 1279d68b46feSOleg Nesterov child->vfork_done = NULL; 1280d68b46feSOleg Nesterov task_unlock(child); 1281d68b46feSOleg Nesterov } 1282d68b46feSOleg Nesterov 1283d68b46feSOleg Nesterov put_task_struct(child); 1284d68b46feSOleg Nesterov return killed; 1285c415c3b4SOleg Nesterov } 1286c415c3b4SOleg Nesterov 12871da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 12881da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 12891da177e4SLinus Torvalds * error success whatever. 12901da177e4SLinus Torvalds * 12911da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 12921da177e4SLinus Torvalds * from the current process. 12931da177e4SLinus Torvalds * 12941da177e4SLinus Torvalds * This difference is important for error handling, when we 12951da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 12961da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 12971da177e4SLinus Torvalds * restoring the old one. . . 12981da177e4SLinus Torvalds * Eric Biederman 10 January 1998 12991da177e4SLinus Torvalds */ 13004610ba7aSThomas Gleixner static void mm_release(struct task_struct *tsk, struct mm_struct *mm) 13011da177e4SLinus Torvalds { 13020326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 13030326f5a9SSrikar Dronamraju 13041da177e4SLinus Torvalds /* Get rid of any cached register state */ 13051da177e4SLinus Torvalds deactivate_mm(tsk, mm); 13061da177e4SLinus Torvalds 1307fec1d011SRoland McGrath /* 1308735f2770SMichal Hocko * Signal userspace if we're not exiting with a core dump 1309735f2770SMichal Hocko * because we want to leave the value intact for debugging 1310735f2770SMichal Hocko * purposes. 1311fec1d011SRoland McGrath */ 13129c8a8228SEric Dumazet if (tsk->clear_child_tid) { 1313735f2770SMichal Hocko if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) && 13149c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 13151da177e4SLinus Torvalds /* 13161da177e4SLinus Torvalds * We don't check the error code - if userspace has 13171da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 13181da177e4SLinus Torvalds */ 13199c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 13202de0db99SDominik Brodowski do_futex(tsk->clear_child_tid, FUTEX_WAKE, 13212de0db99SDominik Brodowski 1, NULL, NULL, 0, 0); 13229c8a8228SEric Dumazet } 13239c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 13241da177e4SLinus Torvalds } 1325f7505d64SKonstantin Khlebnikov 1326f7505d64SKonstantin Khlebnikov /* 1327f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 1328f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 1329f7505d64SKonstantin Khlebnikov */ 1330f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 1331f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13344610ba7aSThomas Gleixner void exit_mm_release(struct task_struct *tsk, struct mm_struct *mm) 13354610ba7aSThomas Gleixner { 1336150d7158SThomas Gleixner futex_exit_release(tsk); 13374610ba7aSThomas Gleixner mm_release(tsk, mm); 13384610ba7aSThomas Gleixner } 13394610ba7aSThomas Gleixner 13404610ba7aSThomas Gleixner void exec_mm_release(struct task_struct *tsk, struct mm_struct *mm) 13414610ba7aSThomas Gleixner { 1342150d7158SThomas Gleixner futex_exec_release(tsk); 13434610ba7aSThomas Gleixner mm_release(tsk, mm); 13444610ba7aSThomas Gleixner } 13454610ba7aSThomas Gleixner 134613585fa0SNadav Amit /** 134713585fa0SNadav Amit * dup_mm() - duplicates an existing mm structure 134813585fa0SNadav Amit * @tsk: the task_struct with which the new mm will be associated. 134913585fa0SNadav Amit * @oldmm: the mm to duplicate. 135013585fa0SNadav Amit * 135113585fa0SNadav Amit * Allocates a new mm structure and duplicates the provided @oldmm structure 135213585fa0SNadav Amit * content into it. 135313585fa0SNadav Amit * 135413585fa0SNadav Amit * Return: the duplicated mm or NULL on failure. 1355a0a7ec30SJANAK DESAI */ 135613585fa0SNadav Amit static struct mm_struct *dup_mm(struct task_struct *tsk, 135713585fa0SNadav Amit struct mm_struct *oldmm) 1358a0a7ec30SJANAK DESAI { 135913585fa0SNadav Amit struct mm_struct *mm; 1360a0a7ec30SJANAK DESAI int err; 1361a0a7ec30SJANAK DESAI 1362a0a7ec30SJANAK DESAI mm = allocate_mm(); 1363a0a7ec30SJANAK DESAI if (!mm) 1364a0a7ec30SJANAK DESAI goto fail_nomem; 1365a0a7ec30SJANAK DESAI 1366a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 1367a0a7ec30SJANAK DESAI 1368bfedb589SEric W. Biederman if (!mm_init(mm, tsk, mm->user_ns)) 1369a0a7ec30SJANAK DESAI goto fail_nomem; 1370a0a7ec30SJANAK DESAI 1371a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 1372a0a7ec30SJANAK DESAI if (err) 1373a0a7ec30SJANAK DESAI goto free_pt; 1374a0a7ec30SJANAK DESAI 1375a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 1376a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 1377a0a7ec30SJANAK DESAI 1378801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1379801460d0SHiroshi Shimamoto goto free_pt; 1380801460d0SHiroshi Shimamoto 1381a0a7ec30SJANAK DESAI return mm; 1382a0a7ec30SJANAK DESAI 1383a0a7ec30SJANAK DESAI free_pt: 1384801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 1385801460d0SHiroshi Shimamoto mm->binfmt = NULL; 1386c3f3ce04SAndrea Arcangeli mm_init_owner(mm, NULL); 1387a0a7ec30SJANAK DESAI mmput(mm); 1388a0a7ec30SJANAK DESAI 1389a0a7ec30SJANAK DESAI fail_nomem: 1390a0a7ec30SJANAK DESAI return NULL; 1391a0a7ec30SJANAK DESAI } 1392a0a7ec30SJANAK DESAI 13931da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 13941da177e4SLinus Torvalds { 13951da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 13961da177e4SLinus Torvalds int retval; 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 13991da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 140017406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 140117406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 1402a2e51445SDmitry Vyukov tsk->last_switch_time = 0; 140317406b82SMandeep Singh Baines #endif 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds tsk->mm = NULL; 14061da177e4SLinus Torvalds tsk->active_mm = NULL; 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds /* 14091da177e4SLinus Torvalds * Are we cloning a kernel thread? 14101da177e4SLinus Torvalds * 14111da177e4SLinus Torvalds * We need to steal a active VM for that.. 14121da177e4SLinus Torvalds */ 14131da177e4SLinus Torvalds oldmm = current->mm; 14141da177e4SLinus Torvalds if (!oldmm) 14151da177e4SLinus Torvalds return 0; 14161da177e4SLinus Torvalds 1417615d6e87SDavidlohr Bueso /* initialize the new vmacache entries */ 1418615d6e87SDavidlohr Bueso vmacache_flush(tsk); 1419615d6e87SDavidlohr Bueso 14201da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 14213fce371bSVegard Nossum mmget(oldmm); 14221da177e4SLinus Torvalds mm = oldmm; 14231da177e4SLinus Torvalds goto good_mm; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds retval = -ENOMEM; 142713585fa0SNadav Amit mm = dup_mm(tsk, current->mm); 14281da177e4SLinus Torvalds if (!mm) 14291da177e4SLinus Torvalds goto fail_nomem; 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds good_mm: 14321da177e4SLinus Torvalds tsk->mm = mm; 14331da177e4SLinus Torvalds tsk->active_mm = mm; 14341da177e4SLinus Torvalds return 0; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds fail_nomem: 14371da177e4SLinus Torvalds return retval; 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds 1440a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 14411da177e4SLinus Torvalds { 1442498052bbSAl Viro struct fs_struct *fs = current->fs; 14431da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 1444498052bbSAl Viro /* tsk->fs is already what we want */ 14452a4419b5SNick Piggin spin_lock(&fs->lock); 1446498052bbSAl Viro if (fs->in_exec) { 14472a4419b5SNick Piggin spin_unlock(&fs->lock); 1448498052bbSAl Viro return -EAGAIN; 1449498052bbSAl Viro } 1450498052bbSAl Viro fs->users++; 14512a4419b5SNick Piggin spin_unlock(&fs->lock); 14521da177e4SLinus Torvalds return 0; 14531da177e4SLinus Torvalds } 1454498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 14551da177e4SLinus Torvalds if (!tsk->fs) 14561da177e4SLinus Torvalds return -ENOMEM; 14571da177e4SLinus Torvalds return 0; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 1460a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 1461a016f338SJANAK DESAI { 1462a016f338SJANAK DESAI struct files_struct *oldf, *newf; 1463a016f338SJANAK DESAI int error = 0; 1464a016f338SJANAK DESAI 1465a016f338SJANAK DESAI /* 1466a016f338SJANAK DESAI * A background process may not have any files ... 1467a016f338SJANAK DESAI */ 1468a016f338SJANAK DESAI oldf = current->files; 1469a016f338SJANAK DESAI if (!oldf) 1470a016f338SJANAK DESAI goto out; 1471a016f338SJANAK DESAI 1472a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 1473a016f338SJANAK DESAI atomic_inc(&oldf->count); 1474a016f338SJANAK DESAI goto out; 1475a016f338SJANAK DESAI } 1476a016f338SJANAK DESAI 1477a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 1478a016f338SJANAK DESAI if (!newf) 1479a016f338SJANAK DESAI goto out; 1480a016f338SJANAK DESAI 1481a016f338SJANAK DESAI tsk->files = newf; 1482a016f338SJANAK DESAI error = 0; 1483a016f338SJANAK DESAI out: 1484a016f338SJANAK DESAI return error; 1485a016f338SJANAK DESAI } 1486a016f338SJANAK DESAI 1487fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 1488fd0928dfSJens Axboe { 1489fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 1490fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 14916e736be7STejun Heo struct io_context *new_ioc; 1492fd0928dfSJens Axboe 1493fd0928dfSJens Axboe if (!ioc) 1494fd0928dfSJens Axboe return 0; 1495fadad878SJens Axboe /* 1496fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 1497fadad878SJens Axboe */ 1498fadad878SJens Axboe if (clone_flags & CLONE_IO) { 14993d48749dSTejun Heo ioc_task_link(ioc); 15003d48749dSTejun Heo tsk->io_context = ioc; 1501fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 15026e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 15036e736be7STejun Heo if (unlikely(!new_ioc)) 1504fd0928dfSJens Axboe return -ENOMEM; 1505fd0928dfSJens Axboe 15066e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 150711a3122fSTejun Heo put_io_context(new_ioc); 1508fd0928dfSJens Axboe } 1509fd0928dfSJens Axboe #endif 1510fd0928dfSJens Axboe return 0; 1511fd0928dfSJens Axboe } 1512fd0928dfSJens Axboe 1513a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 15141da177e4SLinus Torvalds { 15151da177e4SLinus Torvalds struct sighand_struct *sig; 15161da177e4SLinus Torvalds 151760348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 1518d036bda7SElena Reshetova refcount_inc(¤t->sighand->count); 15191da177e4SLinus Torvalds return 0; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 15220c282b06SMadhuparna Bhowmik RCU_INIT_POINTER(tsk->sighand, sig); 15231da177e4SLinus Torvalds if (!sig) 15241da177e4SLinus Torvalds return -ENOMEM; 15259d7fb042SPeter Zijlstra 1526d036bda7SElena Reshetova refcount_set(&sig->count, 1); 152706e62a46SJann Horn spin_lock_irq(¤t->sighand->siglock); 15281da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 152906e62a46SJann Horn spin_unlock_irq(¤t->sighand->siglock); 1530b612e5dfSChristian Brauner 1531b612e5dfSChristian Brauner /* Reset all signal handler not set to SIG_IGN to SIG_DFL. */ 1532b612e5dfSChristian Brauner if (clone_flags & CLONE_CLEAR_SIGHAND) 1533b612e5dfSChristian Brauner flush_signal_handlers(tsk, 0); 1534b612e5dfSChristian Brauner 15351da177e4SLinus Torvalds return 0; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds 1538a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1539c81addc9SOleg Nesterov { 1540d036bda7SElena Reshetova if (refcount_dec_and_test(&sighand->count)) { 1541d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1542392809b2SOleg Nesterov /* 15435f0d5a3aSPaul E. McKenney * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it 1544392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1545392809b2SOleg Nesterov */ 1546c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1547c81addc9SOleg Nesterov } 1548d80e731eSOleg Nesterov } 1549c81addc9SOleg Nesterov 1550f06febc9SFrank Mayhar /* 1551f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1552f06febc9SFrank Mayhar */ 1553f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1554f06febc9SFrank Mayhar { 15552b69942fSThomas Gleixner struct posix_cputimers *pct = &sig->posix_cputimers; 155678d7d407SJiri Slaby unsigned long cpu_limit; 155778d7d407SJiri Slaby 1558316c1608SJason Low cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 15593a245c0fSThomas Gleixner posix_cputimers_group_init(pct, cpu_limit); 15606279a751SOleg Nesterov } 15616279a751SOleg Nesterov 1562a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 15631da177e4SLinus Torvalds { 15641da177e4SLinus Torvalds struct signal_struct *sig; 15651da177e4SLinus Torvalds 15664ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1567490dea45SPeter Zijlstra return 0; 15686279a751SOleg Nesterov 1569a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 15701da177e4SLinus Torvalds tsk->signal = sig; 15711da177e4SLinus Torvalds if (!sig) 15721da177e4SLinus Torvalds return -ENOMEM; 15731da177e4SLinus Torvalds 1574b3ac022cSOleg Nesterov sig->nr_threads = 1; 15751da177e4SLinus Torvalds atomic_set(&sig->live, 1); 157660d4de3fSElena Reshetova refcount_set(&sig->sigcnt, 1); 15770c740d0aSOleg Nesterov 15780c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 15790c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 15800c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 15810c740d0aSOleg Nesterov 15821da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1583db51aeccSOleg Nesterov sig->curr_target = tsk; 15841da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 1585c3ad2c3bSEric W. Biederman INIT_HLIST_HEAD(&sig->multiprocess); 1586e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 15879d7fb042SPeter Zijlstra prev_cputime_init(&sig->prev_cputime); 15881da177e4SLinus Torvalds 1589baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 1590b18b6a9cSNicolas Pitre INIT_LIST_HEAD(&sig->posix_timers); 1591c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 15921da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 1593baa73d9eSNicolas Pitre #endif 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds task_lock(current->group_leader); 15961da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 15971da177e4SLinus Torvalds task_unlock(current->group_leader); 15981da177e4SLinus Torvalds 15996279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 16006279a751SOleg Nesterov 1601522ed776SMiloslav Trmac tty_audit_fork(sig); 16025091faa4SMike Galbraith sched_autogroup_fork(sig); 1603522ed776SMiloslav Trmac 1604a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1605dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 160628b83c51SKOSAKI Motohiro 16079b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 1608eea96732SEric W. Biederman mutex_init(&sig->exec_update_mutex); 16099b1bf12dSKOSAKI Motohiro 16101da177e4SLinus Torvalds return 0; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 1613dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1614dbd95212SKees Cook { 1615dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1616dbd95212SKees Cook /* 1617dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1618dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1619dbd95212SKees Cook * needed because this new task is not yet running and cannot 1620dbd95212SKees Cook * be racing exec. 1621dbd95212SKees Cook */ 162269f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1623dbd95212SKees Cook 1624dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1625dbd95212SKees Cook get_seccomp_filter(current); 1626dbd95212SKees Cook p->seccomp = current->seccomp; 1627dbd95212SKees Cook 1628dbd95212SKees Cook /* 1629dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1630dbd95212SKees Cook * between the task_struct being duplicated and holding the 1631dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1632dbd95212SKees Cook */ 1633dbd95212SKees Cook if (task_no_new_privs(current)) 1634dbd95212SKees Cook task_set_no_new_privs(p); 1635dbd95212SKees Cook 1636dbd95212SKees Cook /* 1637dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1638dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1639dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1640dbd95212SKees Cook */ 1641dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1642dbd95212SKees Cook set_tsk_thread_flag(p, TIF_SECCOMP); 1643dbd95212SKees Cook #endif 1644dbd95212SKees Cook } 1645dbd95212SKees Cook 164617da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 16471da177e4SLinus Torvalds { 16481da177e4SLinus Torvalds current->clear_child_tid = tidptr; 16491da177e4SLinus Torvalds 1650b488893aSPavel Emelyanov return task_pid_vnr(current); 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 1653a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 165423f78d4aSIngo Molnar { 16551d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1656e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1657a23ba907SDavidlohr Bueso p->pi_waiters = RB_ROOT_CACHED; 1658e96a7705SXunlei Pang p->pi_top_task = NULL; 165923f78d4aSIngo Molnar p->pi_blocked_on = NULL; 166023f78d4aSIngo Molnar #endif 166123f78d4aSIngo Molnar } 166223f78d4aSIngo Molnar 16632c470475SEric W. Biederman static inline void init_task_pid_links(struct task_struct *task) 16642c470475SEric W. Biederman { 16652c470475SEric W. Biederman enum pid_type type; 16662c470475SEric W. Biederman 16672c470475SEric W. Biederman for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 16682c470475SEric W. Biederman INIT_HLIST_NODE(&task->pid_links[type]); 16692c470475SEric W. Biederman } 16702c470475SEric W. Biederman } 16712c470475SEric W. Biederman 167281907739SOleg Nesterov static inline void 167381907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 167481907739SOleg Nesterov { 16752c470475SEric W. Biederman if (type == PIDTYPE_PID) 16762c470475SEric W. Biederman task->thread_pid = pid; 16772c470475SEric W. Biederman else 16782c470475SEric W. Biederman task->signal->pids[type] = pid; 167981907739SOleg Nesterov } 168081907739SOleg Nesterov 16816bfbaa51SIngo Molnar static inline void rcu_copy_process(struct task_struct *p) 16826bfbaa51SIngo Molnar { 16836bfbaa51SIngo Molnar #ifdef CONFIG_PREEMPT_RCU 16846bfbaa51SIngo Molnar p->rcu_read_lock_nesting = 0; 16856bfbaa51SIngo Molnar p->rcu_read_unlock_special.s = 0; 16866bfbaa51SIngo Molnar p->rcu_blocked_node = NULL; 16876bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_node_entry); 16886bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_PREEMPT_RCU */ 16896bfbaa51SIngo Molnar #ifdef CONFIG_TASKS_RCU 16906bfbaa51SIngo Molnar p->rcu_tasks_holdout = false; 16916bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); 16926bfbaa51SIngo Molnar p->rcu_tasks_idle_cpu = -1; 16936bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_TASKS_RCU */ 1694d5f177d3SPaul E. McKenney #ifdef CONFIG_TASKS_TRACE_RCU 1695d5f177d3SPaul E. McKenney p->trc_reader_nesting = 0; 1696276c4104SPaul E. McKenney p->trc_reader_special.s = 0; 1697d5f177d3SPaul E. McKenney INIT_LIST_HEAD(&p->trc_holdout_list); 1698d5f177d3SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */ 16996bfbaa51SIngo Molnar } 17006bfbaa51SIngo Molnar 17013695eae5SChristian Brauner struct pid *pidfd_pid(const struct file *file) 17023695eae5SChristian Brauner { 17033695eae5SChristian Brauner if (file->f_op == &pidfd_fops) 17043695eae5SChristian Brauner return file->private_data; 17053695eae5SChristian Brauner 17063695eae5SChristian Brauner return ERR_PTR(-EBADF); 17073695eae5SChristian Brauner } 17083695eae5SChristian Brauner 1709b3e58382SChristian Brauner static int pidfd_release(struct inode *inode, struct file *file) 1710b3e58382SChristian Brauner { 1711b3e58382SChristian Brauner struct pid *pid = file->private_data; 1712b3e58382SChristian Brauner 1713b3e58382SChristian Brauner file->private_data = NULL; 1714b3e58382SChristian Brauner put_pid(pid); 1715b3e58382SChristian Brauner return 0; 1716b3e58382SChristian Brauner } 1717b3e58382SChristian Brauner 1718b3e58382SChristian Brauner #ifdef CONFIG_PROC_FS 171915d42eb2SChristian Kellner /** 172015d42eb2SChristian Kellner * pidfd_show_fdinfo - print information about a pidfd 172115d42eb2SChristian Kellner * @m: proc fdinfo file 172215d42eb2SChristian Kellner * @f: file referencing a pidfd 172315d42eb2SChristian Kellner * 172415d42eb2SChristian Kellner * Pid: 172515d42eb2SChristian Kellner * This function will print the pid that a given pidfd refers to in the 172615d42eb2SChristian Kellner * pid namespace of the procfs instance. 172715d42eb2SChristian Kellner * If the pid namespace of the process is not a descendant of the pid 172815d42eb2SChristian Kellner * namespace of the procfs instance 0 will be shown as its pid. This is 172915d42eb2SChristian Kellner * similar to calling getppid() on a process whose parent is outside of 173015d42eb2SChristian Kellner * its pid namespace. 173115d42eb2SChristian Kellner * 173215d42eb2SChristian Kellner * NSpid: 173315d42eb2SChristian Kellner * If pid namespaces are supported then this function will also print 173415d42eb2SChristian Kellner * the pid of a given pidfd refers to for all descendant pid namespaces 173515d42eb2SChristian Kellner * starting from the current pid namespace of the instance, i.e. the 173615d42eb2SChristian Kellner * Pid field and the first entry in the NSpid field will be identical. 173715d42eb2SChristian Kellner * If the pid namespace of the process is not a descendant of the pid 173815d42eb2SChristian Kellner * namespace of the procfs instance 0 will be shown as its first NSpid 173915d42eb2SChristian Kellner * entry and no others will be shown. 174015d42eb2SChristian Kellner * Note that this differs from the Pid and NSpid fields in 174115d42eb2SChristian Kellner * /proc/<pid>/status where Pid and NSpid are always shown relative to 174215d42eb2SChristian Kellner * the pid namespace of the procfs instance. The difference becomes 174315d42eb2SChristian Kellner * obvious when sending around a pidfd between pid namespaces from a 174415d42eb2SChristian Kellner * different branch of the tree, i.e. where no ancestoral relation is 174515d42eb2SChristian Kellner * present between the pid namespaces: 174615d42eb2SChristian Kellner * - create two new pid namespaces ns1 and ns2 in the initial pid 174715d42eb2SChristian Kellner * namespace (also take care to create new mount namespaces in the 174815d42eb2SChristian Kellner * new pid namespace and mount procfs) 174915d42eb2SChristian Kellner * - create a process with a pidfd in ns1 175015d42eb2SChristian Kellner * - send pidfd from ns1 to ns2 175115d42eb2SChristian Kellner * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid 175215d42eb2SChristian Kellner * have exactly one entry, which is 0 175315d42eb2SChristian Kellner */ 1754b3e58382SChristian Brauner static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) 1755b3e58382SChristian Brauner { 1756b3e58382SChristian Brauner struct pid *pid = f->private_data; 17573d6d8da4SChristian Brauner struct pid_namespace *ns; 17583d6d8da4SChristian Brauner pid_t nr = -1; 1759b3e58382SChristian Brauner 17603d6d8da4SChristian Brauner if (likely(pid_has_task(pid, PIDTYPE_PID))) { 17619d78edeaSAlexey Gladkov ns = proc_pid_ns(file_inode(m->file)->i_sb); 17623d6d8da4SChristian Brauner nr = pid_nr_ns(pid, ns); 17633d6d8da4SChristian Brauner } 17643d6d8da4SChristian Brauner 17653d6d8da4SChristian Brauner seq_put_decimal_ll(m, "Pid:\t", nr); 176615d42eb2SChristian Kellner 176715d42eb2SChristian Kellner #ifdef CONFIG_PID_NS 17683d6d8da4SChristian Brauner seq_put_decimal_ll(m, "\nNSpid:\t", nr); 17693d6d8da4SChristian Brauner if (nr > 0) { 177015d42eb2SChristian Kellner int i; 177115d42eb2SChristian Kellner 177215d42eb2SChristian Kellner /* If nr is non-zero it means that 'pid' is valid and that 177315d42eb2SChristian Kellner * ns, i.e. the pid namespace associated with the procfs 177415d42eb2SChristian Kellner * instance, is in the pid namespace hierarchy of pid. 177515d42eb2SChristian Kellner * Start at one below the already printed level. 177615d42eb2SChristian Kellner */ 177715d42eb2SChristian Kellner for (i = ns->level + 1; i <= pid->level; i++) 17783d6d8da4SChristian Brauner seq_put_decimal_ll(m, "\t", pid->numbers[i].nr); 177915d42eb2SChristian Kellner } 178015d42eb2SChristian Kellner #endif 1781b3e58382SChristian Brauner seq_putc(m, '\n'); 1782b3e58382SChristian Brauner } 1783b3e58382SChristian Brauner #endif 1784b3e58382SChristian Brauner 1785b53b0b9dSJoel Fernandes (Google) /* 1786b53b0b9dSJoel Fernandes (Google) * Poll support for process exit notification. 1787b53b0b9dSJoel Fernandes (Google) */ 17889e77716aSLuc Van Oostenryck static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) 1789b53b0b9dSJoel Fernandes (Google) { 1790b53b0b9dSJoel Fernandes (Google) struct task_struct *task; 1791b53b0b9dSJoel Fernandes (Google) struct pid *pid = file->private_data; 17929e77716aSLuc Van Oostenryck __poll_t poll_flags = 0; 1793b53b0b9dSJoel Fernandes (Google) 1794b53b0b9dSJoel Fernandes (Google) poll_wait(file, &pid->wait_pidfd, pts); 1795b53b0b9dSJoel Fernandes (Google) 1796b53b0b9dSJoel Fernandes (Google) rcu_read_lock(); 1797b53b0b9dSJoel Fernandes (Google) task = pid_task(pid, PIDTYPE_PID); 1798b53b0b9dSJoel Fernandes (Google) /* 1799b53b0b9dSJoel Fernandes (Google) * Inform pollers only when the whole thread group exits. 1800b53b0b9dSJoel Fernandes (Google) * If the thread group leader exits before all other threads in the 1801b53b0b9dSJoel Fernandes (Google) * group, then poll(2) should block, similar to the wait(2) family. 1802b53b0b9dSJoel Fernandes (Google) */ 1803b53b0b9dSJoel Fernandes (Google) if (!task || (task->exit_state && thread_group_empty(task))) 18049e77716aSLuc Van Oostenryck poll_flags = EPOLLIN | EPOLLRDNORM; 1805b53b0b9dSJoel Fernandes (Google) rcu_read_unlock(); 1806b53b0b9dSJoel Fernandes (Google) 1807b53b0b9dSJoel Fernandes (Google) return poll_flags; 1808b53b0b9dSJoel Fernandes (Google) } 1809b53b0b9dSJoel Fernandes (Google) 1810b3e58382SChristian Brauner const struct file_operations pidfd_fops = { 1811b3e58382SChristian Brauner .release = pidfd_release, 1812b53b0b9dSJoel Fernandes (Google) .poll = pidfd_poll, 1813b3e58382SChristian Brauner #ifdef CONFIG_PROC_FS 1814b3e58382SChristian Brauner .show_fdinfo = pidfd_show_fdinfo, 1815b3e58382SChristian Brauner #endif 1816b3e58382SChristian Brauner }; 1817b3e58382SChristian Brauner 1818c3f3ce04SAndrea Arcangeli static void __delayed_free_task(struct rcu_head *rhp) 1819c3f3ce04SAndrea Arcangeli { 1820c3f3ce04SAndrea Arcangeli struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); 1821c3f3ce04SAndrea Arcangeli 1822c3f3ce04SAndrea Arcangeli free_task(tsk); 1823c3f3ce04SAndrea Arcangeli } 1824c3f3ce04SAndrea Arcangeli 1825c3f3ce04SAndrea Arcangeli static __always_inline void delayed_free_task(struct task_struct *tsk) 1826c3f3ce04SAndrea Arcangeli { 1827c3f3ce04SAndrea Arcangeli if (IS_ENABLED(CONFIG_MEMCG)) 1828c3f3ce04SAndrea Arcangeli call_rcu(&tsk->rcu, __delayed_free_task); 1829c3f3ce04SAndrea Arcangeli else 1830c3f3ce04SAndrea Arcangeli free_task(tsk); 1831c3f3ce04SAndrea Arcangeli } 1832c3f3ce04SAndrea Arcangeli 1833f06febc9SFrank Mayhar /* 18341da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 18351da177e4SLinus Torvalds * but does not actually start it yet. 18361da177e4SLinus Torvalds * 18371da177e4SLinus Torvalds * It copies the registers, and all the appropriate 18381da177e4SLinus Torvalds * parts of the process environment (as per the clone 18391da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 18401da177e4SLinus Torvalds */ 18410766f788SEmese Revfy static __latent_entropy struct task_struct *copy_process( 184209a05394SRoland McGrath struct pid *pid, 18433033f14aSJosh Triplett int trace, 18447f192e3cSChristian Brauner int node, 18457f192e3cSChristian Brauner struct kernel_clone_args *args) 18461da177e4SLinus Torvalds { 1847b3e58382SChristian Brauner int pidfd = -1, retval; 1848a24efe62SMariusz Kozlowski struct task_struct *p; 1849c3ad2c3bSEric W. Biederman struct multiprocess_signals delayed; 18506fd2fe49SAl Viro struct file *pidfile = NULL; 18517f192e3cSChristian Brauner u64 clone_flags = args->flags; 1852769071acSAndrei Vagin struct nsproxy *nsp = current->nsproxy; 18531da177e4SLinus Torvalds 1854667b6094SMarcos Paulo de Souza /* 1855667b6094SMarcos Paulo de Souza * Don't allow sharing the root directory with processes in a different 1856667b6094SMarcos Paulo de Souza * namespace 1857667b6094SMarcos Paulo de Souza */ 18581da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 18591da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 18601da177e4SLinus Torvalds 1861e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1862e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1863e66eded8SEric W. Biederman 18641da177e4SLinus Torvalds /* 18651da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 18661da177e4SLinus Torvalds * can only be started up within the thread group. 18671da177e4SLinus Torvalds */ 18681da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 18691da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds /* 18721da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 18731da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 18741da177e4SLinus Torvalds * for various simplifications in other code. 18751da177e4SLinus Torvalds */ 18761da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 18771da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 18781da177e4SLinus Torvalds 1879123be07bSSukadev Bhattiprolu /* 1880123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1881123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1882123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1883123be07bSSukadev Bhattiprolu * from creating siblings. 1884123be07bSSukadev Bhattiprolu */ 1885123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1886123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1887123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1888123be07bSSukadev Bhattiprolu 18898382fcacSEric W. Biederman /* 189040a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 1891faf00da5SEric W. Biederman * do not allow it to share a thread group with the forking task. 18928382fcacSEric W. Biederman */ 1893faf00da5SEric W. Biederman if (clone_flags & CLONE_THREAD) { 189440a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 1895769071acSAndrei Vagin (task_active_pid_ns(current) != nsp->pid_ns_for_children)) 1896769071acSAndrei Vagin return ERR_PTR(-EINVAL); 1897769071acSAndrei Vagin } 1898769071acSAndrei Vagin 1899769071acSAndrei Vagin /* 1900769071acSAndrei Vagin * If the new process will be in a different time namespace 1901769071acSAndrei Vagin * do not allow it to share VM or a thread group with the forking task. 1902769071acSAndrei Vagin */ 1903769071acSAndrei Vagin if (clone_flags & (CLONE_THREAD | CLONE_VM)) { 1904769071acSAndrei Vagin if (nsp->time_ns != nsp->time_ns_for_children) 19058382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 190640a0d32dSOleg Nesterov } 19078382fcacSEric W. Biederman 1908b3e58382SChristian Brauner if (clone_flags & CLONE_PIDFD) { 1909b3e58382SChristian Brauner /* 1910b3e58382SChristian Brauner * - CLONE_DETACHED is blocked so that we can potentially 1911b3e58382SChristian Brauner * reuse it later for CLONE_PIDFD. 1912b3e58382SChristian Brauner * - CLONE_THREAD is blocked until someone really needs it. 1913b3e58382SChristian Brauner */ 19147f192e3cSChristian Brauner if (clone_flags & (CLONE_DETACHED | CLONE_THREAD)) 1915b3e58382SChristian Brauner return ERR_PTR(-EINVAL); 1916b3e58382SChristian Brauner } 1917b3e58382SChristian Brauner 1918c3ad2c3bSEric W. Biederman /* 1919c3ad2c3bSEric W. Biederman * Force any signals received before this point to be delivered 1920c3ad2c3bSEric W. Biederman * before the fork happens. Collect up signals sent to multiple 1921c3ad2c3bSEric W. Biederman * processes that happen during the fork and delay them so that 1922c3ad2c3bSEric W. Biederman * they appear to happen after the fork. 1923c3ad2c3bSEric W. Biederman */ 1924c3ad2c3bSEric W. Biederman sigemptyset(&delayed.signal); 1925c3ad2c3bSEric W. Biederman INIT_HLIST_NODE(&delayed.node); 1926c3ad2c3bSEric W. Biederman 1927c3ad2c3bSEric W. Biederman spin_lock_irq(¤t->sighand->siglock); 1928c3ad2c3bSEric W. Biederman if (!(clone_flags & CLONE_THREAD)) 1929c3ad2c3bSEric W. Biederman hlist_add_head(&delayed.node, ¤t->signal->multiprocess); 1930c3ad2c3bSEric W. Biederman recalc_sigpending(); 1931c3ad2c3bSEric W. Biederman spin_unlock_irq(¤t->sighand->siglock); 1932c3ad2c3bSEric W. Biederman retval = -ERESTARTNOINTR; 1933c3ad2c3bSEric W. Biederman if (signal_pending(current)) 1934c3ad2c3bSEric W. Biederman goto fork_out; 1935c3ad2c3bSEric W. Biederman 19361da177e4SLinus Torvalds retval = -ENOMEM; 1937725fc629SAndi Kleen p = dup_task_struct(current, node); 19381da177e4SLinus Torvalds if (!p) 19391da177e4SLinus Torvalds goto fork_out; 19401da177e4SLinus Torvalds 19414d6501dcSVegard Nossum /* 19424d6501dcSVegard Nossum * This _must_ happen before we call free_task(), i.e. before we jump 19434d6501dcSVegard Nossum * to any of the bad_fork_* labels. This is to avoid freeing 19444d6501dcSVegard Nossum * p->set_child_tid which is (ab)used as a kthread's data pointer for 19454d6501dcSVegard Nossum * kernel threads (PF_KTHREAD). 19464d6501dcSVegard Nossum */ 19477f192e3cSChristian Brauner p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL; 19484d6501dcSVegard Nossum /* 19494d6501dcSVegard Nossum * Clear TID on mm_release()? 19504d6501dcSVegard Nossum */ 19517f192e3cSChristian Brauner p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL; 19524d6501dcSVegard Nossum 1953f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1954f7e8b616SSteven Rostedt 1955bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1956bea493a0SPeter Zijlstra 1957a21ee605SPeter Zijlstra lockdep_assert_irqs_enabled(); 1958d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1959de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1960de30a2b3SIngo Molnar #endif 19611da177e4SLinus Torvalds retval = -EAGAIN; 19623b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 196378d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1964b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1965b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 19661da177e4SLinus Torvalds goto bad_fork_free; 19671da177e4SLinus Torvalds } 196872fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 19691da177e4SLinus Torvalds 1970f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1971f1752eecSDavid Howells if (retval < 0) 1972f1752eecSDavid Howells goto bad_fork_free; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds /* 19751da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 19761da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 19771da177e4SLinus Torvalds * to stop root fork bombs. 19781da177e4SLinus Torvalds */ 197904ec93feSLi Zefan retval = -EAGAIN; 1980c17d1a3aSWeilong Chen if (data_race(nr_threads >= max_threads)) 19811da177e4SLinus Torvalds goto bad_fork_cleanup_count; 19821da177e4SLinus Torvalds 1983ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1984c1de45caSPeter Zijlstra p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE); 1985514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 19861da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 19871da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1988f41d911fSPaul E. McKenney rcu_copy_process(p); 19891da177e4SLinus Torvalds p->vfork_done = NULL; 19901da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds init_sigpending(&p->pending); 19931da177e4SLinus Torvalds 199464861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 199540565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 199664861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 199740565b5aSStanislaw Gruszka #endif 19989d7fb042SPeter Zijlstra prev_cputime_init(&p->prev_cputime); 19999d7fb042SPeter Zijlstra 20006a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 2001bac5b6b6SFrederic Weisbecker seqcount_init(&p->vtime.seqcount); 2002bac5b6b6SFrederic Weisbecker p->vtime.starttime = 0; 2003bac5b6b6SFrederic Weisbecker p->vtime.state = VTIME_INACTIVE; 20046a61671bSFrederic Weisbecker #endif 20056a61671bSFrederic Weisbecker 2006a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 2007a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 2008a3a2e76cSKAMEZAWA Hiroyuki #endif 2009172ba844SBalbir Singh 20106976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 20116976675dSArjan van de Ven 2012eb414681SJohannes Weiner #ifdef CONFIG_PSI 2013eb414681SJohannes Weiner p->psi_flags = 0; 2014eb414681SJohannes Weiner #endif 2015eb414681SJohannes Weiner 20165995477aSAndrea Righi task_io_accounting_init(&p->ioac); 20171da177e4SLinus Torvalds acct_clear_integrals(p); 20181da177e4SLinus Torvalds 20193a245c0fSThomas Gleixner posix_cputimers_init(&p->posix_cputimers); 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds p->io_context = NULL; 2022c0b0ae8aSRichard Guy Briggs audit_set_context(p, NULL); 2023b4f48b63SPaul Menage cgroup_fork(p); 20241da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2025846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 20261da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 20271da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 20281da177e4SLinus Torvalds p->mempolicy = NULL; 2029e8604cb4SLi Zefan goto bad_fork_cleanup_threadgroup_lock; 20301da177e4SLinus Torvalds } 20311da177e4SLinus Torvalds #endif 2032778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 2033778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 2034778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 2035*b7505861SAhmed S. Darwish seqcount_spinlock_init(&p->mems_allowed_seq, &p->alloc_lock); 2036778d3b0fSMichal Hocko #endif 2037de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 2038de30a2b3SIngo Molnar p->irq_events = 0; 2039de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 2040de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 2041de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 2042de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 2043de30a2b3SIngo Molnar p->softirqs_enabled = 1; 2044de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 2045de30a2b3SIngo Molnar p->softirq_enable_event = 0; 2046de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 2047de30a2b3SIngo Molnar p->softirq_disable_event = 0; 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; 21057f192e3cSChristian Brauner retval = copy_thread_tls(clone_flags, args->stack, args->stack_size, p, 21067f192e3cSChristian Brauner args->tls); 21071da177e4SLinus Torvalds if (retval) 2108fd0928dfSJens Axboe goto bad_fork_cleanup_io; 21091da177e4SLinus Torvalds 2110afaef01cSAlexander Popov stackleak_task_init(p); 2111afaef01cSAlexander Popov 2112425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 211349cb2fc4SAdrian Reber pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid, 211449cb2fc4SAdrian Reber args->set_tid_size); 211535f71bc0SMichal Hocko if (IS_ERR(pid)) { 211635f71bc0SMichal Hocko retval = PTR_ERR(pid); 21170740aa5fSJiri Slaby goto bad_fork_cleanup_thread; 2118425fb2b4SPavel Emelyanov } 211935f71bc0SMichal Hocko } 2120425fb2b4SPavel Emelyanov 2121b3e58382SChristian Brauner /* 2122b3e58382SChristian Brauner * This has to happen after we've potentially unshared the file 2123b3e58382SChristian Brauner * descriptor table (so that the pidfd doesn't leak into the child 2124b3e58382SChristian Brauner * if the fd table isn't shared). 2125b3e58382SChristian Brauner */ 2126b3e58382SChristian Brauner if (clone_flags & CLONE_PIDFD) { 21276fd2fe49SAl Viro retval = get_unused_fd_flags(O_RDWR | O_CLOEXEC); 2128b3e58382SChristian Brauner if (retval < 0) 2129b3e58382SChristian Brauner goto bad_fork_free_pid; 2130b3e58382SChristian Brauner 2131b3e58382SChristian Brauner pidfd = retval; 21326fd2fe49SAl Viro 21336fd2fe49SAl Viro pidfile = anon_inode_getfile("[pidfd]", &pidfd_fops, pid, 21346fd2fe49SAl Viro O_RDWR | O_CLOEXEC); 21356fd2fe49SAl Viro if (IS_ERR(pidfile)) { 21366fd2fe49SAl Viro put_unused_fd(pidfd); 213728dd29c0SChristian Brauner retval = PTR_ERR(pidfile); 21386fd2fe49SAl Viro goto bad_fork_free_pid; 21396fd2fe49SAl Viro } 21406fd2fe49SAl Viro get_pid(pid); /* held by pidfile now */ 21416fd2fe49SAl Viro 21427f192e3cSChristian Brauner retval = put_user(pidfd, args->pidfd); 2143b3e58382SChristian Brauner if (retval) 2144b3e58382SChristian Brauner goto bad_fork_put_pidfd; 2145b3e58382SChristian Brauner } 2146b3e58382SChristian Brauner 214773c10101SJens Axboe #ifdef CONFIG_BLOCK 214873c10101SJens Axboe p->plug = NULL; 214973c10101SJens Axboe #endif 2150ba31c1a4SThomas Gleixner futex_init_task(p); 2151ba31c1a4SThomas Gleixner 21521da177e4SLinus Torvalds /* 2153f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 2154f9a3879aSGOTO Masanori */ 2155f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 21562a742138SStas Sergeev sas_ss_reset(p); 2157f9a3879aSGOTO Masanori 2158f9a3879aSGOTO Masanori /* 21596580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 21606580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 21611da177e4SLinus Torvalds */ 21626580807dSOleg Nesterov user_disable_single_step(p); 21631da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 2164ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 2165ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 2166ed75e8d5SLaurent Vivier #endif 2167e02c9b0dSLin Feng clear_tsk_latency_tracing(p); 21681da177e4SLinus Torvalds 21691da177e4SLinus Torvalds /* ok, now we should be set up.. */ 217018c830dfSOleg Nesterov p->pid = pid_nr(pid); 217118c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 21725f8aadd8SOleg Nesterov p->exit_signal = -1; 217318c830dfSOleg Nesterov p->group_leader = current->group_leader; 217418c830dfSOleg Nesterov p->tgid = current->tgid; 217518c830dfSOleg Nesterov } else { 217618c830dfSOleg Nesterov if (clone_flags & CLONE_PARENT) 21775f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 21785f8aadd8SOleg Nesterov else 21797f192e3cSChristian Brauner p->exit_signal = args->exit_signal; 218018c830dfSOleg Nesterov p->group_leader = p; 218118c830dfSOleg Nesterov p->tgid = p->pid; 218218c830dfSOleg Nesterov } 21835f8aadd8SOleg Nesterov 21849d823e8fSWu Fengguang p->nr_dirtied = 0; 21859d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 218683712358SWu Fengguang p->dirty_paused_when = 0; 21879d823e8fSWu Fengguang 2188bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 218947e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 2190158e1645SAl Viro p->task_works = NULL; 21911da177e4SLinus Torvalds 219218c830dfSOleg Nesterov /* 21937e47682eSAleksa Sarai * Ensure that the cgroup subsystem policies allow the new process to be 21947e47682eSAleksa Sarai * forked. It should be noted the the new process's css_set can be changed 21957e47682eSAleksa Sarai * between here and cgroup_post_fork() if an organisation operation is in 21967e47682eSAleksa Sarai * progress. 21977e47682eSAleksa Sarai */ 2198ef2c41cfSChristian Brauner retval = cgroup_can_fork(p, args); 21997e47682eSAleksa Sarai if (retval) 22005a5cf5cbSChristian Brauner goto bad_fork_put_pidfd; 22017e47682eSAleksa Sarai 22027e47682eSAleksa Sarai /* 22037b558513SDavid Herrmann * From this point on we must avoid any synchronous user-space 22047b558513SDavid Herrmann * communication until we take the tasklist-lock. In particular, we do 22057b558513SDavid Herrmann * not want user-space to be able to predict the process start-time by 22067b558513SDavid Herrmann * stalling fork(2) after we recorded the start_time but before it is 22077b558513SDavid Herrmann * visible to the system. 22087b558513SDavid Herrmann */ 22097b558513SDavid Herrmann 22107b558513SDavid Herrmann p->start_time = ktime_get_ns(); 2211cf25e24dSPeter Zijlstra p->start_boottime = ktime_get_boottime_ns(); 22127b558513SDavid Herrmann 22137b558513SDavid Herrmann /* 221418c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 221518c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 221618c830dfSOleg Nesterov */ 22171da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 22202d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 22211da177e4SLinus Torvalds p->real_parent = current->real_parent; 22222d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 22232d5516cbSOleg Nesterov } else { 22241da177e4SLinus Torvalds p->real_parent = current; 22252d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 22262d5516cbSOleg Nesterov } 22271da177e4SLinus Torvalds 2228d83a7cb3SJosh Poimboeuf klp_copy_process(p); 2229d83a7cb3SJosh Poimboeuf 22301da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 22314a2c7a78SOleg Nesterov 22324a2c7a78SOleg Nesterov /* 2233dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 2234dbd95212SKees Cook * before holding sighand lock. 2235dbd95212SKees Cook */ 2236dbd95212SKees Cook copy_seccomp(p); 2237dbd95212SKees Cook 2238d7822b1eSMathieu Desnoyers rseq_fork(p, clone_flags); 2239d7822b1eSMathieu Desnoyers 22404ca1d3eeSEric W. Biederman /* Don't start children in a dying pid namespace */ 2241e8cfbc24SGargi Sharma if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { 22423fd37226SKirill Tkhai retval = -ENOMEM; 22433fd37226SKirill Tkhai goto bad_fork_cancel_cgroup; 22443fd37226SKirill Tkhai } 22454a2c7a78SOleg Nesterov 22467673bf55SEric W. Biederman /* Let kill terminate clone/fork in the middle */ 22477673bf55SEric W. Biederman if (fatal_signal_pending(current)) { 22487673bf55SEric W. Biederman retval = -EINTR; 22497673bf55SEric W. Biederman goto bad_fork_cancel_cgroup; 22507673bf55SEric W. Biederman } 22517673bf55SEric W. Biederman 22526fd2fe49SAl Viro /* past the last point of failure */ 22536fd2fe49SAl Viro if (pidfile) 22546fd2fe49SAl Viro fd_install(pidfd, pidfile); 22551da177e4SLinus Torvalds 22562c470475SEric W. Biederman init_task_pid_links(p); 225773b9ebfeSOleg Nesterov if (likely(p->pid)) { 22584b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 22591da177e4SLinus Torvalds 226081907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 22611da177e4SLinus Torvalds if (thread_group_leader(p)) { 22626883f81aSEric W. Biederman init_task_pid(p, PIDTYPE_TGID, pid); 226381907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 226481907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 226581907739SOleg Nesterov 22661c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 226717cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 22681c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 22691c4042c2SEric W. Biederman } 2270c3ad2c3bSEric W. Biederman p->signal->shared_pending.signal = delayed.signal; 22719c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 2272749860ceSPavel Tikhomirov /* 2273749860ceSPavel Tikhomirov * Inherit has_child_subreaper flag under the same 2274749860ceSPavel Tikhomirov * tasklist_lock with adding child to the process tree 2275749860ceSPavel Tikhomirov * for propagate_has_child_subreaper optimization. 2276749860ceSPavel Tikhomirov */ 2277749860ceSPavel Tikhomirov p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 2278749860ceSPavel Tikhomirov p->real_parent->signal->is_child_subreaper; 22799cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 22805e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 22816883f81aSEric W. Biederman attach_pid(p, PIDTYPE_TGID); 228281907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 228381907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 2284909ea964SChristoph Lameter __this_cpu_inc(process_counts); 228580628ca0SOleg Nesterov } else { 228680628ca0SOleg Nesterov current->signal->nr_threads++; 228780628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 228860d4de3fSElena Reshetova refcount_inc(¤t->signal->sigcnt); 2289924de3b8SEric W. Biederman task_join_group_stop(p); 229080628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 229180628ca0SOleg Nesterov &p->group_leader->thread_group); 22920c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 22930c740d0aSOleg Nesterov &p->signal->thread_head); 22941da177e4SLinus Torvalds } 229581907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 22961da177e4SLinus Torvalds nr_threads++; 229773b9ebfeSOleg Nesterov } 22981da177e4SLinus Torvalds total_forks++; 2299c3ad2c3bSEric W. Biederman hlist_del_init(&delayed.node); 23003f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 23014af4206bSOleg Nesterov syscall_tracepoint_update(p); 23021da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 23034af4206bSOleg Nesterov 2304c13cf856SAndrew Morton proc_fork_connector(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 23111da177e4SLinus Torvalds return p; 23121da177e4SLinus Torvalds 23137e47682eSAleksa Sarai bad_fork_cancel_cgroup: 23143fd37226SKirill Tkhai spin_unlock(¤t->sighand->siglock); 23153fd37226SKirill Tkhai write_unlock_irq(&tasklist_lock); 2316ef2c41cfSChristian Brauner cgroup_cancel_fork(p, args); 2317b3e58382SChristian Brauner bad_fork_put_pidfd: 23186fd2fe49SAl Viro if (clone_flags & CLONE_PIDFD) { 23196fd2fe49SAl Viro fput(pidfile); 23206fd2fe49SAl Viro put_unused_fd(pidfd); 23216fd2fe49SAl Viro } 2322425fb2b4SPavel Emelyanov bad_fork_free_pid: 2323425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 2324425fb2b4SPavel Emelyanov free_pid(pid); 23250740aa5fSJiri Slaby bad_fork_cleanup_thread: 23260740aa5fSJiri Slaby exit_thread(p); 2327fd0928dfSJens Axboe bad_fork_cleanup_io: 2328b69f2292SLouis Rilling if (p->io_context) 2329b69f2292SLouis Rilling exit_io_context(p); 2330ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 2331444f378bSLinus Torvalds exit_task_namespaces(p); 23321da177e4SLinus Torvalds bad_fork_cleanup_mm: 2333c3f3ce04SAndrea Arcangeli if (p->mm) { 2334c3f3ce04SAndrea Arcangeli mm_clear_owner(p->mm, p); 23351da177e4SLinus Torvalds mmput(p->mm); 2336c3f3ce04SAndrea Arcangeli } 23371da177e4SLinus Torvalds bad_fork_cleanup_signal: 23384ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 23391c5354deSMike Galbraith free_signal_struct(p->signal); 23401da177e4SLinus Torvalds bad_fork_cleanup_sighand: 2341a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 23421da177e4SLinus Torvalds bad_fork_cleanup_fs: 23431da177e4SLinus Torvalds exit_fs(p); /* blocking */ 23441da177e4SLinus Torvalds bad_fork_cleanup_files: 23451da177e4SLinus Torvalds exit_files(p); /* blocking */ 23461da177e4SLinus Torvalds bad_fork_cleanup_semundo: 23471da177e4SLinus Torvalds exit_sem(p); 2348e4e55b47STetsuo Handa bad_fork_cleanup_security: 2349e4e55b47STetsuo Handa security_task_free(p); 23501da177e4SLinus Torvalds bad_fork_cleanup_audit: 23511da177e4SLinus Torvalds audit_free(p); 23526c72e350SPeter Zijlstra bad_fork_cleanup_perf: 2353cdd6c482SIngo Molnar perf_event_free_task(p); 23546c72e350SPeter Zijlstra bad_fork_cleanup_policy: 2355b09be676SByungchul Park lockdep_free_task(p); 23561da177e4SLinus Torvalds #ifdef CONFIG_NUMA 2357f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 2358e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock: 23591da177e4SLinus Torvalds #endif 236035df17c5SShailabh Nagar delayacct_tsk_free(p); 23611da177e4SLinus Torvalds bad_fork_cleanup_count: 2362d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 2363e0e81739SDavid Howells exit_creds(p); 23641da177e4SLinus Torvalds bad_fork_free: 2365405c0759SAndy Lutomirski p->state = TASK_DEAD; 236668f24b08SAndy Lutomirski put_task_stack(p); 2367c3f3ce04SAndrea Arcangeli delayed_free_task(p); 2368fe7d37d1SOleg Nesterov fork_out: 2369c3ad2c3bSEric W. Biederman spin_lock_irq(¤t->sighand->siglock); 2370c3ad2c3bSEric W. Biederman hlist_del_init(&delayed.node); 2371c3ad2c3bSEric W. Biederman spin_unlock_irq(¤t->sighand->siglock); 2372fe7d37d1SOleg Nesterov return ERR_PTR(retval); 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds 23752c470475SEric W. Biederman static inline void init_idle_pids(struct task_struct *idle) 2376f106eee1SOleg Nesterov { 2377f106eee1SOleg Nesterov enum pid_type type; 2378f106eee1SOleg Nesterov 2379f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 23802c470475SEric W. Biederman INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */ 23812c470475SEric W. Biederman init_task_pid(idle, type, &init_struct_pid); 2382f106eee1SOleg Nesterov } 2383f106eee1SOleg Nesterov } 2384f106eee1SOleg Nesterov 23850db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 23861da177e4SLinus Torvalds { 238736c8b586SIngo Molnar struct task_struct *task; 23887f192e3cSChristian Brauner struct kernel_clone_args args = { 23897f192e3cSChristian Brauner .flags = CLONE_VM, 23907f192e3cSChristian Brauner }; 23917f192e3cSChristian Brauner 23927f192e3cSChristian Brauner task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args); 2393f106eee1SOleg Nesterov if (!IS_ERR(task)) { 23942c470475SEric W. Biederman init_idle_pids(task); 23951da177e4SLinus Torvalds init_idle(task, cpu); 2396f106eee1SOleg Nesterov } 239773b9ebfeSOleg Nesterov 23981da177e4SLinus Torvalds return task; 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds 240113585fa0SNadav Amit struct mm_struct *copy_init_mm(void) 240213585fa0SNadav Amit { 240313585fa0SNadav Amit return dup_mm(NULL, &init_mm); 240413585fa0SNadav Amit } 240513585fa0SNadav Amit 24061da177e4SLinus Torvalds /* 24071da177e4SLinus Torvalds * Ok, this is the main fork-routine. 24081da177e4SLinus Torvalds * 24091da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 24101da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 2411a0eb9abdSEugene Syromiatnikov * 2412a0eb9abdSEugene Syromiatnikov * args->exit_signal is expected to be checked for sanity by the caller. 24131da177e4SLinus Torvalds */ 24147f192e3cSChristian Brauner long _do_fork(struct kernel_clone_args *args) 24151da177e4SLinus Torvalds { 24167f192e3cSChristian Brauner u64 clone_flags = args->flags; 24179f5325aaSMarcos Paulo de Souza struct completion vfork; 24189f5325aaSMarcos Paulo de Souza struct pid *pid; 24191da177e4SLinus Torvalds struct task_struct *p; 24201da177e4SLinus Torvalds int trace = 0; 242192476d7fSEric W. Biederman long nr; 24221da177e4SLinus Torvalds 2423bdff746aSAndrew Morton /* 24244b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 24254b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 24264b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 24274b9d33e6STejun Heo * for the type of forking is enabled. 242809a05394SRoland McGrath */ 2429e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 24304b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 24314b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 24327f192e3cSChristian Brauner else if (args->exit_signal != SIGCHLD) 24334b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 24344b9d33e6STejun Heo else 24354b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 24364b9d33e6STejun Heo 24374b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 24384b9d33e6STejun Heo trace = 0; 24394b9d33e6STejun Heo } 24401da177e4SLinus Torvalds 24417f192e3cSChristian Brauner p = copy_process(NULL, trace, NUMA_NO_NODE, args); 244238addce8SEmese Revfy add_latent_entropy(); 24439f5325aaSMarcos Paulo de Souza 24449f5325aaSMarcos Paulo de Souza if (IS_ERR(p)) 24459f5325aaSMarcos Paulo de Souza return PTR_ERR(p); 24469f5325aaSMarcos Paulo de Souza 24471da177e4SLinus Torvalds /* 24481da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 24491da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 24501da177e4SLinus Torvalds */ 24510a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 24520a16b607SMathieu Desnoyers 24534e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 24544e52365fSMatthew Dempsky nr = pid_vnr(pid); 245530e49c26SPavel Emelyanov 245630e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 24577f192e3cSChristian Brauner put_user(nr, args->parent_tid); 2458a6f5e063SSukadev Bhattiprolu 24591da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 24601da177e4SLinus Torvalds p->vfork_done = &vfork; 24611da177e4SLinus Torvalds init_completion(&vfork); 2462d68b46feSOleg Nesterov get_task_struct(p); 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24653e51e3edSSamir Bellabes wake_up_new_task(p); 24661da177e4SLinus Torvalds 24674b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 24684b9d33e6STejun Heo if (unlikely(trace)) 24694e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 247009a05394SRoland McGrath 24711da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 2472d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 24734e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 24749f59ce5dSChuck Ebbert } 24754e52365fSMatthew Dempsky 24764e52365fSMatthew Dempsky put_pid(pid); 247792476d7fSEric W. Biederman return nr; 24781da177e4SLinus Torvalds } 24791da177e4SLinus Torvalds 2480028b6e8aSDmitry V. Levin bool legacy_clone_args_valid(const struct kernel_clone_args *kargs) 2481028b6e8aSDmitry V. Levin { 2482028b6e8aSDmitry V. Levin /* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */ 2483028b6e8aSDmitry V. Levin if ((kargs->flags & CLONE_PIDFD) && 2484028b6e8aSDmitry V. Levin (kargs->flags & CLONE_PARENT_SETTID)) 2485028b6e8aSDmitry V. Levin return false; 2486028b6e8aSDmitry V. Levin 2487028b6e8aSDmitry V. Levin return true; 2488028b6e8aSDmitry V. Levin } 2489028b6e8aSDmitry V. Levin 24903033f14aSJosh Triplett #ifndef CONFIG_HAVE_COPY_THREAD_TLS 24913033f14aSJosh Triplett /* For compatibility with architectures that call do_fork directly rather than 24923033f14aSJosh Triplett * using the syscall entry points below. */ 24933033f14aSJosh Triplett long do_fork(unsigned long clone_flags, 24943033f14aSJosh Triplett unsigned long stack_start, 24953033f14aSJosh Triplett unsigned long stack_size, 24963033f14aSJosh Triplett int __user *parent_tidptr, 24973033f14aSJosh Triplett int __user *child_tidptr) 24983033f14aSJosh Triplett { 24997f192e3cSChristian Brauner struct kernel_clone_args args = { 25003f2c788aSChristian Brauner .flags = (lower_32_bits(clone_flags) & ~CSIGNAL), 2501028b6e8aSDmitry V. Levin .pidfd = parent_tidptr, 25027f192e3cSChristian Brauner .child_tid = child_tidptr, 25037f192e3cSChristian Brauner .parent_tid = parent_tidptr, 25043f2c788aSChristian Brauner .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL), 25057f192e3cSChristian Brauner .stack = stack_start, 25067f192e3cSChristian Brauner .stack_size = stack_size, 25077f192e3cSChristian Brauner }; 25087f192e3cSChristian Brauner 2509028b6e8aSDmitry V. Levin if (!legacy_clone_args_valid(&args)) 2510028b6e8aSDmitry V. Levin return -EINVAL; 2511028b6e8aSDmitry V. Levin 25127f192e3cSChristian Brauner return _do_fork(&args); 25133033f14aSJosh Triplett } 25143033f14aSJosh Triplett #endif 25153033f14aSJosh Triplett 25162aa3a7f8SAl Viro /* 25172aa3a7f8SAl Viro * Create a kernel thread. 25182aa3a7f8SAl Viro */ 25192aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 25202aa3a7f8SAl Viro { 25217f192e3cSChristian Brauner struct kernel_clone_args args = { 25223f2c788aSChristian Brauner .flags = ((lower_32_bits(flags) | CLONE_VM | 25233f2c788aSChristian Brauner CLONE_UNTRACED) & ~CSIGNAL), 25243f2c788aSChristian Brauner .exit_signal = (lower_32_bits(flags) & CSIGNAL), 25257f192e3cSChristian Brauner .stack = (unsigned long)fn, 25267f192e3cSChristian Brauner .stack_size = (unsigned long)arg, 25277f192e3cSChristian Brauner }; 25287f192e3cSChristian Brauner 25297f192e3cSChristian Brauner return _do_fork(&args); 25302aa3a7f8SAl Viro } 25312aa3a7f8SAl Viro 2532d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 2533d2125043SAl Viro SYSCALL_DEFINE0(fork) 2534d2125043SAl Viro { 2535d2125043SAl Viro #ifdef CONFIG_MMU 25367f192e3cSChristian Brauner struct kernel_clone_args args = { 25377f192e3cSChristian Brauner .exit_signal = SIGCHLD, 25387f192e3cSChristian Brauner }; 25397f192e3cSChristian Brauner 25407f192e3cSChristian Brauner return _do_fork(&args); 2541d2125043SAl Viro #else 2542d2125043SAl Viro /* can not support in nommu mode */ 25435d59e182SDaeseok Youn return -EINVAL; 2544d2125043SAl Viro #endif 2545d2125043SAl Viro } 2546d2125043SAl Viro #endif 2547d2125043SAl Viro 2548d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 2549d2125043SAl Viro SYSCALL_DEFINE0(vfork) 2550d2125043SAl Viro { 25517f192e3cSChristian Brauner struct kernel_clone_args args = { 25527f192e3cSChristian Brauner .flags = CLONE_VFORK | CLONE_VM, 25537f192e3cSChristian Brauner .exit_signal = SIGCHLD, 25547f192e3cSChristian Brauner }; 25557f192e3cSChristian Brauner 25567f192e3cSChristian Brauner return _do_fork(&args); 2557d2125043SAl Viro } 2558d2125043SAl Viro #endif 2559d2125043SAl Viro 2560d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 2561d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 2562d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2563d2125043SAl Viro int __user *, parent_tidptr, 25643033f14aSJosh Triplett unsigned long, tls, 2565d2125043SAl Viro int __user *, child_tidptr) 2566d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 2567d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 2568d2125043SAl Viro int __user *, parent_tidptr, 2569d2125043SAl Viro int __user *, child_tidptr, 25703033f14aSJosh Triplett unsigned long, tls) 2571dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 2572dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 2573dfa9771aSMichal Simek int, stack_size, 2574dfa9771aSMichal Simek int __user *, parent_tidptr, 2575dfa9771aSMichal Simek int __user *, child_tidptr, 25763033f14aSJosh Triplett unsigned long, tls) 2577d2125043SAl Viro #else 2578d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2579d2125043SAl Viro int __user *, parent_tidptr, 2580d2125043SAl Viro int __user *, child_tidptr, 25813033f14aSJosh Triplett unsigned long, tls) 2582d2125043SAl Viro #endif 2583d2125043SAl Viro { 25847f192e3cSChristian Brauner struct kernel_clone_args args = { 25853f2c788aSChristian Brauner .flags = (lower_32_bits(clone_flags) & ~CSIGNAL), 25867f192e3cSChristian Brauner .pidfd = parent_tidptr, 25877f192e3cSChristian Brauner .child_tid = child_tidptr, 25887f192e3cSChristian Brauner .parent_tid = parent_tidptr, 25893f2c788aSChristian Brauner .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL), 25907f192e3cSChristian Brauner .stack = newsp, 25917f192e3cSChristian Brauner .tls = tls, 25927f192e3cSChristian Brauner }; 25937f192e3cSChristian Brauner 2594028b6e8aSDmitry V. Levin if (!legacy_clone_args_valid(&args)) 25957f192e3cSChristian Brauner return -EINVAL; 25967f192e3cSChristian Brauner 25977f192e3cSChristian Brauner return _do_fork(&args); 25987f192e3cSChristian Brauner } 2599d68dbb0cSChristian Brauner #endif 26007f192e3cSChristian Brauner 2601d68dbb0cSChristian Brauner #ifdef __ARCH_WANT_SYS_CLONE3 2602dd499f7aSAmanieu d'Antras 2603dd499f7aSAmanieu d'Antras /* 2604dd499f7aSAmanieu d'Antras * copy_thread implementations handle CLONE_SETTLS by reading the TLS value from 2605dd499f7aSAmanieu d'Antras * the registers containing the syscall arguments for clone. This doesn't work 2606dd499f7aSAmanieu d'Antras * with clone3 since the TLS value is passed in clone_args instead. 2607dd499f7aSAmanieu d'Antras */ 2608dd499f7aSAmanieu d'Antras #ifndef CONFIG_HAVE_COPY_THREAD_TLS 2609dd499f7aSAmanieu d'Antras #error clone3 requires copy_thread_tls support in arch 2610dd499f7aSAmanieu d'Antras #endif 2611dd499f7aSAmanieu d'Antras 26127f192e3cSChristian Brauner noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, 26137f192e3cSChristian Brauner struct clone_args __user *uargs, 2614f14c234bSAleksa Sarai size_t usize) 26157f192e3cSChristian Brauner { 2616f14c234bSAleksa Sarai int err; 26177f192e3cSChristian Brauner struct clone_args args; 261849cb2fc4SAdrian Reber pid_t *kset_tid = kargs->set_tid; 26197f192e3cSChristian Brauner 2620a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, tls) != 2621a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER0); 2622a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, set_tid_size) != 2623a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER1); 2624a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) != 2625a966dcfeSEugene Syromiatnikov CLONE_ARGS_SIZE_VER2); 2626a966dcfeSEugene Syromiatnikov BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2); 2627a966dcfeSEugene Syromiatnikov 2628f14c234bSAleksa Sarai if (unlikely(usize > PAGE_SIZE)) 26297f192e3cSChristian Brauner return -E2BIG; 2630f14c234bSAleksa Sarai if (unlikely(usize < CLONE_ARGS_SIZE_VER0)) 26317f192e3cSChristian Brauner return -EINVAL; 26327f192e3cSChristian Brauner 2633f14c234bSAleksa Sarai err = copy_struct_from_user(&args, sizeof(args), uargs, usize); 2634f14c234bSAleksa Sarai if (err) 2635f14c234bSAleksa Sarai return err; 26367f192e3cSChristian Brauner 263749cb2fc4SAdrian Reber if (unlikely(args.set_tid_size > MAX_PID_NS_LEVEL)) 263849cb2fc4SAdrian Reber return -EINVAL; 263949cb2fc4SAdrian Reber 264049cb2fc4SAdrian Reber if (unlikely(!args.set_tid && args.set_tid_size > 0)) 264149cb2fc4SAdrian Reber return -EINVAL; 264249cb2fc4SAdrian Reber 264349cb2fc4SAdrian Reber if (unlikely(args.set_tid && args.set_tid_size == 0)) 264449cb2fc4SAdrian Reber return -EINVAL; 264549cb2fc4SAdrian Reber 2646a0eb9abdSEugene Syromiatnikov /* 2647a0eb9abdSEugene Syromiatnikov * Verify that higher 32bits of exit_signal are unset and that 2648a0eb9abdSEugene Syromiatnikov * it is a valid signal 2649a0eb9abdSEugene Syromiatnikov */ 2650a0eb9abdSEugene Syromiatnikov if (unlikely((args.exit_signal & ~((u64)CSIGNAL)) || 2651a0eb9abdSEugene Syromiatnikov !valid_signal(args.exit_signal))) 2652a0eb9abdSEugene Syromiatnikov return -EINVAL; 2653a0eb9abdSEugene Syromiatnikov 265462173872SEugene Syromiatnikov if ((args.flags & CLONE_INTO_CGROUP) && 265562173872SEugene Syromiatnikov (args.cgroup > INT_MAX || usize < CLONE_ARGS_SIZE_VER2)) 2656ef2c41cfSChristian Brauner return -EINVAL; 2657ef2c41cfSChristian Brauner 26587f192e3cSChristian Brauner *kargs = (struct kernel_clone_args){ 26597f192e3cSChristian Brauner .flags = args.flags, 26607f192e3cSChristian Brauner .pidfd = u64_to_user_ptr(args.pidfd), 26617f192e3cSChristian Brauner .child_tid = u64_to_user_ptr(args.child_tid), 26627f192e3cSChristian Brauner .parent_tid = u64_to_user_ptr(args.parent_tid), 26637f192e3cSChristian Brauner .exit_signal = args.exit_signal, 26647f192e3cSChristian Brauner .stack = args.stack, 26657f192e3cSChristian Brauner .stack_size = args.stack_size, 26667f192e3cSChristian Brauner .tls = args.tls, 266749cb2fc4SAdrian Reber .set_tid_size = args.set_tid_size, 2668ef2c41cfSChristian Brauner .cgroup = args.cgroup, 26697f192e3cSChristian Brauner }; 26707f192e3cSChristian Brauner 267149cb2fc4SAdrian Reber if (args.set_tid && 267249cb2fc4SAdrian Reber copy_from_user(kset_tid, u64_to_user_ptr(args.set_tid), 267349cb2fc4SAdrian Reber (kargs->set_tid_size * sizeof(pid_t)))) 267449cb2fc4SAdrian Reber return -EFAULT; 267549cb2fc4SAdrian Reber 267649cb2fc4SAdrian Reber kargs->set_tid = kset_tid; 267749cb2fc4SAdrian Reber 26787f192e3cSChristian Brauner return 0; 26797f192e3cSChristian Brauner } 26807f192e3cSChristian Brauner 2681fa729c4dSChristian Brauner /** 2682fa729c4dSChristian Brauner * clone3_stack_valid - check and prepare stack 2683fa729c4dSChristian Brauner * @kargs: kernel clone args 2684fa729c4dSChristian Brauner * 2685fa729c4dSChristian Brauner * Verify that the stack arguments userspace gave us are sane. 2686fa729c4dSChristian Brauner * In addition, set the stack direction for userspace since it's easy for us to 2687fa729c4dSChristian Brauner * determine. 2688fa729c4dSChristian Brauner */ 2689fa729c4dSChristian Brauner static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) 2690fa729c4dSChristian Brauner { 2691fa729c4dSChristian Brauner if (kargs->stack == 0) { 2692fa729c4dSChristian Brauner if (kargs->stack_size > 0) 2693fa729c4dSChristian Brauner return false; 2694fa729c4dSChristian Brauner } else { 2695fa729c4dSChristian Brauner if (kargs->stack_size == 0) 2696fa729c4dSChristian Brauner return false; 2697fa729c4dSChristian Brauner 2698fa729c4dSChristian Brauner if (!access_ok((void __user *)kargs->stack, kargs->stack_size)) 2699fa729c4dSChristian Brauner return false; 2700fa729c4dSChristian Brauner 2701fa729c4dSChristian Brauner #if !defined(CONFIG_STACK_GROWSUP) && !defined(CONFIG_IA64) 2702fa729c4dSChristian Brauner kargs->stack += kargs->stack_size; 2703fa729c4dSChristian Brauner #endif 2704fa729c4dSChristian Brauner } 2705fa729c4dSChristian Brauner 2706fa729c4dSChristian Brauner return true; 2707fa729c4dSChristian Brauner } 2708fa729c4dSChristian Brauner 2709fa729c4dSChristian Brauner static bool clone3_args_valid(struct kernel_clone_args *kargs) 27107f192e3cSChristian Brauner { 2711b612e5dfSChristian Brauner /* Verify that no unknown flags are passed along. */ 2712ef2c41cfSChristian Brauner if (kargs->flags & 2713ef2c41cfSChristian Brauner ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP)) 27147f192e3cSChristian Brauner return false; 27157f192e3cSChristian Brauner 27167f192e3cSChristian Brauner /* 27177f192e3cSChristian Brauner * - make the CLONE_DETACHED bit reuseable for clone3 27187f192e3cSChristian Brauner * - make the CSIGNAL bits reuseable for clone3 27197f192e3cSChristian Brauner */ 27207f192e3cSChristian Brauner if (kargs->flags & (CLONE_DETACHED | CSIGNAL)) 27217f192e3cSChristian Brauner return false; 27227f192e3cSChristian Brauner 2723b612e5dfSChristian Brauner if ((kargs->flags & (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) == 2724b612e5dfSChristian Brauner (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) 2725b612e5dfSChristian Brauner return false; 2726b612e5dfSChristian Brauner 27277f192e3cSChristian Brauner if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) && 27287f192e3cSChristian Brauner kargs->exit_signal) 27297f192e3cSChristian Brauner return false; 27307f192e3cSChristian Brauner 2731fa729c4dSChristian Brauner if (!clone3_stack_valid(kargs)) 2732fa729c4dSChristian Brauner return false; 2733fa729c4dSChristian Brauner 27347f192e3cSChristian Brauner return true; 27357f192e3cSChristian Brauner } 27367f192e3cSChristian Brauner 2737501bd016SChristian Brauner /** 2738501bd016SChristian Brauner * clone3 - create a new process with specific properties 2739501bd016SChristian Brauner * @uargs: argument structure 2740501bd016SChristian Brauner * @size: size of @uargs 2741501bd016SChristian Brauner * 2742501bd016SChristian Brauner * clone3() is the extensible successor to clone()/clone2(). 2743501bd016SChristian Brauner * It takes a struct as argument that is versioned by its size. 2744501bd016SChristian Brauner * 2745501bd016SChristian Brauner * Return: On success, a positive PID for the child process. 2746501bd016SChristian Brauner * On error, a negative errno number. 2747501bd016SChristian Brauner */ 27487f192e3cSChristian Brauner SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size) 27497f192e3cSChristian Brauner { 27507f192e3cSChristian Brauner int err; 27517f192e3cSChristian Brauner 27527f192e3cSChristian Brauner struct kernel_clone_args kargs; 275349cb2fc4SAdrian Reber pid_t set_tid[MAX_PID_NS_LEVEL]; 275449cb2fc4SAdrian Reber 275549cb2fc4SAdrian Reber kargs.set_tid = set_tid; 27567f192e3cSChristian Brauner 27577f192e3cSChristian Brauner err = copy_clone_args_from_user(&kargs, uargs, size); 27587f192e3cSChristian Brauner if (err) 27597f192e3cSChristian Brauner return err; 27607f192e3cSChristian Brauner 27617f192e3cSChristian Brauner if (!clone3_args_valid(&kargs)) 27627f192e3cSChristian Brauner return -EINVAL; 27637f192e3cSChristian Brauner 27647f192e3cSChristian Brauner return _do_fork(&kargs); 2765d2125043SAl Viro } 2766d2125043SAl Viro #endif 2767d2125043SAl Viro 27680f1b92cbSOleg Nesterov void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) 27690f1b92cbSOleg Nesterov { 27700f1b92cbSOleg Nesterov struct task_struct *leader, *parent, *child; 27710f1b92cbSOleg Nesterov int res; 27720f1b92cbSOleg Nesterov 27730f1b92cbSOleg Nesterov read_lock(&tasklist_lock); 27740f1b92cbSOleg Nesterov leader = top = top->group_leader; 27750f1b92cbSOleg Nesterov down: 27760f1b92cbSOleg Nesterov for_each_thread(leader, parent) { 27770f1b92cbSOleg Nesterov list_for_each_entry(child, &parent->children, sibling) { 27780f1b92cbSOleg Nesterov res = visitor(child, data); 27790f1b92cbSOleg Nesterov if (res) { 27800f1b92cbSOleg Nesterov if (res < 0) 27810f1b92cbSOleg Nesterov goto out; 27820f1b92cbSOleg Nesterov leader = child; 27830f1b92cbSOleg Nesterov goto down; 27840f1b92cbSOleg Nesterov } 27850f1b92cbSOleg Nesterov up: 27860f1b92cbSOleg Nesterov ; 27870f1b92cbSOleg Nesterov } 27880f1b92cbSOleg Nesterov } 27890f1b92cbSOleg Nesterov 27900f1b92cbSOleg Nesterov if (leader != top) { 27910f1b92cbSOleg Nesterov child = leader; 27920f1b92cbSOleg Nesterov parent = child->real_parent; 27930f1b92cbSOleg Nesterov leader = parent->group_leader; 27940f1b92cbSOleg Nesterov goto up; 27950f1b92cbSOleg Nesterov } 27960f1b92cbSOleg Nesterov out: 27970f1b92cbSOleg Nesterov read_unlock(&tasklist_lock); 27980f1b92cbSOleg Nesterov } 27990f1b92cbSOleg Nesterov 28005fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 28015fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 28025fd63b30SRavikiran G Thirumalai #endif 28035fd63b30SRavikiran G Thirumalai 280451cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 2805aa1757f9SOleg Nesterov { 2806aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 2807aa1757f9SOleg Nesterov 2808aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 2809b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 2810fba2afaaSDavide Libenzi } 2811aa1757f9SOleg Nesterov 28121da177e4SLinus Torvalds void __init proc_caches_init(void) 28131da177e4SLinus Torvalds { 2814c1a2f7f0SRik van Riel unsigned int mm_size; 2815c1a2f7f0SRik van Riel 28161da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 28171da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 28185f0d5a3aSPaul E. McKenney SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| 281975f296d9SLevin, Alexander (Sasha Levin) SLAB_ACCOUNT, sighand_ctor); 28201da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 28211da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 282275f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 28235d097056SVladimir Davydov NULL); 28241da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 28251da177e4SLinus Torvalds sizeof(struct files_struct), 0, 282675f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 28275d097056SVladimir Davydov NULL); 28281da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 28291da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 283075f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 28315d097056SVladimir Davydov NULL); 2832c1a2f7f0SRik van Riel 28336345d24dSLinus Torvalds /* 2834c1a2f7f0SRik van Riel * The mm_cpumask is located at the end of mm_struct, and is 2835c1a2f7f0SRik van Riel * dynamically sized based on the maximum CPU number this system 2836c1a2f7f0SRik van Riel * can have, taking hotplug into account (nr_cpu_ids). 28376345d24dSLinus Torvalds */ 2838c1a2f7f0SRik van Riel mm_size = sizeof(struct mm_struct) + cpumask_size(); 2839c1a2f7f0SRik van Riel 284007dcd7feSDavid Windsor mm_cachep = kmem_cache_create_usercopy("mm_struct", 2841c1a2f7f0SRik van Riel mm_size, ARCH_MIN_MMSTRUCT_ALIGN, 284275f296d9SLevin, Alexander (Sasha Levin) SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 284307dcd7feSDavid Windsor offsetof(struct mm_struct, saved_auxv), 284407dcd7feSDavid Windsor sizeof_field(struct mm_struct, saved_auxv), 28455d097056SVladimir Davydov NULL); 28465d097056SVladimir Davydov vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 28478feae131SDavid Howells mmap_init(); 284866577193SAl Viro nsproxy_cache_init(); 28491da177e4SLinus Torvalds } 2850cf2e340fSJANAK DESAI 2851cf2e340fSJANAK DESAI /* 28529bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 2853cf2e340fSJANAK DESAI */ 28549bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 2855cf2e340fSJANAK DESAI { 28569bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 28579bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 285850804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 2859769071acSAndrei Vagin CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP| 2860769071acSAndrei Vagin CLONE_NEWTIME)) 2861cf2e340fSJANAK DESAI return -EINVAL; 28629bfb23fcSOleg Nesterov /* 286312c641abSEric W. Biederman * Not implemented, but pretend it works if there is nothing 286412c641abSEric W. Biederman * to unshare. Note that unsharing the address space or the 286512c641abSEric W. Biederman * signal handlers also need to unshare the signal queues (aka 286612c641abSEric W. Biederman * CLONE_THREAD). 28679bfb23fcSOleg Nesterov */ 28689bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 286912c641abSEric W. Biederman if (!thread_group_empty(current)) 287012c641abSEric W. Biederman return -EINVAL; 287112c641abSEric W. Biederman } 287212c641abSEric W. Biederman if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 2873d036bda7SElena Reshetova if (refcount_read(¤t->sighand->count) > 1) 287412c641abSEric W. Biederman return -EINVAL; 287512c641abSEric W. Biederman } 287612c641abSEric W. Biederman if (unshare_flags & CLONE_VM) { 287712c641abSEric W. Biederman if (!current_is_single_threaded()) 28789bfb23fcSOleg Nesterov return -EINVAL; 28799bfb23fcSOleg Nesterov } 2880cf2e340fSJANAK DESAI 2881cf2e340fSJANAK DESAI return 0; 2882cf2e340fSJANAK DESAI } 2883cf2e340fSJANAK DESAI 2884cf2e340fSJANAK DESAI /* 288599d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 2886cf2e340fSJANAK DESAI */ 2887cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 2888cf2e340fSJANAK DESAI { 2889cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 2890cf2e340fSJANAK DESAI 2891498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 2892498052bbSAl Viro return 0; 2893498052bbSAl Viro 2894498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 2895498052bbSAl Viro if (fs->users == 1) 2896498052bbSAl Viro return 0; 2897498052bbSAl Viro 2898498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 289999d1419dSJANAK DESAI if (!*new_fsp) 290099d1419dSJANAK DESAI return -ENOMEM; 2901cf2e340fSJANAK DESAI 2902cf2e340fSJANAK DESAI return 0; 2903cf2e340fSJANAK DESAI } 2904cf2e340fSJANAK DESAI 2905cf2e340fSJANAK DESAI /* 2906a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 2907cf2e340fSJANAK DESAI */ 2908cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 2909cf2e340fSJANAK DESAI { 2910cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 2911a016f338SJANAK DESAI int error = 0; 2912cf2e340fSJANAK DESAI 2913cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 2914a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 2915a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 2916a016f338SJANAK DESAI if (!*new_fdp) 2917a016f338SJANAK DESAI return error; 2918a016f338SJANAK DESAI } 2919cf2e340fSJANAK DESAI 2920cf2e340fSJANAK DESAI return 0; 2921cf2e340fSJANAK DESAI } 2922cf2e340fSJANAK DESAI 2923cf2e340fSJANAK DESAI /* 2924cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 2925cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 2926cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 2927cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 2928cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 2929cf2e340fSJANAK DESAI * task_struct. 2930cf2e340fSJANAK DESAI */ 29319b32105eSDominik Brodowski int ksys_unshare(unsigned long unshare_flags) 2932cf2e340fSJANAK DESAI { 2933cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 2934cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 2935b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 2936cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 29379edff4abSManfred Spraul int do_sysvsem = 0; 29389bfb23fcSOleg Nesterov int err; 2939cf2e340fSJANAK DESAI 294050804fe3SEric W. Biederman /* 2941faf00da5SEric W. Biederman * If unsharing a user namespace must also unshare the thread group 2942faf00da5SEric W. Biederman * and unshare the filesystem root and working directories. 2943b2e0d987SEric W. Biederman */ 2944b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 2945e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 2946b2e0d987SEric W. Biederman /* 294750804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 294850804fe3SEric W. Biederman */ 294950804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 295050804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 29516013f67fSManfred Spraul /* 295212c641abSEric W. Biederman * If unsharing a signal handlers, must also unshare the signal queues. 295312c641abSEric W. Biederman */ 295412c641abSEric W. Biederman if (unshare_flags & CLONE_SIGHAND) 295512c641abSEric W. Biederman unshare_flags |= CLONE_THREAD; 295612c641abSEric W. Biederman /* 29579bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 29589bfb23fcSOleg Nesterov */ 29599bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 29609bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 296150804fe3SEric W. Biederman 296250804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 296350804fe3SEric W. Biederman if (err) 296450804fe3SEric W. Biederman goto bad_unshare_out; 29659bfb23fcSOleg Nesterov /* 29666013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 29676013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 29686013f67fSManfred Spraul * namespace are unreachable. 29696013f67fSManfred Spraul */ 29706013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 29719edff4abSManfred Spraul do_sysvsem = 1; 2972fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 2973fb0a685cSDaniel Rebelo de Oliveira if (err) 29749bfb23fcSOleg Nesterov goto bad_unshare_out; 2975fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 2976fb0a685cSDaniel Rebelo de Oliveira if (err) 29779bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 2978b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 2979fb0a685cSDaniel Rebelo de Oliveira if (err) 29809edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 2981b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 2982b2e0d987SEric W. Biederman new_cred, new_fs); 2983b2e0d987SEric W. Biederman if (err) 2984b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 2985cf2e340fSJANAK DESAI 2986b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 29879edff4abSManfred Spraul if (do_sysvsem) { 29889edff4abSManfred Spraul /* 29899edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 29909edff4abSManfred Spraul */ 29919edff4abSManfred Spraul exit_sem(current); 29929edff4abSManfred Spraul } 2993ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 2994ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 2995ab602f79SJack Miller exit_shm(current); 2996ab602f79SJack Miller shm_init_task(current); 2997ab602f79SJack Miller } 2998ab516013SSerge E. Hallyn 29996f977e6bSAlan Cox if (new_nsproxy) 3000cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 3001cf2e340fSJANAK DESAI 3002cf7b708cSPavel Emelyanov task_lock(current); 3003cf7b708cSPavel Emelyanov 3004cf2e340fSJANAK DESAI if (new_fs) { 3005cf2e340fSJANAK DESAI fs = current->fs; 30062a4419b5SNick Piggin spin_lock(&fs->lock); 3007cf2e340fSJANAK DESAI current->fs = new_fs; 3008498052bbSAl Viro if (--fs->users) 3009498052bbSAl Viro new_fs = NULL; 3010498052bbSAl Viro else 3011cf2e340fSJANAK DESAI new_fs = fs; 30122a4419b5SNick Piggin spin_unlock(&fs->lock); 3013cf2e340fSJANAK DESAI } 3014cf2e340fSJANAK DESAI 3015cf2e340fSJANAK DESAI if (new_fd) { 3016cf2e340fSJANAK DESAI fd = current->files; 3017cf2e340fSJANAK DESAI current->files = new_fd; 3018cf2e340fSJANAK DESAI new_fd = fd; 3019cf2e340fSJANAK DESAI } 3020cf2e340fSJANAK DESAI 3021cf2e340fSJANAK DESAI task_unlock(current); 3022b2e0d987SEric W. Biederman 3023b2e0d987SEric W. Biederman if (new_cred) { 3024b2e0d987SEric W. Biederman /* Install the new user namespace */ 3025b2e0d987SEric W. Biederman commit_creds(new_cred); 3026b2e0d987SEric W. Biederman new_cred = NULL; 3027b2e0d987SEric W. Biederman } 3028cf2e340fSJANAK DESAI } 3029cf2e340fSJANAK DESAI 3030e4222673SHari Bathini perf_event_namespaces(current); 3031e4222673SHari Bathini 3032b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 3033b2e0d987SEric W. Biederman if (new_cred) 3034b2e0d987SEric W. Biederman put_cred(new_cred); 3035cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 3036cf2e340fSJANAK DESAI if (new_fd) 3037cf2e340fSJANAK DESAI put_files_struct(new_fd); 3038cf2e340fSJANAK DESAI 3039cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 3040cf2e340fSJANAK DESAI if (new_fs) 3041498052bbSAl Viro free_fs_struct(new_fs); 3042cf2e340fSJANAK DESAI 3043cf2e340fSJANAK DESAI bad_unshare_out: 3044cf2e340fSJANAK DESAI return err; 3045cf2e340fSJANAK DESAI } 30463b125388SAl Viro 30479b32105eSDominik Brodowski SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 30489b32105eSDominik Brodowski { 30499b32105eSDominik Brodowski return ksys_unshare(unshare_flags); 30509b32105eSDominik Brodowski } 30519b32105eSDominik Brodowski 30523b125388SAl Viro /* 30533b125388SAl Viro * Helper to unshare the files of the current task. 30543b125388SAl Viro * We don't want to expose copy_files internals to 30553b125388SAl Viro * the exec layer of the kernel. 30563b125388SAl Viro */ 30573b125388SAl Viro 30583b125388SAl Viro int unshare_files(struct files_struct **displaced) 30593b125388SAl Viro { 30603b125388SAl Viro struct task_struct *task = current; 306150704516SAl Viro struct files_struct *copy = NULL; 30623b125388SAl Viro int error; 30633b125388SAl Viro 30643b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 30653b125388SAl Viro if (error || !copy) { 30663b125388SAl Viro *displaced = NULL; 30673b125388SAl Viro return error; 30683b125388SAl Viro } 30693b125388SAl Viro *displaced = task->files; 30703b125388SAl Viro task_lock(task); 30713b125388SAl Viro task->files = copy; 30723b125388SAl Viro task_unlock(task); 30733b125388SAl Viro return 0; 30743b125388SAl Viro } 307516db3d3fSHeinrich Schuchardt 307616db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write, 307716db3d3fSHeinrich Schuchardt void __user *buffer, size_t *lenp, loff_t *ppos) 307816db3d3fSHeinrich Schuchardt { 307916db3d3fSHeinrich Schuchardt struct ctl_table t; 308016db3d3fSHeinrich Schuchardt int ret; 308116db3d3fSHeinrich Schuchardt int threads = max_threads; 3082b0f53dbcSMichal Hocko int min = 1; 308316db3d3fSHeinrich Schuchardt int max = MAX_THREADS; 308416db3d3fSHeinrich Schuchardt 308516db3d3fSHeinrich Schuchardt t = *table; 308616db3d3fSHeinrich Schuchardt t.data = &threads; 308716db3d3fSHeinrich Schuchardt t.extra1 = &min; 308816db3d3fSHeinrich Schuchardt t.extra2 = &max; 308916db3d3fSHeinrich Schuchardt 309016db3d3fSHeinrich Schuchardt ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 309116db3d3fSHeinrich Schuchardt if (ret || !write) 309216db3d3fSHeinrich Schuchardt return ret; 309316db3d3fSHeinrich Schuchardt 3094b0f53dbcSMichal Hocko max_threads = threads; 309516db3d3fSHeinrich Schuchardt 309616db3d3fSHeinrich Schuchardt return 0; 309716db3d3fSHeinrich Schuchardt } 3098