4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 'fork.c' contains the help-routines for the 'fork' system call
9 * (see also entry.S and others).
10 * Fork is rather simple, once you get the hang of it, but the memory
11 * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
14 #include <linux/config.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/unistd.h>
18 #include <linux/smp_lock.h>
19 #include <linux/module.h>
20 #include <linux/vmalloc.h>
21 #include <linux/completion.h>
22 #include <linux/namespace.h>
23 #include <linux/personality.h>
24 #include <linux/mempolicy.h>
25 #include <linux/sem.h>
26 #include <linux/file.h>
27 #include <linux/binfmts.h>
28 #include <linux/mman.h>
30 #include <linux/cpu.h>
31 #include <linux/security.h>
32 #include <linux/swap.h>
33 #include <linux/syscalls.h>
34 #include <linux/jiffies.h>
35 #include <linux/futex.h>
36 #include <linux/ptrace.h>
37 #include <linux/mount.h>
38 #include <linux/audit.h>
39 #include <linux/profile.h>
40 #include <linux/rmap.h>
41 #include <linux/vs_network.h>
42 #include <linux/vs_limit.h>
43 #include <linux/vs_memory.h>
44 #include <linux/ckrm.h>
45 #include <linux/ckrm_tsk.h>
46 #include <linux/ckrm_mem_inline.h>
48 #include <asm/pgtable.h>
49 #include <asm/pgalloc.h>
50 #include <asm/uaccess.h>
51 #include <asm/mmu_context.h>
52 #include <asm/cacheflush.h>
53 #include <asm/tlbflush.h>
55 /* The idle threads do not count..
56 * Protected by write_lock_irq(&tasklist_lock)
61 unsigned long total_forks; /* Handle normal Linux uptimes. */
63 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
65 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
67 EXPORT_SYMBOL(tasklist_lock);
69 int nr_processes(void)
74 for_each_online_cpu(cpu)
75 total += per_cpu(process_counts, cpu);
80 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
81 # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
82 # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
83 static kmem_cache_t *task_struct_cachep;
86 void free_task(struct task_struct *tsk)
88 free_thread_info(tsk->thread_info);
89 clr_vx_info(&tsk->vx_info);
90 clr_nx_info(&tsk->nx_info);
91 free_task_struct(tsk);
93 EXPORT_SYMBOL(free_task);
95 void __put_task_struct(struct task_struct *tsk)
97 WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
98 WARN_ON(atomic_read(&tsk->usage));
99 WARN_ON(tsk == current);
101 if (unlikely(tsk->audit_context))
103 security_task_free(tsk);
105 put_group_info(tsk->group_info);
107 if (!profile_handoff_task(tsk))
111 void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
115 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
116 spin_lock_irqsave(&q->lock, flags);
117 __add_wait_queue(q, wait);
118 spin_unlock_irqrestore(&q->lock, flags);
121 EXPORT_SYMBOL(add_wait_queue);
123 void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
127 wait->flags |= WQ_FLAG_EXCLUSIVE;
128 spin_lock_irqsave(&q->lock, flags);
129 __add_wait_queue_tail(q, wait);
130 spin_unlock_irqrestore(&q->lock, flags);
133 EXPORT_SYMBOL(add_wait_queue_exclusive);
135 void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
139 spin_lock_irqsave(&q->lock, flags);
140 __remove_wait_queue(q, wait);
141 spin_unlock_irqrestore(&q->lock, flags);
144 EXPORT_SYMBOL(remove_wait_queue);
148 * Note: we use "set_current_state()" _after_ the wait-queue add,
149 * because we need a memory barrier there on SMP, so that any
150 * wake-function that tests for the wait-queue being active
151 * will be guaranteed to see waitqueue addition _or_ subsequent
152 * tests in this thread will see the wakeup having taken place.
154 * The spin_unlock() itself is semi-permeable and only protects
155 * one way (it only protects stuff inside the critical region and
156 * stops them from bleeding out - it would still allow subsequent
157 * loads to move into the the critical region).
159 void fastcall prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
163 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
164 spin_lock_irqsave(&q->lock, flags);
165 if (list_empty(&wait->task_list))
166 __add_wait_queue(q, wait);
168 * don't alter the task state if this is just going to
169 * queue an async wait queue callback
171 if (is_sync_wait(wait))
172 set_current_state(state);
173 spin_unlock_irqrestore(&q->lock, flags);
176 EXPORT_SYMBOL(prepare_to_wait);
179 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
183 wait->flags |= WQ_FLAG_EXCLUSIVE;
184 spin_lock_irqsave(&q->lock, flags);
185 if (list_empty(&wait->task_list))
186 __add_wait_queue_tail(q, wait);
188 * don't alter the task state if this is just going to
189 * queue an async wait queue callback
191 if (is_sync_wait(wait))
192 set_current_state(state);
193 spin_unlock_irqrestore(&q->lock, flags);
196 EXPORT_SYMBOL(prepare_to_wait_exclusive);
198 void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
202 __set_current_state(TASK_RUNNING);
204 * We can check for list emptiness outside the lock
206 * - we use the "careful" check that verifies both
207 * the next and prev pointers, so that there cannot
208 * be any half-pending updates in progress on other
209 * CPU's that we haven't seen yet (and that might
210 * still change the stack area.
212 * - all other users take the lock (ie we can only
213 * have _one_ other CPU that looks at or modifies
216 if (!list_empty_careful(&wait->task_list)) {
217 spin_lock_irqsave(&q->lock, flags);
218 list_del_init(&wait->task_list);
219 spin_unlock_irqrestore(&q->lock, flags);
223 EXPORT_SYMBOL(finish_wait);
225 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
227 int ret = default_wake_function(wait, mode, sync, key);
230 list_del_init(&wait->task_list);
234 EXPORT_SYMBOL(autoremove_wake_function);
236 void __init fork_init(unsigned long mempages)
238 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
239 #ifndef ARCH_MIN_TASKALIGN
240 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
242 /* create a slab on which task_structs can be allocated */
244 kmem_cache_create("task_struct", sizeof(struct task_struct),
245 ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
249 * The default maximum number of threads is set to a safe
250 * value: the thread structures can take up at most half
253 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8;
255 * we need to allow at least 20 threads to boot a system
260 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
261 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
264 static struct task_struct *dup_task_struct(struct task_struct *orig)
266 struct task_struct *tsk;
267 struct thread_info *ti;
269 prepare_to_copy(orig);
271 tsk = alloc_task_struct();
275 ti = alloc_thread_info(tsk);
277 free_task_struct(tsk);
281 *ti = *orig->thread_info;
283 tsk->thread_info = ti;
286 ckrm_cb_newtask(tsk);
287 /* One for us, one for whoever does the "release_task()" (usually parent) */
288 atomic_set(&tsk->usage,2);
289 #ifdef CONFIG_CKRM_RES_MEM
290 INIT_LIST_HEAD(&tsk->mm_peers);
296 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
298 struct vm_area_struct * mpnt, *tmp, **pprev;
299 struct rb_node **rb_link, *rb_parent;
301 unsigned long charge;
302 struct mempolicy *pol;
304 down_write(&oldmm->mmap_sem);
305 flush_cache_mm(current->mm);
308 mm->mmap_cache = NULL;
309 mm->free_area_cache = oldmm->mmap_base;
313 cpus_clear(mm->cpu_vm_mask);
315 rb_link = &mm->mm_rb.rb_node;
320 * Add it to the mmlist after the parent.
321 * Doing it this way means that we can order the list,
322 * and fork() won't mess up the ordering significantly.
323 * Add it first so that swapoff can see any swap entries.
325 spin_lock(&mmlist_lock);
326 list_add(&mm->mmlist, ¤t->mm->mmlist);
328 spin_unlock(&mmlist_lock);
330 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
333 if (mpnt->vm_flags & VM_DONTCOPY) {
334 __vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
339 if (mpnt->vm_flags & VM_ACCOUNT) {
340 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
341 if (security_vm_enough_memory(len))
345 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
349 pol = mpol_copy(vma_policy(mpnt));
350 retval = PTR_ERR(pol);
352 goto fail_nomem_policy;
353 vma_set_policy(tmp, pol);
354 tmp->vm_flags &= ~VM_LOCKED;
360 struct inode *inode = file->f_dentry->d_inode;
362 if (tmp->vm_flags & VM_DENYWRITE)
363 atomic_dec(&inode->i_writecount);
365 /* insert tmp into the share list, just after mpnt */
366 spin_lock(&file->f_mapping->i_mmap_lock);
367 flush_dcache_mmap_lock(file->f_mapping);
368 vma_prio_tree_add(tmp, mpnt);
369 flush_dcache_mmap_unlock(file->f_mapping);
370 spin_unlock(&file->f_mapping->i_mmap_lock);
374 * Link in the new vma and copy the page table entries:
375 * link in first so that swapoff can see swap entries,
376 * and try_to_unmap_one's find_vma find the new vma.
378 spin_lock(&mm->page_table_lock);
380 pprev = &tmp->vm_next;
382 __vma_link_rb(mm, tmp, rb_link, rb_parent);
383 rb_link = &tmp->vm_rb.rb_right;
384 rb_parent = &tmp->vm_rb;
387 retval = copy_page_range(mm, current->mm, tmp);
388 spin_unlock(&mm->page_table_lock);
390 if (tmp->vm_ops && tmp->vm_ops->open)
391 tmp->vm_ops->open(tmp);
399 flush_tlb_mm(current->mm);
400 up_write(&oldmm->mmap_sem);
403 kmem_cache_free(vm_area_cachep, tmp);
406 vm_unacct_memory(charge);
410 static inline int mm_alloc_pgd(struct mm_struct * mm)
412 mm->pgd = pgd_alloc(mm);
413 if (unlikely(!mm->pgd))
418 static inline void mm_free_pgd(struct mm_struct * mm)
423 #define dup_mmap(mm, oldmm) (0)
424 #define mm_alloc_pgd(mm) (0)
425 #define mm_free_pgd(mm)
426 #endif /* CONFIG_MMU */
428 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
431 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
432 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
434 #include <linux/init_task.h>
436 static struct mm_struct * mm_init(struct mm_struct * mm)
438 atomic_set(&mm->mm_users, 1);
439 atomic_set(&mm->mm_count, 1);
440 init_rwsem(&mm->mmap_sem);
441 mm->core_waiters = 0;
442 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
443 mm->ioctx_list_lock = RW_LOCK_UNLOCKED;
444 mm->ioctx_list = NULL;
445 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
446 mm->free_area_cache = TASK_UNMAPPED_BASE;
447 #ifdef CONFIG_CKRM_RES_MEM
448 INIT_LIST_HEAD(&mm->tasklist);
449 mm->peertask_lock = SPIN_LOCK_UNLOCKED;
452 if (likely(!mm_alloc_pgd(mm))) {
454 set_vx_info(&mm->mm_vx_info, current->vx_info);
462 * Allocate and initialize an mm_struct.
464 struct mm_struct * mm_alloc(void)
466 struct mm_struct * mm;
470 memset(mm, 0, sizeof(*mm));
472 #ifdef CONFIG_CKRM_RES_MEM
473 mm->memclass = GET_MEM_CLASS(current);
474 mem_class_get(mm->memclass);
481 * Called when the last reference to the mm
482 * is dropped: either by a lazy thread or by
483 * mmput. Free the page directory and the mm.
485 void fastcall __mmdrop(struct mm_struct *mm)
487 BUG_ON(mm == &init_mm);
490 clr_vx_info(&mm->mm_vx_info);
491 #ifdef CONFIG_CKRM_RES_MEM
492 /* class can be null and mm's tasklist can be empty here */
494 mem_class_put(mm->memclass);
502 * Decrement the use count and release all resources for an mm.
504 void mmput(struct mm_struct *mm)
506 if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)) {
507 list_del(&mm->mmlist);
509 spin_unlock(&mmlist_lock);
516 EXPORT_SYMBOL_GPL(mmput);
519 * get_task_mm - acquire a reference to the task's mm
521 * Returns %NULL if the task has no mm. Checks if the use count
522 * of the mm is non-zero and if so returns a reference to it, after
523 * bumping up the use count. User must release the mm via mmput()
524 * after use. Typically used by /proc and ptrace.
526 * If the use count is zero, it means that this mm is going away,
527 * so return %NULL. This only happens in the case of an AIO daemon
528 * which has temporarily adopted an mm (see use_mm), in the course
529 * of its final mmput, before exit_aio has completed.
531 struct mm_struct *get_task_mm(struct task_struct *task)
533 struct mm_struct *mm;
538 spin_lock(&mmlist_lock);
539 if (!atomic_read(&mm->mm_users))
542 atomic_inc(&mm->mm_users);
543 spin_unlock(&mmlist_lock);
548 EXPORT_SYMBOL_GPL(get_task_mm);
550 /* Please note the differences between mmput and mm_release.
551 * mmput is called whenever we stop holding onto a mm_struct,
552 * error success whatever.
554 * mm_release is called after a mm_struct has been removed
555 * from the current process.
557 * This difference is important for error handling, when we
558 * only half set up a mm_struct for a new process and need to restore
559 * the old one. Because we mmput the new mm_struct before
560 * restoring the old one. . .
561 * Eric Biederman 10 January 1998
563 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
565 struct completion *vfork_done = tsk->vfork_done;
567 /* Get rid of any cached register state */
568 deactivate_mm(tsk, mm);
570 /* notify parent sleeping on vfork() */
572 tsk->vfork_done = NULL;
573 complete(vfork_done);
575 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
576 u32 __user * tidptr = tsk->clear_child_tid;
577 tsk->clear_child_tid = NULL;
580 * We don't check the error code - if userspace has
581 * not set up a proper pointer then tough luck.
584 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
588 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
590 struct mm_struct * mm, *oldmm;
593 tsk->min_flt = tsk->maj_flt = 0;
594 tsk->nvcsw = tsk->nivcsw = 0;
597 tsk->active_mm = NULL;
600 * Are we cloning a kernel thread?
602 * We need to steal a active VM for that..
608 if (clone_flags & CLONE_VM) {
609 atomic_inc(&oldmm->mm_users);
612 * There are cases where the PTL is held to ensure no
613 * new threads start up in user mode using an mm, which
614 * allows optimizing out ipis; the tlb_gather_mmu code
617 spin_unlock_wait(&oldmm->page_table_lock);
626 /* Copy the current MM stuff.. */
627 memcpy(mm, oldmm, sizeof(*mm));
628 mm->mm_vx_info = NULL;
632 if (init_new_context(tsk,mm))
635 retval = dup_mmap(mm, oldmm);
642 ckrm_init_mm_to_task(mm, tsk);
652 * If init_new_context() failed, we cannot use mmput() to free the mm
653 * because it calls destroy_context()
660 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
662 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
663 /* We don't need to lock fs - think why ;-) */
665 atomic_set(&fs->count, 1);
666 fs->lock = RW_LOCK_UNLOCKED;
667 fs->umask = old->umask;
668 read_lock(&old->lock);
669 fs->rootmnt = mntget(old->rootmnt);
670 fs->root = dget(old->root);
671 fs->pwdmnt = mntget(old->pwdmnt);
672 fs->pwd = dget(old->pwd);
674 fs->altrootmnt = mntget(old->altrootmnt);
675 fs->altroot = dget(old->altroot);
677 fs->altrootmnt = NULL;
680 read_unlock(&old->lock);
685 struct fs_struct *copy_fs_struct(struct fs_struct *old)
687 return __copy_fs_struct(old);
690 EXPORT_SYMBOL_GPL(copy_fs_struct);
692 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
694 if (clone_flags & CLONE_FS) {
695 atomic_inc(¤t->fs->count);
698 tsk->fs = __copy_fs_struct(current->fs);
704 static int count_open_files(struct files_struct *files, int size)
708 /* Find the last open fd */
709 for (i = size/(8*sizeof(long)); i > 0; ) {
710 if (files->open_fds->fds_bits[--i])
713 i = (i+1) * 8 * sizeof(long);
717 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
719 struct files_struct *oldf, *newf;
720 struct file **old_fds, **new_fds;
721 int open_files, nfds, size, i, error = 0;
724 * A background process may not have any files ...
726 oldf = current->files;
730 if (clone_flags & CLONE_FILES) {
731 atomic_inc(&oldf->count);
736 * Note: we may be using current for both targets (See exec.c)
737 * This works because we cache current->files (old) as oldf. Don't
742 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
746 atomic_set(&newf->count, 1);
748 newf->file_lock = SPIN_LOCK_UNLOCKED;
750 newf->max_fds = NR_OPEN_DEFAULT;
751 newf->max_fdset = __FD_SETSIZE;
752 newf->close_on_exec = &newf->close_on_exec_init;
753 newf->open_fds = &newf->open_fds_init;
754 newf->fd = &newf->fd_array[0];
756 /* We don't yet have the oldf readlock, but even if the old
757 fdset gets grown now, we'll only copy up to "size" fds */
758 size = oldf->max_fdset;
759 if (size > __FD_SETSIZE) {
761 spin_lock(&newf->file_lock);
762 error = expand_fdset(newf, size-1);
763 spin_unlock(&newf->file_lock);
767 spin_lock(&oldf->file_lock);
769 open_files = count_open_files(oldf, size);
772 * Check whether we need to allocate a larger fd array.
773 * Note: we're not a clone task, so the open count won't
776 nfds = NR_OPEN_DEFAULT;
777 if (open_files > nfds) {
778 spin_unlock(&oldf->file_lock);
780 spin_lock(&newf->file_lock);
781 error = expand_fd_array(newf, open_files-1);
782 spin_unlock(&newf->file_lock);
785 nfds = newf->max_fds;
786 spin_lock(&oldf->file_lock);
792 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
793 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
795 for (i = open_files; i != 0; i--) {
796 struct file *f = *old_fds++;
801 spin_unlock(&oldf->file_lock);
803 /* compute the remainder to be cleared */
804 size = (newf->max_fds - open_files) * sizeof(struct file *);
806 /* This is long word aligned thus could use a optimized version */
807 memset(new_fds, 0, size);
809 if (newf->max_fdset > open_files) {
810 int left = (newf->max_fdset-open_files)/8;
811 int start = open_files / (8 * sizeof(unsigned long));
813 memset(&newf->open_fds->fds_bits[start], 0, left);
814 memset(&newf->close_on_exec->fds_bits[start], 0, left);
823 free_fdset (newf->close_on_exec, newf->max_fdset);
824 free_fdset (newf->open_fds, newf->max_fdset);
825 kmem_cache_free(files_cachep, newf);
830 * Helper to unshare the files of the current task.
831 * We don't want to expose copy_files internals to
832 * the exec layer of the kernel.
835 int unshare_files(void)
837 struct files_struct *files = current->files;
843 /* This can race but the race causes us to copy when we don't
844 need to and drop the copy */
845 if(atomic_read(&files->count) == 1)
847 atomic_inc(&files->count);
850 rc = copy_files(0, current);
852 current->files = files;
856 EXPORT_SYMBOL(unshare_files);
858 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
860 struct sighand_struct *sig;
862 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
863 atomic_inc(¤t->sighand->count);
866 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
870 spin_lock_init(&sig->siglock);
871 atomic_set(&sig->count, 1);
872 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
876 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
878 struct signal_struct *sig;
880 if (clone_flags & CLONE_THREAD) {
881 atomic_inc(¤t->signal->count);
884 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
888 atomic_set(&sig->count, 1);
890 sig->group_exit_code = 0;
891 sig->group_exit_task = NULL;
892 sig->group_stop_count = 0;
893 sig->curr_target = NULL;
894 init_sigpending(&sig->shared_pending);
895 INIT_LIST_HEAD(&sig->posix_timers);
897 sig->tty = current->signal->tty;
898 sig->pgrp = process_group(current);
899 sig->session = current->signal->session;
900 sig->leader = 0; /* session leadership doesn't inherit */
901 sig->tty_old_pgrp = 0;
903 sig->utime = sig->stime = sig->cutime = sig->cstime = 0;
904 sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
905 sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
910 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
912 unsigned long new_flags = p->flags;
914 new_flags &= ~PF_SUPERPRIV;
915 new_flags |= PF_FORKNOEXEC;
916 if (!(clone_flags & CLONE_PTRACE))
918 p->flags = new_flags;
921 asmlinkage long sys_set_tid_address(int __user *tidptr)
923 current->clear_child_tid = tidptr;
929 * This creates a new process as a copy of the old one,
930 * but does not actually start it yet.
932 * It copies the registers, and all the appropriate
933 * parts of the process environment (as per the clone
934 * flags). The actual kick-off is left to the caller.
936 static task_t *copy_process(unsigned long clone_flags,
937 unsigned long stack_start,
938 struct pt_regs *regs,
939 unsigned long stack_size,
940 int __user *parent_tidptr,
941 int __user *child_tidptr,
945 struct task_struct *p = NULL;
948 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
949 return ERR_PTR(-EINVAL);
952 * Thread groups must share signals as well, and detached threads
953 * can only be started up within the thread group.
955 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
956 return ERR_PTR(-EINVAL);
959 * Shared signal handlers imply shared VM. By way of the above,
960 * thread groups also imply shared VM. Blocking this case allows
961 * for various simplifications in other code.
963 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
964 return ERR_PTR(-EINVAL);
966 retval = security_task_create(clone_flags);
971 p = dup_task_struct(current);
977 set_vx_info(&p->vx_info, current->vx_info);
979 set_nx_info(&p->nx_info, current->nx_info);
981 /* check vserver memory */
982 if (p->mm && !(clone_flags & CLONE_VM)) {
983 if (vx_vmpages_avail(p->mm, p->mm->total_vm))
984 vx_pages_add(p->mm->mm_vx_info, RLIMIT_AS, p->mm->total_vm);
988 if (p->mm && vx_flags(VXF_FORK_RSS, 0)) {
989 if (!vx_rsspages_avail(p->mm, p->mm->rss))
990 goto bad_fork_cleanup_vm;
994 if (!vx_nproc_avail(1))
995 goto bad_fork_cleanup_vm;
997 if (atomic_read(&p->user->processes) >=
998 p->rlim[RLIMIT_NPROC].rlim_cur) {
999 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
1000 p->user != &root_user)
1001 goto bad_fork_cleanup_vm;
1004 atomic_inc(&p->user->__count);
1005 atomic_inc(&p->user->processes);
1006 get_group_info(p->group_info);
1009 * If multiple threads are within copy_process(), then this check
1010 * triggers too late. This doesn't hurt, the check is only there
1011 * to stop root fork bombs.
1013 if (nr_threads >= max_threads)
1014 goto bad_fork_cleanup_count;
1016 if (!try_module_get(p->thread_info->exec_domain->module))
1017 goto bad_fork_cleanup_count;
1019 if (p->binfmt && !try_module_get(p->binfmt->module))
1020 goto bad_fork_cleanup_put_domain;
1024 copy_flags(clone_flags, p);
1027 if (clone_flags & CLONE_PARENT_SETTID)
1028 if (put_user(p->pid, parent_tidptr))
1029 goto bad_fork_cleanup;
1031 p->proc_dentry = NULL;
1033 INIT_LIST_HEAD(&p->children);
1034 INIT_LIST_HEAD(&p->sibling);
1035 init_waitqueue_head(&p->wait_chldexit);
1036 p->vfork_done = NULL;
1037 spin_lock_init(&p->alloc_lock);
1038 spin_lock_init(&p->proc_lock);
1040 clear_tsk_thread_flag(p, TIF_SIGPENDING);
1041 init_sigpending(&p->pending);
1043 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
1044 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
1045 init_timer(&p->real_timer);
1046 p->real_timer.data = (unsigned long) p;
1048 p->utime = p->stime = 0;
1049 p->lock_depth = -1; /* -1 = no lock */
1050 do_posix_clock_monotonic_gettime(&p->start_time);
1052 p->io_context = NULL;
1054 p->audit_context = NULL;
1056 p->mempolicy = mpol_copy(p->mempolicy);
1057 if (IS_ERR(p->mempolicy)) {
1058 retval = PTR_ERR(p->mempolicy);
1059 p->mempolicy = NULL;
1060 goto bad_fork_cleanup;
1064 if ((retval = security_task_alloc(p)))
1065 goto bad_fork_cleanup_policy;
1066 if ((retval = audit_alloc(p)))
1067 goto bad_fork_cleanup_security;
1068 /* copy all the process information */
1069 if ((retval = copy_semundo(clone_flags, p)))
1070 goto bad_fork_cleanup_audit;
1071 if ((retval = copy_files(clone_flags, p)))
1072 goto bad_fork_cleanup_semundo;
1073 if ((retval = copy_fs(clone_flags, p)))
1074 goto bad_fork_cleanup_files;
1075 if ((retval = copy_sighand(clone_flags, p)))
1076 goto bad_fork_cleanup_fs;
1077 if ((retval = copy_signal(clone_flags, p)))
1078 goto bad_fork_cleanup_sighand;
1079 if ((retval = copy_mm(clone_flags, p)))
1080 goto bad_fork_cleanup_signal;
1081 if ((retval = copy_namespace(clone_flags, p)))
1082 goto bad_fork_cleanup_mm;
1083 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
1085 goto bad_fork_cleanup_namespace;
1087 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
1089 * Clear TID on mm_release()?
1091 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
1094 * Syscall tracing should be turned off in the child regardless
1097 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1099 /* Our parent execution domain becomes current domain
1100 These must match for thread signalling to apply */
1102 p->parent_exec_id = p->self_exec_id;
1104 /* ok, now we should be set up.. */
1105 p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
1106 p->pdeath_signal = 0;
1109 /* Perform scheduler related setup */
1113 * Ok, make it visible to the rest of the system.
1114 * We dont wake it up yet.
1117 p->group_leader = p;
1118 INIT_LIST_HEAD(&p->ptrace_children);
1119 INIT_LIST_HEAD(&p->ptrace_list);
1121 /* Need tasklist lock for parent etc handling! */
1122 write_lock_irq(&tasklist_lock);
1125 * The task hasn't been attached yet, so cpus_allowed mask cannot
1126 * have changed. The cpus_allowed mask of the parent may have
1127 * changed after it was copied first time, and it may then move to
1128 * another CPU - so we re-copy it here and set the child's CPU to
1129 * the parent's CPU. This avoids alot of nasty races.
1131 p->cpus_allowed = current->cpus_allowed;
1132 set_task_cpu(p, smp_processor_id());
1135 * Check for pending SIGKILL! The new thread should not be allowed
1136 * to slip out of an OOM kill. (or normal SIGKILL.)
1138 if (sigismember(¤t->pending.signal, SIGKILL)) {
1139 write_unlock_irq(&tasklist_lock);
1141 goto bad_fork_cleanup_namespace;
1144 /* CLONE_PARENT re-uses the old parent */
1145 if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
1146 p->real_parent = current->real_parent;
1148 p->real_parent = current;
1149 p->parent = p->real_parent;
1151 if (clone_flags & CLONE_THREAD) {
1152 spin_lock(¤t->sighand->siglock);
1154 * Important: if an exit-all has been started then
1155 * do not create this new thread - the whole thread
1156 * group is supposed to exit anyway.
1158 if (current->signal->group_exit) {
1159 spin_unlock(¤t->sighand->siglock);
1160 write_unlock_irq(&tasklist_lock);
1162 goto bad_fork_cleanup_namespace;
1164 p->tgid = current->tgid;
1165 p->group_leader = current->group_leader;
1167 if (current->signal->group_stop_count > 0) {
1169 * There is an all-stop in progress for the group.
1170 * We ourselves will stop as soon as we check signals.
1171 * Make the new thread part of that group stop too.
1173 current->signal->group_stop_count++;
1174 set_tsk_thread_flag(p, TIF_SIGPENDING);
1177 spin_unlock(¤t->sighand->siglock);
1181 if (unlikely(p->ptrace & PT_PTRACED))
1182 __ptrace_link(p, current->parent);
1184 attach_pid(p, PIDTYPE_PID, p->pid);
1185 attach_pid(p, PIDTYPE_TGID, p->tgid);
1186 if (thread_group_leader(p)) {
1187 attach_pid(p, PIDTYPE_PGID, process_group(p));
1188 attach_pid(p, PIDTYPE_SID, p->signal->session);
1190 __get_cpu_var(process_counts)++;
1193 p->ioprio = current->ioprio;
1195 /* p is copy of current */
1198 atomic_inc(&vxi->cvirt.nr_threads);
1201 write_unlock_irq(&tasklist_lock);
1206 return ERR_PTR(retval);
1209 bad_fork_cleanup_namespace:
1211 bad_fork_cleanup_mm:
1214 bad_fork_cleanup_signal:
1216 bad_fork_cleanup_sighand:
1218 bad_fork_cleanup_fs:
1219 exit_fs(p); /* blocking */
1220 bad_fork_cleanup_files:
1221 exit_files(p); /* blocking */
1222 bad_fork_cleanup_semundo:
1224 bad_fork_cleanup_audit:
1226 bad_fork_cleanup_security:
1227 security_task_free(p);
1228 bad_fork_cleanup_policy:
1230 mpol_free(p->mempolicy);
1234 module_put(p->binfmt->module);
1235 bad_fork_cleanup_put_domain:
1236 module_put(p->thread_info->exec_domain->module);
1237 bad_fork_cleanup_count:
1238 put_group_info(p->group_info);
1239 atomic_dec(&p->user->processes);
1241 bad_fork_cleanup_vm:
1242 if (p->mm && !(clone_flags & CLONE_VM))
1243 vx_pages_sub(p->mm->mm_vx_info, RLIMIT_AS, p->mm->total_vm);
1249 struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
1251 memset(regs, 0, sizeof(struct pt_regs));
1255 task_t * __devinit fork_idle(int cpu)
1258 struct pt_regs regs;
1260 task = copy_process(CLONE_VM, 0, idle_regs(®s), 0, NULL, NULL, 0);
1262 return ERR_PTR(-ENOMEM);
1263 init_idle(task, cpu);
1264 unhash_process(task);
1268 static inline int fork_traceflag (unsigned clone_flags)
1270 if (clone_flags & CLONE_UNTRACED)
1272 else if (clone_flags & CLONE_VFORK) {
1273 if (current->ptrace & PT_TRACE_VFORK)
1274 return PTRACE_EVENT_VFORK;
1275 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1276 if (current->ptrace & PT_TRACE_CLONE)
1277 return PTRACE_EVENT_CLONE;
1278 } else if (current->ptrace & PT_TRACE_FORK)
1279 return PTRACE_EVENT_FORK;
1285 * Ok, this is the main fork-routine.
1287 * It copies the process, and if successful kick-starts
1288 * it and waits for it to finish using the VM if required.
1290 long do_fork(unsigned long clone_flags,
1291 unsigned long stack_start,
1292 struct pt_regs *regs,
1293 unsigned long stack_size,
1294 int __user *parent_tidptr,
1295 int __user *child_tidptr)
1297 struct task_struct *p;
1299 long pid = alloc_pidmap();
1303 if (unlikely(current->ptrace)) {
1304 trace = fork_traceflag (clone_flags);
1306 clone_flags |= CLONE_PTRACE;
1309 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
1310 if (numtasks_get_ref(current->taskclass, 0) == 0) {
1315 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
1317 * Do this prior waking up the new thread - the thread pointer
1318 * might get invalid after that point, if the thread exits quickly.
1321 struct completion vfork;
1325 if (clone_flags & CLONE_VFORK) {
1326 p->vfork_done = &vfork;
1327 init_completion(&vfork);
1330 if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
1332 * We'll start up with an immediate SIGSTOP.
1334 sigaddset(&p->pending.signal, SIGSTOP);
1335 set_tsk_thread_flag(p, TIF_SIGPENDING);
1338 if (!(clone_flags & CLONE_STOPPED))
1339 wake_up_new_task(p, clone_flags);
1341 p->state = TASK_STOPPED;
1344 if (unlikely (trace)) {
1345 current->ptrace_message = pid;
1346 ptrace_notify ((trace << 8) | SIGTRAP);
1349 if (clone_flags & CLONE_VFORK) {
1350 wait_for_completion(&vfork);
1351 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1352 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1355 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
1356 numtasks_put_ref(current->taskclass);
1364 /* SLAB cache for signal_struct structures (tsk->signal) */
1365 kmem_cache_t *signal_cachep;
1367 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1368 kmem_cache_t *sighand_cachep;
1370 /* SLAB cache for files_struct structures (tsk->files) */
1371 kmem_cache_t *files_cachep;
1373 /* SLAB cache for fs_struct structures (tsk->fs) */
1374 kmem_cache_t *fs_cachep;
1376 /* SLAB cache for vm_area_struct structures */
1377 kmem_cache_t *vm_area_cachep;
1379 /* SLAB cache for mm_struct structures (tsk->mm) */
1380 kmem_cache_t *mm_cachep;
1382 void __init proc_caches_init(void)
1384 sighand_cachep = kmem_cache_create("sighand_cache",
1385 sizeof(struct sighand_struct), 0,
1386 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1387 signal_cachep = kmem_cache_create("signal_cache",
1388 sizeof(struct signal_struct), 0,
1389 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1390 files_cachep = kmem_cache_create("files_cache",
1391 sizeof(struct files_struct), 0,
1392 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1393 fs_cachep = kmem_cache_create("fs_cache",
1394 sizeof(struct fs_struct), 0,
1395 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1396 vm_area_cachep = kmem_cache_create("vm_area_struct",
1397 sizeof(struct vm_area_struct), 0,
1398 SLAB_PANIC, NULL, NULL);
1399 mm_cachep = kmem_cache_create("mm_struct",
1400 sizeof(struct mm_struct), 0,
1401 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);