11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/fork.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * 'fork.c' contains the help-routines for the 'fork' system call 91da177e4SLinus Torvalds * (see also entry.S and others). 101da177e4SLinus Torvalds * Fork is rather simple, once you get the hang of it, but the memory 111da177e4SLinus Torvalds * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/slab.h> 151da177e4SLinus Torvalds #include <linux/init.h> 161da177e4SLinus Torvalds #include <linux/unistd.h> 171da177e4SLinus Torvalds #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/vmalloc.h> 191da177e4SLinus Torvalds #include <linux/completion.h> 201da177e4SLinus Torvalds #include <linux/personality.h> 211da177e4SLinus Torvalds #include <linux/mempolicy.h> 221da177e4SLinus Torvalds #include <linux/sem.h> 231da177e4SLinus Torvalds #include <linux/file.h> 249f3acc31SAl Viro #include <linux/fdtable.h> 25da9cbc87SJens Axboe #include <linux/iocontext.h> 261da177e4SLinus Torvalds #include <linux/key.h> 271da177e4SLinus Torvalds #include <linux/binfmts.h> 281da177e4SLinus Torvalds #include <linux/mman.h> 29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 301da177e4SLinus Torvalds #include <linux/fs.h> 31ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 32c59ede7bSRandy.Dunlap #include <linux/capability.h> 331da177e4SLinus Torvalds #include <linux/cpu.h> 34b4f48b63SPaul Menage #include <linux/cgroup.h> 351da177e4SLinus Torvalds #include <linux/security.h> 36a1e78772SMel Gorman #include <linux/hugetlb.h> 371da177e4SLinus Torvalds #include <linux/swap.h> 381da177e4SLinus Torvalds #include <linux/syscalls.h> 391da177e4SLinus Torvalds #include <linux/jiffies.h> 4009a05394SRoland McGrath #include <linux/tracehook.h> 411da177e4SLinus Torvalds #include <linux/futex.h> 428141c7f3SLinus Torvalds #include <linux/compat.h> 437c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 44ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 451da177e4SLinus Torvalds #include <linux/ptrace.h> 461da177e4SLinus Torvalds #include <linux/mount.h> 471da177e4SLinus Torvalds #include <linux/audit.h> 4878fb7466SPavel Emelianov #include <linux/memcontrol.h> 49f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 501da177e4SLinus Torvalds #include <linux/profile.h> 511da177e4SLinus Torvalds #include <linux/rmap.h> 521da177e4SLinus Torvalds #include <linux/acct.h> 538f0ab514SJay Lan #include <linux/tsacct_kern.h> 549f46080cSMatt Helsley #include <linux/cn_proc.h> 55ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 56ca74e92bSShailabh Nagar #include <linux/delayacct.h> 57ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 580a425405SArjan van de Ven #include <linux/random.h> 59522ed776SMiloslav Trmac #include <linux/tty.h> 606f4e6433SPavel Emelyanov #include <linux/proc_fs.h> 61fd0928dfSJens Axboe #include <linux/blkdev.h> 625ad4e53bSAl Viro #include <linux/fs_struct.h> 637c9f8861SEric Sandeen #include <linux/magic.h> 64*cdd6c482SIngo Molnar #include <linux/perf_event.h> 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #include <asm/pgtable.h> 671da177e4SLinus Torvalds #include <asm/pgalloc.h> 681da177e4SLinus Torvalds #include <asm/uaccess.h> 691da177e4SLinus Torvalds #include <asm/mmu_context.h> 701da177e4SLinus Torvalds #include <asm/cacheflush.h> 711da177e4SLinus Torvalds #include <asm/tlbflush.h> 721da177e4SLinus Torvalds 73ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 74ad8d75ffSSteven Rostedt 751da177e4SLinus Torvalds /* 761da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 771da177e4SLinus Torvalds */ 781da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 791da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds int nr_processes(void) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds int cpu; 901da177e4SLinus Torvalds int total = 0; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds for_each_online_cpu(cpu) 931da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds return total; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 991da177e4SLinus Torvalds # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL) 1001da177e4SLinus Torvalds # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk)) 101e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 1021da177e4SLinus Torvalds #endif 1031da177e4SLinus Torvalds 104b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR 105b69c49b7SFUJITA Tomonori static inline struct thread_info *alloc_thread_info(struct task_struct *tsk) 106b69c49b7SFUJITA Tomonori { 107b69c49b7SFUJITA Tomonori #ifdef CONFIG_DEBUG_STACK_USAGE 108b69c49b7SFUJITA Tomonori gfp_t mask = GFP_KERNEL | __GFP_ZERO; 109b69c49b7SFUJITA Tomonori #else 110b69c49b7SFUJITA Tomonori gfp_t mask = GFP_KERNEL; 111b69c49b7SFUJITA Tomonori #endif 112b69c49b7SFUJITA Tomonori return (struct thread_info *)__get_free_pages(mask, THREAD_SIZE_ORDER); 113b69c49b7SFUJITA Tomonori } 114b69c49b7SFUJITA Tomonori 115b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti) 116b69c49b7SFUJITA Tomonori { 117b69c49b7SFUJITA Tomonori free_pages((unsigned long)ti, THREAD_SIZE_ORDER); 118b69c49b7SFUJITA Tomonori } 119b69c49b7SFUJITA Tomonori #endif 120b69c49b7SFUJITA Tomonori 1211da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 122e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 125e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 128e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 131e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 134e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 137e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 1401da177e4SLinus Torvalds { 1413e26c149SPeter Zijlstra prop_local_destroy_single(&tsk->dirties); 142f7e4217bSRoman Zippel free_thread_info(tsk->stack); 14323f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 144fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 1451da177e4SLinus Torvalds free_task_struct(tsk); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 1481da177e4SLinus Torvalds 149158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 1501da177e4SLinus Torvalds { 151270f722dSEugene Teo WARN_ON(!tsk->exit_state); 1521da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 1531da177e4SLinus Torvalds WARN_ON(tsk == current); 1541da177e4SLinus Torvalds 155e0e81739SDavid Howells exit_creds(tsk); 15635df17c5SShailabh Nagar delayacct_tsk_free(tsk); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 1591da177e4SLinus Torvalds free_task(tsk); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1622adee9b3SSuresh Siddha /* 1632adee9b3SSuresh Siddha * macro override instead of weak attribute alias, to workaround 1642adee9b3SSuresh Siddha * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions. 1652adee9b3SSuresh Siddha */ 1662adee9b3SSuresh Siddha #ifndef arch_task_cache_init 1672adee9b3SSuresh Siddha #define arch_task_cache_init() 1682adee9b3SSuresh Siddha #endif 16961c4628bSSuresh Siddha 1701da177e4SLinus Torvalds void __init fork_init(unsigned long mempages) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 1731da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 1741da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 1751da177e4SLinus Torvalds #endif 1761da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 1771da177e4SLinus Torvalds task_struct_cachep = 1781da177e4SLinus Torvalds kmem_cache_create("task_struct", sizeof(struct task_struct), 1792dff4405SVegard Nossum ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL); 1801da177e4SLinus Torvalds #endif 1811da177e4SLinus Torvalds 18261c4628bSSuresh Siddha /* do the arch specific task caches init */ 18361c4628bSSuresh Siddha arch_task_cache_init(); 18461c4628bSSuresh Siddha 1851da177e4SLinus Torvalds /* 1861da177e4SLinus Torvalds * The default maximum number of threads is set to a safe 1871da177e4SLinus Torvalds * value: the thread structures can take up at most half 1881da177e4SLinus Torvalds * of memory. 1891da177e4SLinus Torvalds */ 1901da177e4SLinus Torvalds max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* 1931da177e4SLinus Torvalds * we need to allow at least 20 threads to boot a system 1941da177e4SLinus Torvalds */ 1951da177e4SLinus Torvalds if(max_threads < 20) 1961da177e4SLinus Torvalds max_threads = 20; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 1991da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 2001da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 2011da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 20461c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst, 20561c4628bSSuresh Siddha struct task_struct *src) 20661c4628bSSuresh Siddha { 20761c4628bSSuresh Siddha *dst = *src; 20861c4628bSSuresh Siddha return 0; 20961c4628bSSuresh Siddha } 21061c4628bSSuresh Siddha 2111da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds struct task_struct *tsk; 2141da177e4SLinus Torvalds struct thread_info *ti; 2157c9f8861SEric Sandeen unsigned long *stackend; 2167c9f8861SEric Sandeen 2173e26c149SPeter Zijlstra int err; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds prepare_to_copy(orig); 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds tsk = alloc_task_struct(); 2221da177e4SLinus Torvalds if (!tsk) 2231da177e4SLinus Torvalds return NULL; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds ti = alloc_thread_info(tsk); 2261da177e4SLinus Torvalds if (!ti) { 2271da177e4SLinus Torvalds free_task_struct(tsk); 2281da177e4SLinus Torvalds return NULL; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 23161c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 23261c4628bSSuresh Siddha if (err) 23361c4628bSSuresh Siddha goto out; 23461c4628bSSuresh Siddha 235f7e4217bSRoman Zippel tsk->stack = ti; 2363e26c149SPeter Zijlstra 2373e26c149SPeter Zijlstra err = prop_local_init_single(&tsk->dirties); 23861c4628bSSuresh Siddha if (err) 23961c4628bSSuresh Siddha goto out; 2403e26c149SPeter Zijlstra 24110ebffdeSAl Viro setup_thread_stack(tsk, orig); 2427c9f8861SEric Sandeen stackend = end_of_stack(tsk); 2437c9f8861SEric Sandeen *stackend = STACK_END_MAGIC; /* for overflow detection */ 2441da177e4SLinus Torvalds 2450a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 2460a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 2470a425405SArjan van de Ven #endif 2480a425405SArjan van de Ven 2491da177e4SLinus Torvalds /* One for us, one for whoever does the "release_task()" (usually parent) */ 2501da177e4SLinus Torvalds atomic_set(&tsk->usage,2); 2514b5d37acSGiancarlo Formicuccia atomic_set(&tsk->fs_excl, 0); 2526c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 2532056a782SJens Axboe tsk->btrace_seq = 0; 2546c5c9341SAlexey Dobriyan #endif 255a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 2561da177e4SLinus Torvalds return tsk; 25761c4628bSSuresh Siddha 25861c4628bSSuresh Siddha out: 25961c4628bSSuresh Siddha free_thread_info(ti); 26061c4628bSSuresh Siddha free_task_struct(tsk); 26161c4628bSSuresh Siddha return NULL; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds #ifdef CONFIG_MMU 265a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds struct vm_area_struct *mpnt, *tmp, **pprev; 2681da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 2691da177e4SLinus Torvalds int retval; 2701da177e4SLinus Torvalds unsigned long charge; 2711da177e4SLinus Torvalds struct mempolicy *pol; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 274ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 275ad339451SIngo Molnar /* 276ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 277ad339451SIngo Molnar */ 278ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 2797ee78232SHugh Dickins 2801da177e4SLinus Torvalds mm->locked_vm = 0; 2811da177e4SLinus Torvalds mm->mmap = NULL; 2821da177e4SLinus Torvalds mm->mmap_cache = NULL; 2831da177e4SLinus Torvalds mm->free_area_cache = oldmm->mmap_base; 2841363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 2851da177e4SLinus Torvalds mm->map_count = 0; 28694894244SRusty Russell cpumask_clear(mm_cpumask(mm)); 2871da177e4SLinus Torvalds mm->mm_rb = RB_ROOT; 2881da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 2891da177e4SLinus Torvalds rb_parent = NULL; 2901da177e4SLinus Torvalds pprev = &mm->mmap; 2911da177e4SLinus Torvalds 292fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 2931da177e4SLinus Torvalds struct file *file; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 2963b6bfcdbSHugh Dickins long pages = vma_pages(mpnt); 2973b6bfcdbSHugh Dickins mm->total_vm -= pages; 298ab50b8edSHugh Dickins vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 2993b6bfcdbSHugh Dickins -pages); 3001da177e4SLinus Torvalds continue; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds charge = 0; 3031da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 3041da177e4SLinus Torvalds unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT; 3051da177e4SLinus Torvalds if (security_vm_enough_memory(len)) 3061da177e4SLinus Torvalds goto fail_nomem; 3071da177e4SLinus Torvalds charge = len; 3081da177e4SLinus Torvalds } 309e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 3101da177e4SLinus Torvalds if (!tmp) 3111da177e4SLinus Torvalds goto fail_nomem; 3121da177e4SLinus Torvalds *tmp = *mpnt; 313846a16bfSLee Schermerhorn pol = mpol_dup(vma_policy(mpnt)); 3141da177e4SLinus Torvalds retval = PTR_ERR(pol); 3151da177e4SLinus Torvalds if (IS_ERR(pol)) 3161da177e4SLinus Torvalds goto fail_nomem_policy; 3171da177e4SLinus Torvalds vma_set_policy(tmp, pol); 3181da177e4SLinus Torvalds tmp->vm_flags &= ~VM_LOCKED; 3191da177e4SLinus Torvalds tmp->vm_mm = mm; 3201da177e4SLinus Torvalds tmp->vm_next = NULL; 3211da177e4SLinus Torvalds anon_vma_link(tmp); 3221da177e4SLinus Torvalds file = tmp->vm_file; 3231da177e4SLinus Torvalds if (file) { 324f3a43f3fSJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 325b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 326b88ed205SHugh Dickins 3271da177e4SLinus Torvalds get_file(file); 3281da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 3291da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 330b88ed205SHugh Dickins spin_lock(&mapping->i_mmap_lock); 331b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 332b88ed205SHugh Dickins mapping->i_mmap_writable++; 3331da177e4SLinus Torvalds tmp->vm_truncate_count = mpnt->vm_truncate_count; 334b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 335b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 3361da177e4SLinus Torvalds vma_prio_tree_add(tmp, mpnt); 337b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 338b88ed205SHugh Dickins spin_unlock(&mapping->i_mmap_lock); 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* 342a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 343a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 344a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 345a1e78772SMel Gorman */ 346a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 347a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 348a1e78772SMel Gorman 349a1e78772SMel Gorman /* 3507ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 3511da177e4SLinus Torvalds */ 3521da177e4SLinus Torvalds *pprev = tmp; 3531da177e4SLinus Torvalds pprev = &tmp->vm_next; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 3561da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 3571da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds mm->map_count++; 3600b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 3631da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds if (retval) 3661da177e4SLinus Torvalds goto out; 3671da177e4SLinus Torvalds } 368d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 369d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 3701da177e4SLinus Torvalds retval = 0; 3711da177e4SLinus Torvalds out: 3727ee78232SHugh Dickins up_write(&mm->mmap_sem); 373fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 3741da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 3751da177e4SLinus Torvalds return retval; 3761da177e4SLinus Torvalds fail_nomem_policy: 3771da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 3781da177e4SLinus Torvalds fail_nomem: 3791da177e4SLinus Torvalds retval = -ENOMEM; 3801da177e4SLinus Torvalds vm_unacct_memory(charge); 3811da177e4SLinus Torvalds goto out; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 3871da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 3881da177e4SLinus Torvalds return -ENOMEM; 3891da177e4SLinus Torvalds return 0; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm) 3931da177e4SLinus Torvalds { 3945e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds #else 3971da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 3981da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 3991da177e4SLinus Torvalds #define mm_free_pgd(mm) 4001da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 4031da177e4SLinus Torvalds 404e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 4051da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 4061da177e4SLinus Torvalds 4074cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 4084cb0e11bSHidehiro Kawai 4094cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 4104cb0e11bSHidehiro Kawai { 4114cb0e11bSHidehiro Kawai default_dump_filter = 4124cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 4134cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 4144cb0e11bSHidehiro Kawai return 1; 4154cb0e11bSHidehiro Kawai } 4164cb0e11bSHidehiro Kawai 4174cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 4184cb0e11bSHidehiro Kawai 4191da177e4SLinus Torvalds #include <linux/init_task.h> 4201da177e4SLinus Torvalds 42178fb7466SPavel Emelianov static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p) 4221da177e4SLinus Torvalds { 4231da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 4241da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 4251da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 4261da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 4274cb0e11bSHidehiro Kawai mm->flags = (current->mm) ? current->mm->flags : default_dump_filter; 428999d9fc1SOleg Nesterov mm->core_state = NULL; 4291da177e4SLinus Torvalds mm->nr_ptes = 0; 4304294621fSHugh Dickins set_mm_counter(mm, file_rss, 0); 431404351e6SHugh Dickins set_mm_counter(mm, anon_rss, 0); 4321da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 433abf137ddSJens Axboe spin_lock_init(&mm->ioctx_lock); 434abf137ddSJens Axboe INIT_HLIST_HEAD(&mm->ioctx_list); 4351da177e4SLinus Torvalds mm->free_area_cache = TASK_UNMAPPED_BASE; 4361363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 437cf475ad2SBalbir Singh mm_init_owner(mm, p); 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (likely(!mm_alloc_pgd(mm))) { 4401da177e4SLinus Torvalds mm->def_flags = 0; 441cddb8a5cSAndrea Arcangeli mmu_notifier_mm_init(mm); 4421da177e4SLinus Torvalds return mm; 4431da177e4SLinus Torvalds } 44478fb7466SPavel Emelianov 4451da177e4SLinus Torvalds free_mm(mm); 4461da177e4SLinus Torvalds return NULL; 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* 4501da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 4511da177e4SLinus Torvalds */ 4521da177e4SLinus Torvalds struct mm_struct * mm_alloc(void) 4531da177e4SLinus Torvalds { 4541da177e4SLinus Torvalds struct mm_struct * mm; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds mm = allocate_mm(); 4571da177e4SLinus Torvalds if (mm) { 4581da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 45978fb7466SPavel Emelianov mm = mm_init(mm, current); 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds return mm; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* 4651da177e4SLinus Torvalds * Called when the last reference to the mm 4661da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 4671da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 4681da177e4SLinus Torvalds */ 4697ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 4701da177e4SLinus Torvalds { 4711da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 4721da177e4SLinus Torvalds mm_free_pgd(mm); 4731da177e4SLinus Torvalds destroy_context(mm); 474cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 4751da177e4SLinus Torvalds free_mm(mm); 4761da177e4SLinus Torvalds } 4776d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds /* 4801da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 4811da177e4SLinus Torvalds */ 4821da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 4831da177e4SLinus Torvalds { 4840ae26f1bSAndrew Morton might_sleep(); 4850ae26f1bSAndrew Morton 4861da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 4871da177e4SLinus Torvalds exit_aio(mm); 4881da177e4SLinus Torvalds exit_mmap(mm); 489925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 4901da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 4911da177e4SLinus Torvalds spin_lock(&mmlist_lock); 4921da177e4SLinus Torvalds list_del(&mm->mmlist); 4931da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds put_swap_token(mm); 4961da177e4SLinus Torvalds mmdrop(mm); 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /** 5021da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 5031da177e4SLinus Torvalds * 504246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 5051da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 5061da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 5071da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 5081da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 5091da177e4SLinus Torvalds */ 5101da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 5111da177e4SLinus Torvalds { 5121da177e4SLinus Torvalds struct mm_struct *mm; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds task_lock(task); 5151da177e4SLinus Torvalds mm = task->mm; 5161da177e4SLinus Torvalds if (mm) { 517246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 5181da177e4SLinus Torvalds mm = NULL; 5191da177e4SLinus Torvalds else 5201da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds task_unlock(task); 5231da177e4SLinus Torvalds return mm; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 5281da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 5291da177e4SLinus Torvalds * error success whatever. 5301da177e4SLinus Torvalds * 5311da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 5321da177e4SLinus Torvalds * from the current process. 5331da177e4SLinus Torvalds * 5341da177e4SLinus Torvalds * This difference is important for error handling, when we 5351da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 5361da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 5371da177e4SLinus Torvalds * restoring the old one. . . 5381da177e4SLinus Torvalds * Eric Biederman 10 January 1998 5391da177e4SLinus Torvalds */ 5401da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds struct completion *vfork_done = tsk->vfork_done; 5431da177e4SLinus Torvalds 5448141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 5458141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 5468141c7f3SLinus Torvalds if (unlikely(tsk->robust_list)) 5478141c7f3SLinus Torvalds exit_robust_list(tsk); 5488141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 5498141c7f3SLinus Torvalds if (unlikely(tsk->compat_robust_list)) 5508141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 5518141c7f3SLinus Torvalds #endif 5528141c7f3SLinus Torvalds #endif 5538141c7f3SLinus Torvalds 5541da177e4SLinus Torvalds /* Get rid of any cached register state */ 5551da177e4SLinus Torvalds deactivate_mm(tsk, mm); 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds /* notify parent sleeping on vfork() */ 5581da177e4SLinus Torvalds if (vfork_done) { 5591da177e4SLinus Torvalds tsk->vfork_done = NULL; 5601da177e4SLinus Torvalds complete(vfork_done); 5611da177e4SLinus Torvalds } 562fec1d011SRoland McGrath 563fec1d011SRoland McGrath /* 564fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 565fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 566fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 567fec1d011SRoland McGrath * trouble otherwise. Userland only wants this done for a sys_exit. 568fec1d011SRoland McGrath */ 5699c8a8228SEric Dumazet if (tsk->clear_child_tid) { 5709c8a8228SEric Dumazet if (!(tsk->flags & PF_SIGNALED) && 5719c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 5721da177e4SLinus Torvalds /* 5731da177e4SLinus Torvalds * We don't check the error code - if userspace has 5741da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 5751da177e4SLinus Torvalds */ 5769c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 5779c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 5789c8a8228SEric Dumazet 1, NULL, NULL, 0); 5799c8a8228SEric Dumazet } 5809c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 584a0a7ec30SJANAK DESAI /* 585a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 586a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 587a0a7ec30SJANAK DESAI */ 588402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk) 589a0a7ec30SJANAK DESAI { 590a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 591a0a7ec30SJANAK DESAI int err; 592a0a7ec30SJANAK DESAI 593a0a7ec30SJANAK DESAI if (!oldmm) 594a0a7ec30SJANAK DESAI return NULL; 595a0a7ec30SJANAK DESAI 596a0a7ec30SJANAK DESAI mm = allocate_mm(); 597a0a7ec30SJANAK DESAI if (!mm) 598a0a7ec30SJANAK DESAI goto fail_nomem; 599a0a7ec30SJANAK DESAI 600a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 601a0a7ec30SJANAK DESAI 6027602bdf2SAshwin Chaugule /* Initializing for Swap token stuff */ 6037602bdf2SAshwin Chaugule mm->token_priority = 0; 6047602bdf2SAshwin Chaugule mm->last_interval = 0; 6057602bdf2SAshwin Chaugule 60678fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 607a0a7ec30SJANAK DESAI goto fail_nomem; 608a0a7ec30SJANAK DESAI 609a0a7ec30SJANAK DESAI if (init_new_context(tsk, mm)) 610a0a7ec30SJANAK DESAI goto fail_nocontext; 611a0a7ec30SJANAK DESAI 612925d1c40SMatt Helsley dup_mm_exe_file(oldmm, mm); 613925d1c40SMatt Helsley 614a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 615a0a7ec30SJANAK DESAI if (err) 616a0a7ec30SJANAK DESAI goto free_pt; 617a0a7ec30SJANAK DESAI 618a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 619a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 620a0a7ec30SJANAK DESAI 621a0a7ec30SJANAK DESAI return mm; 622a0a7ec30SJANAK DESAI 623a0a7ec30SJANAK DESAI free_pt: 624a0a7ec30SJANAK DESAI mmput(mm); 625a0a7ec30SJANAK DESAI 626a0a7ec30SJANAK DESAI fail_nomem: 627a0a7ec30SJANAK DESAI return NULL; 628a0a7ec30SJANAK DESAI 629a0a7ec30SJANAK DESAI fail_nocontext: 630a0a7ec30SJANAK DESAI /* 631a0a7ec30SJANAK DESAI * If init_new_context() failed, we cannot use mmput() to free the mm 632a0a7ec30SJANAK DESAI * because it calls destroy_context() 633a0a7ec30SJANAK DESAI */ 634a0a7ec30SJANAK DESAI mm_free_pgd(mm); 635a0a7ec30SJANAK DESAI free_mm(mm); 636a0a7ec30SJANAK DESAI return NULL; 637a0a7ec30SJANAK DESAI } 638a0a7ec30SJANAK DESAI 6391da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk) 6401da177e4SLinus Torvalds { 6411da177e4SLinus Torvalds struct mm_struct * mm, *oldmm; 6421da177e4SLinus Torvalds int retval; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 6451da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 64617406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 64717406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 64817406b82SMandeep Singh Baines #endif 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds tsk->mm = NULL; 6511da177e4SLinus Torvalds tsk->active_mm = NULL; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds /* 6541da177e4SLinus Torvalds * Are we cloning a kernel thread? 6551da177e4SLinus Torvalds * 6561da177e4SLinus Torvalds * We need to steal a active VM for that.. 6571da177e4SLinus Torvalds */ 6581da177e4SLinus Torvalds oldmm = current->mm; 6591da177e4SLinus Torvalds if (!oldmm) 6601da177e4SLinus Torvalds return 0; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 6631da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 6641da177e4SLinus Torvalds mm = oldmm; 6651da177e4SLinus Torvalds goto good_mm; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds retval = -ENOMEM; 669a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 6701da177e4SLinus Torvalds if (!mm) 6711da177e4SLinus Torvalds goto fail_nomem; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds good_mm: 6747602bdf2SAshwin Chaugule /* Initializing for Swap token stuff */ 6757602bdf2SAshwin Chaugule mm->token_priority = 0; 6767602bdf2SAshwin Chaugule mm->last_interval = 0; 6777602bdf2SAshwin Chaugule 6781da177e4SLinus Torvalds tsk->mm = mm; 6791da177e4SLinus Torvalds tsk->active_mm = mm; 6801da177e4SLinus Torvalds return 0; 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds fail_nomem: 6831da177e4SLinus Torvalds return retval; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 686a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 6871da177e4SLinus Torvalds { 688498052bbSAl Viro struct fs_struct *fs = current->fs; 6891da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 690498052bbSAl Viro /* tsk->fs is already what we want */ 691498052bbSAl Viro write_lock(&fs->lock); 692498052bbSAl Viro if (fs->in_exec) { 693498052bbSAl Viro write_unlock(&fs->lock); 694498052bbSAl Viro return -EAGAIN; 695498052bbSAl Viro } 696498052bbSAl Viro fs->users++; 697498052bbSAl Viro write_unlock(&fs->lock); 6981da177e4SLinus Torvalds return 0; 6991da177e4SLinus Torvalds } 700498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 7011da177e4SLinus Torvalds if (!tsk->fs) 7021da177e4SLinus Torvalds return -ENOMEM; 7031da177e4SLinus Torvalds return 0; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 706a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct * tsk) 707a016f338SJANAK DESAI { 708a016f338SJANAK DESAI struct files_struct *oldf, *newf; 709a016f338SJANAK DESAI int error = 0; 710a016f338SJANAK DESAI 711a016f338SJANAK DESAI /* 712a016f338SJANAK DESAI * A background process may not have any files ... 713a016f338SJANAK DESAI */ 714a016f338SJANAK DESAI oldf = current->files; 715a016f338SJANAK DESAI if (!oldf) 716a016f338SJANAK DESAI goto out; 717a016f338SJANAK DESAI 718a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 719a016f338SJANAK DESAI atomic_inc(&oldf->count); 720a016f338SJANAK DESAI goto out; 721a016f338SJANAK DESAI } 722a016f338SJANAK DESAI 723a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 724a016f338SJANAK DESAI if (!newf) 725a016f338SJANAK DESAI goto out; 726a016f338SJANAK DESAI 727a016f338SJANAK DESAI tsk->files = newf; 728a016f338SJANAK DESAI error = 0; 729a016f338SJANAK DESAI out: 730a016f338SJANAK DESAI return error; 731a016f338SJANAK DESAI } 732a016f338SJANAK DESAI 733fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 734fd0928dfSJens Axboe { 735fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 736fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 737fd0928dfSJens Axboe 738fd0928dfSJens Axboe if (!ioc) 739fd0928dfSJens Axboe return 0; 740fadad878SJens Axboe /* 741fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 742fadad878SJens Axboe */ 743fadad878SJens Axboe if (clone_flags & CLONE_IO) { 744fadad878SJens Axboe tsk->io_context = ioc_task_link(ioc); 745fadad878SJens Axboe if (unlikely(!tsk->io_context)) 746fadad878SJens Axboe return -ENOMEM; 747fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 748fd0928dfSJens Axboe tsk->io_context = alloc_io_context(GFP_KERNEL, -1); 749fd0928dfSJens Axboe if (unlikely(!tsk->io_context)) 750fd0928dfSJens Axboe return -ENOMEM; 751fd0928dfSJens Axboe 752fd0928dfSJens Axboe tsk->io_context->ioprio = ioc->ioprio; 753fd0928dfSJens Axboe } 754fd0928dfSJens Axboe #endif 755fd0928dfSJens Axboe return 0; 756fd0928dfSJens Axboe } 757fd0928dfSJens Axboe 758a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 7591da177e4SLinus Torvalds { 7601da177e4SLinus Torvalds struct sighand_struct *sig; 7611da177e4SLinus Torvalds 76260348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 7631da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 7641da177e4SLinus Torvalds return 0; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 767e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 7681da177e4SLinus Torvalds if (!sig) 7691da177e4SLinus Torvalds return -ENOMEM; 7701da177e4SLinus Torvalds atomic_set(&sig->count, 1); 7711da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 7721da177e4SLinus Torvalds return 0; 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 775a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 776c81addc9SOleg Nesterov { 777c81addc9SOleg Nesterov if (atomic_dec_and_test(&sighand->count)) 778c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 779c81addc9SOleg Nesterov } 780c81addc9SOleg Nesterov 781f06febc9SFrank Mayhar 782f06febc9SFrank Mayhar /* 783f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 784f06febc9SFrank Mayhar */ 785f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 786f06febc9SFrank Mayhar { 787f06febc9SFrank Mayhar /* Thread group counters. */ 788f06febc9SFrank Mayhar thread_group_cputime_init(sig); 789f06febc9SFrank Mayhar 790f06febc9SFrank Mayhar /* Expiration times and increments. */ 791f06febc9SFrank Mayhar sig->it_virt_expires = cputime_zero; 792f06febc9SFrank Mayhar sig->it_virt_incr = cputime_zero; 793f06febc9SFrank Mayhar sig->it_prof_expires = cputime_zero; 794f06febc9SFrank Mayhar sig->it_prof_incr = cputime_zero; 795f06febc9SFrank Mayhar 796f06febc9SFrank Mayhar /* Cached expiration times. */ 797f06febc9SFrank Mayhar sig->cputime_expires.prof_exp = cputime_zero; 798f06febc9SFrank Mayhar sig->cputime_expires.virt_exp = cputime_zero; 799f06febc9SFrank Mayhar sig->cputime_expires.sched_exp = 0; 800f06febc9SFrank Mayhar 8016279a751SOleg Nesterov if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 8026279a751SOleg Nesterov sig->cputime_expires.prof_exp = 8036279a751SOleg Nesterov secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur); 8046279a751SOleg Nesterov sig->cputimer.running = 1; 8056279a751SOleg Nesterov } 8066279a751SOleg Nesterov 807f06febc9SFrank Mayhar /* The timer lists. */ 808f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 809f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 810f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 811f06febc9SFrank Mayhar } 812f06febc9SFrank Mayhar 813a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 8141da177e4SLinus Torvalds { 8151da177e4SLinus Torvalds struct signal_struct *sig; 8161da177e4SLinus Torvalds 8174ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 818490dea45SPeter Zijlstra return 0; 8196279a751SOleg Nesterov 8201da177e4SLinus Torvalds sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL); 8211da177e4SLinus Torvalds tsk->signal = sig; 8221da177e4SLinus Torvalds if (!sig) 8231da177e4SLinus Torvalds return -ENOMEM; 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds atomic_set(&sig->count, 1); 8261da177e4SLinus Torvalds atomic_set(&sig->live, 1); 8271da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 8281da177e4SLinus Torvalds sig->flags = 0; 829b3bfa0cbSSukadev Bhattiprolu if (clone_flags & CLONE_NEWPID) 830b3bfa0cbSSukadev Bhattiprolu sig->flags |= SIGNAL_UNKILLABLE; 8311da177e4SLinus Torvalds sig->group_exit_code = 0; 8321da177e4SLinus Torvalds sig->group_exit_task = NULL; 8331da177e4SLinus Torvalds sig->group_stop_count = 0; 834db51aeccSOleg Nesterov sig->curr_target = tsk; 8351da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 8361da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 8371da177e4SLinus Torvalds 838c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 8392ff678b8SThomas Gleixner sig->it_real_incr.tv64 = 0; 8401da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds sig->leader = 0; /* session leadership doesn't inherit */ 843ab521dc0SEric W. Biederman sig->tty_old_pgrp = NULL; 8449c9f4dedSAlan Cox sig->tty = NULL; 8451da177e4SLinus Torvalds 84632bd671dSPeter Zijlstra sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero; 8479ac52315SLaurent Vivier sig->gtime = cputime_zero; 8489ac52315SLaurent Vivier sig->cgtime = cputime_zero; 8491da177e4SLinus Torvalds sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0; 8501da177e4SLinus Torvalds sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0; 8516eaeeabaSEric Dumazet sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0; 8525995477aSAndrea Righi task_io_accounting_init(&sig->ioac); 85332bd671dSPeter Zijlstra sig->sum_sched_runtime = 0; 854ad4ecbcbSShailabh Nagar taskstats_tgid_init(sig); 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds task_lock(current->group_leader); 8571da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 8581da177e4SLinus Torvalds task_unlock(current->group_leader); 8591da177e4SLinus Torvalds 8606279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 8616279a751SOleg Nesterov 8620e464814SKaiGai Kohei acct_init_pacct(&sig->pacct); 8631da177e4SLinus Torvalds 864522ed776SMiloslav Trmac tty_audit_fork(sig); 865522ed776SMiloslav Trmac 8661da177e4SLinus Torvalds return 0; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8696b3934efSOleg Nesterov void __cleanup_signal(struct signal_struct *sig) 8706b3934efSOleg Nesterov { 871f06febc9SFrank Mayhar thread_group_cputime_free(sig); 8729c9f4dedSAlan Cox tty_kref_put(sig->tty); 8736b3934efSOleg Nesterov kmem_cache_free(signal_cachep, sig); 8746b3934efSOleg Nesterov } 8756b3934efSOleg Nesterov 876a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p) 8771da177e4SLinus Torvalds { 8781da177e4SLinus Torvalds unsigned long new_flags = p->flags; 8791da177e4SLinus Torvalds 88083144186SRafael J. Wysocki new_flags &= ~PF_SUPERPRIV; 8811da177e4SLinus Torvalds new_flags |= PF_FORKNOEXEC; 88209a05394SRoland McGrath new_flags |= PF_STARTING; 8831da177e4SLinus Torvalds p->flags = new_flags; 8842e131895SRafael J. Wysocki clear_freeze_flag(p); 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds 88717da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 8881da177e4SLinus Torvalds { 8891da177e4SLinus Torvalds current->clear_child_tid = tidptr; 8901da177e4SLinus Torvalds 891b488893aSPavel Emelyanov return task_pid_vnr(current); 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 894a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 89523f78d4aSIngo Molnar { 89623f78d4aSIngo Molnar spin_lock_init(&p->pi_lock); 897e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 89823f78d4aSIngo Molnar plist_head_init(&p->pi_waiters, &p->pi_lock); 89923f78d4aSIngo Molnar p->pi_blocked_on = NULL; 90023f78d4aSIngo Molnar #endif 90123f78d4aSIngo Molnar } 90223f78d4aSIngo Molnar 903cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER 904cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 905cf475ad2SBalbir Singh { 906cf475ad2SBalbir Singh mm->owner = p; 907cf475ad2SBalbir Singh } 908cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */ 909cf475ad2SBalbir Singh 9101da177e4SLinus Torvalds /* 911f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 912f06febc9SFrank Mayhar */ 913f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 914f06febc9SFrank Mayhar { 915f06febc9SFrank Mayhar tsk->cputime_expires.prof_exp = cputime_zero; 916f06febc9SFrank Mayhar tsk->cputime_expires.virt_exp = cputime_zero; 917f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 918f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 919f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 920f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 921f06febc9SFrank Mayhar } 922f06febc9SFrank Mayhar 923f06febc9SFrank Mayhar /* 9241da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 9251da177e4SLinus Torvalds * but does not actually start it yet. 9261da177e4SLinus Torvalds * 9271da177e4SLinus Torvalds * It copies the registers, and all the appropriate 9281da177e4SLinus Torvalds * parts of the process environment (as per the clone 9291da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 9301da177e4SLinus Torvalds */ 93136c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 9321da177e4SLinus Torvalds unsigned long stack_start, 9331da177e4SLinus Torvalds struct pt_regs *regs, 9341da177e4SLinus Torvalds unsigned long stack_size, 9351da177e4SLinus Torvalds int __user *child_tidptr, 93609a05394SRoland McGrath struct pid *pid, 93709a05394SRoland McGrath int trace) 9381da177e4SLinus Torvalds { 9391da177e4SLinus Torvalds int retval; 940a24efe62SMariusz Kozlowski struct task_struct *p; 941b4f48b63SPaul Menage int cgroup_callbacks_done = 0; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 9441da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds /* 9471da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 9481da177e4SLinus Torvalds * can only be started up within the thread group. 9491da177e4SLinus Torvalds */ 9501da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 9511da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds /* 9541da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 9551da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 9561da177e4SLinus Torvalds * for various simplifications in other code. 9571da177e4SLinus Torvalds */ 9581da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 9591da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds retval = security_task_create(clone_flags); 9621da177e4SLinus Torvalds if (retval) 9631da177e4SLinus Torvalds goto fork_out; 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds retval = -ENOMEM; 9661da177e4SLinus Torvalds p = dup_task_struct(current); 9671da177e4SLinus Torvalds if (!p) 9681da177e4SLinus Torvalds goto fork_out; 9691da177e4SLinus Torvalds 970f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 971f7e8b616SSteven Rostedt 972bea493a0SPeter Zijlstra rt_mutex_init_task(p); 973bea493a0SPeter Zijlstra 974d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 975de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 976de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 977de30a2b3SIngo Molnar #endif 9781da177e4SLinus Torvalds retval = -EAGAIN; 9793b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 9801da177e4SLinus Torvalds p->signal->rlim[RLIMIT_NPROC].rlim_cur) { 9811da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) && 98218b6e041SSerge Hallyn p->real_cred->user != INIT_USER) 9831da177e4SLinus Torvalds goto bad_fork_free; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 986f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 987f1752eecSDavid Howells if (retval < 0) 988f1752eecSDavid Howells goto bad_fork_free; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds /* 9911da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 9921da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 9931da177e4SLinus Torvalds * to stop root fork bombs. 9941da177e4SLinus Torvalds */ 99504ec93feSLi Zefan retval = -EAGAIN; 9961da177e4SLinus Torvalds if (nr_threads >= max_threads) 9971da177e4SLinus Torvalds goto bad_fork_cleanup_count; 9981da177e4SLinus Torvalds 999a1261f54SAl Viro if (!try_module_get(task_thread_info(p)->exec_domain->module)) 10001da177e4SLinus Torvalds goto bad_fork_cleanup_count; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds if (p->binfmt && !try_module_get(p->binfmt->module)) 10031da177e4SLinus Torvalds goto bad_fork_cleanup_put_domain; 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds p->did_exec = 0; 1006ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 10071da177e4SLinus Torvalds copy_flags(clone_flags, p); 10081da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 10091da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1010f41d911fSPaul E. McKenney rcu_copy_process(p); 10111da177e4SLinus Torvalds p->vfork_done = NULL; 10121da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds init_sigpending(&p->pending); 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds p->utime = cputime_zero; 10171da177e4SLinus Torvalds p->stime = cputime_zero; 10189ac52315SLaurent Vivier p->gtime = cputime_zero; 1019c66f08beSMichael Neuling p->utimescaled = cputime_zero; 1020c66f08beSMichael Neuling p->stimescaled = cputime_zero; 102173a2bcb0SPeter Zijlstra p->prev_utime = cputime_zero; 10229301899bSBalbir Singh p->prev_stime = cputime_zero; 1023172ba844SBalbir Singh 10246976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 10256976675dSArjan van de Ven 10265995477aSAndrea Righi task_io_accounting_init(&p->ioac); 10271da177e4SLinus Torvalds acct_clear_integrals(p); 10281da177e4SLinus Torvalds 1029f06febc9SFrank Mayhar posix_cpu_timers_init(p); 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds p->lock_depth = -1; /* -1 = no lock */ 10321da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&p->start_time); 1033924b42d5STomas Janousek p->real_start_time = p->start_time; 1034924b42d5STomas Janousek monotonic_to_bootbased(&p->real_start_time); 10351da177e4SLinus Torvalds p->io_context = NULL; 10361da177e4SLinus Torvalds p->audit_context = NULL; 1037b4f48b63SPaul Menage cgroup_fork(p); 10381da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1039846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 10401da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 10411da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 10421da177e4SLinus Torvalds p->mempolicy = NULL; 1043b4f48b63SPaul Menage goto bad_fork_cleanup_cgroup; 10441da177e4SLinus Torvalds } 1045c61afb18SPaul Jackson mpol_fix_fork_child_flag(p); 10461da177e4SLinus Torvalds #endif 1047de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1048de30a2b3SIngo Molnar p->irq_events = 0; 1049b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW 1050b36e4758SRussell King p->hardirqs_enabled = 1; 1051b36e4758SRussell King #else 1052de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1053b36e4758SRussell King #endif 1054de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1055de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1056de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1057de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1058de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1059de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1060de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1061de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1062de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1063de30a2b3SIngo Molnar p->hardirq_context = 0; 1064de30a2b3SIngo Molnar p->softirq_context = 0; 1065de30a2b3SIngo Molnar #endif 1066fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1067fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1068fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1069fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1070fbb9ce95SIngo Molnar #endif 10711da177e4SLinus Torvalds 1072408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1073408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1074408894eeSIngo Molnar #endif 10750f481406SMarkus Metzger 10760f481406SMarkus Metzger p->bts = NULL; 1077408894eeSIngo Molnar 10783c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 10793c90e6e9SSrivatsa Vaddagiri sched_fork(p, clone_flags); 10806ab423e0SPeter Zijlstra 1081*cdd6c482SIngo Molnar retval = perf_event_init_task(p); 10826ab423e0SPeter Zijlstra if (retval) 10836ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 10843c90e6e9SSrivatsa Vaddagiri 10851da177e4SLinus Torvalds if ((retval = audit_alloc(p))) 1086f1752eecSDavid Howells goto bad_fork_cleanup_policy; 10871da177e4SLinus Torvalds /* copy all the process information */ 10881da177e4SLinus Torvalds if ((retval = copy_semundo(clone_flags, p))) 10891da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 10901da177e4SLinus Torvalds if ((retval = copy_files(clone_flags, p))) 10911da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 10921da177e4SLinus Torvalds if ((retval = copy_fs(clone_flags, p))) 10931da177e4SLinus Torvalds goto bad_fork_cleanup_files; 10941da177e4SLinus Torvalds if ((retval = copy_sighand(clone_flags, p))) 10951da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 10961da177e4SLinus Torvalds if ((retval = copy_signal(clone_flags, p))) 10971da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 10981da177e4SLinus Torvalds if ((retval = copy_mm(clone_flags, p))) 10991da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1100ab516013SSerge E. Hallyn if ((retval = copy_namespaces(clone_flags, p))) 1101d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1102fadad878SJens Axboe if ((retval = copy_io(clone_flags, p))) 1103fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 11046f2c55b8SAlexey Dobriyan retval = copy_thread(clone_flags, stack_start, stack_size, p, regs); 11051da177e4SLinus Torvalds if (retval) 1106fd0928dfSJens Axboe goto bad_fork_cleanup_io; 11071da177e4SLinus Torvalds 1108425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1109425fb2b4SPavel Emelyanov retval = -ENOMEM; 111061bce0f1SEric W. Biederman pid = alloc_pid(p->nsproxy->pid_ns); 1111425fb2b4SPavel Emelyanov if (!pid) 1112fd0928dfSJens Axboe goto bad_fork_cleanup_io; 11136f4e6433SPavel Emelyanov 11146f4e6433SPavel Emelyanov if (clone_flags & CLONE_NEWPID) { 111561bce0f1SEric W. Biederman retval = pid_ns_prepare_proc(p->nsproxy->pid_ns); 11166f4e6433SPavel Emelyanov if (retval < 0) 11176f4e6433SPavel Emelyanov goto bad_fork_free_pid; 11186f4e6433SPavel Emelyanov } 1119425fb2b4SPavel Emelyanov } 1120425fb2b4SPavel Emelyanov 1121425fb2b4SPavel Emelyanov p->pid = pid_nr(pid); 1122425fb2b4SPavel Emelyanov p->tgid = p->pid; 1123425fb2b4SPavel Emelyanov if (clone_flags & CLONE_THREAD) 1124425fb2b4SPavel Emelyanov p->tgid = current->tgid; 1125425fb2b4SPavel Emelyanov 1126e885dcdeSSerge E. Hallyn if (current->nsproxy != p->nsproxy) { 1127e885dcdeSSerge E. Hallyn retval = ns_cgroup_clone(p, pid); 1128e885dcdeSSerge E. Hallyn if (retval) 1129f7e8b616SSteven Rostedt goto bad_fork_free_pid; 1130e885dcdeSSerge E. Hallyn } 1131e885dcdeSSerge E. Hallyn 11321da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 11331da177e4SLinus Torvalds /* 11341da177e4SLinus Torvalds * Clear TID on mm_release()? 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL; 113742b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 11388f17d3a5SIngo Molnar p->robust_list = NULL; 11398f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 11408f17d3a5SIngo Molnar p->compat_robust_list = NULL; 11418f17d3a5SIngo Molnar #endif 1142c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1143c87e2837SIngo Molnar p->pi_state_cache = NULL; 114442b2dd0aSAlexey Dobriyan #endif 11451da177e4SLinus Torvalds /* 1146f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1147f9a3879aSGOTO Masanori */ 1148f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 1149f9a3879aSGOTO Masanori p->sas_ss_sp = p->sas_ss_size = 0; 1150f9a3879aSGOTO Masanori 1151f9a3879aSGOTO Masanori /* 11521da177e4SLinus Torvalds * Syscall tracing should be turned off in the child regardless 11531da177e4SLinus Torvalds * of CLONE_PTRACE. 11541da177e4SLinus Torvalds */ 11551da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1156ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1157ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1158ed75e8d5SLaurent Vivier #endif 11599745512cSArjan van de Ven clear_all_latency_tracing(p); 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds /* ok, now we should be set up.. */ 11621da177e4SLinus Torvalds p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL); 11631da177e4SLinus Torvalds p->pdeath_signal = 0; 11641da177e4SLinus Torvalds p->exit_state = 0; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds /* 11671da177e4SLinus Torvalds * Ok, make it visible to the rest of the system. 11681da177e4SLinus Torvalds * We dont wake it up yet. 11691da177e4SLinus Torvalds */ 11701da177e4SLinus Torvalds p->group_leader = p; 117147e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 11721da177e4SLinus Torvalds 1173b4f48b63SPaul Menage /* Now that the task is set up, run cgroup callbacks if 1174b4f48b63SPaul Menage * necessary. We need to run them before the task is visible 1175b4f48b63SPaul Menage * on the tasklist. */ 1176b4f48b63SPaul Menage cgroup_fork_callbacks(p); 1177b4f48b63SPaul Menage cgroup_callbacks_done = 1; 1178b4f48b63SPaul Menage 11791da177e4SLinus Torvalds /* Need tasklist lock for parent etc handling! */ 11801da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds /* 1183476d139cSNick Piggin * The task hasn't been attached yet, so its cpus_allowed mask will 1184476d139cSNick Piggin * not be changed, nor will its assigned CPU. 1185476d139cSNick Piggin * 1186476d139cSNick Piggin * The cpus_allowed mask of the parent may have changed after it was 1187476d139cSNick Piggin * copied first time - so re-copy it here, then check the child's CPU 1188476d139cSNick Piggin * to ensure it is on a valid CPU (and if not, just force it back to 1189476d139cSNick Piggin * parent's CPU). This avoids alot of nasty races. 11901da177e4SLinus Torvalds */ 11911da177e4SLinus Torvalds p->cpus_allowed = current->cpus_allowed; 11926f505b16SPeter Zijlstra p->rt.nr_cpus_allowed = current->rt.nr_cpus_allowed; 119326ff6ad9SSrivatsa Vaddagiri if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) || 119426ff6ad9SSrivatsa Vaddagiri !cpu_online(task_cpu(p)))) 11951da177e4SLinus Torvalds set_task_cpu(p, smp_processor_id()); 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 11982d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 11991da177e4SLinus Torvalds p->real_parent = current->real_parent; 12002d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 12012d5516cbSOleg Nesterov } else { 12021da177e4SLinus Torvalds p->real_parent = current; 12032d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 12042d5516cbSOleg Nesterov } 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 12074a2c7a78SOleg Nesterov 12084a2c7a78SOleg Nesterov /* 12094a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 12104a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 12114a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 12124a2c7a78SOleg Nesterov * it's process group. 12134a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 12144a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 12154a2c7a78SOleg Nesterov */ 12164a2c7a78SOleg Nesterov recalc_sigpending(); 12174a2c7a78SOleg Nesterov if (signal_pending(current)) { 12184a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 12194a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 12204a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 1221f7e8b616SSteven Rostedt goto bad_fork_free_pid; 12224a2c7a78SOleg Nesterov } 12234a2c7a78SOleg Nesterov 12243f17da69SOleg Nesterov if (clone_flags & CLONE_THREAD) { 12254ab6c083SOleg Nesterov atomic_inc(¤t->signal->count); 12264ab6c083SOleg Nesterov atomic_inc(¤t->signal->live); 12271da177e4SLinus Torvalds p->group_leader = current->group_leader; 122847e65328SOleg Nesterov list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group); 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 123173b9ebfeSOleg Nesterov if (likely(p->pid)) { 1232f470021aSRoland McGrath list_add_tail(&p->sibling, &p->real_parent->children); 123309a05394SRoland McGrath tracehook_finish_clone(p, clone_flags, trace); 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds if (thread_group_leader(p)) { 12365cd17569SEric W. Biederman if (clone_flags & CLONE_NEWPID) 123730e49c26SPavel Emelyanov p->nsproxy->pid_ns->child_reaper = p; 12385cd17569SEric W. Biederman 1239fea9d175SOleg Nesterov p->signal->leader_pid = pid; 12409c9f4dedSAlan Cox tty_kref_put(p->signal->tty); 12419c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 12425cd17569SEric W. Biederman attach_pid(p, PIDTYPE_PGID, task_pgrp(current)); 12435cd17569SEric W. Biederman attach_pid(p, PIDTYPE_SID, task_session(current)); 12445e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 12451da177e4SLinus Torvalds __get_cpu_var(process_counts)++; 12461da177e4SLinus Torvalds } 124785868995SSukadev Bhattiprolu attach_pid(p, PIDTYPE_PID, pid); 12481da177e4SLinus Torvalds nr_threads++; 124973b9ebfeSOleg Nesterov } 125073b9ebfeSOleg Nesterov 12511da177e4SLinus Torvalds total_forks++; 12523f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 12531da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 1254c13cf856SAndrew Morton proc_fork_connector(p); 1255817929ecSPaul Menage cgroup_post_fork(p); 1256*cdd6c482SIngo Molnar perf_event_fork(p); 12571da177e4SLinus Torvalds return p; 12581da177e4SLinus Torvalds 1259425fb2b4SPavel Emelyanov bad_fork_free_pid: 1260425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1261425fb2b4SPavel Emelyanov free_pid(pid); 1262fd0928dfSJens Axboe bad_fork_cleanup_io: 1263fd0928dfSJens Axboe put_io_context(p->io_context); 1264ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1265444f378bSLinus Torvalds exit_task_namespaces(p); 12661da177e4SLinus Torvalds bad_fork_cleanup_mm: 12671da177e4SLinus Torvalds if (p->mm) 12681da177e4SLinus Torvalds mmput(p->mm); 12691da177e4SLinus Torvalds bad_fork_cleanup_signal: 12704ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 12714ab6c083SOleg Nesterov __cleanup_signal(p->signal); 12721da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1273a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 12741da177e4SLinus Torvalds bad_fork_cleanup_fs: 12751da177e4SLinus Torvalds exit_fs(p); /* blocking */ 12761da177e4SLinus Torvalds bad_fork_cleanup_files: 12771da177e4SLinus Torvalds exit_files(p); /* blocking */ 12781da177e4SLinus Torvalds bad_fork_cleanup_semundo: 12791da177e4SLinus Torvalds exit_sem(p); 12801da177e4SLinus Torvalds bad_fork_cleanup_audit: 12811da177e4SLinus Torvalds audit_free(p); 12821da177e4SLinus Torvalds bad_fork_cleanup_policy: 1283*cdd6c482SIngo Molnar perf_event_free_task(p); 12841da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1285f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1286b4f48b63SPaul Menage bad_fork_cleanup_cgroup: 12871da177e4SLinus Torvalds #endif 1288b4f48b63SPaul Menage cgroup_exit(p, cgroup_callbacks_done); 128935df17c5SShailabh Nagar delayacct_tsk_free(p); 12901da177e4SLinus Torvalds if (p->binfmt) 12911da177e4SLinus Torvalds module_put(p->binfmt->module); 12921da177e4SLinus Torvalds bad_fork_cleanup_put_domain: 1293a1261f54SAl Viro module_put(task_thread_info(p)->exec_domain->module); 12941da177e4SLinus Torvalds bad_fork_cleanup_count: 1295d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1296e0e81739SDavid Howells exit_creds(p); 12971da177e4SLinus Torvalds bad_fork_free: 12981da177e4SLinus Torvalds free_task(p); 1299fe7d37d1SOleg Nesterov fork_out: 1300fe7d37d1SOleg Nesterov return ERR_PTR(retval); 13011da177e4SLinus Torvalds } 13021da177e4SLinus Torvalds 13036b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs) 13041da177e4SLinus Torvalds { 13051da177e4SLinus Torvalds memset(regs, 0, sizeof(struct pt_regs)); 13061da177e4SLinus Torvalds return regs; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 13099abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu) 13101da177e4SLinus Torvalds { 131136c8b586SIngo Molnar struct task_struct *task; 13121da177e4SLinus Torvalds struct pt_regs regs; 13131da177e4SLinus Torvalds 131430e49c26SPavel Emelyanov task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, 131509a05394SRoland McGrath &init_struct_pid, 0); 1316753ca4f3SAkinobu Mita if (!IS_ERR(task)) 13171da177e4SLinus Torvalds init_idle(task, cpu); 131873b9ebfeSOleg Nesterov 13191da177e4SLinus Torvalds return task; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * Ok, this is the main fork-routine. 13241da177e4SLinus Torvalds * 13251da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 13261da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 13271da177e4SLinus Torvalds */ 13281da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 13291da177e4SLinus Torvalds unsigned long stack_start, 13301da177e4SLinus Torvalds struct pt_regs *regs, 13311da177e4SLinus Torvalds unsigned long stack_size, 13321da177e4SLinus Torvalds int __user *parent_tidptr, 13331da177e4SLinus Torvalds int __user *child_tidptr) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds struct task_struct *p; 13361da177e4SLinus Torvalds int trace = 0; 133792476d7fSEric W. Biederman long nr; 13381da177e4SLinus Torvalds 1339bdff746aSAndrew Morton /* 134018b6e041SSerge Hallyn * Do some preliminary argument and permissions checking before we 134118b6e041SSerge Hallyn * actually start allocating stuff 134218b6e041SSerge Hallyn */ 134318b6e041SSerge Hallyn if (clone_flags & CLONE_NEWUSER) { 134418b6e041SSerge Hallyn if (clone_flags & CLONE_THREAD) 134518b6e041SSerge Hallyn return -EINVAL; 134618b6e041SSerge Hallyn /* hopefully this check will go away when userns support is 134718b6e041SSerge Hallyn * complete 134818b6e041SSerge Hallyn */ 13497657d904SSerge E. Hallyn if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) || 13507657d904SSerge E. Hallyn !capable(CAP_SETGID)) 135118b6e041SSerge Hallyn return -EPERM; 135218b6e041SSerge Hallyn } 135318b6e041SSerge Hallyn 135418b6e041SSerge Hallyn /* 1355bdff746aSAndrew Morton * We hope to recycle these flags after 2.6.26 1356bdff746aSAndrew Morton */ 1357bdff746aSAndrew Morton if (unlikely(clone_flags & CLONE_STOPPED)) { 1358bdff746aSAndrew Morton static int __read_mostly count = 100; 1359bdff746aSAndrew Morton 1360bdff746aSAndrew Morton if (count > 0 && printk_ratelimit()) { 1361bdff746aSAndrew Morton char comm[TASK_COMM_LEN]; 1362bdff746aSAndrew Morton 1363bdff746aSAndrew Morton count--; 1364bdff746aSAndrew Morton printk(KERN_INFO "fork(): process `%s' used deprecated " 1365bdff746aSAndrew Morton "clone flags 0x%lx\n", 1366bdff746aSAndrew Morton get_task_comm(comm, current), 1367bdff746aSAndrew Morton clone_flags & CLONE_STOPPED); 1368bdff746aSAndrew Morton } 1369bdff746aSAndrew Morton } 1370bdff746aSAndrew Morton 137109a05394SRoland McGrath /* 137209a05394SRoland McGrath * When called from kernel_thread, don't do user tracing stuff. 137309a05394SRoland McGrath */ 137409a05394SRoland McGrath if (likely(user_mode(regs))) 137509a05394SRoland McGrath trace = tracehook_prepare_clone(clone_flags); 13761da177e4SLinus Torvalds 1377a6f5e063SSukadev Bhattiprolu p = copy_process(clone_flags, stack_start, regs, stack_size, 137809a05394SRoland McGrath child_tidptr, NULL, trace); 13791da177e4SLinus Torvalds /* 13801da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 13811da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 13821da177e4SLinus Torvalds */ 13831da177e4SLinus Torvalds if (!IS_ERR(p)) { 13841da177e4SLinus Torvalds struct completion vfork; 13851da177e4SLinus Torvalds 13860a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 13870a16b607SMathieu Desnoyers 13886c5f3e7bSPavel Emelyanov nr = task_pid_vnr(p); 138930e49c26SPavel Emelyanov 139030e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 139130e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1392a6f5e063SSukadev Bhattiprolu 13931da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 13941da177e4SLinus Torvalds p->vfork_done = &vfork; 13951da177e4SLinus Torvalds init_completion(&vfork); 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds 1398a64e6494SAl Viro audit_finish_fork(p); 1399087eb437SOleg Nesterov tracehook_report_clone(regs, clone_flags, nr, p); 140009a05394SRoland McGrath 140109a05394SRoland McGrath /* 140209a05394SRoland McGrath * We set PF_STARTING at creation in case tracing wants to 140309a05394SRoland McGrath * use this to distinguish a fully live task from one that 140409a05394SRoland McGrath * hasn't gotten to tracehook_report_clone() yet. Now we 140509a05394SRoland McGrath * clear it and set the child going. 140609a05394SRoland McGrath */ 140709a05394SRoland McGrath p->flags &= ~PF_STARTING; 140809a05394SRoland McGrath 140909a05394SRoland McGrath if (unlikely(clone_flags & CLONE_STOPPED)) { 14101da177e4SLinus Torvalds /* 14111da177e4SLinus Torvalds * We'll start up with an immediate SIGSTOP. 14121da177e4SLinus Torvalds */ 14131da177e4SLinus Torvalds sigaddset(&p->pending.signal, SIGSTOP); 14141da177e4SLinus Torvalds set_tsk_thread_flag(p, TIF_SIGPENDING); 1415d9ae90acSOleg Nesterov __set_task_state(p, TASK_STOPPED); 141609a05394SRoland McGrath } else { 141709a05394SRoland McGrath wake_up_new_task(p, clone_flags); 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 142009a05394SRoland McGrath tracehook_report_clone_complete(trace, regs, 142109a05394SRoland McGrath clone_flags, nr, p); 142209a05394SRoland McGrath 14231da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1424ba96a0c8SRafael J. Wysocki freezer_do_not_count(); 14251da177e4SLinus Torvalds wait_for_completion(&vfork); 1426ba96a0c8SRafael J. Wysocki freezer_count(); 1427daded34bSRoland McGrath tracehook_report_vfork_done(p, nr); 14289f59ce5dSChuck Ebbert } 14291da177e4SLinus Torvalds } else { 143092476d7fSEric W. Biederman nr = PTR_ERR(p); 14311da177e4SLinus Torvalds } 143292476d7fSEric W. Biederman return nr; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14355fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 14365fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 14375fd63b30SRavikiran G Thirumalai #endif 14385fd63b30SRavikiran G Thirumalai 143951cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 1440aa1757f9SOleg Nesterov { 1441aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1442aa1757f9SOleg Nesterov 1443aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1444b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 1445fba2afaaSDavide Libenzi } 1446aa1757f9SOleg Nesterov 14471da177e4SLinus Torvalds void __init proc_caches_init(void) 14481da177e4SLinus Torvalds { 14491da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 14501da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 14512dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 14522dff4405SVegard Nossum SLAB_NOTRACK, sighand_ctor); 14531da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 14541da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 14552dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 14561da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 14571da177e4SLinus Torvalds sizeof(struct files_struct), 0, 14582dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 14591da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 14601da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 14612dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 14621da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 14635fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 14642dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 146533e5d769SDavid Howells vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC); 14668feae131SDavid Howells mmap_init(); 14671da177e4SLinus Torvalds } 1468cf2e340fSJANAK DESAI 1469cf2e340fSJANAK DESAI /* 1470cf2e340fSJANAK DESAI * Check constraints on flags passed to the unshare system call and 1471cf2e340fSJANAK DESAI * force unsharing of additional process context as appropriate. 1472cf2e340fSJANAK DESAI */ 1473a39bc516SAlexey Dobriyan static void check_unshare_flags(unsigned long *flags_ptr) 1474cf2e340fSJANAK DESAI { 1475cf2e340fSJANAK DESAI /* 1476cf2e340fSJANAK DESAI * If unsharing a thread from a thread group, must also 1477cf2e340fSJANAK DESAI * unshare vm. 1478cf2e340fSJANAK DESAI */ 1479cf2e340fSJANAK DESAI if (*flags_ptr & CLONE_THREAD) 1480cf2e340fSJANAK DESAI *flags_ptr |= CLONE_VM; 1481cf2e340fSJANAK DESAI 1482cf2e340fSJANAK DESAI /* 1483cf2e340fSJANAK DESAI * If unsharing vm, must also unshare signal handlers. 1484cf2e340fSJANAK DESAI */ 1485cf2e340fSJANAK DESAI if (*flags_ptr & CLONE_VM) 1486cf2e340fSJANAK DESAI *flags_ptr |= CLONE_SIGHAND; 1487cf2e340fSJANAK DESAI 1488cf2e340fSJANAK DESAI /* 1489cf2e340fSJANAK DESAI * If unsharing signal handlers and the task was created 1490cf2e340fSJANAK DESAI * using CLONE_THREAD, then must unshare the thread 1491cf2e340fSJANAK DESAI */ 1492cf2e340fSJANAK DESAI if ((*flags_ptr & CLONE_SIGHAND) && 1493cf2e340fSJANAK DESAI (atomic_read(¤t->signal->count) > 1)) 1494cf2e340fSJANAK DESAI *flags_ptr |= CLONE_THREAD; 1495cf2e340fSJANAK DESAI 1496cf2e340fSJANAK DESAI /* 1497cf2e340fSJANAK DESAI * If unsharing namespace, must also unshare filesystem information. 1498cf2e340fSJANAK DESAI */ 1499cf2e340fSJANAK DESAI if (*flags_ptr & CLONE_NEWNS) 1500cf2e340fSJANAK DESAI *flags_ptr |= CLONE_FS; 1501cf2e340fSJANAK DESAI } 1502cf2e340fSJANAK DESAI 1503cf2e340fSJANAK DESAI /* 1504cf2e340fSJANAK DESAI * Unsharing of tasks created with CLONE_THREAD is not supported yet 1505cf2e340fSJANAK DESAI */ 1506cf2e340fSJANAK DESAI static int unshare_thread(unsigned long unshare_flags) 1507cf2e340fSJANAK DESAI { 1508cf2e340fSJANAK DESAI if (unshare_flags & CLONE_THREAD) 1509cf2e340fSJANAK DESAI return -EINVAL; 1510cf2e340fSJANAK DESAI 1511cf2e340fSJANAK DESAI return 0; 1512cf2e340fSJANAK DESAI } 1513cf2e340fSJANAK DESAI 1514cf2e340fSJANAK DESAI /* 151599d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1516cf2e340fSJANAK DESAI */ 1517cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1518cf2e340fSJANAK DESAI { 1519cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1520cf2e340fSJANAK DESAI 1521498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 1522498052bbSAl Viro return 0; 1523498052bbSAl Viro 1524498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 1525498052bbSAl Viro if (fs->users == 1) 1526498052bbSAl Viro return 0; 1527498052bbSAl Viro 1528498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 152999d1419dSJANAK DESAI if (!*new_fsp) 153099d1419dSJANAK DESAI return -ENOMEM; 1531cf2e340fSJANAK DESAI 1532cf2e340fSJANAK DESAI return 0; 1533cf2e340fSJANAK DESAI } 1534cf2e340fSJANAK DESAI 1535cf2e340fSJANAK DESAI /* 1536dae3c5a0SOleg Nesterov * Unsharing of sighand is not supported yet 1537cf2e340fSJANAK DESAI */ 1538cf2e340fSJANAK DESAI static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp) 1539cf2e340fSJANAK DESAI { 1540cf2e340fSJANAK DESAI struct sighand_struct *sigh = current->sighand; 1541cf2e340fSJANAK DESAI 1542dae3c5a0SOleg Nesterov if ((unshare_flags & CLONE_SIGHAND) && atomic_read(&sigh->count) > 1) 1543cf2e340fSJANAK DESAI return -EINVAL; 1544cf2e340fSJANAK DESAI else 1545cf2e340fSJANAK DESAI return 0; 1546cf2e340fSJANAK DESAI } 1547cf2e340fSJANAK DESAI 1548cf2e340fSJANAK DESAI /* 1549a0a7ec30SJANAK DESAI * Unshare vm if it is being shared 1550cf2e340fSJANAK DESAI */ 1551cf2e340fSJANAK DESAI static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp) 1552cf2e340fSJANAK DESAI { 1553cf2e340fSJANAK DESAI struct mm_struct *mm = current->mm; 1554cf2e340fSJANAK DESAI 1555cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_VM) && 1556a0a7ec30SJANAK DESAI (mm && atomic_read(&mm->mm_users) > 1)) { 15572d61b867SOleg Nesterov return -EINVAL; 1558a0a7ec30SJANAK DESAI } 1559cf2e340fSJANAK DESAI 1560cf2e340fSJANAK DESAI return 0; 1561cf2e340fSJANAK DESAI } 1562cf2e340fSJANAK DESAI 1563cf2e340fSJANAK DESAI /* 1564a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1565cf2e340fSJANAK DESAI */ 1566cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1567cf2e340fSJANAK DESAI { 1568cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1569a016f338SJANAK DESAI int error = 0; 1570cf2e340fSJANAK DESAI 1571cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1572a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1573a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1574a016f338SJANAK DESAI if (!*new_fdp) 1575a016f338SJANAK DESAI return error; 1576a016f338SJANAK DESAI } 1577cf2e340fSJANAK DESAI 1578cf2e340fSJANAK DESAI return 0; 1579cf2e340fSJANAK DESAI } 1580cf2e340fSJANAK DESAI 1581cf2e340fSJANAK DESAI /* 1582cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1583cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1584cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1585cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1586cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1587cf2e340fSJANAK DESAI * task_struct. 1588cf2e340fSJANAK DESAI */ 15896559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 1590cf2e340fSJANAK DESAI { 1591cf2e340fSJANAK DESAI int err = 0; 1592cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1593dae3c5a0SOleg Nesterov struct sighand_struct *new_sigh = NULL; 1594cf2e340fSJANAK DESAI struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL; 1595cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1596cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 15979edff4abSManfred Spraul int do_sysvsem = 0; 1598cf2e340fSJANAK DESAI 1599cf2e340fSJANAK DESAI check_unshare_flags(&unshare_flags); 1600cf2e340fSJANAK DESAI 160106f9d4f9SEric W. Biederman /* Return -EINVAL for all unsupported flags */ 160206f9d4f9SEric W. Biederman err = -EINVAL; 160306f9d4f9SEric W. Biederman if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 160425b21cb2SKirill Korotaev CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 160518b6e041SSerge Hallyn CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET)) 160606f9d4f9SEric W. Biederman goto bad_unshare_out; 160706f9d4f9SEric W. Biederman 16086013f67fSManfred Spraul /* 16096013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 16106013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 16116013f67fSManfred Spraul * namespace are unreachable. 16126013f67fSManfred Spraul */ 16136013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 16149edff4abSManfred Spraul do_sysvsem = 1; 1615cf2e340fSJANAK DESAI if ((err = unshare_thread(unshare_flags))) 1616cf2e340fSJANAK DESAI goto bad_unshare_out; 1617cf2e340fSJANAK DESAI if ((err = unshare_fs(unshare_flags, &new_fs))) 1618cf2e340fSJANAK DESAI goto bad_unshare_cleanup_thread; 1619cf2e340fSJANAK DESAI if ((err = unshare_sighand(unshare_flags, &new_sigh))) 1620e3222c4eSBadari Pulavarty goto bad_unshare_cleanup_fs; 1621cf2e340fSJANAK DESAI if ((err = unshare_vm(unshare_flags, &new_mm))) 1622cf2e340fSJANAK DESAI goto bad_unshare_cleanup_sigh; 1623cf2e340fSJANAK DESAI if ((err = unshare_fd(unshare_flags, &new_fd))) 1624cf2e340fSJANAK DESAI goto bad_unshare_cleanup_vm; 1625e3222c4eSBadari Pulavarty if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 1626e3222c4eSBadari Pulavarty new_fs))) 16279edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 1628cf2e340fSJANAK DESAI 16299edff4abSManfred Spraul if (new_fs || new_mm || new_fd || do_sysvsem || new_nsproxy) { 16309edff4abSManfred Spraul if (do_sysvsem) { 16319edff4abSManfred Spraul /* 16329edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 16339edff4abSManfred Spraul */ 16349edff4abSManfred Spraul exit_sem(current); 16359edff4abSManfred Spraul } 1636ab516013SSerge E. Hallyn 1637c0b2fc31SSerge Hallyn if (new_nsproxy) { 1638cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 1639cf7b708cSPavel Emelyanov new_nsproxy = NULL; 1640c0b2fc31SSerge Hallyn } 1641cf2e340fSJANAK DESAI 1642cf7b708cSPavel Emelyanov task_lock(current); 1643cf7b708cSPavel Emelyanov 1644cf2e340fSJANAK DESAI if (new_fs) { 1645cf2e340fSJANAK DESAI fs = current->fs; 1646498052bbSAl Viro write_lock(&fs->lock); 1647cf2e340fSJANAK DESAI current->fs = new_fs; 1648498052bbSAl Viro if (--fs->users) 1649498052bbSAl Viro new_fs = NULL; 1650498052bbSAl Viro else 1651cf2e340fSJANAK DESAI new_fs = fs; 1652498052bbSAl Viro write_unlock(&fs->lock); 1653cf2e340fSJANAK DESAI } 1654cf2e340fSJANAK DESAI 1655cf2e340fSJANAK DESAI if (new_mm) { 1656cf2e340fSJANAK DESAI mm = current->mm; 1657cf2e340fSJANAK DESAI active_mm = current->active_mm; 1658cf2e340fSJANAK DESAI current->mm = new_mm; 1659cf2e340fSJANAK DESAI current->active_mm = new_mm; 1660cf2e340fSJANAK DESAI activate_mm(active_mm, new_mm); 1661cf2e340fSJANAK DESAI new_mm = mm; 1662cf2e340fSJANAK DESAI } 1663cf2e340fSJANAK DESAI 1664cf2e340fSJANAK DESAI if (new_fd) { 1665cf2e340fSJANAK DESAI fd = current->files; 1666cf2e340fSJANAK DESAI current->files = new_fd; 1667cf2e340fSJANAK DESAI new_fd = fd; 1668cf2e340fSJANAK DESAI } 1669cf2e340fSJANAK DESAI 1670cf2e340fSJANAK DESAI task_unlock(current); 1671cf2e340fSJANAK DESAI } 1672cf2e340fSJANAK DESAI 1673c0b2fc31SSerge Hallyn if (new_nsproxy) 1674444f378bSLinus Torvalds put_nsproxy(new_nsproxy); 1675c0b2fc31SSerge Hallyn 1676cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 1677cf2e340fSJANAK DESAI if (new_fd) 1678cf2e340fSJANAK DESAI put_files_struct(new_fd); 1679cf2e340fSJANAK DESAI 1680cf2e340fSJANAK DESAI bad_unshare_cleanup_vm: 1681cf2e340fSJANAK DESAI if (new_mm) 1682cf2e340fSJANAK DESAI mmput(new_mm); 1683cf2e340fSJANAK DESAI 1684cf2e340fSJANAK DESAI bad_unshare_cleanup_sigh: 1685cf2e340fSJANAK DESAI if (new_sigh) 1686cf2e340fSJANAK DESAI if (atomic_dec_and_test(&new_sigh->count)) 1687cf2e340fSJANAK DESAI kmem_cache_free(sighand_cachep, new_sigh); 1688cf2e340fSJANAK DESAI 1689cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 1690cf2e340fSJANAK DESAI if (new_fs) 1691498052bbSAl Viro free_fs_struct(new_fs); 1692cf2e340fSJANAK DESAI 1693cf2e340fSJANAK DESAI bad_unshare_cleanup_thread: 1694cf2e340fSJANAK DESAI bad_unshare_out: 1695cf2e340fSJANAK DESAI return err; 1696cf2e340fSJANAK DESAI } 16973b125388SAl Viro 16983b125388SAl Viro /* 16993b125388SAl Viro * Helper to unshare the files of the current task. 17003b125388SAl Viro * We don't want to expose copy_files internals to 17013b125388SAl Viro * the exec layer of the kernel. 17023b125388SAl Viro */ 17033b125388SAl Viro 17043b125388SAl Viro int unshare_files(struct files_struct **displaced) 17053b125388SAl Viro { 17063b125388SAl Viro struct task_struct *task = current; 170750704516SAl Viro struct files_struct *copy = NULL; 17083b125388SAl Viro int error; 17093b125388SAl Viro 17103b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 17113b125388SAl Viro if (error || !copy) { 17123b125388SAl Viro *displaced = NULL; 17133b125388SAl Viro return error; 17143b125388SAl Viro } 17153b125388SAl Viro *displaced = task->files; 17163b125388SAl Viro task_lock(task); 17173b125388SAl Viro task->files = copy; 17183b125388SAl Viro task_unlock(task); 17193b125388SAl Viro return 0; 17203b125388SAl Viro } 1721