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>
39 #include <linux/vs_network.h>
40 #include <linux/vs_limit.h>
41 #include <linux/ckrm.h>
42 #include <linux/ckrm_tsk.h>
44 #include <asm/pgtable.h>
45 #include <asm/pgalloc.h>
46 #include <asm/uaccess.h>
47 #include <asm/mmu_context.h>
48 #include <asm/cacheflush.h>
49 #include <asm/tlbflush.h>
51 /* The idle threads do not count..
52 * Protected by write_lock_irq(&tasklist_lock)
57 unsigned long total_forks; /* Handle normal Linux uptimes. */
59 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
61 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
63 EXPORT_SYMBOL(tasklist_lock);
65 int nr_processes(void)
70 for_each_online_cpu(cpu)
71 total += per_cpu(process_counts, cpu);
76 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
77 # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
78 # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
79 static kmem_cache_t *task_struct_cachep;
82 static void free_task(struct task_struct *tsk)
84 free_thread_info(tsk->thread_info);
85 vxdprintk("freeing up task %p\n", tsk);
86 clr_vx_info(&tsk->vx_info);
87 clr_nx_info(&tsk->nx_info);
88 free_task_struct(tsk);
91 void __put_task_struct(struct task_struct *tsk)
93 WARN_ON(!(tsk->state & (TASK_DEAD | TASK_ZOMBIE)));
94 WARN_ON(atomic_read(&tsk->usage));
95 WARN_ON(tsk == current);
97 if (unlikely(tsk->audit_context))
99 security_task_free(tsk);
101 put_group_info(tsk->group_info);
105 void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
109 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
110 spin_lock_irqsave(&q->lock, flags);
111 __add_wait_queue(q, wait);
112 spin_unlock_irqrestore(&q->lock, flags);
115 EXPORT_SYMBOL(add_wait_queue);
117 void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
121 wait->flags |= WQ_FLAG_EXCLUSIVE;
122 spin_lock_irqsave(&q->lock, flags);
123 __add_wait_queue_tail(q, wait);
124 spin_unlock_irqrestore(&q->lock, flags);
127 EXPORT_SYMBOL(add_wait_queue_exclusive);
129 void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
133 spin_lock_irqsave(&q->lock, flags);
134 __remove_wait_queue(q, wait);
135 spin_unlock_irqrestore(&q->lock, flags);
138 EXPORT_SYMBOL(remove_wait_queue);
142 * Note: we use "set_current_state()" _after_ the wait-queue add,
143 * because we need a memory barrier there on SMP, so that any
144 * wake-function that tests for the wait-queue being active
145 * will be guaranteed to see waitqueue addition _or_ subsequent
146 * tests in this thread will see the wakeup having taken place.
148 * The spin_unlock() itself is semi-permeable and only protects
149 * one way (it only protects stuff inside the critical region and
150 * stops them from bleeding out - it would still allow subsequent
151 * loads to move into the the critical region).
153 void fastcall prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
157 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
158 spin_lock_irqsave(&q->lock, flags);
159 if (list_empty(&wait->task_list))
160 __add_wait_queue(q, wait);
161 set_current_state(state);
162 spin_unlock_irqrestore(&q->lock, flags);
165 EXPORT_SYMBOL(prepare_to_wait);
168 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
172 wait->flags |= WQ_FLAG_EXCLUSIVE;
173 spin_lock_irqsave(&q->lock, flags);
174 if (list_empty(&wait->task_list))
175 __add_wait_queue_tail(q, wait);
176 set_current_state(state);
177 spin_unlock_irqrestore(&q->lock, flags);
180 EXPORT_SYMBOL(prepare_to_wait_exclusive);
182 void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
186 __set_current_state(TASK_RUNNING);
188 * We can check for list emptiness outside the lock
190 * - we use the "careful" check that verifies both
191 * the next and prev pointers, so that there cannot
192 * be any half-pending updates in progress on other
193 * CPU's that we haven't seen yet (and that might
194 * still change the stack area.
196 * - all other users take the lock (ie we can only
197 * have _one_ other CPU that looks at or modifies
200 if (!list_empty_careful(&wait->task_list)) {
201 spin_lock_irqsave(&q->lock, flags);
202 list_del_init(&wait->task_list);
203 spin_unlock_irqrestore(&q->lock, flags);
207 EXPORT_SYMBOL(finish_wait);
209 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
211 int ret = default_wake_function(wait, mode, sync, key);
214 list_del_init(&wait->task_list);
218 EXPORT_SYMBOL(autoremove_wake_function);
220 void __init fork_init(unsigned long mempages)
222 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
223 #ifndef ARCH_MIN_TASKALIGN
224 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
226 /* create a slab on which task_structs can be allocated */
228 kmem_cache_create("task_struct", sizeof(struct task_struct),
229 ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
233 * The default maximum number of threads is set to a safe
234 * value: the thread structures can take up at most half
237 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8;
239 * we need to allow at least 20 threads to boot a system
244 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
245 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
248 static struct task_struct *dup_task_struct(struct task_struct *orig)
250 struct task_struct *tsk;
251 struct thread_info *ti;
253 prepare_to_copy(orig);
255 tsk = alloc_task_struct();
259 ti = alloc_thread_info(tsk);
261 free_task_struct(tsk);
265 *ti = *orig->thread_info;
267 tsk->thread_info = ti;
270 ckrm_cb_newtask(tsk);
271 /* One for us, one for whoever does the "release_task()" (usually parent) */
272 atomic_set(&tsk->usage,2);
277 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
279 struct vm_area_struct * mpnt, *tmp, **pprev;
280 struct rb_node **rb_link, *rb_parent;
282 unsigned long charge;
283 struct mempolicy *pol;
285 down_write(&oldmm->mmap_sem);
286 flush_cache_mm(current->mm);
289 mm->mmap_cache = NULL;
290 mm->free_area_cache = TASK_UNMAPPED_BASE;
293 cpus_clear(mm->cpu_vm_mask);
295 rb_link = &mm->mm_rb.rb_node;
300 * Add it to the mmlist after the parent.
301 * Doing it this way means that we can order the list,
302 * and fork() won't mess up the ordering significantly.
303 * Add it first so that swapoff can see any swap entries.
305 spin_lock(&mmlist_lock);
306 list_add(&mm->mmlist, ¤t->mm->mmlist);
308 spin_unlock(&mmlist_lock);
310 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
313 if(mpnt->vm_flags & VM_DONTCOPY)
316 if (mpnt->vm_flags & VM_ACCOUNT) {
317 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
318 if (security_vm_enough_memory(len))
322 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
326 pol = mpol_copy(vma_policy(mpnt));
327 retval = PTR_ERR(pol);
329 goto fail_nomem_policy;
330 vma_set_policy(tmp, pol);
331 tmp->vm_flags &= ~VM_LOCKED;
335 vma_prio_tree_init(tmp);
338 struct inode *inode = file->f_dentry->d_inode;
340 if (tmp->vm_flags & VM_DENYWRITE)
341 atomic_dec(&inode->i_writecount);
343 /* insert tmp into the share list, just after mpnt */
344 spin_lock(&file->f_mapping->i_mmap_lock);
345 flush_dcache_mmap_lock(file->f_mapping);
346 vma_prio_tree_add(tmp, mpnt);
347 flush_dcache_mmap_unlock(file->f_mapping);
348 spin_unlock(&file->f_mapping->i_mmap_lock);
352 * Link in the new vma and copy the page table entries:
353 * link in first so that swapoff can see swap entries,
354 * and try_to_unmap_one's find_vma find the new vma.
356 spin_lock(&mm->page_table_lock);
358 pprev = &tmp->vm_next;
360 __vma_link_rb(mm, tmp, rb_link, rb_parent);
361 rb_link = &tmp->vm_rb.rb_right;
362 rb_parent = &tmp->vm_rb;
365 retval = copy_page_range(mm, current->mm, tmp);
366 spin_unlock(&mm->page_table_lock);
368 if (tmp->vm_ops && tmp->vm_ops->open)
369 tmp->vm_ops->open(tmp);
377 flush_tlb_mm(current->mm);
378 up_write(&oldmm->mmap_sem);
381 kmem_cache_free(vm_area_cachep, tmp);
384 vm_unacct_memory(charge);
388 static inline int mm_alloc_pgd(struct mm_struct * mm)
390 mm->pgd = pgd_alloc(mm);
391 if (unlikely(!mm->pgd))
396 static inline void mm_free_pgd(struct mm_struct * mm)
401 #define dup_mmap(mm, oldmm) (0)
402 #define mm_alloc_pgd(mm) (0)
403 #define mm_free_pgd(mm)
404 #endif /* CONFIG_MMU */
406 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
409 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
410 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
412 #include <linux/init_task.h>
414 static struct mm_struct * mm_init(struct mm_struct * mm)
416 atomic_set(&mm->mm_users, 1);
417 atomic_set(&mm->mm_count, 1);
418 init_rwsem(&mm->mmap_sem);
419 mm->core_waiters = 0;
420 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
421 mm->ioctx_list_lock = RW_LOCK_UNLOCKED;
422 mm->ioctx_list = NULL;
423 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
424 mm->free_area_cache = TASK_UNMAPPED_BASE;
426 if (likely(!mm_alloc_pgd(mm))) {
428 #ifdef __HAVE_ARCH_MMAP_TOP
429 mm->mmap_top = mmap_top();
431 set_vx_info(&mm->mm_vx_info, current->vx_info);
439 * Allocate and initialize an mm_struct.
441 struct mm_struct * mm_alloc(void)
443 struct mm_struct * mm;
447 memset(mm, 0, sizeof(*mm));
454 * Called when the last reference to the mm
455 * is dropped: either by a lazy thread or by
456 * mmput. Free the page directory and the mm.
458 void fastcall __mmdrop(struct mm_struct *mm)
460 BUG_ON(mm == &init_mm);
463 clr_vx_info(&mm->mm_vx_info);
468 * Decrement the use count and release all resources for an mm.
470 void mmput(struct mm_struct *mm)
472 if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)) {
473 list_del(&mm->mmlist);
475 spin_unlock(&mmlist_lock);
483 * Checks if the use count of an mm is non-zero and if so
484 * returns a reference to it after bumping up the use count.
485 * If the use count is zero, it means this mm is going away,
488 struct mm_struct *mmgrab(struct mm_struct *mm)
490 spin_lock(&mmlist_lock);
491 if (!atomic_read(&mm->mm_users))
494 atomic_inc(&mm->mm_users);
495 spin_unlock(&mmlist_lock);
499 /* Please note the differences between mmput and mm_release.
500 * mmput is called whenever we stop holding onto a mm_struct,
501 * error success whatever.
503 * mm_release is called after a mm_struct has been removed
504 * from the current process.
506 * This difference is important for error handling, when we
507 * only half set up a mm_struct for a new process and need to restore
508 * the old one. Because we mmput the new mm_struct before
509 * restoring the old one. . .
510 * Eric Biederman 10 January 1998
512 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
514 struct completion *vfork_done = tsk->vfork_done;
516 /* Get rid of any cached register state */
517 deactivate_mm(tsk, mm);
519 /* notify parent sleeping on vfork() */
521 tsk->vfork_done = NULL;
522 complete(vfork_done);
524 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
525 u32 __user * tidptr = tsk->clear_child_tid;
526 tsk->clear_child_tid = NULL;
529 * We don't check the error code - if userspace has
530 * not set up a proper pointer then tough luck.
533 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
537 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
539 struct mm_struct * mm, *oldmm;
542 tsk->min_flt = tsk->maj_flt = 0;
543 tsk->cmin_flt = tsk->cmaj_flt = 0;
544 tsk->nvcsw = tsk->nivcsw = tsk->cnvcsw = tsk->cnivcsw = 0;
547 tsk->active_mm = NULL;
550 * Are we cloning a kernel thread?
552 * We need to steal a active VM for that..
558 if (clone_flags & CLONE_VM) {
559 atomic_inc(&oldmm->mm_users);
562 * There are cases where the PTL is held to ensure no
563 * new threads start up in user mode using an mm, which
564 * allows optimizing out ipis; the tlb_gather_mmu code
567 spin_unlock_wait(&oldmm->page_table_lock);
576 /* Copy the current MM stuff.. */
577 memcpy(mm, oldmm, sizeof(*mm));
578 mm->mm_vx_info = NULL;
582 if (init_new_context(tsk,mm))
585 retval = dup_mmap(mm, oldmm);
601 * If init_new_context() failed, we cannot use mmput() to free the mm
602 * because it calls destroy_context()
609 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
611 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
612 /* We don't need to lock fs - think why ;-) */
614 atomic_set(&fs->count, 1);
615 fs->lock = RW_LOCK_UNLOCKED;
616 fs->umask = old->umask;
617 read_lock(&old->lock);
618 fs->rootmnt = mntget(old->rootmnt);
619 fs->root = dget(old->root);
620 fs->pwdmnt = mntget(old->pwdmnt);
621 fs->pwd = dget(old->pwd);
623 fs->altrootmnt = mntget(old->altrootmnt);
624 fs->altroot = dget(old->altroot);
626 fs->altrootmnt = NULL;
629 read_unlock(&old->lock);
634 struct fs_struct *copy_fs_struct(struct fs_struct *old)
636 return __copy_fs_struct(old);
639 EXPORT_SYMBOL_GPL(copy_fs_struct);
641 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
643 if (clone_flags & CLONE_FS) {
644 atomic_inc(¤t->fs->count);
647 tsk->fs = __copy_fs_struct(current->fs);
653 static int count_open_files(struct files_struct *files, int size)
657 /* Find the last open fd */
658 for (i = size/(8*sizeof(long)); i > 0; ) {
659 if (files->open_fds->fds_bits[--i])
662 i = (i+1) * 8 * sizeof(long);
666 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
668 struct files_struct *oldf, *newf;
669 struct file **old_fds, **new_fds;
670 int open_files, nfds, size, i, error = 0;
673 * A background process may not have any files ...
675 oldf = current->files;
679 if (clone_flags & CLONE_FILES) {
680 atomic_inc(&oldf->count);
685 * Note: we may be using current for both targets (See exec.c)
686 * This works because we cache current->files (old) as oldf. Don't
691 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
695 atomic_set(&newf->count, 1);
697 newf->file_lock = SPIN_LOCK_UNLOCKED;
699 newf->max_fds = NR_OPEN_DEFAULT;
700 newf->max_fdset = __FD_SETSIZE;
701 newf->close_on_exec = &newf->close_on_exec_init;
702 newf->open_fds = &newf->open_fds_init;
703 newf->fd = &newf->fd_array[0];
705 /* We don't yet have the oldf readlock, but even if the old
706 fdset gets grown now, we'll only copy up to "size" fds */
707 size = oldf->max_fdset;
708 if (size > __FD_SETSIZE) {
710 spin_lock(&newf->file_lock);
711 error = expand_fdset(newf, size-1);
712 spin_unlock(&newf->file_lock);
716 spin_lock(&oldf->file_lock);
718 open_files = count_open_files(oldf, size);
721 * Check whether we need to allocate a larger fd array.
722 * Note: we're not a clone task, so the open count won't
725 nfds = NR_OPEN_DEFAULT;
726 if (open_files > nfds) {
727 spin_unlock(&oldf->file_lock);
729 spin_lock(&newf->file_lock);
730 error = expand_fd_array(newf, open_files-1);
731 spin_unlock(&newf->file_lock);
734 nfds = newf->max_fds;
735 spin_lock(&oldf->file_lock);
741 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
742 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
744 for (i = open_files; i != 0; i--) {
745 struct file *f = *old_fds++;
750 spin_unlock(&oldf->file_lock);
752 /* compute the remainder to be cleared */
753 size = (newf->max_fds - open_files) * sizeof(struct file *);
755 /* This is long word aligned thus could use a optimized version */
756 memset(new_fds, 0, size);
758 if (newf->max_fdset > open_files) {
759 int left = (newf->max_fdset-open_files)/8;
760 int start = open_files / (8 * sizeof(unsigned long));
762 memset(&newf->open_fds->fds_bits[start], 0, left);
763 memset(&newf->close_on_exec->fds_bits[start], 0, left);
772 free_fdset (newf->close_on_exec, newf->max_fdset);
773 free_fdset (newf->open_fds, newf->max_fdset);
774 kmem_cache_free(files_cachep, newf);
779 * Helper to unshare the files of the current task.
780 * We don't want to expose copy_files internals to
781 * the exec layer of the kernel.
784 int unshare_files(void)
786 struct files_struct *files = current->files;
792 /* This can race but the race causes us to copy when we don't
793 need to and drop the copy */
794 if(atomic_read(&files->count) == 1)
796 atomic_inc(&files->count);
799 rc = copy_files(0, current);
801 current->files = files;
805 EXPORT_SYMBOL(unshare_files);
807 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
809 struct sighand_struct *sig;
811 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
812 atomic_inc(¤t->sighand->count);
815 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
819 spin_lock_init(&sig->siglock);
820 atomic_set(&sig->count, 1);
821 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
825 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
827 struct signal_struct *sig;
829 if (clone_flags & CLONE_THREAD) {
830 atomic_inc(¤t->signal->count);
833 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
837 atomic_set(&sig->count, 1);
839 sig->group_exit_code = 0;
840 sig->group_exit_task = NULL;
841 sig->group_stop_count = 0;
842 sig->curr_target = NULL;
843 init_sigpending(&sig->shared_pending);
844 INIT_LIST_HEAD(&sig->posix_timers);
846 sig->tty = current->signal->tty;
847 sig->pgrp = process_group(current);
848 sig->session = current->signal->session;
849 sig->leader = 0; /* session leadership doesn't inherit */
850 sig->tty_old_pgrp = 0;
855 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
857 unsigned long new_flags = p->flags;
859 new_flags &= ~PF_SUPERPRIV;
860 new_flags |= PF_FORKNOEXEC;
861 if (!(clone_flags & CLONE_PTRACE))
863 p->flags = new_flags;
866 asmlinkage long sys_set_tid_address(int __user *tidptr)
868 current->clear_child_tid = tidptr;
874 * This creates a new process as a copy of the old one,
875 * but does not actually start it yet.
877 * It copies the registers, and all the appropriate
878 * parts of the process environment (as per the clone
879 * flags). The actual kick-off is left to the caller.
881 struct task_struct *copy_process(unsigned long clone_flags,
882 unsigned long stack_start,
883 struct pt_regs *regs,
884 unsigned long stack_size,
885 int __user *parent_tidptr,
886 int __user *child_tidptr)
889 struct task_struct *p = NULL;
892 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
893 return ERR_PTR(-EINVAL);
896 * Thread groups must share signals as well, and detached threads
897 * can only be started up within the thread group.
899 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
900 return ERR_PTR(-EINVAL);
903 * Shared signal handlers imply shared VM. By way of the above,
904 * thread groups also imply shared VM. Blocking this case allows
905 * for various simplifications in other code.
907 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
908 return ERR_PTR(-EINVAL);
910 retval = security_task_create(clone_flags);
916 p = dup_task_struct(current);
921 set_vx_info(&p->vx_info, current->vx_info);
923 set_nx_info(&p->nx_info, current->nx_info);
925 /* check vserver memory */
926 if (p->mm && !(clone_flags & CLONE_VM)) {
927 if (vx_vmpages_avail(p->mm, p->mm->total_vm))
928 vx_pages_add(p->mm->mm_vx_info, RLIMIT_AS, p->mm->total_vm);
932 if (p->mm && vx_flags(VXF_FORK_RSS, 0)) {
933 if (!vx_rsspages_avail(p->mm, p->mm->rss))
938 if (!vx_nproc_avail(1))
941 if (atomic_read(&p->user->processes) >=
942 p->rlim[RLIMIT_NPROC].rlim_cur) {
943 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
944 p->user != &root_user)
948 atomic_inc(&p->user->__count);
949 atomic_inc(&p->user->processes);
950 get_group_info(p->group_info);
953 * If multiple threads are within copy_process(), then this check
954 * triggers too late. This doesn't hurt, the check is only there
955 * to stop root fork bombs.
957 if (nr_threads >= max_threads)
958 goto bad_fork_cleanup_count;
960 if (!try_module_get(p->thread_info->exec_domain->module))
961 goto bad_fork_cleanup_count;
963 if (p->binfmt && !try_module_get(p->binfmt->module))
964 goto bad_fork_cleanup_put_domain;
968 copy_flags(clone_flags, p);
969 if (clone_flags & CLONE_IDLETASK)
972 p->pid = alloc_pidmap();
974 goto bad_fork_cleanup;
977 if (clone_flags & CLONE_PARENT_SETTID)
978 if (put_user(p->pid, parent_tidptr))
979 goto bad_fork_cleanup;
981 p->proc_dentry = NULL;
983 INIT_LIST_HEAD(&p->children);
984 INIT_LIST_HEAD(&p->sibling);
985 init_waitqueue_head(&p->wait_chldexit);
986 p->vfork_done = NULL;
987 spin_lock_init(&p->alloc_lock);
988 spin_lock_init(&p->proc_lock);
990 clear_tsk_thread_flag(p, TIF_SIGPENDING);
991 init_sigpending(&p->pending);
993 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
994 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
995 init_timer(&p->real_timer);
996 p->real_timer.data = (unsigned long) p;
998 p->utime = p->stime = 0;
999 p->cutime = p->cstime = 0;
1000 p->lock_depth = -1; /* -1 = no lock */
1001 p->start_time = get_jiffies_64();
1003 p->io_context = NULL;
1004 p->audit_context = NULL;
1006 p->mempolicy = mpol_copy(p->mempolicy);
1007 if (IS_ERR(p->mempolicy)) {
1008 retval = PTR_ERR(p->mempolicy);
1009 p->mempolicy = NULL;
1010 goto bad_fork_cleanup;
1015 if ((retval = security_task_alloc(p)))
1016 goto bad_fork_cleanup_policy;
1017 if ((retval = audit_alloc(p)))
1018 goto bad_fork_cleanup_security;
1019 /* copy all the process information */
1020 if ((retval = copy_semundo(clone_flags, p)))
1021 goto bad_fork_cleanup_audit;
1022 if ((retval = copy_files(clone_flags, p)))
1023 goto bad_fork_cleanup_semundo;
1024 if ((retval = copy_fs(clone_flags, p)))
1025 goto bad_fork_cleanup_files;
1026 if ((retval = copy_sighand(clone_flags, p)))
1027 goto bad_fork_cleanup_fs;
1028 if ((retval = copy_signal(clone_flags, p)))
1029 goto bad_fork_cleanup_sighand;
1030 if ((retval = copy_mm(clone_flags, p)))
1031 goto bad_fork_cleanup_signal;
1032 if ((retval = copy_namespace(clone_flags, p)))
1033 goto bad_fork_cleanup_mm;
1034 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
1036 goto bad_fork_cleanup_namespace;
1038 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
1040 * Clear TID on mm_release()?
1042 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
1045 * Syscall tracing should be turned off in the child regardless
1048 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1050 /* Our parent execution domain becomes current domain
1051 These must match for thread signalling to apply */
1053 p->parent_exec_id = p->self_exec_id;
1055 /* ok, now we should be set up.. */
1056 p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
1057 p->pdeath_signal = 0;
1059 /* Perform scheduler related setup */
1063 * Ok, make it visible to the rest of the system.
1064 * We dont wake it up yet.
1067 p->group_leader = p;
1068 INIT_LIST_HEAD(&p->ptrace_children);
1069 INIT_LIST_HEAD(&p->ptrace_list);
1071 /* Need tasklist lock for parent etc handling! */
1072 write_lock_irq(&tasklist_lock);
1074 * Check for pending SIGKILL! The new thread should not be allowed
1075 * to slip out of an OOM kill. (or normal SIGKILL.)
1077 if (sigismember(¤t->pending.signal, SIGKILL)) {
1078 write_unlock_irq(&tasklist_lock);
1080 goto bad_fork_cleanup_namespace;
1083 /* CLONE_PARENT re-uses the old parent */
1084 if (clone_flags & CLONE_PARENT)
1085 p->real_parent = current->real_parent;
1087 p->real_parent = current;
1088 p->parent = p->real_parent;
1090 if (clone_flags & CLONE_THREAD) {
1091 spin_lock(¤t->sighand->siglock);
1093 * Important: if an exit-all has been started then
1094 * do not create this new thread - the whole thread
1095 * group is supposed to exit anyway.
1097 if (current->signal->group_exit) {
1098 spin_unlock(¤t->sighand->siglock);
1099 write_unlock_irq(&tasklist_lock);
1101 goto bad_fork_cleanup_namespace;
1103 p->tgid = current->tgid;
1104 p->group_leader = current->group_leader;
1106 if (current->signal->group_stop_count > 0) {
1108 * There is an all-stop in progress for the group.
1109 * We ourselves will stop as soon as we check signals.
1110 * Make the new thread part of that group stop too.
1112 current->signal->group_stop_count++;
1113 set_tsk_thread_flag(p, TIF_SIGPENDING);
1116 spin_unlock(¤t->sighand->siglock);
1120 if (p->ptrace & PT_PTRACED)
1121 __ptrace_link(p, current->parent);
1123 attach_pid(p, PIDTYPE_PID, p->pid);
1124 if (thread_group_leader(p)) {
1125 attach_pid(p, PIDTYPE_TGID, p->tgid);
1126 attach_pid(p, PIDTYPE_PGID, process_group(p));
1127 attach_pid(p, PIDTYPE_SID, p->signal->session);
1129 __get_cpu_var(process_counts)++;
1131 link_pid(p, p->pids + PIDTYPE_TGID, &p->group_leader->pids[PIDTYPE_TGID].pid);
1134 vxi = current->vx_info;
1136 atomic_inc(&vxi->cacct.nr_threads);
1137 // atomic_inc(&vxi->limit.rcur[RLIMIT_NPROC]);
1140 write_unlock_irq(&tasklist_lock);
1145 return ERR_PTR(retval);
1148 bad_fork_cleanup_namespace:
1150 bad_fork_cleanup_mm:
1153 mmdrop(p->active_mm);
1154 bad_fork_cleanup_signal:
1156 bad_fork_cleanup_sighand:
1158 bad_fork_cleanup_fs:
1159 exit_fs(p); /* blocking */
1160 bad_fork_cleanup_files:
1161 exit_files(p); /* blocking */
1162 bad_fork_cleanup_semundo:
1164 bad_fork_cleanup_audit:
1166 bad_fork_cleanup_security:
1167 security_task_free(p);
1168 bad_fork_cleanup_policy:
1170 mpol_free(p->mempolicy);
1174 free_pidmap(p->pid);
1176 module_put(p->binfmt->module);
1177 bad_fork_cleanup_put_domain:
1178 module_put(p->thread_info->exec_domain->module);
1179 bad_fork_cleanup_count:
1180 put_group_info(p->group_info);
1181 atomic_dec(&p->user->processes);
1188 static inline int fork_traceflag (unsigned clone_flags)
1190 if (clone_flags & (CLONE_UNTRACED | CLONE_IDLETASK))
1192 else if (clone_flags & CLONE_VFORK) {
1193 if (current->ptrace & PT_TRACE_VFORK)
1194 return PTRACE_EVENT_VFORK;
1195 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1196 if (current->ptrace & PT_TRACE_CLONE)
1197 return PTRACE_EVENT_CLONE;
1198 } else if (current->ptrace & PT_TRACE_FORK)
1199 return PTRACE_EVENT_FORK;
1205 * Ok, this is the main fork-routine.
1207 * It copies the process, and if successful kick-starts
1208 * it and waits for it to finish using the VM if required.
1210 long do_fork(unsigned long clone_flags,
1211 unsigned long stack_start,
1212 struct pt_regs *regs,
1213 unsigned long stack_size,
1214 int __user *parent_tidptr,
1215 int __user *child_tidptr)
1217 struct task_struct *p;
1221 if (unlikely(current->ptrace)) {
1222 trace = fork_traceflag (clone_flags);
1224 clone_flags |= CLONE_PTRACE;
1227 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
1228 if (numtasks_get_ref(current->taskclass, 0) == 0) {
1233 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr);
1235 * Do this prior waking up the new thread - the thread pointer
1236 * might get invalid after that point, if the thread exits quickly.
1238 pid = IS_ERR(p) ? PTR_ERR(p) : p->pid;
1241 struct completion vfork;
1245 if (clone_flags & CLONE_VFORK) {
1246 p->vfork_done = &vfork;
1247 init_completion(&vfork);
1250 if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
1252 * We'll start up with an immediate SIGSTOP.
1254 sigaddset(&p->pending.signal, SIGSTOP);
1255 set_tsk_thread_flag(p, TIF_SIGPENDING);
1258 if (!(clone_flags & CLONE_STOPPED)) {
1260 * Do the wakeup last. On SMP we treat fork() and
1261 * CLONE_VM separately, because fork() has already
1262 * created cache footprint on this CPU (due to
1263 * copying the pagetables), hence migration would
1264 * probably be costy. Threads on the other hand
1265 * have less traction to the current CPU, and if
1266 * there's an imbalance then the scheduler can
1267 * migrate this fresh thread now, before it
1268 * accumulates a larger cache footprint:
1270 if (clone_flags & CLONE_VM)
1271 wake_up_forked_thread(p);
1273 wake_up_forked_process(p);
1275 int cpu = get_cpu();
1277 p->state = TASK_STOPPED;
1278 if (cpu_is_offline(task_cpu(p)))
1279 set_task_cpu(p, cpu);
1285 if (unlikely (trace)) {
1286 current->ptrace_message = pid;
1287 ptrace_notify ((trace << 8) | SIGTRAP);
1290 if (clone_flags & CLONE_VFORK) {
1291 wait_for_completion(&vfork);
1292 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1293 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1296 * Let the child process run first, to avoid most of the
1297 * COW overhead when the child exec()s afterwards.
1301 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
1302 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);