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> 401da177e4SLinus Torvalds #include <linux/futex.h> 418141c7f3SLinus Torvalds #include <linux/compat.h> 42207205a2SEric Dumazet #include <linux/kthread.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> 52f8af4da3SHugh Dickins #include <linux/ksm.h> 531da177e4SLinus Torvalds #include <linux/acct.h> 548f0ab514SJay Lan #include <linux/tsacct_kern.h> 559f46080cSMatt Helsley #include <linux/cn_proc.h> 56ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 57ca74e92bSShailabh Nagar #include <linux/delayacct.h> 58ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 590a425405SArjan van de Ven #include <linux/random.h> 60522ed776SMiloslav Trmac #include <linux/tty.h> 61fd0928dfSJens Axboe #include <linux/blkdev.h> 625ad4e53bSAl Viro #include <linux/fs_struct.h> 637c9f8861SEric Sandeen #include <linux/magic.h> 64cdd6c482SIngo Molnar #include <linux/perf_event.h> 6542c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 668e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 673d5992d2SYing Han #include <linux/oom.h> 68ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 69d80e731eSOleg Nesterov #include <linux/signalfd.h> 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds #include <asm/pgtable.h> 721da177e4SLinus Torvalds #include <asm/pgalloc.h> 731da177e4SLinus Torvalds #include <asm/uaccess.h> 741da177e4SLinus Torvalds #include <asm/mmu_context.h> 751da177e4SLinus Torvalds #include <asm/cacheflush.h> 761da177e4SLinus Torvalds #include <asm/tlbflush.h> 771da177e4SLinus Torvalds 78ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 79ad8d75ffSSteven Rostedt 8043d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 8143d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 8243d2b113SKAMEZAWA Hiroyuki 831da177e4SLinus Torvalds /* 841da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 871da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 94db1466b3SPaul E. McKenney 95db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 96db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 97db1466b3SPaul E. McKenney { 98db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 99db1466b3SPaul E. McKenney } 100db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 101db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds int nr_processes(void) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds int cpu; 1061da177e4SLinus Torvalds int total = 0; 1071da177e4SLinus Torvalds 1081d510750SIan Campbell for_each_possible_cpu(cpu) 1091da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds return total; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 115e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 11641101809SThomas Gleixner 11741101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 11841101809SThomas Gleixner { 11941101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 12041101809SThomas Gleixner } 12141101809SThomas Gleixner 12241101809SThomas Gleixner void __weak arch_release_task_struct(struct task_struct *tsk) { } 12341101809SThomas Gleixner 12441101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 12541101809SThomas Gleixner { 12641101809SThomas Gleixner arch_release_task_struct(tsk); 12741101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 12841101809SThomas Gleixner } 1291da177e4SLinus Torvalds #endif 1301da177e4SLinus Torvalds 131b69c49b7SFUJITA Tomonori #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR 13241101809SThomas Gleixner 13341101809SThomas Gleixner void __weak arch_release_thread_info(struct thread_info *ti) { } 13441101809SThomas Gleixner 135*0d15d74aSThomas Gleixner /* 136*0d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 137*0d15d74aSThomas Gleixner * kmemcache based allocator. 138*0d15d74aSThomas Gleixner */ 139*0d15d74aSThomas Gleixner # if THREAD_SIZE >= PAGE_SIZE 140b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 141b6a84016SEric Dumazet int node) 142b69c49b7SFUJITA Tomonori { 1432889f608SThomas Gleixner struct page *page = alloc_pages_node(node, THREADINFO_GFP, 1442889f608SThomas Gleixner THREAD_SIZE_ORDER); 145b6a84016SEric Dumazet 146b6a84016SEric Dumazet return page ? page_address(page) : NULL; 147b69c49b7SFUJITA Tomonori } 148b69c49b7SFUJITA Tomonori 149b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti) 150b69c49b7SFUJITA Tomonori { 15141101809SThomas Gleixner arch_release_thread_info(ti); 152b69c49b7SFUJITA Tomonori free_pages((unsigned long)ti, THREAD_SIZE_ORDER); 153b69c49b7SFUJITA Tomonori } 154*0d15d74aSThomas Gleixner # else 155*0d15d74aSThomas Gleixner static struct kmem_cache *thread_info_cache; 156*0d15d74aSThomas Gleixner 157*0d15d74aSThomas Gleixner static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 158*0d15d74aSThomas Gleixner int node) 159*0d15d74aSThomas Gleixner { 160*0d15d74aSThomas Gleixner return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node); 161*0d15d74aSThomas Gleixner } 162*0d15d74aSThomas Gleixner 163*0d15d74aSThomas Gleixner static void free_thread_info(struct thread_info *ti) 164*0d15d74aSThomas Gleixner { 165*0d15d74aSThomas Gleixner arch_release_thread_info(ti); 166*0d15d74aSThomas Gleixner kmem_cache_free(thread_info_cache, ti); 167*0d15d74aSThomas Gleixner } 168*0d15d74aSThomas Gleixner 169*0d15d74aSThomas Gleixner void thread_info_cache_init(void) 170*0d15d74aSThomas Gleixner { 171*0d15d74aSThomas Gleixner thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE, 172*0d15d74aSThomas Gleixner THREAD_SIZE, 0, NULL); 173*0d15d74aSThomas Gleixner BUG_ON(thread_info_cache == NULL); 174*0d15d74aSThomas Gleixner } 175*0d15d74aSThomas Gleixner # endif 176b69c49b7SFUJITA Tomonori #endif 177b69c49b7SFUJITA Tomonori 1781da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 179e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 182e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 185e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 188e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 191e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 194e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 1951da177e4SLinus Torvalds 196c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account) 197c6a7f572SKOSAKI Motohiro { 198c6a7f572SKOSAKI Motohiro struct zone *zone = page_zone(virt_to_page(ti)); 199c6a7f572SKOSAKI Motohiro 200c6a7f572SKOSAKI Motohiro mod_zone_page_state(zone, NR_KERNEL_STACK, account); 201c6a7f572SKOSAKI Motohiro } 202c6a7f572SKOSAKI Motohiro 2031da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 2041da177e4SLinus Torvalds { 205c6a7f572SKOSAKI Motohiro account_kernel_stack(tsk->stack, -1); 206f7e4217bSRoman Zippel free_thread_info(tsk->stack); 20723f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 208fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 2091da177e4SLinus Torvalds free_task_struct(tsk); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 2121da177e4SLinus Torvalds 213ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig) 214ea6d290cSOleg Nesterov { 21597101eb4SOleg Nesterov taskstats_tgid_free(sig); 2161c5354deSMike Galbraith sched_autogroup_exit(sig); 217ea6d290cSOleg Nesterov kmem_cache_free(signal_cachep, sig); 218ea6d290cSOleg Nesterov } 219ea6d290cSOleg Nesterov 220ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig) 221ea6d290cSOleg Nesterov { 2221c5354deSMike Galbraith if (atomic_dec_and_test(&sig->sigcnt)) 223ea6d290cSOleg Nesterov free_signal_struct(sig); 224ea6d290cSOleg Nesterov } 225ea6d290cSOleg Nesterov 226158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 2271da177e4SLinus Torvalds { 228270f722dSEugene Teo WARN_ON(!tsk->exit_state); 2291da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 2301da177e4SLinus Torvalds WARN_ON(tsk == current); 2311da177e4SLinus Torvalds 2321a2a4d06SKees Cook security_task_free(tsk); 233e0e81739SDavid Howells exit_creds(tsk); 23435df17c5SShailabh Nagar delayacct_tsk_free(tsk); 235ea6d290cSOleg Nesterov put_signal_struct(tsk->signal); 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 2381da177e4SLinus Torvalds free_task(tsk); 2391da177e4SLinus Torvalds } 24077c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct); 2411da177e4SLinus Torvalds 2426c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { } 24361c4628bSSuresh Siddha 2441da177e4SLinus Torvalds void __init fork_init(unsigned long mempages) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR 2471da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 2481da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 2491da177e4SLinus Torvalds #endif 2501da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 2511da177e4SLinus Torvalds task_struct_cachep = 2521da177e4SLinus Torvalds kmem_cache_create("task_struct", sizeof(struct task_struct), 2532dff4405SVegard Nossum ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL); 2541da177e4SLinus Torvalds #endif 2551da177e4SLinus Torvalds 25661c4628bSSuresh Siddha /* do the arch specific task caches init */ 25761c4628bSSuresh Siddha arch_task_cache_init(); 25861c4628bSSuresh Siddha 2591da177e4SLinus Torvalds /* 2601da177e4SLinus Torvalds * The default maximum number of threads is set to a safe 2611da177e4SLinus Torvalds * value: the thread structures can take up at most half 2621da177e4SLinus Torvalds * of memory. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /* 2671da177e4SLinus Torvalds * we need to allow at least 20 threads to boot a system 2681da177e4SLinus Torvalds */ 2691da177e4SLinus Torvalds if (max_threads < 20) 2701da177e4SLinus Torvalds max_threads = 20; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 2731da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 2741da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 2751da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 27861c4628bSSuresh Siddha int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst, 27961c4628bSSuresh Siddha struct task_struct *src) 28061c4628bSSuresh Siddha { 28161c4628bSSuresh Siddha *dst = *src; 28261c4628bSSuresh Siddha return 0; 28361c4628bSSuresh Siddha } 28461c4628bSSuresh Siddha 2851da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds struct task_struct *tsk; 2881da177e4SLinus Torvalds struct thread_info *ti; 2897c9f8861SEric Sandeen unsigned long *stackend; 290207205a2SEric Dumazet int node = tsk_fork_get_node(orig); 2913e26c149SPeter Zijlstra int err; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds prepare_to_copy(orig); 2941da177e4SLinus Torvalds 295504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 2961da177e4SLinus Torvalds if (!tsk) 2971da177e4SLinus Torvalds return NULL; 2981da177e4SLinus Torvalds 299b6a84016SEric Dumazet ti = alloc_thread_info_node(tsk, node); 3001da177e4SLinus Torvalds if (!ti) { 3011da177e4SLinus Torvalds free_task_struct(tsk); 3021da177e4SLinus Torvalds return NULL; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 30561c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 30661c4628bSSuresh Siddha if (err) 30761c4628bSSuresh Siddha goto out; 30861c4628bSSuresh Siddha 309f7e4217bSRoman Zippel tsk->stack = ti; 3103e26c149SPeter Zijlstra 31110ebffdeSAl Viro setup_thread_stack(tsk, orig); 3128e7cac79SAvi Kivity clear_user_return_notifier(tsk); 313f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 3147c9f8861SEric Sandeen stackend = end_of_stack(tsk); 3157c9f8861SEric Sandeen *stackend = STACK_END_MAGIC; /* for overflow detection */ 3161da177e4SLinus Torvalds 3170a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 3180a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 3190a425405SArjan van de Ven #endif 3200a425405SArjan van de Ven 321fb0a685cSDaniel Rebelo de Oliveira /* 322fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 323fb0a685cSDaniel Rebelo de Oliveira * parent) 324fb0a685cSDaniel Rebelo de Oliveira */ 3251da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 3266c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 3272056a782SJens Axboe tsk->btrace_seq = 0; 3286c5c9341SAlexey Dobriyan #endif 329a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 330c6a7f572SKOSAKI Motohiro 331c6a7f572SKOSAKI Motohiro account_kernel_stack(ti, 1); 332c6a7f572SKOSAKI Motohiro 3331da177e4SLinus Torvalds return tsk; 33461c4628bSSuresh Siddha 33561c4628bSSuresh Siddha out: 33661c4628bSSuresh Siddha free_thread_info(ti); 33761c4628bSSuresh Siddha free_task_struct(tsk); 33861c4628bSSuresh Siddha return NULL; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds #ifdef CONFIG_MMU 342a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 3431da177e4SLinus Torvalds { 344297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 3451da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 3461da177e4SLinus Torvalds int retval; 3471da177e4SLinus Torvalds unsigned long charge; 3481da177e4SLinus Torvalds struct mempolicy *pol; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 351ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 352ad339451SIngo Molnar /* 353ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 354ad339451SIngo Molnar */ 355ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 3567ee78232SHugh Dickins 3571da177e4SLinus Torvalds mm->locked_vm = 0; 3581da177e4SLinus Torvalds mm->mmap = NULL; 3591da177e4SLinus Torvalds mm->mmap_cache = NULL; 3601da177e4SLinus Torvalds mm->free_area_cache = oldmm->mmap_base; 3611363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 3621da177e4SLinus Torvalds mm->map_count = 0; 36394894244SRusty Russell cpumask_clear(mm_cpumask(mm)); 3641da177e4SLinus Torvalds mm->mm_rb = RB_ROOT; 3651da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 3661da177e4SLinus Torvalds rb_parent = NULL; 3671da177e4SLinus Torvalds pprev = &mm->mmap; 368f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 369f8af4da3SHugh Dickins if (retval) 370f8af4da3SHugh Dickins goto out; 371ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 372ba76149fSAndrea Arcangeli if (retval) 373ba76149fSAndrea Arcangeli goto out; 3741da177e4SLinus Torvalds 375297c5eeeSLinus Torvalds prev = NULL; 376fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 3771da177e4SLinus Torvalds struct file *file; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 3803b6bfcdbSHugh Dickins long pages = vma_pages(mpnt); 3813b6bfcdbSHugh Dickins mm->total_vm -= pages; 382ab50b8edSHugh Dickins vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 3833b6bfcdbSHugh Dickins -pages); 3841da177e4SLinus Torvalds continue; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds charge = 0; 3871da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 3881da177e4SLinus Torvalds unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT; 389191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 3901da177e4SLinus Torvalds goto fail_nomem; 3911da177e4SLinus Torvalds charge = len; 3921da177e4SLinus Torvalds } 393e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 3941da177e4SLinus Torvalds if (!tmp) 3951da177e4SLinus Torvalds goto fail_nomem; 3961da177e4SLinus Torvalds *tmp = *mpnt; 3975beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 398846a16bfSLee Schermerhorn pol = mpol_dup(vma_policy(mpnt)); 3991da177e4SLinus Torvalds retval = PTR_ERR(pol); 4001da177e4SLinus Torvalds if (IS_ERR(pol)) 4011da177e4SLinus Torvalds goto fail_nomem_policy; 4021da177e4SLinus Torvalds vma_set_policy(tmp, pol); 403a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 4045beb4930SRik van Riel if (anon_vma_fork(tmp, mpnt)) 4055beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 4061da177e4SLinus Torvalds tmp->vm_flags &= ~VM_LOCKED; 407297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 4081da177e4SLinus Torvalds file = tmp->vm_file; 4091da177e4SLinus Torvalds if (file) { 410f3a43f3fSJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 411b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 412b88ed205SHugh Dickins 4131da177e4SLinus Torvalds get_file(file); 4141da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 4151da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 4163d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 417b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 418b88ed205SHugh Dickins mapping->i_mmap_writable++; 419b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 420b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 4211da177e4SLinus Torvalds vma_prio_tree_add(tmp, mpnt); 422b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 4233d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* 427a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 428a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 429a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 430a1e78772SMel Gorman */ 431a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 432a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 433a1e78772SMel Gorman 434a1e78772SMel Gorman /* 4357ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 4361da177e4SLinus Torvalds */ 4371da177e4SLinus Torvalds *pprev = tmp; 4381da177e4SLinus Torvalds pprev = &tmp->vm_next; 439297c5eeeSLinus Torvalds tmp->vm_prev = prev; 440297c5eeeSLinus Torvalds prev = tmp; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 4431da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 4441da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds mm->map_count++; 4470b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 4501da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds if (retval) 4531da177e4SLinus Torvalds goto out; 4541da177e4SLinus Torvalds } 455d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 456d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 4571da177e4SLinus Torvalds retval = 0; 4581da177e4SLinus Torvalds out: 4597ee78232SHugh Dickins up_write(&mm->mmap_sem); 460fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 4611da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 4621da177e4SLinus Torvalds return retval; 4635beb4930SRik van Riel fail_nomem_anon_vma_fork: 4645beb4930SRik van Riel mpol_put(pol); 4651da177e4SLinus Torvalds fail_nomem_policy: 4661da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 4671da177e4SLinus Torvalds fail_nomem: 4681da177e4SLinus Torvalds retval = -ENOMEM; 4691da177e4SLinus Torvalds vm_unacct_memory(charge); 4701da177e4SLinus Torvalds goto out; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 4761da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 4771da177e4SLinus Torvalds return -ENOMEM; 4781da177e4SLinus Torvalds return 0; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 4821da177e4SLinus Torvalds { 4835e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds #else 4861da177e4SLinus Torvalds #define dup_mmap(mm, oldmm) (0) 4871da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 4881da177e4SLinus Torvalds #define mm_free_pgd(mm) 4891da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 4921da177e4SLinus Torvalds 493e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 4941da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 4951da177e4SLinus Torvalds 4964cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 4974cb0e11bSHidehiro Kawai 4984cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 4994cb0e11bSHidehiro Kawai { 5004cb0e11bSHidehiro Kawai default_dump_filter = 5014cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 5024cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 5034cb0e11bSHidehiro Kawai return 1; 5044cb0e11bSHidehiro Kawai } 5054cb0e11bSHidehiro Kawai 5064cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 5074cb0e11bSHidehiro Kawai 5081da177e4SLinus Torvalds #include <linux/init_task.h> 5091da177e4SLinus Torvalds 510858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 511858f0993SAlexey Dobriyan { 512858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 513858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 514858f0993SAlexey Dobriyan INIT_HLIST_HEAD(&mm->ioctx_list); 515858f0993SAlexey Dobriyan #endif 516858f0993SAlexey Dobriyan } 517858f0993SAlexey Dobriyan 51878fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 5191da177e4SLinus Torvalds { 5201da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 5211da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 5221da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 5231da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 524f8af4da3SHugh Dickins mm->flags = (current->mm) ? 525f8af4da3SHugh Dickins (current->mm->flags & MMF_INIT_MASK) : default_dump_filter; 526999d9fc1SOleg Nesterov mm->core_state = NULL; 5271da177e4SLinus Torvalds mm->nr_ptes = 0; 528d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 5291da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 5301da177e4SLinus Torvalds mm->free_area_cache = TASK_UNMAPPED_BASE; 5311363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 532858f0993SAlexey Dobriyan mm_init_aio(mm); 533cf475ad2SBalbir Singh mm_init_owner(mm, p); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (likely(!mm_alloc_pgd(mm))) { 5361da177e4SLinus Torvalds mm->def_flags = 0; 537cddb8a5cSAndrea Arcangeli mmu_notifier_mm_init(mm); 5381da177e4SLinus Torvalds return mm; 5391da177e4SLinus Torvalds } 54078fb7466SPavel Emelianov 5411da177e4SLinus Torvalds free_mm(mm); 5421da177e4SLinus Torvalds return NULL; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 545c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm) 546c3f0327fSKonstantin Khlebnikov { 547c3f0327fSKonstantin Khlebnikov int i; 548c3f0327fSKonstantin Khlebnikov 549c3f0327fSKonstantin Khlebnikov for (i = 0; i < NR_MM_COUNTERS; i++) { 550c3f0327fSKonstantin Khlebnikov long x = atomic_long_read(&mm->rss_stat.count[i]); 551c3f0327fSKonstantin Khlebnikov 552c3f0327fSKonstantin Khlebnikov if (unlikely(x)) 553c3f0327fSKonstantin Khlebnikov printk(KERN_ALERT "BUG: Bad rss-counter state " 554c3f0327fSKonstantin Khlebnikov "mm:%p idx:%d val:%ld\n", mm, i, x); 555c3f0327fSKonstantin Khlebnikov } 556c3f0327fSKonstantin Khlebnikov 557c3f0327fSKonstantin Khlebnikov #ifdef CONFIG_TRANSPARENT_HUGEPAGE 558c3f0327fSKonstantin Khlebnikov VM_BUG_ON(mm->pmd_huge_pte); 559c3f0327fSKonstantin Khlebnikov #endif 560c3f0327fSKonstantin Khlebnikov } 561c3f0327fSKonstantin Khlebnikov 5621da177e4SLinus Torvalds /* 5631da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 5641da177e4SLinus Torvalds */ 5651da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 5661da177e4SLinus Torvalds { 5671da177e4SLinus Torvalds struct mm_struct *mm; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds mm = allocate_mm(); 570de03c72cSKOSAKI Motohiro if (!mm) 571de03c72cSKOSAKI Motohiro return NULL; 572de03c72cSKOSAKI Motohiro 5731da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 5746345d24dSLinus Torvalds mm_init_cpumask(mm); 5756345d24dSLinus Torvalds return mm_init(mm, current); 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds /* 5791da177e4SLinus Torvalds * Called when the last reference to the mm 5801da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 5811da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 5821da177e4SLinus Torvalds */ 5837ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 5841da177e4SLinus Torvalds { 5851da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 5861da177e4SLinus Torvalds mm_free_pgd(mm); 5871da177e4SLinus Torvalds destroy_context(mm); 588cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 589c3f0327fSKonstantin Khlebnikov check_mm(mm); 5901da177e4SLinus Torvalds free_mm(mm); 5911da177e4SLinus Torvalds } 5926d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds /* 5951da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 5961da177e4SLinus Torvalds */ 5971da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 5981da177e4SLinus Torvalds { 5990ae26f1bSAndrew Morton might_sleep(); 6000ae26f1bSAndrew Morton 6011da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 6021da177e4SLinus Torvalds exit_aio(mm); 6031c2fb7a4SAndrea Arcangeli ksm_exit(mm); 604ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 6051da177e4SLinus Torvalds exit_mmap(mm); 606925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 6071da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 6081da177e4SLinus Torvalds spin_lock(&mmlist_lock); 6091da177e4SLinus Torvalds list_del(&mm->mmlist); 6101da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds put_swap_token(mm); 613801460d0SHiroshi Shimamoto if (mm->binfmt) 614801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 6151da177e4SLinus Torvalds mmdrop(mm); 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 6191da177e4SLinus Torvalds 62038646013SJiri Slaby /* 62138646013SJiri Slaby * We added or removed a vma mapping the executable. The vmas are only mapped 62238646013SJiri Slaby * during exec and are not mapped with the mmap system call. 62338646013SJiri Slaby * Callers must hold down_write() on the mm's mmap_sem for these 62438646013SJiri Slaby */ 62538646013SJiri Slaby void added_exe_file_vma(struct mm_struct *mm) 62638646013SJiri Slaby { 62738646013SJiri Slaby mm->num_exe_file_vmas++; 62838646013SJiri Slaby } 62938646013SJiri Slaby 63038646013SJiri Slaby void removed_exe_file_vma(struct mm_struct *mm) 63138646013SJiri Slaby { 63238646013SJiri Slaby mm->num_exe_file_vmas--; 63338646013SJiri Slaby if ((mm->num_exe_file_vmas == 0) && mm->exe_file) { 63438646013SJiri Slaby fput(mm->exe_file); 63538646013SJiri Slaby mm->exe_file = NULL; 63638646013SJiri Slaby } 63738646013SJiri Slaby 63838646013SJiri Slaby } 63938646013SJiri Slaby 64038646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 64138646013SJiri Slaby { 64238646013SJiri Slaby if (new_exe_file) 64338646013SJiri Slaby get_file(new_exe_file); 64438646013SJiri Slaby if (mm->exe_file) 64538646013SJiri Slaby fput(mm->exe_file); 64638646013SJiri Slaby mm->exe_file = new_exe_file; 64738646013SJiri Slaby mm->num_exe_file_vmas = 0; 64838646013SJiri Slaby } 64938646013SJiri Slaby 65038646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 65138646013SJiri Slaby { 65238646013SJiri Slaby struct file *exe_file; 65338646013SJiri Slaby 65438646013SJiri Slaby /* We need mmap_sem to protect against races with removal of 65538646013SJiri Slaby * VM_EXECUTABLE vmas */ 65638646013SJiri Slaby down_read(&mm->mmap_sem); 65738646013SJiri Slaby exe_file = mm->exe_file; 65838646013SJiri Slaby if (exe_file) 65938646013SJiri Slaby get_file(exe_file); 66038646013SJiri Slaby up_read(&mm->mmap_sem); 66138646013SJiri Slaby return exe_file; 66238646013SJiri Slaby } 66338646013SJiri Slaby 66438646013SJiri Slaby static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 66538646013SJiri Slaby { 66638646013SJiri Slaby /* It's safe to write the exe_file pointer without exe_file_lock because 66738646013SJiri Slaby * this is called during fork when the task is not yet in /proc */ 66838646013SJiri Slaby newmm->exe_file = get_mm_exe_file(oldmm); 66938646013SJiri Slaby } 67038646013SJiri Slaby 6711da177e4SLinus Torvalds /** 6721da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 6731da177e4SLinus Torvalds * 674246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 6751da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 6761da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 6771da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 6781da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 6791da177e4SLinus Torvalds */ 6801da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 6811da177e4SLinus Torvalds { 6821da177e4SLinus Torvalds struct mm_struct *mm; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds task_lock(task); 6851da177e4SLinus Torvalds mm = task->mm; 6861da177e4SLinus Torvalds if (mm) { 687246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 6881da177e4SLinus Torvalds mm = NULL; 6891da177e4SLinus Torvalds else 6901da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds task_unlock(task); 6931da177e4SLinus Torvalds return mm; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 6961da177e4SLinus Torvalds 6978cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 6988cdb878dSChristopher Yeoh { 6998cdb878dSChristopher Yeoh struct mm_struct *mm; 7008cdb878dSChristopher Yeoh int err; 7018cdb878dSChristopher Yeoh 7028cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 7038cdb878dSChristopher Yeoh if (err) 7048cdb878dSChristopher Yeoh return ERR_PTR(err); 7058cdb878dSChristopher Yeoh 7068cdb878dSChristopher Yeoh mm = get_task_mm(task); 7078cdb878dSChristopher Yeoh if (mm && mm != current->mm && 7088cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 7098cdb878dSChristopher Yeoh mmput(mm); 7108cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 7118cdb878dSChristopher Yeoh } 7128cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 7138cdb878dSChristopher Yeoh 7148cdb878dSChristopher Yeoh return mm; 7158cdb878dSChristopher Yeoh } 7168cdb878dSChristopher Yeoh 71757b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 718c415c3b4SOleg Nesterov { 719d68b46feSOleg Nesterov struct completion *vfork; 720c415c3b4SOleg Nesterov 721d68b46feSOleg Nesterov task_lock(tsk); 722d68b46feSOleg Nesterov vfork = tsk->vfork_done; 723d68b46feSOleg Nesterov if (likely(vfork)) { 724c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 725d68b46feSOleg Nesterov complete(vfork); 726d68b46feSOleg Nesterov } 727d68b46feSOleg Nesterov task_unlock(tsk); 728d68b46feSOleg Nesterov } 729d68b46feSOleg Nesterov 730d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 731d68b46feSOleg Nesterov struct completion *vfork) 732d68b46feSOleg Nesterov { 733d68b46feSOleg Nesterov int killed; 734d68b46feSOleg Nesterov 735d68b46feSOleg Nesterov freezer_do_not_count(); 736d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 737d68b46feSOleg Nesterov freezer_count(); 738d68b46feSOleg Nesterov 739d68b46feSOleg Nesterov if (killed) { 740d68b46feSOleg Nesterov task_lock(child); 741d68b46feSOleg Nesterov child->vfork_done = NULL; 742d68b46feSOleg Nesterov task_unlock(child); 743d68b46feSOleg Nesterov } 744d68b46feSOleg Nesterov 745d68b46feSOleg Nesterov put_task_struct(child); 746d68b46feSOleg Nesterov return killed; 747c415c3b4SOleg Nesterov } 748c415c3b4SOleg Nesterov 7491da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 7501da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 7511da177e4SLinus Torvalds * error success whatever. 7521da177e4SLinus Torvalds * 7531da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 7541da177e4SLinus Torvalds * from the current process. 7551da177e4SLinus Torvalds * 7561da177e4SLinus Torvalds * This difference is important for error handling, when we 7571da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 7581da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 7591da177e4SLinus Torvalds * restoring the old one. . . 7601da177e4SLinus Torvalds * Eric Biederman 10 January 1998 7611da177e4SLinus Torvalds */ 7621da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 7631da177e4SLinus Torvalds { 7648141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 7658141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 766fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 7678141c7f3SLinus Torvalds exit_robust_list(tsk); 768fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 769fc6b177dSPeter Zijlstra } 7708141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 771fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 7728141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 773fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 774fc6b177dSPeter Zijlstra } 7758141c7f3SLinus Torvalds #endif 776322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 777322a2c10SThomas Gleixner exit_pi_state_list(tsk); 7788141c7f3SLinus Torvalds #endif 7798141c7f3SLinus Torvalds 7801da177e4SLinus Torvalds /* Get rid of any cached register state */ 7811da177e4SLinus Torvalds deactivate_mm(tsk, mm); 7821da177e4SLinus Torvalds 783c415c3b4SOleg Nesterov if (tsk->vfork_done) 784c415c3b4SOleg Nesterov complete_vfork_done(tsk); 785fec1d011SRoland McGrath 786fec1d011SRoland McGrath /* 787fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 788fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 789fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 790d68b46feSOleg Nesterov * trouble, say, a killed vfork parent shouldn't touch this mm. 791d68b46feSOleg Nesterov * Userland only wants this done for a sys_exit. 792fec1d011SRoland McGrath */ 7939c8a8228SEric Dumazet if (tsk->clear_child_tid) { 7949c8a8228SEric Dumazet if (!(tsk->flags & PF_SIGNALED) && 7959c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * We don't check the error code - if userspace has 7981da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 7991da177e4SLinus Torvalds */ 8009c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 8019c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 8029c8a8228SEric Dumazet 1, NULL, NULL, 0); 8039c8a8228SEric Dumazet } 8049c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 808a0a7ec30SJANAK DESAI /* 809a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 810a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 811a0a7ec30SJANAK DESAI */ 812402b0862SCarsten Otte struct mm_struct *dup_mm(struct task_struct *tsk) 813a0a7ec30SJANAK DESAI { 814a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 815a0a7ec30SJANAK DESAI int err; 816a0a7ec30SJANAK DESAI 817a0a7ec30SJANAK DESAI if (!oldmm) 818a0a7ec30SJANAK DESAI return NULL; 819a0a7ec30SJANAK DESAI 820a0a7ec30SJANAK DESAI mm = allocate_mm(); 821a0a7ec30SJANAK DESAI if (!mm) 822a0a7ec30SJANAK DESAI goto fail_nomem; 823a0a7ec30SJANAK DESAI 824a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 8256345d24dSLinus Torvalds mm_init_cpumask(mm); 826a0a7ec30SJANAK DESAI 8277602bdf2SAshwin Chaugule /* Initializing for Swap token stuff */ 8287602bdf2SAshwin Chaugule mm->token_priority = 0; 8297602bdf2SAshwin Chaugule mm->last_interval = 0; 8307602bdf2SAshwin Chaugule 831e7a00c45SAndrea Arcangeli #ifdef CONFIG_TRANSPARENT_HUGEPAGE 832e7a00c45SAndrea Arcangeli mm->pmd_huge_pte = NULL; 833e7a00c45SAndrea Arcangeli #endif 834e7a00c45SAndrea Arcangeli 83578fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 836a0a7ec30SJANAK DESAI goto fail_nomem; 837a0a7ec30SJANAK DESAI 838a0a7ec30SJANAK DESAI if (init_new_context(tsk, mm)) 839a0a7ec30SJANAK DESAI goto fail_nocontext; 840a0a7ec30SJANAK DESAI 841925d1c40SMatt Helsley dup_mm_exe_file(oldmm, mm); 842925d1c40SMatt Helsley 843a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 844a0a7ec30SJANAK DESAI if (err) 845a0a7ec30SJANAK DESAI goto free_pt; 846a0a7ec30SJANAK DESAI 847a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 848a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 849a0a7ec30SJANAK DESAI 850801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 851801460d0SHiroshi Shimamoto goto free_pt; 852801460d0SHiroshi Shimamoto 853a0a7ec30SJANAK DESAI return mm; 854a0a7ec30SJANAK DESAI 855a0a7ec30SJANAK DESAI free_pt: 856801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 857801460d0SHiroshi Shimamoto mm->binfmt = NULL; 858a0a7ec30SJANAK DESAI mmput(mm); 859a0a7ec30SJANAK DESAI 860a0a7ec30SJANAK DESAI fail_nomem: 861a0a7ec30SJANAK DESAI return NULL; 862a0a7ec30SJANAK DESAI 863a0a7ec30SJANAK DESAI fail_nocontext: 864a0a7ec30SJANAK DESAI /* 865a0a7ec30SJANAK DESAI * If init_new_context() failed, we cannot use mmput() to free the mm 866a0a7ec30SJANAK DESAI * because it calls destroy_context() 867a0a7ec30SJANAK DESAI */ 868a0a7ec30SJANAK DESAI mm_free_pgd(mm); 869a0a7ec30SJANAK DESAI free_mm(mm); 870a0a7ec30SJANAK DESAI return NULL; 871a0a7ec30SJANAK DESAI } 872a0a7ec30SJANAK DESAI 8731da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 8741da177e4SLinus Torvalds { 8751da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 8761da177e4SLinus Torvalds int retval; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 8791da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 88017406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 88117406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 88217406b82SMandeep Singh Baines #endif 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds tsk->mm = NULL; 8851da177e4SLinus Torvalds tsk->active_mm = NULL; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds /* 8881da177e4SLinus Torvalds * Are we cloning a kernel thread? 8891da177e4SLinus Torvalds * 8901da177e4SLinus Torvalds * We need to steal a active VM for that.. 8911da177e4SLinus Torvalds */ 8921da177e4SLinus Torvalds oldmm = current->mm; 8931da177e4SLinus Torvalds if (!oldmm) 8941da177e4SLinus Torvalds return 0; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 8971da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 8981da177e4SLinus Torvalds mm = oldmm; 8991da177e4SLinus Torvalds goto good_mm; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds retval = -ENOMEM; 903a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 9041da177e4SLinus Torvalds if (!mm) 9051da177e4SLinus Torvalds goto fail_nomem; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds good_mm: 9087602bdf2SAshwin Chaugule /* Initializing for Swap token stuff */ 9097602bdf2SAshwin Chaugule mm->token_priority = 0; 9107602bdf2SAshwin Chaugule mm->last_interval = 0; 9117602bdf2SAshwin Chaugule 9121da177e4SLinus Torvalds tsk->mm = mm; 9131da177e4SLinus Torvalds tsk->active_mm = mm; 9141da177e4SLinus Torvalds return 0; 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds fail_nomem: 9171da177e4SLinus Torvalds return retval; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 920a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 9211da177e4SLinus Torvalds { 922498052bbSAl Viro struct fs_struct *fs = current->fs; 9231da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 924498052bbSAl Viro /* tsk->fs is already what we want */ 9252a4419b5SNick Piggin spin_lock(&fs->lock); 926498052bbSAl Viro if (fs->in_exec) { 9272a4419b5SNick Piggin spin_unlock(&fs->lock); 928498052bbSAl Viro return -EAGAIN; 929498052bbSAl Viro } 930498052bbSAl Viro fs->users++; 9312a4419b5SNick Piggin spin_unlock(&fs->lock); 9321da177e4SLinus Torvalds return 0; 9331da177e4SLinus Torvalds } 934498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 9351da177e4SLinus Torvalds if (!tsk->fs) 9361da177e4SLinus Torvalds return -ENOMEM; 9371da177e4SLinus Torvalds return 0; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 940a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 941a016f338SJANAK DESAI { 942a016f338SJANAK DESAI struct files_struct *oldf, *newf; 943a016f338SJANAK DESAI int error = 0; 944a016f338SJANAK DESAI 945a016f338SJANAK DESAI /* 946a016f338SJANAK DESAI * A background process may not have any files ... 947a016f338SJANAK DESAI */ 948a016f338SJANAK DESAI oldf = current->files; 949a016f338SJANAK DESAI if (!oldf) 950a016f338SJANAK DESAI goto out; 951a016f338SJANAK DESAI 952a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 953a016f338SJANAK DESAI atomic_inc(&oldf->count); 954a016f338SJANAK DESAI goto out; 955a016f338SJANAK DESAI } 956a016f338SJANAK DESAI 957a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 958a016f338SJANAK DESAI if (!newf) 959a016f338SJANAK DESAI goto out; 960a016f338SJANAK DESAI 961a016f338SJANAK DESAI tsk->files = newf; 962a016f338SJANAK DESAI error = 0; 963a016f338SJANAK DESAI out: 964a016f338SJANAK DESAI return error; 965a016f338SJANAK DESAI } 966a016f338SJANAK DESAI 967fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 968fd0928dfSJens Axboe { 969fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 970fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 9716e736be7STejun Heo struct io_context *new_ioc; 972fd0928dfSJens Axboe 973fd0928dfSJens Axboe if (!ioc) 974fd0928dfSJens Axboe return 0; 975fadad878SJens Axboe /* 976fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 977fadad878SJens Axboe */ 978fadad878SJens Axboe if (clone_flags & CLONE_IO) { 979fadad878SJens Axboe tsk->io_context = ioc_task_link(ioc); 980fadad878SJens Axboe if (unlikely(!tsk->io_context)) 981fadad878SJens Axboe return -ENOMEM; 982fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 9836e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 9846e736be7STejun Heo if (unlikely(!new_ioc)) 985fd0928dfSJens Axboe return -ENOMEM; 986fd0928dfSJens Axboe 9876e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 98811a3122fSTejun Heo put_io_context(new_ioc); 989fd0928dfSJens Axboe } 990fd0928dfSJens Axboe #endif 991fd0928dfSJens Axboe return 0; 992fd0928dfSJens Axboe } 993fd0928dfSJens Axboe 994a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 9951da177e4SLinus Torvalds { 9961da177e4SLinus Torvalds struct sighand_struct *sig; 9971da177e4SLinus Torvalds 99860348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 9991da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 10001da177e4SLinus Torvalds return 0; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 1003e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 10041da177e4SLinus Torvalds if (!sig) 10051da177e4SLinus Torvalds return -ENOMEM; 10061da177e4SLinus Torvalds atomic_set(&sig->count, 1); 10071da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 10081da177e4SLinus Torvalds return 0; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 1011a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1012c81addc9SOleg Nesterov { 1013d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 1014d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1015c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1016c81addc9SOleg Nesterov } 1017d80e731eSOleg Nesterov } 1018c81addc9SOleg Nesterov 1019f06febc9SFrank Mayhar 1020f06febc9SFrank Mayhar /* 1021f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1022f06febc9SFrank Mayhar */ 1023f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1024f06febc9SFrank Mayhar { 102578d7d407SJiri Slaby unsigned long cpu_limit; 102678d7d407SJiri Slaby 1027f06febc9SFrank Mayhar /* Thread group counters. */ 1028f06febc9SFrank Mayhar thread_group_cputime_init(sig); 1029f06febc9SFrank Mayhar 103078d7d407SJiri Slaby cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 103178d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 103278d7d407SJiri Slaby sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); 10336279a751SOleg Nesterov sig->cputimer.running = 1; 10346279a751SOleg Nesterov } 10356279a751SOleg Nesterov 1036f06febc9SFrank Mayhar /* The timer lists. */ 1037f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1038f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1039f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1040f06febc9SFrank Mayhar } 1041f06febc9SFrank Mayhar 1042a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 10431da177e4SLinus Torvalds { 10441da177e4SLinus Torvalds struct signal_struct *sig; 10451da177e4SLinus Torvalds 10464ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1047490dea45SPeter Zijlstra return 0; 10486279a751SOleg Nesterov 1049a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 10501da177e4SLinus Torvalds tsk->signal = sig; 10511da177e4SLinus Torvalds if (!sig) 10521da177e4SLinus Torvalds return -ENOMEM; 10531da177e4SLinus Torvalds 1054b3ac022cSOleg Nesterov sig->nr_threads = 1; 10551da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1056b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 10571da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1058b3bfa0cbSSukadev Bhattiprolu if (clone_flags & CLONE_NEWPID) 1059b3bfa0cbSSukadev Bhattiprolu sig->flags |= SIGNAL_UNKILLABLE; 1060db51aeccSOleg Nesterov sig->curr_target = tsk; 10611da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 10621da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 10631da177e4SLinus Torvalds 1064c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 10651da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds task_lock(current->group_leader); 10681da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 10691da177e4SLinus Torvalds task_unlock(current->group_leader); 10701da177e4SLinus Torvalds 10716279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 10726279a751SOleg Nesterov 1073522ed776SMiloslav Trmac tty_audit_fork(sig); 10745091faa4SMike Galbraith sched_autogroup_fork(sig); 1075522ed776SMiloslav Trmac 10764714d1d3SBen Blum #ifdef CONFIG_CGROUPS 1077257058aeSTejun Heo init_rwsem(&sig->group_rwsem); 10784714d1d3SBen Blum #endif 10794714d1d3SBen Blum 108028b83c51SKOSAKI Motohiro sig->oom_adj = current->signal->oom_adj; 1081a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1082dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 108328b83c51SKOSAKI Motohiro 1084ebec18a6SLennart Poettering sig->has_child_subreaper = current->signal->has_child_subreaper || 1085ebec18a6SLennart Poettering current->signal->is_child_subreaper; 1086ebec18a6SLennart Poettering 10879b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 10889b1bf12dSKOSAKI Motohiro 10891da177e4SLinus Torvalds return 0; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 1092a39bc516SAlexey Dobriyan static void copy_flags(unsigned long clone_flags, struct task_struct *p) 10931da177e4SLinus Torvalds { 10941da177e4SLinus Torvalds unsigned long new_flags = p->flags; 10951da177e4SLinus Torvalds 109621aa9af0STejun Heo new_flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER); 10971da177e4SLinus Torvalds new_flags |= PF_FORKNOEXEC; 10981da177e4SLinus Torvalds p->flags = new_flags; 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds 110117da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 11021da177e4SLinus Torvalds { 11031da177e4SLinus Torvalds current->clear_child_tid = tidptr; 11041da177e4SLinus Torvalds 1105b488893aSPavel Emelyanov return task_pid_vnr(current); 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 1108a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 110923f78d4aSIngo Molnar { 11101d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1111e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1112732375c6SDima Zavin plist_head_init(&p->pi_waiters); 111323f78d4aSIngo Molnar p->pi_blocked_on = NULL; 111423f78d4aSIngo Molnar #endif 111523f78d4aSIngo Molnar } 111623f78d4aSIngo Molnar 1117cf475ad2SBalbir Singh #ifdef CONFIG_MM_OWNER 1118cf475ad2SBalbir Singh void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 1119cf475ad2SBalbir Singh { 1120cf475ad2SBalbir Singh mm->owner = p; 1121cf475ad2SBalbir Singh } 1122cf475ad2SBalbir Singh #endif /* CONFIG_MM_OWNER */ 1123cf475ad2SBalbir Singh 11241da177e4SLinus Torvalds /* 1125f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1126f06febc9SFrank Mayhar */ 1127f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1128f06febc9SFrank Mayhar { 112964861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 113064861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1131f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1132f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1133f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1134f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1135f06febc9SFrank Mayhar } 1136f06febc9SFrank Mayhar 1137f06febc9SFrank Mayhar /* 11381da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 11391da177e4SLinus Torvalds * but does not actually start it yet. 11401da177e4SLinus Torvalds * 11411da177e4SLinus Torvalds * It copies the registers, and all the appropriate 11421da177e4SLinus Torvalds * parts of the process environment (as per the clone 11431da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 11441da177e4SLinus Torvalds */ 114536c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 11461da177e4SLinus Torvalds unsigned long stack_start, 11471da177e4SLinus Torvalds struct pt_regs *regs, 11481da177e4SLinus Torvalds unsigned long stack_size, 11491da177e4SLinus Torvalds int __user *child_tidptr, 115009a05394SRoland McGrath struct pid *pid, 115109a05394SRoland McGrath int trace) 11521da177e4SLinus Torvalds { 11531da177e4SLinus Torvalds int retval; 1154a24efe62SMariusz Kozlowski struct task_struct *p; 1155b4f48b63SPaul Menage int cgroup_callbacks_done = 0; 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 11581da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds /* 11611da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 11621da177e4SLinus Torvalds * can only be started up within the thread group. 11631da177e4SLinus Torvalds */ 11641da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 11651da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds /* 11681da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 11691da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 11701da177e4SLinus Torvalds * for various simplifications in other code. 11711da177e4SLinus Torvalds */ 11721da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 11731da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 11741da177e4SLinus Torvalds 1175123be07bSSukadev Bhattiprolu /* 1176123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1177123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1178123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1179123be07bSSukadev Bhattiprolu * from creating siblings. 1180123be07bSSukadev Bhattiprolu */ 1181123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1182123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1183123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1184123be07bSSukadev Bhattiprolu 11851da177e4SLinus Torvalds retval = security_task_create(clone_flags); 11861da177e4SLinus Torvalds if (retval) 11871da177e4SLinus Torvalds goto fork_out; 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds retval = -ENOMEM; 11901da177e4SLinus Torvalds p = dup_task_struct(current); 11911da177e4SLinus Torvalds if (!p) 11921da177e4SLinus Torvalds goto fork_out; 11931da177e4SLinus Torvalds 1194f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1195f7e8b616SSteven Rostedt 1196bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1197bea493a0SPeter Zijlstra 1198d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1199de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1200de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1201de30a2b3SIngo Molnar #endif 12021da177e4SLinus Torvalds retval = -EAGAIN; 12033b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 120478d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 12051da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) && 120618b6e041SSerge Hallyn p->real_cred->user != INIT_USER) 12071da177e4SLinus Torvalds goto bad_fork_free; 12081da177e4SLinus Torvalds } 120972fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 12101da177e4SLinus Torvalds 1211f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1212f1752eecSDavid Howells if (retval < 0) 1213f1752eecSDavid Howells goto bad_fork_free; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds /* 12161da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 12171da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 12181da177e4SLinus Torvalds * to stop root fork bombs. 12191da177e4SLinus Torvalds */ 122004ec93feSLi Zefan retval = -EAGAIN; 12211da177e4SLinus Torvalds if (nr_threads >= max_threads) 12221da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12231da177e4SLinus Torvalds 1224a1261f54SAl Viro if (!try_module_get(task_thread_info(p)->exec_domain->module)) 12251da177e4SLinus Torvalds goto bad_fork_cleanup_count; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds p->did_exec = 0; 1228ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 12291da177e4SLinus Torvalds copy_flags(clone_flags, p); 12301da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 12311da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1232f41d911fSPaul E. McKenney rcu_copy_process(p); 12331da177e4SLinus Torvalds p->vfork_done = NULL; 12341da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds init_sigpending(&p->pending); 12371da177e4SLinus Torvalds 123864861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 123964861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 1240d99ca3b9SHidetoshi Seto #ifndef CONFIG_VIRT_CPU_ACCOUNTING 124164861634SMartin Schwidefsky p->prev_utime = p->prev_stime = 0; 1242d99ca3b9SHidetoshi Seto #endif 1243a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1244a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1245a3a2e76cSKAMEZAWA Hiroyuki #endif 1246172ba844SBalbir Singh 12476976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 12486976675dSArjan van de Ven 12495995477aSAndrea Righi task_io_accounting_init(&p->ioac); 12501da177e4SLinus Torvalds acct_clear_integrals(p); 12511da177e4SLinus Torvalds 1252f06febc9SFrank Mayhar posix_cpu_timers_init(p); 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&p->start_time); 1255924b42d5STomas Janousek p->real_start_time = p->start_time; 1256924b42d5STomas Janousek monotonic_to_bootbased(&p->real_start_time); 12571da177e4SLinus Torvalds p->io_context = NULL; 12581da177e4SLinus Torvalds p->audit_context = NULL; 12594714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1260257058aeSTejun Heo threadgroup_change_begin(current); 1261b4f48b63SPaul Menage cgroup_fork(p); 12621da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1263846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 12641da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 12651da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 12661da177e4SLinus Torvalds p->mempolicy = NULL; 1267b4f48b63SPaul Menage goto bad_fork_cleanup_cgroup; 12681da177e4SLinus Torvalds } 1269c61afb18SPaul Jackson mpol_fix_fork_child_flag(p); 12701da177e4SLinus Torvalds #endif 1271778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1272778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1273778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1274cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1275778d3b0fSMichal Hocko #endif 1276de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1277de30a2b3SIngo Molnar p->irq_events = 0; 1278b36e4758SRussell King #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW 1279b36e4758SRussell King p->hardirqs_enabled = 1; 1280b36e4758SRussell King #else 1281de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1282b36e4758SRussell King #endif 1283de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1284de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1285de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1286de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1287de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1288de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1289de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1290de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1291de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1292de30a2b3SIngo Molnar p->hardirq_context = 0; 1293de30a2b3SIngo Molnar p->softirq_context = 0; 1294de30a2b3SIngo Molnar #endif 1295fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1296fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1297fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1298fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1299fbb9ce95SIngo Molnar #endif 13001da177e4SLinus Torvalds 1301408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1302408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1303408894eeSIngo Molnar #endif 1304569b846dSKAMEZAWA Hiroyuki #ifdef CONFIG_CGROUP_MEM_RES_CTLR 1305569b846dSKAMEZAWA Hiroyuki p->memcg_batch.do_batch = 0; 1306569b846dSKAMEZAWA Hiroyuki p->memcg_batch.memcg = NULL; 1307569b846dSKAMEZAWA Hiroyuki #endif 13080f481406SMarkus Metzger 13093c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 13103e51e3edSSamir Bellabes sched_fork(p); 13116ab423e0SPeter Zijlstra 1312cdd6c482SIngo Molnar retval = perf_event_init_task(p); 13136ab423e0SPeter Zijlstra if (retval) 13146ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1315fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1316fb0a685cSDaniel Rebelo de Oliveira if (retval) 1317f1752eecSDavid Howells goto bad_fork_cleanup_policy; 13181da177e4SLinus Torvalds /* copy all the process information */ 1319fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1320fb0a685cSDaniel Rebelo de Oliveira if (retval) 13211da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1322fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1323fb0a685cSDaniel Rebelo de Oliveira if (retval) 13241da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1325fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1326fb0a685cSDaniel Rebelo de Oliveira if (retval) 13271da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1328fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1329fb0a685cSDaniel Rebelo de Oliveira if (retval) 13301da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1331fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1332fb0a685cSDaniel Rebelo de Oliveira if (retval) 13331da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1334fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1335fb0a685cSDaniel Rebelo de Oliveira if (retval) 13361da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1337fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1338fb0a685cSDaniel Rebelo de Oliveira if (retval) 1339d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1340fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1341fb0a685cSDaniel Rebelo de Oliveira if (retval) 1342fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 13436f2c55b8SAlexey Dobriyan retval = copy_thread(clone_flags, stack_start, stack_size, p, regs); 13441da177e4SLinus Torvalds if (retval) 1345fd0928dfSJens Axboe goto bad_fork_cleanup_io; 13461da177e4SLinus Torvalds 1347425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1348425fb2b4SPavel Emelyanov retval = -ENOMEM; 134961bce0f1SEric W. Biederman pid = alloc_pid(p->nsproxy->pid_ns); 1350425fb2b4SPavel Emelyanov if (!pid) 1351fd0928dfSJens Axboe goto bad_fork_cleanup_io; 1352425fb2b4SPavel Emelyanov } 1353425fb2b4SPavel Emelyanov 1354425fb2b4SPavel Emelyanov p->pid = pid_nr(pid); 1355425fb2b4SPavel Emelyanov p->tgid = p->pid; 1356425fb2b4SPavel Emelyanov if (clone_flags & CLONE_THREAD) 1357425fb2b4SPavel Emelyanov p->tgid = current->tgid; 1358425fb2b4SPavel Emelyanov 13591da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 13601da177e4SLinus Torvalds /* 13611da177e4SLinus Torvalds * Clear TID on mm_release()? 13621da177e4SLinus Torvalds */ 13631da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 136473c10101SJens Axboe #ifdef CONFIG_BLOCK 136573c10101SJens Axboe p->plug = NULL; 136673c10101SJens Axboe #endif 136742b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 13688f17d3a5SIngo Molnar p->robust_list = NULL; 13698f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 13708f17d3a5SIngo Molnar p->compat_robust_list = NULL; 13718f17d3a5SIngo Molnar #endif 1372c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1373c87e2837SIngo Molnar p->pi_state_cache = NULL; 137442b2dd0aSAlexey Dobriyan #endif 13751da177e4SLinus Torvalds /* 1376f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1377f9a3879aSGOTO Masanori */ 1378f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 1379f9a3879aSGOTO Masanori p->sas_ss_sp = p->sas_ss_size = 0; 1380f9a3879aSGOTO Masanori 1381f9a3879aSGOTO Masanori /* 13826580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 13836580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 13841da177e4SLinus Torvalds */ 13856580807dSOleg Nesterov user_disable_single_step(p); 13861da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1387ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1388ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1389ed75e8d5SLaurent Vivier #endif 13909745512cSArjan van de Ven clear_all_latency_tracing(p); 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds /* ok, now we should be set up.. */ 13935f8aadd8SOleg Nesterov if (clone_flags & CLONE_THREAD) 13945f8aadd8SOleg Nesterov p->exit_signal = -1; 13955f8aadd8SOleg Nesterov else if (clone_flags & CLONE_PARENT) 13965f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 13975f8aadd8SOleg Nesterov else 13985f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 13995f8aadd8SOleg Nesterov 14001da177e4SLinus Torvalds p->pdeath_signal = 0; 14011da177e4SLinus Torvalds p->exit_state = 0; 14021da177e4SLinus Torvalds 14039d823e8fSWu Fengguang p->nr_dirtied = 0; 14049d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 140583712358SWu Fengguang p->dirty_paused_when = 0; 14069d823e8fSWu Fengguang 14071da177e4SLinus Torvalds /* 14081da177e4SLinus Torvalds * Ok, make it visible to the rest of the system. 14091da177e4SLinus Torvalds * We dont wake it up yet. 14101da177e4SLinus Torvalds */ 14111da177e4SLinus Torvalds p->group_leader = p; 141247e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 14131da177e4SLinus Torvalds 1414b4f48b63SPaul Menage /* Now that the task is set up, run cgroup callbacks if 1415b4f48b63SPaul Menage * necessary. We need to run them before the task is visible 1416b4f48b63SPaul Menage * on the tasklist. */ 1417b4f48b63SPaul Menage cgroup_fork_callbacks(p); 1418b4f48b63SPaul Menage cgroup_callbacks_done = 1; 1419b4f48b63SPaul Menage 14201da177e4SLinus Torvalds /* Need tasklist lock for parent etc handling! */ 14211da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 14242d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 14251da177e4SLinus Torvalds p->real_parent = current->real_parent; 14262d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 14272d5516cbSOleg Nesterov } else { 14281da177e4SLinus Torvalds p->real_parent = current; 14292d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 14302d5516cbSOleg Nesterov } 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 14334a2c7a78SOleg Nesterov 14344a2c7a78SOleg Nesterov /* 14354a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 14364a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 14374a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 14384a2c7a78SOleg Nesterov * it's process group. 14394a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 14404a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 14414a2c7a78SOleg Nesterov */ 14424a2c7a78SOleg Nesterov recalc_sigpending(); 14434a2c7a78SOleg Nesterov if (signal_pending(current)) { 14444a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 14454a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 14464a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 1447f7e8b616SSteven Rostedt goto bad_fork_free_pid; 14484a2c7a78SOleg Nesterov } 14494a2c7a78SOleg Nesterov 14503f17da69SOleg Nesterov if (clone_flags & CLONE_THREAD) { 1451b3ac022cSOleg Nesterov current->signal->nr_threads++; 14524ab6c083SOleg Nesterov atomic_inc(¤t->signal->live); 1453b3ac022cSOleg Nesterov atomic_inc(¤t->signal->sigcnt); 14541da177e4SLinus Torvalds p->group_leader = current->group_leader; 145547e65328SOleg Nesterov list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group); 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 145873b9ebfeSOleg Nesterov if (likely(p->pid)) { 14594b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds if (thread_group_leader(p)) { 146245a68628SEric W. Biederman if (is_child_reaper(pid)) 146330e49c26SPavel Emelyanov p->nsproxy->pid_ns->child_reaper = p; 14645cd17569SEric W. Biederman 1465fea9d175SOleg Nesterov p->signal->leader_pid = pid; 14669c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 14675cd17569SEric W. Biederman attach_pid(p, PIDTYPE_PGID, task_pgrp(current)); 14685cd17569SEric W. Biederman attach_pid(p, PIDTYPE_SID, task_session(current)); 14699cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 14705e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 1471909ea964SChristoph Lameter __this_cpu_inc(process_counts); 14721da177e4SLinus Torvalds } 147385868995SSukadev Bhattiprolu attach_pid(p, PIDTYPE_PID, pid); 14741da177e4SLinus Torvalds nr_threads++; 147573b9ebfeSOleg Nesterov } 147673b9ebfeSOleg Nesterov 14771da177e4SLinus Torvalds total_forks++; 14783f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 14791da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 1480c13cf856SAndrew Morton proc_fork_connector(p); 1481817929ecSPaul Menage cgroup_post_fork(p); 14824714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1483257058aeSTejun Heo threadgroup_change_end(current); 1484cdd6c482SIngo Molnar perf_event_fork(p); 148543d2b113SKAMEZAWA Hiroyuki 148643d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 148743d2b113SKAMEZAWA Hiroyuki 14881da177e4SLinus Torvalds return p; 14891da177e4SLinus Torvalds 1490425fb2b4SPavel Emelyanov bad_fork_free_pid: 1491425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1492425fb2b4SPavel Emelyanov free_pid(pid); 1493fd0928dfSJens Axboe bad_fork_cleanup_io: 1494b69f2292SLouis Rilling if (p->io_context) 1495b69f2292SLouis Rilling exit_io_context(p); 1496ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1497444f378bSLinus Torvalds exit_task_namespaces(p); 14981da177e4SLinus Torvalds bad_fork_cleanup_mm: 1499c9f01245SDavid Rientjes if (p->mm) 15001da177e4SLinus Torvalds mmput(p->mm); 15011da177e4SLinus Torvalds bad_fork_cleanup_signal: 15024ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 15031c5354deSMike Galbraith free_signal_struct(p->signal); 15041da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1505a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 15061da177e4SLinus Torvalds bad_fork_cleanup_fs: 15071da177e4SLinus Torvalds exit_fs(p); /* blocking */ 15081da177e4SLinus Torvalds bad_fork_cleanup_files: 15091da177e4SLinus Torvalds exit_files(p); /* blocking */ 15101da177e4SLinus Torvalds bad_fork_cleanup_semundo: 15111da177e4SLinus Torvalds exit_sem(p); 15121da177e4SLinus Torvalds bad_fork_cleanup_audit: 15131da177e4SLinus Torvalds audit_free(p); 15141da177e4SLinus Torvalds bad_fork_cleanup_policy: 1515cdd6c482SIngo Molnar perf_event_free_task(p); 15161da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1517f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1518b4f48b63SPaul Menage bad_fork_cleanup_cgroup: 15191da177e4SLinus Torvalds #endif 15204714d1d3SBen Blum if (clone_flags & CLONE_THREAD) 1521257058aeSTejun Heo threadgroup_change_end(current); 1522b4f48b63SPaul Menage cgroup_exit(p, cgroup_callbacks_done); 152335df17c5SShailabh Nagar delayacct_tsk_free(p); 1524a1261f54SAl Viro module_put(task_thread_info(p)->exec_domain->module); 15251da177e4SLinus Torvalds bad_fork_cleanup_count: 1526d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1527e0e81739SDavid Howells exit_creds(p); 15281da177e4SLinus Torvalds bad_fork_free: 15291da177e4SLinus Torvalds free_task(p); 1530fe7d37d1SOleg Nesterov fork_out: 1531fe7d37d1SOleg Nesterov return ERR_PTR(retval); 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds 15346b2fb3c6SAdrian Bunk noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs) 15351da177e4SLinus Torvalds { 15361da177e4SLinus Torvalds memset(regs, 0, sizeof(struct pt_regs)); 15371da177e4SLinus Torvalds return regs; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 1540f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1541f106eee1SOleg Nesterov { 1542f106eee1SOleg Nesterov enum pid_type type; 1543f106eee1SOleg Nesterov 1544f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1545f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1546f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1547f106eee1SOleg Nesterov } 1548f106eee1SOleg Nesterov } 1549f106eee1SOleg Nesterov 15509abcf40bSAl Viro struct task_struct * __cpuinit fork_idle(int cpu) 15511da177e4SLinus Torvalds { 155236c8b586SIngo Molnar struct task_struct *task; 15531da177e4SLinus Torvalds struct pt_regs regs; 15541da177e4SLinus Torvalds 155530e49c26SPavel Emelyanov task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, 155609a05394SRoland McGrath &init_struct_pid, 0); 1557f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1558f106eee1SOleg Nesterov init_idle_pids(task->pids); 15591da177e4SLinus Torvalds init_idle(task, cpu); 1560f106eee1SOleg Nesterov } 156173b9ebfeSOleg Nesterov 15621da177e4SLinus Torvalds return task; 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds /* 15661da177e4SLinus Torvalds * Ok, this is the main fork-routine. 15671da177e4SLinus Torvalds * 15681da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 15691da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 15701da177e4SLinus Torvalds */ 15711da177e4SLinus Torvalds long do_fork(unsigned long clone_flags, 15721da177e4SLinus Torvalds unsigned long stack_start, 15731da177e4SLinus Torvalds struct pt_regs *regs, 15741da177e4SLinus Torvalds unsigned long stack_size, 15751da177e4SLinus Torvalds int __user *parent_tidptr, 15761da177e4SLinus Torvalds int __user *child_tidptr) 15771da177e4SLinus Torvalds { 15781da177e4SLinus Torvalds struct task_struct *p; 15791da177e4SLinus Torvalds int trace = 0; 158092476d7fSEric W. Biederman long nr; 15811da177e4SLinus Torvalds 1582bdff746aSAndrew Morton /* 158318b6e041SSerge Hallyn * Do some preliminary argument and permissions checking before we 158418b6e041SSerge Hallyn * actually start allocating stuff 158518b6e041SSerge Hallyn */ 158618b6e041SSerge Hallyn if (clone_flags & CLONE_NEWUSER) { 158718b6e041SSerge Hallyn if (clone_flags & CLONE_THREAD) 158818b6e041SSerge Hallyn return -EINVAL; 158918b6e041SSerge Hallyn /* hopefully this check will go away when userns support is 159018b6e041SSerge Hallyn * complete 159118b6e041SSerge Hallyn */ 15927657d904SSerge E. Hallyn if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) || 15937657d904SSerge E. Hallyn !capable(CAP_SETGID)) 159418b6e041SSerge Hallyn return -EPERM; 159518b6e041SSerge Hallyn } 159618b6e041SSerge Hallyn 159718b6e041SSerge Hallyn /* 15984b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 15994b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 16004b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 16014b9d33e6STejun Heo * for the type of forking is enabled. 160209a05394SRoland McGrath */ 16034b9d33e6STejun Heo if (likely(user_mode(regs)) && !(clone_flags & CLONE_UNTRACED)) { 16044b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 16054b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 16064b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 16074b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 16084b9d33e6STejun Heo else 16094b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 16104b9d33e6STejun Heo 16114b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 16124b9d33e6STejun Heo trace = 0; 16134b9d33e6STejun Heo } 16141da177e4SLinus Torvalds 1615a6f5e063SSukadev Bhattiprolu p = copy_process(clone_flags, stack_start, regs, stack_size, 161609a05394SRoland McGrath child_tidptr, NULL, trace); 16171da177e4SLinus Torvalds /* 16181da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 16191da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 16201da177e4SLinus Torvalds */ 16211da177e4SLinus Torvalds if (!IS_ERR(p)) { 16221da177e4SLinus Torvalds struct completion vfork; 16231da177e4SLinus Torvalds 16240a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 16250a16b607SMathieu Desnoyers 16266c5f3e7bSPavel Emelyanov nr = task_pid_vnr(p); 162730e49c26SPavel Emelyanov 162830e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 162930e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1630a6f5e063SSukadev Bhattiprolu 16311da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 16321da177e4SLinus Torvalds p->vfork_done = &vfork; 16331da177e4SLinus Torvalds init_completion(&vfork); 1634d68b46feSOleg Nesterov get_task_struct(p); 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 16373e51e3edSSamir Bellabes wake_up_new_task(p); 16381da177e4SLinus Torvalds 16394b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 16404b9d33e6STejun Heo if (unlikely(trace)) 16414b9d33e6STejun Heo ptrace_event(trace, nr); 164209a05394SRoland McGrath 16431da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1644d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 1645a288eeccSTejun Heo ptrace_event(PTRACE_EVENT_VFORK_DONE, nr); 16469f59ce5dSChuck Ebbert } 16471da177e4SLinus Torvalds } else { 164892476d7fSEric W. Biederman nr = PTR_ERR(p); 16491da177e4SLinus Torvalds } 165092476d7fSEric W. Biederman return nr; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16535fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 16545fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 16555fd63b30SRavikiran G Thirumalai #endif 16565fd63b30SRavikiran G Thirumalai 165751cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 1658aa1757f9SOleg Nesterov { 1659aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1660aa1757f9SOleg Nesterov 1661aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1662b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 1663fba2afaaSDavide Libenzi } 1664aa1757f9SOleg Nesterov 16651da177e4SLinus Torvalds void __init proc_caches_init(void) 16661da177e4SLinus Torvalds { 16671da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 16681da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 16692dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 16702dff4405SVegard Nossum SLAB_NOTRACK, sighand_ctor); 16711da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 16721da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 16732dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 16741da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 16751da177e4SLinus Torvalds sizeof(struct files_struct), 0, 16762dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 16771da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 16781da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 16792dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 16806345d24dSLinus Torvalds /* 16816345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 16826345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 16836345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 16846345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 16856345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 16866345d24dSLinus Torvalds */ 16871da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 16885fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 16892dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL); 169033e5d769SDavid Howells vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC); 16918feae131SDavid Howells mmap_init(); 169266577193SAl Viro nsproxy_cache_init(); 16931da177e4SLinus Torvalds } 1694cf2e340fSJANAK DESAI 1695cf2e340fSJANAK DESAI /* 16969bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 1697cf2e340fSJANAK DESAI */ 16989bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 1699cf2e340fSJANAK DESAI { 17009bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 17019bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 17029bfb23fcSOleg Nesterov CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET)) 1703cf2e340fSJANAK DESAI return -EINVAL; 17049bfb23fcSOleg Nesterov /* 17059bfb23fcSOleg Nesterov * Not implemented, but pretend it works if there is nothing to 17069bfb23fcSOleg Nesterov * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND 17079bfb23fcSOleg Nesterov * needs to unshare vm. 17089bfb23fcSOleg Nesterov */ 17099bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 17109bfb23fcSOleg Nesterov /* FIXME: get_task_mm() increments ->mm_users */ 17119bfb23fcSOleg Nesterov if (atomic_read(¤t->mm->mm_users) > 1) 17129bfb23fcSOleg Nesterov return -EINVAL; 17139bfb23fcSOleg Nesterov } 1714cf2e340fSJANAK DESAI 1715cf2e340fSJANAK DESAI return 0; 1716cf2e340fSJANAK DESAI } 1717cf2e340fSJANAK DESAI 1718cf2e340fSJANAK DESAI /* 171999d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1720cf2e340fSJANAK DESAI */ 1721cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1722cf2e340fSJANAK DESAI { 1723cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1724cf2e340fSJANAK DESAI 1725498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 1726498052bbSAl Viro return 0; 1727498052bbSAl Viro 1728498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 1729498052bbSAl Viro if (fs->users == 1) 1730498052bbSAl Viro return 0; 1731498052bbSAl Viro 1732498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 173399d1419dSJANAK DESAI if (!*new_fsp) 173499d1419dSJANAK DESAI return -ENOMEM; 1735cf2e340fSJANAK DESAI 1736cf2e340fSJANAK DESAI return 0; 1737cf2e340fSJANAK DESAI } 1738cf2e340fSJANAK DESAI 1739cf2e340fSJANAK DESAI /* 1740a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1741cf2e340fSJANAK DESAI */ 1742cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1743cf2e340fSJANAK DESAI { 1744cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1745a016f338SJANAK DESAI int error = 0; 1746cf2e340fSJANAK DESAI 1747cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1748a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1749a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1750a016f338SJANAK DESAI if (!*new_fdp) 1751a016f338SJANAK DESAI return error; 1752a016f338SJANAK DESAI } 1753cf2e340fSJANAK DESAI 1754cf2e340fSJANAK DESAI return 0; 1755cf2e340fSJANAK DESAI } 1756cf2e340fSJANAK DESAI 1757cf2e340fSJANAK DESAI /* 1758cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1759cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1760cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1761cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1762cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1763cf2e340fSJANAK DESAI * task_struct. 1764cf2e340fSJANAK DESAI */ 17656559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 1766cf2e340fSJANAK DESAI { 1767cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1768cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1769cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 17709edff4abSManfred Spraul int do_sysvsem = 0; 17719bfb23fcSOleg Nesterov int err; 1772cf2e340fSJANAK DESAI 17739bfb23fcSOleg Nesterov err = check_unshare_flags(unshare_flags); 17749bfb23fcSOleg Nesterov if (err) 177506f9d4f9SEric W. Biederman goto bad_unshare_out; 177606f9d4f9SEric W. Biederman 17776013f67fSManfred Spraul /* 17789bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 17799bfb23fcSOleg Nesterov */ 17809bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 17819bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 17829bfb23fcSOleg Nesterov /* 17836013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 17846013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 17856013f67fSManfred Spraul * namespace are unreachable. 17866013f67fSManfred Spraul */ 17876013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 17889edff4abSManfred Spraul do_sysvsem = 1; 1789fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 1790fb0a685cSDaniel Rebelo de Oliveira if (err) 17919bfb23fcSOleg Nesterov goto bad_unshare_out; 1792fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 1793fb0a685cSDaniel Rebelo de Oliveira if (err) 17949bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 1795fb0a685cSDaniel Rebelo de Oliveira err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, new_fs); 1796fb0a685cSDaniel Rebelo de Oliveira if (err) 17979edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 1798cf2e340fSJANAK DESAI 17999bfb23fcSOleg Nesterov if (new_fs || new_fd || do_sysvsem || new_nsproxy) { 18009edff4abSManfred Spraul if (do_sysvsem) { 18019edff4abSManfred Spraul /* 18029edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 18039edff4abSManfred Spraul */ 18049edff4abSManfred Spraul exit_sem(current); 18059edff4abSManfred Spraul } 1806ab516013SSerge E. Hallyn 1807c0b2fc31SSerge Hallyn if (new_nsproxy) { 1808cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 1809cf7b708cSPavel Emelyanov new_nsproxy = NULL; 1810c0b2fc31SSerge Hallyn } 1811cf2e340fSJANAK DESAI 1812cf7b708cSPavel Emelyanov task_lock(current); 1813cf7b708cSPavel Emelyanov 1814cf2e340fSJANAK DESAI if (new_fs) { 1815cf2e340fSJANAK DESAI fs = current->fs; 18162a4419b5SNick Piggin spin_lock(&fs->lock); 1817cf2e340fSJANAK DESAI current->fs = new_fs; 1818498052bbSAl Viro if (--fs->users) 1819498052bbSAl Viro new_fs = NULL; 1820498052bbSAl Viro else 1821cf2e340fSJANAK DESAI new_fs = fs; 18222a4419b5SNick Piggin spin_unlock(&fs->lock); 1823cf2e340fSJANAK DESAI } 1824cf2e340fSJANAK DESAI 1825cf2e340fSJANAK DESAI if (new_fd) { 1826cf2e340fSJANAK DESAI fd = current->files; 1827cf2e340fSJANAK DESAI current->files = new_fd; 1828cf2e340fSJANAK DESAI new_fd = fd; 1829cf2e340fSJANAK DESAI } 1830cf2e340fSJANAK DESAI 1831cf2e340fSJANAK DESAI task_unlock(current); 1832cf2e340fSJANAK DESAI } 1833cf2e340fSJANAK DESAI 1834c0b2fc31SSerge Hallyn if (new_nsproxy) 1835444f378bSLinus Torvalds put_nsproxy(new_nsproxy); 1836c0b2fc31SSerge Hallyn 1837cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 1838cf2e340fSJANAK DESAI if (new_fd) 1839cf2e340fSJANAK DESAI put_files_struct(new_fd); 1840cf2e340fSJANAK DESAI 1841cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 1842cf2e340fSJANAK DESAI if (new_fs) 1843498052bbSAl Viro free_fs_struct(new_fs); 1844cf2e340fSJANAK DESAI 1845cf2e340fSJANAK DESAI bad_unshare_out: 1846cf2e340fSJANAK DESAI return err; 1847cf2e340fSJANAK DESAI } 18483b125388SAl Viro 18493b125388SAl Viro /* 18503b125388SAl Viro * Helper to unshare the files of the current task. 18513b125388SAl Viro * We don't want to expose copy_files internals to 18523b125388SAl Viro * the exec layer of the kernel. 18533b125388SAl Viro */ 18543b125388SAl Viro 18553b125388SAl Viro int unshare_files(struct files_struct **displaced) 18563b125388SAl Viro { 18573b125388SAl Viro struct task_struct *task = current; 185850704516SAl Viro struct files_struct *copy = NULL; 18593b125388SAl Viro int error; 18603b125388SAl Viro 18613b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 18623b125388SAl Viro if (error || !copy) { 18633b125388SAl Viro *displaced = NULL; 18643b125388SAl Viro return error; 18653b125388SAl Viro } 18663b125388SAl Viro *displaced = task->files; 18673b125388SAl Viro task_lock(task); 18683b125388SAl Viro task->files = copy; 18693b125388SAl Viro task_unlock(task); 18703b125388SAl Viro return 0; 18713b125388SAl Viro } 1872