4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 'fork.c' contains the help-routines for the 'fork' system call
9 * (see also entry.S and others).
10 * Fork is rather simple, once you get the hang of it, but the memory
11 * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/unistd.h>
17 #include <linux/module.h>
18 #include <linux/vmalloc.h>
19 #include <linux/completion.h>
20 #include <linux/personality.h>
21 #include <linux/mempolicy.h>
22 #include <linux/sem.h>
23 #include <linux/file.h>
24 #include <linux/fdtable.h>
25 #include <linux/iocontext.h>
26 #include <linux/key.h>
27 #include <linux/binfmts.h>
28 #include <linux/mman.h>
29 #include <linux/mmu_notifier.h>
32 #include <linux/vmacache.h>
33 #include <linux/nsproxy.h>
34 #include <linux/capability.h>
35 #include <linux/cpu.h>
36 #include <linux/cgroup.h>
37 #include <linux/security.h>
38 #include <linux/hugetlb.h>
39 #include <linux/seccomp.h>
40 #include <linux/swap.h>
41 #include <linux/syscalls.h>
42 #include <linux/jiffies.h>
43 #include <linux/futex.h>
44 #include <linux/compat.h>
45 #include <linux/kthread.h>
46 #include <linux/task_io_accounting_ops.h>
47 #include <linux/rcupdate.h>
48 #include <linux/ptrace.h>
49 #include <linux/mount.h>
50 #include <linux/audit.h>
51 #include <linux/memcontrol.h>
52 #include <linux/ftrace.h>
53 #include <linux/proc_fs.h>
54 #include <linux/profile.h>
55 #include <linux/rmap.h>
56 #include <linux/ksm.h>
57 #include <linux/acct.h>
58 #include <linux/tsacct_kern.h>
59 #include <linux/cn_proc.h>
60 #include <linux/freezer.h>
61 #include <linux/delayacct.h>
62 #include <linux/taskstats_kern.h>
63 #include <linux/random.h>
64 #include <linux/tty.h>
65 #include <linux/blkdev.h>
66 #include <linux/fs_struct.h>
67 #include <linux/magic.h>
68 #include <linux/perf_event.h>
69 #include <linux/posix-timers.h>
70 #include <linux/user-return-notifier.h>
71 #include <linux/oom.h>
72 #include <linux/khugepaged.h>
73 #include <linux/signalfd.h>
74 #include <linux/uprobes.h>
75 #include <linux/aio.h>
76 #include <linux/compiler.h>
77 #include <linux/sysctl.h>
79 #include <asm/pgtable.h>
80 #include <asm/pgalloc.h>
81 #include <asm/uaccess.h>
82 #include <asm/mmu_context.h>
83 #include <asm/cacheflush.h>
84 #include <asm/tlbflush.h>
86 #include <trace/events/sched.h>
88 #define CREATE_TRACE_POINTS
89 #include <trace/events/task.h>
91 extern int unprivileged_userns_clone
;
93 #define unprivileged_userns_clone 0
97 * Minimum number of threads to boot the kernel
99 #define MIN_THREADS 20
102 * Maximum number of threads
104 #define MAX_THREADS FUTEX_TID_MASK
107 * Protected counters by write_lock_irq(&tasklist_lock)
109 unsigned long total_forks
; /* Handle normal Linux uptimes. */
110 int nr_threads
; /* The idle threads do not count.. */
112 int max_threads
; /* tunable limit on nr_threads */
114 DEFINE_PER_CPU(unsigned long, process_counts
) = 0;
116 __cacheline_aligned
DEFINE_RWLOCK(tasklist_lock
); /* outer */
118 #ifdef CONFIG_PROVE_RCU
119 int lockdep_tasklist_lock_is_held(void)
121 return lockdep_is_held(&tasklist_lock
);
123 EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held
);
124 #endif /* #ifdef CONFIG_PROVE_RCU */
126 int nr_processes(void)
131 for_each_possible_cpu(cpu
)
132 total
+= per_cpu(process_counts
, cpu
);
137 void __weak
arch_release_task_struct(struct task_struct
*tsk
)
141 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
142 static struct kmem_cache
*task_struct_cachep
;
144 static inline struct task_struct
*alloc_task_struct_node(int node
)
146 return kmem_cache_alloc_node(task_struct_cachep
, GFP_KERNEL
, node
);
149 static inline void free_task_struct(struct task_struct
*tsk
)
151 kmem_cache_free(task_struct_cachep
, tsk
);
155 void __weak
arch_release_thread_info(struct thread_info
*ti
)
159 #ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR
162 * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
163 * kmemcache based allocator.
165 # if THREAD_SIZE >= PAGE_SIZE
166 static struct thread_info
*alloc_thread_info_node(struct task_struct
*tsk
,
169 struct page
*page
= alloc_kmem_pages_node(node
, THREADINFO_GFP
,
172 return page
? page_address(page
) : NULL
;
175 static inline void free_thread_info(struct thread_info
*ti
)
177 free_kmem_pages((unsigned long)ti
, THREAD_SIZE_ORDER
);
180 static struct kmem_cache
*thread_info_cache
;
182 static struct thread_info
*alloc_thread_info_node(struct task_struct
*tsk
,
185 return kmem_cache_alloc_node(thread_info_cache
, THREADINFO_GFP
, node
);
188 static void free_thread_info(struct thread_info
*ti
)
190 kmem_cache_free(thread_info_cache
, ti
);
193 void thread_info_cache_init(void)
195 thread_info_cache
= kmem_cache_create("thread_info", THREAD_SIZE
,
196 THREAD_SIZE
, 0, NULL
);
197 BUG_ON(thread_info_cache
== NULL
);
202 /* SLAB cache for signal_struct structures (tsk->signal) */
203 static struct kmem_cache
*signal_cachep
;
205 /* SLAB cache for sighand_struct structures (tsk->sighand) */
206 struct kmem_cache
*sighand_cachep
;
208 /* SLAB cache for files_struct structures (tsk->files) */
209 struct kmem_cache
*files_cachep
;
211 /* SLAB cache for fs_struct structures (tsk->fs) */
212 struct kmem_cache
*fs_cachep
;
214 /* SLAB cache for vm_area_struct structures */
215 struct kmem_cache
*vm_area_cachep
;
217 /* SLAB cache for mm_struct structures (tsk->mm) */
218 static struct kmem_cache
*mm_cachep
;
220 static void account_kernel_stack(struct thread_info
*ti
, int account
)
222 struct zone
*zone
= page_zone(virt_to_page(ti
));
224 mod_zone_page_state(zone
, NR_KERNEL_STACK
, account
);
227 void free_task(struct task_struct
*tsk
)
229 account_kernel_stack(tsk
->stack
, -1);
230 arch_release_thread_info(tsk
->stack
);
231 free_thread_info(tsk
->stack
);
232 rt_mutex_debug_task_free(tsk
);
233 ftrace_graph_exit_task(tsk
);
234 put_seccomp_filter(tsk
);
235 arch_release_task_struct(tsk
);
236 free_task_struct(tsk
);
238 EXPORT_SYMBOL(free_task
);
240 static inline void free_signal_struct(struct signal_struct
*sig
)
242 taskstats_tgid_free(sig
);
243 sched_autogroup_exit(sig
);
244 kmem_cache_free(signal_cachep
, sig
);
247 static inline void put_signal_struct(struct signal_struct
*sig
)
249 if (atomic_dec_and_test(&sig
->sigcnt
))
250 free_signal_struct(sig
);
253 void __put_task_struct(struct task_struct
*tsk
)
255 WARN_ON(!tsk
->exit_state
);
256 WARN_ON(atomic_read(&tsk
->usage
));
257 WARN_ON(tsk
== current
);
261 security_task_free(tsk
);
263 delayacct_tsk_free(tsk
);
264 put_signal_struct(tsk
->signal
);
266 if (!profile_handoff_task(tsk
))
269 EXPORT_SYMBOL_GPL(__put_task_struct
);
271 void __init __weak
arch_task_cache_init(void) { }
276 static void set_max_threads(unsigned int max_threads_suggested
)
281 * The number of threads shall be limited such that the thread
282 * structures may only consume a small part of the available memory.
284 if (fls64(totalram_pages
) + fls64(PAGE_SIZE
) > 64)
285 threads
= MAX_THREADS
;
287 threads
= div64_u64((u64
) totalram_pages
* (u64
) PAGE_SIZE
,
288 (u64
) THREAD_SIZE
* 8UL);
290 if (threads
> max_threads_suggested
)
291 threads
= max_threads_suggested
;
293 max_threads
= clamp_t(u64
, threads
, MIN_THREADS
, MAX_THREADS
);
296 #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
297 /* Initialized by the architecture: */
298 int arch_task_struct_size __read_mostly
;
301 void __init
fork_init(void)
303 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
304 #ifndef ARCH_MIN_TASKALIGN
305 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
307 /* create a slab on which task_structs can be allocated */
309 kmem_cache_create("task_struct", arch_task_struct_size
,
310 ARCH_MIN_TASKALIGN
, SLAB_PANIC
| SLAB_NOTRACK
, NULL
);
313 /* do the arch specific task caches init */
314 arch_task_cache_init();
316 set_max_threads(MAX_THREADS
);
318 init_task
.signal
->rlim
[RLIMIT_NPROC
].rlim_cur
= max_threads
/2;
319 init_task
.signal
->rlim
[RLIMIT_NPROC
].rlim_max
= max_threads
/2;
320 init_task
.signal
->rlim
[RLIMIT_SIGPENDING
] =
321 init_task
.signal
->rlim
[RLIMIT_NPROC
];
324 int __weak
arch_dup_task_struct(struct task_struct
*dst
,
325 struct task_struct
*src
)
331 void set_task_stack_end_magic(struct task_struct
*tsk
)
333 unsigned long *stackend
;
335 stackend
= end_of_stack(tsk
);
336 *stackend
= STACK_END_MAGIC
; /* for overflow detection */
339 static struct task_struct
*dup_task_struct(struct task_struct
*orig
, int node
)
341 struct task_struct
*tsk
;
342 struct thread_info
*ti
;
345 if (node
== NUMA_NO_NODE
)
346 node
= tsk_fork_get_node(orig
);
347 tsk
= alloc_task_struct_node(node
);
351 ti
= alloc_thread_info_node(tsk
, node
);
355 err
= arch_dup_task_struct(tsk
, orig
);
360 #ifdef CONFIG_SECCOMP
362 * We must handle setting up seccomp filters once we're under
363 * the sighand lock in case orig has changed between now and
364 * then. Until then, filter must be NULL to avoid messing up
365 * the usage counts on the error path calling free_task.
367 tsk
->seccomp
.filter
= NULL
;
370 setup_thread_stack(tsk
, orig
);
371 clear_user_return_notifier(tsk
);
372 clear_tsk_need_resched(tsk
);
373 set_task_stack_end_magic(tsk
);
375 #ifdef CONFIG_CC_STACKPROTECTOR
376 tsk
->stack_canary
= get_random_int();
380 * One for us, one for whoever does the "release_task()" (usually
383 atomic_set(&tsk
->usage
, 2);
384 #ifdef CONFIG_BLK_DEV_IO_TRACE
387 tsk
->splice_pipe
= NULL
;
388 tsk
->task_frag
.page
= NULL
;
389 tsk
->wake_q
.next
= NULL
;
391 account_kernel_stack(ti
, 1);
396 free_thread_info(ti
);
398 free_task_struct(tsk
);
403 static int dup_mmap(struct mm_struct
*mm
, struct mm_struct
*oldmm
)
405 struct vm_area_struct
*mpnt
, *tmp
, *prev
, **pprev
;
406 struct rb_node
**rb_link
, *rb_parent
;
408 unsigned long charge
;
410 uprobe_start_dup_mmap();
411 down_write(&oldmm
->mmap_sem
);
412 flush_cache_dup_mm(oldmm
);
413 uprobe_dup_mmap(oldmm
, mm
);
415 * Not linked in yet - no deadlock potential:
417 down_write_nested(&mm
->mmap_sem
, SINGLE_DEPTH_NESTING
);
419 /* No ordering required: file already has been exposed. */
420 RCU_INIT_POINTER(mm
->exe_file
, get_mm_exe_file(oldmm
));
422 mm
->total_vm
= oldmm
->total_vm
;
423 mm
->shared_vm
= oldmm
->shared_vm
;
424 mm
->exec_vm
= oldmm
->exec_vm
;
425 mm
->stack_vm
= oldmm
->stack_vm
;
427 rb_link
= &mm
->mm_rb
.rb_node
;
430 retval
= ksm_fork(mm
, oldmm
);
433 retval
= khugepaged_fork(mm
, oldmm
);
438 for (mpnt
= oldmm
->mmap
; mpnt
; mpnt
= mpnt
->vm_next
) {
441 if (mpnt
->vm_flags
& VM_DONTCOPY
) {
442 vm_stat_account(mm
, mpnt
->vm_flags
, mpnt
->vm_file
,
447 if (mpnt
->vm_flags
& VM_ACCOUNT
) {
448 unsigned long len
= vma_pages(mpnt
);
450 if (security_vm_enough_memory_mm(oldmm
, len
)) /* sic */
454 tmp
= kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
458 INIT_LIST_HEAD(&tmp
->anon_vma_chain
);
459 retval
= vma_dup_policy(mpnt
, tmp
);
461 goto fail_nomem_policy
;
463 if (anon_vma_fork(tmp
, mpnt
))
464 goto fail_nomem_anon_vma_fork
;
466 ~(VM_LOCKED
|VM_LOCKONFAULT
|VM_UFFD_MISSING
|VM_UFFD_WP
);
467 tmp
->vm_next
= tmp
->vm_prev
= NULL
;
468 tmp
->vm_userfaultfd_ctx
= NULL_VM_UFFD_CTX
;
471 struct inode
*inode
= file_inode(file
);
472 struct address_space
*mapping
= file
->f_mapping
;
475 if (tmp
->vm_flags
& VM_DENYWRITE
)
476 atomic_dec(&inode
->i_writecount
);
477 i_mmap_lock_write(mapping
);
478 if (tmp
->vm_flags
& VM_SHARED
)
479 atomic_inc(&mapping
->i_mmap_writable
);
480 flush_dcache_mmap_lock(mapping
);
481 /* insert tmp into the share list, just after mpnt */
482 vma_interval_tree_insert_after(tmp
, mpnt
,
484 flush_dcache_mmap_unlock(mapping
);
485 i_mmap_unlock_write(mapping
);
489 * Clear hugetlb-related page reserves for children. This only
490 * affects MAP_PRIVATE mappings. Faults generated by the child
491 * are not guaranteed to succeed, even if read-only
493 if (is_vm_hugetlb_page(tmp
))
494 reset_vma_resv_huge_pages(tmp
);
497 * Link in the new vma and copy the page table entries.
500 pprev
= &tmp
->vm_next
;
504 __vma_link_rb(mm
, tmp
, rb_link
, rb_parent
);
505 rb_link
= &tmp
->vm_rb
.rb_right
;
506 rb_parent
= &tmp
->vm_rb
;
509 retval
= copy_page_range(mm
, oldmm
, mpnt
);
511 if (tmp
->vm_ops
&& tmp
->vm_ops
->open
)
512 tmp
->vm_ops
->open(tmp
);
517 /* a new mm has just been created */
518 arch_dup_mmap(oldmm
, mm
);
521 up_write(&mm
->mmap_sem
);
523 up_write(&oldmm
->mmap_sem
);
524 uprobe_end_dup_mmap();
526 fail_nomem_anon_vma_fork
:
527 mpol_put(vma_policy(tmp
));
529 kmem_cache_free(vm_area_cachep
, tmp
);
532 vm_unacct_memory(charge
);
536 static inline int mm_alloc_pgd(struct mm_struct
*mm
)
538 mm
->pgd
= pgd_alloc(mm
);
539 if (unlikely(!mm
->pgd
))
544 static inline void mm_free_pgd(struct mm_struct
*mm
)
546 pgd_free(mm
, mm
->pgd
);
549 static int dup_mmap(struct mm_struct
*mm
, struct mm_struct
*oldmm
)
551 down_write(&oldmm
->mmap_sem
);
552 RCU_INIT_POINTER(mm
->exe_file
, get_mm_exe_file(oldmm
));
553 up_write(&oldmm
->mmap_sem
);
556 #define mm_alloc_pgd(mm) (0)
557 #define mm_free_pgd(mm)
558 #endif /* CONFIG_MMU */
560 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(mmlist_lock
);
562 #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
563 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
565 static unsigned long default_dump_filter
= MMF_DUMP_FILTER_DEFAULT
;
567 static int __init
coredump_filter_setup(char *s
)
569 default_dump_filter
=
570 (simple_strtoul(s
, NULL
, 0) << MMF_DUMP_FILTER_SHIFT
) &
571 MMF_DUMP_FILTER_MASK
;
575 __setup("coredump_filter=", coredump_filter_setup
);
577 #include <linux/init_task.h>
579 static void mm_init_aio(struct mm_struct
*mm
)
582 spin_lock_init(&mm
->ioctx_lock
);
583 mm
->ioctx_table
= NULL
;
587 static void mm_init_owner(struct mm_struct
*mm
, struct task_struct
*p
)
594 static struct mm_struct
*mm_init(struct mm_struct
*mm
, struct task_struct
*p
)
598 mm
->vmacache_seqnum
= 0;
599 atomic_set(&mm
->mm_users
, 1);
600 atomic_set(&mm
->mm_count
, 1);
601 init_rwsem(&mm
->mmap_sem
);
602 INIT_LIST_HEAD(&mm
->mmlist
);
603 mm
->core_state
= NULL
;
604 atomic_long_set(&mm
->nr_ptes
, 0);
609 memset(&mm
->rss_stat
, 0, sizeof(mm
->rss_stat
));
610 spin_lock_init(&mm
->page_table_lock
);
613 mm_init_owner(mm
, p
);
614 mmu_notifier_mm_init(mm
);
615 clear_tlb_flush_pending(mm
);
616 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
617 mm
->pmd_huge_pte
= NULL
;
621 mm
->flags
= current
->mm
->flags
& MMF_INIT_MASK
;
622 mm
->def_flags
= current
->mm
->def_flags
& VM_INIT_DEF_MASK
;
624 mm
->flags
= default_dump_filter
;
628 if (mm_alloc_pgd(mm
))
631 if (init_new_context(p
, mm
))
643 static void check_mm(struct mm_struct
*mm
)
647 for (i
= 0; i
< NR_MM_COUNTERS
; i
++) {
648 long x
= atomic_long_read(&mm
->rss_stat
.count
[i
]);
651 printk(KERN_ALERT
"BUG: Bad rss-counter state "
652 "mm:%p idx:%d val:%ld\n", mm
, i
, x
);
655 if (atomic_long_read(&mm
->nr_ptes
))
656 pr_alert("BUG: non-zero nr_ptes on freeing mm: %ld\n",
657 atomic_long_read(&mm
->nr_ptes
));
659 pr_alert("BUG: non-zero nr_pmds on freeing mm: %ld\n",
662 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
663 VM_BUG_ON_MM(mm
->pmd_huge_pte
, mm
);
668 * Allocate and initialize an mm_struct.
670 struct mm_struct
*mm_alloc(void)
672 struct mm_struct
*mm
;
678 memset(mm
, 0, sizeof(*mm
));
679 return mm_init(mm
, current
);
683 * Called when the last reference to the mm
684 * is dropped: either by a lazy thread or by
685 * mmput. Free the page directory and the mm.
687 void __mmdrop(struct mm_struct
*mm
)
689 BUG_ON(mm
== &init_mm
);
692 mmu_notifier_mm_destroy(mm
);
696 EXPORT_SYMBOL_GPL(__mmdrop
);
699 * Decrement the use count and release all resources for an mm.
701 void mmput(struct mm_struct
*mm
)
705 if (atomic_dec_and_test(&mm
->mm_users
)) {
706 uprobe_clear_state(mm
);
709 khugepaged_exit(mm
); /* must run before exit_mmap */
711 set_mm_exe_file(mm
, NULL
);
712 if (!list_empty(&mm
->mmlist
)) {
713 spin_lock(&mmlist_lock
);
714 list_del(&mm
->mmlist
);
715 spin_unlock(&mmlist_lock
);
718 module_put(mm
->binfmt
->module
);
722 EXPORT_SYMBOL_GPL(mmput
);
725 * set_mm_exe_file - change a reference to the mm's executable file
727 * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
729 * Main users are mmput() and sys_execve(). Callers prevent concurrent
730 * invocations: in mmput() nobody alive left, in execve task is single
731 * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the
732 * mm->exe_file, but does so without using set_mm_exe_file() in order
733 * to do avoid the need for any locks.
735 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
737 struct file
*old_exe_file
;
740 * It is safe to dereference the exe_file without RCU as
741 * this function is only called if nobody else can access
742 * this mm -- see comment above for justification.
744 old_exe_file
= rcu_dereference_raw(mm
->exe_file
);
747 get_file(new_exe_file
);
748 rcu_assign_pointer(mm
->exe_file
, new_exe_file
);
754 * get_mm_exe_file - acquire a reference to the mm's executable file
756 * Returns %NULL if mm has no associated executable file.
757 * User must release file via fput().
759 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
761 struct file
*exe_file
;
764 exe_file
= rcu_dereference(mm
->exe_file
);
765 if (exe_file
&& !get_file_rcu(exe_file
))
770 EXPORT_SYMBOL(get_mm_exe_file
);
773 * get_task_exe_file - acquire a reference to the task's executable file
775 * Returns %NULL if task's mm (if any) has no associated executable file or
776 * this is a kernel thread with borrowed mm (see the comment above get_task_mm).
777 * User must release file via fput().
779 struct file
*get_task_exe_file(struct task_struct
*task
)
781 struct file
*exe_file
= NULL
;
782 struct mm_struct
*mm
;
787 if (!(task
->flags
& PF_KTHREAD
))
788 exe_file
= get_mm_exe_file(mm
);
793 EXPORT_SYMBOL(get_task_exe_file
);
796 * get_task_mm - acquire a reference to the task's mm
798 * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning
799 * this kernel workthread has transiently adopted a user mm with use_mm,
800 * to do its AIO) is not set and if so returns a reference to it, after
801 * bumping up the use count. User must release the mm via mmput()
802 * after use. Typically used by /proc and ptrace.
804 struct mm_struct
*get_task_mm(struct task_struct
*task
)
806 struct mm_struct
*mm
;
811 if (task
->flags
& PF_KTHREAD
)
814 atomic_inc(&mm
->mm_users
);
819 EXPORT_SYMBOL_GPL(get_task_mm
);
821 struct mm_struct
*mm_access(struct task_struct
*task
, unsigned int mode
)
823 struct mm_struct
*mm
;
826 err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
830 mm
= get_task_mm(task
);
831 if (mm
&& mm
!= current
->mm
&&
832 !ptrace_may_access(task
, mode
)) {
834 mm
= ERR_PTR(-EACCES
);
836 mutex_unlock(&task
->signal
->cred_guard_mutex
);
841 static void complete_vfork_done(struct task_struct
*tsk
)
843 struct completion
*vfork
;
846 vfork
= tsk
->vfork_done
;
848 tsk
->vfork_done
= NULL
;
854 static int wait_for_vfork_done(struct task_struct
*child
,
855 struct completion
*vfork
)
859 freezer_do_not_count();
860 killed
= wait_for_completion_killable(vfork
);
865 child
->vfork_done
= NULL
;
869 put_task_struct(child
);
873 /* Please note the differences between mmput and mm_release.
874 * mmput is called whenever we stop holding onto a mm_struct,
875 * error success whatever.
877 * mm_release is called after a mm_struct has been removed
878 * from the current process.
880 * This difference is important for error handling, when we
881 * only half set up a mm_struct for a new process and need to restore
882 * the old one. Because we mmput the new mm_struct before
883 * restoring the old one. . .
884 * Eric Biederman 10 January 1998
886 void mm_release(struct task_struct
*tsk
, struct mm_struct
*mm
)
888 /* Get rid of any futexes when releasing the mm */
890 if (unlikely(tsk
->robust_list
)) {
891 exit_robust_list(tsk
);
892 tsk
->robust_list
= NULL
;
895 if (unlikely(tsk
->compat_robust_list
)) {
896 compat_exit_robust_list(tsk
);
897 tsk
->compat_robust_list
= NULL
;
900 if (unlikely(!list_empty(&tsk
->pi_state_list
)))
901 exit_pi_state_list(tsk
);
904 uprobe_free_utask(tsk
);
906 /* Get rid of any cached register state */
907 deactivate_mm(tsk
, mm
);
910 * If we're exiting normally, clear a user-space tid field if
911 * requested. We leave this alone when dying by signal, to leave
912 * the value intact in a core dump, and to save the unnecessary
913 * trouble, say, a killed vfork parent shouldn't touch this mm.
914 * Userland only wants this done for a sys_exit.
916 if (tsk
->clear_child_tid
) {
917 if (!(tsk
->flags
& PF_SIGNALED
) &&
918 atomic_read(&mm
->mm_users
) > 1) {
920 * We don't check the error code - if userspace has
921 * not set up a proper pointer then tough luck.
923 put_user(0, tsk
->clear_child_tid
);
924 sys_futex(tsk
->clear_child_tid
, FUTEX_WAKE
,
927 tsk
->clear_child_tid
= NULL
;
931 * All done, finally we can wake up parent and return this mm to him.
932 * Also kthread_stop() uses this completion for synchronization.
935 complete_vfork_done(tsk
);
939 * Allocate a new mm structure and copy contents from the
940 * mm structure of the passed in task structure.
942 static struct mm_struct
*dup_mm(struct task_struct
*tsk
)
944 struct mm_struct
*mm
, *oldmm
= current
->mm
;
951 memcpy(mm
, oldmm
, sizeof(*mm
));
953 if (!mm_init(mm
, tsk
))
956 err
= dup_mmap(mm
, oldmm
);
960 mm
->hiwater_rss
= get_mm_rss(mm
);
961 mm
->hiwater_vm
= mm
->total_vm
;
963 if (mm
->binfmt
&& !try_module_get(mm
->binfmt
->module
))
969 /* don't put binfmt in mmput, we haven't got module yet */
977 static int copy_mm(unsigned long clone_flags
, struct task_struct
*tsk
)
979 struct mm_struct
*mm
, *oldmm
;
982 tsk
->min_flt
= tsk
->maj_flt
= 0;
983 tsk
->nvcsw
= tsk
->nivcsw
= 0;
984 #ifdef CONFIG_DETECT_HUNG_TASK
985 tsk
->last_switch_count
= tsk
->nvcsw
+ tsk
->nivcsw
;
989 tsk
->active_mm
= NULL
;
992 * Are we cloning a kernel thread?
994 * We need to steal a active VM for that..
1000 /* initialize the new vmacache entries */
1001 vmacache_flush(tsk
);
1003 if (clone_flags
& CLONE_VM
) {
1004 atomic_inc(&oldmm
->mm_users
);
1016 tsk
->active_mm
= mm
;
1023 static int copy_fs(unsigned long clone_flags
, struct task_struct
*tsk
)
1025 struct fs_struct
*fs
= current
->fs
;
1026 if (clone_flags
& CLONE_FS
) {
1027 /* tsk->fs is already what we want */
1028 spin_lock(&fs
->lock
);
1030 spin_unlock(&fs
->lock
);
1034 spin_unlock(&fs
->lock
);
1037 tsk
->fs
= copy_fs_struct(fs
);
1043 static int copy_files(unsigned long clone_flags
, struct task_struct
*tsk
)
1045 struct files_struct
*oldf
, *newf
;
1049 * A background process may not have any files ...
1051 oldf
= current
->files
;
1055 if (clone_flags
& CLONE_FILES
) {
1056 atomic_inc(&oldf
->count
);
1060 newf
= dup_fd(oldf
, &error
);
1070 static int copy_io(unsigned long clone_flags
, struct task_struct
*tsk
)
1073 struct io_context
*ioc
= current
->io_context
;
1074 struct io_context
*new_ioc
;
1079 * Share io context with parent, if CLONE_IO is set
1081 if (clone_flags
& CLONE_IO
) {
1083 tsk
->io_context
= ioc
;
1084 } else if (ioprio_valid(ioc
->ioprio
)) {
1085 new_ioc
= get_task_io_context(tsk
, GFP_KERNEL
, NUMA_NO_NODE
);
1086 if (unlikely(!new_ioc
))
1089 new_ioc
->ioprio
= ioc
->ioprio
;
1090 put_io_context(new_ioc
);
1096 static int copy_sighand(unsigned long clone_flags
, struct task_struct
*tsk
)
1098 struct sighand_struct
*sig
;
1100 if (clone_flags
& CLONE_SIGHAND
) {
1101 atomic_inc(¤t
->sighand
->count
);
1104 sig
= kmem_cache_alloc(sighand_cachep
, GFP_KERNEL
);
1105 rcu_assign_pointer(tsk
->sighand
, sig
);
1109 atomic_set(&sig
->count
, 1);
1110 memcpy(sig
->action
, current
->sighand
->action
, sizeof(sig
->action
));
1114 void __cleanup_sighand(struct sighand_struct
*sighand
)
1116 if (atomic_dec_and_test(&sighand
->count
)) {
1117 signalfd_cleanup(sighand
);
1119 * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it
1120 * without an RCU grace period, see __lock_task_sighand().
1122 kmem_cache_free(sighand_cachep
, sighand
);
1127 * Initialize POSIX timer handling for a thread group.
1129 static void posix_cpu_timers_init_group(struct signal_struct
*sig
)
1131 unsigned long cpu_limit
;
1133 cpu_limit
= READ_ONCE(sig
->rlim
[RLIMIT_CPU
].rlim_cur
);
1134 if (cpu_limit
!= RLIM_INFINITY
) {
1135 sig
->cputime_expires
.prof_exp
= secs_to_cputime(cpu_limit
);
1136 sig
->cputimer
.running
= true;
1139 /* The timer lists. */
1140 INIT_LIST_HEAD(&sig
->cpu_timers
[0]);
1141 INIT_LIST_HEAD(&sig
->cpu_timers
[1]);
1142 INIT_LIST_HEAD(&sig
->cpu_timers
[2]);
1145 static int copy_signal(unsigned long clone_flags
, struct task_struct
*tsk
)
1147 struct signal_struct
*sig
;
1149 if (clone_flags
& CLONE_THREAD
)
1152 sig
= kmem_cache_zalloc(signal_cachep
, GFP_KERNEL
);
1157 sig
->nr_threads
= 1;
1158 atomic_set(&sig
->live
, 1);
1159 atomic_set(&sig
->sigcnt
, 1);
1161 /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
1162 sig
->thread_head
= (struct list_head
)LIST_HEAD_INIT(tsk
->thread_node
);
1163 tsk
->thread_node
= (struct list_head
)LIST_HEAD_INIT(sig
->thread_head
);
1165 init_waitqueue_head(&sig
->wait_chldexit
);
1166 sig
->curr_target
= tsk
;
1167 init_sigpending(&sig
->shared_pending
);
1168 INIT_LIST_HEAD(&sig
->posix_timers
);
1169 seqlock_init(&sig
->stats_lock
);
1170 prev_cputime_init(&sig
->prev_cputime
);
1172 hrtimer_init(&sig
->real_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1173 sig
->real_timer
.function
= it_real_fn
;
1175 task_lock(current
->group_leader
);
1176 memcpy(sig
->rlim
, current
->signal
->rlim
, sizeof sig
->rlim
);
1177 task_unlock(current
->group_leader
);
1179 posix_cpu_timers_init_group(sig
);
1181 tty_audit_fork(sig
);
1182 sched_autogroup_fork(sig
);
1184 sig
->oom_score_adj
= current
->signal
->oom_score_adj
;
1185 sig
->oom_score_adj_min
= current
->signal
->oom_score_adj_min
;
1187 sig
->has_child_subreaper
= current
->signal
->has_child_subreaper
||
1188 current
->signal
->is_child_subreaper
;
1190 mutex_init(&sig
->cred_guard_mutex
);
1195 static void copy_seccomp(struct task_struct
*p
)
1197 #ifdef CONFIG_SECCOMP
1199 * Must be called with sighand->lock held, which is common to
1200 * all threads in the group. Holding cred_guard_mutex is not
1201 * needed because this new task is not yet running and cannot
1204 assert_spin_locked(¤t
->sighand
->siglock
);
1206 /* Ref-count the new filter user, and assign it. */
1207 get_seccomp_filter(current
);
1208 p
->seccomp
= current
->seccomp
;
1211 * Explicitly enable no_new_privs here in case it got set
1212 * between the task_struct being duplicated and holding the
1213 * sighand lock. The seccomp state and nnp must be in sync.
1215 if (task_no_new_privs(current
))
1216 task_set_no_new_privs(p
);
1219 * If the parent gained a seccomp mode after copying thread
1220 * flags and between before we held the sighand lock, we have
1221 * to manually enable the seccomp thread flag here.
1223 if (p
->seccomp
.mode
!= SECCOMP_MODE_DISABLED
)
1224 set_tsk_thread_flag(p
, TIF_SECCOMP
);
1228 SYSCALL_DEFINE1(set_tid_address
, int __user
*, tidptr
)
1230 current
->clear_child_tid
= tidptr
;
1232 return task_pid_vnr(current
);
1235 static void rt_mutex_init_task(struct task_struct
*p
)
1237 raw_spin_lock_init(&p
->pi_lock
);
1238 #ifdef CONFIG_RT_MUTEXES
1239 p
->pi_waiters
= RB_ROOT
;
1240 p
->pi_waiters_leftmost
= NULL
;
1241 p
->pi_blocked_on
= NULL
;
1246 * Initialize POSIX timer handling for a single task.
1248 static void posix_cpu_timers_init(struct task_struct
*tsk
)
1250 tsk
->cputime_expires
.prof_exp
= 0;
1251 tsk
->cputime_expires
.virt_exp
= 0;
1252 tsk
->cputime_expires
.sched_exp
= 0;
1253 INIT_LIST_HEAD(&tsk
->cpu_timers
[0]);
1254 INIT_LIST_HEAD(&tsk
->cpu_timers
[1]);
1255 INIT_LIST_HEAD(&tsk
->cpu_timers
[2]);
1259 init_task_pid(struct task_struct
*task
, enum pid_type type
, struct pid
*pid
)
1261 task
->pids
[type
].pid
= pid
;
1265 * This creates a new process as a copy of the old one,
1266 * but does not actually start it yet.
1268 * It copies the registers, and all the appropriate
1269 * parts of the process environment (as per the clone
1270 * flags). The actual kick-off is left to the caller.
1272 static struct task_struct
*copy_process(unsigned long clone_flags
,
1273 unsigned long stack_start
,
1274 unsigned long stack_size
,
1275 int __user
*child_tidptr
,
1282 struct task_struct
*p
;
1283 void *cgrp_ss_priv
[CGROUP_CANFORK_COUNT
] = {};
1285 if ((clone_flags
& (CLONE_NEWNS
|CLONE_FS
)) == (CLONE_NEWNS
|CLONE_FS
))
1286 return ERR_PTR(-EINVAL
);
1288 if ((clone_flags
& (CLONE_NEWUSER
|CLONE_FS
)) == (CLONE_NEWUSER
|CLONE_FS
))
1289 return ERR_PTR(-EINVAL
);
1291 if ((clone_flags
& CLONE_NEWUSER
) && !unprivileged_userns_clone
)
1292 if (!capable(CAP_SYS_ADMIN
))
1293 return ERR_PTR(-EPERM
);
1296 * Thread groups must share signals as well, and detached threads
1297 * can only be started up within the thread group.
1299 if ((clone_flags
& CLONE_THREAD
) && !(clone_flags
& CLONE_SIGHAND
))
1300 return ERR_PTR(-EINVAL
);
1303 * Shared signal handlers imply shared VM. By way of the above,
1304 * thread groups also imply shared VM. Blocking this case allows
1305 * for various simplifications in other code.
1307 if ((clone_flags
& CLONE_SIGHAND
) && !(clone_flags
& CLONE_VM
))
1308 return ERR_PTR(-EINVAL
);
1311 * Siblings of global init remain as zombies on exit since they are
1312 * not reaped by their parent (swapper). To solve this and to avoid
1313 * multi-rooted process trees, prevent global and container-inits
1314 * from creating siblings.
1316 if ((clone_flags
& CLONE_PARENT
) &&
1317 current
->signal
->flags
& SIGNAL_UNKILLABLE
)
1318 return ERR_PTR(-EINVAL
);
1321 * If the new process will be in a different pid or user namespace
1322 * do not allow it to share a thread group with the forking task.
1324 if (clone_flags
& CLONE_THREAD
) {
1325 if ((clone_flags
& (CLONE_NEWUSER
| CLONE_NEWPID
)) ||
1326 (task_active_pid_ns(current
) !=
1327 current
->nsproxy
->pid_ns_for_children
))
1328 return ERR_PTR(-EINVAL
);
1331 retval
= security_task_create(clone_flags
);
1336 p
= dup_task_struct(current
, node
);
1340 ftrace_graph_init_task(p
);
1342 rt_mutex_init_task(p
);
1344 #ifdef CONFIG_PROVE_LOCKING
1345 DEBUG_LOCKS_WARN_ON(!p
->hardirqs_enabled
);
1346 DEBUG_LOCKS_WARN_ON(!p
->softirqs_enabled
);
1349 if (atomic_read(&p
->real_cred
->user
->processes
) >=
1350 task_rlimit(p
, RLIMIT_NPROC
)) {
1351 if (p
->real_cred
->user
!= INIT_USER
&&
1352 !capable(CAP_SYS_RESOURCE
) && !capable(CAP_SYS_ADMIN
))
1355 current
->flags
&= ~PF_NPROC_EXCEEDED
;
1357 retval
= copy_creds(p
, clone_flags
);
1362 * If multiple threads are within copy_process(), then this check
1363 * triggers too late. This doesn't hurt, the check is only there
1364 * to stop root fork bombs.
1367 if (nr_threads
>= max_threads
)
1368 goto bad_fork_cleanup_count
;
1370 delayacct_tsk_init(p
); /* Must remain after dup_task_struct() */
1371 p
->flags
&= ~(PF_SUPERPRIV
| PF_WQ_WORKER
);
1372 p
->flags
|= PF_FORKNOEXEC
;
1373 INIT_LIST_HEAD(&p
->children
);
1374 INIT_LIST_HEAD(&p
->sibling
);
1375 rcu_copy_process(p
);
1376 p
->vfork_done
= NULL
;
1377 spin_lock_init(&p
->alloc_lock
);
1379 init_sigpending(&p
->pending
);
1381 p
->utime
= p
->stime
= p
->gtime
= 0;
1382 p
->utimescaled
= p
->stimescaled
= 0;
1383 prev_cputime_init(&p
->prev_cputime
);
1385 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1386 seqlock_init(&p
->vtime_seqlock
);
1388 p
->vtime_snap_whence
= VTIME_SLEEPING
;
1391 #if defined(SPLIT_RSS_COUNTING)
1392 memset(&p
->rss_stat
, 0, sizeof(p
->rss_stat
));
1395 p
->default_timer_slack_ns
= current
->timer_slack_ns
;
1397 task_io_accounting_init(&p
->ioac
);
1398 acct_clear_integrals(p
);
1400 posix_cpu_timers_init(p
);
1402 p
->start_time
= ktime_get_ns();
1403 p
->real_start_time
= ktime_get_boot_ns();
1404 p
->io_context
= NULL
;
1405 p
->audit_context
= NULL
;
1408 p
->mempolicy
= mpol_dup(p
->mempolicy
);
1409 if (IS_ERR(p
->mempolicy
)) {
1410 retval
= PTR_ERR(p
->mempolicy
);
1411 p
->mempolicy
= NULL
;
1412 goto bad_fork_cleanup_threadgroup_lock
;
1415 #ifdef CONFIG_CPUSETS
1416 p
->cpuset_mem_spread_rotor
= NUMA_NO_NODE
;
1417 p
->cpuset_slab_spread_rotor
= NUMA_NO_NODE
;
1418 seqcount_init(&p
->mems_allowed_seq
);
1420 #ifdef CONFIG_TRACE_IRQFLAGS
1422 p
->hardirqs_enabled
= 0;
1423 p
->hardirq_enable_ip
= 0;
1424 p
->hardirq_enable_event
= 0;
1425 p
->hardirq_disable_ip
= _THIS_IP_
;
1426 p
->hardirq_disable_event
= 0;
1427 p
->softirqs_enabled
= 1;
1428 p
->softirq_enable_ip
= _THIS_IP_
;
1429 p
->softirq_enable_event
= 0;
1430 p
->softirq_disable_ip
= 0;
1431 p
->softirq_disable_event
= 0;
1432 p
->hardirq_context
= 0;
1433 p
->softirq_context
= 0;
1436 p
->pagefault_disabled
= 0;
1438 #ifdef CONFIG_LOCKDEP
1439 p
->lockdep_depth
= 0; /* no locks held yet */
1440 p
->curr_chain_key
= 0;
1441 p
->lockdep_recursion
= 0;
1444 #ifdef CONFIG_DEBUG_MUTEXES
1445 p
->blocked_on
= NULL
; /* not blocked yet */
1447 #ifdef CONFIG_BCACHE
1448 p
->sequential_io
= 0;
1449 p
->sequential_io_avg
= 0;
1452 /* Perform scheduler related setup. Assign this task to a CPU. */
1453 retval
= sched_fork(clone_flags
, p
);
1455 goto bad_fork_cleanup_policy
;
1457 retval
= perf_event_init_task(p
);
1459 goto bad_fork_cleanup_policy
;
1460 retval
= audit_alloc(p
);
1462 goto bad_fork_cleanup_perf
;
1463 /* copy all the process information */
1465 retval
= copy_semundo(clone_flags
, p
);
1467 goto bad_fork_cleanup_audit
;
1468 retval
= copy_files(clone_flags
, p
);
1470 goto bad_fork_cleanup_semundo
;
1471 retval
= copy_fs(clone_flags
, p
);
1473 goto bad_fork_cleanup_files
;
1474 retval
= copy_sighand(clone_flags
, p
);
1476 goto bad_fork_cleanup_fs
;
1477 retval
= copy_signal(clone_flags
, p
);
1479 goto bad_fork_cleanup_sighand
;
1480 retval
= copy_mm(clone_flags
, p
);
1482 goto bad_fork_cleanup_signal
;
1483 retval
= copy_namespaces(clone_flags
, p
);
1485 goto bad_fork_cleanup_mm
;
1486 retval
= copy_io(clone_flags
, p
);
1488 goto bad_fork_cleanup_namespaces
;
1489 retval
= copy_thread_tls(clone_flags
, stack_start
, stack_size
, p
, tls
);
1491 goto bad_fork_cleanup_io
;
1493 if (pid
!= &init_struct_pid
) {
1494 pid
= alloc_pid(p
->nsproxy
->pid_ns_for_children
);
1496 retval
= PTR_ERR(pid
);
1497 goto bad_fork_cleanup_io
;
1501 p
->set_child_tid
= (clone_flags
& CLONE_CHILD_SETTID
) ? child_tidptr
: NULL
;
1503 * Clear TID on mm_release()?
1505 p
->clear_child_tid
= (clone_flags
& CLONE_CHILD_CLEARTID
) ? child_tidptr
: NULL
;
1510 p
->robust_list
= NULL
;
1511 #ifdef CONFIG_COMPAT
1512 p
->compat_robust_list
= NULL
;
1514 INIT_LIST_HEAD(&p
->pi_state_list
);
1515 p
->pi_state_cache
= NULL
;
1518 * sigaltstack should be cleared when sharing the same VM
1520 if ((clone_flags
& (CLONE_VM
|CLONE_VFORK
)) == CLONE_VM
)
1521 p
->sas_ss_sp
= p
->sas_ss_size
= 0;
1524 * Syscall tracing and stepping should be turned off in the
1525 * child regardless of CLONE_PTRACE.
1527 user_disable_single_step(p
);
1528 clear_tsk_thread_flag(p
, TIF_SYSCALL_TRACE
);
1529 #ifdef TIF_SYSCALL_EMU
1530 clear_tsk_thread_flag(p
, TIF_SYSCALL_EMU
);
1532 clear_all_latency_tracing(p
);
1534 /* ok, now we should be set up.. */
1535 p
->pid
= pid_nr(pid
);
1536 if (clone_flags
& CLONE_THREAD
) {
1537 p
->exit_signal
= -1;
1538 p
->group_leader
= current
->group_leader
;
1539 p
->tgid
= current
->tgid
;
1541 if (clone_flags
& CLONE_PARENT
)
1542 p
->exit_signal
= current
->group_leader
->exit_signal
;
1544 p
->exit_signal
= (clone_flags
& CSIGNAL
);
1545 p
->group_leader
= p
;
1550 p
->nr_dirtied_pause
= 128 >> (PAGE_SHIFT
- 10);
1551 p
->dirty_paused_when
= 0;
1553 p
->pdeath_signal
= 0;
1554 INIT_LIST_HEAD(&p
->thread_group
);
1555 p
->task_works
= NULL
;
1557 threadgroup_change_begin(current
);
1559 * Ensure that the cgroup subsystem policies allow the new process to be
1560 * forked. It should be noted the the new process's css_set can be changed
1561 * between here and cgroup_post_fork() if an organisation operation is in
1564 retval
= cgroup_can_fork(p
, cgrp_ss_priv
);
1566 goto bad_fork_free_pid
;
1569 * Make it visible to the rest of the system, but dont wake it up yet.
1570 * Need tasklist lock for parent etc handling!
1572 write_lock_irq(&tasklist_lock
);
1574 /* CLONE_PARENT re-uses the old parent */
1575 if (clone_flags
& (CLONE_PARENT
|CLONE_THREAD
)) {
1576 p
->real_parent
= current
->real_parent
;
1577 p
->parent_exec_id
= current
->parent_exec_id
;
1579 p
->real_parent
= current
;
1580 p
->parent_exec_id
= current
->self_exec_id
;
1583 spin_lock(¤t
->sighand
->siglock
);
1586 * Copy seccomp details explicitly here, in case they were changed
1587 * before holding sighand lock.
1592 * Process group and session signals need to be delivered to just the
1593 * parent before the fork or both the parent and the child after the
1594 * fork. Restart if a signal comes in before we add the new process to
1595 * it's process group.
1596 * A fatal signal pending means that current will exit, so the new
1597 * thread can't slip out of an OOM kill (or normal SIGKILL).
1599 recalc_sigpending();
1600 if (signal_pending(current
)) {
1601 spin_unlock(¤t
->sighand
->siglock
);
1602 write_unlock_irq(&tasklist_lock
);
1603 retval
= -ERESTARTNOINTR
;
1604 goto bad_fork_cancel_cgroup
;
1607 if (likely(p
->pid
)) {
1608 ptrace_init_task(p
, (clone_flags
& CLONE_PTRACE
) || trace
);
1610 init_task_pid(p
, PIDTYPE_PID
, pid
);
1611 if (thread_group_leader(p
)) {
1612 init_task_pid(p
, PIDTYPE_PGID
, task_pgrp(current
));
1613 init_task_pid(p
, PIDTYPE_SID
, task_session(current
));
1615 if (is_child_reaper(pid
)) {
1616 ns_of_pid(pid
)->child_reaper
= p
;
1617 p
->signal
->flags
|= SIGNAL_UNKILLABLE
;
1620 p
->signal
->leader_pid
= pid
;
1621 p
->signal
->tty
= tty_kref_get(current
->signal
->tty
);
1622 list_add_tail(&p
->sibling
, &p
->real_parent
->children
);
1623 list_add_tail_rcu(&p
->tasks
, &init_task
.tasks
);
1624 attach_pid(p
, PIDTYPE_PGID
);
1625 attach_pid(p
, PIDTYPE_SID
);
1626 __this_cpu_inc(process_counts
);
1628 current
->signal
->nr_threads
++;
1629 atomic_inc(¤t
->signal
->live
);
1630 atomic_inc(¤t
->signal
->sigcnt
);
1631 list_add_tail_rcu(&p
->thread_group
,
1632 &p
->group_leader
->thread_group
);
1633 list_add_tail_rcu(&p
->thread_node
,
1634 &p
->signal
->thread_head
);
1636 attach_pid(p
, PIDTYPE_PID
);
1641 spin_unlock(¤t
->sighand
->siglock
);
1642 syscall_tracepoint_update(p
);
1643 write_unlock_irq(&tasklist_lock
);
1645 proc_fork_connector(p
);
1646 cgroup_post_fork(p
, cgrp_ss_priv
);
1647 threadgroup_change_end(current
);
1650 trace_task_newtask(p
, clone_flags
);
1651 uprobe_copy_process(p
, clone_flags
);
1655 bad_fork_cancel_cgroup
:
1656 cgroup_cancel_fork(p
, cgrp_ss_priv
);
1658 threadgroup_change_end(current
);
1659 if (pid
!= &init_struct_pid
)
1661 bad_fork_cleanup_io
:
1664 bad_fork_cleanup_namespaces
:
1665 exit_task_namespaces(p
);
1666 bad_fork_cleanup_mm
:
1669 bad_fork_cleanup_signal
:
1670 if (!(clone_flags
& CLONE_THREAD
))
1671 free_signal_struct(p
->signal
);
1672 bad_fork_cleanup_sighand
:
1673 __cleanup_sighand(p
->sighand
);
1674 bad_fork_cleanup_fs
:
1675 exit_fs(p
); /* blocking */
1676 bad_fork_cleanup_files
:
1677 exit_files(p
); /* blocking */
1678 bad_fork_cleanup_semundo
:
1680 bad_fork_cleanup_audit
:
1682 bad_fork_cleanup_perf
:
1683 perf_event_free_task(p
);
1684 bad_fork_cleanup_policy
:
1686 mpol_put(p
->mempolicy
);
1687 bad_fork_cleanup_threadgroup_lock
:
1689 delayacct_tsk_free(p
);
1690 bad_fork_cleanup_count
:
1691 atomic_dec(&p
->cred
->user
->processes
);
1696 return ERR_PTR(retval
);
1699 static inline void init_idle_pids(struct pid_link
*links
)
1703 for (type
= PIDTYPE_PID
; type
< PIDTYPE_MAX
; ++type
) {
1704 INIT_HLIST_NODE(&links
[type
].node
); /* not really needed */
1705 links
[type
].pid
= &init_struct_pid
;
1709 struct task_struct
*fork_idle(int cpu
)
1711 struct task_struct
*task
;
1712 task
= copy_process(CLONE_VM
, 0, 0, NULL
, &init_struct_pid
, 0, 0,
1714 if (!IS_ERR(task
)) {
1715 init_idle_pids(task
->pids
);
1716 init_idle(task
, cpu
);
1723 * Ok, this is the main fork-routine.
1725 * It copies the process, and if successful kick-starts
1726 * it and waits for it to finish using the VM if required.
1728 long _do_fork(unsigned long clone_flags
,
1729 unsigned long stack_start
,
1730 unsigned long stack_size
,
1731 int __user
*parent_tidptr
,
1732 int __user
*child_tidptr
,
1735 struct task_struct
*p
;
1740 * Determine whether and which event to report to ptracer. When
1741 * called from kernel_thread or CLONE_UNTRACED is explicitly
1742 * requested, no event is reported; otherwise, report if the event
1743 * for the type of forking is enabled.
1745 if (!(clone_flags
& CLONE_UNTRACED
)) {
1746 if (clone_flags
& CLONE_VFORK
)
1747 trace
= PTRACE_EVENT_VFORK
;
1748 else if ((clone_flags
& CSIGNAL
) != SIGCHLD
)
1749 trace
= PTRACE_EVENT_CLONE
;
1751 trace
= PTRACE_EVENT_FORK
;
1753 if (likely(!ptrace_event_enabled(current
, trace
)))
1757 p
= copy_process(clone_flags
, stack_start
, stack_size
,
1758 child_tidptr
, NULL
, trace
, tls
, NUMA_NO_NODE
);
1760 * Do this prior waking up the new thread - the thread pointer
1761 * might get invalid after that point, if the thread exits quickly.
1764 struct completion vfork
;
1767 trace_sched_process_fork(current
, p
);
1769 pid
= get_task_pid(p
, PIDTYPE_PID
);
1772 if (clone_flags
& CLONE_PARENT_SETTID
)
1773 put_user(nr
, parent_tidptr
);
1775 if (clone_flags
& CLONE_VFORK
) {
1776 p
->vfork_done
= &vfork
;
1777 init_completion(&vfork
);
1781 wake_up_new_task(p
);
1783 /* forking complete and child started to run, tell ptracer */
1784 if (unlikely(trace
))
1785 ptrace_event_pid(trace
, pid
);
1787 if (clone_flags
& CLONE_VFORK
) {
1788 if (!wait_for_vfork_done(p
, &vfork
))
1789 ptrace_event_pid(PTRACE_EVENT_VFORK_DONE
, pid
);
1799 #ifndef CONFIG_HAVE_COPY_THREAD_TLS
1800 /* For compatibility with architectures that call do_fork directly rather than
1801 * using the syscall entry points below. */
1802 long do_fork(unsigned long clone_flags
,
1803 unsigned long stack_start
,
1804 unsigned long stack_size
,
1805 int __user
*parent_tidptr
,
1806 int __user
*child_tidptr
)
1808 return _do_fork(clone_flags
, stack_start
, stack_size
,
1809 parent_tidptr
, child_tidptr
, 0);
1814 * Create a kernel thread.
1816 pid_t
kernel_thread(int (*fn
)(void *), void *arg
, unsigned long flags
)
1818 return _do_fork(flags
|CLONE_VM
|CLONE_UNTRACED
, (unsigned long)fn
,
1819 (unsigned long)arg
, NULL
, NULL
, 0);
1822 #ifdef __ARCH_WANT_SYS_FORK
1823 SYSCALL_DEFINE0(fork
)
1826 return _do_fork(SIGCHLD
, 0, 0, NULL
, NULL
, 0);
1828 /* can not support in nommu mode */
1834 #ifdef __ARCH_WANT_SYS_VFORK
1835 SYSCALL_DEFINE0(vfork
)
1837 return _do_fork(CLONE_VFORK
| CLONE_VM
| SIGCHLD
, 0,
1842 #ifdef __ARCH_WANT_SYS_CLONE
1843 #ifdef CONFIG_CLONE_BACKWARDS
1844 SYSCALL_DEFINE5(clone
, unsigned long, clone_flags
, unsigned long, newsp
,
1845 int __user
*, parent_tidptr
,
1847 int __user
*, child_tidptr
)
1848 #elif defined(CONFIG_CLONE_BACKWARDS2)
1849 SYSCALL_DEFINE5(clone
, unsigned long, newsp
, unsigned long, clone_flags
,
1850 int __user
*, parent_tidptr
,
1851 int __user
*, child_tidptr
,
1853 #elif defined(CONFIG_CLONE_BACKWARDS3)
1854 SYSCALL_DEFINE6(clone
, unsigned long, clone_flags
, unsigned long, newsp
,
1856 int __user
*, parent_tidptr
,
1857 int __user
*, child_tidptr
,
1860 SYSCALL_DEFINE5(clone
, unsigned long, clone_flags
, unsigned long, newsp
,
1861 int __user
*, parent_tidptr
,
1862 int __user
*, child_tidptr
,
1866 return _do_fork(clone_flags
, newsp
, 0, parent_tidptr
, child_tidptr
, tls
);
1870 #ifndef ARCH_MIN_MMSTRUCT_ALIGN
1871 #define ARCH_MIN_MMSTRUCT_ALIGN 0
1874 static void sighand_ctor(void *data
)
1876 struct sighand_struct
*sighand
= data
;
1878 spin_lock_init(&sighand
->siglock
);
1879 init_waitqueue_head(&sighand
->signalfd_wqh
);
1882 void __init
proc_caches_init(void)
1884 sighand_cachep
= kmem_cache_create("sighand_cache",
1885 sizeof(struct sighand_struct
), 0,
1886 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
|SLAB_DESTROY_BY_RCU
|
1887 SLAB_NOTRACK
, sighand_ctor
);
1888 signal_cachep
= kmem_cache_create("signal_cache",
1889 sizeof(struct signal_struct
), 0,
1890 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
|SLAB_NOTRACK
, NULL
);
1891 files_cachep
= kmem_cache_create("files_cache",
1892 sizeof(struct files_struct
), 0,
1893 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
|SLAB_NOTRACK
, NULL
);
1894 fs_cachep
= kmem_cache_create("fs_cache",
1895 sizeof(struct fs_struct
), 0,
1896 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
|SLAB_NOTRACK
, NULL
);
1898 * FIXME! The "sizeof(struct mm_struct)" currently includes the
1899 * whole struct cpumask for the OFFSTACK case. We could change
1900 * this to *only* allocate as much of it as required by the
1901 * maximum number of CPU's we can ever have. The cpumask_allocation
1902 * is at the end of the structure, exactly for that reason.
1904 mm_cachep
= kmem_cache_create("mm_struct",
1905 sizeof(struct mm_struct
), ARCH_MIN_MMSTRUCT_ALIGN
,
1906 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
|SLAB_NOTRACK
, NULL
);
1907 vm_area_cachep
= KMEM_CACHE(vm_area_struct
, SLAB_PANIC
);
1909 nsproxy_cache_init();
1913 * Check constraints on flags passed to the unshare system call.
1915 static int check_unshare_flags(unsigned long unshare_flags
)
1917 if (unshare_flags
& ~(CLONE_THREAD
|CLONE_FS
|CLONE_NEWNS
|CLONE_SIGHAND
|
1918 CLONE_VM
|CLONE_FILES
|CLONE_SYSVSEM
|
1919 CLONE_NEWUTS
|CLONE_NEWIPC
|CLONE_NEWNET
|
1920 CLONE_NEWUSER
|CLONE_NEWPID
|CLONE_NEWCGROUP
))
1923 * Not implemented, but pretend it works if there is nothing
1924 * to unshare. Note that unsharing the address space or the
1925 * signal handlers also need to unshare the signal queues (aka
1928 if (unshare_flags
& (CLONE_THREAD
| CLONE_SIGHAND
| CLONE_VM
)) {
1929 if (!thread_group_empty(current
))
1932 if (unshare_flags
& (CLONE_SIGHAND
| CLONE_VM
)) {
1933 if (atomic_read(¤t
->sighand
->count
) > 1)
1936 if (unshare_flags
& CLONE_VM
) {
1937 if (!current_is_single_threaded())
1945 * Unshare the filesystem structure if it is being shared
1947 static int unshare_fs(unsigned long unshare_flags
, struct fs_struct
**new_fsp
)
1949 struct fs_struct
*fs
= current
->fs
;
1951 if (!(unshare_flags
& CLONE_FS
) || !fs
)
1954 /* don't need lock here; in the worst case we'll do useless copy */
1958 *new_fsp
= copy_fs_struct(fs
);
1966 * Unshare file descriptor table if it is being shared
1968 static int unshare_fd(unsigned long unshare_flags
, struct files_struct
**new_fdp
)
1970 struct files_struct
*fd
= current
->files
;
1973 if ((unshare_flags
& CLONE_FILES
) &&
1974 (fd
&& atomic_read(&fd
->count
) > 1)) {
1975 *new_fdp
= dup_fd(fd
, &error
);
1984 * unshare allows a process to 'unshare' part of the process
1985 * context which was originally shared using clone. copy_*
1986 * functions used by do_fork() cannot be used here directly
1987 * because they modify an inactive task_struct that is being
1988 * constructed. Here we are modifying the current, active,
1991 SYSCALL_DEFINE1(unshare
, unsigned long, unshare_flags
)
1993 struct fs_struct
*fs
, *new_fs
= NULL
;
1994 struct files_struct
*fd
, *new_fd
= NULL
;
1995 struct cred
*new_cred
= NULL
;
1996 struct nsproxy
*new_nsproxy
= NULL
;
2001 * If unsharing a user namespace must also unshare the thread group
2002 * and unshare the filesystem root and working directories.
2004 if (unshare_flags
& CLONE_NEWUSER
)
2005 unshare_flags
|= CLONE_THREAD
| CLONE_FS
;
2007 * If unsharing vm, must also unshare signal handlers.
2009 if (unshare_flags
& CLONE_VM
)
2010 unshare_flags
|= CLONE_SIGHAND
;
2012 * If unsharing a signal handlers, must also unshare the signal queues.
2014 if (unshare_flags
& CLONE_SIGHAND
)
2015 unshare_flags
|= CLONE_THREAD
;
2017 * If unsharing namespace, must also unshare filesystem information.
2019 if (unshare_flags
& CLONE_NEWNS
)
2020 unshare_flags
|= CLONE_FS
;
2022 if ((unshare_flags
& CLONE_NEWUSER
) && !unprivileged_userns_clone
) {
2024 if (!capable(CAP_SYS_ADMIN
))
2025 goto bad_unshare_out
;
2028 err
= check_unshare_flags(unshare_flags
);
2030 goto bad_unshare_out
;
2032 * CLONE_NEWIPC must also detach from the undolist: after switching
2033 * to a new ipc namespace, the semaphore arrays from the old
2034 * namespace are unreachable.
2036 if (unshare_flags
& (CLONE_NEWIPC
|CLONE_SYSVSEM
))
2038 err
= unshare_fs(unshare_flags
, &new_fs
);
2040 goto bad_unshare_out
;
2041 err
= unshare_fd(unshare_flags
, &new_fd
);
2043 goto bad_unshare_cleanup_fs
;
2044 err
= unshare_userns(unshare_flags
, &new_cred
);
2046 goto bad_unshare_cleanup_fd
;
2047 err
= unshare_nsproxy_namespaces(unshare_flags
, &new_nsproxy
,
2050 goto bad_unshare_cleanup_cred
;
2052 if (new_fs
|| new_fd
|| do_sysvsem
|| new_cred
|| new_nsproxy
) {
2055 * CLONE_SYSVSEM is equivalent to sys_exit().
2059 if (unshare_flags
& CLONE_NEWIPC
) {
2060 /* Orphan segments in old ns (see sem above). */
2062 shm_init_task(current
);
2066 switch_task_namespaces(current
, new_nsproxy
);
2072 spin_lock(&fs
->lock
);
2073 current
->fs
= new_fs
;
2078 spin_unlock(&fs
->lock
);
2082 fd
= current
->files
;
2083 current
->files
= new_fd
;
2087 task_unlock(current
);
2090 /* Install the new user namespace */
2091 commit_creds(new_cred
);
2096 bad_unshare_cleanup_cred
:
2099 bad_unshare_cleanup_fd
:
2101 put_files_struct(new_fd
);
2103 bad_unshare_cleanup_fs
:
2105 free_fs_struct(new_fs
);
2112 * Helper to unshare the files of the current task.
2113 * We don't want to expose copy_files internals to
2114 * the exec layer of the kernel.
2117 int unshare_files(struct files_struct
**displaced
)
2119 struct task_struct
*task
= current
;
2120 struct files_struct
*copy
= NULL
;
2123 error
= unshare_fd(CLONE_FILES
, ©
);
2124 if (error
|| !copy
) {
2128 *displaced
= task
->files
;
2135 int sysctl_max_threads(struct ctl_table
*table
, int write
,
2136 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
2140 int threads
= max_threads
;
2141 int min
= MIN_THREADS
;
2142 int max
= MAX_THREADS
;
2149 ret
= proc_dointvec_minmax(&t
, write
, buffer
, lenp
, ppos
);
2153 set_max_threads(threads
);