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>
42 #include <linux/ckrm_events.h>
43 #include <linux/ckrm_tsk.h>
44 #include <linux/ckrm_tc.h>
45 #include <linux/ckrm_mem_inline.h>
47 #include <asm/pgtable.h>
48 #include <asm/pgalloc.h>
49 #include <asm/uaccess.h>
50 #include <asm/mmu_context.h>
51 #include <asm/cacheflush.h>
52 #include <asm/tlbflush.h>
54 /* The idle threads do not count..
55 * Protected by write_lock_irq(&tasklist_lock)
60 unsigned long total_forks; /* Handle normal Linux uptimes. */
62 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
64 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
66 EXPORT_SYMBOL(tasklist_lock);
68 int nr_processes(void)
73 for_each_online_cpu(cpu)
74 total += per_cpu(process_counts, cpu);
79 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
80 # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
81 # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
82 static kmem_cache_t *task_struct_cachep;
85 void free_task(struct task_struct *tsk)
87 free_thread_info(tsk->thread_info);
88 free_task_struct(tsk);
90 EXPORT_SYMBOL(free_task);
92 void __put_task_struct(struct task_struct *tsk)
94 WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_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 __init fork_init(unsigned long mempages)
110 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
111 #ifndef ARCH_MIN_TASKALIGN
112 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
114 /* create a slab on which task_structs can be allocated */
116 kmem_cache_create("task_struct", sizeof(struct task_struct),
117 ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
121 * The default maximum number of threads is set to a safe
122 * value: the thread structures can take up at most half
125 max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
128 * we need to allow at least 20 threads to boot a system
133 init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
134 init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
137 static struct task_struct *dup_task_struct(struct task_struct *orig)
139 struct task_struct *tsk;
140 struct thread_info *ti;
142 prepare_to_copy(orig);
144 tsk = alloc_task_struct();
148 ti = alloc_thread_info(tsk);
150 free_task_struct(tsk);
154 *ti = *orig->thread_info;
156 tsk->thread_info = ti;
159 ckrm_cb_newtask(tsk);
160 ckrm_task_mm_init(tsk);
161 /* One for us, one for whoever does the "release_task()" (usually parent) */
162 atomic_set(&tsk->usage,2);
167 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
169 struct vm_area_struct * mpnt, *tmp, **pprev;
170 struct rb_node **rb_link, *rb_parent;
172 unsigned long charge;
173 struct mempolicy *pol;
175 down_write(&oldmm->mmap_sem);
176 flush_cache_mm(current->mm);
179 mm->mmap_cache = NULL;
180 mm->free_area_cache = oldmm->mmap_base;
184 cpus_clear(mm->cpu_vm_mask);
186 rb_link = &mm->mm_rb.rb_node;
190 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
193 if (mpnt->vm_flags & VM_DONTCOPY) {
194 __vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
199 if (mpnt->vm_flags & VM_ACCOUNT) {
200 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
201 if (security_vm_enough_memory(len))
205 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
209 pol = mpol_copy(vma_policy(mpnt));
210 retval = PTR_ERR(pol);
212 goto fail_nomem_policy;
213 vma_set_policy(tmp, pol);
214 tmp->vm_flags &= ~VM_LOCKED;
220 struct inode *inode = file->f_dentry->d_inode;
222 if (tmp->vm_flags & VM_DENYWRITE)
223 atomic_dec(&inode->i_writecount);
225 /* insert tmp into the share list, just after mpnt */
226 spin_lock(&file->f_mapping->i_mmap_lock);
227 flush_dcache_mmap_lock(file->f_mapping);
228 vma_prio_tree_add(tmp, mpnt);
229 flush_dcache_mmap_unlock(file->f_mapping);
230 spin_unlock(&file->f_mapping->i_mmap_lock);
234 * Link in the new vma and copy the page table entries:
235 * link in first so that swapoff can see swap entries,
236 * and try_to_unmap_one's find_vma find the new vma.
238 spin_lock(&mm->page_table_lock);
240 pprev = &tmp->vm_next;
242 __vma_link_rb(mm, tmp, rb_link, rb_parent);
243 rb_link = &tmp->vm_rb.rb_right;
244 rb_parent = &tmp->vm_rb;
247 retval = copy_page_range(mm, current->mm, tmp);
248 spin_unlock(&mm->page_table_lock);
250 if (tmp->vm_ops && tmp->vm_ops->open)
251 tmp->vm_ops->open(tmp);
259 flush_tlb_mm(current->mm);
260 up_write(&oldmm->mmap_sem);
263 kmem_cache_free(vm_area_cachep, tmp);
266 vm_unacct_memory(charge);
270 static inline int mm_alloc_pgd(struct mm_struct * mm)
272 mm->pgd = pgd_alloc(mm);
273 if (unlikely(!mm->pgd))
278 static inline void mm_free_pgd(struct mm_struct * mm)
283 #define dup_mmap(mm, oldmm) (0)
284 #define mm_alloc_pgd(mm) (0)
285 #define mm_free_pgd(mm)
286 #endif /* CONFIG_MMU */
288 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
290 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
291 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
293 #include <linux/init_task.h>
295 static struct mm_struct * mm_init(struct mm_struct * mm)
297 atomic_set(&mm->mm_users, 1);
298 atomic_set(&mm->mm_count, 1);
299 init_rwsem(&mm->mmap_sem);
300 INIT_LIST_HEAD(&mm->mmlist);
301 mm->core_waiters = 0;
303 spin_lock_init(&mm->page_table_lock);
304 rwlock_init(&mm->ioctx_list_lock);
305 mm->ioctx_list = NULL;
306 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
307 mm->free_area_cache = TASK_UNMAPPED_BASE;
310 if (likely(!mm_alloc_pgd(mm))) {
319 * Allocate and initialize an mm_struct.
321 struct mm_struct * mm_alloc(void)
323 struct mm_struct * mm;
327 memset(mm, 0, sizeof(*mm));
329 ckrm_mm_setclass(mm, ckrm_get_mem_class(current));
335 * Called when the last reference to the mm
336 * is dropped: either by a lazy thread or by
337 * mmput. Free the page directory and the mm.
339 void fastcall __mmdrop(struct mm_struct *mm)
341 BUG_ON(mm == &init_mm);
348 * Decrement the use count and release all resources for an mm.
350 void mmput(struct mm_struct *mm)
352 if (atomic_dec_and_test(&mm->mm_users)) {
355 if (!list_empty(&mm->mmlist)) {
356 spin_lock(&mmlist_lock);
357 list_del(&mm->mmlist);
358 spin_unlock(&mmlist_lock);
364 EXPORT_SYMBOL_GPL(mmput);
367 * get_task_mm - acquire a reference to the task's mm
369 * Returns %NULL if the task has no mm. Checks PF_BORROWED_MM (meaning
370 * this kernel workthread has transiently adopted a user mm with use_mm,
371 * to do its AIO) is not set and if so returns a reference to it, after
372 * bumping up the use count. User must release the mm via mmput()
373 * after use. Typically used by /proc and ptrace.
375 struct mm_struct *get_task_mm(struct task_struct *task)
377 struct mm_struct *mm;
382 if (task->flags & PF_BORROWED_MM)
385 atomic_inc(&mm->mm_users);
390 EXPORT_SYMBOL_GPL(get_task_mm);
392 /* Please note the differences between mmput and mm_release.
393 * mmput is called whenever we stop holding onto a mm_struct,
394 * error success whatever.
396 * mm_release is called after a mm_struct has been removed
397 * from the current process.
399 * This difference is important for error handling, when we
400 * only half set up a mm_struct for a new process and need to restore
401 * the old one. Because we mmput the new mm_struct before
402 * restoring the old one. . .
403 * Eric Biederman 10 January 1998
405 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
407 struct completion *vfork_done = tsk->vfork_done;
409 /* Get rid of any cached register state */
410 deactivate_mm(tsk, mm);
412 /* notify parent sleeping on vfork() */
414 tsk->vfork_done = NULL;
415 complete(vfork_done);
417 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
418 u32 __user * tidptr = tsk->clear_child_tid;
419 tsk->clear_child_tid = NULL;
422 * We don't check the error code - if userspace has
423 * not set up a proper pointer then tough luck.
426 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
430 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
432 struct mm_struct * mm, *oldmm;
435 tsk->min_flt = tsk->maj_flt = 0;
436 tsk->nvcsw = tsk->nivcsw = 0;
439 tsk->active_mm = NULL;
442 * Are we cloning a kernel thread?
444 * We need to steal a active VM for that..
450 if (clone_flags & CLONE_VM) {
451 atomic_inc(&oldmm->mm_users);
454 * There are cases where the PTL is held to ensure no
455 * new threads start up in user mode using an mm, which
456 * allows optimizing out ipis; the tlb_gather_mmu code
459 spin_unlock_wait(&oldmm->page_table_lock);
468 /* Copy the current MM stuff.. */
469 memcpy(mm, oldmm, sizeof(*mm));
473 if (init_new_context(tsk,mm))
476 retval = dup_mmap(mm, oldmm);
483 ckrm_mm_setclass(mm, oldmm->memclass);
484 ckrm_task_mm_set(mm, tsk);
494 * If init_new_context() failed, we cannot use mmput() to free the mm
495 * because it calls destroy_context()
502 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
504 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
505 /* We don't need to lock fs - think why ;-) */
507 atomic_set(&fs->count, 1);
508 rwlock_init(&fs->lock);
509 fs->umask = old->umask;
510 read_lock(&old->lock);
511 fs->rootmnt = mntget(old->rootmnt);
512 fs->root = dget(old->root);
513 fs->pwdmnt = mntget(old->pwdmnt);
514 fs->pwd = dget(old->pwd);
516 fs->altrootmnt = mntget(old->altrootmnt);
517 fs->altroot = dget(old->altroot);
519 fs->altrootmnt = NULL;
522 read_unlock(&old->lock);
527 struct fs_struct *copy_fs_struct(struct fs_struct *old)
529 return __copy_fs_struct(old);
532 EXPORT_SYMBOL_GPL(copy_fs_struct);
534 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
536 if (clone_flags & CLONE_FS) {
537 atomic_inc(¤t->fs->count);
540 tsk->fs = __copy_fs_struct(current->fs);
546 static int count_open_files(struct files_struct *files, int size)
550 /* Find the last open fd */
551 for (i = size/(8*sizeof(long)); i > 0; ) {
552 if (files->open_fds->fds_bits[--i])
555 i = (i+1) * 8 * sizeof(long);
559 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
561 struct files_struct *oldf, *newf;
562 struct file **old_fds, **new_fds;
563 int open_files, nfds, size, i, error = 0;
566 * A background process may not have any files ...
568 oldf = current->files;
572 if (clone_flags & CLONE_FILES) {
573 atomic_inc(&oldf->count);
578 * Note: we may be using current for both targets (See exec.c)
579 * This works because we cache current->files (old) as oldf. Don't
584 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
588 atomic_set(&newf->count, 1);
590 spin_lock_init(&newf->file_lock);
592 newf->max_fds = NR_OPEN_DEFAULT;
593 newf->max_fdset = __FD_SETSIZE;
594 newf->close_on_exec = &newf->close_on_exec_init;
595 newf->open_fds = &newf->open_fds_init;
596 newf->fd = &newf->fd_array[0];
598 /* We don't yet have the oldf readlock, but even if the old
599 fdset gets grown now, we'll only copy up to "size" fds */
600 size = oldf->max_fdset;
601 if (size > __FD_SETSIZE) {
603 spin_lock(&newf->file_lock);
604 error = expand_fdset(newf, size-1);
605 spin_unlock(&newf->file_lock);
609 spin_lock(&oldf->file_lock);
611 open_files = count_open_files(oldf, size);
614 * Check whether we need to allocate a larger fd array.
615 * Note: we're not a clone task, so the open count won't
618 nfds = NR_OPEN_DEFAULT;
619 if (open_files > nfds) {
620 spin_unlock(&oldf->file_lock);
622 spin_lock(&newf->file_lock);
623 error = expand_fd_array(newf, open_files-1);
624 spin_unlock(&newf->file_lock);
627 nfds = newf->max_fds;
628 spin_lock(&oldf->file_lock);
634 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
635 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
637 for (i = open_files; i != 0; i--) {
638 struct file *f = *old_fds++;
643 * The fd may be claimed in the fd bitmap but not yet
644 * instantiated in the files array if a sibling thread
645 * is partway through open(). So make sure that this
646 * fd is available to the new process.
648 FD_CLR(open_files - i, newf->open_fds);
652 spin_unlock(&oldf->file_lock);
654 /* compute the remainder to be cleared */
655 size = (newf->max_fds - open_files) * sizeof(struct file *);
657 /* This is long word aligned thus could use a optimized version */
658 memset(new_fds, 0, size);
660 if (newf->max_fdset > open_files) {
661 int left = (newf->max_fdset-open_files)/8;
662 int start = open_files / (8 * sizeof(unsigned long));
664 memset(&newf->open_fds->fds_bits[start], 0, left);
665 memset(&newf->close_on_exec->fds_bits[start], 0, left);
674 free_fdset (newf->close_on_exec, newf->max_fdset);
675 free_fdset (newf->open_fds, newf->max_fdset);
676 kmem_cache_free(files_cachep, newf);
681 * Helper to unshare the files of the current task.
682 * We don't want to expose copy_files internals to
683 * the exec layer of the kernel.
686 int unshare_files(void)
688 struct files_struct *files = current->files;
694 /* This can race but the race causes us to copy when we don't
695 need to and drop the copy */
696 if(atomic_read(&files->count) == 1)
698 atomic_inc(&files->count);
701 rc = copy_files(0, current);
703 current->files = files;
707 EXPORT_SYMBOL(unshare_files);
709 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
711 struct sighand_struct *sig;
713 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
714 atomic_inc(¤t->sighand->count);
717 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
721 spin_lock_init(&sig->siglock);
722 atomic_set(&sig->count, 1);
723 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
727 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
729 struct signal_struct *sig;
731 if (clone_flags & CLONE_THREAD) {
732 atomic_inc(¤t->signal->count);
733 atomic_inc(¤t->signal->live);
736 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
740 atomic_set(&sig->count, 1);
741 atomic_set(&sig->live, 1);
743 sig->group_exit_code = 0;
744 sig->group_exit_task = NULL;
745 sig->group_stop_count = 0;
747 sig->curr_target = NULL;
748 init_sigpending(&sig->shared_pending);
749 INIT_LIST_HEAD(&sig->posix_timers);
751 sig->tty = current->signal->tty;
752 sig->pgrp = process_group(current);
753 sig->session = current->signal->session;
754 sig->leader = 0; /* session leadership doesn't inherit */
755 sig->tty_old_pgrp = 0;
757 sig->utime = sig->stime = sig->cutime = sig->cstime = 0;
758 sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
759 sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
761 task_lock(current->group_leader);
762 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
763 task_unlock(current->group_leader);
768 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
770 unsigned long new_flags = p->flags;
772 new_flags &= ~PF_SUPERPRIV;
773 new_flags |= PF_FORKNOEXEC;
774 if (!(clone_flags & CLONE_PTRACE))
776 p->flags = new_flags;
779 asmlinkage long sys_set_tid_address(int __user *tidptr)
781 current->clear_child_tid = tidptr;
787 * This creates a new process as a copy of the old one,
788 * but does not actually start it yet.
790 * It copies the registers, and all the appropriate
791 * parts of the process environment (as per the clone
792 * flags). The actual kick-off is left to the caller.
794 static task_t *copy_process(unsigned long clone_flags,
795 unsigned long stack_start,
796 struct pt_regs *regs,
797 unsigned long stack_size,
798 int __user *parent_tidptr,
799 int __user *child_tidptr,
803 struct task_struct *p = NULL;
805 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
806 return ERR_PTR(-EINVAL);
809 * Thread groups must share signals as well, and detached threads
810 * can only be started up within the thread group.
812 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
813 return ERR_PTR(-EINVAL);
816 * Shared signal handlers imply shared VM. By way of the above,
817 * thread groups also imply shared VM. Blocking this case allows
818 * for various simplifications in other code.
820 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
821 return ERR_PTR(-EINVAL);
823 retval = security_task_create(clone_flags);
828 p = dup_task_struct(current);
833 if (atomic_read(&p->user->processes) >=
834 p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
835 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
836 p->user != &root_user)
840 atomic_inc(&p->user->__count);
841 atomic_inc(&p->user->processes);
842 get_group_info(p->group_info);
845 * If multiple threads are within copy_process(), then this check
846 * triggers too late. This doesn't hurt, the check is only there
847 * to stop root fork bombs.
849 if (nr_threads >= max_threads)
850 goto bad_fork_cleanup_count;
852 if (!try_module_get(p->thread_info->exec_domain->module))
853 goto bad_fork_cleanup_count;
855 if (p->binfmt && !try_module_get(p->binfmt->module))
856 goto bad_fork_cleanup_put_domain;
860 copy_flags(clone_flags, p);
863 if (clone_flags & CLONE_PARENT_SETTID)
864 if (put_user(p->pid, parent_tidptr))
865 goto bad_fork_cleanup;
867 p->proc_dentry = NULL;
869 INIT_LIST_HEAD(&p->children);
870 INIT_LIST_HEAD(&p->sibling);
871 init_waitqueue_head(&p->wait_chldexit);
872 p->vfork_done = NULL;
873 spin_lock_init(&p->alloc_lock);
874 spin_lock_init(&p->proc_lock);
876 clear_tsk_thread_flag(p, TIF_SIGPENDING);
877 init_sigpending(&p->pending);
879 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
880 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
881 init_timer(&p->real_timer);
882 p->real_timer.data = (unsigned long) p;
884 p->utime = p->stime = 0;
885 p->lock_depth = -1; /* -1 = no lock */
886 do_posix_clock_monotonic_gettime(&p->start_time);
888 p->io_context = NULL;
890 p->audit_context = NULL;
892 p->mempolicy = mpol_copy(p->mempolicy);
893 if (IS_ERR(p->mempolicy)) {
894 retval = PTR_ERR(p->mempolicy);
896 goto bad_fork_cleanup;
901 if (clone_flags & CLONE_THREAD)
902 p->tgid = current->tgid;
904 if ((retval = security_task_alloc(p)))
905 goto bad_fork_cleanup_policy;
906 if ((retval = audit_alloc(p)))
907 goto bad_fork_cleanup_security;
908 /* copy all the process information */
909 if ((retval = copy_semundo(clone_flags, p)))
910 goto bad_fork_cleanup_audit;
911 if ((retval = copy_files(clone_flags, p)))
912 goto bad_fork_cleanup_semundo;
913 if ((retval = copy_fs(clone_flags, p)))
914 goto bad_fork_cleanup_files;
915 if ((retval = copy_sighand(clone_flags, p)))
916 goto bad_fork_cleanup_fs;
917 if ((retval = copy_signal(clone_flags, p)))
918 goto bad_fork_cleanup_sighand;
919 if ((retval = copy_mm(clone_flags, p)))
920 goto bad_fork_cleanup_signal;
921 if ((retval = copy_keys(clone_flags, p)))
922 goto bad_fork_cleanup_mm;
923 if ((retval = copy_namespace(clone_flags, p)))
924 goto bad_fork_cleanup_keys;
925 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
927 goto bad_fork_cleanup_namespace;
929 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
931 * Clear TID on mm_release()?
933 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
936 * Syscall tracing should be turned off in the child regardless
939 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
941 /* Our parent execution domain becomes current domain
942 These must match for thread signalling to apply */
944 p->parent_exec_id = p->self_exec_id;
946 /* ok, now we should be set up.. */
947 p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
948 p->pdeath_signal = 0;
951 /* Perform scheduler related setup */
955 * Ok, make it visible to the rest of the system.
956 * We dont wake it up yet.
959 INIT_LIST_HEAD(&p->ptrace_children);
960 INIT_LIST_HEAD(&p->ptrace_list);
962 /* Need tasklist lock for parent etc handling! */
963 write_lock_irq(&tasklist_lock);
966 * The task hasn't been attached yet, so cpus_allowed mask cannot
967 * have changed. The cpus_allowed mask of the parent may have
968 * changed after it was copied first time, and it may then move to
969 * another CPU - so we re-copy it here and set the child's CPU to
970 * the parent's CPU. This avoids alot of nasty races.
972 p->cpus_allowed = current->cpus_allowed;
973 set_task_cpu(p, smp_processor_id());
976 * Check for pending SIGKILL! The new thread should not be allowed
977 * to slip out of an OOM kill. (or normal SIGKILL.)
979 if (sigismember(¤t->pending.signal, SIGKILL)) {
980 write_unlock_irq(&tasklist_lock);
982 goto bad_fork_cleanup_namespace;
985 /* CLONE_PARENT re-uses the old parent */
986 if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
987 p->real_parent = current->real_parent;
989 p->real_parent = current;
990 p->parent = p->real_parent;
992 if (clone_flags & CLONE_THREAD) {
993 spin_lock(¤t->sighand->siglock);
995 * Important: if an exit-all has been started then
996 * do not create this new thread - the whole thread
997 * group is supposed to exit anyway.
999 if (current->signal->group_exit) {
1000 spin_unlock(¤t->sighand->siglock);
1001 write_unlock_irq(&tasklist_lock);
1003 goto bad_fork_cleanup_namespace;
1005 p->group_leader = current->group_leader;
1007 if (current->signal->group_stop_count > 0) {
1009 * There is an all-stop in progress for the group.
1010 * We ourselves will stop as soon as we check signals.
1011 * Make the new thread part of that group stop too.
1013 current->signal->group_stop_count++;
1014 set_tsk_thread_flag(p, TIF_SIGPENDING);
1017 spin_unlock(¤t->sighand->siglock);
1021 if (unlikely(p->ptrace & PT_PTRACED))
1022 __ptrace_link(p, current->parent);
1024 attach_pid(p, PIDTYPE_PID, p->pid);
1025 attach_pid(p, PIDTYPE_TGID, p->tgid);
1026 if (thread_group_leader(p)) {
1027 attach_pid(p, PIDTYPE_PGID, process_group(p));
1028 attach_pid(p, PIDTYPE_SID, p->signal->session);
1030 __get_cpu_var(process_counts)++;
1034 write_unlock_irq(&tasklist_lock);
1039 return ERR_PTR(retval);
1042 bad_fork_cleanup_namespace:
1044 bad_fork_cleanup_keys:
1046 bad_fork_cleanup_mm:
1049 bad_fork_cleanup_signal:
1051 bad_fork_cleanup_sighand:
1053 bad_fork_cleanup_fs:
1054 exit_fs(p); /* blocking */
1055 bad_fork_cleanup_files:
1056 exit_files(p); /* blocking */
1057 bad_fork_cleanup_semundo:
1059 bad_fork_cleanup_audit:
1061 bad_fork_cleanup_security:
1062 security_task_free(p);
1063 bad_fork_cleanup_policy:
1065 mpol_free(p->mempolicy);
1069 module_put(p->binfmt->module);
1070 bad_fork_cleanup_put_domain:
1071 module_put(p->thread_info->exec_domain->module);
1072 bad_fork_cleanup_count:
1073 put_group_info(p->group_info);
1074 atomic_dec(&p->user->processes);
1081 struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
1083 memset(regs, 0, sizeof(struct pt_regs));
1087 task_t * __devinit fork_idle(int cpu)
1090 struct pt_regs regs;
1092 task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, NULL, 0);
1094 return ERR_PTR(-ENOMEM);
1095 init_idle(task, cpu);
1096 unhash_process(task);
1100 static inline int fork_traceflag (unsigned clone_flags)
1102 if (clone_flags & CLONE_UNTRACED)
1104 else if (clone_flags & CLONE_VFORK) {
1105 if (current->ptrace & PT_TRACE_VFORK)
1106 return PTRACE_EVENT_VFORK;
1107 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1108 if (current->ptrace & PT_TRACE_CLONE)
1109 return PTRACE_EVENT_CLONE;
1110 } else if (current->ptrace & PT_TRACE_FORK)
1111 return PTRACE_EVENT_FORK;
1117 * Ok, this is the main fork-routine.
1119 * It copies the process, and if successful kick-starts
1120 * it and waits for it to finish using the VM if required.
1122 long do_fork(unsigned long clone_flags,
1123 unsigned long stack_start,
1124 struct pt_regs *regs,
1125 unsigned long stack_size,
1126 int __user *parent_tidptr,
1127 int __user *child_tidptr)
1129 struct task_struct *p;
1131 long pid = alloc_pidmap();
1135 if (unlikely(current->ptrace)) {
1136 trace = fork_traceflag (clone_flags);
1138 clone_flags |= CLONE_PTRACE;
1141 if (numtasks_get_ref(current->taskclass, 0) == 0) {
1144 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
1146 * Do this prior waking up the new thread - the thread pointer
1147 * might get invalid after that point, if the thread exits quickly.
1150 struct completion vfork;
1154 if (clone_flags & CLONE_VFORK) {
1155 p->vfork_done = &vfork;
1156 init_completion(&vfork);
1159 if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
1161 * We'll start up with an immediate SIGSTOP.
1163 sigaddset(&p->pending.signal, SIGSTOP);
1164 set_tsk_thread_flag(p, TIF_SIGPENDING);
1167 if (!(clone_flags & CLONE_STOPPED))
1168 wake_up_new_task(p, clone_flags);
1170 p->state = TASK_STOPPED;
1173 if (unlikely (trace)) {
1174 current->ptrace_message = pid;
1175 ptrace_notify ((trace << 8) | SIGTRAP);
1178 if (clone_flags & CLONE_VFORK) {
1179 wait_for_completion(&vfork);
1180 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1181 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1184 numtasks_put_ref(current->taskclass);
1191 /* SLAB cache for signal_struct structures (tsk->signal) */
1192 kmem_cache_t *signal_cachep;
1194 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1195 kmem_cache_t *sighand_cachep;
1197 /* SLAB cache for files_struct structures (tsk->files) */
1198 kmem_cache_t *files_cachep;
1200 /* SLAB cache for fs_struct structures (tsk->fs) */
1201 kmem_cache_t *fs_cachep;
1203 /* SLAB cache for vm_area_struct structures */
1204 kmem_cache_t *vm_area_cachep;
1206 /* SLAB cache for mm_struct structures (tsk->mm) */
1207 kmem_cache_t *mm_cachep;
1209 void __init proc_caches_init(void)
1211 sighand_cachep = kmem_cache_create("sighand_cache",
1212 sizeof(struct sighand_struct), 0,
1213 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1214 signal_cachep = kmem_cache_create("signal_cache",
1215 sizeof(struct signal_struct), 0,
1216 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1217 files_cachep = kmem_cache_create("files_cache",
1218 sizeof(struct files_struct), 0,
1219 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1220 fs_cachep = kmem_cache_create("fs_cache",
1221 sizeof(struct fs_struct), 0,
1222 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1223 vm_area_cachep = kmem_cache_create("vm_area_struct",
1224 sizeof(struct vm_area_struct), 0,
1225 SLAB_PANIC, NULL, NULL);
1226 mm_cachep = kmem_cache_create("mm_struct",
1227 sizeof(struct mm_struct), 0,
1228 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);