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> 206b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 211da177e4SLinus Torvalds #include <linux/personality.h> 221da177e4SLinus Torvalds #include <linux/mempolicy.h> 231da177e4SLinus Torvalds #include <linux/sem.h> 241da177e4SLinus Torvalds #include <linux/file.h> 251da177e4SLinus Torvalds #include <linux/key.h> 261da177e4SLinus Torvalds #include <linux/binfmts.h> 271da177e4SLinus Torvalds #include <linux/mman.h> 281da177e4SLinus Torvalds #include <linux/fs.h> 29ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 30c59ede7bSRandy.Dunlap #include <linux/capability.h> 311da177e4SLinus Torvalds #include <linux/cpu.h> 321da177e4SLinus Torvalds #include <linux/cpuset.h> 331da177e4SLinus Torvalds #include <linux/security.h> 341da177e4SLinus Torvalds #include <linux/swap.h> 351da177e4SLinus Torvalds #include <linux/syscalls.h> 361da177e4SLinus Torvalds #include <linux/jiffies.h> 371da177e4SLinus Torvalds #include <linux/futex.h> 387c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 39ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 401da177e4SLinus Torvalds #include <linux/ptrace.h> 411da177e4SLinus Torvalds #include <linux/mount.h> 421da177e4SLinus Torvalds #include <linux/audit.h> 431da177e4SLinus Torvalds #include <linux/profile.h> 441da177e4SLinus Torvalds #include <linux/rmap.h> 451da177e4SLinus Torvalds #include <linux/acct.h> 468f0ab514SJay Lan #include <linux/tsacct_kern.h> 479f46080cSMatt Helsley #include <linux/cn_proc.h> 48ca74e92bSShailabh Nagar #include <linux/delayacct.h> 49ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 500a425405SArjan van de Ven #include <linux/random.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <asm/pgtable.h> 531da177e4SLinus Torvalds #include <asm/pgalloc.h> 541da177e4SLinus Torvalds #include <asm/uaccess.h> 551da177e4SLinus Torvalds #include <asm/mmu_context.h> 561da177e4SLinus Torvalds #include <asm/cacheflush.h> 571da177e4SLinus Torvalds #include <asm/tlbflush.h> 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* 601da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 611da177e4SLinus Torvalds */ 621da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 631da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds int nr_processes(void) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds int cpu; 741da177e4SLinus Torvalds int total = 0; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds for_each_online_cpu(cpu) 771da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds return total; 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 831da177e4SLinus Torvalds # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL) 841da177e4SLinus Torvalds # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk)) 85e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 861da177e4SLinus Torvalds #endif 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 89e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 92e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 95e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 98e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 101e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 104e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 1071da177e4SLinus Torvalds { 108f7e4217bSRoman Zippel free_thread_info(tsk->stack); 10923f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 1101da177e4SLinus Torvalds free_task_struct(tsk); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 1131da177e4SLinus Torvalds 114158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 1151da177e4SLinus Torvalds { 1161da177e4SLinus Torvalds WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE))); 1171da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 1181da177e4SLinus Torvalds WARN_ON(tsk == current); 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds security_task_free(tsk); 1211da177e4SLinus Torvalds free_uid(tsk->user); 1221da177e4SLinus Torvalds put_group_info(tsk->group_info); 12335df17c5SShailabh Nagar delayacct_tsk_free(tsk); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 1261da177e4SLinus Torvalds free_task(tsk); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds void __init fork_init(unsigned long mempages) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 1321da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 1331da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 1341da177e4SLinus Torvalds #endif 1351da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 1361da177e4SLinus Torvalds task_struct_cachep = 1371da177e4SLinus Torvalds kmem_cache_create("task_struct", sizeof(struct task_struct), 1381da177e4SLinus Torvalds ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL); 1391da177e4SLinus Torvalds #endif 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * The default maximum number of threads is set to a safe 1431da177e4SLinus Torvalds * value: the thread structures can take up at most half 1441da177e4SLinus Torvalds * of memory. 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* 1491da177e4SLinus Torvalds * we need to allow at least 20 threads to boot a system 1501da177e4SLinus Torvalds */ 1511da177e4SLinus Torvalds if(max_threads < 20) 1521da177e4SLinus Torvalds max_threads = 20; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 1551da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 1561da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 1571da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds struct task_struct *tsk; 1631da177e4SLinus Torvalds struct thread_info *ti; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds prepare_to_copy(orig); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds tsk = alloc_task_struct(); 1681da177e4SLinus Torvalds if (!tsk) 1691da177e4SLinus Torvalds return NULL; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds ti = alloc_thread_info(tsk); 1721da177e4SLinus Torvalds if (!ti) { 1731da177e4SLinus Torvalds free_task_struct(tsk); 1741da177e4SLinus Torvalds return NULL; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds *tsk = *orig; 178f7e4217bSRoman Zippel tsk->stack = ti; 17910ebffdeSAl Viro setup_thread_stack(tsk, orig); 1801da177e4SLinus Torvalds 1810a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 1820a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 1830a425405SArjan van de Ven #endif 1840a425405SArjan van de Ven 1851da177e4SLinus Torvalds /* One for us, one for whoever does the "release_task()" (usually parent) */ 1861da177e4SLinus Torvalds atomic_set(&tsk->usage,2); 1874b5d37acSGiancarlo Formicuccia atomic_set(&tsk->fs_excl, 0); 1886c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 1892056a782SJens Axboe tsk->btrace_seq = 0; 1906c5c9341SAlexey Dobriyan #endif 191a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 1921da177e4SLinus Torvalds return tsk; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds #ifdef CONFIG_MMU 1961da177e4SLinus Torvalds static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds struct vm_area_struct *mpnt, *tmp, **pprev; 1991da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 2001da177e4SLinus Torvalds int retval; 2011da177e4SLinus Torvalds unsigned long charge; 2021da177e4SLinus Torvalds struct mempolicy *pol; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 205ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 206ad339451SIngo Molnar /* 207ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 208ad339451SIngo Molnar */ 209ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 2107ee78232SHugh Dickins 2111da177e4SLinus Torvalds mm->locked_vm = 0; 2121da177e4SLinus Torvalds mm->mmap = NULL; 2131da177e4SLinus Torvalds mm->mmap_cache = NULL; 2141da177e4SLinus Torvalds mm->free_area_cache = oldmm->mmap_base; 2151363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 2161da177e4SLinus Torvalds mm->map_count = 0; 2171da177e4SLinus Torvalds cpus_clear(mm->cpu_vm_mask); 2181da177e4SLinus Torvalds mm->mm_rb = RB_ROOT; 2191da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 2201da177e4SLinus Torvalds rb_parent = NULL; 2211da177e4SLinus Torvalds pprev = &mm->mmap; 2221da177e4SLinus Torvalds 223fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 2241da177e4SLinus Torvalds struct file *file; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 2273b6bfcdbSHugh Dickins long pages = vma_pages(mpnt); 2283b6bfcdbSHugh Dickins mm->total_vm -= pages; 229ab50b8edSHugh Dickins vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 2303b6bfcdbSHugh Dickins -pages); 2311da177e4SLinus Torvalds continue; 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds charge = 0; 2341da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 2351da177e4SLinus Torvalds unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT; 2361da177e4SLinus Torvalds if (security_vm_enough_memory(len)) 2371da177e4SLinus Torvalds goto fail_nomem; 2381da177e4SLinus Torvalds charge = len; 2391da177e4SLinus Torvalds } 240e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 2411da177e4SLinus Torvalds if (!tmp) 2421da177e4SLinus Torvalds goto fail_nomem; 2431da177e4SLinus Torvalds *tmp = *mpnt; 2441da177e4SLinus Torvalds pol = mpol_copy(vma_policy(mpnt)); 2451da177e4SLinus Torvalds retval = PTR_ERR(pol); 2461da177e4SLinus Torvalds if (IS_ERR(pol)) 2471da177e4SLinus Torvalds goto fail_nomem_policy; 2481da177e4SLinus Torvalds vma_set_policy(tmp, pol); 2491da177e4SLinus Torvalds tmp->vm_flags &= ~VM_LOCKED; 2501da177e4SLinus Torvalds tmp->vm_mm = mm; 2511da177e4SLinus Torvalds tmp->vm_next = NULL; 2521da177e4SLinus Torvalds anon_vma_link(tmp); 2531da177e4SLinus Torvalds file = tmp->vm_file; 2541da177e4SLinus Torvalds if (file) { 255f3a43f3fSJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 2561da177e4SLinus Torvalds get_file(file); 2571da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 2581da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /* insert tmp into the share list, just after mpnt */ 2611da177e4SLinus Torvalds spin_lock(&file->f_mapping->i_mmap_lock); 2621da177e4SLinus Torvalds tmp->vm_truncate_count = mpnt->vm_truncate_count; 2631da177e4SLinus Torvalds flush_dcache_mmap_lock(file->f_mapping); 2641da177e4SLinus Torvalds vma_prio_tree_add(tmp, mpnt); 2651da177e4SLinus Torvalds flush_dcache_mmap_unlock(file->f_mapping); 2661da177e4SLinus Torvalds spin_unlock(&file->f_mapping->i_mmap_lock); 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* 2707ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds *pprev = tmp; 2731da177e4SLinus Torvalds pprev = &tmp->vm_next; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 2761da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 2771da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds mm->map_count++; 2800b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 2831da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds if (retval) 2861da177e4SLinus Torvalds goto out; 2871da177e4SLinus Torvalds } 288d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 289d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 2901da177e4SLinus Torvalds retval = 0; 2911da177e4SLinus Torvalds out: 2927ee78232SHugh Dickins up_write(&mm->mmap_sem); 293fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 2941da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 2951da177e4SLinus Torvalds return retval; 2961da177e4SLinus Torvalds fail_nomem_policy: 2971da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 2981da177e4SLinus Torvalds fail_nomem: 2991da177e4SLinus Torvalds retval = -ENOMEM; 3001da177e4SLinus Torvalds vm_unacct_memory(charge); 3011da177e4SLinus Torvalds goto out; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct * mm) 3051da177e4SLinus Torvalds { 3061da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 3071da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 3081da177e4SLinus Torvalds return -ENOMEM; 3091da177e4SLinus Torvalds return 0; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct * mm) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds pgd_free(mm->pgd); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds #else 3171da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 3181da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 3191da177e4SLinus Torvalds #define mm_free_pgd(mm) 3201da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 3231da177e4SLinus Torvalds 324e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 3251da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds #include <linux/init_task.h> 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds static struct mm_struct * mm_init(struct mm_struct * mm) 3301da177e4SLinus Torvalds { 3311da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 3321da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 3331da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 3341da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 3351da177e4SLinus Torvalds mm->core_waiters = 0; 3361da177e4SLinus Torvalds mm->nr_ptes = 0; 3374294621fSHugh Dickins set_mm_counter(mm, file_rss, 0); 338404351e6SHugh Dickins set_mm_counter(mm, anon_rss, 0); 3391da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 3401da177e4SLinus Torvalds rwlock_init(&mm->ioctx_list_lock); 3411da177e4SLinus Torvalds mm->ioctx_list = NULL; 3421da177e4SLinus Torvalds mm->free_area_cache = TASK_UNMAPPED_BASE; 3431363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds if (likely(!mm_alloc_pgd(mm))) { 3461da177e4SLinus Torvalds mm->def_flags = 0; 3471da177e4SLinus Torvalds return mm; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds free_mm(mm); 3501da177e4SLinus Torvalds return NULL; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds /* 3541da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 3551da177e4SLinus Torvalds */ 3561da177e4SLinus Torvalds struct mm_struct * mm_alloc(void) 3571da177e4SLinus Torvalds { 3581da177e4SLinus Torvalds struct mm_struct * mm; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds mm = allocate_mm(); 3611da177e4SLinus Torvalds if (mm) { 3621da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 3631da177e4SLinus Torvalds mm = mm_init(mm); 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds return mm; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds /* 3691da177e4SLinus Torvalds * Called when the last reference to the mm 3701da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 3711da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 3721da177e4SLinus Torvalds */ 3731da177e4SLinus Torvalds void fastcall __mmdrop(struct mm_struct *mm) 3741da177e4SLinus Torvalds { 3751da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 3761da177e4SLinus Torvalds mm_free_pgd(mm); 3771da177e4SLinus Torvalds destroy_context(mm); 3781da177e4SLinus Torvalds free_mm(mm); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /* 3821da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 3831da177e4SLinus Torvalds */ 3841da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 3851da177e4SLinus Torvalds { 3860ae26f1bSAndrew Morton might_sleep(); 3870ae26f1bSAndrew Morton 3881da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 3891da177e4SLinus Torvalds exit_aio(mm); 3901da177e4SLinus Torvalds exit_mmap(mm); 3911da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 3921da177e4SLinus Torvalds spin_lock(&mmlist_lock); 3931da177e4SLinus Torvalds list_del(&mm->mmlist); 3941da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds put_swap_token(mm); 3971da177e4SLinus Torvalds mmdrop(mm); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds /** 4031da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 4041da177e4SLinus Torvalds * 4051da177e4SLinus Torvalds * Returns %NULL if the task has no mm. Checks PF_BORROWED_MM (meaning 4061da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 4071da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 4081da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 4091da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 4101da177e4SLinus Torvalds */ 4111da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds struct mm_struct *mm; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds task_lock(task); 4161da177e4SLinus Torvalds mm = task->mm; 4171da177e4SLinus Torvalds if (mm) { 4181da177e4SLinus Torvalds if (task->flags & PF_BORROWED_MM) 4191da177e4SLinus Torvalds mm = NULL; 4201da177e4SLinus Torvalds else 4211da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds task_unlock(task); 4241da177e4SLinus Torvalds return mm; 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 4291da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 4301da177e4SLinus Torvalds * error success whatever. 4311da177e4SLinus Torvalds * 4321da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 4331da177e4SLinus Torvalds * from the current process. 4341da177e4SLinus Torvalds * 4351da177e4SLinus Torvalds * This difference is important for error handling, when we 4361da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 4371da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 4381da177e4SLinus Torvalds * restoring the old one. . . 4391da177e4SLinus Torvalds * Eric Biederman 10 January 1998 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 4421da177e4SLinus Torvalds { 4431da177e4SLinus Torvalds struct completion *vfork_done = tsk->vfork_done; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds /* Get rid of any cached register state */ 4461da177e4SLinus Torvalds deactivate_mm(tsk, mm); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* notify parent sleeping on vfork() */ 4491da177e4SLinus Torvalds if (vfork_done) { 4501da177e4SLinus Torvalds tsk->vfork_done = NULL; 4511da177e4SLinus Torvalds complete(vfork_done); 4521da177e4SLinus Torvalds } 453fec1d011SRoland McGrath 454fec1d011SRoland McGrath /* 455fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 456fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 457fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 458fec1d011SRoland McGrath * trouble otherwise. Userland only wants this done for a sys_exit. 459fec1d011SRoland McGrath */ 460fec1d011SRoland McGrath if (tsk->clear_child_tid 461fec1d011SRoland McGrath && !(tsk->flags & PF_SIGNALED) 462fec1d011SRoland McGrath && atomic_read(&mm->mm_users) > 1) { 4631da177e4SLinus Torvalds u32 __user * tidptr = tsk->clear_child_tid; 4641da177e4SLinus Torvalds tsk->clear_child_tid = NULL; 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds /* 4671da177e4SLinus Torvalds * We don't check the error code - if userspace has 4681da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds put_user(0, tidptr); 4711da177e4SLinus Torvalds sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0); 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds 475a0a7ec30SJANAK DESAI /* 476a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 477a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 478a0a7ec30SJANAK DESAI */ 479a0a7ec30SJANAK DESAI static struct mm_struct *dup_mm(struct task_struct *tsk) 480a0a7ec30SJANAK DESAI { 481a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 482a0a7ec30SJANAK DESAI int err; 483a0a7ec30SJANAK DESAI 484a0a7ec30SJANAK DESAI if (!oldmm) 485a0a7ec30SJANAK DESAI return NULL; 486a0a7ec30SJANAK DESAI 487a0a7ec30SJANAK DESAI mm = allocate_mm(); 488a0a7ec30SJANAK DESAI if (!mm) 489a0a7ec30SJANAK DESAI goto fail_nomem; 490a0a7ec30SJANAK DESAI 491a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 492a0a7ec30SJANAK DESAI 4937602bdf2SAshwin Chaugule /* Initializing for Swap token stuff */ 4947602bdf2SAshwin Chaugule mm->token_priority = 0; 4957602bdf2SAshwin Chaugule mm->last_interval = 0; 4967602bdf2SAshwin Chaugule 497a0a7ec30SJANAK DESAI if (!mm_init(mm)) 498a0a7ec30SJANAK DESAI goto fail_nomem; 499a0a7ec30SJANAK DESAI 500a0a7ec30SJANAK DESAI if (init_new_context(tsk, mm)) 501a0a7ec30SJANAK DESAI goto fail_nocontext; 502a0a7ec30SJANAK DESAI 503a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 504a0a7ec30SJANAK DESAI if (err) 505a0a7ec30SJANAK DESAI goto free_pt; 506a0a7ec30SJANAK DESAI 507a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 508a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 509a0a7ec30SJANAK DESAI 510a0a7ec30SJANAK DESAI return mm; 511a0a7ec30SJANAK DESAI 512a0a7ec30SJANAK DESAI free_pt: 513a0a7ec30SJANAK DESAI mmput(mm); 514a0a7ec30SJANAK DESAI 515a0a7ec30SJANAK DESAI fail_nomem: 516a0a7ec30SJANAK DESAI return NULL; 517a0a7ec30SJANAK DESAI 518a0a7ec30SJANAK DESAI fail_nocontext: 519a0a7ec30SJANAK DESAI /* 520a0a7ec30SJANAK DESAI * If init_new_context() failed, we cannot use mmput() to free the mm 521a0a7ec30SJANAK DESAI * because it calls destroy_context() 522a0a7ec30SJANAK DESAI */ 523a0a7ec30SJANAK DESAI mm_free_pgd(mm); 524a0a7ec30SJANAK DESAI free_mm(mm); 525a0a7ec30SJANAK DESAI return NULL; 526a0a7ec30SJANAK DESAI } 527a0a7ec30SJANAK DESAI 5281da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct * tsk) 5291da177e4SLinus Torvalds { 5301da177e4SLinus Torvalds struct mm_struct * mm, *oldmm; 5311da177e4SLinus Torvalds int retval; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 5341da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds tsk->mm = NULL; 5371da177e4SLinus Torvalds tsk->active_mm = NULL; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds /* 5401da177e4SLinus Torvalds * Are we cloning a kernel thread? 5411da177e4SLinus Torvalds * 5421da177e4SLinus Torvalds * We need to steal a active VM for that.. 5431da177e4SLinus Torvalds */ 5441da177e4SLinus Torvalds oldmm = current->mm; 5451da177e4SLinus Torvalds if (!oldmm) 5461da177e4SLinus Torvalds return 0; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 5491da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 5501da177e4SLinus Torvalds mm = oldmm; 5511da177e4SLinus Torvalds goto good_mm; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds retval = -ENOMEM; 555a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 5561da177e4SLinus Torvalds if (!mm) 5571da177e4SLinus Torvalds goto fail_nomem; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds good_mm: 5607602bdf2SAshwin Chaugule /* Initializing for Swap token stuff */ 5617602bdf2SAshwin Chaugule mm->token_priority = 0; 5627602bdf2SAshwin Chaugule mm->last_interval = 0; 5637602bdf2SAshwin Chaugule 5641da177e4SLinus Torvalds tsk->mm = mm; 5651da177e4SLinus Torvalds tsk->active_mm = mm; 5661da177e4SLinus Torvalds return 0; 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds fail_nomem: 5691da177e4SLinus Torvalds return retval; 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old) 5731da177e4SLinus Torvalds { 5741da177e4SLinus Torvalds struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL); 5751da177e4SLinus Torvalds /* We don't need to lock fs - think why ;-) */ 5761da177e4SLinus Torvalds if (fs) { 5771da177e4SLinus Torvalds atomic_set(&fs->count, 1); 5781da177e4SLinus Torvalds rwlock_init(&fs->lock); 5791da177e4SLinus Torvalds fs->umask = old->umask; 5801da177e4SLinus Torvalds read_lock(&old->lock); 5811da177e4SLinus Torvalds fs->rootmnt = mntget(old->rootmnt); 5821da177e4SLinus Torvalds fs->root = dget(old->root); 5831da177e4SLinus Torvalds fs->pwdmnt = mntget(old->pwdmnt); 5841da177e4SLinus Torvalds fs->pwd = dget(old->pwd); 5851da177e4SLinus Torvalds if (old->altroot) { 5861da177e4SLinus Torvalds fs->altrootmnt = mntget(old->altrootmnt); 5871da177e4SLinus Torvalds fs->altroot = dget(old->altroot); 5881da177e4SLinus Torvalds } else { 5891da177e4SLinus Torvalds fs->altrootmnt = NULL; 5901da177e4SLinus Torvalds fs->altroot = NULL; 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds read_unlock(&old->lock); 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds return fs; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds struct fs_struct *copy_fs_struct(struct fs_struct *old) 5981da177e4SLinus Torvalds { 5991da177e4SLinus Torvalds return __copy_fs_struct(old); 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(copy_fs_struct); 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk) 6051da177e4SLinus Torvalds { 6061da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 6071da177e4SLinus Torvalds atomic_inc(¤t->fs->count); 6081da177e4SLinus Torvalds return 0; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds tsk->fs = __copy_fs_struct(current->fs); 6111da177e4SLinus Torvalds if (!tsk->fs) 6121da177e4SLinus Torvalds return -ENOMEM; 6131da177e4SLinus Torvalds return 0; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 616ab2af1f5SDipankar Sarma static int count_open_files(struct fdtable *fdt) 6171da177e4SLinus Torvalds { 618bbea9f69SVadim Lobanov int size = fdt->max_fds; 6191da177e4SLinus Torvalds int i; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* Find the last open fd */ 6221da177e4SLinus Torvalds for (i = size/(8*sizeof(long)); i > 0; ) { 623badf1662SDipankar Sarma if (fdt->open_fds->fds_bits[--i]) 6241da177e4SLinus Torvalds break; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds i = (i+1) * 8 * sizeof(long); 6271da177e4SLinus Torvalds return i; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 630badf1662SDipankar Sarma static struct files_struct *alloc_files(void) 631badf1662SDipankar Sarma { 632badf1662SDipankar Sarma struct files_struct *newf; 633badf1662SDipankar Sarma struct fdtable *fdt; 634badf1662SDipankar Sarma 635e94b1766SChristoph Lameter newf = kmem_cache_alloc(files_cachep, GFP_KERNEL); 636badf1662SDipankar Sarma if (!newf) 637badf1662SDipankar Sarma goto out; 638badf1662SDipankar Sarma 639badf1662SDipankar Sarma atomic_set(&newf->count, 1); 640badf1662SDipankar Sarma 641badf1662SDipankar Sarma spin_lock_init(&newf->file_lock); 6420c9e63fdSEric Dumazet newf->next_fd = 0; 643ab2af1f5SDipankar Sarma fdt = &newf->fdtab; 644badf1662SDipankar Sarma fdt->max_fds = NR_OPEN_DEFAULT; 6450c9e63fdSEric Dumazet fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init; 6460c9e63fdSEric Dumazet fdt->open_fds = (fd_set *)&newf->open_fds_init; 647badf1662SDipankar Sarma fdt->fd = &newf->fd_array[0]; 648ab2af1f5SDipankar Sarma INIT_RCU_HEAD(&fdt->rcu); 649ab2af1f5SDipankar Sarma fdt->next = NULL; 650ab2af1f5SDipankar Sarma rcu_assign_pointer(newf->fdt, fdt); 651badf1662SDipankar Sarma out: 652badf1662SDipankar Sarma return newf; 653badf1662SDipankar Sarma } 654badf1662SDipankar Sarma 655a016f338SJANAK DESAI /* 656a016f338SJANAK DESAI * Allocate a new files structure and copy contents from the 657a016f338SJANAK DESAI * passed in files structure. 6586e667260SPrasanna Meda * errorp will be valid only when the returned files_struct is NULL. 659a016f338SJANAK DESAI */ 660a016f338SJANAK DESAI static struct files_struct *dup_fd(struct files_struct *oldf, int *errorp) 6611da177e4SLinus Torvalds { 662a016f338SJANAK DESAI struct files_struct *newf; 6631da177e4SLinus Torvalds struct file **old_fds, **new_fds; 664bbea9f69SVadim Lobanov int open_files, size, i; 665badf1662SDipankar Sarma struct fdtable *old_fdt, *new_fdt; 6661da177e4SLinus Torvalds 6676e667260SPrasanna Meda *errorp = -ENOMEM; 668badf1662SDipankar Sarma newf = alloc_files(); 6691da177e4SLinus Torvalds if (!newf) 6701da177e4SLinus Torvalds goto out; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds spin_lock(&oldf->file_lock); 673badf1662SDipankar Sarma old_fdt = files_fdtable(oldf); 674badf1662SDipankar Sarma new_fdt = files_fdtable(newf); 675ab2af1f5SDipankar Sarma open_files = count_open_files(old_fdt); 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds /* 678bbea9f69SVadim Lobanov * Check whether we need to allocate a larger fd array and fd set. 6791da177e4SLinus Torvalds * Note: we're not a clone task, so the open count won't change. 6801da177e4SLinus Torvalds */ 681badf1662SDipankar Sarma if (open_files > new_fdt->max_fds) { 682badf1662SDipankar Sarma new_fdt->max_fds = 0; 6831da177e4SLinus Torvalds spin_unlock(&oldf->file_lock); 6841da177e4SLinus Torvalds spin_lock(&newf->file_lock); 685a016f338SJANAK DESAI *errorp = expand_files(newf, open_files-1); 6861da177e4SLinus Torvalds spin_unlock(&newf->file_lock); 687a016f338SJANAK DESAI if (*errorp < 0) 6881da177e4SLinus Torvalds goto out_release; 689ab2af1f5SDipankar Sarma new_fdt = files_fdtable(newf); 690ab2af1f5SDipankar Sarma /* 691ab2af1f5SDipankar Sarma * Reacquire the oldf lock and a pointer to its fd table 692ab2af1f5SDipankar Sarma * who knows it may have a new bigger fd table. We need 693ab2af1f5SDipankar Sarma * the latest pointer. 694ab2af1f5SDipankar Sarma */ 6951da177e4SLinus Torvalds spin_lock(&oldf->file_lock); 696ab2af1f5SDipankar Sarma old_fdt = files_fdtable(oldf); 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 699badf1662SDipankar Sarma old_fds = old_fdt->fd; 700badf1662SDipankar Sarma new_fds = new_fdt->fd; 7011da177e4SLinus Torvalds 702f3d19c90SVadim Lobanov memcpy(new_fdt->open_fds->fds_bits, 703f3d19c90SVadim Lobanov old_fdt->open_fds->fds_bits, open_files/8); 704f3d19c90SVadim Lobanov memcpy(new_fdt->close_on_exec->fds_bits, 705f3d19c90SVadim Lobanov old_fdt->close_on_exec->fds_bits, open_files/8); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds for (i = open_files; i != 0; i--) { 7081da177e4SLinus Torvalds struct file *f = *old_fds++; 7091da177e4SLinus Torvalds if (f) { 7101da177e4SLinus Torvalds get_file(f); 7111da177e4SLinus Torvalds } else { 7121da177e4SLinus Torvalds /* 7131da177e4SLinus Torvalds * The fd may be claimed in the fd bitmap but not yet 7141da177e4SLinus Torvalds * instantiated in the files array if a sibling thread 7151da177e4SLinus Torvalds * is partway through open(). So make sure that this 7161da177e4SLinus Torvalds * fd is available to the new process. 7171da177e4SLinus Torvalds */ 718badf1662SDipankar Sarma FD_CLR(open_files - i, new_fdt->open_fds); 7191da177e4SLinus Torvalds } 720ab2af1f5SDipankar Sarma rcu_assign_pointer(*new_fds++, f); 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds spin_unlock(&oldf->file_lock); 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds /* compute the remainder to be cleared */ 725badf1662SDipankar Sarma size = (new_fdt->max_fds - open_files) * sizeof(struct file *); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* This is long word aligned thus could use a optimized version */ 7281da177e4SLinus Torvalds memset(new_fds, 0, size); 7291da177e4SLinus Torvalds 730bbea9f69SVadim Lobanov if (new_fdt->max_fds > open_files) { 731bbea9f69SVadim Lobanov int left = (new_fdt->max_fds-open_files)/8; 7321da177e4SLinus Torvalds int start = open_files / (8 * sizeof(unsigned long)); 7331da177e4SLinus Torvalds 734badf1662SDipankar Sarma memset(&new_fdt->open_fds->fds_bits[start], 0, left); 735badf1662SDipankar Sarma memset(&new_fdt->close_on_exec->fds_bits[start], 0, left); 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 738a016f338SJANAK DESAI return newf; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds out_release: 7411da177e4SLinus Torvalds kmem_cache_free(files_cachep, newf); 742f3d19c90SVadim Lobanov out: 74342862298SKirill Korotaev return NULL; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 746a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct * tsk) 747a016f338SJANAK DESAI { 748a016f338SJANAK DESAI struct files_struct *oldf, *newf; 749a016f338SJANAK DESAI int error = 0; 750a016f338SJANAK DESAI 751a016f338SJANAK DESAI /* 752a016f338SJANAK DESAI * A background process may not have any files ... 753a016f338SJANAK DESAI */ 754a016f338SJANAK DESAI oldf = current->files; 755a016f338SJANAK DESAI if (!oldf) 756a016f338SJANAK DESAI goto out; 757a016f338SJANAK DESAI 758a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 759a016f338SJANAK DESAI atomic_inc(&oldf->count); 760a016f338SJANAK DESAI goto out; 761a016f338SJANAK DESAI } 762a016f338SJANAK DESAI 763a016f338SJANAK DESAI /* 764a016f338SJANAK DESAI * Note: we may be using current for both targets (See exec.c) 765a016f338SJANAK DESAI * This works because we cache current->files (old) as oldf. Don't 766a016f338SJANAK DESAI * break this. 767a016f338SJANAK DESAI */ 768a016f338SJANAK DESAI tsk->files = NULL; 769a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 770a016f338SJANAK DESAI if (!newf) 771a016f338SJANAK DESAI goto out; 772a016f338SJANAK DESAI 773a016f338SJANAK DESAI tsk->files = newf; 774a016f338SJANAK DESAI error = 0; 775a016f338SJANAK DESAI out: 776a016f338SJANAK DESAI return error; 777a016f338SJANAK DESAI } 778a016f338SJANAK DESAI 7791da177e4SLinus Torvalds /* 7801da177e4SLinus Torvalds * Helper to unshare the files of the current task. 7811da177e4SLinus Torvalds * We don't want to expose copy_files internals to 7821da177e4SLinus Torvalds * the exec layer of the kernel. 7831da177e4SLinus Torvalds */ 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds int unshare_files(void) 7861da177e4SLinus Torvalds { 7871da177e4SLinus Torvalds struct files_struct *files = current->files; 7881da177e4SLinus Torvalds int rc; 7891da177e4SLinus Torvalds 790910dea7fSEric Sesterhenn BUG_ON(!files); 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* This can race but the race causes us to copy when we don't 7931da177e4SLinus Torvalds need to and drop the copy */ 7941da177e4SLinus Torvalds if(atomic_read(&files->count) == 1) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds atomic_inc(&files->count); 7971da177e4SLinus Torvalds return 0; 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds rc = copy_files(0, current); 8001da177e4SLinus Torvalds if(rc) 8011da177e4SLinus Torvalds current->files = files; 8021da177e4SLinus Torvalds return rc; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds EXPORT_SYMBOL(unshare_files); 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk) 8081da177e4SLinus Torvalds { 8091da177e4SLinus Torvalds struct sighand_struct *sig; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) { 8121da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 8131da177e4SLinus Torvalds return 0; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 816e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 8171da177e4SLinus Torvalds if (!sig) 8181da177e4SLinus Torvalds return -ENOMEM; 8191da177e4SLinus Torvalds atomic_set(&sig->count, 1); 8201da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 8211da177e4SLinus Torvalds return 0; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 824a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 825c81addc9SOleg Nesterov { 826c81addc9SOleg Nesterov if (atomic_dec_and_test(&sighand->count)) 827c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 828c81addc9SOleg Nesterov } 829c81addc9SOleg Nesterov 8301da177e4SLinus Torvalds static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk) 8311da177e4SLinus Torvalds { 8321da177e4SLinus Torvalds struct signal_struct *sig; 8331da177e4SLinus Torvalds int ret; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds if (clone_flags & CLONE_THREAD) { 8361da177e4SLinus Torvalds atomic_inc(¤t->signal->count); 8371da177e4SLinus Torvalds atomic_inc(¤t->signal->live); 8381da177e4SLinus Torvalds return 0; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL); 8411da177e4SLinus Torvalds tsk->signal = sig; 8421da177e4SLinus Torvalds if (!sig) 8431da177e4SLinus Torvalds return -ENOMEM; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds ret = copy_thread_group_keys(tsk); 8461da177e4SLinus Torvalds if (ret < 0) { 8471da177e4SLinus Torvalds kmem_cache_free(signal_cachep, sig); 8481da177e4SLinus Torvalds return ret; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds atomic_set(&sig->count, 1); 8521da177e4SLinus Torvalds atomic_set(&sig->live, 1); 8531da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 8541da177e4SLinus Torvalds sig->flags = 0; 8551da177e4SLinus Torvalds sig->group_exit_code = 0; 8561da177e4SLinus Torvalds sig->group_exit_task = NULL; 8571da177e4SLinus Torvalds sig->group_stop_count = 0; 8581da177e4SLinus Torvalds sig->curr_target = NULL; 8591da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 8601da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 8611da177e4SLinus Torvalds 862c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 8632ff678b8SThomas Gleixner sig->it_real_incr.tv64 = 0; 8641da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 86505cfb614SRoman Zippel sig->tsk = tsk; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds sig->it_virt_expires = cputime_zero; 8681da177e4SLinus Torvalds sig->it_virt_incr = cputime_zero; 8691da177e4SLinus Torvalds sig->it_prof_expires = cputime_zero; 8701da177e4SLinus Torvalds sig->it_prof_incr = cputime_zero; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds sig->leader = 0; /* session leadership doesn't inherit */ 873ab521dc0SEric W. Biederman sig->tty_old_pgrp = NULL; 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero; 8761da177e4SLinus Torvalds sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0; 8771da177e4SLinus Torvalds sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0; 8786eaeeabaSEric Dumazet sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0; 8791da177e4SLinus Torvalds sig->sched_time = 0; 8801da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->cpu_timers[0]); 8811da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->cpu_timers[1]); 8821da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->cpu_timers[2]); 883ad4ecbcbSShailabh Nagar taskstats_tgid_init(sig); 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds task_lock(current->group_leader); 8861da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 8871da177e4SLinus Torvalds task_unlock(current->group_leader); 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 8901da177e4SLinus Torvalds /* 8911da177e4SLinus Torvalds * New sole thread in the process gets an expiry time 8921da177e4SLinus Torvalds * of the whole CPU time limit. 8931da177e4SLinus Torvalds */ 8941da177e4SLinus Torvalds tsk->it_prof_expires = 8951da177e4SLinus Torvalds secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur); 8961da177e4SLinus Torvalds } 8970e464814SKaiGai Kohei acct_init_pacct(&sig->pacct); 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds return 0; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9026b3934efSOleg Nesterov void __cleanup_signal(struct signal_struct *sig) 9036b3934efSOleg Nesterov { 9046b3934efSOleg Nesterov exit_thread_group_keys(sig); 9056b3934efSOleg Nesterov kmem_cache_free(signal_cachep, sig); 9066b3934efSOleg Nesterov } 9076b3934efSOleg Nesterov 9086b3934efSOleg Nesterov static inline void cleanup_signal(struct task_struct *tsk) 9096b3934efSOleg Nesterov { 9106b3934efSOleg Nesterov struct signal_struct *sig = tsk->signal; 9116b3934efSOleg Nesterov 9126b3934efSOleg Nesterov atomic_dec(&sig->live); 9136b3934efSOleg Nesterov 9146b3934efSOleg Nesterov if (atomic_dec_and_test(&sig->count)) 9156b3934efSOleg Nesterov __cleanup_signal(sig); 9166b3934efSOleg Nesterov } 9176b3934efSOleg Nesterov 9181da177e4SLinus Torvalds static inline void copy_flags(unsigned long clone_flags, struct task_struct *p) 9191da177e4SLinus Torvalds { 9201da177e4SLinus Torvalds unsigned long new_flags = p->flags; 9211da177e4SLinus Torvalds 922d1209d04SAlan Stern new_flags &= ~(PF_SUPERPRIV | PF_NOFREEZE); 9231da177e4SLinus Torvalds new_flags |= PF_FORKNOEXEC; 9241da177e4SLinus Torvalds if (!(clone_flags & CLONE_PTRACE)) 9251da177e4SLinus Torvalds p->ptrace = 0; 9261da177e4SLinus Torvalds p->flags = new_flags; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds asmlinkage long sys_set_tid_address(int __user *tidptr) 9301da177e4SLinus Torvalds { 9311da177e4SLinus Torvalds current->clear_child_tid = tidptr; 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds return current->pid; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 93623f78d4aSIngo Molnar static inline void rt_mutex_init_task(struct task_struct *p) 93723f78d4aSIngo Molnar { 93823f78d4aSIngo Molnar spin_lock_init(&p->pi_lock); 939e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 94023f78d4aSIngo Molnar plist_head_init(&p->pi_waiters, &p->pi_lock); 94123f78d4aSIngo Molnar p->pi_blocked_on = NULL; 94223f78d4aSIngo Molnar #endif 94323f78d4aSIngo Molnar } 94423f78d4aSIngo Molnar 9451da177e4SLinus Torvalds /* 9461da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 9471da177e4SLinus Torvalds * but does not actually start it yet. 9481da177e4SLinus Torvalds * 9491da177e4SLinus Torvalds * It copies the registers, and all the appropriate 9501da177e4SLinus Torvalds * parts of the process environment (as per the clone 9511da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 9521da177e4SLinus Torvalds */ 95336c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 9541da177e4SLinus Torvalds unsigned long stack_start, 9551da177e4SLinus Torvalds struct pt_regs *regs, 9561da177e4SLinus Torvalds unsigned long stack_size, 9571da177e4SLinus Torvalds int __user *parent_tidptr, 9581da177e4SLinus Torvalds int __user *child_tidptr, 95985868995SSukadev Bhattiprolu struct pid *pid) 9601da177e4SLinus Torvalds { 9611da177e4SLinus Torvalds int retval; 9621da177e4SLinus Torvalds struct task_struct *p = NULL; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 9651da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds /* 9681da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 9691da177e4SLinus Torvalds * can only be started up within the thread group. 9701da177e4SLinus Torvalds */ 9711da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 9721da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds /* 9751da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 9761da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 9771da177e4SLinus Torvalds * for various simplifications in other code. 9781da177e4SLinus Torvalds */ 9791da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 9801da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds retval = security_task_create(clone_flags); 9831da177e4SLinus Torvalds if (retval) 9841da177e4SLinus Torvalds goto fork_out; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds retval = -ENOMEM; 9871da177e4SLinus Torvalds p = dup_task_struct(current); 9881da177e4SLinus Torvalds if (!p) 9891da177e4SLinus Torvalds goto fork_out; 9901da177e4SLinus Torvalds 991bea493a0SPeter Zijlstra rt_mutex_init_task(p); 992bea493a0SPeter Zijlstra 993de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 994de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 995de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 996de30a2b3SIngo Molnar #endif 9971da177e4SLinus Torvalds retval = -EAGAIN; 9981da177e4SLinus Torvalds if (atomic_read(&p->user->processes) >= 9991da177e4SLinus Torvalds p->signal->rlim[RLIMIT_NPROC].rlim_cur) { 10001da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) && 10011da177e4SLinus Torvalds p->user != &root_user) 10021da177e4SLinus Torvalds goto bad_fork_free; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds atomic_inc(&p->user->__count); 10061da177e4SLinus Torvalds atomic_inc(&p->user->processes); 10071da177e4SLinus Torvalds get_group_info(p->group_info); 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /* 10101da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 10111da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 10121da177e4SLinus Torvalds * to stop root fork bombs. 10131da177e4SLinus Torvalds */ 10141da177e4SLinus Torvalds if (nr_threads >= max_threads) 10151da177e4SLinus Torvalds goto bad_fork_cleanup_count; 10161da177e4SLinus Torvalds 1017a1261f54SAl Viro if (!try_module_get(task_thread_info(p)->exec_domain->module)) 10181da177e4SLinus Torvalds goto bad_fork_cleanup_count; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds if (p->binfmt && !try_module_get(p->binfmt->module)) 10211da177e4SLinus Torvalds goto bad_fork_cleanup_put_domain; 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds p->did_exec = 0; 1024ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 10251da177e4SLinus Torvalds copy_flags(clone_flags, p); 102685868995SSukadev Bhattiprolu p->pid = pid_nr(pid); 10271da177e4SLinus Torvalds retval = -EFAULT; 10281da177e4SLinus Torvalds if (clone_flags & CLONE_PARENT_SETTID) 10291da177e4SLinus Torvalds if (put_user(p->pid, parent_tidptr)) 103035df17c5SShailabh Nagar goto bad_fork_cleanup_delays_binfmt; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 10331da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 10341da177e4SLinus Torvalds p->vfork_done = NULL; 10351da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SIGPENDING); 10381da177e4SLinus Torvalds init_sigpending(&p->pending); 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds p->utime = cputime_zero; 10411da177e4SLinus Torvalds p->stime = cputime_zero; 10421da177e4SLinus Torvalds p->sched_time = 0; 10434b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 10441da177e4SLinus Torvalds p->rchar = 0; /* I/O counter: bytes read */ 10451da177e4SLinus Torvalds p->wchar = 0; /* I/O counter: bytes written */ 10461da177e4SLinus Torvalds p->syscr = 0; /* I/O counter: read syscalls */ 10471da177e4SLinus Torvalds p->syscw = 0; /* I/O counter: write syscalls */ 10484b98d11bSAlexey Dobriyan #endif 10497c3ab738SAndrew Morton task_io_accounting_init(p); 10501da177e4SLinus Torvalds acct_clear_integrals(p); 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds p->it_virt_expires = cputime_zero; 10531da177e4SLinus Torvalds p->it_prof_expires = cputime_zero; 10541da177e4SLinus Torvalds p->it_sched_expires = 0; 10551da177e4SLinus Torvalds INIT_LIST_HEAD(&p->cpu_timers[0]); 10561da177e4SLinus Torvalds INIT_LIST_HEAD(&p->cpu_timers[1]); 10571da177e4SLinus Torvalds INIT_LIST_HEAD(&p->cpu_timers[2]); 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds p->lock_depth = -1; /* -1 = no lock */ 10601da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&p->start_time); 10611da177e4SLinus Torvalds p->security = NULL; 10621da177e4SLinus Torvalds p->io_context = NULL; 10631da177e4SLinus Torvalds p->io_wait = NULL; 10641da177e4SLinus Torvalds p->audit_context = NULL; 1065b4b26418SPaul Jackson cpuset_fork(p); 10661da177e4SLinus Torvalds #ifdef CONFIG_NUMA 10671da177e4SLinus Torvalds p->mempolicy = mpol_copy(p->mempolicy); 10681da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 10691da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 10701da177e4SLinus Torvalds p->mempolicy = NULL; 1071b4b26418SPaul Jackson goto bad_fork_cleanup_cpuset; 10721da177e4SLinus Torvalds } 1073c61afb18SPaul Jackson mpol_fix_fork_child_flag(p); 10741da177e4SLinus Torvalds #endif 1075de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1076de30a2b3SIngo Molnar p->irq_events = 0; 1077b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW 1078b36e4758SRussell King p->hardirqs_enabled = 1; 1079b36e4758SRussell King #else 1080de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1081b36e4758SRussell King #endif 1082de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1083de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1084de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1085de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1086de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1087de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1088de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1089de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1090de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1091de30a2b3SIngo Molnar p->hardirq_context = 0; 1092de30a2b3SIngo Molnar p->softirq_context = 0; 1093de30a2b3SIngo Molnar #endif 1094fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1095fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1096fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1097fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1098fbb9ce95SIngo Molnar #endif 10991da177e4SLinus Torvalds 1100408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1101408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1102408894eeSIngo Molnar #endif 1103408894eeSIngo Molnar 11041da177e4SLinus Torvalds p->tgid = p->pid; 11051da177e4SLinus Torvalds if (clone_flags & CLONE_THREAD) 11061da177e4SLinus Torvalds p->tgid = current->tgid; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds if ((retval = security_task_alloc(p))) 11091da177e4SLinus Torvalds goto bad_fork_cleanup_policy; 11101da177e4SLinus Torvalds if ((retval = audit_alloc(p))) 11111da177e4SLinus Torvalds goto bad_fork_cleanup_security; 11121da177e4SLinus Torvalds /* copy all the process information */ 11131da177e4SLinus Torvalds if ((retval = copy_semundo(clone_flags, p))) 11141da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 11151da177e4SLinus Torvalds if ((retval = copy_files(clone_flags, p))) 11161da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 11171da177e4SLinus Torvalds if ((retval = copy_fs(clone_flags, p))) 11181da177e4SLinus Torvalds goto bad_fork_cleanup_files; 11191da177e4SLinus Torvalds if ((retval = copy_sighand(clone_flags, p))) 11201da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 11211da177e4SLinus Torvalds if ((retval = copy_signal(clone_flags, p))) 11221da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 11231da177e4SLinus Torvalds if ((retval = copy_mm(clone_flags, p))) 11241da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 11251da177e4SLinus Torvalds if ((retval = copy_keys(clone_flags, p))) 11261da177e4SLinus Torvalds goto bad_fork_cleanup_mm; 1127ab516013SSerge E. Hallyn if ((retval = copy_namespaces(clone_flags, p))) 11281da177e4SLinus Torvalds goto bad_fork_cleanup_keys; 11291da177e4SLinus Torvalds retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs); 11301da177e4SLinus Torvalds if (retval) 11311651e14eSSerge E. Hallyn goto bad_fork_cleanup_namespaces; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 11341da177e4SLinus Torvalds /* 11351da177e4SLinus Torvalds * Clear TID on mm_release()? 11361da177e4SLinus Torvalds */ 11371da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL; 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; 1144c87e2837SIngo Molnar 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 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds /* Our parent execution domain becomes current domain 11611da177e4SLinus Torvalds These must match for thread signalling to apply */ 11621da177e4SLinus Torvalds p->parent_exec_id = p->self_exec_id; 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds /* ok, now we should be set up.. */ 11651da177e4SLinus Torvalds p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL); 11661da177e4SLinus Torvalds p->pdeath_signal = 0; 11671da177e4SLinus Torvalds p->exit_state = 0; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds /* 11701da177e4SLinus Torvalds * Ok, make it visible to the rest of the system. 11711da177e4SLinus Torvalds * We dont wake it up yet. 11721da177e4SLinus Torvalds */ 11731da177e4SLinus Torvalds p->group_leader = p; 117447e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 11751da177e4SLinus Torvalds INIT_LIST_HEAD(&p->ptrace_children); 11761da177e4SLinus Torvalds INIT_LIST_HEAD(&p->ptrace_list); 11771da177e4SLinus Torvalds 1178476d139cSNick Piggin /* Perform scheduler related setup. Assign this task to a CPU. */ 1179476d139cSNick Piggin sched_fork(p, clone_flags); 1180476d139cSNick Piggin 11811da177e4SLinus Torvalds /* Need tasklist lock for parent etc handling! */ 11821da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 11831da177e4SLinus Torvalds 11845b160f5eSOleg Nesterov /* for sys_ioprio_set(IOPRIO_WHO_PGRP) */ 11855b160f5eSOleg Nesterov p->ioprio = current->ioprio; 11865b160f5eSOleg Nesterov 11871da177e4SLinus Torvalds /* 1188476d139cSNick Piggin * The task hasn't been attached yet, so its cpus_allowed mask will 1189476d139cSNick Piggin * not be changed, nor will its assigned CPU. 1190476d139cSNick Piggin * 1191476d139cSNick Piggin * The cpus_allowed mask of the parent may have changed after it was 1192476d139cSNick Piggin * copied first time - so re-copy it here, then check the child's CPU 1193476d139cSNick Piggin * to ensure it is on a valid CPU (and if not, just force it back to 1194476d139cSNick Piggin * parent's CPU). This avoids alot of nasty races. 11951da177e4SLinus Torvalds */ 11961da177e4SLinus Torvalds p->cpus_allowed = current->cpus_allowed; 119726ff6ad9SSrivatsa Vaddagiri if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) || 119826ff6ad9SSrivatsa Vaddagiri !cpu_online(task_cpu(p)))) 11991da177e4SLinus Torvalds set_task_cpu(p, smp_processor_id()); 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 12021da177e4SLinus Torvalds if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) 12031da177e4SLinus Torvalds p->real_parent = current->real_parent; 12041da177e4SLinus Torvalds else 12051da177e4SLinus Torvalds p->real_parent = current; 12061da177e4SLinus Torvalds p->parent = p->real_parent; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 12094a2c7a78SOleg Nesterov 12104a2c7a78SOleg Nesterov /* 12114a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 12124a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 12134a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 12144a2c7a78SOleg Nesterov * it's process group. 12154a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 12164a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 12174a2c7a78SOleg Nesterov */ 12184a2c7a78SOleg Nesterov recalc_sigpending(); 12194a2c7a78SOleg Nesterov if (signal_pending(current)) { 12204a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 12214a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 12224a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 12231651e14eSSerge E. Hallyn goto bad_fork_cleanup_namespaces; 12244a2c7a78SOleg Nesterov } 12254a2c7a78SOleg Nesterov 12263f17da69SOleg Nesterov if (clone_flags & CLONE_THREAD) { 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 if (!cputime_eq(current->signal->it_virt_expires, 12311da177e4SLinus Torvalds cputime_zero) || 12321da177e4SLinus Torvalds !cputime_eq(current->signal->it_prof_expires, 12331da177e4SLinus Torvalds cputime_zero) || 12341da177e4SLinus Torvalds current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY || 12351da177e4SLinus Torvalds !list_empty(¤t->signal->cpu_timers[0]) || 12361da177e4SLinus Torvalds !list_empty(¤t->signal->cpu_timers[1]) || 12371da177e4SLinus Torvalds !list_empty(¤t->signal->cpu_timers[2])) { 12381da177e4SLinus Torvalds /* 12391da177e4SLinus Torvalds * Have child wake up on its first tick to check 12401da177e4SLinus Torvalds * for process CPU timers. 12411da177e4SLinus Torvalds */ 12421da177e4SLinus Torvalds p->it_prof_expires = jiffies_to_cputime(1); 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 124673b9ebfeSOleg Nesterov if (likely(p->pid)) { 1247c97d9893SOleg Nesterov add_parent(p); 12481da177e4SLinus Torvalds if (unlikely(p->ptrace & PT_PTRACED)) 12491da177e4SLinus Torvalds __ptrace_link(p, current->parent); 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds if (thread_group_leader(p)) { 12529a5d3023SOren Laadan p->signal->tty = current->signal->tty; 12530800d308SSukadev Bhattiprolu p->signal->pgrp = process_group(current); 12541ec320afSCedric Le Goater set_signal_session(p->signal, process_session(current)); 12550800d308SSukadev Bhattiprolu attach_pid(p, PIDTYPE_PGID, task_pgrp(current)); 12560800d308SSukadev Bhattiprolu attach_pid(p, PIDTYPE_SID, task_session(current)); 1257c97d9893SOleg Nesterov 12585e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 12591da177e4SLinus Torvalds __get_cpu_var(process_counts)++; 12601da177e4SLinus Torvalds } 126185868995SSukadev Bhattiprolu attach_pid(p, PIDTYPE_PID, pid); 12621da177e4SLinus Torvalds nr_threads++; 126373b9ebfeSOleg Nesterov } 126473b9ebfeSOleg Nesterov 12651da177e4SLinus Torvalds total_forks++; 12663f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 12671da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 1268c13cf856SAndrew Morton proc_fork_connector(p); 12691da177e4SLinus Torvalds return p; 12701da177e4SLinus Torvalds 1271ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1272444f378bSLinus Torvalds exit_task_namespaces(p); 12731da177e4SLinus Torvalds bad_fork_cleanup_keys: 12741da177e4SLinus Torvalds exit_keys(p); 12751da177e4SLinus Torvalds bad_fork_cleanup_mm: 12761da177e4SLinus Torvalds if (p->mm) 12771da177e4SLinus Torvalds mmput(p->mm); 12781da177e4SLinus Torvalds bad_fork_cleanup_signal: 12796b3934efSOleg Nesterov cleanup_signal(p); 12801da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1281a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 12821da177e4SLinus Torvalds bad_fork_cleanup_fs: 12831da177e4SLinus Torvalds exit_fs(p); /* blocking */ 12841da177e4SLinus Torvalds bad_fork_cleanup_files: 12851da177e4SLinus Torvalds exit_files(p); /* blocking */ 12861da177e4SLinus Torvalds bad_fork_cleanup_semundo: 12871da177e4SLinus Torvalds exit_sem(p); 12881da177e4SLinus Torvalds bad_fork_cleanup_audit: 12891da177e4SLinus Torvalds audit_free(p); 12901da177e4SLinus Torvalds bad_fork_cleanup_security: 12911da177e4SLinus Torvalds security_task_free(p); 12921da177e4SLinus Torvalds bad_fork_cleanup_policy: 12931da177e4SLinus Torvalds #ifdef CONFIG_NUMA 12941da177e4SLinus Torvalds mpol_free(p->mempolicy); 1295b4b26418SPaul Jackson bad_fork_cleanup_cpuset: 12961da177e4SLinus Torvalds #endif 1297b4b26418SPaul Jackson cpuset_exit(p); 129835df17c5SShailabh Nagar bad_fork_cleanup_delays_binfmt: 129935df17c5SShailabh Nagar delayacct_tsk_free(p); 13001da177e4SLinus Torvalds if (p->binfmt) 13011da177e4SLinus Torvalds module_put(p->binfmt->module); 13021da177e4SLinus Torvalds bad_fork_cleanup_put_domain: 1303a1261f54SAl Viro module_put(task_thread_info(p)->exec_domain->module); 13041da177e4SLinus Torvalds bad_fork_cleanup_count: 13051da177e4SLinus Torvalds put_group_info(p->group_info); 13061da177e4SLinus Torvalds atomic_dec(&p->user->processes); 13071da177e4SLinus Torvalds free_uid(p->user); 13081da177e4SLinus Torvalds bad_fork_free: 13091da177e4SLinus Torvalds free_task(p); 1310fe7d37d1SOleg Nesterov fork_out: 1311fe7d37d1SOleg Nesterov return ERR_PTR(retval); 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 1314f95d47caSJeremy Fitzhardinge noinline struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs) 13151da177e4SLinus Torvalds { 13161da177e4SLinus Torvalds memset(regs, 0, sizeof(struct pt_regs)); 13171da177e4SLinus Torvalds return regs; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13209abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu) 13211da177e4SLinus Torvalds { 132236c8b586SIngo Molnar struct task_struct *task; 13231da177e4SLinus Torvalds struct pt_regs regs; 13241da177e4SLinus Torvalds 132585868995SSukadev Bhattiprolu task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, NULL, 132685868995SSukadev Bhattiprolu &init_struct_pid); 1327753ca4f3SAkinobu Mita if (!IS_ERR(task)) 13281da177e4SLinus Torvalds init_idle(task, cpu); 132973b9ebfeSOleg Nesterov 13301da177e4SLinus Torvalds return task; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds static inline int fork_traceflag (unsigned clone_flags) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds if (clone_flags & CLONE_UNTRACED) 13361da177e4SLinus Torvalds return 0; 13371da177e4SLinus Torvalds else if (clone_flags & CLONE_VFORK) { 13381da177e4SLinus Torvalds if (current->ptrace & PT_TRACE_VFORK) 13391da177e4SLinus Torvalds return PTRACE_EVENT_VFORK; 13401da177e4SLinus Torvalds } else if ((clone_flags & CSIGNAL) != SIGCHLD) { 13411da177e4SLinus Torvalds if (current->ptrace & PT_TRACE_CLONE) 13421da177e4SLinus Torvalds return PTRACE_EVENT_CLONE; 13431da177e4SLinus Torvalds } else if (current->ptrace & PT_TRACE_FORK) 13441da177e4SLinus Torvalds return PTRACE_EVENT_FORK; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds return 0; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds /* 13501da177e4SLinus Torvalds * Ok, this is the main fork-routine. 13511da177e4SLinus Torvalds * 13521da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 13531da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 13541da177e4SLinus Torvalds */ 13551da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 13561da177e4SLinus Torvalds unsigned long stack_start, 13571da177e4SLinus Torvalds struct pt_regs *regs, 13581da177e4SLinus Torvalds unsigned long stack_size, 13591da177e4SLinus Torvalds int __user *parent_tidptr, 13601da177e4SLinus Torvalds int __user *child_tidptr) 13611da177e4SLinus Torvalds { 13621da177e4SLinus Torvalds struct task_struct *p; 13631da177e4SLinus Torvalds int trace = 0; 136492476d7fSEric W. Biederman struct pid *pid = alloc_pid(); 136592476d7fSEric W. Biederman long nr; 13661da177e4SLinus Torvalds 136792476d7fSEric W. Biederman if (!pid) 13681da177e4SLinus Torvalds return -EAGAIN; 136992476d7fSEric W. Biederman nr = pid->nr; 13701da177e4SLinus Torvalds if (unlikely(current->ptrace)) { 13711da177e4SLinus Torvalds trace = fork_traceflag (clone_flags); 13721da177e4SLinus Torvalds if (trace) 13731da177e4SLinus Torvalds clone_flags |= CLONE_PTRACE; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 137685868995SSukadev Bhattiprolu p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid); 13771da177e4SLinus Torvalds /* 13781da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 13791da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 13801da177e4SLinus Torvalds */ 13811da177e4SLinus Torvalds if (!IS_ERR(p)) { 13821da177e4SLinus Torvalds struct completion vfork; 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 13851da177e4SLinus Torvalds p->vfork_done = &vfork; 13861da177e4SLinus Torvalds init_completion(&vfork); 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) { 13901da177e4SLinus Torvalds /* 13911da177e4SLinus Torvalds * We'll start up with an immediate SIGSTOP. 13921da177e4SLinus Torvalds */ 13931da177e4SLinus Torvalds sigaddset(&p->pending.signal, SIGSTOP); 13941da177e4SLinus Torvalds set_tsk_thread_flag(p, TIF_SIGPENDING); 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds if (!(clone_flags & CLONE_STOPPED)) 13981da177e4SLinus Torvalds wake_up_new_task(p, clone_flags); 13991da177e4SLinus Torvalds else 14001da177e4SLinus Torvalds p->state = TASK_STOPPED; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds if (unlikely (trace)) { 140392476d7fSEric W. Biederman current->ptrace_message = nr; 14041da177e4SLinus Torvalds ptrace_notify ((trace << 8) | SIGTRAP); 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 14081da177e4SLinus Torvalds wait_for_completion(&vfork); 14099f59ce5dSChuck Ebbert if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) { 14109f59ce5dSChuck Ebbert current->ptrace_message = nr; 14111da177e4SLinus Torvalds ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP); 14121da177e4SLinus Torvalds } 14139f59ce5dSChuck Ebbert } 14141da177e4SLinus Torvalds } else { 141592476d7fSEric W. Biederman free_pid(pid); 141692476d7fSEric W. Biederman nr = PTR_ERR(p); 14171da177e4SLinus Torvalds } 141892476d7fSEric W. Biederman return nr; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14215fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 14225fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 14235fd63b30SRavikiran G Thirumalai #endif 14245fd63b30SRavikiran G Thirumalai 1425*fba2afaaSDavide Libenzi static void sighand_ctor(void *data, struct kmem_cache *cachep, 1426*fba2afaaSDavide Libenzi unsigned long flags) 1427aa1757f9SOleg Nesterov { 1428aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1429aa1757f9SOleg Nesterov 1430*fba2afaaSDavide Libenzi if (flags & SLAB_CTOR_CONSTRUCTOR) { 1431aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1432*fba2afaaSDavide Libenzi INIT_LIST_HEAD(&sighand->signalfd_list); 1433*fba2afaaSDavide Libenzi } 1434aa1757f9SOleg Nesterov } 1435aa1757f9SOleg Nesterov 14361da177e4SLinus Torvalds void __init proc_caches_init(void) 14371da177e4SLinus Torvalds { 14381da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 14391da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 1440aa1757f9SOleg Nesterov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU, 1441aa1757f9SOleg Nesterov sighand_ctor, NULL); 14421da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 14431da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 14441da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 14451da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 14461da177e4SLinus Torvalds sizeof(struct files_struct), 0, 14471da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 14481da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 14491da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 14501da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 14511da177e4SLinus Torvalds vm_area_cachep = kmem_cache_create("vm_area_struct", 14521da177e4SLinus Torvalds sizeof(struct vm_area_struct), 0, 14531da177e4SLinus Torvalds SLAB_PANIC, NULL, NULL); 14541da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 14555fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 14561da177e4SLinus Torvalds SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); 14571da177e4SLinus Torvalds } 1458cf2e340fSJANAK DESAI 1459cf2e340fSJANAK DESAI /* 1460cf2e340fSJANAK DESAI * Check constraints on flags passed to the unshare system call and 1461cf2e340fSJANAK DESAI * force unsharing of additional process context as appropriate. 1462cf2e340fSJANAK DESAI */ 1463cf2e340fSJANAK DESAI static inline void check_unshare_flags(unsigned long *flags_ptr) 1464cf2e340fSJANAK DESAI { 1465cf2e340fSJANAK DESAI /* 1466cf2e340fSJANAK DESAI * If unsharing a thread from a thread group, must also 1467cf2e340fSJANAK DESAI * unshare vm. 1468cf2e340fSJANAK DESAI */ 1469cf2e340fSJANAK DESAI if (*flags_ptr & CLONE_THREAD) 1470cf2e340fSJANAK DESAI *flags_ptr |= CLONE_VM; 1471cf2e340fSJANAK DESAI 1472cf2e340fSJANAK DESAI /* 1473cf2e340fSJANAK DESAI * If unsharing vm, must also unshare signal handlers. 1474cf2e340fSJANAK DESAI */ 1475cf2e340fSJANAK DESAI if (*flags_ptr & CLONE_VM) 1476cf2e340fSJANAK DESAI *flags_ptr |= CLONE_SIGHAND; 1477cf2e340fSJANAK DESAI 1478cf2e340fSJANAK DESAI /* 1479cf2e340fSJANAK DESAI * If unsharing signal handlers and the task was created 1480cf2e340fSJANAK DESAI * using CLONE_THREAD, then must unshare the thread 1481cf2e340fSJANAK DESAI */ 1482cf2e340fSJANAK DESAI if ((*flags_ptr & CLONE_SIGHAND) && 1483cf2e340fSJANAK DESAI (atomic_read(¤t->signal->count) > 1)) 1484cf2e340fSJANAK DESAI *flags_ptr |= CLONE_THREAD; 1485cf2e340fSJANAK DESAI 1486cf2e340fSJANAK DESAI /* 1487cf2e340fSJANAK DESAI * If unsharing namespace, must also unshare filesystem information. 1488cf2e340fSJANAK DESAI */ 1489cf2e340fSJANAK DESAI if (*flags_ptr & CLONE_NEWNS) 1490cf2e340fSJANAK DESAI *flags_ptr |= CLONE_FS; 1491cf2e340fSJANAK DESAI } 1492cf2e340fSJANAK DESAI 1493cf2e340fSJANAK DESAI /* 1494cf2e340fSJANAK DESAI * Unsharing of tasks created with CLONE_THREAD is not supported yet 1495cf2e340fSJANAK DESAI */ 1496cf2e340fSJANAK DESAI static int unshare_thread(unsigned long unshare_flags) 1497cf2e340fSJANAK DESAI { 1498cf2e340fSJANAK DESAI if (unshare_flags & CLONE_THREAD) 1499cf2e340fSJANAK DESAI return -EINVAL; 1500cf2e340fSJANAK DESAI 1501cf2e340fSJANAK DESAI return 0; 1502cf2e340fSJANAK DESAI } 1503cf2e340fSJANAK DESAI 1504cf2e340fSJANAK DESAI /* 150599d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1506cf2e340fSJANAK DESAI */ 1507cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1508cf2e340fSJANAK DESAI { 1509cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1510cf2e340fSJANAK DESAI 1511cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FS) && 151299d1419dSJANAK DESAI (fs && atomic_read(&fs->count) > 1)) { 151399d1419dSJANAK DESAI *new_fsp = __copy_fs_struct(current->fs); 151499d1419dSJANAK DESAI if (!*new_fsp) 151599d1419dSJANAK DESAI return -ENOMEM; 151699d1419dSJANAK DESAI } 1517cf2e340fSJANAK DESAI 1518cf2e340fSJANAK DESAI return 0; 1519cf2e340fSJANAK DESAI } 1520cf2e340fSJANAK DESAI 1521cf2e340fSJANAK DESAI /* 1522dae3c5a0SOleg Nesterov * Unsharing of sighand is not supported yet 1523cf2e340fSJANAK DESAI */ 1524cf2e340fSJANAK DESAI static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp) 1525cf2e340fSJANAK DESAI { 1526cf2e340fSJANAK DESAI struct sighand_struct *sigh = current->sighand; 1527cf2e340fSJANAK DESAI 1528dae3c5a0SOleg Nesterov if ((unshare_flags & CLONE_SIGHAND) && atomic_read(&sigh->count) > 1) 1529cf2e340fSJANAK DESAI return -EINVAL; 1530cf2e340fSJANAK DESAI else 1531cf2e340fSJANAK DESAI return 0; 1532cf2e340fSJANAK DESAI } 1533cf2e340fSJANAK DESAI 1534cf2e340fSJANAK DESAI /* 1535a0a7ec30SJANAK DESAI * Unshare vm if it is being shared 1536cf2e340fSJANAK DESAI */ 1537cf2e340fSJANAK DESAI static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp) 1538cf2e340fSJANAK DESAI { 1539cf2e340fSJANAK DESAI struct mm_struct *mm = current->mm; 1540cf2e340fSJANAK DESAI 1541cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_VM) && 1542a0a7ec30SJANAK DESAI (mm && atomic_read(&mm->mm_users) > 1)) { 15432d61b867SOleg Nesterov return -EINVAL; 1544a0a7ec30SJANAK DESAI } 1545cf2e340fSJANAK DESAI 1546cf2e340fSJANAK DESAI return 0; 1547cf2e340fSJANAK DESAI } 1548cf2e340fSJANAK DESAI 1549cf2e340fSJANAK DESAI /* 1550a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1551cf2e340fSJANAK DESAI */ 1552cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1553cf2e340fSJANAK DESAI { 1554cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1555a016f338SJANAK DESAI int error = 0; 1556cf2e340fSJANAK DESAI 1557cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1558a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1559a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1560a016f338SJANAK DESAI if (!*new_fdp) 1561a016f338SJANAK DESAI return error; 1562a016f338SJANAK DESAI } 1563cf2e340fSJANAK DESAI 1564cf2e340fSJANAK DESAI return 0; 1565cf2e340fSJANAK DESAI } 1566cf2e340fSJANAK DESAI 1567cf2e340fSJANAK DESAI /* 1568cf2e340fSJANAK DESAI * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not 1569cf2e340fSJANAK DESAI * supported yet 1570cf2e340fSJANAK DESAI */ 1571cf2e340fSJANAK DESAI static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp) 1572cf2e340fSJANAK DESAI { 1573cf2e340fSJANAK DESAI if (unshare_flags & CLONE_SYSVSEM) 1574cf2e340fSJANAK DESAI return -EINVAL; 1575cf2e340fSJANAK DESAI 1576cf2e340fSJANAK DESAI return 0; 1577cf2e340fSJANAK DESAI } 1578cf2e340fSJANAK DESAI 1579cf2e340fSJANAK DESAI /* 1580cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1581cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1582cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1583cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1584cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1585cf2e340fSJANAK DESAI * task_struct. 1586cf2e340fSJANAK DESAI */ 1587cf2e340fSJANAK DESAI asmlinkage long sys_unshare(unsigned long unshare_flags) 1588cf2e340fSJANAK DESAI { 1589cf2e340fSJANAK DESAI int err = 0; 1590cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1591dae3c5a0SOleg Nesterov struct sighand_struct *new_sigh = NULL; 1592cf2e340fSJANAK DESAI struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL; 1593cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1594cf2e340fSJANAK DESAI struct sem_undo_list *new_ulist = NULL; 1595c0b2fc31SSerge Hallyn struct nsproxy *new_nsproxy = NULL, *old_nsproxy = NULL; 1596cf2e340fSJANAK DESAI 1597cf2e340fSJANAK DESAI check_unshare_flags(&unshare_flags); 1598cf2e340fSJANAK DESAI 159906f9d4f9SEric W. Biederman /* Return -EINVAL for all unsupported flags */ 160006f9d4f9SEric W. Biederman err = -EINVAL; 160106f9d4f9SEric W. Biederman if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 160225b21cb2SKirill Korotaev CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 160325b21cb2SKirill Korotaev CLONE_NEWUTS|CLONE_NEWIPC)) 160406f9d4f9SEric W. Biederman goto bad_unshare_out; 160506f9d4f9SEric W. Biederman 1606cf2e340fSJANAK DESAI if ((err = unshare_thread(unshare_flags))) 1607cf2e340fSJANAK DESAI goto bad_unshare_out; 1608cf2e340fSJANAK DESAI if ((err = unshare_fs(unshare_flags, &new_fs))) 1609cf2e340fSJANAK DESAI goto bad_unshare_cleanup_thread; 1610cf2e340fSJANAK DESAI if ((err = unshare_sighand(unshare_flags, &new_sigh))) 1611e3222c4eSBadari Pulavarty goto bad_unshare_cleanup_fs; 1612cf2e340fSJANAK DESAI if ((err = unshare_vm(unshare_flags, &new_mm))) 1613cf2e340fSJANAK DESAI goto bad_unshare_cleanup_sigh; 1614cf2e340fSJANAK DESAI if ((err = unshare_fd(unshare_flags, &new_fd))) 1615cf2e340fSJANAK DESAI goto bad_unshare_cleanup_vm; 1616cf2e340fSJANAK DESAI if ((err = unshare_semundo(unshare_flags, &new_ulist))) 1617cf2e340fSJANAK DESAI goto bad_unshare_cleanup_fd; 1618e3222c4eSBadari Pulavarty if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 1619e3222c4eSBadari Pulavarty new_fs))) 1620071df104SSerge E. Hallyn goto bad_unshare_cleanup_semundo; 1621cf2e340fSJANAK DESAI 1622e3222c4eSBadari Pulavarty if (new_fs || new_mm || new_fd || new_ulist || new_nsproxy) { 1623ab516013SSerge E. Hallyn 1624cf2e340fSJANAK DESAI task_lock(current); 1625c0b2fc31SSerge Hallyn 1626c0b2fc31SSerge Hallyn if (new_nsproxy) { 1627e3222c4eSBadari Pulavarty old_nsproxy = current->nsproxy; 1628ab516013SSerge E. Hallyn current->nsproxy = new_nsproxy; 1629c0b2fc31SSerge Hallyn new_nsproxy = old_nsproxy; 1630c0b2fc31SSerge Hallyn } 1631cf2e340fSJANAK DESAI 1632cf2e340fSJANAK DESAI if (new_fs) { 1633cf2e340fSJANAK DESAI fs = current->fs; 1634cf2e340fSJANAK DESAI current->fs = new_fs; 1635cf2e340fSJANAK DESAI new_fs = fs; 1636cf2e340fSJANAK DESAI } 1637cf2e340fSJANAK DESAI 1638cf2e340fSJANAK DESAI if (new_mm) { 1639cf2e340fSJANAK DESAI mm = current->mm; 1640cf2e340fSJANAK DESAI active_mm = current->active_mm; 1641cf2e340fSJANAK DESAI current->mm = new_mm; 1642cf2e340fSJANAK DESAI current->active_mm = new_mm; 1643cf2e340fSJANAK DESAI activate_mm(active_mm, new_mm); 1644cf2e340fSJANAK DESAI new_mm = mm; 1645cf2e340fSJANAK DESAI } 1646cf2e340fSJANAK DESAI 1647cf2e340fSJANAK DESAI if (new_fd) { 1648cf2e340fSJANAK DESAI fd = current->files; 1649cf2e340fSJANAK DESAI current->files = new_fd; 1650cf2e340fSJANAK DESAI new_fd = fd; 1651cf2e340fSJANAK DESAI } 1652cf2e340fSJANAK DESAI 1653cf2e340fSJANAK DESAI task_unlock(current); 1654cf2e340fSJANAK DESAI } 1655cf2e340fSJANAK DESAI 1656c0b2fc31SSerge Hallyn if (new_nsproxy) 1657444f378bSLinus Torvalds put_nsproxy(new_nsproxy); 1658c0b2fc31SSerge Hallyn 1659ab516013SSerge E. Hallyn bad_unshare_cleanup_semundo: 1660cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 1661cf2e340fSJANAK DESAI if (new_fd) 1662cf2e340fSJANAK DESAI put_files_struct(new_fd); 1663cf2e340fSJANAK DESAI 1664cf2e340fSJANAK DESAI bad_unshare_cleanup_vm: 1665cf2e340fSJANAK DESAI if (new_mm) 1666cf2e340fSJANAK DESAI mmput(new_mm); 1667cf2e340fSJANAK DESAI 1668cf2e340fSJANAK DESAI bad_unshare_cleanup_sigh: 1669cf2e340fSJANAK DESAI if (new_sigh) 1670cf2e340fSJANAK DESAI if (atomic_dec_and_test(&new_sigh->count)) 1671cf2e340fSJANAK DESAI kmem_cache_free(sighand_cachep, new_sigh); 1672cf2e340fSJANAK DESAI 1673cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 1674cf2e340fSJANAK DESAI if (new_fs) 1675cf2e340fSJANAK DESAI put_fs_struct(new_fs); 1676cf2e340fSJANAK DESAI 1677cf2e340fSJANAK DESAI bad_unshare_cleanup_thread: 1678cf2e340fSJANAK DESAI bad_unshare_out: 1679cf2e340fSJANAK DESAI return err; 1680cf2e340fSJANAK DESAI } 1681