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/swap.h>
33 #include <linux/syscalls.h>
34 #include <linux/jiffies.h>
35 #include <linux/futex.h>
36 #include <linux/ptrace.h>
37 #include <linux/mount.h>
38 #include <linux/audit.h>
39 #include <linux/profile.h>
40 #include <linux/rmap.h>
41 #include <linux/vs_network.h>
42 #include <linux/vs_limit.h>
43 #include <linux/vs_memory.h>
45 #include <asm/pgtable.h>
46 #include <asm/pgalloc.h>
47 #include <asm/uaccess.h>
48 #include <asm/mmu_context.h>
49 #include <asm/cacheflush.h>
50 #include <asm/tlbflush.h>
52 /* The idle threads do not count..
53 * Protected by write_lock_irq(&tasklist_lock)
58 unsigned long total_forks; /* Handle normal Linux uptimes. */
60 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
62 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
64 EXPORT_SYMBOL(tasklist_lock);
66 int nr_processes(void)
71 for_each_online_cpu(cpu)
72 total += per_cpu(process_counts, cpu);
77 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
78 # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
79 # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
80 static kmem_cache_t *task_struct_cachep;
83 void free_task(struct task_struct *tsk)
85 free_thread_info(tsk->thread_info);
86 clr_vx_info(&tsk->vx_info);
87 clr_nx_info(&tsk->nx_info);
88 free_task_struct(tsk);
90 EXPORT_SYMBOL(free_task);
92 void __put_task_struct(struct task_struct *tsk)
94 WARN_ON(!(tsk->state & (TASK_DEAD | TASK_ZOMBIE)));
95 WARN_ON(atomic_read(&tsk->usage));
96 WARN_ON(tsk == current);
98 if (unlikely(tsk->audit_context))
100 security_task_free(tsk);
102 put_group_info(tsk->group_info);
104 if (!profile_handoff_task(tsk))
108 void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
112 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
113 spin_lock_irqsave(&q->lock, flags);
114 __add_wait_queue(q, wait);
115 spin_unlock_irqrestore(&q->lock, flags);
118 EXPORT_SYMBOL(add_wait_queue);
120 void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
124 wait->flags |= WQ_FLAG_EXCLUSIVE;
125 spin_lock_irqsave(&q->lock, flags);
126 __add_wait_queue_tail(q, wait);
127 spin_unlock_irqrestore(&q->lock, flags);
130 EXPORT_SYMBOL(add_wait_queue_exclusive);
132 void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
136 spin_lock_irqsave(&q->lock, flags);
137 __remove_wait_queue(q, wait);
138 spin_unlock_irqrestore(&q->lock, flags);
141 EXPORT_SYMBOL(remove_wait_queue);
145 * Note: we use "set_current_state()" _after_ the wait-queue add,
146 * because we need a memory barrier there on SMP, so that any
147 * wake-function that tests for the wait-queue being active
148 * will be guaranteed to see waitqueue addition _or_ subsequent
149 * tests in this thread will see the wakeup having taken place.
151 * The spin_unlock() itself is semi-permeable and only protects
152 * one way (it only protects stuff inside the critical region and
153 * stops them from bleeding out - it would still allow subsequent
154 * loads to move into the the critical region).
156 void fastcall prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
160 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
161 spin_lock_irqsave(&q->lock, flags);
162 if (list_empty(&wait->task_list))
163 __add_wait_queue(q, wait);
165 * don't alter the task state if this is just going to
166 * queue an async wait queue callback
168 if (is_sync_wait(wait))
169 set_current_state(state);
170 spin_unlock_irqrestore(&q->lock, flags);
173 EXPORT_SYMBOL(prepare_to_wait);
176 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
180 wait->flags |= WQ_FLAG_EXCLUSIVE;
181 spin_lock_irqsave(&q->lock, flags);
182 if (list_empty(&wait->task_list))
183 __add_wait_queue_tail(q, wait);
185 * don't alter the task state if this is just going to
186 * queue an async wait queue callback
188 if (is_sync_wait(wait))
189 set_current_state(state);
190 spin_unlock_irqrestore(&q->lock, flags);
193 EXPORT_SYMBOL(prepare_to_wait_exclusive);
195 void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
199 __set_current_state(TASK_RUNNING);
201 * We can check for list emptiness outside the lock
203 * - we use the "careful" check that verifies both
204 * the next and prev pointers, so that there cannot
205 * be any half-pending updates in progress on other
206 * CPU's that we haven't seen yet (and that might
207 * still change the stack area.
209 * - all other users take the lock (ie we can only
210 * have _one_ other CPU that looks at or modifies
213 if (!list_empty_careful(&wait->task_list)) {
214 spin_lock_irqsave(&q->lock, flags);
215 list_del_init(&wait->task_list);
216 spin_unlock_irqrestore(&q->lock, flags);
220 EXPORT_SYMBOL(finish_wait);
222 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
224 int ret = default_wake_function(wait, mode, sync, key);
227 list_del_init(&wait->task_list);
231 EXPORT_SYMBOL(autoremove_wake_function);
233 void __init fork_init(unsigned long mempages)
235 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
236 #ifndef ARCH_MIN_TASKALIGN
237 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
239 /* create a slab on which task_structs can be allocated */
241 kmem_cache_create("task_struct", sizeof(struct task_struct),
242 ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
246 * The default maximum number of threads is set to a safe
247 * value: the thread structures can take up at most half
250 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8;
252 * we need to allow at least 20 threads to boot a system
257 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
258 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
261 static struct task_struct *dup_task_struct(struct task_struct *orig)
263 struct task_struct *tsk;
264 struct thread_info *ti;
266 prepare_to_copy(orig);
268 tsk = alloc_task_struct();
272 ti = alloc_thread_info(tsk);
274 free_task_struct(tsk);
278 *ti = *orig->thread_info;
280 tsk->thread_info = ti;
283 /* One for us, one for whoever does the "release_task()" (usually parent) */
284 atomic_set(&tsk->usage,2);
289 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
291 struct vm_area_struct * mpnt, *tmp, **pprev;
292 struct rb_node **rb_link, *rb_parent;
294 unsigned long charge;
295 struct mempolicy *pol;
297 down_write(&oldmm->mmap_sem);
298 flush_cache_mm(current->mm);
301 mm->mmap_cache = NULL;
302 mm->free_area_cache = oldmm->mmap_base;
305 cpus_clear(mm->cpu_vm_mask);
307 rb_link = &mm->mm_rb.rb_node;
312 * Add it to the mmlist after the parent.
313 * Doing it this way means that we can order the list,
314 * and fork() won't mess up the ordering significantly.
315 * Add it first so that swapoff can see any swap entries.
317 spin_lock(&mmlist_lock);
318 list_add(&mm->mmlist, ¤t->mm->mmlist);
320 spin_unlock(&mmlist_lock);
322 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
325 if (mpnt->vm_flags & VM_DONTCOPY) {
326 __vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
331 if (mpnt->vm_flags & VM_ACCOUNT) {
332 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
333 if (security_vm_enough_memory(len))
337 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
341 pol = mpol_copy(vma_policy(mpnt));
342 retval = PTR_ERR(pol);
344 goto fail_nomem_policy;
345 vma_set_policy(tmp, pol);
346 tmp->vm_flags &= ~VM_LOCKED;
352 struct inode *inode = file->f_dentry->d_inode;
354 if (tmp->vm_flags & VM_DENYWRITE)
355 atomic_dec(&inode->i_writecount);
357 /* insert tmp into the share list, just after mpnt */
358 spin_lock(&file->f_mapping->i_mmap_lock);
359 flush_dcache_mmap_lock(file->f_mapping);
360 vma_prio_tree_add(tmp, mpnt);
361 flush_dcache_mmap_unlock(file->f_mapping);
362 spin_unlock(&file->f_mapping->i_mmap_lock);
366 * Link in the new vma and copy the page table entries:
367 * link in first so that swapoff can see swap entries,
368 * and try_to_unmap_one's find_vma find the new vma.
370 spin_lock(&mm->page_table_lock);
372 pprev = &tmp->vm_next;
374 __vma_link_rb(mm, tmp, rb_link, rb_parent);
375 rb_link = &tmp->vm_rb.rb_right;
376 rb_parent = &tmp->vm_rb;
379 retval = copy_page_range(mm, current->mm, tmp);
380 spin_unlock(&mm->page_table_lock);
382 if (tmp->vm_ops && tmp->vm_ops->open)
383 tmp->vm_ops->open(tmp);
391 flush_tlb_mm(current->mm);
392 up_write(&oldmm->mmap_sem);
395 kmem_cache_free(vm_area_cachep, tmp);
398 vm_unacct_memory(charge);
402 static inline int mm_alloc_pgd(struct mm_struct * mm)
404 mm->pgd = pgd_alloc(mm);
405 if (unlikely(!mm->pgd))
410 static inline void mm_free_pgd(struct mm_struct * mm)
415 #define dup_mmap(mm, oldmm) (0)
416 #define mm_alloc_pgd(mm) (0)
417 #define mm_free_pgd(mm)
418 #endif /* CONFIG_MMU */
420 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
423 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
424 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
426 #include <linux/init_task.h>
428 static struct mm_struct * mm_init(struct mm_struct * mm)
430 atomic_set(&mm->mm_users, 1);
431 atomic_set(&mm->mm_count, 1);
432 init_rwsem(&mm->mmap_sem);
433 mm->core_waiters = 0;
434 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
435 mm->ioctx_list_lock = RW_LOCK_UNLOCKED;
436 mm->ioctx_list = NULL;
437 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
438 mm->free_area_cache = TASK_UNMAPPED_BASE;
440 if (likely(!mm_alloc_pgd(mm))) {
442 set_vx_info(&mm->mm_vx_info, current->vx_info);
450 * Allocate and initialize an mm_struct.
452 struct mm_struct * mm_alloc(void)
454 struct mm_struct * mm;
458 memset(mm, 0, sizeof(*mm));
465 * Called when the last reference to the mm
466 * is dropped: either by a lazy thread or by
467 * mmput. Free the page directory and the mm.
469 void fastcall __mmdrop(struct mm_struct *mm)
471 BUG_ON(mm == &init_mm);
474 clr_vx_info(&mm->mm_vx_info);
479 * Decrement the use count and release all resources for an mm.
481 void mmput(struct mm_struct *mm)
483 if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)) {
484 list_del(&mm->mmlist);
486 spin_unlock(&mmlist_lock);
493 EXPORT_SYMBOL_GPL(mmput);
496 * get_task_mm - acquire a reference to the task's mm
498 * Returns %NULL if the task has no mm. Checks if the use count
499 * of the mm is non-zero and if so returns a reference to it, after
500 * bumping up the use count. User must release the mm via mmput()
501 * after use. Typically used by /proc and ptrace.
503 * If the use count is zero, it means that this mm is going away,
504 * so return %NULL. This only happens in the case of an AIO daemon
505 * which has temporarily adopted an mm (see use_mm), in the course
506 * of its final mmput, before exit_aio has completed.
508 struct mm_struct *get_task_mm(struct task_struct *task)
510 struct mm_struct *mm;
515 spin_lock(&mmlist_lock);
516 if (!atomic_read(&mm->mm_users))
519 atomic_inc(&mm->mm_users);
520 spin_unlock(&mmlist_lock);
525 EXPORT_SYMBOL_GPL(get_task_mm);
527 /* Please note the differences between mmput and mm_release.
528 * mmput is called whenever we stop holding onto a mm_struct,
529 * error success whatever.
531 * mm_release is called after a mm_struct has been removed
532 * from the current process.
534 * This difference is important for error handling, when we
535 * only half set up a mm_struct for a new process and need to restore
536 * the old one. Because we mmput the new mm_struct before
537 * restoring the old one. . .
538 * Eric Biederman 10 January 1998
540 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
542 struct completion *vfork_done = tsk->vfork_done;
544 /* Get rid of any cached register state */
545 deactivate_mm(tsk, mm);
547 /* notify parent sleeping on vfork() */
549 tsk->vfork_done = NULL;
550 complete(vfork_done);
552 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
553 u32 __user * tidptr = tsk->clear_child_tid;
554 tsk->clear_child_tid = NULL;
557 * We don't check the error code - if userspace has
558 * not set up a proper pointer then tough luck.
561 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
565 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
567 struct mm_struct * mm, *oldmm;
570 tsk->min_flt = tsk->maj_flt = 0;
571 tsk->nvcsw = tsk->nivcsw = 0;
574 tsk->active_mm = NULL;
577 * Are we cloning a kernel thread?
579 * We need to steal a active VM for that..
585 if (clone_flags & CLONE_VM) {
586 atomic_inc(&oldmm->mm_users);
589 * There are cases where the PTL is held to ensure no
590 * new threads start up in user mode using an mm, which
591 * allows optimizing out ipis; the tlb_gather_mmu code
594 spin_unlock_wait(&oldmm->page_table_lock);
603 /* Copy the current MM stuff.. */
604 memcpy(mm, oldmm, sizeof(*mm));
605 mm->mm_vx_info = NULL;
609 if (init_new_context(tsk,mm))
612 retval = dup_mmap(mm, oldmm);
628 * If init_new_context() failed, we cannot use mmput() to free the mm
629 * because it calls destroy_context()
636 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
638 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
639 /* We don't need to lock fs - think why ;-) */
641 atomic_set(&fs->count, 1);
642 fs->lock = RW_LOCK_UNLOCKED;
643 fs->umask = old->umask;
644 read_lock(&old->lock);
645 fs->rootmnt = mntget(old->rootmnt);
646 fs->root = dget(old->root);
647 fs->pwdmnt = mntget(old->pwdmnt);
648 fs->pwd = dget(old->pwd);
650 fs->altrootmnt = mntget(old->altrootmnt);
651 fs->altroot = dget(old->altroot);
653 fs->altrootmnt = NULL;
656 read_unlock(&old->lock);
661 struct fs_struct *copy_fs_struct(struct fs_struct *old)
663 return __copy_fs_struct(old);
666 EXPORT_SYMBOL_GPL(copy_fs_struct);
668 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
670 if (clone_flags & CLONE_FS) {
671 atomic_inc(¤t->fs->count);
674 tsk->fs = __copy_fs_struct(current->fs);
680 static int count_open_files(struct files_struct *files, int size)
684 /* Find the last open fd */
685 for (i = size/(8*sizeof(long)); i > 0; ) {
686 if (files->open_fds->fds_bits[--i])
689 i = (i+1) * 8 * sizeof(long);
693 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
695 struct files_struct *oldf, *newf;
696 struct file **old_fds, **new_fds;
697 int open_files, nfds, size, i, error = 0;
700 * A background process may not have any files ...
702 oldf = current->files;
706 if (clone_flags & CLONE_FILES) {
707 atomic_inc(&oldf->count);
712 * Note: we may be using current for both targets (See exec.c)
713 * This works because we cache current->files (old) as oldf. Don't
718 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
722 atomic_set(&newf->count, 1);
724 newf->file_lock = SPIN_LOCK_UNLOCKED;
726 newf->max_fds = NR_OPEN_DEFAULT;
727 newf->max_fdset = __FD_SETSIZE;
728 newf->close_on_exec = &newf->close_on_exec_init;
729 newf->open_fds = &newf->open_fds_init;
730 newf->fd = &newf->fd_array[0];
732 /* We don't yet have the oldf readlock, but even if the old
733 fdset gets grown now, we'll only copy up to "size" fds */
734 size = oldf->max_fdset;
735 if (size > __FD_SETSIZE) {
737 spin_lock(&newf->file_lock);
738 error = expand_fdset(newf, size-1);
739 spin_unlock(&newf->file_lock);
743 spin_lock(&oldf->file_lock);
745 open_files = count_open_files(oldf, size);
748 * Check whether we need to allocate a larger fd array.
749 * Note: we're not a clone task, so the open count won't
752 nfds = NR_OPEN_DEFAULT;
753 if (open_files > nfds) {
754 spin_unlock(&oldf->file_lock);
756 spin_lock(&newf->file_lock);
757 error = expand_fd_array(newf, open_files-1);
758 spin_unlock(&newf->file_lock);
761 nfds = newf->max_fds;
762 spin_lock(&oldf->file_lock);
768 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
769 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
771 for (i = open_files; i != 0; i--) {
772 struct file *f = *old_fds++;
777 spin_unlock(&oldf->file_lock);
779 /* compute the remainder to be cleared */
780 size = (newf->max_fds - open_files) * sizeof(struct file *);
782 /* This is long word aligned thus could use a optimized version */
783 memset(new_fds, 0, size);
785 if (newf->max_fdset > open_files) {
786 int left = (newf->max_fdset-open_files)/8;
787 int start = open_files / (8 * sizeof(unsigned long));
789 memset(&newf->open_fds->fds_bits[start], 0, left);
790 memset(&newf->close_on_exec->fds_bits[start], 0, left);
799 free_fdset (newf->close_on_exec, newf->max_fdset);
800 free_fdset (newf->open_fds, newf->max_fdset);
801 kmem_cache_free(files_cachep, newf);
806 * Helper to unshare the files of the current task.
807 * We don't want to expose copy_files internals to
808 * the exec layer of the kernel.
811 int unshare_files(void)
813 struct files_struct *files = current->files;
819 /* This can race but the race causes us to copy when we don't
820 need to and drop the copy */
821 if(atomic_read(&files->count) == 1)
823 atomic_inc(&files->count);
826 rc = copy_files(0, current);
828 current->files = files;
832 EXPORT_SYMBOL(unshare_files);
834 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
836 struct sighand_struct *sig;
838 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
839 atomic_inc(¤t->sighand->count);
842 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
846 spin_lock_init(&sig->siglock);
847 atomic_set(&sig->count, 1);
848 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
852 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
854 struct signal_struct *sig;
856 if (clone_flags & CLONE_THREAD) {
857 atomic_inc(¤t->signal->count);
860 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
864 atomic_set(&sig->count, 1);
866 sig->group_exit_code = 0;
867 sig->group_exit_task = NULL;
868 sig->group_stop_count = 0;
869 sig->curr_target = NULL;
870 init_sigpending(&sig->shared_pending);
871 INIT_LIST_HEAD(&sig->posix_timers);
873 sig->tty = current->signal->tty;
874 sig->pgrp = process_group(current);
875 sig->session = current->signal->session;
876 sig->leader = 0; /* session leadership doesn't inherit */
877 sig->tty_old_pgrp = 0;
879 sig->utime = sig->stime = sig->cutime = sig->cstime = 0;
880 sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
881 sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
886 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
888 unsigned long new_flags = p->flags;
890 new_flags &= ~PF_SUPERPRIV;
891 new_flags |= PF_FORKNOEXEC;
892 if (!(clone_flags & CLONE_PTRACE))
894 p->flags = new_flags;
897 asmlinkage long sys_set_tid_address(int __user *tidptr)
899 current->clear_child_tid = tidptr;
905 * This creates a new process as a copy of the old one,
906 * but does not actually start it yet.
908 * It copies the registers, and all the appropriate
909 * parts of the process environment (as per the clone
910 * flags). The actual kick-off is left to the caller.
912 static task_t *copy_process(unsigned long clone_flags,
913 unsigned long stack_start,
914 struct pt_regs *regs,
915 unsigned long stack_size,
916 int __user *parent_tidptr,
917 int __user *child_tidptr,
921 struct task_struct *p = NULL;
924 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
925 return ERR_PTR(-EINVAL);
928 * Thread groups must share signals as well, and detached threads
929 * can only be started up within the thread group.
931 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
932 return ERR_PTR(-EINVAL);
935 * Shared signal handlers imply shared VM. By way of the above,
936 * thread groups also imply shared VM. Blocking this case allows
937 * for various simplifications in other code.
939 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
940 return ERR_PTR(-EINVAL);
942 retval = security_task_create(clone_flags);
947 p = dup_task_struct(current);
952 set_vx_info(&p->vx_info, current->vx_info);
954 set_nx_info(&p->nx_info, current->nx_info);
956 /* check vserver memory */
957 if (p->mm && !(clone_flags & CLONE_VM)) {
958 if (vx_vmpages_avail(p->mm, p->mm->total_vm))
959 vx_pages_add(p->mm->mm_vx_info, RLIMIT_AS, p->mm->total_vm);
963 if (p->mm && vx_flags(VXF_FORK_RSS, 0)) {
964 if (!vx_rsspages_avail(p->mm, p->mm->rss))
965 goto bad_fork_cleanup_vm;
969 if (!vx_nproc_avail(1))
970 goto bad_fork_cleanup_vm;
972 if (atomic_read(&p->user->processes) >=
973 p->rlim[RLIMIT_NPROC].rlim_cur) {
974 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
975 p->user != &root_user)
976 goto bad_fork_cleanup_vm;
979 atomic_inc(&p->user->__count);
980 atomic_inc(&p->user->processes);
981 get_group_info(p->group_info);
984 * If multiple threads are within copy_process(), then this check
985 * triggers too late. This doesn't hurt, the check is only there
986 * to stop root fork bombs.
988 if (nr_threads >= max_threads)
989 goto bad_fork_cleanup_count;
991 if (!try_module_get(p->thread_info->exec_domain->module))
992 goto bad_fork_cleanup_count;
994 if (p->binfmt && !try_module_get(p->binfmt->module))
995 goto bad_fork_cleanup_put_domain;
998 copy_flags(clone_flags, p);
1001 if (clone_flags & CLONE_PARENT_SETTID)
1002 if (put_user(p->pid, parent_tidptr))
1003 goto bad_fork_cleanup;
1005 p->proc_dentry = NULL;
1007 INIT_LIST_HEAD(&p->children);
1008 INIT_LIST_HEAD(&p->sibling);
1009 init_waitqueue_head(&p->wait_chldexit);
1010 p->vfork_done = NULL;
1011 spin_lock_init(&p->alloc_lock);
1012 spin_lock_init(&p->proc_lock);
1014 clear_tsk_thread_flag(p, TIF_SIGPENDING);
1015 init_sigpending(&p->pending);
1017 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
1018 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
1019 init_timer(&p->real_timer);
1020 p->real_timer.data = (unsigned long) p;
1022 p->utime = p->stime = 0;
1023 p->lock_depth = -1; /* -1 = no lock */
1024 do_posix_clock_monotonic_gettime(&p->start_time);
1026 p->io_context = NULL;
1028 p->audit_context = NULL;
1030 p->mempolicy = mpol_copy(p->mempolicy);
1031 if (IS_ERR(p->mempolicy)) {
1032 retval = PTR_ERR(p->mempolicy);
1033 p->mempolicy = NULL;
1034 goto bad_fork_cleanup;
1038 if ((retval = security_task_alloc(p)))
1039 goto bad_fork_cleanup_policy;
1040 if ((retval = audit_alloc(p)))
1041 goto bad_fork_cleanup_security;
1042 /* copy all the process information */
1043 if ((retval = copy_semundo(clone_flags, p)))
1044 goto bad_fork_cleanup_audit;
1045 if ((retval = copy_files(clone_flags, p)))
1046 goto bad_fork_cleanup_semundo;
1047 if ((retval = copy_fs(clone_flags, p)))
1048 goto bad_fork_cleanup_files;
1049 if ((retval = copy_sighand(clone_flags, p)))
1050 goto bad_fork_cleanup_fs;
1051 if ((retval = copy_signal(clone_flags, p)))
1052 goto bad_fork_cleanup_sighand;
1053 if ((retval = copy_mm(clone_flags, p)))
1054 goto bad_fork_cleanup_signal;
1055 if ((retval = copy_namespace(clone_flags, p)))
1056 goto bad_fork_cleanup_mm;
1057 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
1059 goto bad_fork_cleanup_namespace;
1061 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
1063 * Clear TID on mm_release()?
1065 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
1068 * Syscall tracing should be turned off in the child regardless
1071 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1073 /* Our parent execution domain becomes current domain
1074 These must match for thread signalling to apply */
1076 p->parent_exec_id = p->self_exec_id;
1078 /* ok, now we should be set up.. */
1079 p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
1080 p->pdeath_signal = 0;
1082 /* Perform scheduler related setup */
1086 * Ok, make it visible to the rest of the system.
1087 * We dont wake it up yet.
1090 p->group_leader = p;
1091 INIT_LIST_HEAD(&p->ptrace_children);
1092 INIT_LIST_HEAD(&p->ptrace_list);
1094 /* Need tasklist lock for parent etc handling! */
1095 write_lock_irq(&tasklist_lock);
1098 * The task hasn't been attached yet, so cpus_allowed mask cannot
1099 * have changed. The cpus_allowed mask of the parent may have
1100 * changed after it was copied first time, and it may then move to
1101 * another CPU - so we re-copy it here and set the child's CPU to
1102 * the parent's CPU. This avoids alot of nasty races.
1104 p->cpus_allowed = current->cpus_allowed;
1105 set_task_cpu(p, smp_processor_id());
1108 * Check for pending SIGKILL! The new thread should not be allowed
1109 * to slip out of an OOM kill. (or normal SIGKILL.)
1111 if (sigismember(¤t->pending.signal, SIGKILL)) {
1112 write_unlock_irq(&tasklist_lock);
1114 goto bad_fork_cleanup_namespace;
1117 /* CLONE_PARENT re-uses the old parent */
1118 if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
1119 p->real_parent = current->real_parent;
1121 p->real_parent = current;
1122 p->parent = p->real_parent;
1124 if (clone_flags & CLONE_THREAD) {
1125 spin_lock(¤t->sighand->siglock);
1127 * Important: if an exit-all has been started then
1128 * do not create this new thread - the whole thread
1129 * group is supposed to exit anyway.
1131 if (current->signal->group_exit) {
1132 spin_unlock(¤t->sighand->siglock);
1133 write_unlock_irq(&tasklist_lock);
1135 goto bad_fork_cleanup_namespace;
1137 p->tgid = current->tgid;
1138 p->group_leader = current->group_leader;
1140 if (current->signal->group_stop_count > 0) {
1142 * There is an all-stop in progress for the group.
1143 * We ourselves will stop as soon as we check signals.
1144 * Make the new thread part of that group stop too.
1146 current->signal->group_stop_count++;
1147 set_tsk_thread_flag(p, TIF_SIGPENDING);
1150 spin_unlock(¤t->sighand->siglock);
1154 if (unlikely(p->ptrace & PT_PTRACED))
1155 __ptrace_link(p, current->parent);
1157 attach_pid(p, PIDTYPE_PID, p->pid);
1158 attach_pid(p, PIDTYPE_TGID, p->tgid);
1159 if (thread_group_leader(p)) {
1160 attach_pid(p, PIDTYPE_PGID, process_group(p));
1161 attach_pid(p, PIDTYPE_SID, p->signal->session);
1163 __get_cpu_var(process_counts)++;
1167 /* p is copy of current */
1170 atomic_inc(&vxi->cvirt.nr_threads);
1173 write_unlock_irq(&tasklist_lock);
1178 return ERR_PTR(retval);
1181 bad_fork_cleanup_namespace:
1183 bad_fork_cleanup_mm:
1186 bad_fork_cleanup_signal:
1188 bad_fork_cleanup_sighand:
1190 bad_fork_cleanup_fs:
1191 exit_fs(p); /* blocking */
1192 bad_fork_cleanup_files:
1193 exit_files(p); /* blocking */
1194 bad_fork_cleanup_semundo:
1196 bad_fork_cleanup_audit:
1198 bad_fork_cleanup_security:
1199 security_task_free(p);
1200 bad_fork_cleanup_policy:
1202 mpol_free(p->mempolicy);
1206 module_put(p->binfmt->module);
1207 bad_fork_cleanup_put_domain:
1208 module_put(p->thread_info->exec_domain->module);
1209 bad_fork_cleanup_count:
1210 put_group_info(p->group_info);
1211 atomic_dec(&p->user->processes);
1213 bad_fork_cleanup_vm:
1214 if (p->mm && !(clone_flags & CLONE_VM))
1215 vx_pages_sub(p->mm->mm_vx_info, RLIMIT_AS, p->mm->total_vm);
1221 struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
1223 memset(regs, 0, sizeof(struct pt_regs));
1227 task_t * __devinit fork_idle(int cpu)
1230 struct pt_regs regs;
1232 task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, NULL, 0);
1234 return ERR_PTR(-ENOMEM);
1235 init_idle(task, cpu);
1236 unhash_process(task);
1240 static inline int fork_traceflag (unsigned clone_flags)
1242 if (clone_flags & CLONE_UNTRACED)
1244 else if (clone_flags & CLONE_VFORK) {
1245 if (current->ptrace & PT_TRACE_VFORK)
1246 return PTRACE_EVENT_VFORK;
1247 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1248 if (current->ptrace & PT_TRACE_CLONE)
1249 return PTRACE_EVENT_CLONE;
1250 } else if (current->ptrace & PT_TRACE_FORK)
1251 return PTRACE_EVENT_FORK;
1257 * Ok, this is the main fork-routine.
1259 * It copies the process, and if successful kick-starts
1260 * it and waits for it to finish using the VM if required.
1262 long do_fork(unsigned long clone_flags,
1263 unsigned long stack_start,
1264 struct pt_regs *regs,
1265 unsigned long stack_size,
1266 int __user *parent_tidptr,
1267 int __user *child_tidptr)
1269 struct task_struct *p;
1271 long pid = alloc_pidmap();
1275 if (unlikely(current->ptrace)) {
1276 trace = fork_traceflag (clone_flags);
1278 clone_flags |= CLONE_PTRACE;
1281 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
1283 * Do this prior waking up the new thread - the thread pointer
1284 * might get invalid after that point, if the thread exits quickly.
1287 struct completion vfork;
1289 if (clone_flags & CLONE_VFORK) {
1290 p->vfork_done = &vfork;
1291 init_completion(&vfork);
1294 if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
1296 * We'll start up with an immediate SIGSTOP.
1298 sigaddset(&p->pending.signal, SIGSTOP);
1299 set_tsk_thread_flag(p, TIF_SIGPENDING);
1302 if (!(clone_flags & CLONE_STOPPED))
1303 wake_up_new_task(p, clone_flags);
1305 p->state = TASK_STOPPED;
1308 if (unlikely (trace)) {
1309 current->ptrace_message = pid;
1310 ptrace_notify ((trace << 8) | SIGTRAP);
1313 if (clone_flags & CLONE_VFORK) {
1314 wait_for_completion(&vfork);
1315 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1316 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1325 /* SLAB cache for signal_struct structures (tsk->signal) */
1326 kmem_cache_t *signal_cachep;
1328 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1329 kmem_cache_t *sighand_cachep;
1331 /* SLAB cache for files_struct structures (tsk->files) */
1332 kmem_cache_t *files_cachep;
1334 /* SLAB cache for fs_struct structures (tsk->fs) */
1335 kmem_cache_t *fs_cachep;
1337 /* SLAB cache for vm_area_struct structures */
1338 kmem_cache_t *vm_area_cachep;
1340 /* SLAB cache for mm_struct structures (tsk->mm) */
1341 kmem_cache_t *mm_cachep;
1343 void __init proc_caches_init(void)
1345 sighand_cachep = kmem_cache_create("sighand_cache",
1346 sizeof(struct sighand_struct), 0,
1347 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1348 signal_cachep = kmem_cache_create("signal_cache",
1349 sizeof(struct signal_struct), 0,
1350 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1351 files_cachep = kmem_cache_create("files_cache",
1352 sizeof(struct files_struct), 0,
1353 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1354 fs_cachep = kmem_cache_create("fs_cache",
1355 sizeof(struct fs_struct), 0,
1356 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1357 vm_area_cachep = kmem_cache_create("vm_area_struct",
1358 sizeof(struct vm_area_struct), 0,
1359 SLAB_PANIC, NULL, NULL);
1360 mm_cachep = kmem_cache_create("mm_struct",
1361 sizeof(struct mm_struct), 0,
1362 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);