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/key.h>
28 #include <linux/binfmts.h>
29 #include <linux/mman.h>
31 #include <linux/cpu.h>
32 #include <linux/security.h>
33 #include <linux/swap.h>
34 #include <linux/syscalls.h>
35 #include <linux/jiffies.h>
36 #include <linux/futex.h>
37 #include <linux/ptrace.h>
38 #include <linux/mount.h>
39 #include <linux/audit.h>
40 #include <linux/profile.h>
41 #include <linux/rmap.h>
43 #include <asm/pgtable.h>
44 #include <asm/pgalloc.h>
45 #include <asm/uaccess.h>
46 #include <asm/mmu_context.h>
47 #include <asm/cacheflush.h>
48 #include <asm/tlbflush.h>
50 /* The idle threads do not count..
51 * Protected by write_lock_irq(&tasklist_lock)
56 unsigned long total_forks; /* Handle normal Linux uptimes. */
58 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
60 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
62 EXPORT_SYMBOL(tasklist_lock);
64 int nr_processes(void)
69 for_each_online_cpu(cpu)
70 total += per_cpu(process_counts, cpu);
75 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
76 # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
77 # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
78 static kmem_cache_t *task_struct_cachep;
81 void free_task(struct task_struct *tsk)
83 free_thread_info(tsk->thread_info);
84 free_task_struct(tsk);
86 EXPORT_SYMBOL(free_task);
88 void __put_task_struct(struct task_struct *tsk)
90 WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
91 WARN_ON(atomic_read(&tsk->usage));
92 WARN_ON(tsk == current);
94 if (unlikely(tsk->audit_context))
96 security_task_free(tsk);
98 put_group_info(tsk->group_info);
100 if (!profile_handoff_task(tsk))
104 void __init fork_init(unsigned long mempages)
106 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
107 #ifndef ARCH_MIN_TASKALIGN
108 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
110 /* create a slab on which task_structs can be allocated */
112 kmem_cache_create("task_struct", sizeof(struct task_struct),
113 ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
117 * The default maximum number of threads is set to a safe
118 * value: the thread structures can take up at most half
121 max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
124 * we need to allow at least 20 threads to boot a system
129 init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
130 init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
133 static struct task_struct *dup_task_struct(struct task_struct *orig)
135 struct task_struct *tsk;
136 struct thread_info *ti;
138 prepare_to_copy(orig);
140 tsk = alloc_task_struct();
144 ti = alloc_thread_info(tsk);
146 free_task_struct(tsk);
150 *ti = *orig->thread_info;
152 tsk->thread_info = ti;
155 /* One for us, one for whoever does the "release_task()" (usually parent) */
156 atomic_set(&tsk->usage,2);
161 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
163 struct vm_area_struct * mpnt, *tmp, **pprev;
164 struct rb_node **rb_link, *rb_parent;
166 unsigned long charge;
167 struct mempolicy *pol;
169 down_write(&oldmm->mmap_sem);
170 flush_cache_mm(current->mm);
173 mm->mmap_cache = NULL;
174 mm->free_area_cache = oldmm->mmap_base;
178 cpus_clear(mm->cpu_vm_mask);
180 rb_link = &mm->mm_rb.rb_node;
184 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
187 if (mpnt->vm_flags & VM_DONTCOPY) {
188 __vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
193 if (mpnt->vm_flags & VM_ACCOUNT) {
194 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
195 if (security_vm_enough_memory(len))
199 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
203 pol = mpol_copy(vma_policy(mpnt));
204 retval = PTR_ERR(pol);
206 goto fail_nomem_policy;
207 vma_set_policy(tmp, pol);
208 tmp->vm_flags &= ~VM_LOCKED;
214 struct inode *inode = file->f_dentry->d_inode;
216 if (tmp->vm_flags & VM_DENYWRITE)
217 atomic_dec(&inode->i_writecount);
219 /* insert tmp into the share list, just after mpnt */
220 spin_lock(&file->f_mapping->i_mmap_lock);
221 flush_dcache_mmap_lock(file->f_mapping);
222 vma_prio_tree_add(tmp, mpnt);
223 flush_dcache_mmap_unlock(file->f_mapping);
224 spin_unlock(&file->f_mapping->i_mmap_lock);
228 * Link in the new vma and copy the page table entries:
229 * link in first so that swapoff can see swap entries,
230 * and try_to_unmap_one's find_vma find the new vma.
232 spin_lock(&mm->page_table_lock);
234 pprev = &tmp->vm_next;
236 __vma_link_rb(mm, tmp, rb_link, rb_parent);
237 rb_link = &tmp->vm_rb.rb_right;
238 rb_parent = &tmp->vm_rb;
241 retval = copy_page_range(mm, current->mm, tmp);
242 spin_unlock(&mm->page_table_lock);
244 if (tmp->vm_ops && tmp->vm_ops->open)
245 tmp->vm_ops->open(tmp);
253 flush_tlb_mm(current->mm);
254 up_write(&oldmm->mmap_sem);
257 kmem_cache_free(vm_area_cachep, tmp);
260 vm_unacct_memory(charge);
264 static inline int mm_alloc_pgd(struct mm_struct * mm)
266 mm->pgd = pgd_alloc(mm);
267 if (unlikely(!mm->pgd))
272 static inline void mm_free_pgd(struct mm_struct * mm)
277 #define dup_mmap(mm, oldmm) (0)
278 #define mm_alloc_pgd(mm) (0)
279 #define mm_free_pgd(mm)
280 #endif /* CONFIG_MMU */
282 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
284 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
285 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
287 #include <linux/init_task.h>
289 static struct mm_struct * mm_init(struct mm_struct * mm)
291 atomic_set(&mm->mm_users, 1);
292 atomic_set(&mm->mm_count, 1);
293 init_rwsem(&mm->mmap_sem);
294 INIT_LIST_HEAD(&mm->mmlist);
295 mm->core_waiters = 0;
297 spin_lock_init(&mm->page_table_lock);
298 rwlock_init(&mm->ioctx_list_lock);
299 mm->ioctx_list = NULL;
300 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
301 mm->free_area_cache = TASK_UNMAPPED_BASE;
303 if (likely(!mm_alloc_pgd(mm))) {
312 * Allocate and initialize an mm_struct.
314 struct mm_struct * mm_alloc(void)
316 struct mm_struct * mm;
320 memset(mm, 0, sizeof(*mm));
327 * Called when the last reference to the mm
328 * is dropped: either by a lazy thread or by
329 * mmput. Free the page directory and the mm.
331 void fastcall __mmdrop(struct mm_struct *mm)
333 BUG_ON(mm == &init_mm);
340 * Decrement the use count and release all resources for an mm.
342 void mmput(struct mm_struct *mm)
344 if (atomic_dec_and_test(&mm->mm_users)) {
347 if (!list_empty(&mm->mmlist)) {
348 spin_lock(&mmlist_lock);
349 list_del(&mm->mmlist);
350 spin_unlock(&mmlist_lock);
356 EXPORT_SYMBOL_GPL(mmput);
359 * get_task_mm - acquire a reference to the task's mm
361 * Returns %NULL if the task has no mm. Checks PF_BORROWED_MM (meaning
362 * this kernel workthread has transiently adopted a user mm with use_mm,
363 * to do its AIO) is not set and if so returns a reference to it, after
364 * bumping up the use count. User must release the mm via mmput()
365 * after use. Typically used by /proc and ptrace.
367 struct mm_struct *get_task_mm(struct task_struct *task)
369 struct mm_struct *mm;
374 if (task->flags & PF_BORROWED_MM)
377 atomic_inc(&mm->mm_users);
382 EXPORT_SYMBOL_GPL(get_task_mm);
384 /* Please note the differences between mmput and mm_release.
385 * mmput is called whenever we stop holding onto a mm_struct,
386 * error success whatever.
388 * mm_release is called after a mm_struct has been removed
389 * from the current process.
391 * This difference is important for error handling, when we
392 * only half set up a mm_struct for a new process and need to restore
393 * the old one. Because we mmput the new mm_struct before
394 * restoring the old one. . .
395 * Eric Biederman 10 January 1998
397 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
399 struct completion *vfork_done = tsk->vfork_done;
401 /* Get rid of any cached register state */
402 deactivate_mm(tsk, mm);
404 /* notify parent sleeping on vfork() */
406 tsk->vfork_done = NULL;
407 complete(vfork_done);
409 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
410 u32 __user * tidptr = tsk->clear_child_tid;
411 tsk->clear_child_tid = NULL;
414 * We don't check the error code - if userspace has
415 * not set up a proper pointer then tough luck.
418 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
422 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
424 struct mm_struct * mm, *oldmm;
427 tsk->min_flt = tsk->maj_flt = 0;
428 tsk->nvcsw = tsk->nivcsw = 0;
431 tsk->active_mm = NULL;
434 * Are we cloning a kernel thread?
436 * We need to steal a active VM for that..
442 if (clone_flags & CLONE_VM) {
443 atomic_inc(&oldmm->mm_users);
446 * There are cases where the PTL is held to ensure no
447 * new threads start up in user mode using an mm, which
448 * allows optimizing out ipis; the tlb_gather_mmu code
451 spin_unlock_wait(&oldmm->page_table_lock);
460 /* Copy the current MM stuff.. */
461 memcpy(mm, oldmm, sizeof(*mm));
465 if (init_new_context(tsk,mm))
468 retval = dup_mmap(mm, oldmm);
484 * If init_new_context() failed, we cannot use mmput() to free the mm
485 * because it calls destroy_context()
492 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
494 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
495 /* We don't need to lock fs - think why ;-) */
497 atomic_set(&fs->count, 1);
498 rwlock_init(&fs->lock);
499 fs->umask = old->umask;
500 read_lock(&old->lock);
501 fs->rootmnt = mntget(old->rootmnt);
502 fs->root = dget(old->root);
503 fs->pwdmnt = mntget(old->pwdmnt);
504 fs->pwd = dget(old->pwd);
506 fs->altrootmnt = mntget(old->altrootmnt);
507 fs->altroot = dget(old->altroot);
509 fs->altrootmnt = NULL;
512 read_unlock(&old->lock);
517 struct fs_struct *copy_fs_struct(struct fs_struct *old)
519 return __copy_fs_struct(old);
522 EXPORT_SYMBOL_GPL(copy_fs_struct);
524 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
526 if (clone_flags & CLONE_FS) {
527 atomic_inc(¤t->fs->count);
530 tsk->fs = __copy_fs_struct(current->fs);
536 static int count_open_files(struct files_struct *files, int size)
540 /* Find the last open fd */
541 for (i = size/(8*sizeof(long)); i > 0; ) {
542 if (files->open_fds->fds_bits[--i])
545 i = (i+1) * 8 * sizeof(long);
549 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
551 struct files_struct *oldf, *newf;
552 struct file **old_fds, **new_fds;
553 int open_files, nfds, size, i, error = 0;
556 * A background process may not have any files ...
558 oldf = current->files;
562 if (clone_flags & CLONE_FILES) {
563 atomic_inc(&oldf->count);
568 * Note: we may be using current for both targets (See exec.c)
569 * This works because we cache current->files (old) as oldf. Don't
574 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
578 atomic_set(&newf->count, 1);
580 spin_lock_init(&newf->file_lock);
582 newf->max_fds = NR_OPEN_DEFAULT;
583 newf->max_fdset = __FD_SETSIZE;
584 newf->close_on_exec = &newf->close_on_exec_init;
585 newf->open_fds = &newf->open_fds_init;
586 newf->fd = &newf->fd_array[0];
588 /* We don't yet have the oldf readlock, but even if the old
589 fdset gets grown now, we'll only copy up to "size" fds */
590 size = oldf->max_fdset;
591 if (size > __FD_SETSIZE) {
593 spin_lock(&newf->file_lock);
594 error = expand_fdset(newf, size-1);
595 spin_unlock(&newf->file_lock);
599 spin_lock(&oldf->file_lock);
601 open_files = count_open_files(oldf, size);
604 * Check whether we need to allocate a larger fd array.
605 * Note: we're not a clone task, so the open count won't
608 nfds = NR_OPEN_DEFAULT;
609 if (open_files > nfds) {
610 spin_unlock(&oldf->file_lock);
612 spin_lock(&newf->file_lock);
613 error = expand_fd_array(newf, open_files-1);
614 spin_unlock(&newf->file_lock);
617 nfds = newf->max_fds;
618 spin_lock(&oldf->file_lock);
624 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
625 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
627 for (i = open_files; i != 0; i--) {
628 struct file *f = *old_fds++;
633 * The fd may be claimed in the fd bitmap but not yet
634 * instantiated in the files array if a sibling thread
635 * is partway through open(). So make sure that this
636 * fd is available to the new process.
638 FD_CLR(open_files - i, newf->open_fds);
642 spin_unlock(&oldf->file_lock);
644 /* compute the remainder to be cleared */
645 size = (newf->max_fds - open_files) * sizeof(struct file *);
647 /* This is long word aligned thus could use a optimized version */
648 memset(new_fds, 0, size);
650 if (newf->max_fdset > open_files) {
651 int left = (newf->max_fdset-open_files)/8;
652 int start = open_files / (8 * sizeof(unsigned long));
654 memset(&newf->open_fds->fds_bits[start], 0, left);
655 memset(&newf->close_on_exec->fds_bits[start], 0, left);
664 free_fdset (newf->close_on_exec, newf->max_fdset);
665 free_fdset (newf->open_fds, newf->max_fdset);
666 kmem_cache_free(files_cachep, newf);
671 * Helper to unshare the files of the current task.
672 * We don't want to expose copy_files internals to
673 * the exec layer of the kernel.
676 int unshare_files(void)
678 struct files_struct *files = current->files;
684 /* This can race but the race causes us to copy when we don't
685 need to and drop the copy */
686 if(atomic_read(&files->count) == 1)
688 atomic_inc(&files->count);
691 rc = copy_files(0, current);
693 current->files = files;
697 EXPORT_SYMBOL(unshare_files);
699 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
701 struct sighand_struct *sig;
703 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
704 atomic_inc(¤t->sighand->count);
707 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
711 spin_lock_init(&sig->siglock);
712 atomic_set(&sig->count, 1);
713 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
717 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
719 struct signal_struct *sig;
721 if (clone_flags & CLONE_THREAD) {
722 atomic_inc(¤t->signal->count);
723 atomic_inc(¤t->signal->live);
726 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
730 atomic_set(&sig->count, 1);
731 atomic_set(&sig->live, 1);
733 sig->group_exit_code = 0;
734 sig->group_exit_task = NULL;
735 sig->group_stop_count = 0;
737 sig->curr_target = NULL;
738 init_sigpending(&sig->shared_pending);
739 INIT_LIST_HEAD(&sig->posix_timers);
741 sig->tty = current->signal->tty;
742 sig->pgrp = process_group(current);
743 sig->session = current->signal->session;
744 sig->leader = 0; /* session leadership doesn't inherit */
745 sig->tty_old_pgrp = 0;
747 sig->utime = sig->stime = sig->cutime = sig->cstime = 0;
748 sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
749 sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
751 task_lock(current->group_leader);
752 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
753 task_unlock(current->group_leader);
758 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
760 unsigned long new_flags = p->flags;
762 new_flags &= ~PF_SUPERPRIV;
763 new_flags |= PF_FORKNOEXEC;
764 if (!(clone_flags & CLONE_PTRACE))
766 p->flags = new_flags;
769 asmlinkage long sys_set_tid_address(int __user *tidptr)
771 current->clear_child_tid = tidptr;
777 * This creates a new process as a copy of the old one,
778 * but does not actually start it yet.
780 * It copies the registers, and all the appropriate
781 * parts of the process environment (as per the clone
782 * flags). The actual kick-off is left to the caller.
784 static task_t *copy_process(unsigned long clone_flags,
785 unsigned long stack_start,
786 struct pt_regs *regs,
787 unsigned long stack_size,
788 int __user *parent_tidptr,
789 int __user *child_tidptr,
793 struct task_struct *p = NULL;
795 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
796 return ERR_PTR(-EINVAL);
799 * Thread groups must share signals as well, and detached threads
800 * can only be started up within the thread group.
802 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
803 return ERR_PTR(-EINVAL);
806 * Shared signal handlers imply shared VM. By way of the above,
807 * thread groups also imply shared VM. Blocking this case allows
808 * for various simplifications in other code.
810 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
811 return ERR_PTR(-EINVAL);
813 retval = security_task_create(clone_flags);
818 p = dup_task_struct(current);
823 if (atomic_read(&p->user->processes) >=
824 p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
825 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
826 p->user != &root_user)
830 atomic_inc(&p->user->__count);
831 atomic_inc(&p->user->processes);
832 get_group_info(p->group_info);
835 * If multiple threads are within copy_process(), then this check
836 * triggers too late. This doesn't hurt, the check is only there
837 * to stop root fork bombs.
839 if (nr_threads >= max_threads)
840 goto bad_fork_cleanup_count;
842 if (!try_module_get(p->thread_info->exec_domain->module))
843 goto bad_fork_cleanup_count;
845 if (p->binfmt && !try_module_get(p->binfmt->module))
846 goto bad_fork_cleanup_put_domain;
849 copy_flags(clone_flags, p);
852 if (clone_flags & CLONE_PARENT_SETTID)
853 if (put_user(p->pid, parent_tidptr))
854 goto bad_fork_cleanup;
856 p->proc_dentry = NULL;
858 INIT_LIST_HEAD(&p->children);
859 INIT_LIST_HEAD(&p->sibling);
860 init_waitqueue_head(&p->wait_chldexit);
861 p->vfork_done = NULL;
862 spin_lock_init(&p->alloc_lock);
863 spin_lock_init(&p->proc_lock);
865 clear_tsk_thread_flag(p, TIF_SIGPENDING);
866 init_sigpending(&p->pending);
868 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
869 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
870 init_timer(&p->real_timer);
871 p->real_timer.data = (unsigned long) p;
873 p->utime = p->stime = 0;
874 p->lock_depth = -1; /* -1 = no lock */
875 do_posix_clock_monotonic_gettime(&p->start_time);
877 p->io_context = NULL;
879 p->audit_context = NULL;
881 p->mempolicy = mpol_copy(p->mempolicy);
882 if (IS_ERR(p->mempolicy)) {
883 retval = PTR_ERR(p->mempolicy);
885 goto bad_fork_cleanup;
890 if (clone_flags & CLONE_THREAD)
891 p->tgid = current->tgid;
893 if ((retval = security_task_alloc(p)))
894 goto bad_fork_cleanup_policy;
895 if ((retval = audit_alloc(p)))
896 goto bad_fork_cleanup_security;
897 /* copy all the process information */
898 if ((retval = copy_semundo(clone_flags, p)))
899 goto bad_fork_cleanup_audit;
900 if ((retval = copy_files(clone_flags, p)))
901 goto bad_fork_cleanup_semundo;
902 if ((retval = copy_fs(clone_flags, p)))
903 goto bad_fork_cleanup_files;
904 if ((retval = copy_sighand(clone_flags, p)))
905 goto bad_fork_cleanup_fs;
906 if ((retval = copy_signal(clone_flags, p)))
907 goto bad_fork_cleanup_sighand;
908 if ((retval = copy_mm(clone_flags, p)))
909 goto bad_fork_cleanup_signal;
910 if ((retval = copy_keys(clone_flags, p)))
911 goto bad_fork_cleanup_mm;
912 if ((retval = copy_namespace(clone_flags, p)))
913 goto bad_fork_cleanup_keys;
914 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
916 goto bad_fork_cleanup_namespace;
918 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
920 * Clear TID on mm_release()?
922 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
925 * Syscall tracing should be turned off in the child regardless
928 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
930 /* Our parent execution domain becomes current domain
931 These must match for thread signalling to apply */
933 p->parent_exec_id = p->self_exec_id;
935 /* ok, now we should be set up.. */
936 p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
937 p->pdeath_signal = 0;
940 /* Perform scheduler related setup */
944 * Ok, make it visible to the rest of the system.
945 * We dont wake it up yet.
948 INIT_LIST_HEAD(&p->ptrace_children);
949 INIT_LIST_HEAD(&p->ptrace_list);
951 /* Need tasklist lock for parent etc handling! */
952 write_lock_irq(&tasklist_lock);
955 * The task hasn't been attached yet, so cpus_allowed mask cannot
956 * have changed. The cpus_allowed mask of the parent may have
957 * changed after it was copied first time, and it may then move to
958 * another CPU - so we re-copy it here and set the child's CPU to
959 * the parent's CPU. This avoids alot of nasty races.
961 p->cpus_allowed = current->cpus_allowed;
962 set_task_cpu(p, smp_processor_id());
965 * Check for pending SIGKILL! The new thread should not be allowed
966 * to slip out of an OOM kill. (or normal SIGKILL.)
968 if (sigismember(¤t->pending.signal, SIGKILL)) {
969 write_unlock_irq(&tasklist_lock);
971 goto bad_fork_cleanup_namespace;
974 /* CLONE_PARENT re-uses the old parent */
975 if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
976 p->real_parent = current->real_parent;
978 p->real_parent = current;
979 p->parent = p->real_parent;
981 if (clone_flags & CLONE_THREAD) {
982 spin_lock(¤t->sighand->siglock);
984 * Important: if an exit-all has been started then
985 * do not create this new thread - the whole thread
986 * group is supposed to exit anyway.
988 if (current->signal->group_exit) {
989 spin_unlock(¤t->sighand->siglock);
990 write_unlock_irq(&tasklist_lock);
992 goto bad_fork_cleanup_namespace;
994 p->group_leader = current->group_leader;
996 if (current->signal->group_stop_count > 0) {
998 * There is an all-stop in progress for the group.
999 * We ourselves will stop as soon as we check signals.
1000 * Make the new thread part of that group stop too.
1002 current->signal->group_stop_count++;
1003 set_tsk_thread_flag(p, TIF_SIGPENDING);
1006 spin_unlock(¤t->sighand->siglock);
1010 if (unlikely(p->ptrace & PT_PTRACED))
1011 __ptrace_link(p, current->parent);
1013 attach_pid(p, PIDTYPE_PID, p->pid);
1014 attach_pid(p, PIDTYPE_TGID, p->tgid);
1015 if (thread_group_leader(p)) {
1016 attach_pid(p, PIDTYPE_PGID, process_group(p));
1017 attach_pid(p, PIDTYPE_SID, p->signal->session);
1019 __get_cpu_var(process_counts)++;
1023 write_unlock_irq(&tasklist_lock);
1028 return ERR_PTR(retval);
1031 bad_fork_cleanup_namespace:
1033 bad_fork_cleanup_keys:
1035 bad_fork_cleanup_mm:
1038 bad_fork_cleanup_signal:
1040 bad_fork_cleanup_sighand:
1042 bad_fork_cleanup_fs:
1043 exit_fs(p); /* blocking */
1044 bad_fork_cleanup_files:
1045 exit_files(p); /* blocking */
1046 bad_fork_cleanup_semundo:
1048 bad_fork_cleanup_audit:
1050 bad_fork_cleanup_security:
1051 security_task_free(p);
1052 bad_fork_cleanup_policy:
1054 mpol_free(p->mempolicy);
1058 module_put(p->binfmt->module);
1059 bad_fork_cleanup_put_domain:
1060 module_put(p->thread_info->exec_domain->module);
1061 bad_fork_cleanup_count:
1062 put_group_info(p->group_info);
1063 atomic_dec(&p->user->processes);
1070 struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
1072 memset(regs, 0, sizeof(struct pt_regs));
1076 task_t * __devinit fork_idle(int cpu)
1079 struct pt_regs regs;
1081 task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, NULL, 0);
1083 return ERR_PTR(-ENOMEM);
1084 init_idle(task, cpu);
1085 unhash_process(task);
1089 static inline int fork_traceflag (unsigned clone_flags)
1091 if (clone_flags & CLONE_UNTRACED)
1093 else if (clone_flags & CLONE_VFORK) {
1094 if (current->ptrace & PT_TRACE_VFORK)
1095 return PTRACE_EVENT_VFORK;
1096 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1097 if (current->ptrace & PT_TRACE_CLONE)
1098 return PTRACE_EVENT_CLONE;
1099 } else if (current->ptrace & PT_TRACE_FORK)
1100 return PTRACE_EVENT_FORK;
1106 * Ok, this is the main fork-routine.
1108 * It copies the process, and if successful kick-starts
1109 * it and waits for it to finish using the VM if required.
1111 long do_fork(unsigned long clone_flags,
1112 unsigned long stack_start,
1113 struct pt_regs *regs,
1114 unsigned long stack_size,
1115 int __user *parent_tidptr,
1116 int __user *child_tidptr)
1118 struct task_struct *p;
1120 long pid = alloc_pidmap();
1124 if (unlikely(current->ptrace)) {
1125 trace = fork_traceflag (clone_flags);
1127 clone_flags |= CLONE_PTRACE;
1130 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
1132 * Do this prior waking up the new thread - the thread pointer
1133 * might get invalid after that point, if the thread exits quickly.
1136 struct completion vfork;
1138 if (clone_flags & CLONE_VFORK) {
1139 p->vfork_done = &vfork;
1140 init_completion(&vfork);
1143 if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
1145 * We'll start up with an immediate SIGSTOP.
1147 sigaddset(&p->pending.signal, SIGSTOP);
1148 set_tsk_thread_flag(p, TIF_SIGPENDING);
1151 if (!(clone_flags & CLONE_STOPPED))
1152 wake_up_new_task(p, clone_flags);
1154 p->state = TASK_STOPPED;
1157 if (unlikely (trace)) {
1158 current->ptrace_message = pid;
1159 ptrace_notify ((trace << 8) | SIGTRAP);
1162 if (clone_flags & CLONE_VFORK) {
1163 wait_for_completion(&vfork);
1164 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1165 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1174 /* SLAB cache for signal_struct structures (tsk->signal) */
1175 kmem_cache_t *signal_cachep;
1177 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1178 kmem_cache_t *sighand_cachep;
1180 /* SLAB cache for files_struct structures (tsk->files) */
1181 kmem_cache_t *files_cachep;
1183 /* SLAB cache for fs_struct structures (tsk->fs) */
1184 kmem_cache_t *fs_cachep;
1186 /* SLAB cache for vm_area_struct structures */
1187 kmem_cache_t *vm_area_cachep;
1189 /* SLAB cache for mm_struct structures (tsk->mm) */
1190 kmem_cache_t *mm_cachep;
1192 void __init proc_caches_init(void)
1194 sighand_cachep = kmem_cache_create("sighand_cache",
1195 sizeof(struct sighand_struct), 0,
1196 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1197 signal_cachep = kmem_cache_create("signal_cache",
1198 sizeof(struct signal_struct), 0,
1199 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1200 files_cachep = kmem_cache_create("files_cache",
1201 sizeof(struct files_struct), 0,
1202 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1203 fs_cachep = kmem_cache_create("fs_cache",
1204 sizeof(struct fs_struct), 0,
1205 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1206 vm_area_cachep = kmem_cache_create("vm_area_struct",
1207 sizeof(struct vm_area_struct), 0,
1208 SLAB_PANIC, NULL, NULL);
1209 mm_cachep = kmem_cache_create("mm_struct",
1210 sizeof(struct mm_struct), 0,
1211 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);