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/config.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/unistd.h>
18 #include <linux/smp_lock.h>
19 #include <linux/module.h>
20 #include <linux/vmalloc.h>
21 #include <linux/completion.h>
22 #include <linux/namespace.h>
23 #include <linux/personality.h>
24 #include <linux/mempolicy.h>
25 #include <linux/sem.h>
26 #include <linux/file.h>
27 #include <linux/binfmts.h>
28 #include <linux/mman.h>
30 #include <linux/cpu.h>
31 #include <linux/security.h>
32 #include <linux/syscalls.h>
33 #include <linux/jiffies.h>
34 #include <linux/futex.h>
35 #include <linux/ptrace.h>
36 #include <linux/mount.h>
37 #include <linux/audit.h>
38 #include <linux/rmap.h>
40 #include <linux/vs_network.h>
41 #include <linux/vs_memory.h>
42 #include <linux/vs_limit.h>
43 #include <linux/vs_base.h>
45 #include <linux/ckrm.h>
46 #include <linux/ckrm_tsk.h>
48 #include <asm/pgtable.h>
49 #include <asm/pgalloc.h>
50 #include <asm/uaccess.h>
51 #include <asm/mmu_context.h>
52 #include <asm/cacheflush.h>
53 #include <asm/tlbflush.h>
55 /* The idle threads do not count..
56 * Protected by write_lock_irq(&tasklist_lock)
61 unsigned long total_forks; /* Handle normal Linux uptimes. */
63 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
65 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
67 EXPORT_SYMBOL(tasklist_lock);
69 int nr_processes(void)
74 for_each_online_cpu(cpu)
75 total += per_cpu(process_counts, cpu);
80 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
81 # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
82 # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
83 static kmem_cache_t *task_struct_cachep;
86 static void free_task(struct task_struct *tsk)
88 free_thread_info(tsk->thread_info);
89 vxdprintk("freeing up task %p\n", tsk);
90 clr_vx_info(&tsk->vx_info);
91 clr_nx_info(&tsk->nx_info);
92 free_task_struct(tsk);
95 void __put_task_struct(struct task_struct *tsk)
97 WARN_ON(!(tsk->state & (TASK_DEAD | TASK_ZOMBIE)));
98 WARN_ON(atomic_read(&tsk->usage));
99 WARN_ON(tsk == current);
101 if (unlikely(tsk->audit_context))
103 security_task_free(tsk);
105 put_group_info(tsk->group_info);
109 void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
113 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
114 spin_lock_irqsave(&q->lock, flags);
115 __add_wait_queue(q, wait);
116 spin_unlock_irqrestore(&q->lock, flags);
119 EXPORT_SYMBOL(add_wait_queue);
121 void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
125 wait->flags |= WQ_FLAG_EXCLUSIVE;
126 spin_lock_irqsave(&q->lock, flags);
127 __add_wait_queue_tail(q, wait);
128 spin_unlock_irqrestore(&q->lock, flags);
131 EXPORT_SYMBOL(add_wait_queue_exclusive);
133 void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
137 spin_lock_irqsave(&q->lock, flags);
138 __remove_wait_queue(q, wait);
139 spin_unlock_irqrestore(&q->lock, flags);
142 EXPORT_SYMBOL(remove_wait_queue);
146 * Note: we use "set_current_state()" _after_ the wait-queue add,
147 * because we need a memory barrier there on SMP, so that any
148 * wake-function that tests for the wait-queue being active
149 * will be guaranteed to see waitqueue addition _or_ subsequent
150 * tests in this thread will see the wakeup having taken place.
152 * The spin_unlock() itself is semi-permeable and only protects
153 * one way (it only protects stuff inside the critical region and
154 * stops them from bleeding out - it would still allow subsequent
155 * loads to move into the the critical region).
157 void fastcall prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
161 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
162 spin_lock_irqsave(&q->lock, flags);
163 if (list_empty(&wait->task_list))
164 __add_wait_queue(q, wait);
165 set_current_state(state);
166 spin_unlock_irqrestore(&q->lock, flags);
169 EXPORT_SYMBOL(prepare_to_wait);
172 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
176 wait->flags |= WQ_FLAG_EXCLUSIVE;
177 spin_lock_irqsave(&q->lock, flags);
178 if (list_empty(&wait->task_list))
179 __add_wait_queue_tail(q, wait);
180 set_current_state(state);
181 spin_unlock_irqrestore(&q->lock, flags);
184 EXPORT_SYMBOL(prepare_to_wait_exclusive);
186 void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
190 __set_current_state(TASK_RUNNING);
192 * We can check for list emptiness outside the lock
194 * - we use the "careful" check that verifies both
195 * the next and prev pointers, so that there cannot
196 * be any half-pending updates in progress on other
197 * CPU's that we haven't seen yet (and that might
198 * still change the stack area.
200 * - all other users take the lock (ie we can only
201 * have _one_ other CPU that looks at or modifies
204 if (!list_empty_careful(&wait->task_list)) {
205 spin_lock_irqsave(&q->lock, flags);
206 list_del_init(&wait->task_list);
207 spin_unlock_irqrestore(&q->lock, flags);
211 EXPORT_SYMBOL(finish_wait);
213 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
215 int ret = default_wake_function(wait, mode, sync, key);
218 list_del_init(&wait->task_list);
222 EXPORT_SYMBOL(autoremove_wake_function);
224 void __init fork_init(unsigned long mempages)
226 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
227 #ifndef ARCH_MIN_TASKALIGN
228 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
230 /* create a slab on which task_structs can be allocated */
232 kmem_cache_create("task_struct", sizeof(struct task_struct),
233 ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
237 * The default maximum number of threads is set to a safe
238 * value: the thread structures can take up at most half
241 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8;
243 * we need to allow at least 20 threads to boot a system
248 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
249 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
252 static struct task_struct *dup_task_struct(struct task_struct *orig)
254 struct task_struct *tsk;
255 struct thread_info *ti;
257 prepare_to_copy(orig);
259 tsk = alloc_task_struct();
263 ti = alloc_thread_info(tsk);
265 free_task_struct(tsk);
269 *ti = *orig->thread_info;
271 tsk->thread_info = ti;
274 ckrm_cb_newtask(tsk);
275 /* One for us, one for whoever does the "release_task()" (usually parent) */
276 atomic_set(&tsk->usage,2);
281 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
283 struct vm_area_struct * mpnt, *tmp, **pprev;
284 struct rb_node **rb_link, *rb_parent;
286 unsigned long charge;
287 struct mempolicy *pol;
289 down_write(&oldmm->mmap_sem);
290 flush_cache_mm(current->mm);
293 mm->mmap_cache = NULL;
294 mm->free_area_cache = TASK_UNMAPPED_BASE;
297 cpus_clear(mm->cpu_vm_mask);
299 rb_link = &mm->mm_rb.rb_node;
304 * Add it to the mmlist after the parent.
305 * Doing it this way means that we can order the list,
306 * and fork() won't mess up the ordering significantly.
307 * Add it first so that swapoff can see any swap entries.
309 spin_lock(&mmlist_lock);
310 list_add(&mm->mmlist, ¤t->mm->mmlist);
312 spin_unlock(&mmlist_lock);
314 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
317 if(mpnt->vm_flags & VM_DONTCOPY)
320 if (mpnt->vm_flags & VM_ACCOUNT) {
321 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
322 if (security_vm_enough_memory(len))
326 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
330 pol = mpol_copy(vma_policy(mpnt));
331 retval = PTR_ERR(pol);
333 goto fail_nomem_policy;
334 vma_set_policy(tmp, pol);
335 tmp->vm_flags &= ~VM_LOCKED;
339 vma_prio_tree_init(tmp);
342 struct inode *inode = file->f_dentry->d_inode;
344 if (tmp->vm_flags & VM_DENYWRITE)
345 atomic_dec(&inode->i_writecount);
347 /* insert tmp into the share list, just after mpnt */
348 spin_lock(&file->f_mapping->i_mmap_lock);
349 flush_dcache_mmap_lock(file->f_mapping);
350 vma_prio_tree_add(tmp, mpnt);
351 flush_dcache_mmap_unlock(file->f_mapping);
352 spin_unlock(&file->f_mapping->i_mmap_lock);
356 * Link in the new vma and copy the page table entries:
357 * link in first so that swapoff can see swap entries,
358 * and try_to_unmap_one's find_vma find the new vma.
360 spin_lock(&mm->page_table_lock);
362 pprev = &tmp->vm_next;
364 __vma_link_rb(mm, tmp, rb_link, rb_parent);
365 rb_link = &tmp->vm_rb.rb_right;
366 rb_parent = &tmp->vm_rb;
369 retval = copy_page_range(mm, current->mm, tmp);
370 spin_unlock(&mm->page_table_lock);
372 if (tmp->vm_ops && tmp->vm_ops->open)
373 tmp->vm_ops->open(tmp);
381 flush_tlb_mm(current->mm);
382 up_write(&oldmm->mmap_sem);
385 kmem_cache_free(vm_area_cachep, tmp);
388 vm_unacct_memory(charge);
392 static inline int mm_alloc_pgd(struct mm_struct * mm)
394 mm->pgd = pgd_alloc(mm);
395 if (unlikely(!mm->pgd))
400 static inline void mm_free_pgd(struct mm_struct * mm)
405 #define dup_mmap(mm, oldmm) (0)
406 #define mm_alloc_pgd(mm) (0)
407 #define mm_free_pgd(mm)
408 #endif /* CONFIG_MMU */
410 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
413 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
414 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
416 #include <linux/init_task.h>
418 static struct mm_struct * mm_init(struct mm_struct * mm)
420 atomic_set(&mm->mm_users, 1);
421 atomic_set(&mm->mm_count, 1);
422 init_rwsem(&mm->mmap_sem);
423 mm->core_waiters = 0;
424 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
425 mm->ioctx_list_lock = RW_LOCK_UNLOCKED;
426 mm->ioctx_list = NULL;
427 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
428 mm->free_area_cache = TASK_UNMAPPED_BASE;
430 if (likely(!mm_alloc_pgd(mm))) {
432 #ifdef __HAVE_ARCH_MMAP_TOP
433 mm->mmap_top = mmap_top();
435 set_vx_info(&mm->mm_vx_info, current->vx_info);
443 * Allocate and initialize an mm_struct.
445 struct mm_struct * mm_alloc(void)
447 struct mm_struct * mm;
451 memset(mm, 0, sizeof(*mm));
458 * Called when the last reference to the mm
459 * is dropped: either by a lazy thread or by
460 * mmput. Free the page directory and the mm.
462 void fastcall __mmdrop(struct mm_struct *mm)
464 BUG_ON(mm == &init_mm);
467 clr_vx_info(&mm->mm_vx_info);
472 * Decrement the use count and release all resources for an mm.
474 void mmput(struct mm_struct *mm)
476 if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)) {
477 list_del(&mm->mmlist);
479 spin_unlock(&mmlist_lock);
487 * Checks if the use count of an mm is non-zero and if so
488 * returns a reference to it after bumping up the use count.
489 * If the use count is zero, it means this mm is going away,
492 struct mm_struct *mmgrab(struct mm_struct *mm)
494 spin_lock(&mmlist_lock);
495 if (!atomic_read(&mm->mm_users))
498 atomic_inc(&mm->mm_users);
499 spin_unlock(&mmlist_lock);
503 /* Please note the differences between mmput and mm_release.
504 * mmput is called whenever we stop holding onto a mm_struct,
505 * error success whatever.
507 * mm_release is called after a mm_struct has been removed
508 * from the current process.
510 * This difference is important for error handling, when we
511 * only half set up a mm_struct for a new process and need to restore
512 * the old one. Because we mmput the new mm_struct before
513 * restoring the old one. . .
514 * Eric Biederman 10 January 1998
516 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
518 struct completion *vfork_done = tsk->vfork_done;
520 /* Get rid of any cached register state */
521 deactivate_mm(tsk, mm);
523 /* notify parent sleeping on vfork() */
525 tsk->vfork_done = NULL;
526 complete(vfork_done);
528 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
529 u32 __user * tidptr = tsk->clear_child_tid;
530 tsk->clear_child_tid = NULL;
533 * We don't check the error code - if userspace has
534 * not set up a proper pointer then tough luck.
537 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
541 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
543 struct mm_struct * mm, *oldmm;
546 tsk->min_flt = tsk->maj_flt = 0;
547 tsk->cmin_flt = tsk->cmaj_flt = 0;
548 tsk->nvcsw = tsk->nivcsw = tsk->cnvcsw = tsk->cnivcsw = 0;
551 tsk->active_mm = NULL;
554 * Are we cloning a kernel thread?
556 * We need to steal a active VM for that..
562 if (clone_flags & CLONE_VM) {
563 atomic_inc(&oldmm->mm_users);
566 * There are cases where the PTL is held to ensure no
567 * new threads start up in user mode using an mm, which
568 * allows optimizing out ipis; the tlb_gather_mmu code
571 spin_unlock_wait(&oldmm->page_table_lock);
580 /* Copy the current MM stuff.. */
581 memcpy(mm, oldmm, sizeof(*mm));
582 mm->mm_vx_info = NULL;
586 if (init_new_context(tsk,mm))
589 retval = dup_mmap(mm, oldmm);
605 * If init_new_context() failed, we cannot use mmput() to free the mm
606 * because it calls destroy_context()
613 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
615 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
616 /* We don't need to lock fs - think why ;-) */
618 atomic_set(&fs->count, 1);
619 fs->lock = RW_LOCK_UNLOCKED;
620 fs->umask = old->umask;
621 read_lock(&old->lock);
622 fs->rootmnt = mntget(old->rootmnt);
623 fs->root = dget(old->root);
624 fs->pwdmnt = mntget(old->pwdmnt);
625 fs->pwd = dget(old->pwd);
627 fs->altrootmnt = mntget(old->altrootmnt);
628 fs->altroot = dget(old->altroot);
630 fs->altrootmnt = NULL;
633 read_unlock(&old->lock);
638 struct fs_struct *copy_fs_struct(struct fs_struct *old)
640 return __copy_fs_struct(old);
643 EXPORT_SYMBOL_GPL(copy_fs_struct);
645 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
647 if (clone_flags & CLONE_FS) {
648 atomic_inc(¤t->fs->count);
651 tsk->fs = __copy_fs_struct(current->fs);
657 static int count_open_files(struct files_struct *files, int size)
661 /* Find the last open fd */
662 for (i = size/(8*sizeof(long)); i > 0; ) {
663 if (files->open_fds->fds_bits[--i])
666 i = (i+1) * 8 * sizeof(long);
670 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
672 struct files_struct *oldf, *newf;
673 struct file **old_fds, **new_fds;
674 int open_files, nfds, size, i, error = 0;
677 * A background process may not have any files ...
679 oldf = current->files;
683 if (clone_flags & CLONE_FILES) {
684 atomic_inc(&oldf->count);
689 * Note: we may be using current for both targets (See exec.c)
690 * This works because we cache current->files (old) as oldf. Don't
695 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
699 atomic_set(&newf->count, 1);
701 newf->file_lock = SPIN_LOCK_UNLOCKED;
703 newf->max_fds = NR_OPEN_DEFAULT;
704 newf->max_fdset = __FD_SETSIZE;
705 newf->close_on_exec = &newf->close_on_exec_init;
706 newf->open_fds = &newf->open_fds_init;
707 newf->fd = &newf->fd_array[0];
709 /* We don't yet have the oldf readlock, but even if the old
710 fdset gets grown now, we'll only copy up to "size" fds */
711 size = oldf->max_fdset;
712 if (size > __FD_SETSIZE) {
714 spin_lock(&newf->file_lock);
715 error = expand_fdset(newf, size-1);
716 spin_unlock(&newf->file_lock);
720 spin_lock(&oldf->file_lock);
722 open_files = count_open_files(oldf, size);
725 * Check whether we need to allocate a larger fd array.
726 * Note: we're not a clone task, so the open count won't
729 nfds = NR_OPEN_DEFAULT;
730 if (open_files > nfds) {
731 spin_unlock(&oldf->file_lock);
733 spin_lock(&newf->file_lock);
734 error = expand_fd_array(newf, open_files-1);
735 spin_unlock(&newf->file_lock);
738 nfds = newf->max_fds;
739 spin_lock(&oldf->file_lock);
745 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
746 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
748 for (i = open_files; i != 0; i--) {
749 struct file *f = *old_fds++;
754 spin_unlock(&oldf->file_lock);
756 /* compute the remainder to be cleared */
757 size = (newf->max_fds - open_files) * sizeof(struct file *);
759 /* This is long word aligned thus could use a optimized version */
760 memset(new_fds, 0, size);
762 if (newf->max_fdset > open_files) {
763 int left = (newf->max_fdset-open_files)/8;
764 int start = open_files / (8 * sizeof(unsigned long));
766 memset(&newf->open_fds->fds_bits[start], 0, left);
767 memset(&newf->close_on_exec->fds_bits[start], 0, left);
776 free_fdset (newf->close_on_exec, newf->max_fdset);
777 free_fdset (newf->open_fds, newf->max_fdset);
778 kmem_cache_free(files_cachep, newf);
783 * Helper to unshare the files of the current task.
784 * We don't want to expose copy_files internals to
785 * the exec layer of the kernel.
788 int unshare_files(void)
790 struct files_struct *files = current->files;
796 /* This can race but the race causes us to copy when we don't
797 need to and drop the copy */
798 if(atomic_read(&files->count) == 1)
800 atomic_inc(&files->count);
803 rc = copy_files(0, current);
805 current->files = files;
809 EXPORT_SYMBOL(unshare_files);
811 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
813 struct sighand_struct *sig;
815 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
816 atomic_inc(¤t->sighand->count);
819 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
823 spin_lock_init(&sig->siglock);
824 atomic_set(&sig->count, 1);
825 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
829 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
831 struct signal_struct *sig;
833 if (clone_flags & CLONE_THREAD) {
834 atomic_inc(¤t->signal->count);
837 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
841 atomic_set(&sig->count, 1);
843 sig->group_exit_code = 0;
844 sig->group_exit_task = NULL;
845 sig->group_stop_count = 0;
846 sig->curr_target = NULL;
847 init_sigpending(&sig->shared_pending);
848 INIT_LIST_HEAD(&sig->posix_timers);
850 sig->tty = current->signal->tty;
851 sig->pgrp = process_group(current);
852 sig->session = current->signal->session;
853 sig->leader = 0; /* session leadership doesn't inherit */
854 sig->tty_old_pgrp = 0;
859 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
861 unsigned long new_flags = p->flags;
863 new_flags &= ~PF_SUPERPRIV;
864 new_flags |= PF_FORKNOEXEC;
865 if (!(clone_flags & CLONE_PTRACE))
867 p->flags = new_flags;
870 asmlinkage long sys_set_tid_address(int __user *tidptr)
872 current->clear_child_tid = tidptr;
878 * This creates a new process as a copy of the old one,
879 * but does not actually start it yet.
881 * It copies the registers, and all the appropriate
882 * parts of the process environment (as per the clone
883 * flags). The actual kick-off is left to the caller.
885 struct task_struct *copy_process(unsigned long clone_flags,
886 unsigned long stack_start,
887 struct pt_regs *regs,
888 unsigned long stack_size,
889 int __user *parent_tidptr,
890 int __user *child_tidptr)
893 struct task_struct *p = NULL;
896 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
897 return ERR_PTR(-EINVAL);
900 * Thread groups must share signals as well, and detached threads
901 * can only be started up within the thread group.
903 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
904 return ERR_PTR(-EINVAL);
907 * Shared signal handlers imply shared VM. By way of the above,
908 * thread groups also imply shared VM. Blocking this case allows
909 * for various simplifications in other code.
911 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
912 return ERR_PTR(-EINVAL);
914 retval = security_task_create(clone_flags);
920 p = dup_task_struct(current);
926 set_vx_info(&p->vx_info, current->vx_info);
928 set_nx_info(&p->nx_info, current->nx_info);
930 /* check vserver memory */
931 if (p->mm && !(clone_flags & CLONE_VM)) {
932 if (vx_vmpages_avail(p->mm, p->mm->total_vm))
933 vx_pages_add(p->mm->mm_vx_info, RLIMIT_AS, p->mm->total_vm);
937 if (p->mm && vx_flags(VXF_FORK_RSS, 0)) {
938 if (!vx_rsspages_avail(p->mm, p->mm->rss))
943 if (!vx_nproc_avail(1))
946 if (atomic_read(&p->user->processes) >=
947 p->rlim[RLIMIT_NPROC].rlim_cur) {
948 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
949 p->user != &root_user)
953 atomic_inc(&p->user->__count);
954 atomic_inc(&p->user->processes);
955 get_group_info(p->group_info);
958 * If multiple threads are within copy_process(), then this check
959 * triggers too late. This doesn't hurt, the check is only there
960 * to stop root fork bombs.
962 if (nr_threads >= max_threads)
963 goto bad_fork_cleanup_count;
965 if (!try_module_get(p->thread_info->exec_domain->module))
966 goto bad_fork_cleanup_count;
968 if (p->binfmt && !try_module_get(p->binfmt->module))
969 goto bad_fork_cleanup_put_domain;
972 copy_flags(clone_flags, p);
973 if (clone_flags & CLONE_IDLETASK)
976 p->pid = alloc_pidmap();
978 goto bad_fork_cleanup;
981 if (clone_flags & CLONE_PARENT_SETTID)
982 if (put_user(p->pid, parent_tidptr))
983 goto bad_fork_cleanup;
985 p->proc_dentry = NULL;
987 INIT_LIST_HEAD(&p->children);
988 INIT_LIST_HEAD(&p->sibling);
989 init_waitqueue_head(&p->wait_chldexit);
990 p->vfork_done = NULL;
991 spin_lock_init(&p->alloc_lock);
992 spin_lock_init(&p->proc_lock);
994 clear_tsk_thread_flag(p, TIF_SIGPENDING);
995 init_sigpending(&p->pending);
997 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
998 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
999 init_timer(&p->real_timer);
1000 p->real_timer.data = (unsigned long) p;
1002 p->utime = p->stime = 0;
1003 p->cutime = p->cstime = 0;
1004 p->lock_depth = -1; /* -1 = no lock */
1005 p->start_time = get_jiffies_64();
1007 p->io_context = NULL;
1008 p->audit_context = NULL;
1010 p->mempolicy = mpol_copy(p->mempolicy);
1011 if (IS_ERR(p->mempolicy)) {
1012 retval = PTR_ERR(p->mempolicy);
1013 p->mempolicy = NULL;
1014 goto bad_fork_cleanup;
1019 if ((retval = security_task_alloc(p)))
1020 goto bad_fork_cleanup_policy;
1021 if ((retval = audit_alloc(p)))
1022 goto bad_fork_cleanup_security;
1023 /* copy all the process information */
1024 if ((retval = copy_semundo(clone_flags, p)))
1025 goto bad_fork_cleanup_audit;
1026 if ((retval = copy_files(clone_flags, p)))
1027 goto bad_fork_cleanup_semundo;
1028 if ((retval = copy_fs(clone_flags, p)))
1029 goto bad_fork_cleanup_files;
1030 if ((retval = copy_sighand(clone_flags, p)))
1031 goto bad_fork_cleanup_fs;
1032 if ((retval = copy_signal(clone_flags, p)))
1033 goto bad_fork_cleanup_sighand;
1034 if ((retval = copy_mm(clone_flags, p)))
1035 goto bad_fork_cleanup_signal;
1036 if ((retval = copy_namespace(clone_flags, p)))
1037 goto bad_fork_cleanup_mm;
1038 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
1040 goto bad_fork_cleanup_namespace;
1042 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
1044 * Clear TID on mm_release()?
1046 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
1049 * Syscall tracing should be turned off in the child regardless
1052 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1054 /* Our parent execution domain becomes current domain
1055 These must match for thread signalling to apply */
1057 p->parent_exec_id = p->self_exec_id;
1059 /* ok, now we should be set up.. */
1060 p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
1061 p->pdeath_signal = 0;
1063 /* Perform scheduler related setup */
1067 * Ok, make it visible to the rest of the system.
1068 * We dont wake it up yet.
1071 p->group_leader = p;
1072 INIT_LIST_HEAD(&p->ptrace_children);
1073 INIT_LIST_HEAD(&p->ptrace_list);
1075 /* Need tasklist lock for parent etc handling! */
1076 write_lock_irq(&tasklist_lock);
1078 * Check for pending SIGKILL! The new thread should not be allowed
1079 * to slip out of an OOM kill. (or normal SIGKILL.)
1081 if (sigismember(¤t->pending.signal, SIGKILL)) {
1082 write_unlock_irq(&tasklist_lock);
1084 goto bad_fork_cleanup_namespace;
1087 /* CLONE_PARENT re-uses the old parent */
1088 if (clone_flags & CLONE_PARENT)
1089 p->real_parent = current->real_parent;
1091 p->real_parent = current;
1092 p->parent = p->real_parent;
1094 if (clone_flags & CLONE_THREAD) {
1095 spin_lock(¤t->sighand->siglock);
1097 * Important: if an exit-all has been started then
1098 * do not create this new thread - the whole thread
1099 * group is supposed to exit anyway.
1101 if (current->signal->group_exit) {
1102 spin_unlock(¤t->sighand->siglock);
1103 write_unlock_irq(&tasklist_lock);
1105 goto bad_fork_cleanup_namespace;
1107 p->tgid = current->tgid;
1108 p->group_leader = current->group_leader;
1110 if (current->signal->group_stop_count > 0) {
1112 * There is an all-stop in progress for the group.
1113 * We ourselves will stop as soon as we check signals.
1114 * Make the new thread part of that group stop too.
1116 current->signal->group_stop_count++;
1117 set_tsk_thread_flag(p, TIF_SIGPENDING);
1120 spin_unlock(¤t->sighand->siglock);
1124 if (p->ptrace & PT_PTRACED)
1125 __ptrace_link(p, current->parent);
1127 attach_pid(p, PIDTYPE_PID, p->pid);
1128 if (thread_group_leader(p)) {
1129 attach_pid(p, PIDTYPE_TGID, p->tgid);
1130 attach_pid(p, PIDTYPE_PGID, process_group(p));
1131 attach_pid(p, PIDTYPE_SID, p->signal->session);
1133 __get_cpu_var(process_counts)++;
1135 link_pid(p, p->pids + PIDTYPE_TGID, &p->group_leader->pids[PIDTYPE_TGID].pid);
1138 vxi = current->vx_info;
1140 atomic_inc(&vxi->cacct.nr_threads);
1141 // atomic_inc(&vxi->limit.res[RLIMIT_NPROC]);
1144 write_unlock_irq(&tasklist_lock);
1149 return ERR_PTR(retval);
1152 bad_fork_cleanup_namespace:
1154 bad_fork_cleanup_mm:
1157 mmdrop(p->active_mm);
1158 bad_fork_cleanup_signal:
1160 bad_fork_cleanup_sighand:
1162 bad_fork_cleanup_fs:
1163 exit_fs(p); /* blocking */
1164 bad_fork_cleanup_files:
1165 exit_files(p); /* blocking */
1166 bad_fork_cleanup_semundo:
1168 bad_fork_cleanup_audit:
1170 bad_fork_cleanup_security:
1171 security_task_free(p);
1172 bad_fork_cleanup_policy:
1174 mpol_free(p->mempolicy);
1178 free_pidmap(p->pid);
1180 module_put(p->binfmt->module);
1181 bad_fork_cleanup_put_domain:
1182 module_put(p->thread_info->exec_domain->module);
1183 bad_fork_cleanup_count:
1184 put_group_info(p->group_info);
1185 atomic_dec(&p->user->processes);
1192 static inline int fork_traceflag (unsigned clone_flags)
1194 if (clone_flags & (CLONE_UNTRACED | CLONE_IDLETASK))
1196 else if (clone_flags & CLONE_VFORK) {
1197 if (current->ptrace & PT_TRACE_VFORK)
1198 return PTRACE_EVENT_VFORK;
1199 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1200 if (current->ptrace & PT_TRACE_CLONE)
1201 return PTRACE_EVENT_CLONE;
1202 } else if (current->ptrace & PT_TRACE_FORK)
1203 return PTRACE_EVENT_FORK;
1209 * Ok, this is the main fork-routine.
1211 * It copies the process, and if successful kick-starts
1212 * it and waits for it to finish using the VM if required.
1214 long do_fork(unsigned long clone_flags,
1215 unsigned long stack_start,
1216 struct pt_regs *regs,
1217 unsigned long stack_size,
1218 int __user *parent_tidptr,
1219 int __user *child_tidptr)
1221 struct task_struct *p;
1225 if (unlikely(current->ptrace)) {
1226 trace = fork_traceflag (clone_flags);
1228 clone_flags |= CLONE_PTRACE;
1231 if (numtasks_get_ref(current->taskclass, 0) == 0) {
1235 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr);
1237 * Do this prior waking up the new thread - the thread pointer
1238 * might get invalid after that point, if the thread exits quickly.
1240 pid = IS_ERR(p) ? PTR_ERR(p) : p->pid;
1243 struct completion vfork;
1247 if (clone_flags & CLONE_VFORK) {
1248 p->vfork_done = &vfork;
1249 init_completion(&vfork);
1252 if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
1254 * We'll start up with an immediate SIGSTOP.
1256 sigaddset(&p->pending.signal, SIGSTOP);
1257 set_tsk_thread_flag(p, TIF_SIGPENDING);
1260 if (!(clone_flags & CLONE_STOPPED)) {
1262 * Do the wakeup last. On SMP we treat fork() and
1263 * CLONE_VM separately, because fork() has already
1264 * created cache footprint on this CPU (due to
1265 * copying the pagetables), hence migration would
1266 * probably be costy. Threads on the other hand
1267 * have less traction to the current CPU, and if
1268 * there's an imbalance then the scheduler can
1269 * migrate this fresh thread now, before it
1270 * accumulates a larger cache footprint:
1272 if (clone_flags & CLONE_VM)
1273 wake_up_forked_thread(p);
1275 wake_up_forked_process(p);
1277 int cpu = get_cpu();
1279 p->state = TASK_STOPPED;
1280 if (cpu_is_offline(task_cpu(p)))
1281 set_task_cpu(p, cpu);
1287 if (unlikely (trace)) {
1288 current->ptrace_message = pid;
1289 ptrace_notify ((trace << 8) | SIGTRAP);
1292 if (clone_flags & CLONE_VFORK) {
1293 wait_for_completion(&vfork);
1294 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1295 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1298 * Let the child process run first, to avoid most of the
1299 * COW overhead when the child exec()s afterwards.
1303 numtasks_put_ref(current->taskclass);
1308 /* SLAB cache for signal_struct structures (tsk->signal) */
1309 kmem_cache_t *signal_cachep;
1311 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1312 kmem_cache_t *sighand_cachep;
1314 /* SLAB cache for files_struct structures (tsk->files) */
1315 kmem_cache_t *files_cachep;
1317 /* SLAB cache for fs_struct structures (tsk->fs) */
1318 kmem_cache_t *fs_cachep;
1320 /* SLAB cache for vm_area_struct structures */
1321 kmem_cache_t *vm_area_cachep;
1323 /* SLAB cache for mm_struct structures (tsk->mm) */
1324 kmem_cache_t *mm_cachep;
1326 void __init proc_caches_init(void)
1328 sighand_cachep = kmem_cache_create("sighand_cache",
1329 sizeof(struct sighand_struct), 0,
1330 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1331 signal_cachep = kmem_cache_create("signal_cache",
1332 sizeof(struct signal_struct), 0,
1333 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1334 files_cachep = kmem_cache_create("files_cache",
1335 sizeof(struct files_struct), 0,
1336 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1337 fs_cachep = kmem_cache_create("fs_cache",
1338 sizeof(struct fs_struct), 0,
1339 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1340 vm_area_cachep = kmem_cache_create("vm_area_struct",
1341 sizeof(struct vm_area_struct), 0,
1342 SLAB_PANIC, NULL, NULL);
1343 mm_cachep = kmem_cache_create("mm_struct",
1344 sizeof(struct mm_struct), 0,
1345 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);