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/sem.h>
25 #include <linux/file.h>
26 #include <linux/binfmts.h>
27 #include <linux/mman.h>
29 #include <linux/security.h>
30 #include <linux/syscalls.h>
31 #include <linux/jiffies.h>
32 #include <linux/futex.h>
33 #include <linux/ptrace.h>
34 #include <linux/mount.h>
35 #include <linux/audit.h>
37 #include <asm/pgtable.h>
38 #include <asm/pgalloc.h>
39 #include <asm/uaccess.h>
40 #include <asm/mmu_context.h>
41 #include <asm/cacheflush.h>
42 #include <asm/tlbflush.h>
44 /* The idle threads do not count..
45 * Protected by write_lock_irq(&tasklist_lock)
50 unsigned long total_forks; /* Handle normal Linux uptimes. */
52 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
54 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
56 EXPORT_SYMBOL(tasklist_lock);
58 int nr_processes(void)
64 total += per_cpu(process_counts, cpu);
69 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
70 # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
71 # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
72 static kmem_cache_t *task_struct_cachep;
75 static void free_task(struct task_struct *tsk)
77 free_thread_info(tsk->thread_info);
78 free_task_struct(tsk);
81 void __put_task_struct(struct task_struct *tsk)
83 WARN_ON(!(tsk->state & (TASK_DEAD | TASK_ZOMBIE)));
84 WARN_ON(atomic_read(&tsk->usage));
85 WARN_ON(tsk == current);
87 if (unlikely(tsk->audit_context))
89 security_task_free(tsk);
91 put_group_info(tsk->group_info);
95 void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
99 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
100 spin_lock_irqsave(&q->lock, flags);
101 __add_wait_queue(q, wait);
102 spin_unlock_irqrestore(&q->lock, flags);
105 EXPORT_SYMBOL(add_wait_queue);
107 void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
111 wait->flags |= WQ_FLAG_EXCLUSIVE;
112 spin_lock_irqsave(&q->lock, flags);
113 __add_wait_queue_tail(q, wait);
114 spin_unlock_irqrestore(&q->lock, flags);
117 EXPORT_SYMBOL(add_wait_queue_exclusive);
119 void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
123 spin_lock_irqsave(&q->lock, flags);
124 __remove_wait_queue(q, wait);
125 spin_unlock_irqrestore(&q->lock, flags);
128 EXPORT_SYMBOL(remove_wait_queue);
132 * Note: we use "set_current_state()" _after_ the wait-queue add,
133 * because we need a memory barrier there on SMP, so that any
134 * wake-function that tests for the wait-queue being active
135 * will be guaranteed to see waitqueue addition _or_ subsequent
136 * tests in this thread will see the wakeup having taken place.
138 * The spin_unlock() itself is semi-permeable and only protects
139 * one way (it only protects stuff inside the critical region and
140 * stops them from bleeding out - it would still allow subsequent
141 * loads to move into the the critical region).
143 void fastcall prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
147 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
148 spin_lock_irqsave(&q->lock, flags);
149 if (list_empty(&wait->task_list))
150 __add_wait_queue(q, wait);
151 set_current_state(state);
152 spin_unlock_irqrestore(&q->lock, flags);
155 EXPORT_SYMBOL(prepare_to_wait);
158 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
162 wait->flags |= WQ_FLAG_EXCLUSIVE;
163 spin_lock_irqsave(&q->lock, flags);
164 if (list_empty(&wait->task_list))
165 __add_wait_queue_tail(q, wait);
166 set_current_state(state);
167 spin_unlock_irqrestore(&q->lock, flags);
170 EXPORT_SYMBOL(prepare_to_wait_exclusive);
172 void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
176 __set_current_state(TASK_RUNNING);
178 * We can check for list emptiness outside the lock
180 * - we use the "careful" check that verifies both
181 * the next and prev pointers, so that there cannot
182 * be any half-pending updates in progress on other
183 * CPU's that we haven't seen yet (and that might
184 * still change the stack area.
186 * - all other users take the lock (ie we can only
187 * have _one_ other CPU that looks at or modifies
190 if (!list_empty_careful(&wait->task_list)) {
191 spin_lock_irqsave(&q->lock, flags);
192 list_del_init(&wait->task_list);
193 spin_unlock_irqrestore(&q->lock, flags);
197 EXPORT_SYMBOL(finish_wait);
199 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync)
201 int ret = default_wake_function(wait, mode, sync);
204 list_del_init(&wait->task_list);
208 EXPORT_SYMBOL(autoremove_wake_function);
210 void __init fork_init(unsigned long mempages)
212 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
213 #ifndef ARCH_MIN_TASKALIGN
214 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
216 /* create a slab on which task_structs can be allocated */
218 kmem_cache_create("task_struct",
219 sizeof(struct task_struct),ARCH_MIN_TASKALIGN,
221 if (!task_struct_cachep)
222 panic("fork_init(): cannot create task_struct SLAB cache");
226 * The default maximum number of threads is set to a safe
227 * value: the thread structures can take up at most half
230 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8;
232 * we need to allow at least 20 threads to boot a system
237 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
238 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
241 static struct task_struct *dup_task_struct(struct task_struct *orig)
243 struct task_struct *tsk;
244 struct thread_info *ti;
246 prepare_to_copy(orig);
248 tsk = alloc_task_struct();
252 ti = alloc_thread_info(tsk);
254 free_task_struct(tsk);
258 *ti = *orig->thread_info;
260 tsk->thread_info = ti;
263 /* One for us, one for whoever does the "release_task()" (usually parent) */
264 atomic_set(&tsk->usage,2);
269 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
271 struct vm_area_struct * mpnt, *tmp, **pprev;
272 struct rb_node **rb_link, *rb_parent;
274 unsigned long charge = 0;
276 down_write(&oldmm->mmap_sem);
277 flush_cache_mm(current->mm);
280 mm->mmap_cache = NULL;
281 mm->free_area_cache = TASK_UNMAPPED_BASE;
284 cpus_clear(mm->cpu_vm_mask);
286 rb_link = &mm->mm_rb.rb_node;
291 * Add it to the mmlist after the parent.
292 * Doing it this way means that we can order the list,
293 * and fork() won't mess up the ordering significantly.
294 * Add it first so that swapoff can see any swap entries.
296 spin_lock(&mmlist_lock);
297 list_add(&mm->mmlist, ¤t->mm->mmlist);
299 spin_unlock(&mmlist_lock);
301 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
304 if(mpnt->vm_flags & VM_DONTCOPY)
306 if (mpnt->vm_flags & VM_ACCOUNT) {
307 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
308 if (security_vm_enough_memory(len))
312 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
316 tmp->vm_flags &= ~VM_LOCKED;
320 INIT_LIST_HEAD(&tmp->shared);
322 struct inode *inode = file->f_dentry->d_inode;
324 if (tmp->vm_flags & VM_DENYWRITE)
325 atomic_dec(&inode->i_writecount);
327 /* insert tmp into the share list, just after mpnt */
328 down(&file->f_mapping->i_shared_sem);
329 list_add(&tmp->shared, &mpnt->shared);
330 up(&file->f_mapping->i_shared_sem);
334 * Link in the new vma and copy the page table entries:
335 * link in first so that swapoff can see swap entries,
336 * and try_to_unmap_one's find_vma find the new vma.
338 spin_lock(&mm->page_table_lock);
340 pprev = &tmp->vm_next;
342 __vma_link_rb(mm, tmp, rb_link, rb_parent);
343 rb_link = &tmp->vm_rb.rb_right;
344 rb_parent = &tmp->vm_rb;
347 retval = copy_page_range(mm, current->mm, tmp);
348 spin_unlock(&mm->page_table_lock);
350 if (tmp->vm_ops && tmp->vm_ops->open)
351 tmp->vm_ops->open(tmp);
359 flush_tlb_mm(current->mm);
360 up_write(&oldmm->mmap_sem);
365 vm_unacct_memory(charge);
368 static inline int mm_alloc_pgd(struct mm_struct * mm)
370 mm->pgd = pgd_alloc(mm);
371 if (unlikely(!mm->pgd))
376 static inline void mm_free_pgd(struct mm_struct * mm)
381 #define dup_mmap(mm, oldmm) (0)
382 #define mm_alloc_pgd(mm) (0)
383 #define mm_free_pgd(mm)
384 #endif /* CONFIG_MMU */
386 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
389 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
390 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
392 #include <linux/init_task.h>
394 static struct mm_struct * mm_init(struct mm_struct * mm)
396 atomic_set(&mm->mm_users, 1);
397 atomic_set(&mm->mm_count, 1);
398 init_rwsem(&mm->mmap_sem);
399 mm->core_waiters = 0;
400 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
401 mm->ioctx_list_lock = RW_LOCK_UNLOCKED;
402 mm->ioctx_list = NULL;
403 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
404 mm->free_area_cache = TASK_UNMAPPED_BASE;
406 if (likely(!mm_alloc_pgd(mm))) {
415 * Allocate and initialize an mm_struct.
417 struct mm_struct * mm_alloc(void)
419 struct mm_struct * mm;
423 memset(mm, 0, sizeof(*mm));
430 * Called when the last reference to the mm
431 * is dropped: either by a lazy thread or by
432 * mmput. Free the page directory and the mm.
434 void fastcall __mmdrop(struct mm_struct *mm)
436 BUG_ON(mm == &init_mm);
443 * Decrement the use count and release all resources for an mm.
445 void mmput(struct mm_struct *mm)
447 if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)) {
448 list_del(&mm->mmlist);
450 spin_unlock(&mmlist_lock);
458 * Checks if the use count of an mm is non-zero and if so
459 * returns a reference to it after bumping up the use count.
460 * If the use count is zero, it means this mm is going away,
463 struct mm_struct *mmgrab(struct mm_struct *mm)
465 spin_lock(&mmlist_lock);
466 if (!atomic_read(&mm->mm_users))
469 atomic_inc(&mm->mm_users);
470 spin_unlock(&mmlist_lock);
474 /* Please note the differences between mmput and mm_release.
475 * mmput is called whenever we stop holding onto a mm_struct,
476 * error success whatever.
478 * mm_release is called after a mm_struct has been removed
479 * from the current process.
481 * This difference is important for error handling, when we
482 * only half set up a mm_struct for a new process and need to restore
483 * the old one. Because we mmput the new mm_struct before
484 * restoring the old one. . .
485 * Eric Biederman 10 January 1998
487 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
489 struct completion *vfork_done = tsk->vfork_done;
491 /* Get rid of any cached register state */
492 deactivate_mm(tsk, mm);
494 /* notify parent sleeping on vfork() */
496 tsk->vfork_done = NULL;
497 complete(vfork_done);
499 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
500 u32 __user * tidptr = tsk->clear_child_tid;
501 tsk->clear_child_tid = NULL;
504 * We don't check the error code - if userspace has
505 * not set up a proper pointer then tough luck.
508 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL);
512 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
514 struct mm_struct * mm, *oldmm;
517 tsk->min_flt = tsk->maj_flt = 0;
518 tsk->cmin_flt = tsk->cmaj_flt = 0;
519 tsk->nvcsw = tsk->nivcsw = tsk->cnvcsw = tsk->cnivcsw = 0;
522 tsk->active_mm = NULL;
525 * Are we cloning a kernel thread?
527 * We need to steal a active VM for that..
533 if (clone_flags & CLONE_VM) {
534 atomic_inc(&oldmm->mm_users);
537 * There are cases where the PTL is held to ensure no
538 * new threads start up in user mode using an mm, which
539 * allows optimizing out ipis; the tlb_gather_mmu code
542 spin_unlock_wait(&oldmm->page_table_lock);
551 /* Copy the current MM stuff.. */
552 memcpy(mm, oldmm, sizeof(*mm));
556 if (init_new_context(tsk,mm))
559 retval = dup_mmap(mm, oldmm);
575 * If init_new_context() failed, we cannot use mmput() to free the mm
576 * because it calls destroy_context()
583 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
585 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
586 /* We don't need to lock fs - think why ;-) */
588 atomic_set(&fs->count, 1);
589 fs->lock = RW_LOCK_UNLOCKED;
590 fs->umask = old->umask;
591 read_lock(&old->lock);
592 fs->rootmnt = mntget(old->rootmnt);
593 fs->root = dget(old->root);
594 fs->pwdmnt = mntget(old->pwdmnt);
595 fs->pwd = dget(old->pwd);
597 fs->altrootmnt = mntget(old->altrootmnt);
598 fs->altroot = dget(old->altroot);
600 fs->altrootmnt = NULL;
603 read_unlock(&old->lock);
608 struct fs_struct *copy_fs_struct(struct fs_struct *old)
610 return __copy_fs_struct(old);
613 EXPORT_SYMBOL_GPL(copy_fs_struct);
615 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
617 if (clone_flags & CLONE_FS) {
618 atomic_inc(¤t->fs->count);
621 tsk->fs = __copy_fs_struct(current->fs);
627 static int count_open_files(struct files_struct *files, int size)
631 /* Find the last open fd */
632 for (i = size/(8*sizeof(long)); i > 0; ) {
633 if (files->open_fds->fds_bits[--i])
636 i = (i+1) * 8 * sizeof(long);
640 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
642 struct files_struct *oldf, *newf;
643 struct file **old_fds, **new_fds;
644 int open_files, nfds, size, i, error = 0;
647 * A background process may not have any files ...
649 oldf = current->files;
653 if (clone_flags & CLONE_FILES) {
654 atomic_inc(&oldf->count);
659 * Note: we may be using current for both targets (See exec.c)
660 * This works because we cache current->files (old) as oldf. Don't
665 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
669 atomic_set(&newf->count, 1);
671 newf->file_lock = SPIN_LOCK_UNLOCKED;
673 newf->max_fds = NR_OPEN_DEFAULT;
674 newf->max_fdset = __FD_SETSIZE;
675 newf->close_on_exec = &newf->close_on_exec_init;
676 newf->open_fds = &newf->open_fds_init;
677 newf->fd = &newf->fd_array[0];
679 /* We don't yet have the oldf readlock, but even if the old
680 fdset gets grown now, we'll only copy up to "size" fds */
681 size = oldf->max_fdset;
682 if (size > __FD_SETSIZE) {
684 spin_lock(&newf->file_lock);
685 error = expand_fdset(newf, size-1);
686 spin_unlock(&newf->file_lock);
690 spin_lock(&oldf->file_lock);
692 open_files = count_open_files(oldf, size);
695 * Check whether we need to allocate a larger fd array.
696 * Note: we're not a clone task, so the open count won't
699 nfds = NR_OPEN_DEFAULT;
700 if (open_files > nfds) {
701 spin_unlock(&oldf->file_lock);
703 spin_lock(&newf->file_lock);
704 error = expand_fd_array(newf, open_files-1);
705 spin_unlock(&newf->file_lock);
708 nfds = newf->max_fds;
709 spin_lock(&oldf->file_lock);
715 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
716 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
718 for (i = open_files; i != 0; i--) {
719 struct file *f = *old_fds++;
724 spin_unlock(&oldf->file_lock);
726 /* compute the remainder to be cleared */
727 size = (newf->max_fds - open_files) * sizeof(struct file *);
729 /* This is long word aligned thus could use a optimized version */
730 memset(new_fds, 0, size);
732 if (newf->max_fdset > open_files) {
733 int left = (newf->max_fdset-open_files)/8;
734 int start = open_files / (8 * sizeof(unsigned long));
736 memset(&newf->open_fds->fds_bits[start], 0, left);
737 memset(&newf->close_on_exec->fds_bits[start], 0, left);
746 free_fdset (newf->close_on_exec, newf->max_fdset);
747 free_fdset (newf->open_fds, newf->max_fdset);
748 kmem_cache_free(files_cachep, newf);
753 * Helper to unshare the files of the current task.
754 * We don't want to expose copy_files internals to
755 * the exec layer of the kernel.
758 int unshare_files(void)
760 struct files_struct *files = current->files;
766 /* This can race but the race causes us to copy when we don't
767 need to and drop the copy */
768 if(atomic_read(&files->count) == 1)
770 atomic_inc(&files->count);
773 rc = copy_files(0, current);
775 current->files = files;
779 EXPORT_SYMBOL(unshare_files);
781 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
783 struct sighand_struct *sig;
785 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
786 atomic_inc(¤t->sighand->count);
789 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
793 spin_lock_init(&sig->siglock);
794 atomic_set(&sig->count, 1);
795 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
799 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
801 struct signal_struct *sig;
803 if (clone_flags & CLONE_THREAD) {
804 atomic_inc(¤t->signal->count);
807 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
811 atomic_set(&sig->count, 1);
813 sig->group_exit_code = 0;
814 sig->group_exit_task = NULL;
815 sig->group_stop_count = 0;
816 sig->curr_target = NULL;
817 init_sigpending(&sig->shared_pending);
818 INIT_LIST_HEAD(&sig->posix_timers);
820 sig->tty = current->signal->tty;
821 sig->pgrp = process_group(current);
822 sig->session = current->signal->session;
823 sig->leader = 0; /* session leadership doesn't inherit */
824 sig->tty_old_pgrp = 0;
829 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
831 unsigned long new_flags = p->flags;
833 new_flags &= ~PF_SUPERPRIV;
834 new_flags |= PF_FORKNOEXEC;
835 if (!(clone_flags & CLONE_PTRACE))
837 p->flags = new_flags;
840 asmlinkage long sys_set_tid_address(int __user *tidptr)
842 current->clear_child_tid = tidptr;
848 * This creates a new process as a copy of the old one,
849 * but does not actually start it yet.
851 * It copies the registers, and all the appropriate
852 * parts of the process environment (as per the clone
853 * flags). The actual kick-off is left to the caller.
855 struct task_struct *copy_process(unsigned long clone_flags,
856 unsigned long stack_start,
857 struct pt_regs *regs,
858 unsigned long stack_size,
859 int __user *parent_tidptr,
860 int __user *child_tidptr)
863 struct task_struct *p = NULL;
865 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
866 return ERR_PTR(-EINVAL);
869 * Thread groups must share signals as well, and detached threads
870 * can only be started up within the thread group.
872 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
873 return ERR_PTR(-EINVAL);
876 * Shared signal handlers imply shared VM. By way of the above,
877 * thread groups also imply shared VM. Blocking this case allows
878 * for various simplifications in other code.
880 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
881 return ERR_PTR(-EINVAL);
883 retval = security_task_create(clone_flags);
888 p = dup_task_struct(current);
893 if (atomic_read(&p->user->processes) >=
894 p->rlim[RLIMIT_NPROC].rlim_cur) {
895 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
896 p->user != &root_user)
900 atomic_inc(&p->user->__count);
901 atomic_inc(&p->user->processes);
902 get_group_info(p->group_info);
905 * If multiple threads are within copy_process(), then this check
906 * triggers too late. This doesn't hurt, the check is only there
907 * to stop root fork bombs.
909 if (nr_threads >= max_threads)
910 goto bad_fork_cleanup_count;
912 if (!try_module_get(p->thread_info->exec_domain->module))
913 goto bad_fork_cleanup_count;
915 if (p->binfmt && !try_module_get(p->binfmt->module))
916 goto bad_fork_cleanup_put_domain;
919 copy_flags(clone_flags, p);
920 if (clone_flags & CLONE_IDLETASK)
923 p->pid = alloc_pidmap();
925 goto bad_fork_cleanup;
928 if (clone_flags & CLONE_PARENT_SETTID)
929 if (put_user(p->pid, parent_tidptr))
930 goto bad_fork_cleanup;
932 p->proc_dentry = NULL;
934 INIT_LIST_HEAD(&p->children);
935 INIT_LIST_HEAD(&p->sibling);
936 init_waitqueue_head(&p->wait_chldexit);
937 p->vfork_done = NULL;
938 spin_lock_init(&p->alloc_lock);
939 spin_lock_init(&p->proc_lock);
941 clear_tsk_thread_flag(p, TIF_SIGPENDING);
942 init_sigpending(&p->pending);
944 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
945 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
946 init_timer(&p->real_timer);
947 p->real_timer.data = (unsigned long) p;
949 p->utime = p->stime = 0;
950 p->cutime = p->cstime = 0;
951 p->lock_depth = -1; /* -1 = no lock */
952 p->start_time = get_jiffies_64();
954 p->io_context = NULL;
955 p->audit_context = NULL;
958 if ((retval = security_task_alloc(p)))
959 goto bad_fork_cleanup;
960 if ((retval = audit_alloc(p)))
961 goto bad_fork_cleanup_security;
962 /* copy all the process information */
963 if ((retval = copy_semundo(clone_flags, p)))
964 goto bad_fork_cleanup_audit;
965 if ((retval = copy_files(clone_flags, p)))
966 goto bad_fork_cleanup_semundo;
967 if ((retval = copy_fs(clone_flags, p)))
968 goto bad_fork_cleanup_files;
969 if ((retval = copy_sighand(clone_flags, p)))
970 goto bad_fork_cleanup_fs;
971 if ((retval = copy_signal(clone_flags, p)))
972 goto bad_fork_cleanup_sighand;
973 if ((retval = copy_mm(clone_flags, p)))
974 goto bad_fork_cleanup_signal;
975 if ((retval = copy_namespace(clone_flags, p)))
976 goto bad_fork_cleanup_mm;
977 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
979 goto bad_fork_cleanup_namespace;
981 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
983 * Clear TID on mm_release()?
985 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
988 * Syscall tracing should be turned off in the child regardless
991 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
993 /* Our parent execution domain becomes current domain
994 These must match for thread signalling to apply */
996 p->parent_exec_id = p->self_exec_id;
998 /* ok, now we should be set up.. */
999 p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
1000 p->pdeath_signal = 0;
1002 /* Perform scheduler related setup */
1006 * Ok, make it visible to the rest of the system.
1007 * We dont wake it up yet.
1010 p->group_leader = p;
1011 INIT_LIST_HEAD(&p->ptrace_children);
1012 INIT_LIST_HEAD(&p->ptrace_list);
1014 /* Need tasklist lock for parent etc handling! */
1015 write_lock_irq(&tasklist_lock);
1017 * Check for pending SIGKILL! The new thread should not be allowed
1018 * to slip out of an OOM kill. (or normal SIGKILL.)
1020 if (sigismember(¤t->pending.signal, SIGKILL)) {
1021 write_unlock_irq(&tasklist_lock);
1023 goto bad_fork_cleanup_namespace;
1026 /* CLONE_PARENT re-uses the old parent */
1027 if (clone_flags & CLONE_PARENT)
1028 p->real_parent = current->real_parent;
1030 p->real_parent = current;
1031 p->parent = p->real_parent;
1033 if (clone_flags & CLONE_THREAD) {
1034 spin_lock(¤t->sighand->siglock);
1036 * Important: if an exit-all has been started then
1037 * do not create this new thread - the whole thread
1038 * group is supposed to exit anyway.
1040 if (current->signal->group_exit) {
1041 spin_unlock(¤t->sighand->siglock);
1042 write_unlock_irq(&tasklist_lock);
1044 goto bad_fork_cleanup_namespace;
1046 p->tgid = current->tgid;
1047 p->group_leader = current->group_leader;
1049 if (current->signal->group_stop_count > 0) {
1051 * There is an all-stop in progress for the group.
1052 * We ourselves will stop as soon as we check signals.
1053 * Make the new thread part of that group stop too.
1055 current->signal->group_stop_count++;
1056 set_tsk_thread_flag(p, TIF_SIGPENDING);
1059 spin_unlock(¤t->sighand->siglock);
1063 if (p->ptrace & PT_PTRACED)
1064 __ptrace_link(p, current->parent);
1066 attach_pid(p, PIDTYPE_PID, p->pid);
1067 if (thread_group_leader(p)) {
1068 attach_pid(p, PIDTYPE_TGID, p->tgid);
1069 attach_pid(p, PIDTYPE_PGID, process_group(p));
1070 attach_pid(p, PIDTYPE_SID, p->signal->session);
1072 __get_cpu_var(process_counts)++;
1074 link_pid(p, p->pids + PIDTYPE_TGID, &p->group_leader->pids[PIDTYPE_TGID].pid);
1077 write_unlock_irq(&tasklist_lock);
1082 return ERR_PTR(retval);
1085 bad_fork_cleanup_namespace:
1087 bad_fork_cleanup_mm:
1090 mmdrop(p->active_mm);
1091 bad_fork_cleanup_signal:
1093 bad_fork_cleanup_sighand:
1095 bad_fork_cleanup_fs:
1096 exit_fs(p); /* blocking */
1097 bad_fork_cleanup_files:
1098 exit_files(p); /* blocking */
1099 bad_fork_cleanup_semundo:
1101 bad_fork_cleanup_audit:
1103 bad_fork_cleanup_security:
1104 security_task_free(p);
1107 free_pidmap(p->pid);
1109 module_put(p->binfmt->module);
1110 bad_fork_cleanup_put_domain:
1111 module_put(p->thread_info->exec_domain->module);
1112 bad_fork_cleanup_count:
1113 put_group_info(p->group_info);
1114 atomic_dec(&p->user->processes);
1121 static inline int fork_traceflag (unsigned clone_flags)
1123 if (clone_flags & (CLONE_UNTRACED | CLONE_IDLETASK))
1125 else if (clone_flags & CLONE_VFORK) {
1126 if (current->ptrace & PT_TRACE_VFORK)
1127 return PTRACE_EVENT_VFORK;
1128 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1129 if (current->ptrace & PT_TRACE_CLONE)
1130 return PTRACE_EVENT_CLONE;
1131 } else if (current->ptrace & PT_TRACE_FORK)
1132 return PTRACE_EVENT_FORK;
1138 * Ok, this is the main fork-routine.
1140 * It copies the process, and if successful kick-starts
1141 * it and waits for it to finish using the VM if required.
1143 long do_fork(unsigned long clone_flags,
1144 unsigned long stack_start,
1145 struct pt_regs *regs,
1146 unsigned long stack_size,
1147 int __user *parent_tidptr,
1148 int __user *child_tidptr)
1150 struct task_struct *p;
1154 if (unlikely(current->ptrace)) {
1155 trace = fork_traceflag (clone_flags);
1157 clone_flags |= CLONE_PTRACE;
1160 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr);
1162 * Do this prior waking up the new thread - the thread pointer
1163 * might get invalid after that point, if the thread exits quickly.
1165 pid = IS_ERR(p) ? PTR_ERR(p) : p->pid;
1168 struct completion vfork;
1170 if (clone_flags & CLONE_VFORK) {
1171 p->vfork_done = &vfork;
1172 init_completion(&vfork);
1175 if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
1177 * We'll start up with an immediate SIGSTOP.
1179 sigaddset(&p->pending.signal, SIGSTOP);
1180 set_tsk_thread_flag(p, TIF_SIGPENDING);
1183 if (!(clone_flags & CLONE_STOPPED))
1184 wake_up_forked_process(p); /* do this last */
1186 p->state = TASK_STOPPED;
1189 if (unlikely (trace)) {
1190 current->ptrace_message = pid;
1191 ptrace_notify ((trace << 8) | SIGTRAP);
1194 if (clone_flags & CLONE_VFORK) {
1195 wait_for_completion(&vfork);
1196 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1197 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1200 * Let the child process run first, to avoid most of the
1201 * COW overhead when the child exec()s afterwards.
1208 /* SLAB cache for signal_struct structures (tsk->signal) */
1209 kmem_cache_t *signal_cachep;
1211 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1212 kmem_cache_t *sighand_cachep;
1214 /* SLAB cache for files_struct structures (tsk->files) */
1215 kmem_cache_t *files_cachep;
1217 /* SLAB cache for fs_struct structures (tsk->fs) */
1218 kmem_cache_t *fs_cachep;
1220 /* SLAB cache for vm_area_struct structures */
1221 kmem_cache_t *vm_area_cachep;
1223 /* SLAB cache for mm_struct structures (tsk->mm) */
1224 kmem_cache_t *mm_cachep;
1226 void __init proc_caches_init(void)
1228 sighand_cachep = kmem_cache_create("sighand_cache",
1229 sizeof(struct sighand_struct), 0,
1230 SLAB_HWCACHE_ALIGN, NULL, NULL);
1231 if (!sighand_cachep)
1232 panic("Cannot create sighand SLAB cache");
1234 signal_cachep = kmem_cache_create("signal_cache",
1235 sizeof(struct signal_struct), 0,
1236 SLAB_HWCACHE_ALIGN, NULL, NULL);
1238 panic("Cannot create signal SLAB cache");
1240 files_cachep = kmem_cache_create("files_cache",
1241 sizeof(struct files_struct), 0,
1242 SLAB_HWCACHE_ALIGN, NULL, NULL);
1244 panic("Cannot create files SLAB cache");
1246 fs_cachep = kmem_cache_create("fs_cache",
1247 sizeof(struct fs_struct), 0,
1248 SLAB_HWCACHE_ALIGN, NULL, NULL);
1250 panic("Cannot create fs_struct SLAB cache");
1252 vm_area_cachep = kmem_cache_create("vm_area_struct",
1253 sizeof(struct vm_area_struct), 0,
1256 panic("vma_init: Cannot alloc vm_area_struct SLAB cache");
1258 mm_cachep = kmem_cache_create("mm_struct",
1259 sizeof(struct mm_struct), 0,
1260 SLAB_HWCACHE_ALIGN, NULL, NULL);
1262 panic("vma_init: Cannot alloc mm_struct SLAB cache");