11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/fork.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * 'fork.c' contains the help-routines for the 'fork' system call 91da177e4SLinus Torvalds * (see also entry.S and others). 101da177e4SLinus Torvalds * Fork is rather simple, once you get the hang of it, but the memory 111da177e4SLinus Torvalds * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/slab.h> 151da177e4SLinus Torvalds #include <linux/init.h> 161da177e4SLinus Torvalds #include <linux/unistd.h> 171da177e4SLinus Torvalds #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/vmalloc.h> 191da177e4SLinus Torvalds #include <linux/completion.h> 201da177e4SLinus Torvalds #include <linux/personality.h> 211da177e4SLinus Torvalds #include <linux/mempolicy.h> 221da177e4SLinus Torvalds #include <linux/sem.h> 231da177e4SLinus Torvalds #include <linux/file.h> 249f3acc31SAl Viro #include <linux/fdtable.h> 25da9cbc87SJens Axboe #include <linux/iocontext.h> 261da177e4SLinus Torvalds #include <linux/key.h> 271da177e4SLinus Torvalds #include <linux/binfmts.h> 281da177e4SLinus Torvalds #include <linux/mman.h> 29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 301da177e4SLinus Torvalds #include <linux/fs.h> 31615d6e87SDavidlohr Bueso #include <linux/mm.h> 32615d6e87SDavidlohr Bueso #include <linux/vmacache.h> 33ab516013SSerge E. Hallyn #include <linux/nsproxy.h> 34c59ede7bSRandy.Dunlap #include <linux/capability.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 36b4f48b63SPaul Menage #include <linux/cgroup.h> 371da177e4SLinus Torvalds #include <linux/security.h> 38a1e78772SMel Gorman #include <linux/hugetlb.h> 39e2cfabdfSWill Drewry #include <linux/seccomp.h> 401da177e4SLinus Torvalds #include <linux/swap.h> 411da177e4SLinus Torvalds #include <linux/syscalls.h> 421da177e4SLinus Torvalds #include <linux/jiffies.h> 431da177e4SLinus Torvalds #include <linux/futex.h> 448141c7f3SLinus Torvalds #include <linux/compat.h> 45207205a2SEric Dumazet #include <linux/kthread.h> 467c3ab738SAndrew Morton #include <linux/task_io_accounting_ops.h> 47ab2af1f5SDipankar Sarma #include <linux/rcupdate.h> 481da177e4SLinus Torvalds #include <linux/ptrace.h> 491da177e4SLinus Torvalds #include <linux/mount.h> 501da177e4SLinus Torvalds #include <linux/audit.h> 5178fb7466SPavel Emelianov #include <linux/memcontrol.h> 52f201ae23SFrederic Weisbecker #include <linux/ftrace.h> 535e2bf014SMike Galbraith #include <linux/proc_fs.h> 541da177e4SLinus Torvalds #include <linux/profile.h> 551da177e4SLinus Torvalds #include <linux/rmap.h> 56f8af4da3SHugh Dickins #include <linux/ksm.h> 571da177e4SLinus Torvalds #include <linux/acct.h> 588f0ab514SJay Lan #include <linux/tsacct_kern.h> 599f46080cSMatt Helsley #include <linux/cn_proc.h> 60ba96a0c8SRafael J. Wysocki #include <linux/freezer.h> 61ca74e92bSShailabh Nagar #include <linux/delayacct.h> 62ad4ecbcbSShailabh Nagar #include <linux/taskstats_kern.h> 630a425405SArjan van de Ven #include <linux/random.h> 64522ed776SMiloslav Trmac #include <linux/tty.h> 65fd0928dfSJens Axboe #include <linux/blkdev.h> 665ad4e53bSAl Viro #include <linux/fs_struct.h> 677c9f8861SEric Sandeen #include <linux/magic.h> 68cdd6c482SIngo Molnar #include <linux/perf_event.h> 6942c4ab41SStanislaw Gruszka #include <linux/posix-timers.h> 708e7cac79SAvi Kivity #include <linux/user-return-notifier.h> 713d5992d2SYing Han #include <linux/oom.h> 72ba76149fSAndrea Arcangeli #include <linux/khugepaged.h> 73d80e731eSOleg Nesterov #include <linux/signalfd.h> 740326f5a9SSrikar Dronamraju #include <linux/uprobes.h> 75a27bb332SKent Overstreet #include <linux/aio.h> 7652f5684cSGideon Israel Dsouza #include <linux/compiler.h> 7716db3d3fSHeinrich Schuchardt #include <linux/sysctl.h> 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds #include <asm/pgtable.h> 801da177e4SLinus Torvalds #include <asm/pgalloc.h> 811da177e4SLinus Torvalds #include <asm/uaccess.h> 821da177e4SLinus Torvalds #include <asm/mmu_context.h> 831da177e4SLinus Torvalds #include <asm/cacheflush.h> 841da177e4SLinus Torvalds #include <asm/tlbflush.h> 851da177e4SLinus Torvalds 86ad8d75ffSSteven Rostedt #include <trace/events/sched.h> 87ad8d75ffSSteven Rostedt 8843d2b113SKAMEZAWA Hiroyuki #define CREATE_TRACE_POINTS 8943d2b113SKAMEZAWA Hiroyuki #include <trace/events/task.h> 9043d2b113SKAMEZAWA Hiroyuki 911da177e4SLinus Torvalds /* 92ac1b398dSHeinrich Schuchardt * Minimum number of threads to boot the kernel 93ac1b398dSHeinrich Schuchardt */ 94ac1b398dSHeinrich Schuchardt #define MIN_THREADS 20 95ac1b398dSHeinrich Schuchardt 96ac1b398dSHeinrich Schuchardt /* 97ac1b398dSHeinrich Schuchardt * Maximum number of threads 98ac1b398dSHeinrich Schuchardt */ 99ac1b398dSHeinrich Schuchardt #define MAX_THREADS FUTEX_TID_MASK 100ac1b398dSHeinrich Schuchardt 101ac1b398dSHeinrich Schuchardt /* 1021da177e4SLinus Torvalds * Protected counters by write_lock_irq(&tasklist_lock) 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds unsigned long total_forks; /* Handle normal Linux uptimes. */ 1051da177e4SLinus Torvalds int nr_threads; /* The idle threads do not count.. */ 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds int max_threads; /* tunable limit on nr_threads */ 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds DEFINE_PER_CPU(unsigned long, process_counts) = 0; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 112db1466b3SPaul E. McKenney 113db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU 114db1466b3SPaul E. McKenney int lockdep_tasklist_lock_is_held(void) 115db1466b3SPaul E. McKenney { 116db1466b3SPaul E. McKenney return lockdep_is_held(&tasklist_lock); 117db1466b3SPaul E. McKenney } 118db1466b3SPaul E. McKenney EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 119db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */ 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds int nr_processes(void) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds int cpu; 1241da177e4SLinus Torvalds int total = 0; 1251da177e4SLinus Torvalds 1261d510750SIan Campbell for_each_possible_cpu(cpu) 1271da177e4SLinus Torvalds total += per_cpu(process_counts, cpu); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds return total; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 132f19b9f74SAkinobu Mita void __weak arch_release_task_struct(struct task_struct *tsk) 133f19b9f74SAkinobu Mita { 134f19b9f74SAkinobu Mita } 135f19b9f74SAkinobu Mita 136f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 137e18b890bSChristoph Lameter static struct kmem_cache *task_struct_cachep; 13841101809SThomas Gleixner 13941101809SThomas Gleixner static inline struct task_struct *alloc_task_struct_node(int node) 14041101809SThomas Gleixner { 14141101809SThomas Gleixner return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 14241101809SThomas Gleixner } 14341101809SThomas Gleixner 14441101809SThomas Gleixner static inline void free_task_struct(struct task_struct *tsk) 14541101809SThomas Gleixner { 14641101809SThomas Gleixner kmem_cache_free(task_struct_cachep, tsk); 14741101809SThomas Gleixner } 1481da177e4SLinus Torvalds #endif 1491da177e4SLinus Torvalds 150f19b9f74SAkinobu Mita void __weak arch_release_thread_info(struct thread_info *ti) 151f19b9f74SAkinobu Mita { 152f19b9f74SAkinobu Mita } 153f19b9f74SAkinobu Mita 154f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR 15541101809SThomas Gleixner 1560d15d74aSThomas Gleixner /* 1570d15d74aSThomas Gleixner * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 1580d15d74aSThomas Gleixner * kmemcache based allocator. 1590d15d74aSThomas Gleixner */ 1600d15d74aSThomas Gleixner # if THREAD_SIZE >= PAGE_SIZE 161b6a84016SEric Dumazet static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 162b6a84016SEric Dumazet int node) 163b69c49b7SFUJITA Tomonori { 16452383431SVladimir Davydov struct page *page = alloc_kmem_pages_node(node, THREADINFO_GFP, 1652889f608SThomas Gleixner THREAD_SIZE_ORDER); 166b6a84016SEric Dumazet 167b6a84016SEric Dumazet return page ? page_address(page) : NULL; 168b69c49b7SFUJITA Tomonori } 169b69c49b7SFUJITA Tomonori 170b69c49b7SFUJITA Tomonori static inline void free_thread_info(struct thread_info *ti) 171b69c49b7SFUJITA Tomonori { 17252383431SVladimir Davydov free_kmem_pages((unsigned long)ti, THREAD_SIZE_ORDER); 173b69c49b7SFUJITA Tomonori } 1740d15d74aSThomas Gleixner # else 1750d15d74aSThomas Gleixner static struct kmem_cache *thread_info_cache; 1760d15d74aSThomas Gleixner 1770d15d74aSThomas Gleixner static struct thread_info *alloc_thread_info_node(struct task_struct *tsk, 1780d15d74aSThomas Gleixner int node) 1790d15d74aSThomas Gleixner { 1800d15d74aSThomas Gleixner return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node); 1810d15d74aSThomas Gleixner } 1820d15d74aSThomas Gleixner 1830d15d74aSThomas Gleixner static void free_thread_info(struct thread_info *ti) 1840d15d74aSThomas Gleixner { 1850d15d74aSThomas Gleixner kmem_cache_free(thread_info_cache, ti); 1860d15d74aSThomas Gleixner } 1870d15d74aSThomas Gleixner 1880d15d74aSThomas Gleixner void thread_info_cache_init(void) 1890d15d74aSThomas Gleixner { 1900d15d74aSThomas Gleixner thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE, 1910d15d74aSThomas Gleixner THREAD_SIZE, 0, NULL); 1920d15d74aSThomas Gleixner BUG_ON(thread_info_cache == NULL); 1930d15d74aSThomas Gleixner } 1940d15d74aSThomas Gleixner # endif 195b69c49b7SFUJITA Tomonori #endif 196b69c49b7SFUJITA Tomonori 1971da177e4SLinus Torvalds /* SLAB cache for signal_struct structures (tsk->signal) */ 198e18b890bSChristoph Lameter static struct kmem_cache *signal_cachep; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* SLAB cache for sighand_struct structures (tsk->sighand) */ 201e18b890bSChristoph Lameter struct kmem_cache *sighand_cachep; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* SLAB cache for files_struct structures (tsk->files) */ 204e18b890bSChristoph Lameter struct kmem_cache *files_cachep; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* SLAB cache for fs_struct structures (tsk->fs) */ 207e18b890bSChristoph Lameter struct kmem_cache *fs_cachep; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds /* SLAB cache for vm_area_struct structures */ 210e18b890bSChristoph Lameter struct kmem_cache *vm_area_cachep; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* SLAB cache for mm_struct structures (tsk->mm) */ 213e18b890bSChristoph Lameter static struct kmem_cache *mm_cachep; 2141da177e4SLinus Torvalds 215c6a7f572SKOSAKI Motohiro static void account_kernel_stack(struct thread_info *ti, int account) 216c6a7f572SKOSAKI Motohiro { 217c6a7f572SKOSAKI Motohiro struct zone *zone = page_zone(virt_to_page(ti)); 218c6a7f572SKOSAKI Motohiro 219c6a7f572SKOSAKI Motohiro mod_zone_page_state(zone, NR_KERNEL_STACK, account); 220c6a7f572SKOSAKI Motohiro } 221c6a7f572SKOSAKI Motohiro 2221da177e4SLinus Torvalds void free_task(struct task_struct *tsk) 2231da177e4SLinus Torvalds { 224c6a7f572SKOSAKI Motohiro account_kernel_stack(tsk->stack, -1); 225f19b9f74SAkinobu Mita arch_release_thread_info(tsk->stack); 226f7e4217bSRoman Zippel free_thread_info(tsk->stack); 22723f78d4aSIngo Molnar rt_mutex_debug_task_free(tsk); 228fb52607aSFrederic Weisbecker ftrace_graph_exit_task(tsk); 229e2cfabdfSWill Drewry put_seccomp_filter(tsk); 230f19b9f74SAkinobu Mita arch_release_task_struct(tsk); 2311da177e4SLinus Torvalds free_task_struct(tsk); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds EXPORT_SYMBOL(free_task); 2341da177e4SLinus Torvalds 235ea6d290cSOleg Nesterov static inline void free_signal_struct(struct signal_struct *sig) 236ea6d290cSOleg Nesterov { 23797101eb4SOleg Nesterov taskstats_tgid_free(sig); 2381c5354deSMike Galbraith sched_autogroup_exit(sig); 239ea6d290cSOleg Nesterov kmem_cache_free(signal_cachep, sig); 240ea6d290cSOleg Nesterov } 241ea6d290cSOleg Nesterov 242ea6d290cSOleg Nesterov static inline void put_signal_struct(struct signal_struct *sig) 243ea6d290cSOleg Nesterov { 2441c5354deSMike Galbraith if (atomic_dec_and_test(&sig->sigcnt)) 245ea6d290cSOleg Nesterov free_signal_struct(sig); 246ea6d290cSOleg Nesterov } 247ea6d290cSOleg Nesterov 248158d9ebdSAndrew Morton void __put_task_struct(struct task_struct *tsk) 2491da177e4SLinus Torvalds { 250270f722dSEugene Teo WARN_ON(!tsk->exit_state); 2511da177e4SLinus Torvalds WARN_ON(atomic_read(&tsk->usage)); 2521da177e4SLinus Torvalds WARN_ON(tsk == current); 2531da177e4SLinus Torvalds 2542e91fa7fSTejun Heo cgroup_free(tsk); 255156654f4SMike Galbraith task_numa_free(tsk); 2561a2a4d06SKees Cook security_task_free(tsk); 257e0e81739SDavid Howells exit_creds(tsk); 25835df17c5SShailabh Nagar delayacct_tsk_free(tsk); 259ea6d290cSOleg Nesterov put_signal_struct(tsk->signal); 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds if (!profile_handoff_task(tsk)) 2621da177e4SLinus Torvalds free_task(tsk); 2631da177e4SLinus Torvalds } 26477c100c8SRik van Riel EXPORT_SYMBOL_GPL(__put_task_struct); 2651da177e4SLinus Torvalds 2666c0a9fa6SThomas Gleixner void __init __weak arch_task_cache_init(void) { } 26761c4628bSSuresh Siddha 268ff691f6eSHeinrich Schuchardt /* 269ff691f6eSHeinrich Schuchardt * set_max_threads 270ff691f6eSHeinrich Schuchardt */ 27116db3d3fSHeinrich Schuchardt static void set_max_threads(unsigned int max_threads_suggested) 272ff691f6eSHeinrich Schuchardt { 273ac1b398dSHeinrich Schuchardt u64 threads; 274ff691f6eSHeinrich Schuchardt 275ff691f6eSHeinrich Schuchardt /* 276ac1b398dSHeinrich Schuchardt * The number of threads shall be limited such that the thread 277ac1b398dSHeinrich Schuchardt * structures may only consume a small part of the available memory. 278ff691f6eSHeinrich Schuchardt */ 279ac1b398dSHeinrich Schuchardt if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64) 280ac1b398dSHeinrich Schuchardt threads = MAX_THREADS; 281ac1b398dSHeinrich Schuchardt else 282ac1b398dSHeinrich Schuchardt threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, 283ac1b398dSHeinrich Schuchardt (u64) THREAD_SIZE * 8UL); 284ac1b398dSHeinrich Schuchardt 28516db3d3fSHeinrich Schuchardt if (threads > max_threads_suggested) 28616db3d3fSHeinrich Schuchardt threads = max_threads_suggested; 28716db3d3fSHeinrich Schuchardt 288ac1b398dSHeinrich Schuchardt max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 289ff691f6eSHeinrich Schuchardt } 290ff691f6eSHeinrich Schuchardt 2915aaeb5c0SIngo Molnar #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 2925aaeb5c0SIngo Molnar /* Initialized by the architecture: */ 2935aaeb5c0SIngo Molnar int arch_task_struct_size __read_mostly; 2945aaeb5c0SIngo Molnar #endif 2950c8c0f03SDave Hansen 296ff691f6eSHeinrich Schuchardt void __init fork_init(void) 2971da177e4SLinus Torvalds { 298f5e10287SThomas Gleixner #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR 2991da177e4SLinus Torvalds #ifndef ARCH_MIN_TASKALIGN 3001da177e4SLinus Torvalds #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES 3011da177e4SLinus Torvalds #endif 3021da177e4SLinus Torvalds /* create a slab on which task_structs can be allocated */ 303*5d097056SVladimir Davydov task_struct_cachep = kmem_cache_create("task_struct", 304*5d097056SVladimir Davydov arch_task_struct_size, ARCH_MIN_TASKALIGN, 305*5d097056SVladimir Davydov SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, NULL); 3061da177e4SLinus Torvalds #endif 3071da177e4SLinus Torvalds 30861c4628bSSuresh Siddha /* do the arch specific task caches init */ 30961c4628bSSuresh Siddha arch_task_cache_init(); 31061c4628bSSuresh Siddha 31116db3d3fSHeinrich Schuchardt set_max_threads(MAX_THREADS); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 3141da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 3151da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_SIGPENDING] = 3161da177e4SLinus Torvalds init_task.signal->rlim[RLIMIT_NPROC]; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 31952f5684cSGideon Israel Dsouza int __weak arch_dup_task_struct(struct task_struct *dst, 32061c4628bSSuresh Siddha struct task_struct *src) 32161c4628bSSuresh Siddha { 32261c4628bSSuresh Siddha *dst = *src; 32361c4628bSSuresh Siddha return 0; 32461c4628bSSuresh Siddha } 32561c4628bSSuresh Siddha 326d4311ff1SAaron Tomlin void set_task_stack_end_magic(struct task_struct *tsk) 327d4311ff1SAaron Tomlin { 328d4311ff1SAaron Tomlin unsigned long *stackend; 329d4311ff1SAaron Tomlin 330d4311ff1SAaron Tomlin stackend = end_of_stack(tsk); 331d4311ff1SAaron Tomlin *stackend = STACK_END_MAGIC; /* for overflow detection */ 332d4311ff1SAaron Tomlin } 333d4311ff1SAaron Tomlin 3341da177e4SLinus Torvalds static struct task_struct *dup_task_struct(struct task_struct *orig) 3351da177e4SLinus Torvalds { 3361da177e4SLinus Torvalds struct task_struct *tsk; 3371da177e4SLinus Torvalds struct thread_info *ti; 338207205a2SEric Dumazet int node = tsk_fork_get_node(orig); 3393e26c149SPeter Zijlstra int err; 3401da177e4SLinus Torvalds 341504f52b5SEric Dumazet tsk = alloc_task_struct_node(node); 3421da177e4SLinus Torvalds if (!tsk) 3431da177e4SLinus Torvalds return NULL; 3441da177e4SLinus Torvalds 345b6a84016SEric Dumazet ti = alloc_thread_info_node(tsk, node); 346f19b9f74SAkinobu Mita if (!ti) 347f19b9f74SAkinobu Mita goto free_tsk; 3481da177e4SLinus Torvalds 34961c4628bSSuresh Siddha err = arch_dup_task_struct(tsk, orig); 35061c4628bSSuresh Siddha if (err) 351f19b9f74SAkinobu Mita goto free_ti; 35261c4628bSSuresh Siddha 35387bec58aSAndrew Morton tsk->stack = ti; 354dbd95212SKees Cook #ifdef CONFIG_SECCOMP 355dbd95212SKees Cook /* 356dbd95212SKees Cook * We must handle setting up seccomp filters once we're under 357dbd95212SKees Cook * the sighand lock in case orig has changed between now and 358dbd95212SKees Cook * then. Until then, filter must be NULL to avoid messing up 359dbd95212SKees Cook * the usage counts on the error path calling free_task. 360dbd95212SKees Cook */ 361dbd95212SKees Cook tsk->seccomp.filter = NULL; 362dbd95212SKees Cook #endif 36387bec58aSAndrew Morton 36487bec58aSAndrew Morton setup_thread_stack(tsk, orig); 3658e7cac79SAvi Kivity clear_user_return_notifier(tsk); 366f26f9affSMike Galbraith clear_tsk_need_resched(tsk); 367d4311ff1SAaron Tomlin set_task_stack_end_magic(tsk); 3681da177e4SLinus Torvalds 3690a425405SArjan van de Ven #ifdef CONFIG_CC_STACKPROTECTOR 3700a425405SArjan van de Ven tsk->stack_canary = get_random_int(); 3710a425405SArjan van de Ven #endif 3720a425405SArjan van de Ven 373fb0a685cSDaniel Rebelo de Oliveira /* 374fb0a685cSDaniel Rebelo de Oliveira * One for us, one for whoever does the "release_task()" (usually 375fb0a685cSDaniel Rebelo de Oliveira * parent) 376fb0a685cSDaniel Rebelo de Oliveira */ 3771da177e4SLinus Torvalds atomic_set(&tsk->usage, 2); 3786c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE 3792056a782SJens Axboe tsk->btrace_seq = 0; 3806c5c9341SAlexey Dobriyan #endif 381a0aa7f68SJens Axboe tsk->splice_pipe = NULL; 3825640f768SEric Dumazet tsk->task_frag.page = NULL; 383093e5840SSebastian Andrzej Siewior tsk->wake_q.next = NULL; 384c6a7f572SKOSAKI Motohiro 385c6a7f572SKOSAKI Motohiro account_kernel_stack(ti, 1); 386c6a7f572SKOSAKI Motohiro 3871da177e4SLinus Torvalds return tsk; 38861c4628bSSuresh Siddha 389f19b9f74SAkinobu Mita free_ti: 39061c4628bSSuresh Siddha free_thread_info(ti); 391f19b9f74SAkinobu Mita free_tsk: 39261c4628bSSuresh Siddha free_task_struct(tsk); 39361c4628bSSuresh Siddha return NULL; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds #ifdef CONFIG_MMU 397a39bc516SAlexey Dobriyan static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 3981da177e4SLinus Torvalds { 399297c5eeeSLinus Torvalds struct vm_area_struct *mpnt, *tmp, *prev, **pprev; 4001da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 4011da177e4SLinus Torvalds int retval; 4021da177e4SLinus Torvalds unsigned long charge; 4031da177e4SLinus Torvalds 40432cdba1eSOleg Nesterov uprobe_start_dup_mmap(); 4051da177e4SLinus Torvalds down_write(&oldmm->mmap_sem); 406ec8c0446SRalf Baechle flush_cache_dup_mm(oldmm); 407f8ac4ec9SOleg Nesterov uprobe_dup_mmap(oldmm, mm); 408ad339451SIngo Molnar /* 409ad339451SIngo Molnar * Not linked in yet - no deadlock potential: 410ad339451SIngo Molnar */ 411ad339451SIngo Molnar down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); 4127ee78232SHugh Dickins 41390f31d0eSKonstantin Khlebnikov /* No ordering required: file already has been exposed. */ 41490f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 41590f31d0eSKonstantin Khlebnikov 4164f7d4614SVladimir Davydov mm->total_vm = oldmm->total_vm; 4174f7d4614SVladimir Davydov mm->shared_vm = oldmm->shared_vm; 4184f7d4614SVladimir Davydov mm->exec_vm = oldmm->exec_vm; 4194f7d4614SVladimir Davydov mm->stack_vm = oldmm->stack_vm; 4204f7d4614SVladimir Davydov 4211da177e4SLinus Torvalds rb_link = &mm->mm_rb.rb_node; 4221da177e4SLinus Torvalds rb_parent = NULL; 4231da177e4SLinus Torvalds pprev = &mm->mmap; 424f8af4da3SHugh Dickins retval = ksm_fork(mm, oldmm); 425f8af4da3SHugh Dickins if (retval) 426f8af4da3SHugh Dickins goto out; 427ba76149fSAndrea Arcangeli retval = khugepaged_fork(mm, oldmm); 428ba76149fSAndrea Arcangeli if (retval) 429ba76149fSAndrea Arcangeli goto out; 4301da177e4SLinus Torvalds 431297c5eeeSLinus Torvalds prev = NULL; 432fd3e42fcSHugh Dickins for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { 4331da177e4SLinus Torvalds struct file *file; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds if (mpnt->vm_flags & VM_DONTCOPY) { 436ab50b8edSHugh Dickins vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file, 43744de9d0cSHuang Shijie -vma_pages(mpnt)); 4381da177e4SLinus Torvalds continue; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds charge = 0; 4411da177e4SLinus Torvalds if (mpnt->vm_flags & VM_ACCOUNT) { 442b2412b7fSHuang Shijie unsigned long len = vma_pages(mpnt); 443b2412b7fSHuang Shijie 444191c5424SAl Viro if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ 4451da177e4SLinus Torvalds goto fail_nomem; 4461da177e4SLinus Torvalds charge = len; 4471da177e4SLinus Torvalds } 448e94b1766SChristoph Lameter tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 4491da177e4SLinus Torvalds if (!tmp) 4501da177e4SLinus Torvalds goto fail_nomem; 4511da177e4SLinus Torvalds *tmp = *mpnt; 4525beb4930SRik van Riel INIT_LIST_HEAD(&tmp->anon_vma_chain); 453ef0855d3SOleg Nesterov retval = vma_dup_policy(mpnt, tmp); 454ef0855d3SOleg Nesterov if (retval) 4551da177e4SLinus Torvalds goto fail_nomem_policy; 456a247c3a9SAndrea Arcangeli tmp->vm_mm = mm; 4575beb4930SRik van Riel if (anon_vma_fork(tmp, mpnt)) 4585beb4930SRik van Riel goto fail_nomem_anon_vma_fork; 459de60f5f1SEric B Munson tmp->vm_flags &= 460de60f5f1SEric B Munson ~(VM_LOCKED|VM_LOCKONFAULT|VM_UFFD_MISSING|VM_UFFD_WP); 461297c5eeeSLinus Torvalds tmp->vm_next = tmp->vm_prev = NULL; 462745f234bSAndrea Arcangeli tmp->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; 4631da177e4SLinus Torvalds file = tmp->vm_file; 4641da177e4SLinus Torvalds if (file) { 465496ad9aaSAl Viro struct inode *inode = file_inode(file); 466b88ed205SHugh Dickins struct address_space *mapping = file->f_mapping; 467b88ed205SHugh Dickins 4681da177e4SLinus Torvalds get_file(file); 4691da177e4SLinus Torvalds if (tmp->vm_flags & VM_DENYWRITE) 4701da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 47183cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 472b88ed205SHugh Dickins if (tmp->vm_flags & VM_SHARED) 4734bb5f5d9SDavid Herrmann atomic_inc(&mapping->i_mmap_writable); 474b88ed205SHugh Dickins flush_dcache_mmap_lock(mapping); 475b88ed205SHugh Dickins /* insert tmp into the share list, just after mpnt */ 4769826a516SMichel Lespinasse vma_interval_tree_insert_after(tmp, mpnt, 4779826a516SMichel Lespinasse &mapping->i_mmap); 478b88ed205SHugh Dickins flush_dcache_mmap_unlock(mapping); 47983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* 483a1e78772SMel Gorman * Clear hugetlb-related page reserves for children. This only 484a1e78772SMel Gorman * affects MAP_PRIVATE mappings. Faults generated by the child 485a1e78772SMel Gorman * are not guaranteed to succeed, even if read-only 486a1e78772SMel Gorman */ 487a1e78772SMel Gorman if (is_vm_hugetlb_page(tmp)) 488a1e78772SMel Gorman reset_vma_resv_huge_pages(tmp); 489a1e78772SMel Gorman 490a1e78772SMel Gorman /* 4917ee78232SHugh Dickins * Link in the new vma and copy the page table entries. 4921da177e4SLinus Torvalds */ 4931da177e4SLinus Torvalds *pprev = tmp; 4941da177e4SLinus Torvalds pprev = &tmp->vm_next; 495297c5eeeSLinus Torvalds tmp->vm_prev = prev; 496297c5eeeSLinus Torvalds prev = tmp; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds __vma_link_rb(mm, tmp, rb_link, rb_parent); 4991da177e4SLinus Torvalds rb_link = &tmp->vm_rb.rb_right; 5001da177e4SLinus Torvalds rb_parent = &tmp->vm_rb; 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds mm->map_count++; 5030b0db14cSHugh Dickins retval = copy_page_range(mm, oldmm, mpnt); 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds if (tmp->vm_ops && tmp->vm_ops->open) 5061da177e4SLinus Torvalds tmp->vm_ops->open(tmp); 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds if (retval) 5091da177e4SLinus Torvalds goto out; 5101da177e4SLinus Torvalds } 511d6dd61c8SJeremy Fitzhardinge /* a new mm has just been created */ 512d6dd61c8SJeremy Fitzhardinge arch_dup_mmap(oldmm, mm); 5131da177e4SLinus Torvalds retval = 0; 5141da177e4SLinus Torvalds out: 5157ee78232SHugh Dickins up_write(&mm->mmap_sem); 516fd3e42fcSHugh Dickins flush_tlb_mm(oldmm); 5171da177e4SLinus Torvalds up_write(&oldmm->mmap_sem); 51832cdba1eSOleg Nesterov uprobe_end_dup_mmap(); 5191da177e4SLinus Torvalds return retval; 5205beb4930SRik van Riel fail_nomem_anon_vma_fork: 521ef0855d3SOleg Nesterov mpol_put(vma_policy(tmp)); 5221da177e4SLinus Torvalds fail_nomem_policy: 5231da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, tmp); 5241da177e4SLinus Torvalds fail_nomem: 5251da177e4SLinus Torvalds retval = -ENOMEM; 5261da177e4SLinus Torvalds vm_unacct_memory(charge); 5271da177e4SLinus Torvalds goto out; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds static inline int mm_alloc_pgd(struct mm_struct *mm) 5311da177e4SLinus Torvalds { 5321da177e4SLinus Torvalds mm->pgd = pgd_alloc(mm); 5331da177e4SLinus Torvalds if (unlikely(!mm->pgd)) 5341da177e4SLinus Torvalds return -ENOMEM; 5351da177e4SLinus Torvalds return 0; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds static inline void mm_free_pgd(struct mm_struct *mm) 5391da177e4SLinus Torvalds { 5405e541973SBenjamin Herrenschmidt pgd_free(mm, mm->pgd); 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds #else 54390f31d0eSKonstantin Khlebnikov static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) 54490f31d0eSKonstantin Khlebnikov { 54590f31d0eSKonstantin Khlebnikov down_write(&oldmm->mmap_sem); 54690f31d0eSKonstantin Khlebnikov RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); 54790f31d0eSKonstantin Khlebnikov up_write(&oldmm->mmap_sem); 54890f31d0eSKonstantin Khlebnikov return 0; 54990f31d0eSKonstantin Khlebnikov } 5501da177e4SLinus Torvalds #define mm_alloc_pgd(mm) (0) 5511da177e4SLinus Torvalds #define mm_free_pgd(mm) 5521da177e4SLinus Torvalds #endif /* CONFIG_MMU */ 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 5551da177e4SLinus Torvalds 556e94b1766SChristoph Lameter #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 5571da177e4SLinus Torvalds #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 5581da177e4SLinus Torvalds 5594cb0e11bSHidehiro Kawai static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; 5604cb0e11bSHidehiro Kawai 5614cb0e11bSHidehiro Kawai static int __init coredump_filter_setup(char *s) 5624cb0e11bSHidehiro Kawai { 5634cb0e11bSHidehiro Kawai default_dump_filter = 5644cb0e11bSHidehiro Kawai (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & 5654cb0e11bSHidehiro Kawai MMF_DUMP_FILTER_MASK; 5664cb0e11bSHidehiro Kawai return 1; 5674cb0e11bSHidehiro Kawai } 5684cb0e11bSHidehiro Kawai 5694cb0e11bSHidehiro Kawai __setup("coredump_filter=", coredump_filter_setup); 5704cb0e11bSHidehiro Kawai 5711da177e4SLinus Torvalds #include <linux/init_task.h> 5721da177e4SLinus Torvalds 573858f0993SAlexey Dobriyan static void mm_init_aio(struct mm_struct *mm) 574858f0993SAlexey Dobriyan { 575858f0993SAlexey Dobriyan #ifdef CONFIG_AIO 576858f0993SAlexey Dobriyan spin_lock_init(&mm->ioctx_lock); 577db446a08SBenjamin LaHaise mm->ioctx_table = NULL; 578858f0993SAlexey Dobriyan #endif 579858f0993SAlexey Dobriyan } 580858f0993SAlexey Dobriyan 58133144e84SVladimir Davydov static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 58233144e84SVladimir Davydov { 58333144e84SVladimir Davydov #ifdef CONFIG_MEMCG 58433144e84SVladimir Davydov mm->owner = p; 58533144e84SVladimir Davydov #endif 58633144e84SVladimir Davydov } 58733144e84SVladimir Davydov 58878fb7466SPavel Emelianov static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 5891da177e4SLinus Torvalds { 59041f727fdSVladimir Davydov mm->mmap = NULL; 59141f727fdSVladimir Davydov mm->mm_rb = RB_ROOT; 59241f727fdSVladimir Davydov mm->vmacache_seqnum = 0; 5931da177e4SLinus Torvalds atomic_set(&mm->mm_users, 1); 5941da177e4SLinus Torvalds atomic_set(&mm->mm_count, 1); 5951da177e4SLinus Torvalds init_rwsem(&mm->mmap_sem); 5961da177e4SLinus Torvalds INIT_LIST_HEAD(&mm->mmlist); 597999d9fc1SOleg Nesterov mm->core_state = NULL; 598e1f56c89SKirill A. Shutemov atomic_long_set(&mm->nr_ptes, 0); 5992d2f5119SKirill A. Shutemov mm_nr_pmds_init(mm); 60041f727fdSVladimir Davydov mm->map_count = 0; 60141f727fdSVladimir Davydov mm->locked_vm = 0; 602ce65cefaSVladimir Davydov mm->pinned_vm = 0; 603d559db08SKAMEZAWA Hiroyuki memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 6041da177e4SLinus Torvalds spin_lock_init(&mm->page_table_lock); 60541f727fdSVladimir Davydov mm_init_cpumask(mm); 606858f0993SAlexey Dobriyan mm_init_aio(mm); 607cf475ad2SBalbir Singh mm_init_owner(mm, p); 60841f727fdSVladimir Davydov mmu_notifier_mm_init(mm); 60920841405SRik van Riel clear_tlb_flush_pending(mm); 61041f727fdSVladimir Davydov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 61141f727fdSVladimir Davydov mm->pmd_huge_pte = NULL; 61241f727fdSVladimir Davydov #endif 6131da177e4SLinus Torvalds 614a0715cc2SAlex Thorlton if (current->mm) { 615a0715cc2SAlex Thorlton mm->flags = current->mm->flags & MMF_INIT_MASK; 616a0715cc2SAlex Thorlton mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 617a0715cc2SAlex Thorlton } else { 618a0715cc2SAlex Thorlton mm->flags = default_dump_filter; 6191da177e4SLinus Torvalds mm->def_flags = 0; 620a0715cc2SAlex Thorlton } 621a0715cc2SAlex Thorlton 62241f727fdSVladimir Davydov if (mm_alloc_pgd(mm)) 62341f727fdSVladimir Davydov goto fail_nopgd; 62478fb7466SPavel Emelianov 62541f727fdSVladimir Davydov if (init_new_context(p, mm)) 62641f727fdSVladimir Davydov goto fail_nocontext; 62741f727fdSVladimir Davydov 62841f727fdSVladimir Davydov return mm; 62941f727fdSVladimir Davydov 63041f727fdSVladimir Davydov fail_nocontext: 63141f727fdSVladimir Davydov mm_free_pgd(mm); 63241f727fdSVladimir Davydov fail_nopgd: 6331da177e4SLinus Torvalds free_mm(mm); 6341da177e4SLinus Torvalds return NULL; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 637c3f0327fSKonstantin Khlebnikov static void check_mm(struct mm_struct *mm) 638c3f0327fSKonstantin Khlebnikov { 639c3f0327fSKonstantin Khlebnikov int i; 640c3f0327fSKonstantin Khlebnikov 641c3f0327fSKonstantin Khlebnikov for (i = 0; i < NR_MM_COUNTERS; i++) { 642c3f0327fSKonstantin Khlebnikov long x = atomic_long_read(&mm->rss_stat.count[i]); 643c3f0327fSKonstantin Khlebnikov 644c3f0327fSKonstantin Khlebnikov if (unlikely(x)) 645c3f0327fSKonstantin Khlebnikov printk(KERN_ALERT "BUG: Bad rss-counter state " 646c3f0327fSKonstantin Khlebnikov "mm:%p idx:%d val:%ld\n", mm, i, x); 647c3f0327fSKonstantin Khlebnikov } 648b30fe6c7SKirill A. Shutemov 649b30fe6c7SKirill A. Shutemov if (atomic_long_read(&mm->nr_ptes)) 650b30fe6c7SKirill A. Shutemov pr_alert("BUG: non-zero nr_ptes on freeing mm: %ld\n", 651b30fe6c7SKirill A. Shutemov atomic_long_read(&mm->nr_ptes)); 652b30fe6c7SKirill A. Shutemov if (mm_nr_pmds(mm)) 653b30fe6c7SKirill A. Shutemov pr_alert("BUG: non-zero nr_pmds on freeing mm: %ld\n", 654b30fe6c7SKirill A. Shutemov mm_nr_pmds(mm)); 655b30fe6c7SKirill A. Shutemov 656e009bb30SKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS 65796dad67fSSasha Levin VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 658c3f0327fSKonstantin Khlebnikov #endif 659c3f0327fSKonstantin Khlebnikov } 660c3f0327fSKonstantin Khlebnikov 6611da177e4SLinus Torvalds /* 6621da177e4SLinus Torvalds * Allocate and initialize an mm_struct. 6631da177e4SLinus Torvalds */ 6641da177e4SLinus Torvalds struct mm_struct *mm_alloc(void) 6651da177e4SLinus Torvalds { 6661da177e4SLinus Torvalds struct mm_struct *mm; 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds mm = allocate_mm(); 669de03c72cSKOSAKI Motohiro if (!mm) 670de03c72cSKOSAKI Motohiro return NULL; 671de03c72cSKOSAKI Motohiro 6721da177e4SLinus Torvalds memset(mm, 0, sizeof(*mm)); 6736345d24dSLinus Torvalds return mm_init(mm, current); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 6771da177e4SLinus Torvalds * Called when the last reference to the mm 6781da177e4SLinus Torvalds * is dropped: either by a lazy thread or by 6791da177e4SLinus Torvalds * mmput. Free the page directory and the mm. 6801da177e4SLinus Torvalds */ 6817ad5b3a5SHarvey Harrison void __mmdrop(struct mm_struct *mm) 6821da177e4SLinus Torvalds { 6831da177e4SLinus Torvalds BUG_ON(mm == &init_mm); 6841da177e4SLinus Torvalds mm_free_pgd(mm); 6851da177e4SLinus Torvalds destroy_context(mm); 686cddb8a5cSAndrea Arcangeli mmu_notifier_mm_destroy(mm); 687c3f0327fSKonstantin Khlebnikov check_mm(mm); 6881da177e4SLinus Torvalds free_mm(mm); 6891da177e4SLinus Torvalds } 6906d4e4c4fSAvi Kivity EXPORT_SYMBOL_GPL(__mmdrop); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* 6931da177e4SLinus Torvalds * Decrement the use count and release all resources for an mm. 6941da177e4SLinus Torvalds */ 6951da177e4SLinus Torvalds void mmput(struct mm_struct *mm) 6961da177e4SLinus Torvalds { 6970ae26f1bSAndrew Morton might_sleep(); 6980ae26f1bSAndrew Morton 6991da177e4SLinus Torvalds if (atomic_dec_and_test(&mm->mm_users)) { 700d4b3b638SSrikar Dronamraju uprobe_clear_state(mm); 7011da177e4SLinus Torvalds exit_aio(mm); 7021c2fb7a4SAndrea Arcangeli ksm_exit(mm); 703ba76149fSAndrea Arcangeli khugepaged_exit(mm); /* must run before exit_mmap */ 7041da177e4SLinus Torvalds exit_mmap(mm); 705925d1c40SMatt Helsley set_mm_exe_file(mm, NULL); 7061da177e4SLinus Torvalds if (!list_empty(&mm->mmlist)) { 7071da177e4SLinus Torvalds spin_lock(&mmlist_lock); 7081da177e4SLinus Torvalds list_del(&mm->mmlist); 7091da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7101da177e4SLinus Torvalds } 711801460d0SHiroshi Shimamoto if (mm->binfmt) 712801460d0SHiroshi Shimamoto module_put(mm->binfmt->module); 7131da177e4SLinus Torvalds mmdrop(mm); 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mmput); 7171da177e4SLinus Torvalds 71890f31d0eSKonstantin Khlebnikov /** 71990f31d0eSKonstantin Khlebnikov * set_mm_exe_file - change a reference to the mm's executable file 72090f31d0eSKonstantin Khlebnikov * 72190f31d0eSKonstantin Khlebnikov * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 72290f31d0eSKonstantin Khlebnikov * 7236e399cd1SDavidlohr Bueso * Main users are mmput() and sys_execve(). Callers prevent concurrent 7246e399cd1SDavidlohr Bueso * invocations: in mmput() nobody alive left, in execve task is single 7256e399cd1SDavidlohr Bueso * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the 7266e399cd1SDavidlohr Bueso * mm->exe_file, but does so without using set_mm_exe_file() in order 7276e399cd1SDavidlohr Bueso * to do avoid the need for any locks. 72890f31d0eSKonstantin Khlebnikov */ 72938646013SJiri Slaby void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 73038646013SJiri Slaby { 7316e399cd1SDavidlohr Bueso struct file *old_exe_file; 7326e399cd1SDavidlohr Bueso 7336e399cd1SDavidlohr Bueso /* 7346e399cd1SDavidlohr Bueso * It is safe to dereference the exe_file without RCU as 7356e399cd1SDavidlohr Bueso * this function is only called if nobody else can access 7366e399cd1SDavidlohr Bueso * this mm -- see comment above for justification. 7376e399cd1SDavidlohr Bueso */ 7386e399cd1SDavidlohr Bueso old_exe_file = rcu_dereference_raw(mm->exe_file); 73990f31d0eSKonstantin Khlebnikov 74038646013SJiri Slaby if (new_exe_file) 74138646013SJiri Slaby get_file(new_exe_file); 74290f31d0eSKonstantin Khlebnikov rcu_assign_pointer(mm->exe_file, new_exe_file); 74390f31d0eSKonstantin Khlebnikov if (old_exe_file) 74490f31d0eSKonstantin Khlebnikov fput(old_exe_file); 74538646013SJiri Slaby } 74638646013SJiri Slaby 74790f31d0eSKonstantin Khlebnikov /** 74890f31d0eSKonstantin Khlebnikov * get_mm_exe_file - acquire a reference to the mm's executable file 74990f31d0eSKonstantin Khlebnikov * 75090f31d0eSKonstantin Khlebnikov * Returns %NULL if mm has no associated executable file. 75190f31d0eSKonstantin Khlebnikov * User must release file via fput(). 75290f31d0eSKonstantin Khlebnikov */ 75338646013SJiri Slaby struct file *get_mm_exe_file(struct mm_struct *mm) 75438646013SJiri Slaby { 75538646013SJiri Slaby struct file *exe_file; 75638646013SJiri Slaby 75790f31d0eSKonstantin Khlebnikov rcu_read_lock(); 75890f31d0eSKonstantin Khlebnikov exe_file = rcu_dereference(mm->exe_file); 75990f31d0eSKonstantin Khlebnikov if (exe_file && !get_file_rcu(exe_file)) 76090f31d0eSKonstantin Khlebnikov exe_file = NULL; 76190f31d0eSKonstantin Khlebnikov rcu_read_unlock(); 76238646013SJiri Slaby return exe_file; 76338646013SJiri Slaby } 76411163348SDavidlohr Bueso EXPORT_SYMBOL(get_mm_exe_file); 76538646013SJiri Slaby 7661da177e4SLinus Torvalds /** 7671da177e4SLinus Torvalds * get_task_mm - acquire a reference to the task's mm 7681da177e4SLinus Torvalds * 769246bb0b1SOleg Nesterov * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 7701da177e4SLinus Torvalds * this kernel workthread has transiently adopted a user mm with use_mm, 7711da177e4SLinus Torvalds * to do its AIO) is not set and if so returns a reference to it, after 7721da177e4SLinus Torvalds * bumping up the use count. User must release the mm via mmput() 7731da177e4SLinus Torvalds * after use. Typically used by /proc and ptrace. 7741da177e4SLinus Torvalds */ 7751da177e4SLinus Torvalds struct mm_struct *get_task_mm(struct task_struct *task) 7761da177e4SLinus Torvalds { 7771da177e4SLinus Torvalds struct mm_struct *mm; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds task_lock(task); 7801da177e4SLinus Torvalds mm = task->mm; 7811da177e4SLinus Torvalds if (mm) { 782246bb0b1SOleg Nesterov if (task->flags & PF_KTHREAD) 7831da177e4SLinus Torvalds mm = NULL; 7841da177e4SLinus Torvalds else 7851da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds task_unlock(task); 7881da177e4SLinus Torvalds return mm; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_task_mm); 7911da177e4SLinus Torvalds 7928cdb878dSChristopher Yeoh struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 7938cdb878dSChristopher Yeoh { 7948cdb878dSChristopher Yeoh struct mm_struct *mm; 7958cdb878dSChristopher Yeoh int err; 7968cdb878dSChristopher Yeoh 7978cdb878dSChristopher Yeoh err = mutex_lock_killable(&task->signal->cred_guard_mutex); 7988cdb878dSChristopher Yeoh if (err) 7998cdb878dSChristopher Yeoh return ERR_PTR(err); 8008cdb878dSChristopher Yeoh 8018cdb878dSChristopher Yeoh mm = get_task_mm(task); 8028cdb878dSChristopher Yeoh if (mm && mm != current->mm && 8038cdb878dSChristopher Yeoh !ptrace_may_access(task, mode)) { 8048cdb878dSChristopher Yeoh mmput(mm); 8058cdb878dSChristopher Yeoh mm = ERR_PTR(-EACCES); 8068cdb878dSChristopher Yeoh } 8078cdb878dSChristopher Yeoh mutex_unlock(&task->signal->cred_guard_mutex); 8088cdb878dSChristopher Yeoh 8098cdb878dSChristopher Yeoh return mm; 8108cdb878dSChristopher Yeoh } 8118cdb878dSChristopher Yeoh 81257b59c4aSOleg Nesterov static void complete_vfork_done(struct task_struct *tsk) 813c415c3b4SOleg Nesterov { 814d68b46feSOleg Nesterov struct completion *vfork; 815c415c3b4SOleg Nesterov 816d68b46feSOleg Nesterov task_lock(tsk); 817d68b46feSOleg Nesterov vfork = tsk->vfork_done; 818d68b46feSOleg Nesterov if (likely(vfork)) { 819c415c3b4SOleg Nesterov tsk->vfork_done = NULL; 820d68b46feSOleg Nesterov complete(vfork); 821d68b46feSOleg Nesterov } 822d68b46feSOleg Nesterov task_unlock(tsk); 823d68b46feSOleg Nesterov } 824d68b46feSOleg Nesterov 825d68b46feSOleg Nesterov static int wait_for_vfork_done(struct task_struct *child, 826d68b46feSOleg Nesterov struct completion *vfork) 827d68b46feSOleg Nesterov { 828d68b46feSOleg Nesterov int killed; 829d68b46feSOleg Nesterov 830d68b46feSOleg Nesterov freezer_do_not_count(); 831d68b46feSOleg Nesterov killed = wait_for_completion_killable(vfork); 832d68b46feSOleg Nesterov freezer_count(); 833d68b46feSOleg Nesterov 834d68b46feSOleg Nesterov if (killed) { 835d68b46feSOleg Nesterov task_lock(child); 836d68b46feSOleg Nesterov child->vfork_done = NULL; 837d68b46feSOleg Nesterov task_unlock(child); 838d68b46feSOleg Nesterov } 839d68b46feSOleg Nesterov 840d68b46feSOleg Nesterov put_task_struct(child); 841d68b46feSOleg Nesterov return killed; 842c415c3b4SOleg Nesterov } 843c415c3b4SOleg Nesterov 8441da177e4SLinus Torvalds /* Please note the differences between mmput and mm_release. 8451da177e4SLinus Torvalds * mmput is called whenever we stop holding onto a mm_struct, 8461da177e4SLinus Torvalds * error success whatever. 8471da177e4SLinus Torvalds * 8481da177e4SLinus Torvalds * mm_release is called after a mm_struct has been removed 8491da177e4SLinus Torvalds * from the current process. 8501da177e4SLinus Torvalds * 8511da177e4SLinus Torvalds * This difference is important for error handling, when we 8521da177e4SLinus Torvalds * only half set up a mm_struct for a new process and need to restore 8531da177e4SLinus Torvalds * the old one. Because we mmput the new mm_struct before 8541da177e4SLinus Torvalds * restoring the old one. . . 8551da177e4SLinus Torvalds * Eric Biederman 10 January 1998 8561da177e4SLinus Torvalds */ 8571da177e4SLinus Torvalds void mm_release(struct task_struct *tsk, struct mm_struct *mm) 8581da177e4SLinus Torvalds { 8598141c7f3SLinus Torvalds /* Get rid of any futexes when releasing the mm */ 8608141c7f3SLinus Torvalds #ifdef CONFIG_FUTEX 861fc6b177dSPeter Zijlstra if (unlikely(tsk->robust_list)) { 8628141c7f3SLinus Torvalds exit_robust_list(tsk); 863fc6b177dSPeter Zijlstra tsk->robust_list = NULL; 864fc6b177dSPeter Zijlstra } 8658141c7f3SLinus Torvalds #ifdef CONFIG_COMPAT 866fc6b177dSPeter Zijlstra if (unlikely(tsk->compat_robust_list)) { 8678141c7f3SLinus Torvalds compat_exit_robust_list(tsk); 868fc6b177dSPeter Zijlstra tsk->compat_robust_list = NULL; 869fc6b177dSPeter Zijlstra } 8708141c7f3SLinus Torvalds #endif 871322a2c10SThomas Gleixner if (unlikely(!list_empty(&tsk->pi_state_list))) 872322a2c10SThomas Gleixner exit_pi_state_list(tsk); 8738141c7f3SLinus Torvalds #endif 8748141c7f3SLinus Torvalds 8750326f5a9SSrikar Dronamraju uprobe_free_utask(tsk); 8760326f5a9SSrikar Dronamraju 8771da177e4SLinus Torvalds /* Get rid of any cached register state */ 8781da177e4SLinus Torvalds deactivate_mm(tsk, mm); 8791da177e4SLinus Torvalds 880fec1d011SRoland McGrath /* 881fec1d011SRoland McGrath * If we're exiting normally, clear a user-space tid field if 882fec1d011SRoland McGrath * requested. We leave this alone when dying by signal, to leave 883fec1d011SRoland McGrath * the value intact in a core dump, and to save the unnecessary 884d68b46feSOleg Nesterov * trouble, say, a killed vfork parent shouldn't touch this mm. 885d68b46feSOleg Nesterov * Userland only wants this done for a sys_exit. 886fec1d011SRoland McGrath */ 8879c8a8228SEric Dumazet if (tsk->clear_child_tid) { 8889c8a8228SEric Dumazet if (!(tsk->flags & PF_SIGNALED) && 8899c8a8228SEric Dumazet atomic_read(&mm->mm_users) > 1) { 8901da177e4SLinus Torvalds /* 8911da177e4SLinus Torvalds * We don't check the error code - if userspace has 8921da177e4SLinus Torvalds * not set up a proper pointer then tough luck. 8931da177e4SLinus Torvalds */ 8949c8a8228SEric Dumazet put_user(0, tsk->clear_child_tid); 8959c8a8228SEric Dumazet sys_futex(tsk->clear_child_tid, FUTEX_WAKE, 8969c8a8228SEric Dumazet 1, NULL, NULL, 0); 8979c8a8228SEric Dumazet } 8989c8a8228SEric Dumazet tsk->clear_child_tid = NULL; 8991da177e4SLinus Torvalds } 900f7505d64SKonstantin Khlebnikov 901f7505d64SKonstantin Khlebnikov /* 902f7505d64SKonstantin Khlebnikov * All done, finally we can wake up parent and return this mm to him. 903f7505d64SKonstantin Khlebnikov * Also kthread_stop() uses this completion for synchronization. 904f7505d64SKonstantin Khlebnikov */ 905f7505d64SKonstantin Khlebnikov if (tsk->vfork_done) 906f7505d64SKonstantin Khlebnikov complete_vfork_done(tsk); 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 909a0a7ec30SJANAK DESAI /* 910a0a7ec30SJANAK DESAI * Allocate a new mm structure and copy contents from the 911a0a7ec30SJANAK DESAI * mm structure of the passed in task structure. 912a0a7ec30SJANAK DESAI */ 913ff252c1fSDaeSeok Youn static struct mm_struct *dup_mm(struct task_struct *tsk) 914a0a7ec30SJANAK DESAI { 915a0a7ec30SJANAK DESAI struct mm_struct *mm, *oldmm = current->mm; 916a0a7ec30SJANAK DESAI int err; 917a0a7ec30SJANAK DESAI 918a0a7ec30SJANAK DESAI mm = allocate_mm(); 919a0a7ec30SJANAK DESAI if (!mm) 920a0a7ec30SJANAK DESAI goto fail_nomem; 921a0a7ec30SJANAK DESAI 922a0a7ec30SJANAK DESAI memcpy(mm, oldmm, sizeof(*mm)); 923a0a7ec30SJANAK DESAI 92478fb7466SPavel Emelianov if (!mm_init(mm, tsk)) 925a0a7ec30SJANAK DESAI goto fail_nomem; 926a0a7ec30SJANAK DESAI 927a0a7ec30SJANAK DESAI err = dup_mmap(mm, oldmm); 928a0a7ec30SJANAK DESAI if (err) 929a0a7ec30SJANAK DESAI goto free_pt; 930a0a7ec30SJANAK DESAI 931a0a7ec30SJANAK DESAI mm->hiwater_rss = get_mm_rss(mm); 932a0a7ec30SJANAK DESAI mm->hiwater_vm = mm->total_vm; 933a0a7ec30SJANAK DESAI 934801460d0SHiroshi Shimamoto if (mm->binfmt && !try_module_get(mm->binfmt->module)) 935801460d0SHiroshi Shimamoto goto free_pt; 936801460d0SHiroshi Shimamoto 937a0a7ec30SJANAK DESAI return mm; 938a0a7ec30SJANAK DESAI 939a0a7ec30SJANAK DESAI free_pt: 940801460d0SHiroshi Shimamoto /* don't put binfmt in mmput, we haven't got module yet */ 941801460d0SHiroshi Shimamoto mm->binfmt = NULL; 942a0a7ec30SJANAK DESAI mmput(mm); 943a0a7ec30SJANAK DESAI 944a0a7ec30SJANAK DESAI fail_nomem: 945a0a7ec30SJANAK DESAI return NULL; 946a0a7ec30SJANAK DESAI } 947a0a7ec30SJANAK DESAI 9481da177e4SLinus Torvalds static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) 9491da177e4SLinus Torvalds { 9501da177e4SLinus Torvalds struct mm_struct *mm, *oldmm; 9511da177e4SLinus Torvalds int retval; 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds tsk->min_flt = tsk->maj_flt = 0; 9541da177e4SLinus Torvalds tsk->nvcsw = tsk->nivcsw = 0; 95517406b82SMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK 95617406b82SMandeep Singh Baines tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 95717406b82SMandeep Singh Baines #endif 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds tsk->mm = NULL; 9601da177e4SLinus Torvalds tsk->active_mm = NULL; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds /* 9631da177e4SLinus Torvalds * Are we cloning a kernel thread? 9641da177e4SLinus Torvalds * 9651da177e4SLinus Torvalds * We need to steal a active VM for that.. 9661da177e4SLinus Torvalds */ 9671da177e4SLinus Torvalds oldmm = current->mm; 9681da177e4SLinus Torvalds if (!oldmm) 9691da177e4SLinus Torvalds return 0; 9701da177e4SLinus Torvalds 971615d6e87SDavidlohr Bueso /* initialize the new vmacache entries */ 972615d6e87SDavidlohr Bueso vmacache_flush(tsk); 973615d6e87SDavidlohr Bueso 9741da177e4SLinus Torvalds if (clone_flags & CLONE_VM) { 9751da177e4SLinus Torvalds atomic_inc(&oldmm->mm_users); 9761da177e4SLinus Torvalds mm = oldmm; 9771da177e4SLinus Torvalds goto good_mm; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds retval = -ENOMEM; 981a0a7ec30SJANAK DESAI mm = dup_mm(tsk); 9821da177e4SLinus Torvalds if (!mm) 9831da177e4SLinus Torvalds goto fail_nomem; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds good_mm: 9861da177e4SLinus Torvalds tsk->mm = mm; 9871da177e4SLinus Torvalds tsk->active_mm = mm; 9881da177e4SLinus Torvalds return 0; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds fail_nomem: 9911da177e4SLinus Torvalds return retval; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 994a39bc516SAlexey Dobriyan static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 9951da177e4SLinus Torvalds { 996498052bbSAl Viro struct fs_struct *fs = current->fs; 9971da177e4SLinus Torvalds if (clone_flags & CLONE_FS) { 998498052bbSAl Viro /* tsk->fs is already what we want */ 9992a4419b5SNick Piggin spin_lock(&fs->lock); 1000498052bbSAl Viro if (fs->in_exec) { 10012a4419b5SNick Piggin spin_unlock(&fs->lock); 1002498052bbSAl Viro return -EAGAIN; 1003498052bbSAl Viro } 1004498052bbSAl Viro fs->users++; 10052a4419b5SNick Piggin spin_unlock(&fs->lock); 10061da177e4SLinus Torvalds return 0; 10071da177e4SLinus Torvalds } 1008498052bbSAl Viro tsk->fs = copy_fs_struct(fs); 10091da177e4SLinus Torvalds if (!tsk->fs) 10101da177e4SLinus Torvalds return -ENOMEM; 10111da177e4SLinus Torvalds return 0; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 1014a016f338SJANAK DESAI static int copy_files(unsigned long clone_flags, struct task_struct *tsk) 1015a016f338SJANAK DESAI { 1016a016f338SJANAK DESAI struct files_struct *oldf, *newf; 1017a016f338SJANAK DESAI int error = 0; 1018a016f338SJANAK DESAI 1019a016f338SJANAK DESAI /* 1020a016f338SJANAK DESAI * A background process may not have any files ... 1021a016f338SJANAK DESAI */ 1022a016f338SJANAK DESAI oldf = current->files; 1023a016f338SJANAK DESAI if (!oldf) 1024a016f338SJANAK DESAI goto out; 1025a016f338SJANAK DESAI 1026a016f338SJANAK DESAI if (clone_flags & CLONE_FILES) { 1027a016f338SJANAK DESAI atomic_inc(&oldf->count); 1028a016f338SJANAK DESAI goto out; 1029a016f338SJANAK DESAI } 1030a016f338SJANAK DESAI 1031a016f338SJANAK DESAI newf = dup_fd(oldf, &error); 1032a016f338SJANAK DESAI if (!newf) 1033a016f338SJANAK DESAI goto out; 1034a016f338SJANAK DESAI 1035a016f338SJANAK DESAI tsk->files = newf; 1036a016f338SJANAK DESAI error = 0; 1037a016f338SJANAK DESAI out: 1038a016f338SJANAK DESAI return error; 1039a016f338SJANAK DESAI } 1040a016f338SJANAK DESAI 1041fadad878SJens Axboe static int copy_io(unsigned long clone_flags, struct task_struct *tsk) 1042fd0928dfSJens Axboe { 1043fd0928dfSJens Axboe #ifdef CONFIG_BLOCK 1044fd0928dfSJens Axboe struct io_context *ioc = current->io_context; 10456e736be7STejun Heo struct io_context *new_ioc; 1046fd0928dfSJens Axboe 1047fd0928dfSJens Axboe if (!ioc) 1048fd0928dfSJens Axboe return 0; 1049fadad878SJens Axboe /* 1050fadad878SJens Axboe * Share io context with parent, if CLONE_IO is set 1051fadad878SJens Axboe */ 1052fadad878SJens Axboe if (clone_flags & CLONE_IO) { 10533d48749dSTejun Heo ioc_task_link(ioc); 10543d48749dSTejun Heo tsk->io_context = ioc; 1055fadad878SJens Axboe } else if (ioprio_valid(ioc->ioprio)) { 10566e736be7STejun Heo new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); 10576e736be7STejun Heo if (unlikely(!new_ioc)) 1058fd0928dfSJens Axboe return -ENOMEM; 1059fd0928dfSJens Axboe 10606e736be7STejun Heo new_ioc->ioprio = ioc->ioprio; 106111a3122fSTejun Heo put_io_context(new_ioc); 1062fd0928dfSJens Axboe } 1063fd0928dfSJens Axboe #endif 1064fd0928dfSJens Axboe return 0; 1065fd0928dfSJens Axboe } 1066fd0928dfSJens Axboe 1067a39bc516SAlexey Dobriyan static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) 10681da177e4SLinus Torvalds { 10691da177e4SLinus Torvalds struct sighand_struct *sig; 10701da177e4SLinus Torvalds 107160348802SZhaolei if (clone_flags & CLONE_SIGHAND) { 10721da177e4SLinus Torvalds atomic_inc(¤t->sighand->count); 10731da177e4SLinus Torvalds return 0; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 1076e56d0903SIngo Molnar rcu_assign_pointer(tsk->sighand, sig); 10771da177e4SLinus Torvalds if (!sig) 10781da177e4SLinus Torvalds return -ENOMEM; 10799d7fb042SPeter Zijlstra 10801da177e4SLinus Torvalds atomic_set(&sig->count, 1); 10811da177e4SLinus Torvalds memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 10821da177e4SLinus Torvalds return 0; 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 1085a7e5328aSOleg Nesterov void __cleanup_sighand(struct sighand_struct *sighand) 1086c81addc9SOleg Nesterov { 1087d80e731eSOleg Nesterov if (atomic_dec_and_test(&sighand->count)) { 1088d80e731eSOleg Nesterov signalfd_cleanup(sighand); 1089392809b2SOleg Nesterov /* 1090392809b2SOleg Nesterov * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it 1091392809b2SOleg Nesterov * without an RCU grace period, see __lock_task_sighand(). 1092392809b2SOleg Nesterov */ 1093c81addc9SOleg Nesterov kmem_cache_free(sighand_cachep, sighand); 1094c81addc9SOleg Nesterov } 1095d80e731eSOleg Nesterov } 1096c81addc9SOleg Nesterov 1097f06febc9SFrank Mayhar /* 1098f06febc9SFrank Mayhar * Initialize POSIX timer handling for a thread group. 1099f06febc9SFrank Mayhar */ 1100f06febc9SFrank Mayhar static void posix_cpu_timers_init_group(struct signal_struct *sig) 1101f06febc9SFrank Mayhar { 110278d7d407SJiri Slaby unsigned long cpu_limit; 110378d7d407SJiri Slaby 1104316c1608SJason Low cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 110578d7d407SJiri Slaby if (cpu_limit != RLIM_INFINITY) { 110678d7d407SJiri Slaby sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); 1107d5c373ebSJason Low sig->cputimer.running = true; 11086279a751SOleg Nesterov } 11096279a751SOleg Nesterov 1110f06febc9SFrank Mayhar /* The timer lists. */ 1111f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[0]); 1112f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[1]); 1113f06febc9SFrank Mayhar INIT_LIST_HEAD(&sig->cpu_timers[2]); 1114f06febc9SFrank Mayhar } 1115f06febc9SFrank Mayhar 1116a39bc516SAlexey Dobriyan static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) 11171da177e4SLinus Torvalds { 11181da177e4SLinus Torvalds struct signal_struct *sig; 11191da177e4SLinus Torvalds 11204ab6c083SOleg Nesterov if (clone_flags & CLONE_THREAD) 1121490dea45SPeter Zijlstra return 0; 11226279a751SOleg Nesterov 1123a56704efSVeaceslav Falico sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 11241da177e4SLinus Torvalds tsk->signal = sig; 11251da177e4SLinus Torvalds if (!sig) 11261da177e4SLinus Torvalds return -ENOMEM; 11271da177e4SLinus Torvalds 1128b3ac022cSOleg Nesterov sig->nr_threads = 1; 11291da177e4SLinus Torvalds atomic_set(&sig->live, 1); 1130b3ac022cSOleg Nesterov atomic_set(&sig->sigcnt, 1); 11310c740d0aSOleg Nesterov 11320c740d0aSOleg Nesterov /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 11330c740d0aSOleg Nesterov sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 11340c740d0aSOleg Nesterov tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 11350c740d0aSOleg Nesterov 11361da177e4SLinus Torvalds init_waitqueue_head(&sig->wait_chldexit); 1137db51aeccSOleg Nesterov sig->curr_target = tsk; 11381da177e4SLinus Torvalds init_sigpending(&sig->shared_pending); 11391da177e4SLinus Torvalds INIT_LIST_HEAD(&sig->posix_timers); 1140e78c3496SRik van Riel seqlock_init(&sig->stats_lock); 11419d7fb042SPeter Zijlstra prev_cputime_init(&sig->prev_cputime); 11421da177e4SLinus Torvalds 1143c9cb2e3dSThomas Gleixner hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 11441da177e4SLinus Torvalds sig->real_timer.function = it_real_fn; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds task_lock(current->group_leader); 11471da177e4SLinus Torvalds memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 11481da177e4SLinus Torvalds task_unlock(current->group_leader); 11491da177e4SLinus Torvalds 11506279a751SOleg Nesterov posix_cpu_timers_init_group(sig); 11516279a751SOleg Nesterov 1152522ed776SMiloslav Trmac tty_audit_fork(sig); 11535091faa4SMike Galbraith sched_autogroup_fork(sig); 1154522ed776SMiloslav Trmac 1155a63d83f4SDavid Rientjes sig->oom_score_adj = current->signal->oom_score_adj; 1156dabb16f6SMandeep Singh Baines sig->oom_score_adj_min = current->signal->oom_score_adj_min; 115728b83c51SKOSAKI Motohiro 1158ebec18a6SLennart Poettering sig->has_child_subreaper = current->signal->has_child_subreaper || 1159ebec18a6SLennart Poettering current->signal->is_child_subreaper; 1160ebec18a6SLennart Poettering 11619b1bf12dSKOSAKI Motohiro mutex_init(&sig->cred_guard_mutex); 11629b1bf12dSKOSAKI Motohiro 11631da177e4SLinus Torvalds return 0; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 1166dbd95212SKees Cook static void copy_seccomp(struct task_struct *p) 1167dbd95212SKees Cook { 1168dbd95212SKees Cook #ifdef CONFIG_SECCOMP 1169dbd95212SKees Cook /* 1170dbd95212SKees Cook * Must be called with sighand->lock held, which is common to 1171dbd95212SKees Cook * all threads in the group. Holding cred_guard_mutex is not 1172dbd95212SKees Cook * needed because this new task is not yet running and cannot 1173dbd95212SKees Cook * be racing exec. 1174dbd95212SKees Cook */ 117569f6a34bSGuenter Roeck assert_spin_locked(¤t->sighand->siglock); 1176dbd95212SKees Cook 1177dbd95212SKees Cook /* Ref-count the new filter user, and assign it. */ 1178dbd95212SKees Cook get_seccomp_filter(current); 1179dbd95212SKees Cook p->seccomp = current->seccomp; 1180dbd95212SKees Cook 1181dbd95212SKees Cook /* 1182dbd95212SKees Cook * Explicitly enable no_new_privs here in case it got set 1183dbd95212SKees Cook * between the task_struct being duplicated and holding the 1184dbd95212SKees Cook * sighand lock. The seccomp state and nnp must be in sync. 1185dbd95212SKees Cook */ 1186dbd95212SKees Cook if (task_no_new_privs(current)) 1187dbd95212SKees Cook task_set_no_new_privs(p); 1188dbd95212SKees Cook 1189dbd95212SKees Cook /* 1190dbd95212SKees Cook * If the parent gained a seccomp mode after copying thread 1191dbd95212SKees Cook * flags and between before we held the sighand lock, we have 1192dbd95212SKees Cook * to manually enable the seccomp thread flag here. 1193dbd95212SKees Cook */ 1194dbd95212SKees Cook if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1195dbd95212SKees Cook set_tsk_thread_flag(p, TIF_SECCOMP); 1196dbd95212SKees Cook #endif 1197dbd95212SKees Cook } 1198dbd95212SKees Cook 119917da2bd9SHeiko Carstens SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 12001da177e4SLinus Torvalds { 12011da177e4SLinus Torvalds current->clear_child_tid = tidptr; 12021da177e4SLinus Torvalds 1203b488893aSPavel Emelyanov return task_pid_vnr(current); 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 1206a39bc516SAlexey Dobriyan static void rt_mutex_init_task(struct task_struct *p) 120723f78d4aSIngo Molnar { 12081d615482SThomas Gleixner raw_spin_lock_init(&p->pi_lock); 1209e29e175bSZilvinas Valinskas #ifdef CONFIG_RT_MUTEXES 1210fb00aca4SPeter Zijlstra p->pi_waiters = RB_ROOT; 1211fb00aca4SPeter Zijlstra p->pi_waiters_leftmost = NULL; 121223f78d4aSIngo Molnar p->pi_blocked_on = NULL; 121323f78d4aSIngo Molnar #endif 121423f78d4aSIngo Molnar } 121523f78d4aSIngo Molnar 12161da177e4SLinus Torvalds /* 1217f06febc9SFrank Mayhar * Initialize POSIX timer handling for a single task. 1218f06febc9SFrank Mayhar */ 1219f06febc9SFrank Mayhar static void posix_cpu_timers_init(struct task_struct *tsk) 1220f06febc9SFrank Mayhar { 122164861634SMartin Schwidefsky tsk->cputime_expires.prof_exp = 0; 122264861634SMartin Schwidefsky tsk->cputime_expires.virt_exp = 0; 1223f06febc9SFrank Mayhar tsk->cputime_expires.sched_exp = 0; 1224f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[0]); 1225f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[1]); 1226f06febc9SFrank Mayhar INIT_LIST_HEAD(&tsk->cpu_timers[2]); 1227f06febc9SFrank Mayhar } 1228f06febc9SFrank Mayhar 122981907739SOleg Nesterov static inline void 123081907739SOleg Nesterov init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 123181907739SOleg Nesterov { 123281907739SOleg Nesterov task->pids[type].pid = pid; 123381907739SOleg Nesterov } 123481907739SOleg Nesterov 1235f06febc9SFrank Mayhar /* 12361da177e4SLinus Torvalds * This creates a new process as a copy of the old one, 12371da177e4SLinus Torvalds * but does not actually start it yet. 12381da177e4SLinus Torvalds * 12391da177e4SLinus Torvalds * It copies the registers, and all the appropriate 12401da177e4SLinus Torvalds * parts of the process environment (as per the clone 12411da177e4SLinus Torvalds * flags). The actual kick-off is left to the caller. 12421da177e4SLinus Torvalds */ 124336c8b586SIngo Molnar static struct task_struct *copy_process(unsigned long clone_flags, 12441da177e4SLinus Torvalds unsigned long stack_start, 12451da177e4SLinus Torvalds unsigned long stack_size, 12461da177e4SLinus Torvalds int __user *child_tidptr, 124709a05394SRoland McGrath struct pid *pid, 12483033f14aSJosh Triplett int trace, 12493033f14aSJosh Triplett unsigned long tls) 12501da177e4SLinus Torvalds { 12511da177e4SLinus Torvalds int retval; 1252a24efe62SMariusz Kozlowski struct task_struct *p; 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 12551da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 12561da177e4SLinus Torvalds 1257e66eded8SEric W. Biederman if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 1258e66eded8SEric W. Biederman return ERR_PTR(-EINVAL); 1259e66eded8SEric W. Biederman 12601da177e4SLinus Torvalds /* 12611da177e4SLinus Torvalds * Thread groups must share signals as well, and detached threads 12621da177e4SLinus Torvalds * can only be started up within the thread group. 12631da177e4SLinus Torvalds */ 12641da177e4SLinus Torvalds if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 12651da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds /* 12681da177e4SLinus Torvalds * Shared signal handlers imply shared VM. By way of the above, 12691da177e4SLinus Torvalds * thread groups also imply shared VM. Blocking this case allows 12701da177e4SLinus Torvalds * for various simplifications in other code. 12711da177e4SLinus Torvalds */ 12721da177e4SLinus Torvalds if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 12731da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 12741da177e4SLinus Torvalds 1275123be07bSSukadev Bhattiprolu /* 1276123be07bSSukadev Bhattiprolu * Siblings of global init remain as zombies on exit since they are 1277123be07bSSukadev Bhattiprolu * not reaped by their parent (swapper). To solve this and to avoid 1278123be07bSSukadev Bhattiprolu * multi-rooted process trees, prevent global and container-inits 1279123be07bSSukadev Bhattiprolu * from creating siblings. 1280123be07bSSukadev Bhattiprolu */ 1281123be07bSSukadev Bhattiprolu if ((clone_flags & CLONE_PARENT) && 1282123be07bSSukadev Bhattiprolu current->signal->flags & SIGNAL_UNKILLABLE) 1283123be07bSSukadev Bhattiprolu return ERR_PTR(-EINVAL); 1284123be07bSSukadev Bhattiprolu 12858382fcacSEric W. Biederman /* 128640a0d32dSOleg Nesterov * If the new process will be in a different pid or user namespace 1287faf00da5SEric W. Biederman * do not allow it to share a thread group with the forking task. 12888382fcacSEric W. Biederman */ 1289faf00da5SEric W. Biederman if (clone_flags & CLONE_THREAD) { 129040a0d32dSOleg Nesterov if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 129140a0d32dSOleg Nesterov (task_active_pid_ns(current) != 1292c2b1df2eSAndy Lutomirski current->nsproxy->pid_ns_for_children)) 12938382fcacSEric W. Biederman return ERR_PTR(-EINVAL); 129440a0d32dSOleg Nesterov } 12958382fcacSEric W. Biederman 12961da177e4SLinus Torvalds retval = security_task_create(clone_flags); 12971da177e4SLinus Torvalds if (retval) 12981da177e4SLinus Torvalds goto fork_out; 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds retval = -ENOMEM; 13011da177e4SLinus Torvalds p = dup_task_struct(current); 13021da177e4SLinus Torvalds if (!p) 13031da177e4SLinus Torvalds goto fork_out; 13041da177e4SLinus Torvalds 1305f7e8b616SSteven Rostedt ftrace_graph_init_task(p); 1306f7e8b616SSteven Rostedt 1307bea493a0SPeter Zijlstra rt_mutex_init_task(p); 1308bea493a0SPeter Zijlstra 1309d12c1a37SIngo Molnar #ifdef CONFIG_PROVE_LOCKING 1310de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); 1311de30a2b3SIngo Molnar DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 1312de30a2b3SIngo Molnar #endif 13131da177e4SLinus Torvalds retval = -EAGAIN; 13143b11a1deSDavid Howells if (atomic_read(&p->real_cred->user->processes) >= 131578d7d407SJiri Slaby task_rlimit(p, RLIMIT_NPROC)) { 1316b57922b6SEric Paris if (p->real_cred->user != INIT_USER && 1317b57922b6SEric Paris !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 13181da177e4SLinus Torvalds goto bad_fork_free; 13191da177e4SLinus Torvalds } 132072fa5997SVasiliy Kulikov current->flags &= ~PF_NPROC_EXCEEDED; 13211da177e4SLinus Torvalds 1322f1752eecSDavid Howells retval = copy_creds(p, clone_flags); 1323f1752eecSDavid Howells if (retval < 0) 1324f1752eecSDavid Howells goto bad_fork_free; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds /* 13271da177e4SLinus Torvalds * If multiple threads are within copy_process(), then this check 13281da177e4SLinus Torvalds * triggers too late. This doesn't hurt, the check is only there 13291da177e4SLinus Torvalds * to stop root fork bombs. 13301da177e4SLinus Torvalds */ 133104ec93feSLi Zefan retval = -EAGAIN; 13321da177e4SLinus Torvalds if (nr_threads >= max_threads) 13331da177e4SLinus Torvalds goto bad_fork_cleanup_count; 13341da177e4SLinus Torvalds 1335ca74e92bSShailabh Nagar delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 1336514ddb44SDavid Rientjes p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER); 1337514ddb44SDavid Rientjes p->flags |= PF_FORKNOEXEC; 13381da177e4SLinus Torvalds INIT_LIST_HEAD(&p->children); 13391da177e4SLinus Torvalds INIT_LIST_HEAD(&p->sibling); 1340f41d911fSPaul E. McKenney rcu_copy_process(p); 13411da177e4SLinus Torvalds p->vfork_done = NULL; 13421da177e4SLinus Torvalds spin_lock_init(&p->alloc_lock); 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds init_sigpending(&p->pending); 13451da177e4SLinus Torvalds 134664861634SMartin Schwidefsky p->utime = p->stime = p->gtime = 0; 134764861634SMartin Schwidefsky p->utimescaled = p->stimescaled = 0; 13489d7fb042SPeter Zijlstra prev_cputime_init(&p->prev_cputime); 13499d7fb042SPeter Zijlstra 13506a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 1351b7ce2277SFrederic Weisbecker seqcount_init(&p->vtime_seqcount); 13526a61671bSFrederic Weisbecker p->vtime_snap = 0; 13537098c1eaSFrederic Weisbecker p->vtime_snap_whence = VTIME_INACTIVE; 13546a61671bSFrederic Weisbecker #endif 13556a61671bSFrederic Weisbecker 1356a3a2e76cSKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING) 1357a3a2e76cSKAMEZAWA Hiroyuki memset(&p->rss_stat, 0, sizeof(p->rss_stat)); 1358a3a2e76cSKAMEZAWA Hiroyuki #endif 1359172ba844SBalbir Singh 13606976675dSArjan van de Ven p->default_timer_slack_ns = current->timer_slack_ns; 13616976675dSArjan van de Ven 13625995477aSAndrea Righi task_io_accounting_init(&p->ioac); 13631da177e4SLinus Torvalds acct_clear_integrals(p); 13641da177e4SLinus Torvalds 1365f06febc9SFrank Mayhar posix_cpu_timers_init(p); 13661da177e4SLinus Torvalds 1367ccbf62d8SThomas Gleixner p->start_time = ktime_get_ns(); 136857e0be04SThomas Gleixner p->real_start_time = ktime_get_boot_ns(); 13691da177e4SLinus Torvalds p->io_context = NULL; 13701da177e4SLinus Torvalds p->audit_context = NULL; 1371257058aeSTejun Heo threadgroup_change_begin(current); 1372b4f48b63SPaul Menage cgroup_fork(p); 13731da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1374846a16bfSLee Schermerhorn p->mempolicy = mpol_dup(p->mempolicy); 13751da177e4SLinus Torvalds if (IS_ERR(p->mempolicy)) { 13761da177e4SLinus Torvalds retval = PTR_ERR(p->mempolicy); 13771da177e4SLinus Torvalds p->mempolicy = NULL; 1378e8604cb4SLi Zefan goto bad_fork_cleanup_threadgroup_lock; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds #endif 1381778d3b0fSMichal Hocko #ifdef CONFIG_CPUSETS 1382778d3b0fSMichal Hocko p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 1383778d3b0fSMichal Hocko p->cpuset_slab_spread_rotor = NUMA_NO_NODE; 1384cc9a6c87SMel Gorman seqcount_init(&p->mems_allowed_seq); 1385778d3b0fSMichal Hocko #endif 1386de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS 1387de30a2b3SIngo Molnar p->irq_events = 0; 1388de30a2b3SIngo Molnar p->hardirqs_enabled = 0; 1389de30a2b3SIngo Molnar p->hardirq_enable_ip = 0; 1390de30a2b3SIngo Molnar p->hardirq_enable_event = 0; 1391de30a2b3SIngo Molnar p->hardirq_disable_ip = _THIS_IP_; 1392de30a2b3SIngo Molnar p->hardirq_disable_event = 0; 1393de30a2b3SIngo Molnar p->softirqs_enabled = 1; 1394de30a2b3SIngo Molnar p->softirq_enable_ip = _THIS_IP_; 1395de30a2b3SIngo Molnar p->softirq_enable_event = 0; 1396de30a2b3SIngo Molnar p->softirq_disable_ip = 0; 1397de30a2b3SIngo Molnar p->softirq_disable_event = 0; 1398de30a2b3SIngo Molnar p->hardirq_context = 0; 1399de30a2b3SIngo Molnar p->softirq_context = 0; 1400de30a2b3SIngo Molnar #endif 14018bcbde54SDavid Hildenbrand 14028bcbde54SDavid Hildenbrand p->pagefault_disabled = 0; 14038bcbde54SDavid Hildenbrand 1404fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1405fbb9ce95SIngo Molnar p->lockdep_depth = 0; /* no locks held yet */ 1406fbb9ce95SIngo Molnar p->curr_chain_key = 0; 1407fbb9ce95SIngo Molnar p->lockdep_recursion = 0; 1408fbb9ce95SIngo Molnar #endif 14091da177e4SLinus Torvalds 1410408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES 1411408894eeSIngo Molnar p->blocked_on = NULL; /* not blocked yet */ 1412408894eeSIngo Molnar #endif 1413cafe5635SKent Overstreet #ifdef CONFIG_BCACHE 1414cafe5635SKent Overstreet p->sequential_io = 0; 1415cafe5635SKent Overstreet p->sequential_io_avg = 0; 1416cafe5635SKent Overstreet #endif 14170f481406SMarkus Metzger 14183c90e6e9SSrivatsa Vaddagiri /* Perform scheduler related setup. Assign this task to a CPU. */ 1419aab03e05SDario Faggioli retval = sched_fork(clone_flags, p); 1420aab03e05SDario Faggioli if (retval) 1421aab03e05SDario Faggioli goto bad_fork_cleanup_policy; 14226ab423e0SPeter Zijlstra 1423cdd6c482SIngo Molnar retval = perf_event_init_task(p); 14246ab423e0SPeter Zijlstra if (retval) 14256ab423e0SPeter Zijlstra goto bad_fork_cleanup_policy; 1426fb0a685cSDaniel Rebelo de Oliveira retval = audit_alloc(p); 1427fb0a685cSDaniel Rebelo de Oliveira if (retval) 14286c72e350SPeter Zijlstra goto bad_fork_cleanup_perf; 14291da177e4SLinus Torvalds /* copy all the process information */ 1430ab602f79SJack Miller shm_init_task(p); 1431fb0a685cSDaniel Rebelo de Oliveira retval = copy_semundo(clone_flags, p); 1432fb0a685cSDaniel Rebelo de Oliveira if (retval) 14331da177e4SLinus Torvalds goto bad_fork_cleanup_audit; 1434fb0a685cSDaniel Rebelo de Oliveira retval = copy_files(clone_flags, p); 1435fb0a685cSDaniel Rebelo de Oliveira if (retval) 14361da177e4SLinus Torvalds goto bad_fork_cleanup_semundo; 1437fb0a685cSDaniel Rebelo de Oliveira retval = copy_fs(clone_flags, p); 1438fb0a685cSDaniel Rebelo de Oliveira if (retval) 14391da177e4SLinus Torvalds goto bad_fork_cleanup_files; 1440fb0a685cSDaniel Rebelo de Oliveira retval = copy_sighand(clone_flags, p); 1441fb0a685cSDaniel Rebelo de Oliveira if (retval) 14421da177e4SLinus Torvalds goto bad_fork_cleanup_fs; 1443fb0a685cSDaniel Rebelo de Oliveira retval = copy_signal(clone_flags, p); 1444fb0a685cSDaniel Rebelo de Oliveira if (retval) 14451da177e4SLinus Torvalds goto bad_fork_cleanup_sighand; 1446fb0a685cSDaniel Rebelo de Oliveira retval = copy_mm(clone_flags, p); 1447fb0a685cSDaniel Rebelo de Oliveira if (retval) 14481da177e4SLinus Torvalds goto bad_fork_cleanup_signal; 1449fb0a685cSDaniel Rebelo de Oliveira retval = copy_namespaces(clone_flags, p); 1450fb0a685cSDaniel Rebelo de Oliveira if (retval) 1451d84f4f99SDavid Howells goto bad_fork_cleanup_mm; 1452fb0a685cSDaniel Rebelo de Oliveira retval = copy_io(clone_flags, p); 1453fb0a685cSDaniel Rebelo de Oliveira if (retval) 1454fd0928dfSJens Axboe goto bad_fork_cleanup_namespaces; 14553033f14aSJosh Triplett retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls); 14561da177e4SLinus Torvalds if (retval) 1457fd0928dfSJens Axboe goto bad_fork_cleanup_io; 14581da177e4SLinus Torvalds 1459425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) { 1460c2b1df2eSAndy Lutomirski pid = alloc_pid(p->nsproxy->pid_ns_for_children); 146135f71bc0SMichal Hocko if (IS_ERR(pid)) { 146235f71bc0SMichal Hocko retval = PTR_ERR(pid); 1463fd0928dfSJens Axboe goto bad_fork_cleanup_io; 1464425fb2b4SPavel Emelyanov } 146535f71bc0SMichal Hocko } 1466425fb2b4SPavel Emelyanov 14671da177e4SLinus Torvalds p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; 14681da177e4SLinus Torvalds /* 14691da177e4SLinus Torvalds * Clear TID on mm_release()? 14701da177e4SLinus Torvalds */ 14711da177e4SLinus Torvalds p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; 147273c10101SJens Axboe #ifdef CONFIG_BLOCK 147373c10101SJens Axboe p->plug = NULL; 147473c10101SJens Axboe #endif 147542b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX 14768f17d3a5SIngo Molnar p->robust_list = NULL; 14778f17d3a5SIngo Molnar #ifdef CONFIG_COMPAT 14788f17d3a5SIngo Molnar p->compat_robust_list = NULL; 14798f17d3a5SIngo Molnar #endif 1480c87e2837SIngo Molnar INIT_LIST_HEAD(&p->pi_state_list); 1481c87e2837SIngo Molnar p->pi_state_cache = NULL; 148242b2dd0aSAlexey Dobriyan #endif 14831da177e4SLinus Torvalds /* 1484f9a3879aSGOTO Masanori * sigaltstack should be cleared when sharing the same VM 1485f9a3879aSGOTO Masanori */ 1486f9a3879aSGOTO Masanori if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 1487f9a3879aSGOTO Masanori p->sas_ss_sp = p->sas_ss_size = 0; 1488f9a3879aSGOTO Masanori 1489f9a3879aSGOTO Masanori /* 14906580807dSOleg Nesterov * Syscall tracing and stepping should be turned off in the 14916580807dSOleg Nesterov * child regardless of CLONE_PTRACE. 14921da177e4SLinus Torvalds */ 14936580807dSOleg Nesterov user_disable_single_step(p); 14941da177e4SLinus Torvalds clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); 1495ed75e8d5SLaurent Vivier #ifdef TIF_SYSCALL_EMU 1496ed75e8d5SLaurent Vivier clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); 1497ed75e8d5SLaurent Vivier #endif 14989745512cSArjan van de Ven clear_all_latency_tracing(p); 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds /* ok, now we should be set up.. */ 150118c830dfSOleg Nesterov p->pid = pid_nr(pid); 150218c830dfSOleg Nesterov if (clone_flags & CLONE_THREAD) { 15035f8aadd8SOleg Nesterov p->exit_signal = -1; 150418c830dfSOleg Nesterov p->group_leader = current->group_leader; 150518c830dfSOleg Nesterov p->tgid = current->tgid; 150618c830dfSOleg Nesterov } else { 150718c830dfSOleg Nesterov if (clone_flags & CLONE_PARENT) 15085f8aadd8SOleg Nesterov p->exit_signal = current->group_leader->exit_signal; 15095f8aadd8SOleg Nesterov else 15105f8aadd8SOleg Nesterov p->exit_signal = (clone_flags & CSIGNAL); 151118c830dfSOleg Nesterov p->group_leader = p; 151218c830dfSOleg Nesterov p->tgid = p->pid; 151318c830dfSOleg Nesterov } 15145f8aadd8SOleg Nesterov 15159d823e8fSWu Fengguang p->nr_dirtied = 0; 15169d823e8fSWu Fengguang p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 151783712358SWu Fengguang p->dirty_paused_when = 0; 15189d823e8fSWu Fengguang 1519bb8cbbfeSOleg Nesterov p->pdeath_signal = 0; 152047e65328SOleg Nesterov INIT_LIST_HEAD(&p->thread_group); 1521158e1645SAl Viro p->task_works = NULL; 15221da177e4SLinus Torvalds 152318c830dfSOleg Nesterov /* 15247e47682eSAleksa Sarai * Ensure that the cgroup subsystem policies allow the new process to be 15257e47682eSAleksa Sarai * forked. It should be noted the the new process's css_set can be changed 15267e47682eSAleksa Sarai * between here and cgroup_post_fork() if an organisation operation is in 15277e47682eSAleksa Sarai * progress. 15287e47682eSAleksa Sarai */ 1529b53202e6SOleg Nesterov retval = cgroup_can_fork(p); 15307e47682eSAleksa Sarai if (retval) 15317e47682eSAleksa Sarai goto bad_fork_free_pid; 15327e47682eSAleksa Sarai 15337e47682eSAleksa Sarai /* 153418c830dfSOleg Nesterov * Make it visible to the rest of the system, but dont wake it up yet. 153518c830dfSOleg Nesterov * Need tasklist lock for parent etc handling! 153618c830dfSOleg Nesterov */ 15371da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds /* CLONE_PARENT re-uses the old parent */ 15402d5516cbSOleg Nesterov if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 15411da177e4SLinus Torvalds p->real_parent = current->real_parent; 15422d5516cbSOleg Nesterov p->parent_exec_id = current->parent_exec_id; 15432d5516cbSOleg Nesterov } else { 15441da177e4SLinus Torvalds p->real_parent = current; 15452d5516cbSOleg Nesterov p->parent_exec_id = current->self_exec_id; 15462d5516cbSOleg Nesterov } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds spin_lock(¤t->sighand->siglock); 15494a2c7a78SOleg Nesterov 15504a2c7a78SOleg Nesterov /* 1551dbd95212SKees Cook * Copy seccomp details explicitly here, in case they were changed 1552dbd95212SKees Cook * before holding sighand lock. 1553dbd95212SKees Cook */ 1554dbd95212SKees Cook copy_seccomp(p); 1555dbd95212SKees Cook 1556dbd95212SKees Cook /* 15574a2c7a78SOleg Nesterov * Process group and session signals need to be delivered to just the 15584a2c7a78SOleg Nesterov * parent before the fork or both the parent and the child after the 15594a2c7a78SOleg Nesterov * fork. Restart if a signal comes in before we add the new process to 15604a2c7a78SOleg Nesterov * it's process group. 15614a2c7a78SOleg Nesterov * A fatal signal pending means that current will exit, so the new 15624a2c7a78SOleg Nesterov * thread can't slip out of an OOM kill (or normal SIGKILL). 15634a2c7a78SOleg Nesterov */ 15644a2c7a78SOleg Nesterov recalc_sigpending(); 15654a2c7a78SOleg Nesterov if (signal_pending(current)) { 15664a2c7a78SOleg Nesterov spin_unlock(¤t->sighand->siglock); 15674a2c7a78SOleg Nesterov write_unlock_irq(&tasklist_lock); 15684a2c7a78SOleg Nesterov retval = -ERESTARTNOINTR; 15697e47682eSAleksa Sarai goto bad_fork_cancel_cgroup; 15704a2c7a78SOleg Nesterov } 15714a2c7a78SOleg Nesterov 157273b9ebfeSOleg Nesterov if (likely(p->pid)) { 15734b9d33e6STejun Heo ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 15741da177e4SLinus Torvalds 157581907739SOleg Nesterov init_task_pid(p, PIDTYPE_PID, pid); 15761da177e4SLinus Torvalds if (thread_group_leader(p)) { 157781907739SOleg Nesterov init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 157881907739SOleg Nesterov init_task_pid(p, PIDTYPE_SID, task_session(current)); 157981907739SOleg Nesterov 15801c4042c2SEric W. Biederman if (is_child_reaper(pid)) { 158117cf22c3SEric W. Biederman ns_of_pid(pid)->child_reaper = p; 15821c4042c2SEric W. Biederman p->signal->flags |= SIGNAL_UNKILLABLE; 15831c4042c2SEric W. Biederman } 15845cd17569SEric W. Biederman 1585fea9d175SOleg Nesterov p->signal->leader_pid = pid; 15869c9f4dedSAlan Cox p->signal->tty = tty_kref_get(current->signal->tty); 15879cd80bbbSOleg Nesterov list_add_tail(&p->sibling, &p->real_parent->children); 15885e85d4abSEric W. Biederman list_add_tail_rcu(&p->tasks, &init_task.tasks); 158981907739SOleg Nesterov attach_pid(p, PIDTYPE_PGID); 159081907739SOleg Nesterov attach_pid(p, PIDTYPE_SID); 1591909ea964SChristoph Lameter __this_cpu_inc(process_counts); 159280628ca0SOleg Nesterov } else { 159380628ca0SOleg Nesterov current->signal->nr_threads++; 159480628ca0SOleg Nesterov atomic_inc(¤t->signal->live); 159580628ca0SOleg Nesterov atomic_inc(¤t->signal->sigcnt); 159680628ca0SOleg Nesterov list_add_tail_rcu(&p->thread_group, 159780628ca0SOleg Nesterov &p->group_leader->thread_group); 15980c740d0aSOleg Nesterov list_add_tail_rcu(&p->thread_node, 15990c740d0aSOleg Nesterov &p->signal->thread_head); 16001da177e4SLinus Torvalds } 160181907739SOleg Nesterov attach_pid(p, PIDTYPE_PID); 16021da177e4SLinus Torvalds nr_threads++; 160373b9ebfeSOleg Nesterov } 160473b9ebfeSOleg Nesterov 16051da177e4SLinus Torvalds total_forks++; 16063f17da69SOleg Nesterov spin_unlock(¤t->sighand->siglock); 16074af4206bSOleg Nesterov syscall_tracepoint_update(p); 16081da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 16094af4206bSOleg Nesterov 1610c13cf856SAndrew Morton proc_fork_connector(p); 1611b53202e6SOleg Nesterov cgroup_post_fork(p); 1612257058aeSTejun Heo threadgroup_change_end(current); 1613cdd6c482SIngo Molnar perf_event_fork(p); 161443d2b113SKAMEZAWA Hiroyuki 161543d2b113SKAMEZAWA Hiroyuki trace_task_newtask(p, clone_flags); 16163ab67966SOleg Nesterov uprobe_copy_process(p, clone_flags); 161743d2b113SKAMEZAWA Hiroyuki 16181da177e4SLinus Torvalds return p; 16191da177e4SLinus Torvalds 16207e47682eSAleksa Sarai bad_fork_cancel_cgroup: 1621b53202e6SOleg Nesterov cgroup_cancel_fork(p); 1622425fb2b4SPavel Emelyanov bad_fork_free_pid: 1623425fb2b4SPavel Emelyanov if (pid != &init_struct_pid) 1624425fb2b4SPavel Emelyanov free_pid(pid); 1625fd0928dfSJens Axboe bad_fork_cleanup_io: 1626b69f2292SLouis Rilling if (p->io_context) 1627b69f2292SLouis Rilling exit_io_context(p); 1628ab516013SSerge E. Hallyn bad_fork_cleanup_namespaces: 1629444f378bSLinus Torvalds exit_task_namespaces(p); 16301da177e4SLinus Torvalds bad_fork_cleanup_mm: 1631c9f01245SDavid Rientjes if (p->mm) 16321da177e4SLinus Torvalds mmput(p->mm); 16331da177e4SLinus Torvalds bad_fork_cleanup_signal: 16344ab6c083SOleg Nesterov if (!(clone_flags & CLONE_THREAD)) 16351c5354deSMike Galbraith free_signal_struct(p->signal); 16361da177e4SLinus Torvalds bad_fork_cleanup_sighand: 1637a7e5328aSOleg Nesterov __cleanup_sighand(p->sighand); 16381da177e4SLinus Torvalds bad_fork_cleanup_fs: 16391da177e4SLinus Torvalds exit_fs(p); /* blocking */ 16401da177e4SLinus Torvalds bad_fork_cleanup_files: 16411da177e4SLinus Torvalds exit_files(p); /* blocking */ 16421da177e4SLinus Torvalds bad_fork_cleanup_semundo: 16431da177e4SLinus Torvalds exit_sem(p); 16441da177e4SLinus Torvalds bad_fork_cleanup_audit: 16451da177e4SLinus Torvalds audit_free(p); 16466c72e350SPeter Zijlstra bad_fork_cleanup_perf: 1647cdd6c482SIngo Molnar perf_event_free_task(p); 16486c72e350SPeter Zijlstra bad_fork_cleanup_policy: 16491da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1650f0be3d32SLee Schermerhorn mpol_put(p->mempolicy); 1651e8604cb4SLi Zefan bad_fork_cleanup_threadgroup_lock: 16521da177e4SLinus Torvalds #endif 1653257058aeSTejun Heo threadgroup_change_end(current); 165435df17c5SShailabh Nagar delayacct_tsk_free(p); 16551da177e4SLinus Torvalds bad_fork_cleanup_count: 1656d84f4f99SDavid Howells atomic_dec(&p->cred->user->processes); 1657e0e81739SDavid Howells exit_creds(p); 16581da177e4SLinus Torvalds bad_fork_free: 16591da177e4SLinus Torvalds free_task(p); 1660fe7d37d1SOleg Nesterov fork_out: 1661fe7d37d1SOleg Nesterov return ERR_PTR(retval); 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 1664f106eee1SOleg Nesterov static inline void init_idle_pids(struct pid_link *links) 1665f106eee1SOleg Nesterov { 1666f106eee1SOleg Nesterov enum pid_type type; 1667f106eee1SOleg Nesterov 1668f106eee1SOleg Nesterov for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 1669f106eee1SOleg Nesterov INIT_HLIST_NODE(&links[type].node); /* not really needed */ 1670f106eee1SOleg Nesterov links[type].pid = &init_struct_pid; 1671f106eee1SOleg Nesterov } 1672f106eee1SOleg Nesterov } 1673f106eee1SOleg Nesterov 16740db0628dSPaul Gortmaker struct task_struct *fork_idle(int cpu) 16751da177e4SLinus Torvalds { 167636c8b586SIngo Molnar struct task_struct *task; 16773033f14aSJosh Triplett task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0); 1678f106eee1SOleg Nesterov if (!IS_ERR(task)) { 1679f106eee1SOleg Nesterov init_idle_pids(task->pids); 16801da177e4SLinus Torvalds init_idle(task, cpu); 1681f106eee1SOleg Nesterov } 168273b9ebfeSOleg Nesterov 16831da177e4SLinus Torvalds return task; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds /* 16871da177e4SLinus Torvalds * Ok, this is the main fork-routine. 16881da177e4SLinus Torvalds * 16891da177e4SLinus Torvalds * It copies the process, and if successful kick-starts 16901da177e4SLinus Torvalds * it and waits for it to finish using the VM if required. 16911da177e4SLinus Torvalds */ 16923033f14aSJosh Triplett long _do_fork(unsigned long clone_flags, 16931da177e4SLinus Torvalds unsigned long stack_start, 16941da177e4SLinus Torvalds unsigned long stack_size, 16951da177e4SLinus Torvalds int __user *parent_tidptr, 16963033f14aSJosh Triplett int __user *child_tidptr, 16973033f14aSJosh Triplett unsigned long tls) 16981da177e4SLinus Torvalds { 16991da177e4SLinus Torvalds struct task_struct *p; 17001da177e4SLinus Torvalds int trace = 0; 170192476d7fSEric W. Biederman long nr; 17021da177e4SLinus Torvalds 1703bdff746aSAndrew Morton /* 17044b9d33e6STejun Heo * Determine whether and which event to report to ptracer. When 17054b9d33e6STejun Heo * called from kernel_thread or CLONE_UNTRACED is explicitly 17064b9d33e6STejun Heo * requested, no event is reported; otherwise, report if the event 17074b9d33e6STejun Heo * for the type of forking is enabled. 170809a05394SRoland McGrath */ 1709e80d6661SAl Viro if (!(clone_flags & CLONE_UNTRACED)) { 17104b9d33e6STejun Heo if (clone_flags & CLONE_VFORK) 17114b9d33e6STejun Heo trace = PTRACE_EVENT_VFORK; 17124b9d33e6STejun Heo else if ((clone_flags & CSIGNAL) != SIGCHLD) 17134b9d33e6STejun Heo trace = PTRACE_EVENT_CLONE; 17144b9d33e6STejun Heo else 17154b9d33e6STejun Heo trace = PTRACE_EVENT_FORK; 17164b9d33e6STejun Heo 17174b9d33e6STejun Heo if (likely(!ptrace_event_enabled(current, trace))) 17184b9d33e6STejun Heo trace = 0; 17194b9d33e6STejun Heo } 17201da177e4SLinus Torvalds 172162e791c1SAl Viro p = copy_process(clone_flags, stack_start, stack_size, 17223033f14aSJosh Triplett child_tidptr, NULL, trace, tls); 17231da177e4SLinus Torvalds /* 17241da177e4SLinus Torvalds * Do this prior waking up the new thread - the thread pointer 17251da177e4SLinus Torvalds * might get invalid after that point, if the thread exits quickly. 17261da177e4SLinus Torvalds */ 17271da177e4SLinus Torvalds if (!IS_ERR(p)) { 17281da177e4SLinus Torvalds struct completion vfork; 17294e52365fSMatthew Dempsky struct pid *pid; 17301da177e4SLinus Torvalds 17310a16b607SMathieu Desnoyers trace_sched_process_fork(current, p); 17320a16b607SMathieu Desnoyers 17334e52365fSMatthew Dempsky pid = get_task_pid(p, PIDTYPE_PID); 17344e52365fSMatthew Dempsky nr = pid_vnr(pid); 173530e49c26SPavel Emelyanov 173630e49c26SPavel Emelyanov if (clone_flags & CLONE_PARENT_SETTID) 173730e49c26SPavel Emelyanov put_user(nr, parent_tidptr); 1738a6f5e063SSukadev Bhattiprolu 17391da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 17401da177e4SLinus Torvalds p->vfork_done = &vfork; 17411da177e4SLinus Torvalds init_completion(&vfork); 1742d68b46feSOleg Nesterov get_task_struct(p); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 17453e51e3edSSamir Bellabes wake_up_new_task(p); 17461da177e4SLinus Torvalds 17474b9d33e6STejun Heo /* forking complete and child started to run, tell ptracer */ 17484b9d33e6STejun Heo if (unlikely(trace)) 17494e52365fSMatthew Dempsky ptrace_event_pid(trace, pid); 175009a05394SRoland McGrath 17511da177e4SLinus Torvalds if (clone_flags & CLONE_VFORK) { 1752d68b46feSOleg Nesterov if (!wait_for_vfork_done(p, &vfork)) 17534e52365fSMatthew Dempsky ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 17549f59ce5dSChuck Ebbert } 17554e52365fSMatthew Dempsky 17564e52365fSMatthew Dempsky put_pid(pid); 17571da177e4SLinus Torvalds } else { 175892476d7fSEric W. Biederman nr = PTR_ERR(p); 17591da177e4SLinus Torvalds } 176092476d7fSEric W. Biederman return nr; 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds 17633033f14aSJosh Triplett #ifndef CONFIG_HAVE_COPY_THREAD_TLS 17643033f14aSJosh Triplett /* For compatibility with architectures that call do_fork directly rather than 17653033f14aSJosh Triplett * using the syscall entry points below. */ 17663033f14aSJosh Triplett long do_fork(unsigned long clone_flags, 17673033f14aSJosh Triplett unsigned long stack_start, 17683033f14aSJosh Triplett unsigned long stack_size, 17693033f14aSJosh Triplett int __user *parent_tidptr, 17703033f14aSJosh Triplett int __user *child_tidptr) 17713033f14aSJosh Triplett { 17723033f14aSJosh Triplett return _do_fork(clone_flags, stack_start, stack_size, 17733033f14aSJosh Triplett parent_tidptr, child_tidptr, 0); 17743033f14aSJosh Triplett } 17753033f14aSJosh Triplett #endif 17763033f14aSJosh Triplett 17772aa3a7f8SAl Viro /* 17782aa3a7f8SAl Viro * Create a kernel thread. 17792aa3a7f8SAl Viro */ 17802aa3a7f8SAl Viro pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) 17812aa3a7f8SAl Viro { 17823033f14aSJosh Triplett return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, 17833033f14aSJosh Triplett (unsigned long)arg, NULL, NULL, 0); 17842aa3a7f8SAl Viro } 17852aa3a7f8SAl Viro 1786d2125043SAl Viro #ifdef __ARCH_WANT_SYS_FORK 1787d2125043SAl Viro SYSCALL_DEFINE0(fork) 1788d2125043SAl Viro { 1789d2125043SAl Viro #ifdef CONFIG_MMU 17903033f14aSJosh Triplett return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0); 1791d2125043SAl Viro #else 1792d2125043SAl Viro /* can not support in nommu mode */ 17935d59e182SDaeseok Youn return -EINVAL; 1794d2125043SAl Viro #endif 1795d2125043SAl Viro } 1796d2125043SAl Viro #endif 1797d2125043SAl Viro 1798d2125043SAl Viro #ifdef __ARCH_WANT_SYS_VFORK 1799d2125043SAl Viro SYSCALL_DEFINE0(vfork) 1800d2125043SAl Viro { 18013033f14aSJosh Triplett return _do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 18023033f14aSJosh Triplett 0, NULL, NULL, 0); 1803d2125043SAl Viro } 1804d2125043SAl Viro #endif 1805d2125043SAl Viro 1806d2125043SAl Viro #ifdef __ARCH_WANT_SYS_CLONE 1807d2125043SAl Viro #ifdef CONFIG_CLONE_BACKWARDS 1808d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1809d2125043SAl Viro int __user *, parent_tidptr, 18103033f14aSJosh Triplett unsigned long, tls, 1811d2125043SAl Viro int __user *, child_tidptr) 1812d2125043SAl Viro #elif defined(CONFIG_CLONE_BACKWARDS2) 1813d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 1814d2125043SAl Viro int __user *, parent_tidptr, 1815d2125043SAl Viro int __user *, child_tidptr, 18163033f14aSJosh Triplett unsigned long, tls) 1817dfa9771aSMichal Simek #elif defined(CONFIG_CLONE_BACKWARDS3) 1818dfa9771aSMichal Simek SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 1819dfa9771aSMichal Simek int, stack_size, 1820dfa9771aSMichal Simek int __user *, parent_tidptr, 1821dfa9771aSMichal Simek int __user *, child_tidptr, 18223033f14aSJosh Triplett unsigned long, tls) 1823d2125043SAl Viro #else 1824d2125043SAl Viro SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 1825d2125043SAl Viro int __user *, parent_tidptr, 1826d2125043SAl Viro int __user *, child_tidptr, 18273033f14aSJosh Triplett unsigned long, tls) 1828d2125043SAl Viro #endif 1829d2125043SAl Viro { 18303033f14aSJosh Triplett return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls); 1831d2125043SAl Viro } 1832d2125043SAl Viro #endif 1833d2125043SAl Viro 18345fd63b30SRavikiran G Thirumalai #ifndef ARCH_MIN_MMSTRUCT_ALIGN 18355fd63b30SRavikiran G Thirumalai #define ARCH_MIN_MMSTRUCT_ALIGN 0 18365fd63b30SRavikiran G Thirumalai #endif 18375fd63b30SRavikiran G Thirumalai 183851cc5068SAlexey Dobriyan static void sighand_ctor(void *data) 1839aa1757f9SOleg Nesterov { 1840aa1757f9SOleg Nesterov struct sighand_struct *sighand = data; 1841aa1757f9SOleg Nesterov 1842aa1757f9SOleg Nesterov spin_lock_init(&sighand->siglock); 1843b8fceee1SDavide Libenzi init_waitqueue_head(&sighand->signalfd_wqh); 1844fba2afaaSDavide Libenzi } 1845aa1757f9SOleg Nesterov 18461da177e4SLinus Torvalds void __init proc_caches_init(void) 18471da177e4SLinus Torvalds { 18481da177e4SLinus Torvalds sighand_cachep = kmem_cache_create("sighand_cache", 18491da177e4SLinus Torvalds sizeof(struct sighand_struct), 0, 18502dff4405SVegard Nossum SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| 1851*5d097056SVladimir Davydov SLAB_NOTRACK|SLAB_ACCOUNT, sighand_ctor); 18521da177e4SLinus Torvalds signal_cachep = kmem_cache_create("signal_cache", 18531da177e4SLinus Torvalds sizeof(struct signal_struct), 0, 1854*5d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 1855*5d097056SVladimir Davydov NULL); 18561da177e4SLinus Torvalds files_cachep = kmem_cache_create("files_cache", 18571da177e4SLinus Torvalds sizeof(struct files_struct), 0, 1858*5d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 1859*5d097056SVladimir Davydov NULL); 18601da177e4SLinus Torvalds fs_cachep = kmem_cache_create("fs_cache", 18611da177e4SLinus Torvalds sizeof(struct fs_struct), 0, 1862*5d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 1863*5d097056SVladimir Davydov NULL); 18646345d24dSLinus Torvalds /* 18656345d24dSLinus Torvalds * FIXME! The "sizeof(struct mm_struct)" currently includes the 18666345d24dSLinus Torvalds * whole struct cpumask for the OFFSTACK case. We could change 18676345d24dSLinus Torvalds * this to *only* allocate as much of it as required by the 18686345d24dSLinus Torvalds * maximum number of CPU's we can ever have. The cpumask_allocation 18696345d24dSLinus Torvalds * is at the end of the structure, exactly for that reason. 18706345d24dSLinus Torvalds */ 18711da177e4SLinus Torvalds mm_cachep = kmem_cache_create("mm_struct", 18725fd63b30SRavikiran G Thirumalai sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, 1873*5d097056SVladimir Davydov SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, 1874*5d097056SVladimir Davydov NULL); 1875*5d097056SVladimir Davydov vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); 18768feae131SDavid Howells mmap_init(); 187766577193SAl Viro nsproxy_cache_init(); 18781da177e4SLinus Torvalds } 1879cf2e340fSJANAK DESAI 1880cf2e340fSJANAK DESAI /* 18819bfb23fcSOleg Nesterov * Check constraints on flags passed to the unshare system call. 1882cf2e340fSJANAK DESAI */ 18839bfb23fcSOleg Nesterov static int check_unshare_flags(unsigned long unshare_flags) 1884cf2e340fSJANAK DESAI { 18859bfb23fcSOleg Nesterov if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 18869bfb23fcSOleg Nesterov CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 188750804fe3SEric W. Biederman CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| 1888b2e0d987SEric W. Biederman CLONE_NEWUSER|CLONE_NEWPID)) 1889cf2e340fSJANAK DESAI return -EINVAL; 18909bfb23fcSOleg Nesterov /* 189112c641abSEric W. Biederman * Not implemented, but pretend it works if there is nothing 189212c641abSEric W. Biederman * to unshare. Note that unsharing the address space or the 189312c641abSEric W. Biederman * signal handlers also need to unshare the signal queues (aka 189412c641abSEric W. Biederman * CLONE_THREAD). 18959bfb23fcSOleg Nesterov */ 18969bfb23fcSOleg Nesterov if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 189712c641abSEric W. Biederman if (!thread_group_empty(current)) 189812c641abSEric W. Biederman return -EINVAL; 189912c641abSEric W. Biederman } 190012c641abSEric W. Biederman if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 190112c641abSEric W. Biederman if (atomic_read(¤t->sighand->count) > 1) 190212c641abSEric W. Biederman return -EINVAL; 190312c641abSEric W. Biederman } 190412c641abSEric W. Biederman if (unshare_flags & CLONE_VM) { 190512c641abSEric W. Biederman if (!current_is_single_threaded()) 19069bfb23fcSOleg Nesterov return -EINVAL; 19079bfb23fcSOleg Nesterov } 1908cf2e340fSJANAK DESAI 1909cf2e340fSJANAK DESAI return 0; 1910cf2e340fSJANAK DESAI } 1911cf2e340fSJANAK DESAI 1912cf2e340fSJANAK DESAI /* 191399d1419dSJANAK DESAI * Unshare the filesystem structure if it is being shared 1914cf2e340fSJANAK DESAI */ 1915cf2e340fSJANAK DESAI static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 1916cf2e340fSJANAK DESAI { 1917cf2e340fSJANAK DESAI struct fs_struct *fs = current->fs; 1918cf2e340fSJANAK DESAI 1919498052bbSAl Viro if (!(unshare_flags & CLONE_FS) || !fs) 1920498052bbSAl Viro return 0; 1921498052bbSAl Viro 1922498052bbSAl Viro /* don't need lock here; in the worst case we'll do useless copy */ 1923498052bbSAl Viro if (fs->users == 1) 1924498052bbSAl Viro return 0; 1925498052bbSAl Viro 1926498052bbSAl Viro *new_fsp = copy_fs_struct(fs); 192799d1419dSJANAK DESAI if (!*new_fsp) 192899d1419dSJANAK DESAI return -ENOMEM; 1929cf2e340fSJANAK DESAI 1930cf2e340fSJANAK DESAI return 0; 1931cf2e340fSJANAK DESAI } 1932cf2e340fSJANAK DESAI 1933cf2e340fSJANAK DESAI /* 1934a016f338SJANAK DESAI * Unshare file descriptor table if it is being shared 1935cf2e340fSJANAK DESAI */ 1936cf2e340fSJANAK DESAI static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 1937cf2e340fSJANAK DESAI { 1938cf2e340fSJANAK DESAI struct files_struct *fd = current->files; 1939a016f338SJANAK DESAI int error = 0; 1940cf2e340fSJANAK DESAI 1941cf2e340fSJANAK DESAI if ((unshare_flags & CLONE_FILES) && 1942a016f338SJANAK DESAI (fd && atomic_read(&fd->count) > 1)) { 1943a016f338SJANAK DESAI *new_fdp = dup_fd(fd, &error); 1944a016f338SJANAK DESAI if (!*new_fdp) 1945a016f338SJANAK DESAI return error; 1946a016f338SJANAK DESAI } 1947cf2e340fSJANAK DESAI 1948cf2e340fSJANAK DESAI return 0; 1949cf2e340fSJANAK DESAI } 1950cf2e340fSJANAK DESAI 1951cf2e340fSJANAK DESAI /* 1952cf2e340fSJANAK DESAI * unshare allows a process to 'unshare' part of the process 1953cf2e340fSJANAK DESAI * context which was originally shared using clone. copy_* 1954cf2e340fSJANAK DESAI * functions used by do_fork() cannot be used here directly 1955cf2e340fSJANAK DESAI * because they modify an inactive task_struct that is being 1956cf2e340fSJANAK DESAI * constructed. Here we are modifying the current, active, 1957cf2e340fSJANAK DESAI * task_struct. 1958cf2e340fSJANAK DESAI */ 19596559eed8SHeiko Carstens SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 1960cf2e340fSJANAK DESAI { 1961cf2e340fSJANAK DESAI struct fs_struct *fs, *new_fs = NULL; 1962cf2e340fSJANAK DESAI struct files_struct *fd, *new_fd = NULL; 1963b2e0d987SEric W. Biederman struct cred *new_cred = NULL; 1964cf7b708cSPavel Emelyanov struct nsproxy *new_nsproxy = NULL; 19659edff4abSManfred Spraul int do_sysvsem = 0; 19669bfb23fcSOleg Nesterov int err; 1967cf2e340fSJANAK DESAI 196850804fe3SEric W. Biederman /* 1969faf00da5SEric W. Biederman * If unsharing a user namespace must also unshare the thread group 1970faf00da5SEric W. Biederman * and unshare the filesystem root and working directories. 1971b2e0d987SEric W. Biederman */ 1972b2e0d987SEric W. Biederman if (unshare_flags & CLONE_NEWUSER) 1973e66eded8SEric W. Biederman unshare_flags |= CLONE_THREAD | CLONE_FS; 1974b2e0d987SEric W. Biederman /* 197550804fe3SEric W. Biederman * If unsharing vm, must also unshare signal handlers. 197650804fe3SEric W. Biederman */ 197750804fe3SEric W. Biederman if (unshare_flags & CLONE_VM) 197850804fe3SEric W. Biederman unshare_flags |= CLONE_SIGHAND; 19796013f67fSManfred Spraul /* 198012c641abSEric W. Biederman * If unsharing a signal handlers, must also unshare the signal queues. 198112c641abSEric W. Biederman */ 198212c641abSEric W. Biederman if (unshare_flags & CLONE_SIGHAND) 198312c641abSEric W. Biederman unshare_flags |= CLONE_THREAD; 198412c641abSEric W. Biederman /* 19859bfb23fcSOleg Nesterov * If unsharing namespace, must also unshare filesystem information. 19869bfb23fcSOleg Nesterov */ 19879bfb23fcSOleg Nesterov if (unshare_flags & CLONE_NEWNS) 19889bfb23fcSOleg Nesterov unshare_flags |= CLONE_FS; 198950804fe3SEric W. Biederman 199050804fe3SEric W. Biederman err = check_unshare_flags(unshare_flags); 199150804fe3SEric W. Biederman if (err) 199250804fe3SEric W. Biederman goto bad_unshare_out; 19939bfb23fcSOleg Nesterov /* 19946013f67fSManfred Spraul * CLONE_NEWIPC must also detach from the undolist: after switching 19956013f67fSManfred Spraul * to a new ipc namespace, the semaphore arrays from the old 19966013f67fSManfred Spraul * namespace are unreachable. 19976013f67fSManfred Spraul */ 19986013f67fSManfred Spraul if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 19999edff4abSManfred Spraul do_sysvsem = 1; 2000fb0a685cSDaniel Rebelo de Oliveira err = unshare_fs(unshare_flags, &new_fs); 2001fb0a685cSDaniel Rebelo de Oliveira if (err) 20029bfb23fcSOleg Nesterov goto bad_unshare_out; 2003fb0a685cSDaniel Rebelo de Oliveira err = unshare_fd(unshare_flags, &new_fd); 2004fb0a685cSDaniel Rebelo de Oliveira if (err) 20059bfb23fcSOleg Nesterov goto bad_unshare_cleanup_fs; 2006b2e0d987SEric W. Biederman err = unshare_userns(unshare_flags, &new_cred); 2007fb0a685cSDaniel Rebelo de Oliveira if (err) 20089edff4abSManfred Spraul goto bad_unshare_cleanup_fd; 2009b2e0d987SEric W. Biederman err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 2010b2e0d987SEric W. Biederman new_cred, new_fs); 2011b2e0d987SEric W. Biederman if (err) 2012b2e0d987SEric W. Biederman goto bad_unshare_cleanup_cred; 2013cf2e340fSJANAK DESAI 2014b2e0d987SEric W. Biederman if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 20159edff4abSManfred Spraul if (do_sysvsem) { 20169edff4abSManfred Spraul /* 20179edff4abSManfred Spraul * CLONE_SYSVSEM is equivalent to sys_exit(). 20189edff4abSManfred Spraul */ 20199edff4abSManfred Spraul exit_sem(current); 20209edff4abSManfred Spraul } 2021ab602f79SJack Miller if (unshare_flags & CLONE_NEWIPC) { 2022ab602f79SJack Miller /* Orphan segments in old ns (see sem above). */ 2023ab602f79SJack Miller exit_shm(current); 2024ab602f79SJack Miller shm_init_task(current); 2025ab602f79SJack Miller } 2026ab516013SSerge E. Hallyn 20276f977e6bSAlan Cox if (new_nsproxy) 2028cf7b708cSPavel Emelyanov switch_task_namespaces(current, new_nsproxy); 2029cf2e340fSJANAK DESAI 2030cf7b708cSPavel Emelyanov task_lock(current); 2031cf7b708cSPavel Emelyanov 2032cf2e340fSJANAK DESAI if (new_fs) { 2033cf2e340fSJANAK DESAI fs = current->fs; 20342a4419b5SNick Piggin spin_lock(&fs->lock); 2035cf2e340fSJANAK DESAI current->fs = new_fs; 2036498052bbSAl Viro if (--fs->users) 2037498052bbSAl Viro new_fs = NULL; 2038498052bbSAl Viro else 2039cf2e340fSJANAK DESAI new_fs = fs; 20402a4419b5SNick Piggin spin_unlock(&fs->lock); 2041cf2e340fSJANAK DESAI } 2042cf2e340fSJANAK DESAI 2043cf2e340fSJANAK DESAI if (new_fd) { 2044cf2e340fSJANAK DESAI fd = current->files; 2045cf2e340fSJANAK DESAI current->files = new_fd; 2046cf2e340fSJANAK DESAI new_fd = fd; 2047cf2e340fSJANAK DESAI } 2048cf2e340fSJANAK DESAI 2049cf2e340fSJANAK DESAI task_unlock(current); 2050b2e0d987SEric W. Biederman 2051b2e0d987SEric W. Biederman if (new_cred) { 2052b2e0d987SEric W. Biederman /* Install the new user namespace */ 2053b2e0d987SEric W. Biederman commit_creds(new_cred); 2054b2e0d987SEric W. Biederman new_cred = NULL; 2055b2e0d987SEric W. Biederman } 2056cf2e340fSJANAK DESAI } 2057cf2e340fSJANAK DESAI 2058b2e0d987SEric W. Biederman bad_unshare_cleanup_cred: 2059b2e0d987SEric W. Biederman if (new_cred) 2060b2e0d987SEric W. Biederman put_cred(new_cred); 2061cf2e340fSJANAK DESAI bad_unshare_cleanup_fd: 2062cf2e340fSJANAK DESAI if (new_fd) 2063cf2e340fSJANAK DESAI put_files_struct(new_fd); 2064cf2e340fSJANAK DESAI 2065cf2e340fSJANAK DESAI bad_unshare_cleanup_fs: 2066cf2e340fSJANAK DESAI if (new_fs) 2067498052bbSAl Viro free_fs_struct(new_fs); 2068cf2e340fSJANAK DESAI 2069cf2e340fSJANAK DESAI bad_unshare_out: 2070cf2e340fSJANAK DESAI return err; 2071cf2e340fSJANAK DESAI } 20723b125388SAl Viro 20733b125388SAl Viro /* 20743b125388SAl Viro * Helper to unshare the files of the current task. 20753b125388SAl Viro * We don't want to expose copy_files internals to 20763b125388SAl Viro * the exec layer of the kernel. 20773b125388SAl Viro */ 20783b125388SAl Viro 20793b125388SAl Viro int unshare_files(struct files_struct **displaced) 20803b125388SAl Viro { 20813b125388SAl Viro struct task_struct *task = current; 208250704516SAl Viro struct files_struct *copy = NULL; 20833b125388SAl Viro int error; 20843b125388SAl Viro 20853b125388SAl Viro error = unshare_fd(CLONE_FILES, ©); 20863b125388SAl Viro if (error || !copy) { 20873b125388SAl Viro *displaced = NULL; 20883b125388SAl Viro return error; 20893b125388SAl Viro } 20903b125388SAl Viro *displaced = task->files; 20913b125388SAl Viro task_lock(task); 20923b125388SAl Viro task->files = copy; 20933b125388SAl Viro task_unlock(task); 20943b125388SAl Viro return 0; 20953b125388SAl Viro } 209616db3d3fSHeinrich Schuchardt 209716db3d3fSHeinrich Schuchardt int sysctl_max_threads(struct ctl_table *table, int write, 209816db3d3fSHeinrich Schuchardt void __user *buffer, size_t *lenp, loff_t *ppos) 209916db3d3fSHeinrich Schuchardt { 210016db3d3fSHeinrich Schuchardt struct ctl_table t; 210116db3d3fSHeinrich Schuchardt int ret; 210216db3d3fSHeinrich Schuchardt int threads = max_threads; 210316db3d3fSHeinrich Schuchardt int min = MIN_THREADS; 210416db3d3fSHeinrich Schuchardt int max = MAX_THREADS; 210516db3d3fSHeinrich Schuchardt 210616db3d3fSHeinrich Schuchardt t = *table; 210716db3d3fSHeinrich Schuchardt t.data = &threads; 210816db3d3fSHeinrich Schuchardt t.extra1 = &min; 210916db3d3fSHeinrich Schuchardt t.extra2 = &max; 211016db3d3fSHeinrich Schuchardt 211116db3d3fSHeinrich Schuchardt ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 211216db3d3fSHeinrich Schuchardt if (ret || !write) 211316db3d3fSHeinrich Schuchardt return ret; 211416db3d3fSHeinrich Schuchardt 211516db3d3fSHeinrich Schuchardt set_max_threads(threads); 211616db3d3fSHeinrich Schuchardt 211716db3d3fSHeinrich Schuchardt return 0; 211816db3d3fSHeinrich Schuchardt } 2119