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/file.h>
25 #include <linux/binfmts.h>
26 #include <linux/mman.h>
28 #include <linux/security.h>
29 #include <linux/jiffies.h>
30 #include <linux/futex.h>
31 #include <linux/ptrace.h>
32 #include <linux/mount.h>
34 #include <asm/pgtable.h>
35 #include <asm/pgalloc.h>
36 #include <asm/uaccess.h>
37 #include <asm/mmu_context.h>
38 #include <asm/cacheflush.h>
39 #include <asm/tlbflush.h>
41 extern int copy_semundo(unsigned long clone_flags, struct task_struct *tsk);
42 extern void exit_semundo(struct task_struct *tsk);
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 int nr_processes(void)
61 for (cpu = 0; cpu < NR_CPUS; cpu++) {
63 total += per_cpu(process_counts, cpu);
69 # define HAVE_ARCH_DUP_TASK_STRUCT
72 #ifdef HAVE_ARCH_DUP_TASK_STRUCT
73 extern void free_task_struct (struct task_struct *tsk);
75 static kmem_cache_t *task_struct_cachep;
78 * A per-CPU task cache - this relies on the fact that
79 * the very last portion of sys_exit() is executed with
80 * preemption turned off.
82 static task_t *task_cache[NR_CPUS] __cacheline_aligned;
84 static void free_task_struct(struct task_struct *tsk)
87 * The task cache is effectively disabled right now.
88 * Do we want it? The slab cache already has per-cpu
89 * stuff, but the thread info (usually a order-1 page
90 * allocation) doesn't.
93 free_thread_info(tsk->thread_info);
94 kmem_cache_free(task_struct_cachep,tsk);
98 tsk = task_cache[cpu];
100 free_thread_info(tsk->thread_info);
101 kmem_cache_free(task_struct_cachep,tsk);
103 task_cache[cpu] = current;
107 #endif /* HAVE_ARCH_DUP_TASK_STRUCT */
109 void __put_task_struct(struct task_struct *tsk)
111 WARN_ON(!(tsk->state & (TASK_DEAD | TASK_ZOMBIE)));
112 WARN_ON(atomic_read(&tsk->usage));
113 WARN_ON(tsk == current);
115 security_task_free(tsk);
117 free_task_struct(tsk);
120 void add_wait_queue(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(q, wait);
127 spin_unlock_irqrestore(&q->lock, flags);
130 void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
134 wait->flags |= WQ_FLAG_EXCLUSIVE;
135 spin_lock_irqsave(&q->lock, flags);
136 __add_wait_queue_tail(q, wait);
137 spin_unlock_irqrestore(&q->lock, flags);
140 void remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
144 spin_lock_irqsave(&q->lock, flags);
145 __remove_wait_queue(q, wait);
146 spin_unlock_irqrestore(&q->lock, flags);
149 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
153 __set_current_state(state);
154 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
155 spin_lock_irqsave(&q->lock, flags);
156 if (list_empty(&wait->task_list))
157 __add_wait_queue(q, wait);
158 spin_unlock_irqrestore(&q->lock, flags);
162 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
166 __set_current_state(state);
167 wait->flags |= WQ_FLAG_EXCLUSIVE;
168 spin_lock_irqsave(&q->lock, flags);
169 if (list_empty(&wait->task_list))
170 __add_wait_queue_tail(q, wait);
171 spin_unlock_irqrestore(&q->lock, flags);
174 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
178 __set_current_state(TASK_RUNNING);
179 if (!list_empty(&wait->task_list)) {
180 spin_lock_irqsave(&q->lock, flags);
181 list_del_init(&wait->task_list);
182 spin_unlock_irqrestore(&q->lock, flags);
186 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync)
188 int ret = default_wake_function(wait, mode, sync);
191 list_del_init(&wait->task_list);
195 void __init fork_init(unsigned long mempages)
197 #ifndef HAVE_ARCH_DUP_TASK_STRUCT
198 /* create a slab on which task_structs can be allocated */
200 kmem_cache_create("task_struct",
201 sizeof(struct task_struct),0,
202 SLAB_MUST_HWCACHE_ALIGN, NULL, NULL);
203 if (!task_struct_cachep)
204 panic("fork_init(): cannot create task_struct SLAB cache");
208 * The default maximum number of threads is set to a safe
209 * value: the thread structures can take up at most half
212 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8;
214 * we need to allow at least 20 threads to boot a system
219 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
220 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
223 #ifdef HAVE_ARCH_DUP_TASK_STRUCT
224 extern struct task_struct *dup_task_struct (struct task_struct *orig);
225 #else /* !HAVE_ARCH_DUP_TASK_STRUCT */
227 struct task_struct *dup_task_struct(struct task_struct *orig)
229 struct task_struct *tsk;
230 struct thread_info *ti;
233 prepare_to_copy(orig);
235 tsk = task_cache[cpu];
236 task_cache[cpu] = NULL;
239 ti = alloc_thread_info();
243 tsk = kmem_cache_alloc(task_struct_cachep, GFP_KERNEL);
245 free_thread_info(ti);
249 ti = tsk->thread_info;
251 *ti = *orig->thread_info;
253 tsk->thread_info = ti;
256 /* One for us, one for whoever does the "release_task()" (usually parent) */
257 atomic_set(&tsk->usage,2);
261 #endif /* !HAVE_ARCH_DUP_TASK_STRUCT */
264 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
266 struct vm_area_struct * mpnt, *tmp, **pprev;
268 unsigned long charge = 0;
270 down_write(&oldmm->mmap_sem);
271 flush_cache_mm(current->mm);
274 mm->mmap_cache = NULL;
275 mm->free_area_cache = TASK_UNMAPPED_BASE;
282 * Add it to the mmlist after the parent.
283 * Doing it this way means that we can order the list,
284 * and fork() won't mess up the ordering significantly.
285 * Add it first so that swapoff can see any swap entries.
287 spin_lock(&mmlist_lock);
288 list_add(&mm->mmlist, ¤t->mm->mmlist);
290 spin_unlock(&mmlist_lock);
292 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
295 if(mpnt->vm_flags & VM_DONTCOPY)
297 if (mpnt->vm_flags & VM_ACCOUNT) {
298 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
299 if (!vm_enough_memory(len))
303 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
307 tmp->vm_flags &= ~VM_LOCKED;
311 INIT_LIST_HEAD(&tmp->shared);
313 struct inode *inode = file->f_dentry->d_inode;
315 if (tmp->vm_flags & VM_DENYWRITE)
316 atomic_dec(&inode->i_writecount);
318 /* insert tmp into the share list, just after mpnt */
319 down(&inode->i_mapping->i_shared_sem);
320 list_add_tail(&tmp->shared, &mpnt->shared);
321 up(&inode->i_mapping->i_shared_sem);
325 * Link in the new vma and copy the page table entries:
326 * link in first so that swapoff can see swap entries.
328 spin_lock(&mm->page_table_lock);
330 pprev = &tmp->vm_next;
332 retval = copy_page_range(mm, current->mm, tmp);
333 spin_unlock(&mm->page_table_lock);
335 if (tmp->vm_ops && tmp->vm_ops->open)
336 tmp->vm_ops->open(tmp);
345 flush_tlb_mm(current->mm);
346 up_write(&oldmm->mmap_sem);
351 vm_unacct_memory(charge);
354 static inline int mm_alloc_pgd(struct mm_struct * mm)
356 mm->pgd = pgd_alloc(mm);
357 if (unlikely(!mm->pgd))
362 static inline void mm_free_pgd(struct mm_struct * mm)
367 #define dup_mmap(mm, oldmm) (0)
368 #define mm_alloc_pgd(mm) (0)
369 #define mm_free_pgd(mm)
370 #endif /* CONFIG_MMU */
372 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
375 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
376 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
378 #include <linux/init_task.h>
380 static struct mm_struct * mm_init(struct mm_struct * mm)
382 atomic_set(&mm->mm_users, 1);
383 atomic_set(&mm->mm_count, 1);
384 init_rwsem(&mm->mmap_sem);
385 mm->core_waiters = 0;
386 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
387 mm->ioctx_list_lock = RW_LOCK_UNLOCKED;
388 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
389 mm->free_area_cache = TASK_UNMAPPED_BASE;
391 if (likely(!mm_alloc_pgd(mm))) {
401 * Allocate and initialize an mm_struct.
403 struct mm_struct * mm_alloc(void)
405 struct mm_struct * mm;
409 memset(mm, 0, sizeof(*mm));
416 * Called when the last reference to the mm
417 * is dropped: either by a lazy thread or by
418 * mmput. Free the page directory and the mm.
420 inline void __mmdrop(struct mm_struct *mm)
422 BUG_ON(mm == &init_mm);
429 * Decrement the use count and release all resources for an mm.
431 void mmput(struct mm_struct *mm)
433 if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)) {
434 list_del(&mm->mmlist);
436 spin_unlock(&mmlist_lock);
443 /* Please note the differences between mmput and mm_release.
444 * mmput is called whenever we stop holding onto a mm_struct,
445 * error success whatever.
447 * mm_release is called after a mm_struct has been removed
448 * from the current process.
450 * This difference is important for error handling, when we
451 * only half set up a mm_struct for a new process and need to restore
452 * the old one. Because we mmput the new mm_struct before
453 * restoring the old one. . .
454 * Eric Biederman 10 January 1998
456 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
458 struct completion *vfork_done = tsk->vfork_done;
460 /* Get rid of any cached register state */
461 deactivate_mm(tsk, mm);
463 /* notify parent sleeping on vfork() */
465 tsk->vfork_done = NULL;
466 complete(vfork_done);
468 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
469 u32 * tidptr = tsk->clear_child_tid;
470 tsk->clear_child_tid = NULL;
473 * We don't check the error code - if userspace has
474 * not set up a proper pointer then tough luck.
477 sys_futex(tidptr, FUTEX_WAKE, 1, NULL);
481 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
483 struct mm_struct * mm, *oldmm;
486 tsk->min_flt = tsk->maj_flt = 0;
487 tsk->cmin_flt = tsk->cmaj_flt = 0;
488 tsk->nswap = tsk->cnswap = 0;
491 tsk->active_mm = NULL;
494 * Are we cloning a kernel thread?
496 * We need to steal a active VM for that..
502 if (clone_flags & CLONE_VM) {
503 atomic_inc(&oldmm->mm_users);
506 * There are cases where the PTL is held to ensure no
507 * new threads start up in user mode using an mm, which
508 * allows optimizing out ipis; the tlb_gather_mmu code
511 spin_unlock_wait(&oldmm->page_table_lock);
520 /* Copy the current MM stuff.. */
521 memcpy(mm, oldmm, sizeof(*mm));
525 if (init_new_context(tsk,mm))
528 retval = dup_mmap(mm, oldmm);
543 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
545 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
546 /* We don't need to lock fs - think why ;-) */
548 atomic_set(&fs->count, 1);
549 fs->lock = RW_LOCK_UNLOCKED;
550 fs->umask = old->umask;
551 read_lock(&old->lock);
552 fs->rootmnt = mntget(old->rootmnt);
553 fs->root = dget(old->root);
554 fs->pwdmnt = mntget(old->pwdmnt);
555 fs->pwd = dget(old->pwd);
557 fs->altrootmnt = mntget(old->altrootmnt);
558 fs->altroot = dget(old->altroot);
560 fs->altrootmnt = NULL;
563 read_unlock(&old->lock);
568 struct fs_struct *copy_fs_struct(struct fs_struct *old)
570 return __copy_fs_struct(old);
573 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
575 if (clone_flags & CLONE_FS) {
576 atomic_inc(¤t->fs->count);
579 tsk->fs = __copy_fs_struct(current->fs);
585 static int count_open_files(struct files_struct *files, int size)
589 /* Find the last open fd */
590 for (i = size/(8*sizeof(long)); i > 0; ) {
591 if (files->open_fds->fds_bits[--i])
594 i = (i+1) * 8 * sizeof(long);
598 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
600 struct files_struct *oldf, *newf;
601 struct file **old_fds, **new_fds;
602 int open_files, nfds, size, i, error = 0;
605 * A background process may not have any files ...
607 oldf = current->files;
611 if (clone_flags & CLONE_FILES) {
612 atomic_inc(&oldf->count);
618 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
622 atomic_set(&newf->count, 1);
624 newf->file_lock = SPIN_LOCK_UNLOCKED;
626 newf->max_fds = NR_OPEN_DEFAULT;
627 newf->max_fdset = __FD_SETSIZE;
628 newf->close_on_exec = &newf->close_on_exec_init;
629 newf->open_fds = &newf->open_fds_init;
630 newf->fd = &newf->fd_array[0];
632 /* We don't yet have the oldf readlock, but even if the old
633 fdset gets grown now, we'll only copy up to "size" fds */
634 size = oldf->max_fdset;
635 if (size > __FD_SETSIZE) {
637 spin_lock(&newf->file_lock);
638 error = expand_fdset(newf, size-1);
639 spin_unlock(&newf->file_lock);
643 spin_lock(&oldf->file_lock);
645 open_files = count_open_files(oldf, size);
648 * Check whether we need to allocate a larger fd array.
649 * Note: we're not a clone task, so the open count won't
652 nfds = NR_OPEN_DEFAULT;
653 if (open_files > nfds) {
654 spin_unlock(&oldf->file_lock);
656 spin_lock(&newf->file_lock);
657 error = expand_fd_array(newf, open_files-1);
658 spin_unlock(&newf->file_lock);
661 nfds = newf->max_fds;
662 spin_lock(&oldf->file_lock);
668 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
669 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
671 for (i = open_files; i != 0; i--) {
672 struct file *f = *old_fds++;
677 spin_unlock(&oldf->file_lock);
679 /* compute the remainder to be cleared */
680 size = (newf->max_fds - open_files) * sizeof(struct file *);
682 /* This is long word aligned thus could use a optimized version */
683 memset(new_fds, 0, size);
685 if (newf->max_fdset > open_files) {
686 int left = (newf->max_fdset-open_files)/8;
687 int start = open_files / (8 * sizeof(unsigned long));
689 memset(&newf->open_fds->fds_bits[start], 0, left);
690 memset(&newf->close_on_exec->fds_bits[start], 0, left);
699 free_fdset (newf->close_on_exec, newf->max_fdset);
700 free_fdset (newf->open_fds, newf->max_fdset);
701 kmem_cache_free(files_cachep, newf);
705 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
707 struct sighand_struct *sig;
709 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
710 atomic_inc(¤t->sighand->count);
713 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
717 spin_lock_init(&sig->siglock);
718 atomic_set(&sig->count, 1);
719 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
723 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
725 struct signal_struct *sig;
727 if (clone_flags & CLONE_THREAD) {
728 atomic_inc(¤t->signal->count);
731 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
735 atomic_set(&sig->count, 1);
737 sig->group_exit_code = 0;
738 sig->group_exit_task = NULL;
739 sig->group_stop_count = 0;
740 sig->curr_target = NULL;
741 init_sigpending(&sig->shared_pending);
746 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
748 unsigned long new_flags = p->flags;
750 new_flags &= ~PF_SUPERPRIV;
751 new_flags |= PF_FORKNOEXEC;
752 if (!(clone_flags & CLONE_PTRACE))
754 p->flags = new_flags;
757 asmlinkage long sys_set_tid_address(int *tidptr)
759 current->clear_child_tid = tidptr;
765 * This creates a new process as a copy of the old one,
766 * but does not actually start it yet.
768 * It copies the registers, and all the appropriate
769 * parts of the process environment (as per the clone
770 * flags). The actual kick-off is left to the caller.
772 static struct task_struct *copy_process(unsigned long clone_flags,
773 unsigned long stack_start,
774 struct pt_regs *regs,
775 unsigned long stack_size,
780 struct task_struct *p = NULL;
782 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
783 return ERR_PTR(-EINVAL);
786 * Thread groups must share signals as well, and detached threads
787 * can only be started up within the thread group.
789 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
790 return ERR_PTR(-EINVAL);
791 if ((clone_flags & CLONE_DETACHED) && !(clone_flags & CLONE_THREAD))
792 return ERR_PTR(-EINVAL);
794 retval = security_task_create(clone_flags);
799 p = dup_task_struct(current);
804 if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur) {
805 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))
809 atomic_inc(&p->user->__count);
810 atomic_inc(&p->user->processes);
813 * If multiple threads are within copy_process(), then this check
814 * triggers too late. This doesn't hurt, the check is only there
815 * to stop root fork bombs.
817 if (nr_threads >= max_threads)
818 goto bad_fork_cleanup_count;
820 if (!try_module_get(p->thread_info->exec_domain->module))
821 goto bad_fork_cleanup_count;
823 if (p->binfmt && !try_module_get(p->binfmt->module))
824 goto bad_fork_cleanup_put_domain;
826 #ifdef CONFIG_PREEMPT
828 * schedule_tail drops this_rq()->lock so we compensate with a count
829 * of 1. Also, we want to start with kernel preemption disabled.
831 p->thread_info->preempt_count = 1;
834 p->state = TASK_UNINTERRUPTIBLE;
836 copy_flags(clone_flags, p);
837 if (clone_flags & CLONE_IDLETASK)
840 p->pid = alloc_pidmap();
842 goto bad_fork_cleanup;
845 if (clone_flags & CLONE_PARENT_SETTID)
846 if (put_user(p->pid, parent_tidptr))
847 goto bad_fork_cleanup;
849 p->proc_dentry = NULL;
851 INIT_LIST_HEAD(&p->run_list);
853 INIT_LIST_HEAD(&p->children);
854 INIT_LIST_HEAD(&p->sibling);
855 INIT_LIST_HEAD(&p->posix_timers);
856 init_waitqueue_head(&p->wait_chldexit);
857 p->vfork_done = NULL;
858 spin_lock_init(&p->alloc_lock);
859 spin_lock_init(&p->switch_lock);
861 clear_tsk_thread_flag(p, TIF_SIGPENDING);
862 init_sigpending(&p->pending);
864 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
865 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
866 init_timer(&p->real_timer);
867 p->real_timer.data = (unsigned long) p;
869 p->leader = 0; /* session leadership doesn't inherit */
871 p->utime = p->stime = 0;
872 p->cutime = p->cstime = 0;
874 p->lock_depth = -1; /* -1 = no lock */
875 p->start_time = get_jiffies_64();
879 if (security_task_alloc(p))
880 goto bad_fork_cleanup;
881 /* copy all the process information */
882 if (copy_semundo(clone_flags, p))
883 goto bad_fork_cleanup_security;
884 if (copy_files(clone_flags, p))
885 goto bad_fork_cleanup_semundo;
886 if (copy_fs(clone_flags, p))
887 goto bad_fork_cleanup_files;
888 if (copy_sighand(clone_flags, p))
889 goto bad_fork_cleanup_fs;
890 if (copy_signal(clone_flags, p))
891 goto bad_fork_cleanup_sighand;
892 if (copy_mm(clone_flags, p))
893 goto bad_fork_cleanup_signal;
894 retval = copy_namespace(clone_flags, p);
896 goto bad_fork_cleanup_mm;
897 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
899 goto bad_fork_cleanup_namespace;
901 if (clone_flags & CLONE_CHILD_SETTID)
902 p->set_child_tid = child_tidptr;
904 p->set_child_tid = NULL;
906 * Clear TID on mm_release()?
908 if (clone_flags & CLONE_CHILD_CLEARTID)
909 p->clear_child_tid = child_tidptr;
911 p->clear_child_tid = NULL;
914 * Syscall tracing should be turned off in the child regardless
917 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
919 /* Our parent execution domain becomes current domain
920 These must match for thread signalling to apply */
922 p->parent_exec_id = p->self_exec_id;
924 /* ok, now we should be set up.. */
925 if (clone_flags & CLONE_DETACHED)
928 p->exit_signal = clone_flags & CSIGNAL;
929 p->pdeath_signal = 0;
932 * Share the timeslice between parent and child, thus the
933 * total amount of pending timeslices in the system doesn't change,
934 * resulting in more scheduling fairness.
937 p->time_slice = (current->time_slice + 1) >> 1;
939 * The remainder of the first timeslice might be recovered by
940 * the parent if the child exits early enough.
942 p->first_time_slice = 1;
943 current->time_slice >>= 1;
944 p->last_run = jiffies;
945 if (!current->time_slice) {
947 * This case is rare, it happens when the parent has only
948 * a single jiffy left from its timeslice. Taking the
949 * runqueue lock is not a problem.
951 current->time_slice = 1;
953 scheduler_tick(0, 0);
959 * Ok, add it to the run-queues and make it
960 * visible to the rest of the system.
966 INIT_LIST_HEAD(&p->ptrace_children);
967 INIT_LIST_HEAD(&p->ptrace_list);
969 /* Need tasklist lock for parent etc handling! */
970 write_lock_irq(&tasklist_lock);
972 * Check for pending SIGKILL! The new thread should not be allowed
973 * to slip out of an OOM kill. (or normal SIGKILL.)
975 if (sigismember(¤t->pending.signal, SIGKILL)) {
976 write_unlock_irq(&tasklist_lock);
978 goto bad_fork_cleanup_namespace;
981 /* CLONE_PARENT re-uses the old parent */
982 if (clone_flags & CLONE_PARENT)
983 p->real_parent = current->real_parent;
985 p->real_parent = current;
986 p->parent = p->real_parent;
988 if (clone_flags & CLONE_THREAD) {
989 spin_lock(¤t->sighand->siglock);
991 * Important: if an exit-all has been started then
992 * do not create this new thread - the whole thread
993 * group is supposed to exit anyway.
995 if (current->signal->group_exit) {
996 spin_unlock(¤t->sighand->siglock);
997 write_unlock_irq(&tasklist_lock);
998 goto bad_fork_cleanup_namespace;
1000 p->tgid = current->tgid;
1001 p->group_leader = current->group_leader;
1003 if (current->signal->group_stop_count > 0) {
1005 * There is an all-stop in progress for the group.
1006 * We ourselves will stop as soon as we check signals.
1007 * Make the new thread part of that group stop too.
1009 current->signal->group_stop_count++;
1010 set_tsk_thread_flag(p, TIF_SIGPENDING);
1013 spin_unlock(¤t->sighand->siglock);
1017 if (p->ptrace & PT_PTRACED)
1018 __ptrace_link(p, current->parent);
1020 attach_pid(p, PIDTYPE_PID, p->pid);
1021 if (thread_group_leader(p)) {
1022 attach_pid(p, PIDTYPE_TGID, p->tgid);
1023 attach_pid(p, PIDTYPE_PGID, p->pgrp);
1024 attach_pid(p, PIDTYPE_SID, p->session);
1026 per_cpu(process_counts, smp_processor_id())++;
1028 link_pid(p, p->pids + PIDTYPE_TGID, &p->group_leader->pids[PIDTYPE_TGID].pid);
1031 write_unlock_irq(&tasklist_lock);
1036 return ERR_PTR(retval);
1039 bad_fork_cleanup_namespace:
1041 bad_fork_cleanup_mm:
1043 bad_fork_cleanup_signal:
1045 bad_fork_cleanup_sighand:
1047 bad_fork_cleanup_fs:
1048 exit_fs(p); /* blocking */
1049 bad_fork_cleanup_files:
1050 exit_files(p); /* blocking */
1051 bad_fork_cleanup_semundo:
1053 bad_fork_cleanup_security:
1054 security_task_free(p);
1057 free_pidmap(p->pid);
1059 module_put(p->binfmt->module);
1060 bad_fork_cleanup_put_domain:
1061 module_put(p->thread_info->exec_domain->module);
1062 bad_fork_cleanup_count:
1063 atomic_dec(&p->user->processes);
1066 free_task_struct(p);
1070 static inline int fork_traceflag (unsigned clone_flags)
1072 if (clone_flags & (CLONE_UNTRACED | CLONE_IDLETASK))
1074 else if (clone_flags & CLONE_VFORK) {
1075 if (current->ptrace & PT_TRACE_VFORK)
1076 return PTRACE_EVENT_VFORK;
1077 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1078 if (current->ptrace & PT_TRACE_CLONE)
1079 return PTRACE_EVENT_CLONE;
1080 } else if (current->ptrace & PT_TRACE_FORK)
1081 return PTRACE_EVENT_FORK;
1087 * Ok, this is the main fork-routine.
1089 * It copies the process, and if successful kick-starts
1090 * it and waits for it to finish using the VM if required.
1092 struct task_struct *do_fork(unsigned long clone_flags,
1093 unsigned long stack_start,
1094 struct pt_regs *regs,
1095 unsigned long stack_size,
1099 struct task_struct *p;
1102 if (unlikely(current->ptrace)) {
1103 trace = fork_traceflag (clone_flags);
1105 clone_flags |= CLONE_PTRACE;
1108 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr);
1110 struct completion vfork;
1112 if (clone_flags & CLONE_VFORK) {
1113 p->vfork_done = &vfork;
1114 init_completion(&vfork);
1117 if (p->ptrace & PT_PTRACED) {
1119 * We'll start up with an immediate SIGSTOP.
1121 sigaddset(&p->pending.signal, SIGSTOP);
1122 set_tsk_thread_flag(p, TIF_SIGPENDING);
1125 wake_up_forked_process(p); /* do this last */
1128 if (unlikely (trace)) {
1129 current->ptrace_message = (unsigned long) p->pid;
1130 ptrace_notify ((trace << 8) | SIGTRAP);
1133 if (clone_flags & CLONE_VFORK) {
1134 wait_for_completion(&vfork);
1135 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1136 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1139 * Let the child process run first, to avoid most of the
1140 * COW overhead when the child exec()s afterwards.
1147 /* SLAB cache for signal_struct structures (tsk->signal) */
1148 kmem_cache_t *signal_cachep;
1150 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1151 kmem_cache_t *sighand_cachep;
1153 /* SLAB cache for files_struct structures (tsk->files) */
1154 kmem_cache_t *files_cachep;
1156 /* SLAB cache for fs_struct structures (tsk->fs) */
1157 kmem_cache_t *fs_cachep;
1159 /* SLAB cache for vm_area_struct structures */
1160 kmem_cache_t *vm_area_cachep;
1162 /* SLAB cache for mm_struct structures (tsk->mm) */
1163 kmem_cache_t *mm_cachep;
1165 void __init proc_caches_init(void)
1167 sighand_cachep = kmem_cache_create("sighand_cache",
1168 sizeof(struct sighand_struct), 0,
1169 SLAB_HWCACHE_ALIGN, NULL, NULL);
1170 if (!sighand_cachep)
1171 panic("Cannot create sighand SLAB cache");
1173 signal_cachep = kmem_cache_create("signal_cache",
1174 sizeof(struct signal_struct), 0,
1175 SLAB_HWCACHE_ALIGN, NULL, NULL);
1177 panic("Cannot create signal SLAB cache");
1179 files_cachep = kmem_cache_create("files_cache",
1180 sizeof(struct files_struct), 0,
1181 SLAB_HWCACHE_ALIGN, NULL, NULL);
1183 panic("Cannot create files SLAB cache");
1185 fs_cachep = kmem_cache_create("fs_cache",
1186 sizeof(struct fs_struct), 0,
1187 SLAB_HWCACHE_ALIGN, NULL, NULL);
1189 panic("Cannot create fs_struct SLAB cache");
1191 vm_area_cachep = kmem_cache_create("vm_area_struct",
1192 sizeof(struct vm_area_struct), 0,
1195 panic("vma_init: Cannot alloc vm_area_struct SLAB cache");
1197 mm_cachep = kmem_cache_create("mm_struct",
1198 sizeof(struct mm_struct), 0,
1199 SLAB_HWCACHE_ALIGN, NULL, NULL);
1201 panic("vma_init: Cannot alloc mm_struct SLAB cache");