11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/fork.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * 'fork.c' contains the help-routines for the 'fork' system call 91da177e4SLinus Torvalds * (see also entry.S and others). 101da177e4SLinus Torvalds * Fork is rather simple, once you get the hang of it, but the memory 111da177e4SLinus Torvalds * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/slab.h> 151da177e4SLinus Torvalds #include <linux/init.h> 161da177e4SLinus Torvalds #include <linux/unistd.h> 171da177e4SLinus Torvalds #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/vmalloc.h> 191da177e4SLinus Torvalds #include <linux/completion.h> 201da177e4SLinus Torvalds #include <linux/personality.h> 211da177e4SLinus Torvalds #include <linux/mempolicy.h> 221da177e4SLinus Torvalds #include <linux/sem.h> 231da177e4SLinus Torvalds #include <linux/file.h> 249f3acc31SAl Viro #include <linux/fdtable.h> 25da9cbc87SJens Axboe #include <linux/iocontext.h> 261da177e4SLinus Torvalds #include <linux/key.h> 271da177e4SLinus Torvalds #include <linux/binfmts.h> 281da177e4SLinus Torvalds #include <linux/mman.h> 29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 301da177e4SLinus Torvalds #include <linux/fs.h> 31615d6e87SDavidlohr Bueso #include <linux/mm.h> 32615d6e87SDavidlohr Bueso #include <linux/vmacache.h> 33ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 34c59ede7bSRandy.Dunlap #include <linux/capability.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 36b4f48b63SPaul Menage #include <linux/cgroup.h> 371da177e4SLinus Torvalds #include <linux/security.h> 38a1e78772SMel Gorman #include <linux/hugetlb.h> 39e2cfabdfSWill Drewry #include <linux/seccomp.h> 401da177e4SLinus Torvalds #include <linux/swap.h> 411da177e4SLinus Torvalds #include <linux/syscalls.h> 421da177e4SLinus Torvalds #include <linux/jiffies.h> 431da177e4SLinus Torvalds #include <linux/futex.h> 448141c7f3SLinus Torvalds #include <linux/compat.h> 45207205a2SEric Dumazet #include <linux/kthread.h> 467c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 47ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 481da177e4SLinus Torvalds #include <linux/ptrace.h> 491da177e4SLinus Torvalds #include <linux/mount.h> 501da177e4SLinus Torvalds #include <linux/audit.h> 5178fb7466SPavel Emelianov #include <linux/memcontrol.h> 52f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 535e2bf014SMike Galbraith #include <linux/proc_fs.h> 541da177e4SLinus Torvalds #include <linux/profile.h> 551da177e4SLinus Torvalds #include <linux/rmap.h> 56f8af4da3SHugh Dickins #include <linux/ksm.h> 571da177e4SLinus Torvalds #include <linux/acct.h> 588f0ab514SJay Lan #include <linux/tsacct_kern.h> 599f46080cSMatt Helsley #include <linux/cn_proc.h> 60ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 61ca74e92bSShailabh Nagar #include <linux/delayacct.h> 62ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 630a425405SArjan van de Ven #include <linux/random.h> 64522ed776SMiloslav Trmac #include <linux/tty.h> 65fd0928dfSJens Axboe #include <linux/blkdev.h> 665ad4e53bSAl Viro #include <linux/fs_struct.h> 677c9f8861SEric Sandeen #include <linux/magic.h> 68cdd6c482SIngo Molnar #include <linux/perf_event.h> 6942c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 708e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 713d5992d2SYing Han #include <linux/oom.h> 72ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 73d80e731eSOleg Nesterov #include <linux/signalfd.h> 740326f5a9SSrikar Dronamraju #include <linux/uprobes.h> 75a27bb332SKent Overstreet #include <linux/aio.h> 7652f5684cSGideon Israel Dsouza #include <linux/compiler.h> 7716db3d3fSHeinrich Schuchardt #include <linux/sysctl.h> 785c9a8750SDmitry Vyukov #include <linux/kcov.h> 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #include <asm/pgtable.h> 811da177e4SLinus Torvalds #include <asm/pgalloc.h> 821da177e4SLinus Torvalds #include <asm/uaccess.h> 831da177e4SLinus Torvalds #include <asm/mmu_context.h> 841da177e4SLinus Torvalds #include <asm/cacheflush.h> 851da177e4SLinus Torvalds #include <asm/tlbflush.h> 861da177e4SLinus Torvalds 87ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 88ad8d75ffSSteven Rostedt 8943d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 9043d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 9143d2b113SKAMEZAWA Hiroyuki 921da177e4SLinus Torvalds /* 93ac1b398dSHeinrich Schuchardt * Minimum number of threads to boot the kernel 94ac1b398dSHeinrich Schuchardt */ 95ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20 96ac1b398dSHeinrich Schuchardt 97ac1b398dSHeinrich Schuchardt /* 98ac1b398dSHeinrich Schuchardt * Maximum number of threads 99ac1b398dSHeinrich Schuchardt */ 100ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK 101ac1b398dSHeinrich Schuchardt 102ac1b398dSHeinrich Schuchardt /* 1031da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 1061da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 113db1466b3SPaul E. McKenney 114db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 115db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 116db1466b3SPaul E. McKenney { 117db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 118db1466b3SPaul E. McKenney } 119db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 120db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds int nr_processes(void) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds int cpu; 1251da177e4SLinus Torvalds int total = 0; 1261da177e4SLinus Torvalds 1271d510750SIan Campbell for_each_possible_cpu(cpu) 1281da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds return total; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 133f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 134f19b9f74SAkinobu Mita { 135f19b9f74SAkinobu Mita } 136f19b9f74SAkinobu Mita 137f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 138e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 13941101809SThomas Gleixner 14041101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 14141101809SThomas Gleixner { 14241101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 14341101809SThomas Gleixner } 14441101809SThomas Gleixner 14541101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 14641101809SThomas Gleixner { 14741101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 14841101809SThomas Gleixner } 1491da177e4SLinus Torvalds #endif 1501da177e4SLinus Torvalds 151b235beeaSLinus Torvalds void __weak arch_release_thread_stack(unsigned long *stack) 152f19b9f74SAkinobu Mita { 153f19b9f74SAkinobu Mita } 154f19b9f74SAkinobu Mita 155b235beeaSLinus Torvalds #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR 15641101809SThomas Gleixner 1570d15d74aSThomas Gleixner /* 1580d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1590d15d74aSThomas Gleixner * kmemcache based allocator. 1600d15d74aSThomas Gleixner */ 161ba14a194SAndy Lutomirski # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) 162ac496bf4SAndy Lutomirski 163ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 164ac496bf4SAndy Lutomirski /* 165ac496bf4SAndy Lutomirski * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 166ac496bf4SAndy Lutomirski * flush. Try to minimize the number of calls by caching stacks. 167ac496bf4SAndy Lutomirski */ 168ac496bf4SAndy Lutomirski #define NR_CACHED_STACKS 2 169ac496bf4SAndy Lutomirski static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 170ac496bf4SAndy Lutomirski #endif 171ac496bf4SAndy Lutomirski 172ba14a194SAndy Lutomirski static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) 173b69c49b7SFUJITA Tomonori { 174ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 175ac496bf4SAndy Lutomirski void *stack; 176ac496bf4SAndy Lutomirski int i; 177ac496bf4SAndy Lutomirski 178ac496bf4SAndy Lutomirski local_irq_disable(); 179ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 180ac496bf4SAndy Lutomirski struct vm_struct *s = this_cpu_read(cached_stacks[i]); 181ac496bf4SAndy Lutomirski 182ac496bf4SAndy Lutomirski if (!s) 183ac496bf4SAndy Lutomirski continue; 184ac496bf4SAndy Lutomirski this_cpu_write(cached_stacks[i], NULL); 185ac496bf4SAndy Lutomirski 186ac496bf4SAndy Lutomirski tsk->stack_vm_area = s; 187ac496bf4SAndy Lutomirski local_irq_enable(); 188ac496bf4SAndy Lutomirski return s->addr; 189ac496bf4SAndy Lutomirski } 190ac496bf4SAndy Lutomirski local_irq_enable(); 191ac496bf4SAndy Lutomirski 192ac496bf4SAndy Lutomirski stack = __vmalloc_node_range(THREAD_SIZE, THREAD_SIZE, 193ba14a194SAndy Lutomirski VMALLOC_START, VMALLOC_END, 194ba14a194SAndy Lutomirski THREADINFO_GFP | __GFP_HIGHMEM, 195ba14a194SAndy Lutomirski PAGE_KERNEL, 196ac496bf4SAndy Lutomirski 0, node, __builtin_return_address(0)); 197ba14a194SAndy Lutomirski 198ba14a194SAndy Lutomirski /* 199ba14a194SAndy Lutomirski * We can't call find_vm_area() in interrupt context, and 200ba14a194SAndy Lutomirski * free_thread_stack() can be called in interrupt context, 201ba14a194SAndy Lutomirski * so cache the vm_struct. 202ba14a194SAndy Lutomirski */ 203ba14a194SAndy Lutomirski if (stack) 204ba14a194SAndy Lutomirski tsk->stack_vm_area = find_vm_area(stack); 205ba14a194SAndy Lutomirski return stack; 206ba14a194SAndy Lutomirski #else 2074949148aSVladimir Davydov struct page *page = alloc_pages_node(node, THREADINFO_GFP, 2082889f608SThomas Gleixner THREAD_SIZE_ORDER); 209b6a84016SEric Dumazet 210b6a84016SEric Dumazet return page ? page_address(page) : NULL; 211ba14a194SAndy Lutomirski #endif 212b69c49b7SFUJITA Tomonori } 213b69c49b7SFUJITA Tomonori 214ba14a194SAndy Lutomirski static inline void free_thread_stack(struct task_struct *tsk) 215b69c49b7SFUJITA Tomonori { 216ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 217ac496bf4SAndy Lutomirski if (task_stack_vm_area(tsk)) { 218ac496bf4SAndy Lutomirski unsigned long flags; 219ac496bf4SAndy Lutomirski int i; 220ac496bf4SAndy Lutomirski 221ac496bf4SAndy Lutomirski local_irq_save(flags); 222ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 223ac496bf4SAndy Lutomirski if (this_cpu_read(cached_stacks[i])) 224ac496bf4SAndy Lutomirski continue; 225ac496bf4SAndy Lutomirski 226ac496bf4SAndy Lutomirski this_cpu_write(cached_stacks[i], tsk->stack_vm_area); 227ac496bf4SAndy Lutomirski local_irq_restore(flags); 228ac496bf4SAndy Lutomirski return; 229ac496bf4SAndy Lutomirski } 230ac496bf4SAndy Lutomirski local_irq_restore(flags); 231ac496bf4SAndy Lutomirski 232*0f110a9bSAndrey Ryabinin vfree_atomic(tsk->stack); 233ac496bf4SAndy Lutomirski return; 234ac496bf4SAndy Lutomirski } 235ac496bf4SAndy Lutomirski #endif 236ac496bf4SAndy Lutomirski 237ba14a194SAndy Lutomirski __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); 238b69c49b7SFUJITA Tomonori } 2390d15d74aSThomas Gleixner # else 240b235beeaSLinus Torvalds static struct kmem_cache *thread_stack_cache; 2410d15d74aSThomas Gleixner 2429521d399SMichael Ellerman static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, 2430d15d74aSThomas Gleixner int node) 2440d15d74aSThomas Gleixner { 245b235beeaSLinus Torvalds return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 2460d15d74aSThomas Gleixner } 2470d15d74aSThomas Gleixner 248ba14a194SAndy Lutomirski static void free_thread_stack(struct task_struct *tsk) 2490d15d74aSThomas Gleixner { 250ba14a194SAndy Lutomirski kmem_cache_free(thread_stack_cache, tsk->stack); 2510d15d74aSThomas Gleixner } 2520d15d74aSThomas Gleixner 253b235beeaSLinus Torvalds void thread_stack_cache_init(void) 2540d15d74aSThomas Gleixner { 255b235beeaSLinus Torvalds thread_stack_cache = kmem_cache_create("thread_stack", THREAD_SIZE, 2560d15d74aSThomas Gleixner THREAD_SIZE, 0, NULL); 257b235beeaSLinus Torvalds BUG_ON(thread_stack_cache == NULL); 2580d15d74aSThomas Gleixner } 2590d15d74aSThomas Gleixner # endif 260b69c49b7SFUJITA Tomonori #endif 261b69c49b7SFUJITA Tomonori 2621da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 263e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 266e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 269e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 272e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 275e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 278e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 2791da177e4SLinus Torvalds 280ba14a194SAndy Lutomirski static void account_kernel_stack(struct task_struct *tsk, int account) 281c6a7f572SKOSAKI Motohiro { 282ba14a194SAndy Lutomirski void *stack = task_stack_page(tsk); 283ba14a194SAndy Lutomirski struct vm_struct *vm = task_stack_vm_area(tsk); 284ba14a194SAndy Lutomirski 285ba14a194SAndy Lutomirski BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); 286ba14a194SAndy Lutomirski 287ba14a194SAndy Lutomirski if (vm) { 288ba14a194SAndy Lutomirski int i; 289ba14a194SAndy Lutomirski 290ba14a194SAndy Lutomirski BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); 291ba14a194SAndy Lutomirski 292ba14a194SAndy Lutomirski for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 293ba14a194SAndy Lutomirski mod_zone_page_state(page_zone(vm->pages[i]), 294ba14a194SAndy Lutomirski NR_KERNEL_STACK_KB, 295ba14a194SAndy Lutomirski PAGE_SIZE / 1024 * account); 296ba14a194SAndy Lutomirski } 297ba14a194SAndy Lutomirski 298ba14a194SAndy Lutomirski /* All stack pages belong to the same memcg. */ 299ba14a194SAndy Lutomirski memcg_kmem_update_page_stat(vm->pages[0], MEMCG_KERNEL_STACK_KB, 300ba14a194SAndy Lutomirski account * (THREAD_SIZE / 1024)); 301ba14a194SAndy Lutomirski } else { 302ba14a194SAndy Lutomirski /* 303ba14a194SAndy Lutomirski * All stack pages are in the same zone and belong to the 304ba14a194SAndy Lutomirski * same memcg. 305ba14a194SAndy Lutomirski */ 306efdc9490SAndy Lutomirski struct page *first_page = virt_to_page(stack); 307c6a7f572SKOSAKI Motohiro 308efdc9490SAndy Lutomirski mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB, 309d30dd8beSAndy Lutomirski THREAD_SIZE / 1024 * account); 310efdc9490SAndy Lutomirski 311ba14a194SAndy Lutomirski memcg_kmem_update_page_stat(first_page, MEMCG_KERNEL_STACK_KB, 312efdc9490SAndy Lutomirski account * (THREAD_SIZE / 1024)); 313c6a7f572SKOSAKI Motohiro } 314ba14a194SAndy Lutomirski } 315c6a7f572SKOSAKI Motohiro 31668f24b08SAndy Lutomirski static void release_task_stack(struct task_struct *tsk) 3171da177e4SLinus Torvalds { 318405c0759SAndy Lutomirski if (WARN_ON(tsk->state != TASK_DEAD)) 319405c0759SAndy Lutomirski return; /* Better to leak the stack than to free prematurely */ 320405c0759SAndy Lutomirski 321ba14a194SAndy Lutomirski account_kernel_stack(tsk, -1); 322b235beeaSLinus Torvalds arch_release_thread_stack(tsk->stack); 323ba14a194SAndy Lutomirski free_thread_stack(tsk); 32468f24b08SAndy Lutomirski tsk->stack = NULL; 32568f24b08SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 32668f24b08SAndy Lutomirski tsk->stack_vm_area = NULL; 32768f24b08SAndy Lutomirski #endif 32868f24b08SAndy Lutomirski } 32968f24b08SAndy Lutomirski 33068f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 33168f24b08SAndy Lutomirski void put_task_stack(struct task_struct *tsk) 33268f24b08SAndy Lutomirski { 33368f24b08SAndy Lutomirski if (atomic_dec_and_test(&tsk->stack_refcount)) 33468f24b08SAndy Lutomirski release_task_stack(tsk); 33568f24b08SAndy Lutomirski } 33668f24b08SAndy Lutomirski #endif 33768f24b08SAndy Lutomirski 33868f24b08SAndy Lutomirski void free_task(struct task_struct *tsk) 33968f24b08SAndy Lutomirski { 34068f24b08SAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK 34168f24b08SAndy Lutomirski /* 34268f24b08SAndy Lutomirski * The task is finally done with both the stack and thread_info, 34368f24b08SAndy Lutomirski * so free both. 34468f24b08SAndy Lutomirski */ 34568f24b08SAndy Lutomirski release_task_stack(tsk); 34668f24b08SAndy Lutomirski #else 34768f24b08SAndy Lutomirski /* 34868f24b08SAndy Lutomirski * If the task had a separate stack allocation, it should be gone 34968f24b08SAndy Lutomirski * by now. 35068f24b08SAndy Lutomirski */ 35168f24b08SAndy Lutomirski WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0); 35268f24b08SAndy Lutomirski #endif 35323f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 354fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 355e2cfabdfSWill Drewry put_seccomp_filter(tsk); 356f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 3571da5c46fSOleg Nesterov if (tsk->flags & PF_KTHREAD) 3581da5c46fSOleg Nesterov free_kthread_struct(tsk); 3591da177e4SLinus Torvalds free_task_struct(tsk); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 3621da177e4SLinus Torvalds 363ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig) 364ea6d290cSOleg Nesterov { 36597101eb4SOleg Nesterov taskstats_tgid_free(sig); 3661c5354deSMike Galbraith sched_autogroup_exit(sig); 3677283094eSMichal Hocko /* 3687283094eSMichal Hocko * __mmdrop is not safe to call from softirq context on x86 due to 3697283094eSMichal Hocko * pgd_dtor so postpone it to the async context 3707283094eSMichal Hocko */ 37126db62f1SMichal Hocko if (sig->oom_mm) 3727283094eSMichal Hocko mmdrop_async(sig->oom_mm); 373ea6d290cSOleg Nesterov kmem_cache_free(signal_cachep, sig); 374ea6d290cSOleg Nesterov } 375ea6d290cSOleg Nesterov 376ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig) 377ea6d290cSOleg Nesterov { 3781c5354deSMike Galbraith if (atomic_dec_and_test(&sig->sigcnt)) 379ea6d290cSOleg Nesterov free_signal_struct(sig); 380ea6d290cSOleg Nesterov } 381ea6d290cSOleg Nesterov 382158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 3831da177e4SLinus Torvalds { 384270f722dSEugene Teo WARN_ON(!tsk->exit_state); 3851da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 3861da177e4SLinus Torvalds WARN_ON(tsk == current); 3871da177e4SLinus Torvalds 3882e91fa7fSTejun Heo cgroup_free(tsk); 389156654f4SMike Galbraith task_numa_free(tsk); 3901a2a4d06SKees Cook security_task_free(tsk); 391e0e81739SDavid Howells exit_creds(tsk); 39235df17c5SShailabh Nagar delayacct_tsk_free(tsk); 393ea6d290cSOleg Nesterov put_signal_struct(tsk->signal); 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 3961da177e4SLinus Torvalds free_task(tsk); 3971da177e4SLinus Torvalds } 39877c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct); 3991da177e4SLinus Torvalds 4006c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { } 40161c4628bSSuresh Siddha 402ff691f6eSHeinrich Schuchardt /* 403ff691f6eSHeinrich Schuchardt * set_max_threads 404ff691f6eSHeinrich Schuchardt */ 40516db3d3fSHeinrich Schuchardt static void set_max_threads(unsigned int max_threads_suggested) 406ff691f6eSHeinrich Schuchardt { 407ac1b398dSHeinrich Schuchardt u64 threads; 408ff691f6eSHeinrich Schuchardt 409ff691f6eSHeinrich Schuchardt /* 410ac1b398dSHeinrich Schuchardt * The number of threads shall be limited such that the thread 411ac1b398dSHeinrich Schuchardt * structures may only consume a small part of the available memory. 412ff691f6eSHeinrich Schuchardt */ 413ac1b398dSHeinrich Schuchardt if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64) 414ac1b398dSHeinrich Schuchardt threads = MAX_THREADS; 415ac1b398dSHeinrich Schuchardt else 416ac1b398dSHeinrich Schuchardt threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, 417ac1b398dSHeinrich Schuchardt (u64) THREAD_SIZE * 8UL); 418ac1b398dSHeinrich Schuchardt 41916db3d3fSHeinrich Schuchardt if (threads > max_threads_suggested) 42016db3d3fSHeinrich Schuchardt threads = max_threads_suggested; 42116db3d3fSHeinrich Schuchardt 422ac1b398dSHeinrich Schuchardt max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 423ff691f6eSHeinrich Schuchardt } 424ff691f6eSHeinrich Schuchardt 4255aaeb5c0SIngo Molnar #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 4265aaeb5c0SIngo Molnar /* Initialized by the architecture: */ 4275aaeb5c0SIngo Molnar int arch_task_struct_size __read_mostly; 4285aaeb5c0SIngo Molnar #endif 4290c8c0f03SDave Hansen 430ff691f6eSHeinrich Schuchardt void __init fork_init(void) 4311da177e4SLinus Torvalds { 43225f9c081SEric W. Biederman int i; 433f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 4341da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 4351da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 4361da177e4SLinus Torvalds #endif 4371da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 4385d097056SVladimir Davydov task_struct_cachep = kmem_cache_create("task_struct", 4395d097056SVladimir Davydov arch_task_struct_size, ARCH_MIN_TASKALIGN, 4405d097056SVladimir Davydov SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, NULL); 4411da177e4SLinus Torvalds #endif 4421da177e4SLinus Torvalds 44361c4628bSSuresh Siddha /* do the arch specific task caches init */ 44461c4628bSSuresh Siddha arch_task_cache_init(); 44561c4628bSSuresh Siddha 44616db3d3fSHeinrich Schuchardt set_max_threads(MAX_THREADS); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 4491da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 4501da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 4511da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 452b376c3e1SEric W. Biederman 45325f9c081SEric W. Biederman for (i = 0; i < UCOUNT_COUNTS; i++) { 45425f9c081SEric W. Biederman init_user_ns.ucount_max[i] = max_threads/2; 45525f9c081SEric W. Biederman } 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 45852f5684cSGideon Israel Dsouza int __weak arch_dup_task_struct(struct task_struct *dst, 45961c4628bSSuresh Siddha struct task_struct *src) 46061c4628bSSuresh Siddha { 46161c4628bSSuresh Siddha *dst = *src; 46261c4628bSSuresh Siddha return 0; 46361c4628bSSuresh Siddha } 46461c4628bSSuresh Siddha 465d4311ff1SAaron Tomlin void set_task_stack_end_magic(struct task_struct *tsk) 466d4311ff1SAaron Tomlin { 467d4311ff1SAaron Tomlin unsigned long *stackend; 468d4311ff1SAaron Tomlin 469d4311ff1SAaron Tomlin stackend = end_of_stack(tsk); 470d4311ff1SAaron Tomlin *stackend = STACK_END_MAGIC; /* for overflow detection */ 471d4311ff1SAaron Tomlin } 472d4311ff1SAaron Tomlin 473725fc629SAndi Kleen static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds struct task_struct *tsk; 476b235beeaSLinus Torvalds unsigned long *stack; 477ba14a194SAndy Lutomirski struct vm_struct *stack_vm_area; 4783e26c149SPeter Zijlstra int err; 4791da177e4SLinus Torvalds 480725fc629SAndi Kleen if (node == NUMA_NO_NODE) 481725fc629SAndi Kleen node = tsk_fork_get_node(orig); 482504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 4831da177e4SLinus Torvalds if (!tsk) 4841da177e4SLinus Torvalds return NULL; 4851da177e4SLinus Torvalds 486b235beeaSLinus Torvalds stack = alloc_thread_stack_node(tsk, node); 487b235beeaSLinus Torvalds if (!stack) 488f19b9f74SAkinobu Mita goto free_tsk; 4891da177e4SLinus Torvalds 490ba14a194SAndy Lutomirski stack_vm_area = task_stack_vm_area(tsk); 491ba14a194SAndy Lutomirski 49261c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 493ba14a194SAndy Lutomirski 494ba14a194SAndy Lutomirski /* 495ba14a194SAndy Lutomirski * arch_dup_task_struct() clobbers the stack-related fields. Make 496ba14a194SAndy Lutomirski * sure they're properly initialized before using any stack-related 497ba14a194SAndy Lutomirski * functions again. 498ba14a194SAndy Lutomirski */ 499ba14a194SAndy Lutomirski tsk->stack = stack; 500ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 501ba14a194SAndy Lutomirski tsk->stack_vm_area = stack_vm_area; 502ba14a194SAndy Lutomirski #endif 50368f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 50468f24b08SAndy Lutomirski atomic_set(&tsk->stack_refcount, 1); 50568f24b08SAndy Lutomirski #endif 506ba14a194SAndy Lutomirski 50761c4628bSSuresh Siddha if (err) 508b235beeaSLinus Torvalds goto free_stack; 50961c4628bSSuresh Siddha 510dbd95212SKees Cook #ifdef CONFIG_SECCOMP 511dbd95212SKees Cook /* 512dbd95212SKees Cook * We must handle setting up seccomp filters once we're under 513dbd95212SKees Cook * the sighand lock in case orig has changed between now and 514dbd95212SKees Cook * then. Until then, filter must be NULL to avoid messing up 515dbd95212SKees Cook * the usage counts on the error path calling free_task. 516dbd95212SKees Cook */ 517dbd95212SKees Cook tsk->seccomp.filter = NULL; 518dbd95212SKees Cook #endif 51987bec58aSAndrew Morton 52087bec58aSAndrew Morton setup_thread_stack(tsk, orig); 5218e7cac79SAvi Kivity clear_user_return_notifier(tsk); 522f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 523d4311ff1SAaron Tomlin set_task_stack_end_magic(tsk); 5241da177e4SLinus Torvalds 5250a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 5260a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 5270a425405SArjan van de Ven #endif 5280a425405SArjan van de Ven 529fb0a685cSDaniel Rebelo de Oliveira /* 530fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 531fb0a685cSDaniel Rebelo de Oliveira * parent) 532fb0a685cSDaniel Rebelo de Oliveira */ 5331da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 5346c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 5352056a782SJens Axboe tsk->btrace_seq = 0; 5366c5c9341SAlexey Dobriyan #endif 537a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 5385640f768SEric Dumazet tsk->task_frag.page = NULL; 539093e5840SSebastian Andrzej Siewior tsk->wake_q.next = NULL; 540c6a7f572SKOSAKI Motohiro 541ba14a194SAndy Lutomirski account_kernel_stack(tsk, 1); 542c6a7f572SKOSAKI Motohiro 5435c9a8750SDmitry Vyukov kcov_task_init(tsk); 5445c9a8750SDmitry Vyukov 5451da177e4SLinus Torvalds return tsk; 54661c4628bSSuresh Siddha 547b235beeaSLinus Torvalds free_stack: 548ba14a194SAndy Lutomirski free_thread_stack(tsk); 549f19b9f74SAkinobu Mita free_tsk: 55061c4628bSSuresh Siddha free_task_struct(tsk); 55161c4628bSSuresh Siddha return NULL; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds #ifdef CONFIG_MMU 5550766f788SEmese Revfy static __latent_entropy int dup_mmap(struct mm_struct *mm, 5560766f788SEmese Revfy struct mm_struct *oldmm) 5571da177e4SLinus Torvalds { 558297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 5591da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 5601da177e4SLinus Torvalds int retval; 5611da177e4SLinus Torvalds unsigned long charge; 5621da177e4SLinus Torvalds 56332cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 5647c051267SMichal Hocko if (down_write_killable(&oldmm->mmap_sem)) { 5657c051267SMichal Hocko retval = -EINTR; 5667c051267SMichal Hocko goto fail_uprobe_end; 5677c051267SMichal Hocko } 568ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 569f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 570ad339451SIngo Molnar /* 571ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 572ad339451SIngo Molnar */ 573ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 5747ee78232SHugh Dickins 57590f31d0eSKonstantin Khlebnikov /* No ordering required: file already has been exposed. */ 57690f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 57790f31d0eSKonstantin Khlebnikov 5784f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 57984638335SKonstantin Khlebnikov mm->data_vm = oldmm->data_vm; 5804f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 5814f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 5824f7d4614SVladimir Davydov 5831da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 5841da177e4SLinus Torvalds rb_parent = NULL; 5851da177e4SLinus Torvalds pprev = &mm->mmap; 586f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 587f8af4da3SHugh Dickins if (retval) 588f8af4da3SHugh Dickins goto out; 589ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 590ba76149fSAndrea Arcangeli if (retval) 591ba76149fSAndrea Arcangeli goto out; 5921da177e4SLinus Torvalds 593297c5eeeSLinus Torvalds prev = NULL; 594fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 5951da177e4SLinus Torvalds struct file *file; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 59884638335SKonstantin Khlebnikov vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); 5991da177e4SLinus Torvalds continue; 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds charge = 0; 6021da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 603b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 604b2412b7fSHuang Shijie 605191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 6061da177e4SLinus Torvalds goto fail_nomem; 6071da177e4SLinus Torvalds charge = len; 6081da177e4SLinus Torvalds } 609e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 6101da177e4SLinus Torvalds if (!tmp) 6111da177e4SLinus Torvalds goto fail_nomem; 6121da177e4SLinus Torvalds *tmp = *mpnt; 6135beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 614ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 615ef0855d3SOleg Nesterov if (retval) 6161da177e4SLinus Torvalds goto fail_nomem_policy; 617a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 6185beb4930SRik van Riel if (anon_vma_fork(tmp, mpnt)) 6195beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 620de60f5f1SEric B Munson tmp->vm_flags &= 621de60f5f1SEric B Munson ~(VM_LOCKED|VM_LOCKONFAULT|VM_UFFD_MISSING|VM_UFFD_WP); 622297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 623745f234bSAndrea Arcangeli tmp->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; 6241da177e4SLinus Torvalds file = tmp->vm_file; 6251da177e4SLinus Torvalds if (file) { 626496ad9aaSAl Viro struct inode *inode = file_inode(file); 627b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 628b88ed205SHugh Dickins 6291da177e4SLinus Torvalds get_file(file); 6301da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 6311da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 63283cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 633b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 6344bb5f5d9SDavid Herrmann atomic_inc(&mapping->i_mmap_writable); 635b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 636b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 6379826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 6389826a516SMichel Lespinasse &mapping->i_mmap); 639b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 64083cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds /* 644a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 645a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 646a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 647a1e78772SMel Gorman */ 648a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 649a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 650a1e78772SMel Gorman 651a1e78772SMel Gorman /* 6527ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 6531da177e4SLinus Torvalds */ 6541da177e4SLinus Torvalds *pprev = tmp; 6551da177e4SLinus Torvalds pprev = &tmp->vm_next; 656297c5eeeSLinus Torvalds tmp->vm_prev = prev; 657297c5eeeSLinus Torvalds prev = tmp; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 6601da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 6611da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds mm->map_count++; 6640b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 6671da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds if (retval) 6701da177e4SLinus Torvalds goto out; 6711da177e4SLinus Torvalds } 672d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 673d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 6741da177e4SLinus Torvalds retval = 0; 6751da177e4SLinus Torvalds out: 6767ee78232SHugh Dickins up_write(&mm->mmap_sem); 677fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 6781da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 6797c051267SMichal Hocko fail_uprobe_end: 68032cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 6811da177e4SLinus Torvalds return retval; 6825beb4930SRik van Riel fail_nomem_anon_vma_fork: 683ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 6841da177e4SLinus Torvalds fail_nomem_policy: 6851da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 6861da177e4SLinus Torvalds fail_nomem: 6871da177e4SLinus Torvalds retval = -ENOMEM; 6881da177e4SLinus Torvalds vm_unacct_memory(charge); 6891da177e4SLinus Torvalds goto out; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 6931da177e4SLinus Torvalds { 6941da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 6951da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 6961da177e4SLinus Torvalds return -ENOMEM; 6971da177e4SLinus Torvalds return 0; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 7011da177e4SLinus Torvalds { 7025e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds #else 70590f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 70690f31d0eSKonstantin Khlebnikov { 70790f31d0eSKonstantin Khlebnikov down_write(&oldmm->mmap_sem); 70890f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 70990f31d0eSKonstantin Khlebnikov up_write(&oldmm->mmap_sem); 71090f31d0eSKonstantin Khlebnikov return 0; 71190f31d0eSKonstantin Khlebnikov } 7121da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 7131da177e4SLinus Torvalds #define mm_free_pgd(mm) 7141da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 7171da177e4SLinus Torvalds 718e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 7191da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 7201da177e4SLinus Torvalds 7214cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 7224cb0e11bSHidehiro Kawai 7234cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 7244cb0e11bSHidehiro Kawai { 7254cb0e11bSHidehiro Kawai default_dump_filter = 7264cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 7274cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 7284cb0e11bSHidehiro Kawai return 1; 7294cb0e11bSHidehiro Kawai } 7304cb0e11bSHidehiro Kawai 7314cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 7324cb0e11bSHidehiro Kawai 7331da177e4SLinus Torvalds #include <linux/init_task.h> 7341da177e4SLinus Torvalds 735858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 736858f0993SAlexey Dobriyan { 737858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 738858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 739db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 740858f0993SAlexey Dobriyan #endif 741858f0993SAlexey Dobriyan } 742858f0993SAlexey Dobriyan 74333144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 74433144e84SVladimir Davydov { 74533144e84SVladimir Davydov #ifdef CONFIG_MEMCG 74633144e84SVladimir Davydov mm->owner = p; 74733144e84SVladimir Davydov #endif 74833144e84SVladimir Davydov } 74933144e84SVladimir Davydov 75078fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 7511da177e4SLinus Torvalds { 75241f727fdSVladimir Davydov mm->mmap = NULL; 75341f727fdSVladimir Davydov mm->mm_rb = RB_ROOT; 75441f727fdSVladimir Davydov mm->vmacache_seqnum = 0; 7551da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 7561da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 7571da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 7581da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 759999d9fc1SOleg Nesterov mm->core_state = NULL; 760e1f56c89SKirill A. Shutemov atomic_long_set(&mm->nr_ptes, 0); 7612d2f5119SKirill A. Shutemov mm_nr_pmds_init(mm); 76241f727fdSVladimir Davydov mm->map_count = 0; 76341f727fdSVladimir Davydov mm->locked_vm = 0; 764ce65cefaSVladimir Davydov mm->pinned_vm = 0; 765d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 7661da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 76741f727fdSVladimir Davydov mm_init_cpumask(mm); 768858f0993SAlexey Dobriyan mm_init_aio(mm); 769cf475ad2SBalbir Singh mm_init_owner(mm, p); 77041f727fdSVladimir Davydov mmu_notifier_mm_init(mm); 77120841405SRik van Riel clear_tlb_flush_pending(mm); 77241f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 77341f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 77441f727fdSVladimir Davydov #endif 7751da177e4SLinus Torvalds 776a0715cc2SAlex Thorlton if (current->mm) { 777a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 778a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 779a0715cc2SAlex Thorlton } else { 780a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 7811da177e4SLinus Torvalds mm->def_flags = 0; 782a0715cc2SAlex Thorlton } 783a0715cc2SAlex Thorlton 78441f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 78541f727fdSVladimir Davydov goto fail_nopgd; 78678fb7466SPavel Emelianov 78741f727fdSVladimir Davydov if (init_new_context(p, mm)) 78841f727fdSVladimir Davydov goto fail_nocontext; 78941f727fdSVladimir Davydov 79041f727fdSVladimir Davydov return mm; 79141f727fdSVladimir Davydov 79241f727fdSVladimir Davydov fail_nocontext: 79341f727fdSVladimir Davydov mm_free_pgd(mm); 79441f727fdSVladimir Davydov fail_nopgd: 7951da177e4SLinus Torvalds free_mm(mm); 7961da177e4SLinus Torvalds return NULL; 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds 799c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm) 800c3f0327fSKonstantin Khlebnikov { 801c3f0327fSKonstantin Khlebnikov int i; 802c3f0327fSKonstantin Khlebnikov 803c3f0327fSKonstantin Khlebnikov for (i = 0; i < NR_MM_COUNTERS; i++) { 804c3f0327fSKonstantin Khlebnikov long x = atomic_long_read(&mm->rss_stat.count[i]); 805c3f0327fSKonstantin Khlebnikov 806c3f0327fSKonstantin Khlebnikov if (unlikely(x)) 807c3f0327fSKonstantin Khlebnikov printk(KERN_ALERT "BUG: Bad rss-counter state " 808c3f0327fSKonstantin Khlebnikov "mm:%p idx:%d val:%ld\n", mm, i, x); 809c3f0327fSKonstantin Khlebnikov } 810b30fe6c7SKirill A. Shutemov 811b30fe6c7SKirill A. Shutemov if (atomic_long_read(&mm->nr_ptes)) 812b30fe6c7SKirill A. Shutemov pr_alert("BUG: non-zero nr_ptes on freeing mm: %ld\n", 813b30fe6c7SKirill A. Shutemov atomic_long_read(&mm->nr_ptes)); 814b30fe6c7SKirill A. Shutemov if (mm_nr_pmds(mm)) 815b30fe6c7SKirill A. Shutemov pr_alert("BUG: non-zero nr_pmds on freeing mm: %ld\n", 816b30fe6c7SKirill A. Shutemov mm_nr_pmds(mm)); 817b30fe6c7SKirill A. Shutemov 818e009bb30SKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 81996dad67fSSasha Levin VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 820c3f0327fSKonstantin Khlebnikov #endif 821c3f0327fSKonstantin Khlebnikov } 822c3f0327fSKonstantin Khlebnikov 8231da177e4SLinus Torvalds /* 8241da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 8251da177e4SLinus Torvalds */ 8261da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 8271da177e4SLinus Torvalds { 8281da177e4SLinus Torvalds struct mm_struct *mm; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds mm = allocate_mm(); 831de03c72cSKOSAKI Motohiro if (!mm) 832de03c72cSKOSAKI Motohiro return NULL; 833de03c72cSKOSAKI Motohiro 8341da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 8356345d24dSLinus Torvalds return mm_init(mm, current); 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds /* 8391da177e4SLinus Torvalds * Called when the last reference to the mm 8401da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 8411da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 8421da177e4SLinus Torvalds */ 8437ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 8461da177e4SLinus Torvalds mm_free_pgd(mm); 8471da177e4SLinus Torvalds destroy_context(mm); 848cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 849c3f0327fSKonstantin Khlebnikov check_mm(mm); 8501da177e4SLinus Torvalds free_mm(mm); 8511da177e4SLinus Torvalds } 8526d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 8531da177e4SLinus Torvalds 854ec8d7c14SMichal Hocko static inline void __mmput(struct mm_struct *mm) 8551da177e4SLinus Torvalds { 856ec8d7c14SMichal Hocko VM_BUG_ON(atomic_read(&mm->mm_users)); 8570ae26f1bSAndrew Morton 858d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 8591da177e4SLinus Torvalds exit_aio(mm); 8601c2fb7a4SAndrea Arcangeli ksm_exit(mm); 861ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 8621da177e4SLinus Torvalds exit_mmap(mm); 8636fcb52a5SAaron Lu mm_put_huge_zero_page(mm); 864925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 8651da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 8661da177e4SLinus Torvalds spin_lock(&mmlist_lock); 8671da177e4SLinus Torvalds list_del(&mm->mmlist); 8681da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8691da177e4SLinus Torvalds } 870801460d0SHiroshi Shimamoto if (mm->binfmt) 871801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 872862e3073SMichal Hocko set_bit(MMF_OOM_SKIP, &mm->flags); 8731da177e4SLinus Torvalds mmdrop(mm); 8741da177e4SLinus Torvalds } 875ec8d7c14SMichal Hocko 876ec8d7c14SMichal Hocko /* 877ec8d7c14SMichal Hocko * Decrement the use count and release all resources for an mm. 878ec8d7c14SMichal Hocko */ 879ec8d7c14SMichal Hocko void mmput(struct mm_struct *mm) 880ec8d7c14SMichal Hocko { 881ec8d7c14SMichal Hocko might_sleep(); 882ec8d7c14SMichal Hocko 883ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) 884ec8d7c14SMichal Hocko __mmput(mm); 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 8871da177e4SLinus Torvalds 8887ef949d7SMichal Hocko #ifdef CONFIG_MMU 889ec8d7c14SMichal Hocko static void mmput_async_fn(struct work_struct *work) 890ec8d7c14SMichal Hocko { 891ec8d7c14SMichal Hocko struct mm_struct *mm = container_of(work, struct mm_struct, async_put_work); 892ec8d7c14SMichal Hocko __mmput(mm); 893ec8d7c14SMichal Hocko } 894ec8d7c14SMichal Hocko 895ec8d7c14SMichal Hocko void mmput_async(struct mm_struct *mm) 896ec8d7c14SMichal Hocko { 897ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) { 898ec8d7c14SMichal Hocko INIT_WORK(&mm->async_put_work, mmput_async_fn); 899ec8d7c14SMichal Hocko schedule_work(&mm->async_put_work); 900ec8d7c14SMichal Hocko } 901ec8d7c14SMichal Hocko } 9027ef949d7SMichal Hocko #endif 903ec8d7c14SMichal Hocko 90490f31d0eSKonstantin Khlebnikov /** 90590f31d0eSKonstantin Khlebnikov * set_mm_exe_file - change a reference to the mm's executable file 90690f31d0eSKonstantin Khlebnikov * 90790f31d0eSKonstantin Khlebnikov * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 90890f31d0eSKonstantin Khlebnikov * 9096e399cd1SDavidlohr Bueso * Main users are mmput() and sys_execve(). Callers prevent concurrent 9106e399cd1SDavidlohr Bueso * invocations: in mmput() nobody alive left, in execve task is single 9116e399cd1SDavidlohr Bueso * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the 9126e399cd1SDavidlohr Bueso * mm->exe_file, but does so without using set_mm_exe_file() in order 9136e399cd1SDavidlohr Bueso * to do avoid the need for any locks. 91490f31d0eSKonstantin Khlebnikov */ 91538646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 91638646013SJiri Slaby { 9176e399cd1SDavidlohr Bueso struct file *old_exe_file; 9186e399cd1SDavidlohr Bueso 9196e399cd1SDavidlohr Bueso /* 9206e399cd1SDavidlohr Bueso * It is safe to dereference the exe_file without RCU as 9216e399cd1SDavidlohr Bueso * this function is only called if nobody else can access 9226e399cd1SDavidlohr Bueso * this mm -- see comment above for justification. 9236e399cd1SDavidlohr Bueso */ 9246e399cd1SDavidlohr Bueso old_exe_file = rcu_dereference_raw(mm->exe_file); 92590f31d0eSKonstantin Khlebnikov 92638646013SJiri Slaby if (new_exe_file) 92738646013SJiri Slaby get_file(new_exe_file); 92890f31d0eSKonstantin Khlebnikov rcu_assign_pointer(mm->exe_file, new_exe_file); 92990f31d0eSKonstantin Khlebnikov if (old_exe_file) 93090f31d0eSKonstantin Khlebnikov fput(old_exe_file); 93138646013SJiri Slaby } 93238646013SJiri Slaby 93390f31d0eSKonstantin Khlebnikov /** 93490f31d0eSKonstantin Khlebnikov * get_mm_exe_file - acquire a reference to the mm's executable file 93590f31d0eSKonstantin Khlebnikov * 93690f31d0eSKonstantin Khlebnikov * Returns %NULL if mm has no associated executable file. 93790f31d0eSKonstantin Khlebnikov * User must release file via fput(). 93890f31d0eSKonstantin Khlebnikov */ 93938646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 94038646013SJiri Slaby { 94138646013SJiri Slaby struct file *exe_file; 94238646013SJiri Slaby 94390f31d0eSKonstantin Khlebnikov rcu_read_lock(); 94490f31d0eSKonstantin Khlebnikov exe_file = rcu_dereference(mm->exe_file); 94590f31d0eSKonstantin Khlebnikov if (exe_file && !get_file_rcu(exe_file)) 94690f31d0eSKonstantin Khlebnikov exe_file = NULL; 94790f31d0eSKonstantin Khlebnikov rcu_read_unlock(); 94838646013SJiri Slaby return exe_file; 94938646013SJiri Slaby } 95011163348SDavidlohr Bueso EXPORT_SYMBOL(get_mm_exe_file); 95138646013SJiri Slaby 9521da177e4SLinus Torvalds /** 953cd81a917SMateusz Guzik * get_task_exe_file - acquire a reference to the task's executable file 954cd81a917SMateusz Guzik * 955cd81a917SMateusz Guzik * Returns %NULL if task's mm (if any) has no associated executable file or 956cd81a917SMateusz Guzik * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 957cd81a917SMateusz Guzik * User must release file via fput(). 958cd81a917SMateusz Guzik */ 959cd81a917SMateusz Guzik struct file *get_task_exe_file(struct task_struct *task) 960cd81a917SMateusz Guzik { 961cd81a917SMateusz Guzik struct file *exe_file = NULL; 962cd81a917SMateusz Guzik struct mm_struct *mm; 963cd81a917SMateusz Guzik 964cd81a917SMateusz Guzik task_lock(task); 965cd81a917SMateusz Guzik mm = task->mm; 966cd81a917SMateusz Guzik if (mm) { 967cd81a917SMateusz Guzik if (!(task->flags & PF_KTHREAD)) 968cd81a917SMateusz Guzik exe_file = get_mm_exe_file(mm); 969cd81a917SMateusz Guzik } 970cd81a917SMateusz Guzik task_unlock(task); 971cd81a917SMateusz Guzik return exe_file; 972cd81a917SMateusz Guzik } 973cd81a917SMateusz Guzik EXPORT_SYMBOL(get_task_exe_file); 974cd81a917SMateusz Guzik 975cd81a917SMateusz Guzik /** 9761da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 9771da177e4SLinus Torvalds * 978246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 9791da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 9801da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 9811da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 9821da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 9831da177e4SLinus Torvalds */ 9841da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 9851da177e4SLinus Torvalds { 9861da177e4SLinus Torvalds struct mm_struct *mm; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds task_lock(task); 9891da177e4SLinus Torvalds mm = task->mm; 9901da177e4SLinus Torvalds if (mm) { 991246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 9921da177e4SLinus Torvalds mm = NULL; 9931da177e4SLinus Torvalds else 9941da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds task_unlock(task); 9971da177e4SLinus Torvalds return mm; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 10001da177e4SLinus Torvalds 10018cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 10028cdb878dSChristopher Yeoh { 10038cdb878dSChristopher Yeoh struct mm_struct *mm; 10048cdb878dSChristopher Yeoh int err; 10058cdb878dSChristopher Yeoh 10068cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 10078cdb878dSChristopher Yeoh if (err) 10088cdb878dSChristopher Yeoh return ERR_PTR(err); 10098cdb878dSChristopher Yeoh 10108cdb878dSChristopher Yeoh mm = get_task_mm(task); 10118cdb878dSChristopher Yeoh if (mm && mm != current->mm && 10128cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 10138cdb878dSChristopher Yeoh mmput(mm); 10148cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 10158cdb878dSChristopher Yeoh } 10168cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 10178cdb878dSChristopher Yeoh 10188cdb878dSChristopher Yeoh return mm; 10198cdb878dSChristopher Yeoh } 10208cdb878dSChristopher Yeoh 102157b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 1022c415c3b4SOleg Nesterov { 1023d68b46feSOleg Nesterov struct completion *vfork; 1024c415c3b4SOleg Nesterov 1025d68b46feSOleg Nesterov task_lock(tsk); 1026d68b46feSOleg Nesterov vfork = tsk->vfork_done; 1027d68b46feSOleg Nesterov if (likely(vfork)) { 1028c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 1029d68b46feSOleg Nesterov complete(vfork); 1030d68b46feSOleg Nesterov } 1031d68b46feSOleg Nesterov task_unlock(tsk); 1032d68b46feSOleg Nesterov } 1033d68b46feSOleg Nesterov 1034d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 1035d68b46feSOleg Nesterov struct completion *vfork) 1036d68b46feSOleg Nesterov { 1037d68b46feSOleg Nesterov int killed; 1038d68b46feSOleg Nesterov 1039d68b46feSOleg Nesterov freezer_do_not_count(); 1040d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 1041d68b46feSOleg Nesterov freezer_count(); 1042d68b46feSOleg Nesterov 1043d68b46feSOleg Nesterov if (killed) { 1044d68b46feSOleg Nesterov task_lock(child); 1045d68b46feSOleg Nesterov child->vfork_done = NULL; 1046d68b46feSOleg Nesterov task_unlock(child); 1047d68b46feSOleg Nesterov } 1048d68b46feSOleg Nesterov 1049d68b46feSOleg Nesterov put_task_struct(child); 1050d68b46feSOleg Nesterov return killed; 1051c415c3b4SOleg Nesterov } 1052c415c3b4SOleg Nesterov 10531da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 10541da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 10551da177e4SLinus Torvalds * error success whatever. 10561da177e4SLinus Torvalds * 10571da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 10581da177e4SLinus Torvalds * from the current process. 10591da177e4SLinus Torvalds * 10601da177e4SLinus Torvalds * This difference is important for error handling, when we 10611da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 10621da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 10631da177e4SLinus Torvalds * restoring the old one. . . 10641da177e4SLinus Torvalds * Eric Biederman 10 January 1998 10651da177e4SLinus Torvalds */ 10661da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 10671da177e4SLinus Torvalds { 10688141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 10698141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 1070fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 10718141c7f3SLinus Torvalds exit_robust_list(tsk); 1072fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 1073fc6b177dSPeter Zijlstra } 10748141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 1075fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 10768141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 1077fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 1078fc6b177dSPeter Zijlstra } 10798141c7f3SLinus Torvalds #endif 1080322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 1081322a2c10SThomas Gleixner exit_pi_state_list(tsk); 10828141c7f3SLinus Torvalds #endif 10838141c7f3SLinus Torvalds 10840326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 10850326f5a9SSrikar Dronamraju 10861da177e4SLinus Torvalds /* Get rid of any cached register state */ 10871da177e4SLinus Torvalds deactivate_mm(tsk, mm); 10881da177e4SLinus Torvalds 1089fec1d011SRoland McGrath /* 1090735f2770SMichal Hocko * Signal userspace if we're not exiting with a core dump 1091735f2770SMichal Hocko * because we want to leave the value intact for debugging 1092735f2770SMichal Hocko * purposes. 1093fec1d011SRoland McGrath */ 10949c8a8228SEric Dumazet if (tsk->clear_child_tid) { 1095735f2770SMichal Hocko if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) && 10969c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 10971da177e4SLinus Torvalds /* 10981da177e4SLinus Torvalds * We don't check the error code - if userspace has 10991da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 11001da177e4SLinus Torvalds */ 11019c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 11029c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 11039c8a8228SEric Dumazet 1, NULL, NULL, 0); 11049c8a8228SEric Dumazet } 11059c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 11061da177e4SLinus Torvalds } 1107f7505d64SKonstantin Khlebnikov 1108f7505d64SKonstantin Khlebnikov /* 1109f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 1110f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 1111f7505d64SKonstantin Khlebnikov */ 1112f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 1113f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 1116a0a7ec30SJANAK DESAI /* 1117a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 1118a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 1119a0a7ec30SJANAK DESAI */ 1120ff252c1fSDaeSeok Youn static struct mm_struct *dup_mm(struct task_struct *tsk) 1121a0a7ec30SJANAK DESAI { 1122a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 1123a0a7ec30SJANAK DESAI int err; 1124a0a7ec30SJANAK DESAI 1125a0a7ec30SJANAK DESAI mm = allocate_mm(); 1126a0a7ec30SJANAK DESAI if (!mm) 1127a0a7ec30SJANAK DESAI goto fail_nomem; 1128a0a7ec30SJANAK DESAI 1129a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 1130a0a7ec30SJANAK DESAI 113178fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 1132a0a7ec30SJANAK DESAI goto fail_nomem; 1133a0a7ec30SJANAK DESAI 1134a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 1135a0a7ec30SJANAK DESAI if (err) 1136a0a7ec30SJANAK DESAI goto free_pt; 1137a0a7ec30SJANAK DESAI 1138a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 1139a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 1140a0a7ec30SJANAK DESAI 1141801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1142801460d0SHiroshi Shimamoto goto free_pt; 1143801460d0SHiroshi Shimamoto 1144a0a7ec30SJANAK DESAI return mm; 1145a0a7ec30SJANAK DESAI 1146a0a7ec30SJANAK DESAI free_pt: 1147801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 1148801460d0SHiroshi Shimamoto mm->binfmt = NULL; 1149a0a7ec30SJANAK DESAI mmput(mm); 1150a0a7ec30SJANAK DESAI 1151a0a7ec30SJANAK DESAI fail_nomem: 1152a0a7ec30SJANAK DESAI return NULL; 1153a0a7ec30SJANAK DESAI } 1154a0a7ec30SJANAK DESAI 11551da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 11581da177e4SLinus Torvalds int retval; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 11611da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 116217406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 116317406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 116417406b82SMandeep Singh Baines #endif 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds tsk->mm = NULL; 11671da177e4SLinus Torvalds tsk->active_mm = NULL; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds /* 11701da177e4SLinus Torvalds * Are we cloning a kernel thread? 11711da177e4SLinus Torvalds * 11721da177e4SLinus Torvalds * We need to steal a active VM for that.. 11731da177e4SLinus Torvalds */ 11741da177e4SLinus Torvalds oldmm = current->mm; 11751da177e4SLinus Torvalds if (!oldmm) 11761da177e4SLinus Torvalds return 0; 11771da177e4SLinus Torvalds 1178615d6e87SDavidlohr Bueso /* initialize the new vmacache entries */ 1179615d6e87SDavidlohr Bueso vmacache_flush(tsk); 1180615d6e87SDavidlohr Bueso 11811da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 11821da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 11831da177e4SLinus Torvalds mm = oldmm; 11841da177e4SLinus Torvalds goto good_mm; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds retval = -ENOMEM; 1188a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 11891da177e4SLinus Torvalds if (!mm) 11901da177e4SLinus Torvalds goto fail_nomem; 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds good_mm: 11931da177e4SLinus Torvalds tsk->mm = mm; 11941da177e4SLinus Torvalds tsk->active_mm = mm; 11951da177e4SLinus Torvalds return 0; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds fail_nomem: 11981da177e4SLinus Torvalds return retval; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 1201a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 12021da177e4SLinus Torvalds { 1203498052bbSAl Viro struct fs_struct *fs = current->fs; 12041da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 1205498052bbSAl Viro /* tsk->fs is already what we want */ 12062a4419b5SNick Piggin spin_lock(&fs->lock); 1207498052bbSAl Viro if (fs->in_exec) { 12082a4419b5SNick Piggin spin_unlock(&fs->lock); 1209498052bbSAl Viro return -EAGAIN; 1210498052bbSAl Viro } 1211498052bbSAl Viro fs->users++; 12122a4419b5SNick Piggin spin_unlock(&fs->lock); 12131da177e4SLinus Torvalds return 0; 12141da177e4SLinus Torvalds } 1215498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 12161da177e4SLinus Torvalds if (!tsk->fs) 12171da177e4SLinus Torvalds return -ENOMEM; 12181da177e4SLinus Torvalds return 0; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 1221a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 1222a016f338SJANAK DESAI { 1223a016f338SJANAK DESAI struct files_struct *oldf, *newf; 1224a016f338SJANAK DESAI int error = 0; 1225a016f338SJANAK DESAI 1226a016f338SJANAK DESAI /* 1227a016f338SJANAK DESAI * A background process may not have any files ... 1228a016f338SJANAK DESAI */ 1229a016f338SJANAK DESAI oldf = current->files; 1230a016f338SJANAK DESAI if (!oldf) 1231a016f338SJANAK DESAI goto out; 1232a016f338SJANAK DESAI 1233a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 1234a016f338SJANAK DESAI atomic_inc(&oldf->count); 1235a016f338SJANAK DESAI goto out; 1236a016f338SJANAK DESAI } 1237a016f338SJANAK DESAI 1238a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 1239a016f338SJANAK DESAI if (!newf) 1240a016f338SJANAK DESAI goto out; 1241a016f338SJANAK DESAI 1242a016f338SJANAK DESAI tsk->files = newf; 1243a016f338SJANAK DESAI error = 0; 1244a016f338SJANAK DESAI out: 1245a016f338SJANAK DESAI return error; 1246a016f338SJANAK DESAI } 1247a016f338SJANAK DESAI 1248fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 1249fd0928dfSJens Axboe { 1250fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 1251fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 12526e736be7STejun Heo struct io_context *new_ioc; 1253fd0928dfSJens Axboe 1254fd0928dfSJens Axboe if (!ioc) 1255fd0928dfSJens Axboe return 0; 1256fadad878SJens Axboe /* 1257fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 1258fadad878SJens Axboe */ 1259fadad878SJens Axboe if (clone_flags & CLONE_IO) { 12603d48749dSTejun Heo ioc_task_link(ioc); 12613d48749dSTejun Heo tsk->io_context = ioc; 1262fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 12636e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 12646e736be7STejun Heo if (unlikely(!new_ioc)) 1265fd0928dfSJens Axboe return -ENOMEM; 1266fd0928dfSJens Axboe 12676e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 126811a3122fSTejun Heo put_io_context(new_ioc); 1269fd0928dfSJens Axboe } 1270fd0928dfSJens Axboe #endif 1271fd0928dfSJens Axboe return 0; 1272fd0928dfSJens Axboe } 1273fd0928dfSJens Axboe 1274a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 12751da177e4SLinus Torvalds { 12761da177e4SLinus Torvalds struct sighand_struct *sig; 12771da177e4SLinus Torvalds 127860348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 12791da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 12801da177e4SLinus Torvalds return 0; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 1283e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 12841da177e4SLinus Torvalds if (!sig) 12851da177e4SLinus Torvalds return -ENOMEM; 12869d7fb042SPeter Zijlstra 12871da177e4SLinus Torvalds atomic_set(&sig->count, 1); 12881da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 12891da177e4SLinus Torvalds return 0; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 1292a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1293c81addc9SOleg Nesterov { 1294d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 1295d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1296392809b2SOleg Nesterov /* 1297392809b2SOleg Nesterov * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it 1298392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1299392809b2SOleg Nesterov */ 1300c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1301c81addc9SOleg Nesterov } 1302d80e731eSOleg Nesterov } 1303c81addc9SOleg Nesterov 1304f06febc9SFrank Mayhar /* 1305f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1306f06febc9SFrank Mayhar */ 1307f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1308f06febc9SFrank Mayhar { 130978d7d407SJiri Slaby unsigned long cpu_limit; 131078d7d407SJiri Slaby 1311316c1608SJason Low cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 131278d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 131378d7d407SJiri Slaby sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); 1314d5c373ebSJason Low sig->cputimer.running = true; 13156279a751SOleg Nesterov } 13166279a751SOleg Nesterov 1317f06febc9SFrank Mayhar /* The timer lists. */ 1318f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1319f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1320f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1321f06febc9SFrank Mayhar } 1322f06febc9SFrank Mayhar 1323a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 13241da177e4SLinus Torvalds { 13251da177e4SLinus Torvalds struct signal_struct *sig; 13261da177e4SLinus Torvalds 13274ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1328490dea45SPeter Zijlstra return 0; 13296279a751SOleg Nesterov 1330a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 13311da177e4SLinus Torvalds tsk->signal = sig; 13321da177e4SLinus Torvalds if (!sig) 13331da177e4SLinus Torvalds return -ENOMEM; 13341da177e4SLinus Torvalds 1335b3ac022cSOleg Nesterov sig->nr_threads = 1; 13361da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1337b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 13380c740d0aSOleg Nesterov 13390c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 13400c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 13410c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 13420c740d0aSOleg Nesterov 13431da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1344db51aeccSOleg Nesterov sig->curr_target = tsk; 13451da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 13461da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 1347e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 13489d7fb042SPeter Zijlstra prev_cputime_init(&sig->prev_cputime); 13491da177e4SLinus Torvalds 1350c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 13511da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds task_lock(current->group_leader); 13541da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 13551da177e4SLinus Torvalds task_unlock(current->group_leader); 13561da177e4SLinus Torvalds 13576279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 13586279a751SOleg Nesterov 1359522ed776SMiloslav Trmac tty_audit_fork(sig); 13605091faa4SMike Galbraith sched_autogroup_fork(sig); 1361522ed776SMiloslav Trmac 1362a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1363dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 136428b83c51SKOSAKI Motohiro 1365ebec18a6SLennart Poettering sig->has_child_subreaper = current->signal->has_child_subreaper || 1366ebec18a6SLennart Poettering current->signal->is_child_subreaper; 1367ebec18a6SLennart Poettering 13689b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 13699b1bf12dSKOSAKI Motohiro 13701da177e4SLinus Torvalds return 0; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 1373dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1374dbd95212SKees Cook { 1375dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1376dbd95212SKees Cook /* 1377dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1378dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1379dbd95212SKees Cook * needed because this new task is not yet running and cannot 1380dbd95212SKees Cook * be racing exec. 1381dbd95212SKees Cook */ 138269f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1383dbd95212SKees Cook 1384dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1385dbd95212SKees Cook get_seccomp_filter(current); 1386dbd95212SKees Cook p->seccomp = current->seccomp; 1387dbd95212SKees Cook 1388dbd95212SKees Cook /* 1389dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1390dbd95212SKees Cook * between the task_struct being duplicated and holding the 1391dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1392dbd95212SKees Cook */ 1393dbd95212SKees Cook if (task_no_new_privs(current)) 1394dbd95212SKees Cook task_set_no_new_privs(p); 1395dbd95212SKees Cook 1396dbd95212SKees Cook /* 1397dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1398dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1399dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1400dbd95212SKees Cook */ 1401dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1402dbd95212SKees Cook set_tsk_thread_flag(p, TIF_SECCOMP); 1403dbd95212SKees Cook #endif 1404dbd95212SKees Cook } 1405dbd95212SKees Cook 140617da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 14071da177e4SLinus Torvalds { 14081da177e4SLinus Torvalds current->clear_child_tid = tidptr; 14091da177e4SLinus Torvalds 1410b488893aSPavel Emelyanov return task_pid_vnr(current); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 1413a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 141423f78d4aSIngo Molnar { 14151d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1416e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1417fb00aca4SPeter Zijlstra p->pi_waiters = RB_ROOT; 1418fb00aca4SPeter Zijlstra p->pi_waiters_leftmost = NULL; 141923f78d4aSIngo Molnar p->pi_blocked_on = NULL; 142023f78d4aSIngo Molnar #endif 142123f78d4aSIngo Molnar } 142223f78d4aSIngo Molnar 14231da177e4SLinus Torvalds /* 1424f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1425f06febc9SFrank Mayhar */ 1426f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1427f06febc9SFrank Mayhar { 142864861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 142964861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1430f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1431f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1432f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1433f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1434f06febc9SFrank Mayhar } 1435f06febc9SFrank Mayhar 143681907739SOleg Nesterov static inline void 143781907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 143881907739SOleg Nesterov { 143981907739SOleg Nesterov task->pids[type].pid = pid; 144081907739SOleg Nesterov } 144181907739SOleg Nesterov 1442f06febc9SFrank Mayhar /* 14431da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 14441da177e4SLinus Torvalds * but does not actually start it yet. 14451da177e4SLinus Torvalds * 14461da177e4SLinus Torvalds * It copies the registers, and all the appropriate 14471da177e4SLinus Torvalds * parts of the process environment (as per the clone 14481da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 14491da177e4SLinus Torvalds */ 14500766f788SEmese Revfy static __latent_entropy struct task_struct *copy_process( 14510766f788SEmese Revfy unsigned long clone_flags, 14521da177e4SLinus Torvalds unsigned long stack_start, 14531da177e4SLinus Torvalds unsigned long stack_size, 14541da177e4SLinus Torvalds int __user *child_tidptr, 145509a05394SRoland McGrath struct pid *pid, 14563033f14aSJosh Triplett int trace, 1457725fc629SAndi Kleen unsigned long tls, 1458725fc629SAndi Kleen int node) 14591da177e4SLinus Torvalds { 14601da177e4SLinus Torvalds int retval; 1461a24efe62SMariusz Kozlowski struct task_struct *p; 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 14641da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 14651da177e4SLinus Torvalds 1466e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1467e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1468e66eded8SEric W. Biederman 14691da177e4SLinus Torvalds /* 14701da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 14711da177e4SLinus Torvalds * can only be started up within the thread group. 14721da177e4SLinus Torvalds */ 14731da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 14741da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds /* 14771da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 14781da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 14791da177e4SLinus Torvalds * for various simplifications in other code. 14801da177e4SLinus Torvalds */ 14811da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 14821da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 14831da177e4SLinus Torvalds 1484123be07bSSukadev Bhattiprolu /* 1485123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1486123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1487123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1488123be07bSSukadev Bhattiprolu * from creating siblings. 1489123be07bSSukadev Bhattiprolu */ 1490123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1491123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1492123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1493123be07bSSukadev Bhattiprolu 14948382fcacSEric W. Biederman /* 149540a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 1496faf00da5SEric W. Biederman * do not allow it to share a thread group with the forking task. 14978382fcacSEric W. Biederman */ 1498faf00da5SEric W. Biederman if (clone_flags & CLONE_THREAD) { 149940a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 150040a0d32dSOleg Nesterov (task_active_pid_ns(current) != 1501c2b1df2eSAndy Lutomirski current->nsproxy->pid_ns_for_children)) 15028382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 150340a0d32dSOleg Nesterov } 15048382fcacSEric W. Biederman 15051da177e4SLinus Torvalds retval = security_task_create(clone_flags); 15061da177e4SLinus Torvalds if (retval) 15071da177e4SLinus Torvalds goto fork_out; 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds retval = -ENOMEM; 1510725fc629SAndi Kleen p = dup_task_struct(current, node); 15111da177e4SLinus Torvalds if (!p) 15121da177e4SLinus Torvalds goto fork_out; 15131da177e4SLinus Torvalds 1514f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1515f7e8b616SSteven Rostedt 1516bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1517bea493a0SPeter Zijlstra 1518d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1519de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1520de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1521de30a2b3SIngo Molnar #endif 15221da177e4SLinus Torvalds retval = -EAGAIN; 15233b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 152478d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1525b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1526b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 15271da177e4SLinus Torvalds goto bad_fork_free; 15281da177e4SLinus Torvalds } 152972fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 15301da177e4SLinus Torvalds 1531f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1532f1752eecSDavid Howells if (retval < 0) 1533f1752eecSDavid Howells goto bad_fork_free; 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds /* 15361da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 15371da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 15381da177e4SLinus Torvalds * to stop root fork bombs. 15391da177e4SLinus Torvalds */ 154004ec93feSLi Zefan retval = -EAGAIN; 15411da177e4SLinus Torvalds if (nr_threads >= max_threads) 15421da177e4SLinus Torvalds goto bad_fork_cleanup_count; 15431da177e4SLinus Torvalds 1544ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1545514ddb44SDavid Rientjes p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER); 1546514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 15471da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 15481da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1549f41d911fSPaul E. McKenney rcu_copy_process(p); 15501da177e4SLinus Torvalds p->vfork_done = NULL; 15511da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds init_sigpending(&p->pending); 15541da177e4SLinus Torvalds 155564861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 155640565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 155764861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 155840565b5aSStanislaw Gruszka #endif 15599d7fb042SPeter Zijlstra prev_cputime_init(&p->prev_cputime); 15609d7fb042SPeter Zijlstra 15616a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 1562b7ce2277SFrederic Weisbecker seqcount_init(&p->vtime_seqcount); 15636a61671bSFrederic Weisbecker p->vtime_snap = 0; 15647098c1eaSFrederic Weisbecker p->vtime_snap_whence = VTIME_INACTIVE; 15656a61671bSFrederic Weisbecker #endif 15666a61671bSFrederic Weisbecker 1567a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1568a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1569a3a2e76cSKAMEZAWA Hiroyuki #endif 1570172ba844SBalbir Singh 15716976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 15726976675dSArjan van de Ven 15735995477aSAndrea Righi task_io_accounting_init(&p->ioac); 15741da177e4SLinus Torvalds acct_clear_integrals(p); 15751da177e4SLinus Torvalds 1576f06febc9SFrank Mayhar posix_cpu_timers_init(p); 15771da177e4SLinus Torvalds 1578ccbf62d8SThomas Gleixner p->start_time = ktime_get_ns(); 157957e0be04SThomas Gleixner p->real_start_time = ktime_get_boot_ns(); 15801da177e4SLinus Torvalds p->io_context = NULL; 15811da177e4SLinus Torvalds p->audit_context = NULL; 1582b4f48b63SPaul Menage cgroup_fork(p); 15831da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1584846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 15851da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 15861da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 15871da177e4SLinus Torvalds p->mempolicy = NULL; 1588e8604cb4SLi Zefan goto bad_fork_cleanup_threadgroup_lock; 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds #endif 1591778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1592778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1593778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1594cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1595778d3b0fSMichal Hocko #endif 1596de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1597de30a2b3SIngo Molnar p->irq_events = 0; 1598de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1599de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1600de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1601de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1602de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1603de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1604de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1605de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1606de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1607de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1608de30a2b3SIngo Molnar p->hardirq_context = 0; 1609de30a2b3SIngo Molnar p->softirq_context = 0; 1610de30a2b3SIngo Molnar #endif 16118bcbde54SDavid Hildenbrand 16128bcbde54SDavid Hildenbrand p->pagefault_disabled = 0; 16138bcbde54SDavid Hildenbrand 1614fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1615fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1616fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1617fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1618fbb9ce95SIngo Molnar #endif 16191da177e4SLinus Torvalds 1620408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1621408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1622408894eeSIngo Molnar #endif 1623cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 1624cafe5635SKent Overstreet p->sequential_io = 0; 1625cafe5635SKent Overstreet p->sequential_io_avg = 0; 1626cafe5635SKent Overstreet #endif 16270f481406SMarkus Metzger 16283c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 1629aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 1630aab03e05SDario Faggioli if (retval) 1631aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 16326ab423e0SPeter Zijlstra 1633cdd6c482SIngo Molnar retval = perf_event_init_task(p); 16346ab423e0SPeter Zijlstra if (retval) 16356ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1636fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1637fb0a685cSDaniel Rebelo de Oliveira if (retval) 16386c72e350SPeter Zijlstra goto bad_fork_cleanup_perf; 16391da177e4SLinus Torvalds /* copy all the process information */ 1640ab602f79SJack Miller shm_init_task(p); 1641fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1642fb0a685cSDaniel Rebelo de Oliveira if (retval) 16431da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1644fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1645fb0a685cSDaniel Rebelo de Oliveira if (retval) 16461da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1647fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1648fb0a685cSDaniel Rebelo de Oliveira if (retval) 16491da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1650fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1651fb0a685cSDaniel Rebelo de Oliveira if (retval) 16521da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1653fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1654fb0a685cSDaniel Rebelo de Oliveira if (retval) 16551da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1656fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1657fb0a685cSDaniel Rebelo de Oliveira if (retval) 16581da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1659fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1660fb0a685cSDaniel Rebelo de Oliveira if (retval) 1661d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1662fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1663fb0a685cSDaniel Rebelo de Oliveira if (retval) 1664fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 16653033f14aSJosh Triplett retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls); 16661da177e4SLinus Torvalds if (retval) 1667fd0928dfSJens Axboe goto bad_fork_cleanup_io; 16681da177e4SLinus Torvalds 1669425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1670c2b1df2eSAndy Lutomirski pid = alloc_pid(p->nsproxy->pid_ns_for_children); 167135f71bc0SMichal Hocko if (IS_ERR(pid)) { 167235f71bc0SMichal Hocko retval = PTR_ERR(pid); 16730740aa5fSJiri Slaby goto bad_fork_cleanup_thread; 1674425fb2b4SPavel Emelyanov } 167535f71bc0SMichal Hocko } 1676425fb2b4SPavel Emelyanov 16771da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 16781da177e4SLinus Torvalds /* 16791da177e4SLinus Torvalds * Clear TID on mm_release()? 16801da177e4SLinus Torvalds */ 16811da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 168273c10101SJens Axboe #ifdef CONFIG_BLOCK 168373c10101SJens Axboe p->plug = NULL; 168473c10101SJens Axboe #endif 168542b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 16868f17d3a5SIngo Molnar p->robust_list = NULL; 16878f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 16888f17d3a5SIngo Molnar p->compat_robust_list = NULL; 16898f17d3a5SIngo Molnar #endif 1690c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1691c87e2837SIngo Molnar p->pi_state_cache = NULL; 169242b2dd0aSAlexey Dobriyan #endif 16931da177e4SLinus Torvalds /* 1694f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1695f9a3879aSGOTO Masanori */ 1696f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 16972a742138SStas Sergeev sas_ss_reset(p); 1698f9a3879aSGOTO Masanori 1699f9a3879aSGOTO Masanori /* 17006580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 17016580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 17021da177e4SLinus Torvalds */ 17036580807dSOleg Nesterov user_disable_single_step(p); 17041da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1705ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1706ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1707ed75e8d5SLaurent Vivier #endif 17089745512cSArjan van de Ven clear_all_latency_tracing(p); 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds /* ok, now we should be set up.. */ 171118c830dfSOleg Nesterov p->pid = pid_nr(pid); 171218c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 17135f8aadd8SOleg Nesterov p->exit_signal = -1; 171418c830dfSOleg Nesterov p->group_leader = current->group_leader; 171518c830dfSOleg Nesterov p->tgid = current->tgid; 171618c830dfSOleg Nesterov } else { 171718c830dfSOleg Nesterov if (clone_flags & CLONE_PARENT) 17185f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 17195f8aadd8SOleg Nesterov else 17205f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 172118c830dfSOleg Nesterov p->group_leader = p; 172218c830dfSOleg Nesterov p->tgid = p->pid; 172318c830dfSOleg Nesterov } 17245f8aadd8SOleg Nesterov 17259d823e8fSWu Fengguang p->nr_dirtied = 0; 17269d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 172783712358SWu Fengguang p->dirty_paused_when = 0; 17289d823e8fSWu Fengguang 1729bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 173047e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 1731158e1645SAl Viro p->task_works = NULL; 17321da177e4SLinus Torvalds 1733568ac888SBalbir Singh threadgroup_change_begin(current); 173418c830dfSOleg Nesterov /* 17357e47682eSAleksa Sarai * Ensure that the cgroup subsystem policies allow the new process to be 17367e47682eSAleksa Sarai * forked. It should be noted the the new process's css_set can be changed 17377e47682eSAleksa Sarai * between here and cgroup_post_fork() if an organisation operation is in 17387e47682eSAleksa Sarai * progress. 17397e47682eSAleksa Sarai */ 1740b53202e6SOleg Nesterov retval = cgroup_can_fork(p); 17417e47682eSAleksa Sarai if (retval) 17427e47682eSAleksa Sarai goto bad_fork_free_pid; 17437e47682eSAleksa Sarai 17447e47682eSAleksa Sarai /* 174518c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 174618c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 174718c830dfSOleg Nesterov */ 17481da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 17512d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 17521da177e4SLinus Torvalds p->real_parent = current->real_parent; 17532d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 17542d5516cbSOleg Nesterov } else { 17551da177e4SLinus Torvalds p->real_parent = current; 17562d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 17572d5516cbSOleg Nesterov } 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 17604a2c7a78SOleg Nesterov 17614a2c7a78SOleg Nesterov /* 1762dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 1763dbd95212SKees Cook * before holding sighand lock. 1764dbd95212SKees Cook */ 1765dbd95212SKees Cook copy_seccomp(p); 1766dbd95212SKees Cook 1767dbd95212SKees Cook /* 17684a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 17694a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 17704a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 17714a2c7a78SOleg Nesterov * it's process group. 17724a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 17734a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 17744a2c7a78SOleg Nesterov */ 17754a2c7a78SOleg Nesterov recalc_sigpending(); 17764a2c7a78SOleg Nesterov if (signal_pending(current)) { 17774a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 17784a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 17794a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 17807e47682eSAleksa Sarai goto bad_fork_cancel_cgroup; 17814a2c7a78SOleg Nesterov } 17824a2c7a78SOleg Nesterov 178373b9ebfeSOleg Nesterov if (likely(p->pid)) { 17844b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 17851da177e4SLinus Torvalds 178681907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 17871da177e4SLinus Torvalds if (thread_group_leader(p)) { 178881907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 178981907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 179081907739SOleg Nesterov 17911c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 179217cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 17931c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 17941c4042c2SEric W. Biederman } 17955cd17569SEric W. Biederman 1796fea9d175SOleg Nesterov p->signal->leader_pid = pid; 17979c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 17989cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 17995e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 180081907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 180181907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 1802909ea964SChristoph Lameter __this_cpu_inc(process_counts); 180380628ca0SOleg Nesterov } else { 180480628ca0SOleg Nesterov current->signal->nr_threads++; 180580628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 180680628ca0SOleg Nesterov atomic_inc(¤t->signal->sigcnt); 180780628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 180880628ca0SOleg Nesterov &p->group_leader->thread_group); 18090c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 18100c740d0aSOleg Nesterov &p->signal->thread_head); 18111da177e4SLinus Torvalds } 181281907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 18131da177e4SLinus Torvalds nr_threads++; 181473b9ebfeSOleg Nesterov } 181573b9ebfeSOleg Nesterov 18161da177e4SLinus Torvalds total_forks++; 18173f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 18184af4206bSOleg Nesterov syscall_tracepoint_update(p); 18191da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 18204af4206bSOleg Nesterov 1821c13cf856SAndrew Morton proc_fork_connector(p); 1822b53202e6SOleg Nesterov cgroup_post_fork(p); 1823257058aeSTejun Heo threadgroup_change_end(current); 1824cdd6c482SIngo Molnar perf_event_fork(p); 182543d2b113SKAMEZAWA Hiroyuki 182643d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 18273ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 182843d2b113SKAMEZAWA Hiroyuki 18291da177e4SLinus Torvalds return p; 18301da177e4SLinus Torvalds 18317e47682eSAleksa Sarai bad_fork_cancel_cgroup: 1832b53202e6SOleg Nesterov cgroup_cancel_fork(p); 1833425fb2b4SPavel Emelyanov bad_fork_free_pid: 1834568ac888SBalbir Singh threadgroup_change_end(current); 1835425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1836425fb2b4SPavel Emelyanov free_pid(pid); 18370740aa5fSJiri Slaby bad_fork_cleanup_thread: 18380740aa5fSJiri Slaby exit_thread(p); 1839fd0928dfSJens Axboe bad_fork_cleanup_io: 1840b69f2292SLouis Rilling if (p->io_context) 1841b69f2292SLouis Rilling exit_io_context(p); 1842ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1843444f378bSLinus Torvalds exit_task_namespaces(p); 18441da177e4SLinus Torvalds bad_fork_cleanup_mm: 1845c9f01245SDavid Rientjes if (p->mm) 18461da177e4SLinus Torvalds mmput(p->mm); 18471da177e4SLinus Torvalds bad_fork_cleanup_signal: 18484ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 18491c5354deSMike Galbraith free_signal_struct(p->signal); 18501da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1851a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 18521da177e4SLinus Torvalds bad_fork_cleanup_fs: 18531da177e4SLinus Torvalds exit_fs(p); /* blocking */ 18541da177e4SLinus Torvalds bad_fork_cleanup_files: 18551da177e4SLinus Torvalds exit_files(p); /* blocking */ 18561da177e4SLinus Torvalds bad_fork_cleanup_semundo: 18571da177e4SLinus Torvalds exit_sem(p); 18581da177e4SLinus Torvalds bad_fork_cleanup_audit: 18591da177e4SLinus Torvalds audit_free(p); 18606c72e350SPeter Zijlstra bad_fork_cleanup_perf: 1861cdd6c482SIngo Molnar perf_event_free_task(p); 18626c72e350SPeter Zijlstra bad_fork_cleanup_policy: 18631da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1864f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1865e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock: 18661da177e4SLinus Torvalds #endif 186735df17c5SShailabh Nagar delayacct_tsk_free(p); 18681da177e4SLinus Torvalds bad_fork_cleanup_count: 1869d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1870e0e81739SDavid Howells exit_creds(p); 18711da177e4SLinus Torvalds bad_fork_free: 1872405c0759SAndy Lutomirski p->state = TASK_DEAD; 187368f24b08SAndy Lutomirski put_task_stack(p); 18741da177e4SLinus Torvalds free_task(p); 1875fe7d37d1SOleg Nesterov fork_out: 1876fe7d37d1SOleg Nesterov return ERR_PTR(retval); 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 1879f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1880f106eee1SOleg Nesterov { 1881f106eee1SOleg Nesterov enum pid_type type; 1882f106eee1SOleg Nesterov 1883f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1884f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1885f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1886f106eee1SOleg Nesterov } 1887f106eee1SOleg Nesterov } 1888f106eee1SOleg Nesterov 18890db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 18901da177e4SLinus Torvalds { 189136c8b586SIngo Molnar struct task_struct *task; 1892725fc629SAndi Kleen task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0, 1893725fc629SAndi Kleen cpu_to_node(cpu)); 1894f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1895f106eee1SOleg Nesterov init_idle_pids(task->pids); 18961da177e4SLinus Torvalds init_idle(task, cpu); 1897f106eee1SOleg Nesterov } 189873b9ebfeSOleg Nesterov 18991da177e4SLinus Torvalds return task; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds /* 19031da177e4SLinus Torvalds * Ok, this is the main fork-routine. 19041da177e4SLinus Torvalds * 19051da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 19061da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 19071da177e4SLinus Torvalds */ 19083033f14aSJosh Triplett long _do_fork(unsigned long clone_flags, 19091da177e4SLinus Torvalds unsigned long stack_start, 19101da177e4SLinus Torvalds unsigned long stack_size, 19111da177e4SLinus Torvalds int __user *parent_tidptr, 19123033f14aSJosh Triplett int __user *child_tidptr, 19133033f14aSJosh Triplett unsigned long tls) 19141da177e4SLinus Torvalds { 19151da177e4SLinus Torvalds struct task_struct *p; 19161da177e4SLinus Torvalds int trace = 0; 191792476d7fSEric W. Biederman long nr; 19181da177e4SLinus Torvalds 1919bdff746aSAndrew Morton /* 19204b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 19214b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 19224b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 19234b9d33e6STejun Heo * for the type of forking is enabled. 192409a05394SRoland McGrath */ 1925e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 19264b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 19274b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 19284b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 19294b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 19304b9d33e6STejun Heo else 19314b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 19324b9d33e6STejun Heo 19334b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 19344b9d33e6STejun Heo trace = 0; 19354b9d33e6STejun Heo } 19361da177e4SLinus Torvalds 193762e791c1SAl Viro p = copy_process(clone_flags, stack_start, stack_size, 1938725fc629SAndi Kleen child_tidptr, NULL, trace, tls, NUMA_NO_NODE); 193938addce8SEmese Revfy add_latent_entropy(); 19401da177e4SLinus Torvalds /* 19411da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 19421da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 19431da177e4SLinus Torvalds */ 19441da177e4SLinus Torvalds if (!IS_ERR(p)) { 19451da177e4SLinus Torvalds struct completion vfork; 19464e52365fSMatthew Dempsky struct pid *pid; 19471da177e4SLinus Torvalds 19480a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 19490a16b607SMathieu Desnoyers 19504e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 19514e52365fSMatthew Dempsky nr = pid_vnr(pid); 195230e49c26SPavel Emelyanov 195330e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 195430e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1955a6f5e063SSukadev Bhattiprolu 19561da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 19571da177e4SLinus Torvalds p->vfork_done = &vfork; 19581da177e4SLinus Torvalds init_completion(&vfork); 1959d68b46feSOleg Nesterov get_task_struct(p); 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds 19623e51e3edSSamir Bellabes wake_up_new_task(p); 19631da177e4SLinus Torvalds 19644b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 19654b9d33e6STejun Heo if (unlikely(trace)) 19664e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 196709a05394SRoland McGrath 19681da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1969d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 19704e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 19719f59ce5dSChuck Ebbert } 19724e52365fSMatthew Dempsky 19734e52365fSMatthew Dempsky put_pid(pid); 19741da177e4SLinus Torvalds } else { 197592476d7fSEric W. Biederman nr = PTR_ERR(p); 19761da177e4SLinus Torvalds } 197792476d7fSEric W. Biederman return nr; 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 19803033f14aSJosh Triplett #ifndef CONFIG_HAVE_COPY_THREAD_TLS 19813033f14aSJosh Triplett /* For compatibility with architectures that call do_fork directly rather than 19823033f14aSJosh Triplett * using the syscall entry points below. */ 19833033f14aSJosh Triplett long do_fork(unsigned long clone_flags, 19843033f14aSJosh Triplett unsigned long stack_start, 19853033f14aSJosh Triplett unsigned long stack_size, 19863033f14aSJosh Triplett int __user *parent_tidptr, 19873033f14aSJosh Triplett int __user *child_tidptr) 19883033f14aSJosh Triplett { 19893033f14aSJosh Triplett return _do_fork(clone_flags, stack_start, stack_size, 19903033f14aSJosh Triplett parent_tidptr, child_tidptr, 0); 19913033f14aSJosh Triplett } 19923033f14aSJosh Triplett #endif 19933033f14aSJosh Triplett 19942aa3a7f8SAl Viro /* 19952aa3a7f8SAl Viro * Create a kernel thread. 19962aa3a7f8SAl Viro */ 19972aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 19982aa3a7f8SAl Viro { 19993033f14aSJosh Triplett return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, 20003033f14aSJosh Triplett (unsigned long)arg, NULL, NULL, 0); 20012aa3a7f8SAl Viro } 20022aa3a7f8SAl Viro 2003d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 2004d2125043SAl Viro SYSCALL_DEFINE0(fork) 2005d2125043SAl Viro { 2006d2125043SAl Viro #ifdef CONFIG_MMU 20073033f14aSJosh Triplett return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0); 2008d2125043SAl Viro #else 2009d2125043SAl Viro /* can not support in nommu mode */ 20105d59e182SDaeseok Youn return -EINVAL; 2011d2125043SAl Viro #endif 2012d2125043SAl Viro } 2013d2125043SAl Viro #endif 2014d2125043SAl Viro 2015d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 2016d2125043SAl Viro SYSCALL_DEFINE0(vfork) 2017d2125043SAl Viro { 20183033f14aSJosh Triplett return _do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 20193033f14aSJosh Triplett 0, NULL, NULL, 0); 2020d2125043SAl Viro } 2021d2125043SAl Viro #endif 2022d2125043SAl Viro 2023d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 2024d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 2025d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2026d2125043SAl Viro int __user *, parent_tidptr, 20273033f14aSJosh Triplett unsigned long, tls, 2028d2125043SAl Viro int __user *, child_tidptr) 2029d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 2030d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 2031d2125043SAl Viro int __user *, parent_tidptr, 2032d2125043SAl Viro int __user *, child_tidptr, 20333033f14aSJosh Triplett unsigned long, tls) 2034dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 2035dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 2036dfa9771aSMichal Simek int, stack_size, 2037dfa9771aSMichal Simek int __user *, parent_tidptr, 2038dfa9771aSMichal Simek int __user *, child_tidptr, 20393033f14aSJosh Triplett unsigned long, tls) 2040d2125043SAl Viro #else 2041d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2042d2125043SAl Viro int __user *, parent_tidptr, 2043d2125043SAl Viro int __user *, child_tidptr, 20443033f14aSJosh Triplett unsigned long, tls) 2045d2125043SAl Viro #endif 2046d2125043SAl Viro { 20473033f14aSJosh Triplett return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls); 2048d2125043SAl Viro } 2049d2125043SAl Viro #endif 2050d2125043SAl Viro 20515fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 20525fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 20535fd63b30SRavikiran G Thirumalai #endif 20545fd63b30SRavikiran G Thirumalai 205551cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 2056aa1757f9SOleg Nesterov { 2057aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 2058aa1757f9SOleg Nesterov 2059aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 2060b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 2061fba2afaaSDavide Libenzi } 2062aa1757f9SOleg Nesterov 20631da177e4SLinus Torvalds void __init proc_caches_init(void) 20641da177e4SLinus Torvalds { 20651da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 20661da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 20672dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 20685d097056SVladimir Davydov SLAB_NOTRACK|SLAB_ACCOUNT, sighand_ctor); 20691da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 20701da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 20715d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 20725d097056SVladimir Davydov NULL); 20731da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 20741da177e4SLinus Torvalds sizeof(struct files_struct), 0, 20755d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 20765d097056SVladimir Davydov NULL); 20771da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 20781da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 20795d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 20805d097056SVladimir Davydov NULL); 20816345d24dSLinus Torvalds /* 20826345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 20836345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 20846345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 20856345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 20866345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 20876345d24dSLinus Torvalds */ 20881da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 20895fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 20905d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 20915d097056SVladimir Davydov NULL); 20925d097056SVladimir Davydov vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 20938feae131SDavid Howells mmap_init(); 209466577193SAl Viro nsproxy_cache_init(); 20951da177e4SLinus Torvalds } 2096cf2e340fSJANAK DESAI 2097cf2e340fSJANAK DESAI /* 20989bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 2099cf2e340fSJANAK DESAI */ 21009bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 2101cf2e340fSJANAK DESAI { 21029bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 21039bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 210450804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 2105a79a908fSAditya Kali CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP)) 2106cf2e340fSJANAK DESAI return -EINVAL; 21079bfb23fcSOleg Nesterov /* 210812c641abSEric W. Biederman * Not implemented, but pretend it works if there is nothing 210912c641abSEric W. Biederman * to unshare. Note that unsharing the address space or the 211012c641abSEric W. Biederman * signal handlers also need to unshare the signal queues (aka 211112c641abSEric W. Biederman * CLONE_THREAD). 21129bfb23fcSOleg Nesterov */ 21139bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 211412c641abSEric W. Biederman if (!thread_group_empty(current)) 211512c641abSEric W. Biederman return -EINVAL; 211612c641abSEric W. Biederman } 211712c641abSEric W. Biederman if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 211812c641abSEric W. Biederman if (atomic_read(¤t->sighand->count) > 1) 211912c641abSEric W. Biederman return -EINVAL; 212012c641abSEric W. Biederman } 212112c641abSEric W. Biederman if (unshare_flags & CLONE_VM) { 212212c641abSEric W. Biederman if (!current_is_single_threaded()) 21239bfb23fcSOleg Nesterov return -EINVAL; 21249bfb23fcSOleg Nesterov } 2125cf2e340fSJANAK DESAI 2126cf2e340fSJANAK DESAI return 0; 2127cf2e340fSJANAK DESAI } 2128cf2e340fSJANAK DESAI 2129cf2e340fSJANAK DESAI /* 213099d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 2131cf2e340fSJANAK DESAI */ 2132cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 2133cf2e340fSJANAK DESAI { 2134cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 2135cf2e340fSJANAK DESAI 2136498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 2137498052bbSAl Viro return 0; 2138498052bbSAl Viro 2139498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 2140498052bbSAl Viro if (fs->users == 1) 2141498052bbSAl Viro return 0; 2142498052bbSAl Viro 2143498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 214499d1419dSJANAK DESAI if (!*new_fsp) 214599d1419dSJANAK DESAI return -ENOMEM; 2146cf2e340fSJANAK DESAI 2147cf2e340fSJANAK DESAI return 0; 2148cf2e340fSJANAK DESAI } 2149cf2e340fSJANAK DESAI 2150cf2e340fSJANAK DESAI /* 2151a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 2152cf2e340fSJANAK DESAI */ 2153cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 2154cf2e340fSJANAK DESAI { 2155cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 2156a016f338SJANAK DESAI int error = 0; 2157cf2e340fSJANAK DESAI 2158cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 2159a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 2160a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 2161a016f338SJANAK DESAI if (!*new_fdp) 2162a016f338SJANAK DESAI return error; 2163a016f338SJANAK DESAI } 2164cf2e340fSJANAK DESAI 2165cf2e340fSJANAK DESAI return 0; 2166cf2e340fSJANAK DESAI } 2167cf2e340fSJANAK DESAI 2168cf2e340fSJANAK DESAI /* 2169cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 2170cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 2171cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 2172cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 2173cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 2174cf2e340fSJANAK DESAI * task_struct. 2175cf2e340fSJANAK DESAI */ 21766559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 2177cf2e340fSJANAK DESAI { 2178cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 2179cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 2180b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 2181cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 21829edff4abSManfred Spraul int do_sysvsem = 0; 21839bfb23fcSOleg Nesterov int err; 2184cf2e340fSJANAK DESAI 218550804fe3SEric W. Biederman /* 2186faf00da5SEric W. Biederman * If unsharing a user namespace must also unshare the thread group 2187faf00da5SEric W. Biederman * and unshare the filesystem root and working directories. 2188b2e0d987SEric W. Biederman */ 2189b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 2190e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 2191b2e0d987SEric W. Biederman /* 219250804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 219350804fe3SEric W. Biederman */ 219450804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 219550804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 21966013f67fSManfred Spraul /* 219712c641abSEric W. Biederman * If unsharing a signal handlers, must also unshare the signal queues. 219812c641abSEric W. Biederman */ 219912c641abSEric W. Biederman if (unshare_flags & CLONE_SIGHAND) 220012c641abSEric W. Biederman unshare_flags |= CLONE_THREAD; 220112c641abSEric W. Biederman /* 22029bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 22039bfb23fcSOleg Nesterov */ 22049bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 22059bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 220650804fe3SEric W. Biederman 220750804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 220850804fe3SEric W. Biederman if (err) 220950804fe3SEric W. Biederman goto bad_unshare_out; 22109bfb23fcSOleg Nesterov /* 22116013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 22126013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 22136013f67fSManfred Spraul * namespace are unreachable. 22146013f67fSManfred Spraul */ 22156013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 22169edff4abSManfred Spraul do_sysvsem = 1; 2217fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 2218fb0a685cSDaniel Rebelo de Oliveira if (err) 22199bfb23fcSOleg Nesterov goto bad_unshare_out; 2220fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 2221fb0a685cSDaniel Rebelo de Oliveira if (err) 22229bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 2223b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 2224fb0a685cSDaniel Rebelo de Oliveira if (err) 22259edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 2226b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 2227b2e0d987SEric W. Biederman new_cred, new_fs); 2228b2e0d987SEric W. Biederman if (err) 2229b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 2230cf2e340fSJANAK DESAI 2231b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 22329edff4abSManfred Spraul if (do_sysvsem) { 22339edff4abSManfred Spraul /* 22349edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 22359edff4abSManfred Spraul */ 22369edff4abSManfred Spraul exit_sem(current); 22379edff4abSManfred Spraul } 2238ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 2239ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 2240ab602f79SJack Miller exit_shm(current); 2241ab602f79SJack Miller shm_init_task(current); 2242ab602f79SJack Miller } 2243ab516013SSerge E. Hallyn 22446f977e6bSAlan Cox if (new_nsproxy) 2245cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 2246cf2e340fSJANAK DESAI 2247cf7b708cSPavel Emelyanov task_lock(current); 2248cf7b708cSPavel Emelyanov 2249cf2e340fSJANAK DESAI if (new_fs) { 2250cf2e340fSJANAK DESAI fs = current->fs; 22512a4419b5SNick Piggin spin_lock(&fs->lock); 2252cf2e340fSJANAK DESAI current->fs = new_fs; 2253498052bbSAl Viro if (--fs->users) 2254498052bbSAl Viro new_fs = NULL; 2255498052bbSAl Viro else 2256cf2e340fSJANAK DESAI new_fs = fs; 22572a4419b5SNick Piggin spin_unlock(&fs->lock); 2258cf2e340fSJANAK DESAI } 2259cf2e340fSJANAK DESAI 2260cf2e340fSJANAK DESAI if (new_fd) { 2261cf2e340fSJANAK DESAI fd = current->files; 2262cf2e340fSJANAK DESAI current->files = new_fd; 2263cf2e340fSJANAK DESAI new_fd = fd; 2264cf2e340fSJANAK DESAI } 2265cf2e340fSJANAK DESAI 2266cf2e340fSJANAK DESAI task_unlock(current); 2267b2e0d987SEric W. Biederman 2268b2e0d987SEric W. Biederman if (new_cred) { 2269b2e0d987SEric W. Biederman /* Install the new user namespace */ 2270b2e0d987SEric W. Biederman commit_creds(new_cred); 2271b2e0d987SEric W. Biederman new_cred = NULL; 2272b2e0d987SEric W. Biederman } 2273cf2e340fSJANAK DESAI } 2274cf2e340fSJANAK DESAI 2275b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 2276b2e0d987SEric W. Biederman if (new_cred) 2277b2e0d987SEric W. Biederman put_cred(new_cred); 2278cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 2279cf2e340fSJANAK DESAI if (new_fd) 2280cf2e340fSJANAK DESAI put_files_struct(new_fd); 2281cf2e340fSJANAK DESAI 2282cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 2283cf2e340fSJANAK DESAI if (new_fs) 2284498052bbSAl Viro free_fs_struct(new_fs); 2285cf2e340fSJANAK DESAI 2286cf2e340fSJANAK DESAI bad_unshare_out: 2287cf2e340fSJANAK DESAI return err; 2288cf2e340fSJANAK DESAI } 22893b125388SAl Viro 22903b125388SAl Viro /* 22913b125388SAl Viro * Helper to unshare the files of the current task. 22923b125388SAl Viro * We don't want to expose copy_files internals to 22933b125388SAl Viro * the exec layer of the kernel. 22943b125388SAl Viro */ 22953b125388SAl Viro 22963b125388SAl Viro int unshare_files(struct files_struct **displaced) 22973b125388SAl Viro { 22983b125388SAl Viro struct task_struct *task = current; 229950704516SAl Viro struct files_struct *copy = NULL; 23003b125388SAl Viro int error; 23013b125388SAl Viro 23023b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 23033b125388SAl Viro if (error || !copy) { 23043b125388SAl Viro *displaced = NULL; 23053b125388SAl Viro return error; 23063b125388SAl Viro } 23073b125388SAl Viro *displaced = task->files; 23083b125388SAl Viro task_lock(task); 23093b125388SAl Viro task->files = copy; 23103b125388SAl Viro task_unlock(task); 23113b125388SAl Viro return 0; 23123b125388SAl Viro } 231316db3d3fSHeinrich Schuchardt 231416db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write, 231516db3d3fSHeinrich Schuchardt void __user *buffer, size_t *lenp, loff_t *ppos) 231616db3d3fSHeinrich Schuchardt { 231716db3d3fSHeinrich Schuchardt struct ctl_table t; 231816db3d3fSHeinrich Schuchardt int ret; 231916db3d3fSHeinrich Schuchardt int threads = max_threads; 232016db3d3fSHeinrich Schuchardt int min = MIN_THREADS; 232116db3d3fSHeinrich Schuchardt int max = MAX_THREADS; 232216db3d3fSHeinrich Schuchardt 232316db3d3fSHeinrich Schuchardt t = *table; 232416db3d3fSHeinrich Schuchardt t.data = &threads; 232516db3d3fSHeinrich Schuchardt t.extra1 = &min; 232616db3d3fSHeinrich Schuchardt t.extra2 = &max; 232716db3d3fSHeinrich Schuchardt 232816db3d3fSHeinrich Schuchardt ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 232916db3d3fSHeinrich Schuchardt if (ret || !write) 233016db3d3fSHeinrich Schuchardt return ret; 233116db3d3fSHeinrich Schuchardt 233216db3d3fSHeinrich Schuchardt set_max_threads(threads); 233316db3d3fSHeinrich Schuchardt 233416db3d3fSHeinrich Schuchardt return 0; 233516db3d3fSHeinrich Schuchardt } 2336