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> 154eb5aaa3SIngo Molnar #include <linux/sched/autogroup.h> 166e84f315SIngo Molnar #include <linux/sched/mm.h> 17f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 188703e8a4SIngo Molnar #include <linux/sched/user.h> 196a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 2003441a34SIngo Molnar #include <linux/sched/stat.h> 2129930025SIngo Molnar #include <linux/sched/task.h> 2268db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 2332ef5517SIngo Molnar #include <linux/sched/cputime.h> 24037741a6SIngo Molnar #include <linux/rtmutex.h> 251da177e4SLinus Torvalds #include <linux/init.h> 261da177e4SLinus Torvalds #include <linux/unistd.h> 271da177e4SLinus Torvalds #include <linux/module.h> 281da177e4SLinus Torvalds #include <linux/vmalloc.h> 291da177e4SLinus Torvalds #include <linux/completion.h> 301da177e4SLinus Torvalds #include <linux/personality.h> 311da177e4SLinus Torvalds #include <linux/mempolicy.h> 321da177e4SLinus Torvalds #include <linux/sem.h> 331da177e4SLinus Torvalds #include <linux/file.h> 349f3acc31SAl Viro #include <linux/fdtable.h> 35da9cbc87SJens Axboe #include <linux/iocontext.h> 361da177e4SLinus Torvalds #include <linux/key.h> 371da177e4SLinus Torvalds #include <linux/binfmts.h> 381da177e4SLinus Torvalds #include <linux/mman.h> 39cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 401da177e4SLinus Torvalds #include <linux/fs.h> 41615d6e87SDavidlohr Bueso #include <linux/mm.h> 42615d6e87SDavidlohr Bueso #include <linux/vmacache.h> 43ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 44c59ede7bSRandy.Dunlap #include <linux/capability.h> 451da177e4SLinus Torvalds #include <linux/cpu.h> 46b4f48b63SPaul Menage #include <linux/cgroup.h> 471da177e4SLinus Torvalds #include <linux/security.h> 48a1e78772SMel Gorman #include <linux/hugetlb.h> 49e2cfabdfSWill Drewry #include <linux/seccomp.h> 501da177e4SLinus Torvalds #include <linux/swap.h> 511da177e4SLinus Torvalds #include <linux/syscalls.h> 521da177e4SLinus Torvalds #include <linux/jiffies.h> 531da177e4SLinus Torvalds #include <linux/futex.h> 548141c7f3SLinus Torvalds #include <linux/compat.h> 55207205a2SEric Dumazet #include <linux/kthread.h> 567c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 57ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 581da177e4SLinus Torvalds #include <linux/ptrace.h> 591da177e4SLinus Torvalds #include <linux/mount.h> 601da177e4SLinus Torvalds #include <linux/audit.h> 6178fb7466SPavel Emelianov #include <linux/memcontrol.h> 62f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 635e2bf014SMike Galbraith #include <linux/proc_fs.h> 641da177e4SLinus Torvalds #include <linux/profile.h> 651da177e4SLinus Torvalds #include <linux/rmap.h> 66f8af4da3SHugh Dickins #include <linux/ksm.h> 671da177e4SLinus Torvalds #include <linux/acct.h> 68893e26e6SPavel Emelyanov #include <linux/userfaultfd_k.h> 698f0ab514SJay Lan #include <linux/tsacct_kern.h> 709f46080cSMatt Helsley #include <linux/cn_proc.h> 71ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 72ca74e92bSShailabh Nagar #include <linux/delayacct.h> 73ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 740a425405SArjan van de Ven #include <linux/random.h> 75522ed776SMiloslav Trmac #include <linux/tty.h> 76fd0928dfSJens Axboe #include <linux/blkdev.h> 775ad4e53bSAl Viro #include <linux/fs_struct.h> 787c9f8861SEric Sandeen #include <linux/magic.h> 79cdd6c482SIngo Molnar #include <linux/perf_event.h> 8042c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 818e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 823d5992d2SYing Han #include <linux/oom.h> 83ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 84d80e731eSOleg Nesterov #include <linux/signalfd.h> 850326f5a9SSrikar Dronamraju #include <linux/uprobes.h> 86a27bb332SKent Overstreet #include <linux/aio.h> 8752f5684cSGideon Israel Dsouza #include <linux/compiler.h> 8816db3d3fSHeinrich Schuchardt #include <linux/sysctl.h> 895c9a8750SDmitry Vyukov #include <linux/kcov.h> 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds #include <asm/pgtable.h> 921da177e4SLinus Torvalds #include <asm/pgalloc.h> 937c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 941da177e4SLinus Torvalds #include <asm/mmu_context.h> 951da177e4SLinus Torvalds #include <asm/cacheflush.h> 961da177e4SLinus Torvalds #include <asm/tlbflush.h> 971da177e4SLinus Torvalds 98ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 99ad8d75ffSSteven Rostedt 10043d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 10143d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 10243d2b113SKAMEZAWA Hiroyuki 1031da177e4SLinus Torvalds /* 104ac1b398dSHeinrich Schuchardt * Minimum number of threads to boot the kernel 105ac1b398dSHeinrich Schuchardt */ 106ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20 107ac1b398dSHeinrich Schuchardt 108ac1b398dSHeinrich Schuchardt /* 109ac1b398dSHeinrich Schuchardt * Maximum number of threads 110ac1b398dSHeinrich Schuchardt */ 111ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK 112ac1b398dSHeinrich Schuchardt 113ac1b398dSHeinrich Schuchardt /* 1141da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 1171da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 124db1466b3SPaul E. McKenney 125db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 126db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 127db1466b3SPaul E. McKenney { 128db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 129db1466b3SPaul E. McKenney } 130db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 131db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds int nr_processes(void) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds int cpu; 1361da177e4SLinus Torvalds int total = 0; 1371da177e4SLinus Torvalds 1381d510750SIan Campbell for_each_possible_cpu(cpu) 1391da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds return total; 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 144f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 145f19b9f74SAkinobu Mita { 146f19b9f74SAkinobu Mita } 147f19b9f74SAkinobu Mita 148f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 149e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 15041101809SThomas Gleixner 15141101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 15241101809SThomas Gleixner { 15341101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 15441101809SThomas Gleixner } 15541101809SThomas Gleixner 15641101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 15741101809SThomas Gleixner { 15841101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 15941101809SThomas Gleixner } 1601da177e4SLinus Torvalds #endif 1611da177e4SLinus Torvalds 162b235beeaSLinus Torvalds void __weak arch_release_thread_stack(unsigned long *stack) 163f19b9f74SAkinobu Mita { 164f19b9f74SAkinobu Mita } 165f19b9f74SAkinobu Mita 166b235beeaSLinus Torvalds #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR 16741101809SThomas Gleixner 1680d15d74aSThomas Gleixner /* 1690d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1700d15d74aSThomas Gleixner * kmemcache based allocator. 1710d15d74aSThomas Gleixner */ 172ba14a194SAndy Lutomirski # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) 173ac496bf4SAndy Lutomirski 174ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 175ac496bf4SAndy Lutomirski /* 176ac496bf4SAndy Lutomirski * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 177ac496bf4SAndy Lutomirski * flush. Try to minimize the number of calls by caching stacks. 178ac496bf4SAndy Lutomirski */ 179ac496bf4SAndy Lutomirski #define NR_CACHED_STACKS 2 180ac496bf4SAndy Lutomirski static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 181ac496bf4SAndy Lutomirski #endif 182ac496bf4SAndy Lutomirski 183ba14a194SAndy Lutomirski static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) 184b69c49b7SFUJITA Tomonori { 185ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 186ac496bf4SAndy Lutomirski void *stack; 187ac496bf4SAndy Lutomirski int i; 188ac496bf4SAndy Lutomirski 189ac496bf4SAndy Lutomirski local_irq_disable(); 190ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 191ac496bf4SAndy Lutomirski struct vm_struct *s = this_cpu_read(cached_stacks[i]); 192ac496bf4SAndy Lutomirski 193ac496bf4SAndy Lutomirski if (!s) 194ac496bf4SAndy Lutomirski continue; 195ac496bf4SAndy Lutomirski this_cpu_write(cached_stacks[i], NULL); 196ac496bf4SAndy Lutomirski 197ac496bf4SAndy Lutomirski tsk->stack_vm_area = s; 198ac496bf4SAndy Lutomirski local_irq_enable(); 199ac496bf4SAndy Lutomirski return s->addr; 200ac496bf4SAndy Lutomirski } 201ac496bf4SAndy Lutomirski local_irq_enable(); 202ac496bf4SAndy Lutomirski 203ac496bf4SAndy Lutomirski stack = __vmalloc_node_range(THREAD_SIZE, THREAD_SIZE, 204ba14a194SAndy Lutomirski VMALLOC_START, VMALLOC_END, 205ba14a194SAndy Lutomirski THREADINFO_GFP | __GFP_HIGHMEM, 206ba14a194SAndy Lutomirski PAGE_KERNEL, 207ac496bf4SAndy Lutomirski 0, node, __builtin_return_address(0)); 208ba14a194SAndy Lutomirski 209ba14a194SAndy Lutomirski /* 210ba14a194SAndy Lutomirski * We can't call find_vm_area() in interrupt context, and 211ba14a194SAndy Lutomirski * free_thread_stack() can be called in interrupt context, 212ba14a194SAndy Lutomirski * so cache the vm_struct. 213ba14a194SAndy Lutomirski */ 214ba14a194SAndy Lutomirski if (stack) 215ba14a194SAndy Lutomirski tsk->stack_vm_area = find_vm_area(stack); 216ba14a194SAndy Lutomirski return stack; 217ba14a194SAndy Lutomirski #else 2184949148aSVladimir Davydov struct page *page = alloc_pages_node(node, THREADINFO_GFP, 2192889f608SThomas Gleixner THREAD_SIZE_ORDER); 220b6a84016SEric Dumazet 221b6a84016SEric Dumazet return page ? page_address(page) : NULL; 222ba14a194SAndy Lutomirski #endif 223b69c49b7SFUJITA Tomonori } 224b69c49b7SFUJITA Tomonori 225ba14a194SAndy Lutomirski static inline void free_thread_stack(struct task_struct *tsk) 226b69c49b7SFUJITA Tomonori { 227ac496bf4SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 228ac496bf4SAndy Lutomirski if (task_stack_vm_area(tsk)) { 229ac496bf4SAndy Lutomirski unsigned long flags; 230ac496bf4SAndy Lutomirski int i; 231ac496bf4SAndy Lutomirski 232ac496bf4SAndy Lutomirski local_irq_save(flags); 233ac496bf4SAndy Lutomirski for (i = 0; i < NR_CACHED_STACKS; i++) { 234ac496bf4SAndy Lutomirski if (this_cpu_read(cached_stacks[i])) 235ac496bf4SAndy Lutomirski continue; 236ac496bf4SAndy Lutomirski 237ac496bf4SAndy Lutomirski this_cpu_write(cached_stacks[i], tsk->stack_vm_area); 238ac496bf4SAndy Lutomirski local_irq_restore(flags); 239ac496bf4SAndy Lutomirski return; 240ac496bf4SAndy Lutomirski } 241ac496bf4SAndy Lutomirski local_irq_restore(flags); 242ac496bf4SAndy Lutomirski 2430f110a9bSAndrey Ryabinin vfree_atomic(tsk->stack); 244ac496bf4SAndy Lutomirski return; 245ac496bf4SAndy Lutomirski } 246ac496bf4SAndy Lutomirski #endif 247ac496bf4SAndy Lutomirski 248ba14a194SAndy Lutomirski __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); 249b69c49b7SFUJITA Tomonori } 2500d15d74aSThomas Gleixner # else 251b235beeaSLinus Torvalds static struct kmem_cache *thread_stack_cache; 2520d15d74aSThomas Gleixner 2539521d399SMichael Ellerman static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, 2540d15d74aSThomas Gleixner int node) 2550d15d74aSThomas Gleixner { 256b235beeaSLinus Torvalds return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 2570d15d74aSThomas Gleixner } 2580d15d74aSThomas Gleixner 259ba14a194SAndy Lutomirski static void free_thread_stack(struct task_struct *tsk) 2600d15d74aSThomas Gleixner { 261ba14a194SAndy Lutomirski kmem_cache_free(thread_stack_cache, tsk->stack); 2620d15d74aSThomas Gleixner } 2630d15d74aSThomas Gleixner 264b235beeaSLinus Torvalds void thread_stack_cache_init(void) 2650d15d74aSThomas Gleixner { 266b235beeaSLinus Torvalds thread_stack_cache = kmem_cache_create("thread_stack", THREAD_SIZE, 2670d15d74aSThomas Gleixner THREAD_SIZE, 0, NULL); 268b235beeaSLinus Torvalds BUG_ON(thread_stack_cache == NULL); 2690d15d74aSThomas Gleixner } 2700d15d74aSThomas Gleixner # endif 271b69c49b7SFUJITA Tomonori #endif 272b69c49b7SFUJITA Tomonori 2731da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 274e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 277e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 280e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 283e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 286e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 289e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 2901da177e4SLinus Torvalds 291ba14a194SAndy Lutomirski static void account_kernel_stack(struct task_struct *tsk, int account) 292c6a7f572SKOSAKI Motohiro { 293ba14a194SAndy Lutomirski void *stack = task_stack_page(tsk); 294ba14a194SAndy Lutomirski struct vm_struct *vm = task_stack_vm_area(tsk); 295ba14a194SAndy Lutomirski 296ba14a194SAndy Lutomirski BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); 297ba14a194SAndy Lutomirski 298ba14a194SAndy Lutomirski if (vm) { 299ba14a194SAndy Lutomirski int i; 300ba14a194SAndy Lutomirski 301ba14a194SAndy Lutomirski BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); 302ba14a194SAndy Lutomirski 303ba14a194SAndy Lutomirski for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 304ba14a194SAndy Lutomirski mod_zone_page_state(page_zone(vm->pages[i]), 305ba14a194SAndy Lutomirski NR_KERNEL_STACK_KB, 306ba14a194SAndy Lutomirski PAGE_SIZE / 1024 * account); 307ba14a194SAndy Lutomirski } 308ba14a194SAndy Lutomirski 309ba14a194SAndy Lutomirski /* All stack pages belong to the same memcg. */ 310ba14a194SAndy Lutomirski memcg_kmem_update_page_stat(vm->pages[0], MEMCG_KERNEL_STACK_KB, 311ba14a194SAndy Lutomirski account * (THREAD_SIZE / 1024)); 312ba14a194SAndy Lutomirski } else { 313ba14a194SAndy Lutomirski /* 314ba14a194SAndy Lutomirski * All stack pages are in the same zone and belong to the 315ba14a194SAndy Lutomirski * same memcg. 316ba14a194SAndy Lutomirski */ 317efdc9490SAndy Lutomirski struct page *first_page = virt_to_page(stack); 318c6a7f572SKOSAKI Motohiro 319efdc9490SAndy Lutomirski mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB, 320d30dd8beSAndy Lutomirski THREAD_SIZE / 1024 * account); 321efdc9490SAndy Lutomirski 322ba14a194SAndy Lutomirski memcg_kmem_update_page_stat(first_page, MEMCG_KERNEL_STACK_KB, 323efdc9490SAndy Lutomirski account * (THREAD_SIZE / 1024)); 324c6a7f572SKOSAKI Motohiro } 325ba14a194SAndy Lutomirski } 326c6a7f572SKOSAKI Motohiro 32768f24b08SAndy Lutomirski static void release_task_stack(struct task_struct *tsk) 3281da177e4SLinus Torvalds { 329405c0759SAndy Lutomirski if (WARN_ON(tsk->state != TASK_DEAD)) 330405c0759SAndy Lutomirski return; /* Better to leak the stack than to free prematurely */ 331405c0759SAndy Lutomirski 332ba14a194SAndy Lutomirski account_kernel_stack(tsk, -1); 333b235beeaSLinus Torvalds arch_release_thread_stack(tsk->stack); 334ba14a194SAndy Lutomirski free_thread_stack(tsk); 33568f24b08SAndy Lutomirski tsk->stack = NULL; 33668f24b08SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 33768f24b08SAndy Lutomirski tsk->stack_vm_area = NULL; 33868f24b08SAndy Lutomirski #endif 33968f24b08SAndy Lutomirski } 34068f24b08SAndy Lutomirski 34168f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 34268f24b08SAndy Lutomirski void put_task_stack(struct task_struct *tsk) 34368f24b08SAndy Lutomirski { 34468f24b08SAndy Lutomirski if (atomic_dec_and_test(&tsk->stack_refcount)) 34568f24b08SAndy Lutomirski release_task_stack(tsk); 34668f24b08SAndy Lutomirski } 34768f24b08SAndy Lutomirski #endif 34868f24b08SAndy Lutomirski 34968f24b08SAndy Lutomirski void free_task(struct task_struct *tsk) 35068f24b08SAndy Lutomirski { 35168f24b08SAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK 35268f24b08SAndy Lutomirski /* 35368f24b08SAndy Lutomirski * The task is finally done with both the stack and thread_info, 35468f24b08SAndy Lutomirski * so free both. 35568f24b08SAndy Lutomirski */ 35668f24b08SAndy Lutomirski release_task_stack(tsk); 35768f24b08SAndy Lutomirski #else 35868f24b08SAndy Lutomirski /* 35968f24b08SAndy Lutomirski * If the task had a separate stack allocation, it should be gone 36068f24b08SAndy Lutomirski * by now. 36168f24b08SAndy Lutomirski */ 36268f24b08SAndy Lutomirski WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0); 36368f24b08SAndy Lutomirski #endif 36423f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 365fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 366e2cfabdfSWill Drewry put_seccomp_filter(tsk); 367f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 3681da5c46fSOleg Nesterov if (tsk->flags & PF_KTHREAD) 3691da5c46fSOleg Nesterov free_kthread_struct(tsk); 3701da177e4SLinus Torvalds free_task_struct(tsk); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 3731da177e4SLinus Torvalds 374ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig) 375ea6d290cSOleg Nesterov { 37697101eb4SOleg Nesterov taskstats_tgid_free(sig); 3771c5354deSMike Galbraith sched_autogroup_exit(sig); 3787283094eSMichal Hocko /* 3797283094eSMichal Hocko * __mmdrop is not safe to call from softirq context on x86 due to 3807283094eSMichal Hocko * pgd_dtor so postpone it to the async context 3817283094eSMichal Hocko */ 38226db62f1SMichal Hocko if (sig->oom_mm) 3837283094eSMichal Hocko mmdrop_async(sig->oom_mm); 384ea6d290cSOleg Nesterov kmem_cache_free(signal_cachep, sig); 385ea6d290cSOleg Nesterov } 386ea6d290cSOleg Nesterov 387ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig) 388ea6d290cSOleg Nesterov { 3891c5354deSMike Galbraith if (atomic_dec_and_test(&sig->sigcnt)) 390ea6d290cSOleg Nesterov free_signal_struct(sig); 391ea6d290cSOleg Nesterov } 392ea6d290cSOleg Nesterov 393158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 3941da177e4SLinus Torvalds { 395270f722dSEugene Teo WARN_ON(!tsk->exit_state); 3961da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 3971da177e4SLinus Torvalds WARN_ON(tsk == current); 3981da177e4SLinus Torvalds 3992e91fa7fSTejun Heo cgroup_free(tsk); 400156654f4SMike Galbraith task_numa_free(tsk); 4011a2a4d06SKees Cook security_task_free(tsk); 402e0e81739SDavid Howells exit_creds(tsk); 40335df17c5SShailabh Nagar delayacct_tsk_free(tsk); 404ea6d290cSOleg Nesterov put_signal_struct(tsk->signal); 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 4071da177e4SLinus Torvalds free_task(tsk); 4081da177e4SLinus Torvalds } 40977c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct); 4101da177e4SLinus Torvalds 4116c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { } 41261c4628bSSuresh Siddha 413ff691f6eSHeinrich Schuchardt /* 414ff691f6eSHeinrich Schuchardt * set_max_threads 415ff691f6eSHeinrich Schuchardt */ 41616db3d3fSHeinrich Schuchardt static void set_max_threads(unsigned int max_threads_suggested) 417ff691f6eSHeinrich Schuchardt { 418ac1b398dSHeinrich Schuchardt u64 threads; 419ff691f6eSHeinrich Schuchardt 420ff691f6eSHeinrich Schuchardt /* 421ac1b398dSHeinrich Schuchardt * The number of threads shall be limited such that the thread 422ac1b398dSHeinrich Schuchardt * structures may only consume a small part of the available memory. 423ff691f6eSHeinrich Schuchardt */ 424ac1b398dSHeinrich Schuchardt if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64) 425ac1b398dSHeinrich Schuchardt threads = MAX_THREADS; 426ac1b398dSHeinrich Schuchardt else 427ac1b398dSHeinrich Schuchardt threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, 428ac1b398dSHeinrich Schuchardt (u64) THREAD_SIZE * 8UL); 429ac1b398dSHeinrich Schuchardt 43016db3d3fSHeinrich Schuchardt if (threads > max_threads_suggested) 43116db3d3fSHeinrich Schuchardt threads = max_threads_suggested; 43216db3d3fSHeinrich Schuchardt 433ac1b398dSHeinrich Schuchardt max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 434ff691f6eSHeinrich Schuchardt } 435ff691f6eSHeinrich Schuchardt 4365aaeb5c0SIngo Molnar #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 4375aaeb5c0SIngo Molnar /* Initialized by the architecture: */ 4385aaeb5c0SIngo Molnar int arch_task_struct_size __read_mostly; 4395aaeb5c0SIngo Molnar #endif 4400c8c0f03SDave Hansen 441ff691f6eSHeinrich Schuchardt void __init fork_init(void) 4421da177e4SLinus Torvalds { 44325f9c081SEric W. Biederman int i; 444f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 4451da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 446e274795eSPeter Zijlstra #define ARCH_MIN_TASKALIGN 0 4471da177e4SLinus Torvalds #endif 44895cb64c1SPeter Zijlstra int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); 449e274795eSPeter Zijlstra 4501da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 4515d097056SVladimir Davydov task_struct_cachep = kmem_cache_create("task_struct", 452e274795eSPeter Zijlstra arch_task_struct_size, align, 4535d097056SVladimir Davydov SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, NULL); 4541da177e4SLinus Torvalds #endif 4551da177e4SLinus Torvalds 45661c4628bSSuresh Siddha /* do the arch specific task caches init */ 45761c4628bSSuresh Siddha arch_task_cache_init(); 45861c4628bSSuresh Siddha 45916db3d3fSHeinrich Schuchardt set_max_threads(MAX_THREADS); 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 4621da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 4631da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 4641da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 465b376c3e1SEric W. Biederman 46625f9c081SEric W. Biederman for (i = 0; i < UCOUNT_COUNTS; i++) { 46725f9c081SEric W. Biederman init_user_ns.ucount_max[i] = max_threads/2; 46825f9c081SEric W. Biederman } 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 47152f5684cSGideon Israel Dsouza int __weak arch_dup_task_struct(struct task_struct *dst, 47261c4628bSSuresh Siddha struct task_struct *src) 47361c4628bSSuresh Siddha { 47461c4628bSSuresh Siddha *dst = *src; 47561c4628bSSuresh Siddha return 0; 47661c4628bSSuresh Siddha } 47761c4628bSSuresh Siddha 478d4311ff1SAaron Tomlin void set_task_stack_end_magic(struct task_struct *tsk) 479d4311ff1SAaron Tomlin { 480d4311ff1SAaron Tomlin unsigned long *stackend; 481d4311ff1SAaron Tomlin 482d4311ff1SAaron Tomlin stackend = end_of_stack(tsk); 483d4311ff1SAaron Tomlin *stackend = STACK_END_MAGIC; /* for overflow detection */ 484d4311ff1SAaron Tomlin } 485d4311ff1SAaron Tomlin 486725fc629SAndi Kleen static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 4871da177e4SLinus Torvalds { 4881da177e4SLinus Torvalds struct task_struct *tsk; 489b235beeaSLinus Torvalds unsigned long *stack; 490ba14a194SAndy Lutomirski struct vm_struct *stack_vm_area; 4913e26c149SPeter Zijlstra int err; 4921da177e4SLinus Torvalds 493725fc629SAndi Kleen if (node == NUMA_NO_NODE) 494725fc629SAndi Kleen node = tsk_fork_get_node(orig); 495504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 4961da177e4SLinus Torvalds if (!tsk) 4971da177e4SLinus Torvalds return NULL; 4981da177e4SLinus Torvalds 499b235beeaSLinus Torvalds stack = alloc_thread_stack_node(tsk, node); 500b235beeaSLinus Torvalds if (!stack) 501f19b9f74SAkinobu Mita goto free_tsk; 5021da177e4SLinus Torvalds 503ba14a194SAndy Lutomirski stack_vm_area = task_stack_vm_area(tsk); 504ba14a194SAndy Lutomirski 50561c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 506ba14a194SAndy Lutomirski 507ba14a194SAndy Lutomirski /* 508ba14a194SAndy Lutomirski * arch_dup_task_struct() clobbers the stack-related fields. Make 509ba14a194SAndy Lutomirski * sure they're properly initialized before using any stack-related 510ba14a194SAndy Lutomirski * functions again. 511ba14a194SAndy Lutomirski */ 512ba14a194SAndy Lutomirski tsk->stack = stack; 513ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK 514ba14a194SAndy Lutomirski tsk->stack_vm_area = stack_vm_area; 515ba14a194SAndy Lutomirski #endif 51668f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK 51768f24b08SAndy Lutomirski atomic_set(&tsk->stack_refcount, 1); 51868f24b08SAndy Lutomirski #endif 519ba14a194SAndy Lutomirski 52061c4628bSSuresh Siddha if (err) 521b235beeaSLinus Torvalds goto free_stack; 52261c4628bSSuresh Siddha 523dbd95212SKees Cook #ifdef CONFIG_SECCOMP 524dbd95212SKees Cook /* 525dbd95212SKees Cook * We must handle setting up seccomp filters once we're under 526dbd95212SKees Cook * the sighand lock in case orig has changed between now and 527dbd95212SKees Cook * then. Until then, filter must be NULL to avoid messing up 528dbd95212SKees Cook * the usage counts on the error path calling free_task. 529dbd95212SKees Cook */ 530dbd95212SKees Cook tsk->seccomp.filter = NULL; 531dbd95212SKees Cook #endif 53287bec58aSAndrew Morton 53387bec58aSAndrew Morton setup_thread_stack(tsk, orig); 5348e7cac79SAvi Kivity clear_user_return_notifier(tsk); 535f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 536d4311ff1SAaron Tomlin set_task_stack_end_magic(tsk); 5371da177e4SLinus Torvalds 5380a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 5390a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 5400a425405SArjan van de Ven #endif 5410a425405SArjan van de Ven 542fb0a685cSDaniel Rebelo de Oliveira /* 543fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 544fb0a685cSDaniel Rebelo de Oliveira * parent) 545fb0a685cSDaniel Rebelo de Oliveira */ 5461da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 5476c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 5482056a782SJens Axboe tsk->btrace_seq = 0; 5496c5c9341SAlexey Dobriyan #endif 550a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 5515640f768SEric Dumazet tsk->task_frag.page = NULL; 552093e5840SSebastian Andrzej Siewior tsk->wake_q.next = NULL; 553c6a7f572SKOSAKI Motohiro 554ba14a194SAndy Lutomirski account_kernel_stack(tsk, 1); 555c6a7f572SKOSAKI Motohiro 5565c9a8750SDmitry Vyukov kcov_task_init(tsk); 5575c9a8750SDmitry Vyukov 5581da177e4SLinus Torvalds return tsk; 55961c4628bSSuresh Siddha 560b235beeaSLinus Torvalds free_stack: 561ba14a194SAndy Lutomirski free_thread_stack(tsk); 562f19b9f74SAkinobu Mita free_tsk: 56361c4628bSSuresh Siddha free_task_struct(tsk); 56461c4628bSSuresh Siddha return NULL; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds #ifdef CONFIG_MMU 5680766f788SEmese Revfy static __latent_entropy int dup_mmap(struct mm_struct *mm, 5690766f788SEmese Revfy struct mm_struct *oldmm) 5701da177e4SLinus Torvalds { 571297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 5721da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 5731da177e4SLinus Torvalds int retval; 5741da177e4SLinus Torvalds unsigned long charge; 575893e26e6SPavel Emelyanov LIST_HEAD(uf); 5761da177e4SLinus Torvalds 57732cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 5787c051267SMichal Hocko if (down_write_killable(&oldmm->mmap_sem)) { 5797c051267SMichal Hocko retval = -EINTR; 5807c051267SMichal Hocko goto fail_uprobe_end; 5817c051267SMichal Hocko } 582ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 583f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 584ad339451SIngo Molnar /* 585ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 586ad339451SIngo Molnar */ 587ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 5887ee78232SHugh Dickins 58990f31d0eSKonstantin Khlebnikov /* No ordering required: file already has been exposed. */ 59090f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 59190f31d0eSKonstantin Khlebnikov 5924f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 59384638335SKonstantin Khlebnikov mm->data_vm = oldmm->data_vm; 5944f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 5954f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 5964f7d4614SVladimir Davydov 5971da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 5981da177e4SLinus Torvalds rb_parent = NULL; 5991da177e4SLinus Torvalds pprev = &mm->mmap; 600f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 601f8af4da3SHugh Dickins if (retval) 602f8af4da3SHugh Dickins goto out; 603ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 604ba76149fSAndrea Arcangeli if (retval) 605ba76149fSAndrea Arcangeli goto out; 6061da177e4SLinus Torvalds 607297c5eeeSLinus Torvalds prev = NULL; 608fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 6091da177e4SLinus Torvalds struct file *file; 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 61284638335SKonstantin Khlebnikov vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); 6131da177e4SLinus Torvalds continue; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds charge = 0; 6161da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 617b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 618b2412b7fSHuang Shijie 619191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 6201da177e4SLinus Torvalds goto fail_nomem; 6211da177e4SLinus Torvalds charge = len; 6221da177e4SLinus Torvalds } 623e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 6241da177e4SLinus Torvalds if (!tmp) 6251da177e4SLinus Torvalds goto fail_nomem; 6261da177e4SLinus Torvalds *tmp = *mpnt; 6275beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 628ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 629ef0855d3SOleg Nesterov if (retval) 6301da177e4SLinus Torvalds goto fail_nomem_policy; 631a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 632893e26e6SPavel Emelyanov retval = dup_userfaultfd(tmp, &uf); 633893e26e6SPavel Emelyanov if (retval) 634893e26e6SPavel Emelyanov goto fail_nomem_anon_vma_fork; 6355beb4930SRik van Riel if (anon_vma_fork(tmp, mpnt)) 6365beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 637893e26e6SPavel Emelyanov tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT); 638297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 6391da177e4SLinus Torvalds file = tmp->vm_file; 6401da177e4SLinus Torvalds if (file) { 641496ad9aaSAl Viro struct inode *inode = file_inode(file); 642b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 643b88ed205SHugh Dickins 6441da177e4SLinus Torvalds get_file(file); 6451da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 6461da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 64783cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 648b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 6494bb5f5d9SDavid Herrmann atomic_inc(&mapping->i_mmap_writable); 650b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 651b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 6529826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 6539826a516SMichel Lespinasse &mapping->i_mmap); 654b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 65583cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* 659a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 660a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 661a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 662a1e78772SMel Gorman */ 663a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 664a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 665a1e78772SMel Gorman 666a1e78772SMel Gorman /* 6677ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 6681da177e4SLinus Torvalds */ 6691da177e4SLinus Torvalds *pprev = tmp; 6701da177e4SLinus Torvalds pprev = &tmp->vm_next; 671297c5eeeSLinus Torvalds tmp->vm_prev = prev; 672297c5eeeSLinus Torvalds prev = tmp; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 6751da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 6761da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds mm->map_count++; 6790b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 6821da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds if (retval) 6851da177e4SLinus Torvalds goto out; 6861da177e4SLinus Torvalds } 687d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 688d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 6891da177e4SLinus Torvalds retval = 0; 6901da177e4SLinus Torvalds out: 6917ee78232SHugh Dickins up_write(&mm->mmap_sem); 692fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 6931da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 694893e26e6SPavel Emelyanov dup_userfaultfd_complete(&uf); 6957c051267SMichal Hocko fail_uprobe_end: 69632cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 6971da177e4SLinus Torvalds return retval; 6985beb4930SRik van Riel fail_nomem_anon_vma_fork: 699ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 7001da177e4SLinus Torvalds fail_nomem_policy: 7011da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 7021da177e4SLinus Torvalds fail_nomem: 7031da177e4SLinus Torvalds retval = -ENOMEM; 7041da177e4SLinus Torvalds vm_unacct_memory(charge); 7051da177e4SLinus Torvalds goto out; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 7111da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 7121da177e4SLinus Torvalds return -ENOMEM; 7131da177e4SLinus Torvalds return 0; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 7171da177e4SLinus Torvalds { 7185e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds #else 72190f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 72290f31d0eSKonstantin Khlebnikov { 72390f31d0eSKonstantin Khlebnikov down_write(&oldmm->mmap_sem); 72490f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 72590f31d0eSKonstantin Khlebnikov up_write(&oldmm->mmap_sem); 72690f31d0eSKonstantin Khlebnikov return 0; 72790f31d0eSKonstantin Khlebnikov } 7281da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 7291da177e4SLinus Torvalds #define mm_free_pgd(mm) 7301da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 7331da177e4SLinus Torvalds 734e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 7351da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 7361da177e4SLinus Torvalds 7374cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 7384cb0e11bSHidehiro Kawai 7394cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 7404cb0e11bSHidehiro Kawai { 7414cb0e11bSHidehiro Kawai default_dump_filter = 7424cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 7434cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 7444cb0e11bSHidehiro Kawai return 1; 7454cb0e11bSHidehiro Kawai } 7464cb0e11bSHidehiro Kawai 7474cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 7484cb0e11bSHidehiro Kawai 7491da177e4SLinus Torvalds #include <linux/init_task.h> 7501da177e4SLinus Torvalds 751858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 752858f0993SAlexey Dobriyan { 753858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 754858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 755db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 756858f0993SAlexey Dobriyan #endif 757858f0993SAlexey Dobriyan } 758858f0993SAlexey Dobriyan 75933144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 76033144e84SVladimir Davydov { 76133144e84SVladimir Davydov #ifdef CONFIG_MEMCG 76233144e84SVladimir Davydov mm->owner = p; 76333144e84SVladimir Davydov #endif 76433144e84SVladimir Davydov } 76533144e84SVladimir Davydov 766bfedb589SEric W. Biederman static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, 767bfedb589SEric W. Biederman struct user_namespace *user_ns) 7681da177e4SLinus Torvalds { 76941f727fdSVladimir Davydov mm->mmap = NULL; 77041f727fdSVladimir Davydov mm->mm_rb = RB_ROOT; 77141f727fdSVladimir Davydov mm->vmacache_seqnum = 0; 7721da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 7731da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 7741da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 7751da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 776999d9fc1SOleg Nesterov mm->core_state = NULL; 777e1f56c89SKirill A. Shutemov atomic_long_set(&mm->nr_ptes, 0); 7782d2f5119SKirill A. Shutemov mm_nr_pmds_init(mm); 77941f727fdSVladimir Davydov mm->map_count = 0; 78041f727fdSVladimir Davydov mm->locked_vm = 0; 781ce65cefaSVladimir Davydov mm->pinned_vm = 0; 782d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 7831da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 78441f727fdSVladimir Davydov mm_init_cpumask(mm); 785858f0993SAlexey Dobriyan mm_init_aio(mm); 786cf475ad2SBalbir Singh mm_init_owner(mm, p); 78741f727fdSVladimir Davydov mmu_notifier_mm_init(mm); 78820841405SRik van Riel clear_tlb_flush_pending(mm); 78941f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 79041f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 79141f727fdSVladimir Davydov #endif 7921da177e4SLinus Torvalds 793a0715cc2SAlex Thorlton if (current->mm) { 794a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 795a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 796a0715cc2SAlex Thorlton } else { 797a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 7981da177e4SLinus Torvalds mm->def_flags = 0; 799a0715cc2SAlex Thorlton } 800a0715cc2SAlex Thorlton 80141f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 80241f727fdSVladimir Davydov goto fail_nopgd; 80378fb7466SPavel Emelianov 80441f727fdSVladimir Davydov if (init_new_context(p, mm)) 80541f727fdSVladimir Davydov goto fail_nocontext; 80641f727fdSVladimir Davydov 807bfedb589SEric W. Biederman mm->user_ns = get_user_ns(user_ns); 80841f727fdSVladimir Davydov return mm; 80941f727fdSVladimir Davydov 81041f727fdSVladimir Davydov fail_nocontext: 81141f727fdSVladimir Davydov mm_free_pgd(mm); 81241f727fdSVladimir Davydov fail_nopgd: 8131da177e4SLinus Torvalds free_mm(mm); 8141da177e4SLinus Torvalds return NULL; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 817c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm) 818c3f0327fSKonstantin Khlebnikov { 819c3f0327fSKonstantin Khlebnikov int i; 820c3f0327fSKonstantin Khlebnikov 821c3f0327fSKonstantin Khlebnikov for (i = 0; i < NR_MM_COUNTERS; i++) { 822c3f0327fSKonstantin Khlebnikov long x = atomic_long_read(&mm->rss_stat.count[i]); 823c3f0327fSKonstantin Khlebnikov 824c3f0327fSKonstantin Khlebnikov if (unlikely(x)) 825c3f0327fSKonstantin Khlebnikov printk(KERN_ALERT "BUG: Bad rss-counter state " 826c3f0327fSKonstantin Khlebnikov "mm:%p idx:%d val:%ld\n", mm, i, x); 827c3f0327fSKonstantin Khlebnikov } 828b30fe6c7SKirill A. Shutemov 829b30fe6c7SKirill A. Shutemov if (atomic_long_read(&mm->nr_ptes)) 830b30fe6c7SKirill A. Shutemov pr_alert("BUG: non-zero nr_ptes on freeing mm: %ld\n", 831b30fe6c7SKirill A. Shutemov atomic_long_read(&mm->nr_ptes)); 832b30fe6c7SKirill A. Shutemov if (mm_nr_pmds(mm)) 833b30fe6c7SKirill A. Shutemov pr_alert("BUG: non-zero nr_pmds on freeing mm: %ld\n", 834b30fe6c7SKirill A. Shutemov mm_nr_pmds(mm)); 835b30fe6c7SKirill A. Shutemov 836e009bb30SKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 83796dad67fSSasha Levin VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 838c3f0327fSKonstantin Khlebnikov #endif 839c3f0327fSKonstantin Khlebnikov } 840c3f0327fSKonstantin Khlebnikov 8411da177e4SLinus Torvalds /* 8421da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 8431da177e4SLinus Torvalds */ 8441da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 8451da177e4SLinus Torvalds { 8461da177e4SLinus Torvalds struct mm_struct *mm; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds mm = allocate_mm(); 849de03c72cSKOSAKI Motohiro if (!mm) 850de03c72cSKOSAKI Motohiro return NULL; 851de03c72cSKOSAKI Motohiro 8521da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 853bfedb589SEric W. Biederman return mm_init(mm, current, current_user_ns()); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds /* 8571da177e4SLinus Torvalds * Called when the last reference to the mm 8581da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 8591da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 8601da177e4SLinus Torvalds */ 8617ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 8621da177e4SLinus Torvalds { 8631da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 8641da177e4SLinus Torvalds mm_free_pgd(mm); 8651da177e4SLinus Torvalds destroy_context(mm); 866cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 867c3f0327fSKonstantin Khlebnikov check_mm(mm); 868bfedb589SEric W. Biederman put_user_ns(mm->user_ns); 8691da177e4SLinus Torvalds free_mm(mm); 8701da177e4SLinus Torvalds } 8716d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 8721da177e4SLinus Torvalds 873ec8d7c14SMichal Hocko static inline void __mmput(struct mm_struct *mm) 8741da177e4SLinus Torvalds { 875ec8d7c14SMichal Hocko VM_BUG_ON(atomic_read(&mm->mm_users)); 8760ae26f1bSAndrew Morton 877d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 8781da177e4SLinus Torvalds exit_aio(mm); 8791c2fb7a4SAndrea Arcangeli ksm_exit(mm); 880ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 8811da177e4SLinus Torvalds exit_mmap(mm); 8826fcb52a5SAaron Lu mm_put_huge_zero_page(mm); 883925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 8841da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 8851da177e4SLinus Torvalds spin_lock(&mmlist_lock); 8861da177e4SLinus Torvalds list_del(&mm->mmlist); 8871da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8881da177e4SLinus Torvalds } 889801460d0SHiroshi Shimamoto if (mm->binfmt) 890801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 891862e3073SMichal Hocko set_bit(MMF_OOM_SKIP, &mm->flags); 8921da177e4SLinus Torvalds mmdrop(mm); 8931da177e4SLinus Torvalds } 894ec8d7c14SMichal Hocko 895ec8d7c14SMichal Hocko /* 896ec8d7c14SMichal Hocko * Decrement the use count and release all resources for an mm. 897ec8d7c14SMichal Hocko */ 898ec8d7c14SMichal Hocko void mmput(struct mm_struct *mm) 899ec8d7c14SMichal Hocko { 900ec8d7c14SMichal Hocko might_sleep(); 901ec8d7c14SMichal Hocko 902ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) 903ec8d7c14SMichal Hocko __mmput(mm); 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 9061da177e4SLinus Torvalds 9077ef949d7SMichal Hocko #ifdef CONFIG_MMU 908ec8d7c14SMichal Hocko static void mmput_async_fn(struct work_struct *work) 909ec8d7c14SMichal Hocko { 910ec8d7c14SMichal Hocko struct mm_struct *mm = container_of(work, struct mm_struct, async_put_work); 911ec8d7c14SMichal Hocko __mmput(mm); 912ec8d7c14SMichal Hocko } 913ec8d7c14SMichal Hocko 914ec8d7c14SMichal Hocko void mmput_async(struct mm_struct *mm) 915ec8d7c14SMichal Hocko { 916ec8d7c14SMichal Hocko if (atomic_dec_and_test(&mm->mm_users)) { 917ec8d7c14SMichal Hocko INIT_WORK(&mm->async_put_work, mmput_async_fn); 918ec8d7c14SMichal Hocko schedule_work(&mm->async_put_work); 919ec8d7c14SMichal Hocko } 920ec8d7c14SMichal Hocko } 9217ef949d7SMichal Hocko #endif 922ec8d7c14SMichal Hocko 92390f31d0eSKonstantin Khlebnikov /** 92490f31d0eSKonstantin Khlebnikov * set_mm_exe_file - change a reference to the mm's executable file 92590f31d0eSKonstantin Khlebnikov * 92690f31d0eSKonstantin Khlebnikov * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 92790f31d0eSKonstantin Khlebnikov * 9286e399cd1SDavidlohr Bueso * Main users are mmput() and sys_execve(). Callers prevent concurrent 9296e399cd1SDavidlohr Bueso * invocations: in mmput() nobody alive left, in execve task is single 9306e399cd1SDavidlohr Bueso * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the 9316e399cd1SDavidlohr Bueso * mm->exe_file, but does so without using set_mm_exe_file() in order 9326e399cd1SDavidlohr Bueso * to do avoid the need for any locks. 93390f31d0eSKonstantin Khlebnikov */ 93438646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 93538646013SJiri Slaby { 9366e399cd1SDavidlohr Bueso struct file *old_exe_file; 9376e399cd1SDavidlohr Bueso 9386e399cd1SDavidlohr Bueso /* 9396e399cd1SDavidlohr Bueso * It is safe to dereference the exe_file without RCU as 9406e399cd1SDavidlohr Bueso * this function is only called if nobody else can access 9416e399cd1SDavidlohr Bueso * this mm -- see comment above for justification. 9426e399cd1SDavidlohr Bueso */ 9436e399cd1SDavidlohr Bueso old_exe_file = rcu_dereference_raw(mm->exe_file); 94490f31d0eSKonstantin Khlebnikov 94538646013SJiri Slaby if (new_exe_file) 94638646013SJiri Slaby get_file(new_exe_file); 94790f31d0eSKonstantin Khlebnikov rcu_assign_pointer(mm->exe_file, new_exe_file); 94890f31d0eSKonstantin Khlebnikov if (old_exe_file) 94990f31d0eSKonstantin Khlebnikov fput(old_exe_file); 95038646013SJiri Slaby } 95138646013SJiri Slaby 95290f31d0eSKonstantin Khlebnikov /** 95390f31d0eSKonstantin Khlebnikov * get_mm_exe_file - acquire a reference to the mm's executable file 95490f31d0eSKonstantin Khlebnikov * 95590f31d0eSKonstantin Khlebnikov * Returns %NULL if mm has no associated executable file. 95690f31d0eSKonstantin Khlebnikov * User must release file via fput(). 95790f31d0eSKonstantin Khlebnikov */ 95838646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 95938646013SJiri Slaby { 96038646013SJiri Slaby struct file *exe_file; 96138646013SJiri Slaby 96290f31d0eSKonstantin Khlebnikov rcu_read_lock(); 96390f31d0eSKonstantin Khlebnikov exe_file = rcu_dereference(mm->exe_file); 96490f31d0eSKonstantin Khlebnikov if (exe_file && !get_file_rcu(exe_file)) 96590f31d0eSKonstantin Khlebnikov exe_file = NULL; 96690f31d0eSKonstantin Khlebnikov rcu_read_unlock(); 96738646013SJiri Slaby return exe_file; 96838646013SJiri Slaby } 96911163348SDavidlohr Bueso EXPORT_SYMBOL(get_mm_exe_file); 97038646013SJiri Slaby 9711da177e4SLinus Torvalds /** 972cd81a917SMateusz Guzik * get_task_exe_file - acquire a reference to the task's executable file 973cd81a917SMateusz Guzik * 974cd81a917SMateusz Guzik * Returns %NULL if task's mm (if any) has no associated executable file or 975cd81a917SMateusz Guzik * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 976cd81a917SMateusz Guzik * User must release file via fput(). 977cd81a917SMateusz Guzik */ 978cd81a917SMateusz Guzik struct file *get_task_exe_file(struct task_struct *task) 979cd81a917SMateusz Guzik { 980cd81a917SMateusz Guzik struct file *exe_file = NULL; 981cd81a917SMateusz Guzik struct mm_struct *mm; 982cd81a917SMateusz Guzik 983cd81a917SMateusz Guzik task_lock(task); 984cd81a917SMateusz Guzik mm = task->mm; 985cd81a917SMateusz Guzik if (mm) { 986cd81a917SMateusz Guzik if (!(task->flags & PF_KTHREAD)) 987cd81a917SMateusz Guzik exe_file = get_mm_exe_file(mm); 988cd81a917SMateusz Guzik } 989cd81a917SMateusz Guzik task_unlock(task); 990cd81a917SMateusz Guzik return exe_file; 991cd81a917SMateusz Guzik } 992cd81a917SMateusz Guzik EXPORT_SYMBOL(get_task_exe_file); 993cd81a917SMateusz Guzik 994cd81a917SMateusz Guzik /** 9951da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 9961da177e4SLinus Torvalds * 997246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 9981da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 9991da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 10001da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 10011da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 10021da177e4SLinus Torvalds */ 10031da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 10041da177e4SLinus Torvalds { 10051da177e4SLinus Torvalds struct mm_struct *mm; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds task_lock(task); 10081da177e4SLinus Torvalds mm = task->mm; 10091da177e4SLinus Torvalds if (mm) { 1010246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 10111da177e4SLinus Torvalds mm = NULL; 10121da177e4SLinus Torvalds else 10133fce371bSVegard Nossum mmget(mm); 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds task_unlock(task); 10161da177e4SLinus Torvalds return mm; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 10191da177e4SLinus Torvalds 10208cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 10218cdb878dSChristopher Yeoh { 10228cdb878dSChristopher Yeoh struct mm_struct *mm; 10238cdb878dSChristopher Yeoh int err; 10248cdb878dSChristopher Yeoh 10258cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 10268cdb878dSChristopher Yeoh if (err) 10278cdb878dSChristopher Yeoh return ERR_PTR(err); 10288cdb878dSChristopher Yeoh 10298cdb878dSChristopher Yeoh mm = get_task_mm(task); 10308cdb878dSChristopher Yeoh if (mm && mm != current->mm && 10318cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 10328cdb878dSChristopher Yeoh mmput(mm); 10338cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 10348cdb878dSChristopher Yeoh } 10358cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 10368cdb878dSChristopher Yeoh 10378cdb878dSChristopher Yeoh return mm; 10388cdb878dSChristopher Yeoh } 10398cdb878dSChristopher Yeoh 104057b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 1041c415c3b4SOleg Nesterov { 1042d68b46feSOleg Nesterov struct completion *vfork; 1043c415c3b4SOleg Nesterov 1044d68b46feSOleg Nesterov task_lock(tsk); 1045d68b46feSOleg Nesterov vfork = tsk->vfork_done; 1046d68b46feSOleg Nesterov if (likely(vfork)) { 1047c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 1048d68b46feSOleg Nesterov complete(vfork); 1049d68b46feSOleg Nesterov } 1050d68b46feSOleg Nesterov task_unlock(tsk); 1051d68b46feSOleg Nesterov } 1052d68b46feSOleg Nesterov 1053d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 1054d68b46feSOleg Nesterov struct completion *vfork) 1055d68b46feSOleg Nesterov { 1056d68b46feSOleg Nesterov int killed; 1057d68b46feSOleg Nesterov 1058d68b46feSOleg Nesterov freezer_do_not_count(); 1059d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 1060d68b46feSOleg Nesterov freezer_count(); 1061d68b46feSOleg Nesterov 1062d68b46feSOleg Nesterov if (killed) { 1063d68b46feSOleg Nesterov task_lock(child); 1064d68b46feSOleg Nesterov child->vfork_done = NULL; 1065d68b46feSOleg Nesterov task_unlock(child); 1066d68b46feSOleg Nesterov } 1067d68b46feSOleg Nesterov 1068d68b46feSOleg Nesterov put_task_struct(child); 1069d68b46feSOleg Nesterov return killed; 1070c415c3b4SOleg Nesterov } 1071c415c3b4SOleg Nesterov 10721da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 10731da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 10741da177e4SLinus Torvalds * error success whatever. 10751da177e4SLinus Torvalds * 10761da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 10771da177e4SLinus Torvalds * from the current process. 10781da177e4SLinus Torvalds * 10791da177e4SLinus Torvalds * This difference is important for error handling, when we 10801da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 10811da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 10821da177e4SLinus Torvalds * restoring the old one. . . 10831da177e4SLinus Torvalds * Eric Biederman 10 January 1998 10841da177e4SLinus Torvalds */ 10851da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 10861da177e4SLinus Torvalds { 10878141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 10888141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 1089fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 10908141c7f3SLinus Torvalds exit_robust_list(tsk); 1091fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 1092fc6b177dSPeter Zijlstra } 10938141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 1094fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 10958141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 1096fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 1097fc6b177dSPeter Zijlstra } 10988141c7f3SLinus Torvalds #endif 1099322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 1100322a2c10SThomas Gleixner exit_pi_state_list(tsk); 11018141c7f3SLinus Torvalds #endif 11028141c7f3SLinus Torvalds 11030326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 11040326f5a9SSrikar Dronamraju 11051da177e4SLinus Torvalds /* Get rid of any cached register state */ 11061da177e4SLinus Torvalds deactivate_mm(tsk, mm); 11071da177e4SLinus Torvalds 1108fec1d011SRoland McGrath /* 1109735f2770SMichal Hocko * Signal userspace if we're not exiting with a core dump 1110735f2770SMichal Hocko * because we want to leave the value intact for debugging 1111735f2770SMichal Hocko * purposes. 1112fec1d011SRoland McGrath */ 11139c8a8228SEric Dumazet if (tsk->clear_child_tid) { 1114735f2770SMichal Hocko if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) && 11159c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 11161da177e4SLinus Torvalds /* 11171da177e4SLinus Torvalds * We don't check the error code - if userspace has 11181da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 11191da177e4SLinus Torvalds */ 11209c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 11219c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 11229c8a8228SEric Dumazet 1, NULL, NULL, 0); 11239c8a8228SEric Dumazet } 11249c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 11251da177e4SLinus Torvalds } 1126f7505d64SKonstantin Khlebnikov 1127f7505d64SKonstantin Khlebnikov /* 1128f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 1129f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 1130f7505d64SKonstantin Khlebnikov */ 1131f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 1132f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 1135a0a7ec30SJANAK DESAI /* 1136a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 1137a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 1138a0a7ec30SJANAK DESAI */ 1139ff252c1fSDaeSeok Youn static struct mm_struct *dup_mm(struct task_struct *tsk) 1140a0a7ec30SJANAK DESAI { 1141a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 1142a0a7ec30SJANAK DESAI int err; 1143a0a7ec30SJANAK DESAI 1144a0a7ec30SJANAK DESAI mm = allocate_mm(); 1145a0a7ec30SJANAK DESAI if (!mm) 1146a0a7ec30SJANAK DESAI goto fail_nomem; 1147a0a7ec30SJANAK DESAI 1148a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 1149a0a7ec30SJANAK DESAI 1150bfedb589SEric W. Biederman if (!mm_init(mm, tsk, mm->user_ns)) 1151a0a7ec30SJANAK DESAI goto fail_nomem; 1152a0a7ec30SJANAK DESAI 1153a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 1154a0a7ec30SJANAK DESAI if (err) 1155a0a7ec30SJANAK DESAI goto free_pt; 1156a0a7ec30SJANAK DESAI 1157a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 1158a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 1159a0a7ec30SJANAK DESAI 1160801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1161801460d0SHiroshi Shimamoto goto free_pt; 1162801460d0SHiroshi Shimamoto 1163a0a7ec30SJANAK DESAI return mm; 1164a0a7ec30SJANAK DESAI 1165a0a7ec30SJANAK DESAI free_pt: 1166801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 1167801460d0SHiroshi Shimamoto mm->binfmt = NULL; 1168a0a7ec30SJANAK DESAI mmput(mm); 1169a0a7ec30SJANAK DESAI 1170a0a7ec30SJANAK DESAI fail_nomem: 1171a0a7ec30SJANAK DESAI return NULL; 1172a0a7ec30SJANAK DESAI } 1173a0a7ec30SJANAK DESAI 11741da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 11751da177e4SLinus Torvalds { 11761da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 11771da177e4SLinus Torvalds int retval; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 11801da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 118117406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 118217406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 118317406b82SMandeep Singh Baines #endif 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds tsk->mm = NULL; 11861da177e4SLinus Torvalds tsk->active_mm = NULL; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds /* 11891da177e4SLinus Torvalds * Are we cloning a kernel thread? 11901da177e4SLinus Torvalds * 11911da177e4SLinus Torvalds * We need to steal a active VM for that.. 11921da177e4SLinus Torvalds */ 11931da177e4SLinus Torvalds oldmm = current->mm; 11941da177e4SLinus Torvalds if (!oldmm) 11951da177e4SLinus Torvalds return 0; 11961da177e4SLinus Torvalds 1197615d6e87SDavidlohr Bueso /* initialize the new vmacache entries */ 1198615d6e87SDavidlohr Bueso vmacache_flush(tsk); 1199615d6e87SDavidlohr Bueso 12001da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 12013fce371bSVegard Nossum mmget(oldmm); 12021da177e4SLinus Torvalds mm = oldmm; 12031da177e4SLinus Torvalds goto good_mm; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds retval = -ENOMEM; 1207a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 12081da177e4SLinus Torvalds if (!mm) 12091da177e4SLinus Torvalds goto fail_nomem; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds good_mm: 12121da177e4SLinus Torvalds tsk->mm = mm; 12131da177e4SLinus Torvalds tsk->active_mm = mm; 12141da177e4SLinus Torvalds return 0; 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds fail_nomem: 12171da177e4SLinus Torvalds return retval; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 1220a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 12211da177e4SLinus Torvalds { 1222498052bbSAl Viro struct fs_struct *fs = current->fs; 12231da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 1224498052bbSAl Viro /* tsk->fs is already what we want */ 12252a4419b5SNick Piggin spin_lock(&fs->lock); 1226498052bbSAl Viro if (fs->in_exec) { 12272a4419b5SNick Piggin spin_unlock(&fs->lock); 1228498052bbSAl Viro return -EAGAIN; 1229498052bbSAl Viro } 1230498052bbSAl Viro fs->users++; 12312a4419b5SNick Piggin spin_unlock(&fs->lock); 12321da177e4SLinus Torvalds return 0; 12331da177e4SLinus Torvalds } 1234498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 12351da177e4SLinus Torvalds if (!tsk->fs) 12361da177e4SLinus Torvalds return -ENOMEM; 12371da177e4SLinus Torvalds return 0; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 1240a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 1241a016f338SJANAK DESAI { 1242a016f338SJANAK DESAI struct files_struct *oldf, *newf; 1243a016f338SJANAK DESAI int error = 0; 1244a016f338SJANAK DESAI 1245a016f338SJANAK DESAI /* 1246a016f338SJANAK DESAI * A background process may not have any files ... 1247a016f338SJANAK DESAI */ 1248a016f338SJANAK DESAI oldf = current->files; 1249a016f338SJANAK DESAI if (!oldf) 1250a016f338SJANAK DESAI goto out; 1251a016f338SJANAK DESAI 1252a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 1253a016f338SJANAK DESAI atomic_inc(&oldf->count); 1254a016f338SJANAK DESAI goto out; 1255a016f338SJANAK DESAI } 1256a016f338SJANAK DESAI 1257a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 1258a016f338SJANAK DESAI if (!newf) 1259a016f338SJANAK DESAI goto out; 1260a016f338SJANAK DESAI 1261a016f338SJANAK DESAI tsk->files = newf; 1262a016f338SJANAK DESAI error = 0; 1263a016f338SJANAK DESAI out: 1264a016f338SJANAK DESAI return error; 1265a016f338SJANAK DESAI } 1266a016f338SJANAK DESAI 1267fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 1268fd0928dfSJens Axboe { 1269fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 1270fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 12716e736be7STejun Heo struct io_context *new_ioc; 1272fd0928dfSJens Axboe 1273fd0928dfSJens Axboe if (!ioc) 1274fd0928dfSJens Axboe return 0; 1275fadad878SJens Axboe /* 1276fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 1277fadad878SJens Axboe */ 1278fadad878SJens Axboe if (clone_flags & CLONE_IO) { 12793d48749dSTejun Heo ioc_task_link(ioc); 12803d48749dSTejun Heo tsk->io_context = ioc; 1281fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 12826e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 12836e736be7STejun Heo if (unlikely(!new_ioc)) 1284fd0928dfSJens Axboe return -ENOMEM; 1285fd0928dfSJens Axboe 12866e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 128711a3122fSTejun Heo put_io_context(new_ioc); 1288fd0928dfSJens Axboe } 1289fd0928dfSJens Axboe #endif 1290fd0928dfSJens Axboe return 0; 1291fd0928dfSJens Axboe } 1292fd0928dfSJens Axboe 1293a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 12941da177e4SLinus Torvalds { 12951da177e4SLinus Torvalds struct sighand_struct *sig; 12961da177e4SLinus Torvalds 129760348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 12981da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 12991da177e4SLinus Torvalds return 0; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 1302e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 13031da177e4SLinus Torvalds if (!sig) 13041da177e4SLinus Torvalds return -ENOMEM; 13059d7fb042SPeter Zijlstra 13061da177e4SLinus Torvalds atomic_set(&sig->count, 1); 13071da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 13081da177e4SLinus Torvalds return 0; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 1311a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1312c81addc9SOleg Nesterov { 1313d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 1314d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1315392809b2SOleg Nesterov /* 1316392809b2SOleg Nesterov * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it 1317392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1318392809b2SOleg Nesterov */ 1319c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1320c81addc9SOleg Nesterov } 1321d80e731eSOleg Nesterov } 1322c81addc9SOleg Nesterov 1323b18b6a9cSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 1324f06febc9SFrank Mayhar /* 1325f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1326f06febc9SFrank Mayhar */ 1327f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1328f06febc9SFrank Mayhar { 132978d7d407SJiri Slaby unsigned long cpu_limit; 133078d7d407SJiri Slaby 1331316c1608SJason Low cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 133278d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 1333ebd7e7fcSFrederic Weisbecker sig->cputime_expires.prof_exp = cpu_limit * NSEC_PER_SEC; 1334d5c373ebSJason Low sig->cputimer.running = true; 13356279a751SOleg Nesterov } 13366279a751SOleg Nesterov 1337f06febc9SFrank Mayhar /* The timer lists. */ 1338f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1339f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1340f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1341f06febc9SFrank Mayhar } 1342b18b6a9cSNicolas Pitre #else 1343b18b6a9cSNicolas Pitre static inline void posix_cpu_timers_init_group(struct signal_struct *sig) { } 1344b18b6a9cSNicolas Pitre #endif 1345f06febc9SFrank Mayhar 1346a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 13471da177e4SLinus Torvalds { 13481da177e4SLinus Torvalds struct signal_struct *sig; 13491da177e4SLinus Torvalds 13504ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1351490dea45SPeter Zijlstra return 0; 13526279a751SOleg Nesterov 1353a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 13541da177e4SLinus Torvalds tsk->signal = sig; 13551da177e4SLinus Torvalds if (!sig) 13561da177e4SLinus Torvalds return -ENOMEM; 13571da177e4SLinus Torvalds 1358b3ac022cSOleg Nesterov sig->nr_threads = 1; 13591da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1360b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 13610c740d0aSOleg Nesterov 13620c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 13630c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 13640c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 13650c740d0aSOleg Nesterov 13661da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1367db51aeccSOleg Nesterov sig->curr_target = tsk; 13681da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 1369e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 13709d7fb042SPeter Zijlstra prev_cputime_init(&sig->prev_cputime); 13711da177e4SLinus Torvalds 1372baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 1373b18b6a9cSNicolas Pitre INIT_LIST_HEAD(&sig->posix_timers); 1374c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 13751da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 1376baa73d9eSNicolas Pitre #endif 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds task_lock(current->group_leader); 13791da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 13801da177e4SLinus Torvalds task_unlock(current->group_leader); 13811da177e4SLinus Torvalds 13826279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 13836279a751SOleg Nesterov 1384522ed776SMiloslav Trmac tty_audit_fork(sig); 13855091faa4SMike Galbraith sched_autogroup_fork(sig); 1386522ed776SMiloslav Trmac 1387a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1388dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 138928b83c51SKOSAKI Motohiro 13909b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 13919b1bf12dSKOSAKI Motohiro 13921da177e4SLinus Torvalds return 0; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds 1395dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1396dbd95212SKees Cook { 1397dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1398dbd95212SKees Cook /* 1399dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1400dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1401dbd95212SKees Cook * needed because this new task is not yet running and cannot 1402dbd95212SKees Cook * be racing exec. 1403dbd95212SKees Cook */ 140469f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1405dbd95212SKees Cook 1406dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1407dbd95212SKees Cook get_seccomp_filter(current); 1408dbd95212SKees Cook p->seccomp = current->seccomp; 1409dbd95212SKees Cook 1410dbd95212SKees Cook /* 1411dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1412dbd95212SKees Cook * between the task_struct being duplicated and holding the 1413dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1414dbd95212SKees Cook */ 1415dbd95212SKees Cook if (task_no_new_privs(current)) 1416dbd95212SKees Cook task_set_no_new_privs(p); 1417dbd95212SKees Cook 1418dbd95212SKees Cook /* 1419dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1420dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1421dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1422dbd95212SKees Cook */ 1423dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1424dbd95212SKees Cook set_tsk_thread_flag(p, TIF_SECCOMP); 1425dbd95212SKees Cook #endif 1426dbd95212SKees Cook } 1427dbd95212SKees Cook 142817da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 14291da177e4SLinus Torvalds { 14301da177e4SLinus Torvalds current->clear_child_tid = tidptr; 14311da177e4SLinus Torvalds 1432b488893aSPavel Emelyanov return task_pid_vnr(current); 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 1435a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 143623f78d4aSIngo Molnar { 14371d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1438e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1439fb00aca4SPeter Zijlstra p->pi_waiters = RB_ROOT; 1440fb00aca4SPeter Zijlstra p->pi_waiters_leftmost = NULL; 1441*e96a7705SXunlei Pang p->pi_top_task = NULL; 144223f78d4aSIngo Molnar p->pi_blocked_on = NULL; 144323f78d4aSIngo Molnar #endif 144423f78d4aSIngo Molnar } 144523f78d4aSIngo Molnar 1446b18b6a9cSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS 14471da177e4SLinus Torvalds /* 1448f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1449f06febc9SFrank Mayhar */ 1450f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1451f06febc9SFrank Mayhar { 145264861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 145364861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1454f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1455f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1456f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1457f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1458f06febc9SFrank Mayhar } 1459b18b6a9cSNicolas Pitre #else 1460b18b6a9cSNicolas Pitre static inline void posix_cpu_timers_init(struct task_struct *tsk) { } 1461b18b6a9cSNicolas Pitre #endif 1462f06febc9SFrank Mayhar 146381907739SOleg Nesterov static inline void 146481907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 146581907739SOleg Nesterov { 146681907739SOleg Nesterov task->pids[type].pid = pid; 146781907739SOleg Nesterov } 146881907739SOleg Nesterov 14696bfbaa51SIngo Molnar static inline void rcu_copy_process(struct task_struct *p) 14706bfbaa51SIngo Molnar { 14716bfbaa51SIngo Molnar #ifdef CONFIG_PREEMPT_RCU 14726bfbaa51SIngo Molnar p->rcu_read_lock_nesting = 0; 14736bfbaa51SIngo Molnar p->rcu_read_unlock_special.s = 0; 14746bfbaa51SIngo Molnar p->rcu_blocked_node = NULL; 14756bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_node_entry); 14766bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_PREEMPT_RCU */ 14776bfbaa51SIngo Molnar #ifdef CONFIG_TASKS_RCU 14786bfbaa51SIngo Molnar p->rcu_tasks_holdout = false; 14796bfbaa51SIngo Molnar INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); 14806bfbaa51SIngo Molnar p->rcu_tasks_idle_cpu = -1; 14816bfbaa51SIngo Molnar #endif /* #ifdef CONFIG_TASKS_RCU */ 14826bfbaa51SIngo Molnar } 14836bfbaa51SIngo Molnar 1484f06febc9SFrank Mayhar /* 14851da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 14861da177e4SLinus Torvalds * but does not actually start it yet. 14871da177e4SLinus Torvalds * 14881da177e4SLinus Torvalds * It copies the registers, and all the appropriate 14891da177e4SLinus Torvalds * parts of the process environment (as per the clone 14901da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 14911da177e4SLinus Torvalds */ 14920766f788SEmese Revfy static __latent_entropy struct task_struct *copy_process( 14930766f788SEmese Revfy unsigned long clone_flags, 14941da177e4SLinus Torvalds unsigned long stack_start, 14951da177e4SLinus Torvalds unsigned long stack_size, 14961da177e4SLinus Torvalds int __user *child_tidptr, 149709a05394SRoland McGrath struct pid *pid, 14983033f14aSJosh Triplett int trace, 1499725fc629SAndi Kleen unsigned long tls, 1500725fc629SAndi Kleen int node) 15011da177e4SLinus Torvalds { 15021da177e4SLinus Torvalds int retval; 1503a24efe62SMariusz Kozlowski struct task_struct *p; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 15061da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 15071da177e4SLinus Torvalds 1508e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1509e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1510e66eded8SEric W. Biederman 15111da177e4SLinus Torvalds /* 15121da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 15131da177e4SLinus Torvalds * can only be started up within the thread group. 15141da177e4SLinus Torvalds */ 15151da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 15161da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds /* 15191da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 15201da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 15211da177e4SLinus Torvalds * for various simplifications in other code. 15221da177e4SLinus Torvalds */ 15231da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 15241da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 15251da177e4SLinus Torvalds 1526123be07bSSukadev Bhattiprolu /* 1527123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1528123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1529123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1530123be07bSSukadev Bhattiprolu * from creating siblings. 1531123be07bSSukadev Bhattiprolu */ 1532123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1533123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1534123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1535123be07bSSukadev Bhattiprolu 15368382fcacSEric W. Biederman /* 153740a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 1538faf00da5SEric W. Biederman * do not allow it to share a thread group with the forking task. 15398382fcacSEric W. Biederman */ 1540faf00da5SEric W. Biederman if (clone_flags & CLONE_THREAD) { 154140a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 154240a0d32dSOleg Nesterov (task_active_pid_ns(current) != 1543c2b1df2eSAndy Lutomirski current->nsproxy->pid_ns_for_children)) 15448382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 154540a0d32dSOleg Nesterov } 15468382fcacSEric W. Biederman 15471da177e4SLinus Torvalds retval = security_task_create(clone_flags); 15481da177e4SLinus Torvalds if (retval) 15491da177e4SLinus Torvalds goto fork_out; 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds retval = -ENOMEM; 1552725fc629SAndi Kleen p = dup_task_struct(current, node); 15531da177e4SLinus Torvalds if (!p) 15541da177e4SLinus Torvalds goto fork_out; 15551da177e4SLinus Torvalds 1556f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1557f7e8b616SSteven Rostedt 1558bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1559bea493a0SPeter Zijlstra 1560d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1561de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1562de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1563de30a2b3SIngo Molnar #endif 15641da177e4SLinus Torvalds retval = -EAGAIN; 15653b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 156678d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1567b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1568b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 15691da177e4SLinus Torvalds goto bad_fork_free; 15701da177e4SLinus Torvalds } 157172fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 15721da177e4SLinus Torvalds 1573f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1574f1752eecSDavid Howells if (retval < 0) 1575f1752eecSDavid Howells goto bad_fork_free; 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds /* 15781da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 15791da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 15801da177e4SLinus Torvalds * to stop root fork bombs. 15811da177e4SLinus Torvalds */ 158204ec93feSLi Zefan retval = -EAGAIN; 15831da177e4SLinus Torvalds if (nr_threads >= max_threads) 15841da177e4SLinus Torvalds goto bad_fork_cleanup_count; 15851da177e4SLinus Torvalds 1586ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1587c1de45caSPeter Zijlstra p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE); 1588514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 15891da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 15901da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1591f41d911fSPaul E. McKenney rcu_copy_process(p); 15921da177e4SLinus Torvalds p->vfork_done = NULL; 15931da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds init_sigpending(&p->pending); 15961da177e4SLinus Torvalds 159764861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 159840565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 159964861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 160040565b5aSStanislaw Gruszka #endif 16019d7fb042SPeter Zijlstra prev_cputime_init(&p->prev_cputime); 16029d7fb042SPeter Zijlstra 16036a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 1604b7ce2277SFrederic Weisbecker seqcount_init(&p->vtime_seqcount); 16056a61671bSFrederic Weisbecker p->vtime_snap = 0; 16067098c1eaSFrederic Weisbecker p->vtime_snap_whence = VTIME_INACTIVE; 16076a61671bSFrederic Weisbecker #endif 16086a61671bSFrederic Weisbecker 1609a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1610a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1611a3a2e76cSKAMEZAWA Hiroyuki #endif 1612172ba844SBalbir Singh 16136976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 16146976675dSArjan van de Ven 16155995477aSAndrea Righi task_io_accounting_init(&p->ioac); 16161da177e4SLinus Torvalds acct_clear_integrals(p); 16171da177e4SLinus Torvalds 1618f06febc9SFrank Mayhar posix_cpu_timers_init(p); 16191da177e4SLinus Torvalds 1620ccbf62d8SThomas Gleixner p->start_time = ktime_get_ns(); 162157e0be04SThomas Gleixner p->real_start_time = ktime_get_boot_ns(); 16221da177e4SLinus Torvalds p->io_context = NULL; 16231da177e4SLinus Torvalds p->audit_context = NULL; 1624b4f48b63SPaul Menage cgroup_fork(p); 16251da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1626846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 16271da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 16281da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 16291da177e4SLinus Torvalds p->mempolicy = NULL; 1630e8604cb4SLi Zefan goto bad_fork_cleanup_threadgroup_lock; 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds #endif 1633778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1634778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1635778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1636cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1637778d3b0fSMichal Hocko #endif 1638de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1639de30a2b3SIngo Molnar p->irq_events = 0; 1640de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1641de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1642de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1643de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1644de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1645de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1646de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1647de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1648de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1649de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1650de30a2b3SIngo Molnar p->hardirq_context = 0; 1651de30a2b3SIngo Molnar p->softirq_context = 0; 1652de30a2b3SIngo Molnar #endif 16538bcbde54SDavid Hildenbrand 16548bcbde54SDavid Hildenbrand p->pagefault_disabled = 0; 16558bcbde54SDavid Hildenbrand 1656fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1657fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1658fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1659fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1660fbb9ce95SIngo Molnar #endif 16611da177e4SLinus Torvalds 1662408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1663408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1664408894eeSIngo Molnar #endif 1665cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 1666cafe5635SKent Overstreet p->sequential_io = 0; 1667cafe5635SKent Overstreet p->sequential_io_avg = 0; 1668cafe5635SKent Overstreet #endif 16690f481406SMarkus Metzger 16703c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 1671aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 1672aab03e05SDario Faggioli if (retval) 1673aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 16746ab423e0SPeter Zijlstra 1675cdd6c482SIngo Molnar retval = perf_event_init_task(p); 16766ab423e0SPeter Zijlstra if (retval) 16776ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1678fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1679fb0a685cSDaniel Rebelo de Oliveira if (retval) 16806c72e350SPeter Zijlstra goto bad_fork_cleanup_perf; 16811da177e4SLinus Torvalds /* copy all the process information */ 1682ab602f79SJack Miller shm_init_task(p); 1683fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1684fb0a685cSDaniel Rebelo de Oliveira if (retval) 16851da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1686fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1687fb0a685cSDaniel Rebelo de Oliveira if (retval) 16881da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1689fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1690fb0a685cSDaniel Rebelo de Oliveira if (retval) 16911da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1692fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1693fb0a685cSDaniel Rebelo de Oliveira if (retval) 16941da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1695fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1696fb0a685cSDaniel Rebelo de Oliveira if (retval) 16971da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1698fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1699fb0a685cSDaniel Rebelo de Oliveira if (retval) 17001da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1701fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1702fb0a685cSDaniel Rebelo de Oliveira if (retval) 1703d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1704fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1705fb0a685cSDaniel Rebelo de Oliveira if (retval) 1706fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 17073033f14aSJosh Triplett retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls); 17081da177e4SLinus Torvalds if (retval) 1709fd0928dfSJens Axboe goto bad_fork_cleanup_io; 17101da177e4SLinus Torvalds 1711425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1712c2b1df2eSAndy Lutomirski pid = alloc_pid(p->nsproxy->pid_ns_for_children); 171335f71bc0SMichal Hocko if (IS_ERR(pid)) { 171435f71bc0SMichal Hocko retval = PTR_ERR(pid); 17150740aa5fSJiri Slaby goto bad_fork_cleanup_thread; 1716425fb2b4SPavel Emelyanov } 171735f71bc0SMichal Hocko } 1718425fb2b4SPavel Emelyanov 17191da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 17201da177e4SLinus Torvalds /* 17211da177e4SLinus Torvalds * Clear TID on mm_release()? 17221da177e4SLinus Torvalds */ 17231da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 172473c10101SJens Axboe #ifdef CONFIG_BLOCK 172573c10101SJens Axboe p->plug = NULL; 172673c10101SJens Axboe #endif 172742b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 17288f17d3a5SIngo Molnar p->robust_list = NULL; 17298f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 17308f17d3a5SIngo Molnar p->compat_robust_list = NULL; 17318f17d3a5SIngo Molnar #endif 1732c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1733c87e2837SIngo Molnar p->pi_state_cache = NULL; 173442b2dd0aSAlexey Dobriyan #endif 17351da177e4SLinus Torvalds /* 1736f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1737f9a3879aSGOTO Masanori */ 1738f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 17392a742138SStas Sergeev sas_ss_reset(p); 1740f9a3879aSGOTO Masanori 1741f9a3879aSGOTO Masanori /* 17426580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 17436580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 17441da177e4SLinus Torvalds */ 17456580807dSOleg Nesterov user_disable_single_step(p); 17461da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1747ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1748ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1749ed75e8d5SLaurent Vivier #endif 17509745512cSArjan van de Ven clear_all_latency_tracing(p); 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds /* ok, now we should be set up.. */ 175318c830dfSOleg Nesterov p->pid = pid_nr(pid); 175418c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 17555f8aadd8SOleg Nesterov p->exit_signal = -1; 175618c830dfSOleg Nesterov p->group_leader = current->group_leader; 175718c830dfSOleg Nesterov p->tgid = current->tgid; 175818c830dfSOleg Nesterov } else { 175918c830dfSOleg Nesterov if (clone_flags & CLONE_PARENT) 17605f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 17615f8aadd8SOleg Nesterov else 17625f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 176318c830dfSOleg Nesterov p->group_leader = p; 176418c830dfSOleg Nesterov p->tgid = p->pid; 176518c830dfSOleg Nesterov } 17665f8aadd8SOleg Nesterov 17679d823e8fSWu Fengguang p->nr_dirtied = 0; 17689d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 176983712358SWu Fengguang p->dirty_paused_when = 0; 17709d823e8fSWu Fengguang 1771bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 177247e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 1773158e1645SAl Viro p->task_works = NULL; 17741da177e4SLinus Torvalds 1775780de9ddSIngo Molnar cgroup_threadgroup_change_begin(current); 177618c830dfSOleg Nesterov /* 17777e47682eSAleksa Sarai * Ensure that the cgroup subsystem policies allow the new process to be 17787e47682eSAleksa Sarai * forked. It should be noted the the new process's css_set can be changed 17797e47682eSAleksa Sarai * between here and cgroup_post_fork() if an organisation operation is in 17807e47682eSAleksa Sarai * progress. 17817e47682eSAleksa Sarai */ 1782b53202e6SOleg Nesterov retval = cgroup_can_fork(p); 17837e47682eSAleksa Sarai if (retval) 17847e47682eSAleksa Sarai goto bad_fork_free_pid; 17857e47682eSAleksa Sarai 17867e47682eSAleksa Sarai /* 178718c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 178818c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 178918c830dfSOleg Nesterov */ 17901da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 17932d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 17941da177e4SLinus Torvalds p->real_parent = current->real_parent; 17952d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 17962d5516cbSOleg Nesterov } else { 17971da177e4SLinus Torvalds p->real_parent = current; 17982d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 17992d5516cbSOleg Nesterov } 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 18024a2c7a78SOleg Nesterov 18034a2c7a78SOleg Nesterov /* 1804dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 1805dbd95212SKees Cook * before holding sighand lock. 1806dbd95212SKees Cook */ 1807dbd95212SKees Cook copy_seccomp(p); 1808dbd95212SKees Cook 1809dbd95212SKees Cook /* 18104a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 18114a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 18124a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 18134a2c7a78SOleg Nesterov * it's process group. 18144a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 18154a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 18164a2c7a78SOleg Nesterov */ 18174a2c7a78SOleg Nesterov recalc_sigpending(); 18184a2c7a78SOleg Nesterov if (signal_pending(current)) { 18194a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 18204a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 18214a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 18227e47682eSAleksa Sarai goto bad_fork_cancel_cgroup; 18234a2c7a78SOleg Nesterov } 18244a2c7a78SOleg Nesterov 182573b9ebfeSOleg Nesterov if (likely(p->pid)) { 18264b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 18271da177e4SLinus Torvalds 182881907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 18291da177e4SLinus Torvalds if (thread_group_leader(p)) { 183081907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 183181907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 183281907739SOleg Nesterov 18331c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 183417cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 18351c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 18361c4042c2SEric W. Biederman } 18375cd17569SEric W. Biederman 1838fea9d175SOleg Nesterov p->signal->leader_pid = pid; 18399c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 1840749860ceSPavel Tikhomirov /* 1841749860ceSPavel Tikhomirov * Inherit has_child_subreaper flag under the same 1842749860ceSPavel Tikhomirov * tasklist_lock with adding child to the process tree 1843749860ceSPavel Tikhomirov * for propagate_has_child_subreaper optimization. 1844749860ceSPavel Tikhomirov */ 1845749860ceSPavel Tikhomirov p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 1846749860ceSPavel Tikhomirov p->real_parent->signal->is_child_subreaper; 18479cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 18485e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 184981907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 185081907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 1851909ea964SChristoph Lameter __this_cpu_inc(process_counts); 185280628ca0SOleg Nesterov } else { 185380628ca0SOleg Nesterov current->signal->nr_threads++; 185480628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 185580628ca0SOleg Nesterov atomic_inc(¤t->signal->sigcnt); 185680628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 185780628ca0SOleg Nesterov &p->group_leader->thread_group); 18580c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 18590c740d0aSOleg Nesterov &p->signal->thread_head); 18601da177e4SLinus Torvalds } 186181907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 18621da177e4SLinus Torvalds nr_threads++; 186373b9ebfeSOleg Nesterov } 186473b9ebfeSOleg Nesterov 18651da177e4SLinus Torvalds total_forks++; 18663f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 18674af4206bSOleg Nesterov syscall_tracepoint_update(p); 18681da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 18694af4206bSOleg Nesterov 1870c13cf856SAndrew Morton proc_fork_connector(p); 1871b53202e6SOleg Nesterov cgroup_post_fork(p); 1872780de9ddSIngo Molnar cgroup_threadgroup_change_end(current); 1873cdd6c482SIngo Molnar perf_event_fork(p); 187443d2b113SKAMEZAWA Hiroyuki 187543d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 18763ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 187743d2b113SKAMEZAWA Hiroyuki 18781da177e4SLinus Torvalds return p; 18791da177e4SLinus Torvalds 18807e47682eSAleksa Sarai bad_fork_cancel_cgroup: 1881b53202e6SOleg Nesterov cgroup_cancel_fork(p); 1882425fb2b4SPavel Emelyanov bad_fork_free_pid: 1883780de9ddSIngo Molnar cgroup_threadgroup_change_end(current); 1884425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1885425fb2b4SPavel Emelyanov free_pid(pid); 18860740aa5fSJiri Slaby bad_fork_cleanup_thread: 18870740aa5fSJiri Slaby exit_thread(p); 1888fd0928dfSJens Axboe bad_fork_cleanup_io: 1889b69f2292SLouis Rilling if (p->io_context) 1890b69f2292SLouis Rilling exit_io_context(p); 1891ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1892444f378bSLinus Torvalds exit_task_namespaces(p); 18931da177e4SLinus Torvalds bad_fork_cleanup_mm: 1894c9f01245SDavid Rientjes if (p->mm) 18951da177e4SLinus Torvalds mmput(p->mm); 18961da177e4SLinus Torvalds bad_fork_cleanup_signal: 18974ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 18981c5354deSMike Galbraith free_signal_struct(p->signal); 18991da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1900a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 19011da177e4SLinus Torvalds bad_fork_cleanup_fs: 19021da177e4SLinus Torvalds exit_fs(p); /* blocking */ 19031da177e4SLinus Torvalds bad_fork_cleanup_files: 19041da177e4SLinus Torvalds exit_files(p); /* blocking */ 19051da177e4SLinus Torvalds bad_fork_cleanup_semundo: 19061da177e4SLinus Torvalds exit_sem(p); 19071da177e4SLinus Torvalds bad_fork_cleanup_audit: 19081da177e4SLinus Torvalds audit_free(p); 19096c72e350SPeter Zijlstra bad_fork_cleanup_perf: 1910cdd6c482SIngo Molnar perf_event_free_task(p); 19116c72e350SPeter Zijlstra bad_fork_cleanup_policy: 19121da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1913f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1914e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock: 19151da177e4SLinus Torvalds #endif 191635df17c5SShailabh Nagar delayacct_tsk_free(p); 19171da177e4SLinus Torvalds bad_fork_cleanup_count: 1918d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1919e0e81739SDavid Howells exit_creds(p); 19201da177e4SLinus Torvalds bad_fork_free: 1921405c0759SAndy Lutomirski p->state = TASK_DEAD; 192268f24b08SAndy Lutomirski put_task_stack(p); 19231da177e4SLinus Torvalds free_task(p); 1924fe7d37d1SOleg Nesterov fork_out: 1925fe7d37d1SOleg Nesterov return ERR_PTR(retval); 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 1928f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1929f106eee1SOleg Nesterov { 1930f106eee1SOleg Nesterov enum pid_type type; 1931f106eee1SOleg Nesterov 1932f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1933f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1934f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1935f106eee1SOleg Nesterov } 1936f106eee1SOleg Nesterov } 1937f106eee1SOleg Nesterov 19380db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 19391da177e4SLinus Torvalds { 194036c8b586SIngo Molnar struct task_struct *task; 1941725fc629SAndi Kleen task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0, 1942725fc629SAndi Kleen cpu_to_node(cpu)); 1943f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1944f106eee1SOleg Nesterov init_idle_pids(task->pids); 19451da177e4SLinus Torvalds init_idle(task, cpu); 1946f106eee1SOleg Nesterov } 194773b9ebfeSOleg Nesterov 19481da177e4SLinus Torvalds return task; 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 19511da177e4SLinus Torvalds /* 19521da177e4SLinus Torvalds * Ok, this is the main fork-routine. 19531da177e4SLinus Torvalds * 19541da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 19551da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 19561da177e4SLinus Torvalds */ 19573033f14aSJosh Triplett long _do_fork(unsigned long clone_flags, 19581da177e4SLinus Torvalds unsigned long stack_start, 19591da177e4SLinus Torvalds unsigned long stack_size, 19601da177e4SLinus Torvalds int __user *parent_tidptr, 19613033f14aSJosh Triplett int __user *child_tidptr, 19623033f14aSJosh Triplett unsigned long tls) 19631da177e4SLinus Torvalds { 19641da177e4SLinus Torvalds struct task_struct *p; 19651da177e4SLinus Torvalds int trace = 0; 196692476d7fSEric W. Biederman long nr; 19671da177e4SLinus Torvalds 1968bdff746aSAndrew Morton /* 19694b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 19704b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 19714b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 19724b9d33e6STejun Heo * for the type of forking is enabled. 197309a05394SRoland McGrath */ 1974e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 19754b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 19764b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 19774b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 19784b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 19794b9d33e6STejun Heo else 19804b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 19814b9d33e6STejun Heo 19824b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 19834b9d33e6STejun Heo trace = 0; 19844b9d33e6STejun Heo } 19851da177e4SLinus Torvalds 198662e791c1SAl Viro p = copy_process(clone_flags, stack_start, stack_size, 1987725fc629SAndi Kleen child_tidptr, NULL, trace, tls, NUMA_NO_NODE); 198838addce8SEmese Revfy add_latent_entropy(); 19891da177e4SLinus Torvalds /* 19901da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 19911da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 19921da177e4SLinus Torvalds */ 19931da177e4SLinus Torvalds if (!IS_ERR(p)) { 19941da177e4SLinus Torvalds struct completion vfork; 19954e52365fSMatthew Dempsky struct pid *pid; 19961da177e4SLinus Torvalds 19970a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 19980a16b607SMathieu Desnoyers 19994e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 20004e52365fSMatthew Dempsky nr = pid_vnr(pid); 200130e49c26SPavel Emelyanov 200230e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 200330e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 2004a6f5e063SSukadev Bhattiprolu 20051da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 20061da177e4SLinus Torvalds p->vfork_done = &vfork; 20071da177e4SLinus Torvalds init_completion(&vfork); 2008d68b46feSOleg Nesterov get_task_struct(p); 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds 20113e51e3edSSamir Bellabes wake_up_new_task(p); 20121da177e4SLinus Torvalds 20134b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 20144b9d33e6STejun Heo if (unlikely(trace)) 20154e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 201609a05394SRoland McGrath 20171da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 2018d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 20194e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 20209f59ce5dSChuck Ebbert } 20214e52365fSMatthew Dempsky 20224e52365fSMatthew Dempsky put_pid(pid); 20231da177e4SLinus Torvalds } else { 202492476d7fSEric W. Biederman nr = PTR_ERR(p); 20251da177e4SLinus Torvalds } 202692476d7fSEric W. Biederman return nr; 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds 20293033f14aSJosh Triplett #ifndef CONFIG_HAVE_COPY_THREAD_TLS 20303033f14aSJosh Triplett /* For compatibility with architectures that call do_fork directly rather than 20313033f14aSJosh Triplett * using the syscall entry points below. */ 20323033f14aSJosh Triplett long do_fork(unsigned long clone_flags, 20333033f14aSJosh Triplett unsigned long stack_start, 20343033f14aSJosh Triplett unsigned long stack_size, 20353033f14aSJosh Triplett int __user *parent_tidptr, 20363033f14aSJosh Triplett int __user *child_tidptr) 20373033f14aSJosh Triplett { 20383033f14aSJosh Triplett return _do_fork(clone_flags, stack_start, stack_size, 20393033f14aSJosh Triplett parent_tidptr, child_tidptr, 0); 20403033f14aSJosh Triplett } 20413033f14aSJosh Triplett #endif 20423033f14aSJosh Triplett 20432aa3a7f8SAl Viro /* 20442aa3a7f8SAl Viro * Create a kernel thread. 20452aa3a7f8SAl Viro */ 20462aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 20472aa3a7f8SAl Viro { 20483033f14aSJosh Triplett return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, 20493033f14aSJosh Triplett (unsigned long)arg, NULL, NULL, 0); 20502aa3a7f8SAl Viro } 20512aa3a7f8SAl Viro 2052d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 2053d2125043SAl Viro SYSCALL_DEFINE0(fork) 2054d2125043SAl Viro { 2055d2125043SAl Viro #ifdef CONFIG_MMU 20563033f14aSJosh Triplett return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0); 2057d2125043SAl Viro #else 2058d2125043SAl Viro /* can not support in nommu mode */ 20595d59e182SDaeseok Youn return -EINVAL; 2060d2125043SAl Viro #endif 2061d2125043SAl Viro } 2062d2125043SAl Viro #endif 2063d2125043SAl Viro 2064d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 2065d2125043SAl Viro SYSCALL_DEFINE0(vfork) 2066d2125043SAl Viro { 20673033f14aSJosh Triplett return _do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 20683033f14aSJosh Triplett 0, NULL, NULL, 0); 2069d2125043SAl Viro } 2070d2125043SAl Viro #endif 2071d2125043SAl Viro 2072d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 2073d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 2074d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2075d2125043SAl Viro int __user *, parent_tidptr, 20763033f14aSJosh Triplett unsigned long, tls, 2077d2125043SAl Viro int __user *, child_tidptr) 2078d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 2079d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 2080d2125043SAl Viro int __user *, parent_tidptr, 2081d2125043SAl Viro int __user *, child_tidptr, 20823033f14aSJosh Triplett unsigned long, tls) 2083dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 2084dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 2085dfa9771aSMichal Simek int, stack_size, 2086dfa9771aSMichal Simek int __user *, parent_tidptr, 2087dfa9771aSMichal Simek int __user *, child_tidptr, 20883033f14aSJosh Triplett unsigned long, tls) 2089d2125043SAl Viro #else 2090d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2091d2125043SAl Viro int __user *, parent_tidptr, 2092d2125043SAl Viro int __user *, child_tidptr, 20933033f14aSJosh Triplett unsigned long, tls) 2094d2125043SAl Viro #endif 2095d2125043SAl Viro { 20963033f14aSJosh Triplett return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls); 2097d2125043SAl Viro } 2098d2125043SAl Viro #endif 2099d2125043SAl Viro 21000f1b92cbSOleg Nesterov void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) 21010f1b92cbSOleg Nesterov { 21020f1b92cbSOleg Nesterov struct task_struct *leader, *parent, *child; 21030f1b92cbSOleg Nesterov int res; 21040f1b92cbSOleg Nesterov 21050f1b92cbSOleg Nesterov read_lock(&tasklist_lock); 21060f1b92cbSOleg Nesterov leader = top = top->group_leader; 21070f1b92cbSOleg Nesterov down: 21080f1b92cbSOleg Nesterov for_each_thread(leader, parent) { 21090f1b92cbSOleg Nesterov list_for_each_entry(child, &parent->children, sibling) { 21100f1b92cbSOleg Nesterov res = visitor(child, data); 21110f1b92cbSOleg Nesterov if (res) { 21120f1b92cbSOleg Nesterov if (res < 0) 21130f1b92cbSOleg Nesterov goto out; 21140f1b92cbSOleg Nesterov leader = child; 21150f1b92cbSOleg Nesterov goto down; 21160f1b92cbSOleg Nesterov } 21170f1b92cbSOleg Nesterov up: 21180f1b92cbSOleg Nesterov ; 21190f1b92cbSOleg Nesterov } 21200f1b92cbSOleg Nesterov } 21210f1b92cbSOleg Nesterov 21220f1b92cbSOleg Nesterov if (leader != top) { 21230f1b92cbSOleg Nesterov child = leader; 21240f1b92cbSOleg Nesterov parent = child->real_parent; 21250f1b92cbSOleg Nesterov leader = parent->group_leader; 21260f1b92cbSOleg Nesterov goto up; 21270f1b92cbSOleg Nesterov } 21280f1b92cbSOleg Nesterov out: 21290f1b92cbSOleg Nesterov read_unlock(&tasklist_lock); 21300f1b92cbSOleg Nesterov } 21310f1b92cbSOleg Nesterov 21325fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 21335fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 21345fd63b30SRavikiran G Thirumalai #endif 21355fd63b30SRavikiran G Thirumalai 213651cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 2137aa1757f9SOleg Nesterov { 2138aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 2139aa1757f9SOleg Nesterov 2140aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 2141b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 2142fba2afaaSDavide Libenzi } 2143aa1757f9SOleg Nesterov 21441da177e4SLinus Torvalds void __init proc_caches_init(void) 21451da177e4SLinus Torvalds { 21461da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 21471da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 21482dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 21495d097056SVladimir Davydov SLAB_NOTRACK|SLAB_ACCOUNT, sighand_ctor); 21501da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 21511da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 21525d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 21535d097056SVladimir Davydov NULL); 21541da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 21551da177e4SLinus Torvalds sizeof(struct files_struct), 0, 21565d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 21575d097056SVladimir Davydov NULL); 21581da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 21591da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 21605d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 21615d097056SVladimir Davydov NULL); 21626345d24dSLinus Torvalds /* 21636345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 21646345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 21656345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 21666345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 21676345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 21686345d24dSLinus Torvalds */ 21691da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 21705fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 21715d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 21725d097056SVladimir Davydov NULL); 21735d097056SVladimir Davydov vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 21748feae131SDavid Howells mmap_init(); 217566577193SAl Viro nsproxy_cache_init(); 21761da177e4SLinus Torvalds } 2177cf2e340fSJANAK DESAI 2178cf2e340fSJANAK DESAI /* 21799bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 2180cf2e340fSJANAK DESAI */ 21819bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 2182cf2e340fSJANAK DESAI { 21839bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 21849bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 218550804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 2186a79a908fSAditya Kali CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP)) 2187cf2e340fSJANAK DESAI return -EINVAL; 21889bfb23fcSOleg Nesterov /* 218912c641abSEric W. Biederman * Not implemented, but pretend it works if there is nothing 219012c641abSEric W. Biederman * to unshare. Note that unsharing the address space or the 219112c641abSEric W. Biederman * signal handlers also need to unshare the signal queues (aka 219212c641abSEric W. Biederman * CLONE_THREAD). 21939bfb23fcSOleg Nesterov */ 21949bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 219512c641abSEric W. Biederman if (!thread_group_empty(current)) 219612c641abSEric W. Biederman return -EINVAL; 219712c641abSEric W. Biederman } 219812c641abSEric W. Biederman if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 219912c641abSEric W. Biederman if (atomic_read(¤t->sighand->count) > 1) 220012c641abSEric W. Biederman return -EINVAL; 220112c641abSEric W. Biederman } 220212c641abSEric W. Biederman if (unshare_flags & CLONE_VM) { 220312c641abSEric W. Biederman if (!current_is_single_threaded()) 22049bfb23fcSOleg Nesterov return -EINVAL; 22059bfb23fcSOleg Nesterov } 2206cf2e340fSJANAK DESAI 2207cf2e340fSJANAK DESAI return 0; 2208cf2e340fSJANAK DESAI } 2209cf2e340fSJANAK DESAI 2210cf2e340fSJANAK DESAI /* 221199d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 2212cf2e340fSJANAK DESAI */ 2213cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 2214cf2e340fSJANAK DESAI { 2215cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 2216cf2e340fSJANAK DESAI 2217498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 2218498052bbSAl Viro return 0; 2219498052bbSAl Viro 2220498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 2221498052bbSAl Viro if (fs->users == 1) 2222498052bbSAl Viro return 0; 2223498052bbSAl Viro 2224498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 222599d1419dSJANAK DESAI if (!*new_fsp) 222699d1419dSJANAK DESAI return -ENOMEM; 2227cf2e340fSJANAK DESAI 2228cf2e340fSJANAK DESAI return 0; 2229cf2e340fSJANAK DESAI } 2230cf2e340fSJANAK DESAI 2231cf2e340fSJANAK DESAI /* 2232a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 2233cf2e340fSJANAK DESAI */ 2234cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 2235cf2e340fSJANAK DESAI { 2236cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 2237a016f338SJANAK DESAI int error = 0; 2238cf2e340fSJANAK DESAI 2239cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 2240a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 2241a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 2242a016f338SJANAK DESAI if (!*new_fdp) 2243a016f338SJANAK DESAI return error; 2244a016f338SJANAK DESAI } 2245cf2e340fSJANAK DESAI 2246cf2e340fSJANAK DESAI return 0; 2247cf2e340fSJANAK DESAI } 2248cf2e340fSJANAK DESAI 2249cf2e340fSJANAK DESAI /* 2250cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 2251cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 2252cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 2253cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 2254cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 2255cf2e340fSJANAK DESAI * task_struct. 2256cf2e340fSJANAK DESAI */ 22576559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 2258cf2e340fSJANAK DESAI { 2259cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 2260cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 2261b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 2262cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 22639edff4abSManfred Spraul int do_sysvsem = 0; 22649bfb23fcSOleg Nesterov int err; 2265cf2e340fSJANAK DESAI 226650804fe3SEric W. Biederman /* 2267faf00da5SEric W. Biederman * If unsharing a user namespace must also unshare the thread group 2268faf00da5SEric W. Biederman * and unshare the filesystem root and working directories. 2269b2e0d987SEric W. Biederman */ 2270b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 2271e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 2272b2e0d987SEric W. Biederman /* 227350804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 227450804fe3SEric W. Biederman */ 227550804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 227650804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 22776013f67fSManfred Spraul /* 227812c641abSEric W. Biederman * If unsharing a signal handlers, must also unshare the signal queues. 227912c641abSEric W. Biederman */ 228012c641abSEric W. Biederman if (unshare_flags & CLONE_SIGHAND) 228112c641abSEric W. Biederman unshare_flags |= CLONE_THREAD; 228212c641abSEric W. Biederman /* 22839bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 22849bfb23fcSOleg Nesterov */ 22859bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 22869bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 228750804fe3SEric W. Biederman 228850804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 228950804fe3SEric W. Biederman if (err) 229050804fe3SEric W. Biederman goto bad_unshare_out; 22919bfb23fcSOleg Nesterov /* 22926013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 22936013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 22946013f67fSManfred Spraul * namespace are unreachable. 22956013f67fSManfred Spraul */ 22966013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 22979edff4abSManfred Spraul do_sysvsem = 1; 2298fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 2299fb0a685cSDaniel Rebelo de Oliveira if (err) 23009bfb23fcSOleg Nesterov goto bad_unshare_out; 2301fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 2302fb0a685cSDaniel Rebelo de Oliveira if (err) 23039bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 2304b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 2305fb0a685cSDaniel Rebelo de Oliveira if (err) 23069edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 2307b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 2308b2e0d987SEric W. Biederman new_cred, new_fs); 2309b2e0d987SEric W. Biederman if (err) 2310b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 2311cf2e340fSJANAK DESAI 2312b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 23139edff4abSManfred Spraul if (do_sysvsem) { 23149edff4abSManfred Spraul /* 23159edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 23169edff4abSManfred Spraul */ 23179edff4abSManfred Spraul exit_sem(current); 23189edff4abSManfred Spraul } 2319ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 2320ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 2321ab602f79SJack Miller exit_shm(current); 2322ab602f79SJack Miller shm_init_task(current); 2323ab602f79SJack Miller } 2324ab516013SSerge E. Hallyn 23256f977e6bSAlan Cox if (new_nsproxy) 2326cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 2327cf2e340fSJANAK DESAI 2328cf7b708cSPavel Emelyanov task_lock(current); 2329cf7b708cSPavel Emelyanov 2330cf2e340fSJANAK DESAI if (new_fs) { 2331cf2e340fSJANAK DESAI fs = current->fs; 23322a4419b5SNick Piggin spin_lock(&fs->lock); 2333cf2e340fSJANAK DESAI current->fs = new_fs; 2334498052bbSAl Viro if (--fs->users) 2335498052bbSAl Viro new_fs = NULL; 2336498052bbSAl Viro else 2337cf2e340fSJANAK DESAI new_fs = fs; 23382a4419b5SNick Piggin spin_unlock(&fs->lock); 2339cf2e340fSJANAK DESAI } 2340cf2e340fSJANAK DESAI 2341cf2e340fSJANAK DESAI if (new_fd) { 2342cf2e340fSJANAK DESAI fd = current->files; 2343cf2e340fSJANAK DESAI current->files = new_fd; 2344cf2e340fSJANAK DESAI new_fd = fd; 2345cf2e340fSJANAK DESAI } 2346cf2e340fSJANAK DESAI 2347cf2e340fSJANAK DESAI task_unlock(current); 2348b2e0d987SEric W. Biederman 2349b2e0d987SEric W. Biederman if (new_cred) { 2350b2e0d987SEric W. Biederman /* Install the new user namespace */ 2351b2e0d987SEric W. Biederman commit_creds(new_cred); 2352b2e0d987SEric W. Biederman new_cred = NULL; 2353b2e0d987SEric W. Biederman } 2354cf2e340fSJANAK DESAI } 2355cf2e340fSJANAK DESAI 2356b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 2357b2e0d987SEric W. Biederman if (new_cred) 2358b2e0d987SEric W. Biederman put_cred(new_cred); 2359cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 2360cf2e340fSJANAK DESAI if (new_fd) 2361cf2e340fSJANAK DESAI put_files_struct(new_fd); 2362cf2e340fSJANAK DESAI 2363cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 2364cf2e340fSJANAK DESAI if (new_fs) 2365498052bbSAl Viro free_fs_struct(new_fs); 2366cf2e340fSJANAK DESAI 2367cf2e340fSJANAK DESAI bad_unshare_out: 2368cf2e340fSJANAK DESAI return err; 2369cf2e340fSJANAK DESAI } 23703b125388SAl Viro 23713b125388SAl Viro /* 23723b125388SAl Viro * Helper to unshare the files of the current task. 23733b125388SAl Viro * We don't want to expose copy_files internals to 23743b125388SAl Viro * the exec layer of the kernel. 23753b125388SAl Viro */ 23763b125388SAl Viro 23773b125388SAl Viro int unshare_files(struct files_struct **displaced) 23783b125388SAl Viro { 23793b125388SAl Viro struct task_struct *task = current; 238050704516SAl Viro struct files_struct *copy = NULL; 23813b125388SAl Viro int error; 23823b125388SAl Viro 23833b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 23843b125388SAl Viro if (error || !copy) { 23853b125388SAl Viro *displaced = NULL; 23863b125388SAl Viro return error; 23873b125388SAl Viro } 23883b125388SAl Viro *displaced = task->files; 23893b125388SAl Viro task_lock(task); 23903b125388SAl Viro task->files = copy; 23913b125388SAl Viro task_unlock(task); 23923b125388SAl Viro return 0; 23933b125388SAl Viro } 239416db3d3fSHeinrich Schuchardt 239516db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write, 239616db3d3fSHeinrich Schuchardt void __user *buffer, size_t *lenp, loff_t *ppos) 239716db3d3fSHeinrich Schuchardt { 239816db3d3fSHeinrich Schuchardt struct ctl_table t; 239916db3d3fSHeinrich Schuchardt int ret; 240016db3d3fSHeinrich Schuchardt int threads = max_threads; 240116db3d3fSHeinrich Schuchardt int min = MIN_THREADS; 240216db3d3fSHeinrich Schuchardt int max = MAX_THREADS; 240316db3d3fSHeinrich Schuchardt 240416db3d3fSHeinrich Schuchardt t = *table; 240516db3d3fSHeinrich Schuchardt t.data = &threads; 240616db3d3fSHeinrich Schuchardt t.extra1 = &min; 240716db3d3fSHeinrich Schuchardt t.extra2 = &max; 240816db3d3fSHeinrich Schuchardt 240916db3d3fSHeinrich Schuchardt ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 241016db3d3fSHeinrich Schuchardt if (ret || !write) 241116db3d3fSHeinrich Schuchardt return ret; 241216db3d3fSHeinrich Schuchardt 241316db3d3fSHeinrich Schuchardt set_max_threads(threads); 241416db3d3fSHeinrich Schuchardt 241516db3d3fSHeinrich Schuchardt return 0; 241616db3d3fSHeinrich Schuchardt } 2417