Get it to compile again ...
[linux-2.6/linux-mips.git] / kernel / fork.c
blob23c6d34f800f7324204eff19e61024125a1bcb99
1 /*
2 * linux/kernel/fork.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
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>
27 #include <linux/fs.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 static kmem_cache_t *task_struct_cachep;
43 extern int copy_semundo(unsigned long clone_flags, struct task_struct *tsk);
44 extern void exit_semundo(struct task_struct *tsk);
46 /* The idle threads do not count..
47 * Protected by write_lock_irq(&tasklist_lock)
49 int nr_threads;
51 int max_threads;
52 unsigned long total_forks; /* Handle normal Linux uptimes. */
54 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
56 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED; /* outer */
59 * A per-CPU task cache - this relies on the fact that
60 * the very last portion of sys_exit() is executed with
61 * preemption turned off.
63 static task_t *task_cache[NR_CPUS] __cacheline_aligned;
65 int nr_processes(void)
67 int cpu;
68 int total = 0;
70 for (cpu = 0; cpu < NR_CPUS; cpu++) {
71 if (cpu_online(cpu))
72 total += per_cpu(process_counts, cpu);
74 return total;
77 static void free_task_struct(struct task_struct *tsk)
80 * The task cache is effectively disabled right now.
81 * Do we want it? The slab cache already has per-cpu
82 * stuff, but the thread info (usually a order-1 page
83 * allocation) doesn't.
85 if (tsk != current) {
86 free_thread_info(tsk->thread_info);
87 kmem_cache_free(task_struct_cachep,tsk);
88 } else {
89 int cpu = get_cpu();
91 tsk = task_cache[cpu];
92 if (tsk) {
93 free_thread_info(tsk->thread_info);
94 kmem_cache_free(task_struct_cachep,tsk);
96 task_cache[cpu] = current;
97 put_cpu();
101 void __put_task_struct(struct task_struct *tsk)
103 WARN_ON(!(tsk->state & (TASK_DEAD | TASK_ZOMBIE)));
104 WARN_ON(atomic_read(&tsk->usage));
105 WARN_ON(tsk == current);
107 security_task_free(tsk);
108 free_uid(tsk->user);
109 free_task_struct(tsk);
112 void add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
114 unsigned long flags;
116 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
117 spin_lock_irqsave(&q->lock, flags);
118 __add_wait_queue(q, wait);
119 spin_unlock_irqrestore(&q->lock, flags);
122 void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
124 unsigned long flags;
126 wait->flags |= WQ_FLAG_EXCLUSIVE;
127 spin_lock_irqsave(&q->lock, flags);
128 __add_wait_queue_tail(q, wait);
129 spin_unlock_irqrestore(&q->lock, flags);
132 void remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
134 unsigned long flags;
136 spin_lock_irqsave(&q->lock, flags);
137 __remove_wait_queue(q, wait);
138 spin_unlock_irqrestore(&q->lock, flags);
141 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
143 unsigned long flags;
145 __set_current_state(state);
146 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
147 spin_lock_irqsave(&q->lock, flags);
148 if (list_empty(&wait->task_list))
149 __add_wait_queue(q, wait);
150 spin_unlock_irqrestore(&q->lock, flags);
153 void
154 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
156 unsigned long flags;
158 __set_current_state(state);
159 wait->flags |= WQ_FLAG_EXCLUSIVE;
160 spin_lock_irqsave(&q->lock, flags);
161 if (list_empty(&wait->task_list))
162 __add_wait_queue_tail(q, wait);
163 spin_unlock_irqrestore(&q->lock, flags);
166 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
168 unsigned long flags;
170 __set_current_state(TASK_RUNNING);
171 if (!list_empty(&wait->task_list)) {
172 spin_lock_irqsave(&q->lock, flags);
173 list_del_init(&wait->task_list);
174 spin_unlock_irqrestore(&q->lock, flags);
178 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync)
180 int ret = default_wake_function(wait, mode, sync);
182 if (ret)
183 list_del_init(&wait->task_list);
184 return ret;
187 void __init fork_init(unsigned long mempages)
189 /* create a slab on which task_structs can be allocated */
190 task_struct_cachep =
191 kmem_cache_create("task_struct",
192 sizeof(struct task_struct),0,
193 SLAB_MUST_HWCACHE_ALIGN, NULL, NULL);
194 if (!task_struct_cachep)
195 panic("fork_init(): cannot create task_struct SLAB cache");
198 * The default maximum number of threads is set to a safe
199 * value: the thread structures can take up at most half
200 * of memory.
202 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8;
204 * we need to allow at least 20 threads to boot a system
206 if(max_threads < 20)
207 max_threads = 20;
209 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
210 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
213 static struct task_struct *dup_task_struct(struct task_struct *orig)
215 struct task_struct *tsk;
216 struct thread_info *ti;
217 int cpu = get_cpu();
219 prepare_to_copy(orig);
221 tsk = task_cache[cpu];
222 task_cache[cpu] = NULL;
223 put_cpu();
224 if (!tsk) {
225 ti = alloc_thread_info();
226 if (!ti)
227 return NULL;
229 tsk = kmem_cache_alloc(task_struct_cachep, GFP_KERNEL);
230 if (!tsk) {
231 free_thread_info(ti);
232 return NULL;
234 } else
235 ti = tsk->thread_info;
237 *ti = *orig->thread_info;
238 *tsk = *orig;
239 tsk->thread_info = ti;
240 ti->task = tsk;
242 /* One for us, one for whoever does the "release_task()" (usually parent) */
243 atomic_set(&tsk->usage,2);
244 return tsk;
247 #ifdef CONFIG_MMU
248 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
250 struct vm_area_struct * mpnt, *tmp, **pprev;
251 int retval;
252 unsigned long charge = 0;
254 down_write(&oldmm->mmap_sem);
255 flush_cache_mm(current->mm);
256 mm->locked_vm = 0;
257 mm->mmap = NULL;
258 mm->mmap_cache = NULL;
259 mm->free_area_cache = TASK_UNMAPPED_BASE;
260 mm->map_count = 0;
261 mm->rss = 0;
262 mm->cpu_vm_mask = 0;
263 pprev = &mm->mmap;
266 * Add it to the mmlist after the parent.
267 * Doing it this way means that we can order the list,
268 * and fork() won't mess up the ordering significantly.
269 * Add it first so that swapoff can see any swap entries.
271 spin_lock(&mmlist_lock);
272 list_add(&mm->mmlist, &current->mm->mmlist);
273 mmlist_nr++;
274 spin_unlock(&mmlist_lock);
276 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
277 struct file *file;
279 if(mpnt->vm_flags & VM_DONTCOPY)
280 continue;
281 if (mpnt->vm_flags & VM_ACCOUNT) {
282 unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
283 if (!vm_enough_memory(len))
284 goto fail_nomem;
285 charge += len;
287 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
288 if (!tmp)
289 goto fail_nomem;
290 *tmp = *mpnt;
291 tmp->vm_flags &= ~VM_LOCKED;
292 tmp->vm_mm = mm;
293 tmp->vm_next = NULL;
294 file = tmp->vm_file;
295 INIT_LIST_HEAD(&tmp->shared);
296 if (file) {
297 struct inode *inode = file->f_dentry->d_inode;
298 get_file(file);
299 if (tmp->vm_flags & VM_DENYWRITE)
300 atomic_dec(&inode->i_writecount);
302 /* insert tmp into the share list, just after mpnt */
303 down(&inode->i_mapping->i_shared_sem);
304 list_add_tail(&tmp->shared, &mpnt->shared);
305 up(&inode->i_mapping->i_shared_sem);
309 * Link in the new vma and copy the page table entries:
310 * link in first so that swapoff can see swap entries.
312 spin_lock(&mm->page_table_lock);
313 *pprev = tmp;
314 pprev = &tmp->vm_next;
315 mm->map_count++;
316 retval = copy_page_range(mm, current->mm, tmp);
317 spin_unlock(&mm->page_table_lock);
319 if (tmp->vm_ops && tmp->vm_ops->open)
320 tmp->vm_ops->open(tmp);
322 if (retval)
323 goto fail;
325 retval = 0;
326 build_mmap_rb(mm);
328 out:
329 flush_tlb_mm(current->mm);
330 up_write(&oldmm->mmap_sem);
331 return retval;
332 fail_nomem:
333 retval = -ENOMEM;
334 fail:
335 vm_unacct_memory(charge);
336 goto out;
338 static inline int mm_alloc_pgd(struct mm_struct * mm)
340 mm->pgd = pgd_alloc(mm);
341 if (unlikely(!mm->pgd))
342 return -ENOMEM;
343 return 0;
346 static inline void mm_free_pgd(struct mm_struct * mm)
348 pgd_free(mm->pgd);
350 #else
351 #define dup_mmap(mm, oldmm) (0)
352 #define mm_alloc_pgd(mm) (0)
353 #define mm_free_pgd(mm)
354 #endif /* CONFIG_MMU */
356 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
357 int mmlist_nr;
359 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
360 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
362 #include <linux/init_task.h>
364 static struct mm_struct * mm_init(struct mm_struct * mm)
366 atomic_set(&mm->mm_users, 1);
367 atomic_set(&mm->mm_count, 1);
368 init_rwsem(&mm->mmap_sem);
369 mm->core_waiters = 0;
370 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
371 mm->ioctx_list_lock = RW_LOCK_UNLOCKED;
372 mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
373 mm->free_area_cache = TASK_UNMAPPED_BASE;
375 if (likely(!mm_alloc_pgd(mm))) {
376 mm->def_flags = 0;
377 return mm;
379 free_mm(mm);
380 return NULL;
384 * Allocate and initialize an mm_struct.
386 struct mm_struct * mm_alloc(void)
388 struct mm_struct * mm;
390 mm = allocate_mm();
391 if (mm) {
392 memset(mm, 0, sizeof(*mm));
393 return mm_init(mm);
395 return NULL;
399 * Called when the last reference to the mm
400 * is dropped: either by a lazy thread or by
401 * mmput. Free the page directory and the mm.
403 inline void __mmdrop(struct mm_struct *mm)
405 BUG_ON(mm == &init_mm);
406 mm_free_pgd(mm);
407 destroy_context(mm);
408 free_mm(mm);
412 * Decrement the use count and release all resources for an mm.
414 void mmput(struct mm_struct *mm)
416 if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)) {
417 list_del(&mm->mmlist);
418 mmlist_nr--;
419 spin_unlock(&mmlist_lock);
420 exit_aio(mm);
421 exit_mmap(mm);
422 mmdrop(mm);
426 /* Please note the differences between mmput and mm_release.
427 * mmput is called whenever we stop holding onto a mm_struct,
428 * error success whatever.
430 * mm_release is called after a mm_struct has been removed
431 * from the current process.
433 * This difference is important for error handling, when we
434 * only half set up a mm_struct for a new process and need to restore
435 * the old one. Because we mmput the new mm_struct before
436 * restoring the old one. . .
437 * Eric Biederman 10 January 1998
439 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
441 struct completion *vfork_done = tsk->vfork_done;
443 /* Get rid of any cached register state */
444 deactivate_mm(tsk, mm);
446 /* notify parent sleeping on vfork() */
447 if (vfork_done) {
448 tsk->vfork_done = NULL;
449 complete(vfork_done);
451 if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
452 u32 __user * tidptr = tsk->clear_child_tid;
453 tsk->clear_child_tid = NULL;
456 * We don't check the error code - if userspace has
457 * not set up a proper pointer then tough luck.
459 put_user(0, tidptr);
460 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL);
464 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
466 struct mm_struct * mm, *oldmm;
467 int retval;
469 tsk->min_flt = tsk->maj_flt = 0;
470 tsk->cmin_flt = tsk->cmaj_flt = 0;
471 tsk->nswap = tsk->cnswap = 0;
473 tsk->mm = NULL;
474 tsk->active_mm = NULL;
477 * Are we cloning a kernel thread?
479 * We need to steal a active VM for that..
481 oldmm = current->mm;
482 if (!oldmm)
483 return 0;
485 if (clone_flags & CLONE_VM) {
486 atomic_inc(&oldmm->mm_users);
487 mm = oldmm;
489 * There are cases where the PTL is held to ensure no
490 * new threads start up in user mode using an mm, which
491 * allows optimizing out ipis; the tlb_gather_mmu code
492 * is an example.
494 spin_unlock_wait(&oldmm->page_table_lock);
495 goto good_mm;
498 retval = -ENOMEM;
499 mm = allocate_mm();
500 if (!mm)
501 goto fail_nomem;
503 /* Copy the current MM stuff.. */
504 memcpy(mm, oldmm, sizeof(*mm));
505 if (!mm_init(mm))
506 goto fail_nomem;
508 if (init_new_context(tsk,mm))
509 goto free_pt;
511 retval = dup_mmap(mm, oldmm);
512 if (retval)
513 goto free_pt;
515 good_mm:
516 tsk->mm = mm;
517 tsk->active_mm = mm;
518 return 0;
520 free_pt:
521 mmput(mm);
522 fail_nomem:
523 return retval;
526 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
528 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
529 /* We don't need to lock fs - think why ;-) */
530 if (fs) {
531 atomic_set(&fs->count, 1);
532 fs->lock = RW_LOCK_UNLOCKED;
533 fs->umask = old->umask;
534 read_lock(&old->lock);
535 fs->rootmnt = mntget(old->rootmnt);
536 fs->root = dget(old->root);
537 fs->pwdmnt = mntget(old->pwdmnt);
538 fs->pwd = dget(old->pwd);
539 if (old->altroot) {
540 fs->altrootmnt = mntget(old->altrootmnt);
541 fs->altroot = dget(old->altroot);
542 } else {
543 fs->altrootmnt = NULL;
544 fs->altroot = NULL;
546 read_unlock(&old->lock);
548 return fs;
551 struct fs_struct *copy_fs_struct(struct fs_struct *old)
553 return __copy_fs_struct(old);
556 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
558 if (clone_flags & CLONE_FS) {
559 atomic_inc(&current->fs->count);
560 return 0;
562 tsk->fs = __copy_fs_struct(current->fs);
563 if (!tsk->fs)
564 return -ENOMEM;
565 return 0;
568 static int count_open_files(struct files_struct *files, int size)
570 int i;
572 /* Find the last open fd */
573 for (i = size/(8*sizeof(long)); i > 0; ) {
574 if (files->open_fds->fds_bits[--i])
575 break;
577 i = (i+1) * 8 * sizeof(long);
578 return i;
581 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
583 struct files_struct *oldf, *newf;
584 struct file **old_fds, **new_fds;
585 int open_files, nfds, size, i, error = 0;
588 * A background process may not have any files ...
590 oldf = current->files;
591 if (!oldf)
592 goto out;
594 if (clone_flags & CLONE_FILES) {
595 atomic_inc(&oldf->count);
596 goto out;
599 tsk->files = NULL;
600 error = -ENOMEM;
601 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
602 if (!newf)
603 goto out;
605 atomic_set(&newf->count, 1);
607 newf->file_lock = SPIN_LOCK_UNLOCKED;
608 newf->next_fd = 0;
609 newf->max_fds = NR_OPEN_DEFAULT;
610 newf->max_fdset = __FD_SETSIZE;
611 newf->close_on_exec = &newf->close_on_exec_init;
612 newf->open_fds = &newf->open_fds_init;
613 newf->fd = &newf->fd_array[0];
615 /* We don't yet have the oldf readlock, but even if the old
616 fdset gets grown now, we'll only copy up to "size" fds */
617 size = oldf->max_fdset;
618 if (size > __FD_SETSIZE) {
619 newf->max_fdset = 0;
620 spin_lock(&newf->file_lock);
621 error = expand_fdset(newf, size-1);
622 spin_unlock(&newf->file_lock);
623 if (error)
624 goto out_release;
626 spin_lock(&oldf->file_lock);
628 open_files = count_open_files(oldf, size);
631 * Check whether we need to allocate a larger fd array.
632 * Note: we're not a clone task, so the open count won't
633 * change.
635 nfds = NR_OPEN_DEFAULT;
636 if (open_files > nfds) {
637 spin_unlock(&oldf->file_lock);
638 newf->max_fds = 0;
639 spin_lock(&newf->file_lock);
640 error = expand_fd_array(newf, open_files-1);
641 spin_unlock(&newf->file_lock);
642 if (error)
643 goto out_release;
644 nfds = newf->max_fds;
645 spin_lock(&oldf->file_lock);
648 old_fds = oldf->fd;
649 new_fds = newf->fd;
651 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
652 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
654 for (i = open_files; i != 0; i--) {
655 struct file *f = *old_fds++;
656 if (f)
657 get_file(f);
658 *new_fds++ = f;
660 spin_unlock(&oldf->file_lock);
662 /* compute the remainder to be cleared */
663 size = (newf->max_fds - open_files) * sizeof(struct file *);
665 /* This is long word aligned thus could use a optimized version */
666 memset(new_fds, 0, size);
668 if (newf->max_fdset > open_files) {
669 int left = (newf->max_fdset-open_files)/8;
670 int start = open_files / (8 * sizeof(unsigned long));
672 memset(&newf->open_fds->fds_bits[start], 0, left);
673 memset(&newf->close_on_exec->fds_bits[start], 0, left);
676 tsk->files = newf;
677 error = 0;
678 out:
679 return error;
681 out_release:
682 free_fdset (newf->close_on_exec, newf->max_fdset);
683 free_fdset (newf->open_fds, newf->max_fdset);
684 kmem_cache_free(files_cachep, newf);
685 goto out;
688 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
690 struct sighand_struct *sig;
692 if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
693 atomic_inc(&current->sighand->count);
694 return 0;
696 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
697 tsk->sighand = sig;
698 if (!sig)
699 return -ENOMEM;
700 spin_lock_init(&sig->siglock);
701 atomic_set(&sig->count, 1);
702 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
703 return 0;
706 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
708 struct signal_struct *sig;
710 if (clone_flags & CLONE_THREAD) {
711 atomic_inc(&current->signal->count);
712 return 0;
714 sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
715 tsk->signal = sig;
716 if (!sig)
717 return -ENOMEM;
718 atomic_set(&sig->count, 1);
719 sig->group_exit = 0;
720 sig->group_exit_code = 0;
721 sig->group_exit_task = NULL;
722 sig->group_stop_count = 0;
723 sig->curr_target = NULL;
724 init_sigpending(&sig->shared_pending);
726 return 0;
729 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
731 unsigned long new_flags = p->flags;
733 new_flags &= ~PF_SUPERPRIV;
734 new_flags |= PF_FORKNOEXEC;
735 if (!(clone_flags & CLONE_PTRACE))
736 p->ptrace = 0;
737 p->flags = new_flags;
740 asmlinkage long sys_set_tid_address(int __user *tidptr)
742 current->clear_child_tid = tidptr;
744 return current->pid;
748 * This creates a new process as a copy of the old one,
749 * but does not actually start it yet.
751 * It copies the registers, and all the appropriate
752 * parts of the process environment (as per the clone
753 * flags). The actual kick-off is left to the caller.
755 struct task_struct *copy_process(unsigned long clone_flags,
756 unsigned long stack_start,
757 struct pt_regs *regs,
758 unsigned long stack_size,
759 int __user *parent_tidptr,
760 int __user *child_tidptr)
762 int retval;
763 struct task_struct *p = NULL;
765 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
766 return ERR_PTR(-EINVAL);
769 * Thread groups must share signals as well, and detached threads
770 * can only be started up within the thread group.
772 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
773 return ERR_PTR(-EINVAL);
774 if ((clone_flags & CLONE_DETACHED) && !(clone_flags & CLONE_THREAD))
775 return ERR_PTR(-EINVAL);
777 retval = security_task_create(clone_flags);
778 if (retval)
779 goto fork_out;
781 retval = -ENOMEM;
782 p = dup_task_struct(current);
783 if (!p)
784 goto fork_out;
786 retval = -EAGAIN;
787 if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur) {
788 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE))
789 goto bad_fork_free;
792 atomic_inc(&p->user->__count);
793 atomic_inc(&p->user->processes);
796 * If multiple threads are within copy_process(), then this check
797 * triggers too late. This doesn't hurt, the check is only there
798 * to stop root fork bombs.
800 if (nr_threads >= max_threads)
801 goto bad_fork_cleanup_count;
803 if (!try_module_get(p->thread_info->exec_domain->module))
804 goto bad_fork_cleanup_count;
806 if (p->binfmt && !try_module_get(p->binfmt->module))
807 goto bad_fork_cleanup_put_domain;
809 #ifdef CONFIG_PREEMPT
811 * schedule_tail drops this_rq()->lock so we compensate with a count
812 * of 1. Also, we want to start with kernel preemption disabled.
814 p->thread_info->preempt_count = 1;
815 #endif
816 p->did_exec = 0;
817 p->state = TASK_UNINTERRUPTIBLE;
819 copy_flags(clone_flags, p);
820 if (clone_flags & CLONE_IDLETASK)
821 p->pid = 0;
822 else {
823 p->pid = alloc_pidmap();
824 if (p->pid == -1)
825 goto bad_fork_cleanup;
827 retval = -EFAULT;
828 if (clone_flags & CLONE_PARENT_SETTID)
829 if (put_user(p->pid, parent_tidptr))
830 goto bad_fork_cleanup;
832 p->proc_dentry = NULL;
834 INIT_LIST_HEAD(&p->run_list);
836 INIT_LIST_HEAD(&p->children);
837 INIT_LIST_HEAD(&p->sibling);
838 INIT_LIST_HEAD(&p->posix_timers);
839 init_waitqueue_head(&p->wait_chldexit);
840 p->vfork_done = NULL;
841 spin_lock_init(&p->alloc_lock);
842 spin_lock_init(&p->switch_lock);
844 clear_tsk_thread_flag(p, TIF_SIGPENDING);
845 init_sigpending(&p->pending);
847 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
848 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
849 init_timer(&p->real_timer);
850 p->real_timer.data = (unsigned long) p;
852 p->leader = 0; /* session leadership doesn't inherit */
853 p->tty_old_pgrp = 0;
854 p->utime = p->stime = 0;
855 p->cutime = p->cstime = 0;
856 p->array = NULL;
857 p->lock_depth = -1; /* -1 = no lock */
858 p->start_time = get_jiffies_64();
859 p->security = NULL;
861 retval = -ENOMEM;
862 if ((retval = security_task_alloc(p)))
863 goto bad_fork_cleanup;
864 /* copy all the process information */
865 if ((retval = copy_semundo(clone_flags, p)))
866 goto bad_fork_cleanup_security;
867 if ((retval = copy_files(clone_flags, p)))
868 goto bad_fork_cleanup_semundo;
869 if ((retval = copy_fs(clone_flags, p)))
870 goto bad_fork_cleanup_files;
871 if ((retval = copy_sighand(clone_flags, p)))
872 goto bad_fork_cleanup_fs;
873 if ((retval = copy_signal(clone_flags, p)))
874 goto bad_fork_cleanup_sighand;
875 if ((retval = copy_mm(clone_flags, p)))
876 goto bad_fork_cleanup_signal;
877 if ((retval = copy_namespace(clone_flags, p)))
878 goto bad_fork_cleanup_mm;
879 retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
880 if (retval)
881 goto bad_fork_cleanup_namespace;
883 if (clone_flags & CLONE_CHILD_SETTID)
884 p->set_child_tid = child_tidptr;
886 * Clear TID on mm_release()?
888 if (clone_flags & CLONE_CHILD_CLEARTID)
889 p->clear_child_tid = child_tidptr;
892 * Syscall tracing should be turned off in the child regardless
893 * of CLONE_PTRACE.
895 clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
897 /* Our parent execution domain becomes current domain
898 These must match for thread signalling to apply */
900 p->parent_exec_id = p->self_exec_id;
902 /* ok, now we should be set up.. */
903 if (clone_flags & CLONE_DETACHED)
904 p->exit_signal = -1;
905 else
906 p->exit_signal = clone_flags & CSIGNAL;
907 p->pdeath_signal = 0;
910 * Share the timeslice between parent and child, thus the
911 * total amount of pending timeslices in the system doesn't change,
912 * resulting in more scheduling fairness.
914 local_irq_disable();
915 p->time_slice = (current->time_slice + 1) >> 1;
917 * The remainder of the first timeslice might be recovered by
918 * the parent if the child exits early enough.
920 p->first_time_slice = 1;
921 current->time_slice >>= 1;
922 p->last_run = jiffies;
923 if (!current->time_slice) {
925 * This case is rare, it happens when the parent has only
926 * a single jiffy left from its timeslice. Taking the
927 * runqueue lock is not a problem.
929 current->time_slice = 1;
930 preempt_disable();
931 scheduler_tick(0, 0);
932 local_irq_enable();
933 preempt_enable();
934 } else
935 local_irq_enable();
937 * Ok, add it to the run-queues and make it
938 * visible to the rest of the system.
940 * Let it rip!
942 p->tgid = p->pid;
943 p->group_leader = p;
944 INIT_LIST_HEAD(&p->ptrace_children);
945 INIT_LIST_HEAD(&p->ptrace_list);
947 /* Need tasklist lock for parent etc handling! */
948 write_lock_irq(&tasklist_lock);
950 * Check for pending SIGKILL! The new thread should not be allowed
951 * to slip out of an OOM kill. (or normal SIGKILL.)
953 if (sigismember(&current->pending.signal, SIGKILL)) {
954 write_unlock_irq(&tasklist_lock);
955 retval = -EINTR;
956 goto bad_fork_cleanup_namespace;
959 /* CLONE_PARENT re-uses the old parent */
960 if (clone_flags & CLONE_PARENT)
961 p->real_parent = current->real_parent;
962 else
963 p->real_parent = current;
964 p->parent = p->real_parent;
966 if (clone_flags & CLONE_THREAD) {
967 spin_lock(&current->sighand->siglock);
969 * Important: if an exit-all has been started then
970 * do not create this new thread - the whole thread
971 * group is supposed to exit anyway.
973 if (current->signal->group_exit) {
974 spin_unlock(&current->sighand->siglock);
975 write_unlock_irq(&tasklist_lock);
976 goto bad_fork_cleanup_namespace;
978 p->tgid = current->tgid;
979 p->group_leader = current->group_leader;
981 if (current->signal->group_stop_count > 0) {
983 * There is an all-stop in progress for the group.
984 * We ourselves will stop as soon as we check signals.
985 * Make the new thread part of that group stop too.
987 current->signal->group_stop_count++;
988 set_tsk_thread_flag(p, TIF_SIGPENDING);
991 spin_unlock(&current->sighand->siglock);
994 SET_LINKS(p);
995 if (p->ptrace & PT_PTRACED)
996 __ptrace_link(p, current->parent);
998 attach_pid(p, PIDTYPE_PID, p->pid);
999 if (thread_group_leader(p)) {
1000 attach_pid(p, PIDTYPE_TGID, p->tgid);
1001 attach_pid(p, PIDTYPE_PGID, p->pgrp);
1002 attach_pid(p, PIDTYPE_SID, p->session);
1003 if (p->pid)
1004 per_cpu(process_counts, smp_processor_id())++;
1005 } else
1006 link_pid(p, p->pids + PIDTYPE_TGID, &p->group_leader->pids[PIDTYPE_TGID].pid);
1008 nr_threads++;
1009 write_unlock_irq(&tasklist_lock);
1010 retval = 0;
1012 fork_out:
1013 if (retval)
1014 return ERR_PTR(retval);
1015 return p;
1017 bad_fork_cleanup_namespace:
1018 exit_namespace(p);
1019 bad_fork_cleanup_mm:
1020 exit_mm(p);
1021 bad_fork_cleanup_signal:
1022 exit_signal(p);
1023 bad_fork_cleanup_sighand:
1024 exit_sighand(p);
1025 bad_fork_cleanup_fs:
1026 exit_fs(p); /* blocking */
1027 bad_fork_cleanup_files:
1028 exit_files(p); /* blocking */
1029 bad_fork_cleanup_semundo:
1030 exit_semundo(p);
1031 bad_fork_cleanup_security:
1032 security_task_free(p);
1033 bad_fork_cleanup:
1034 if (p->pid > 0)
1035 free_pidmap(p->pid);
1036 if (p->binfmt)
1037 module_put(p->binfmt->module);
1038 bad_fork_cleanup_put_domain:
1039 module_put(p->thread_info->exec_domain->module);
1040 bad_fork_cleanup_count:
1041 atomic_dec(&p->user->processes);
1042 free_uid(p->user);
1043 bad_fork_free:
1044 free_task_struct(p);
1045 goto fork_out;
1048 static inline int fork_traceflag (unsigned clone_flags)
1050 if (clone_flags & (CLONE_UNTRACED | CLONE_IDLETASK))
1051 return 0;
1052 else if (clone_flags & CLONE_VFORK) {
1053 if (current->ptrace & PT_TRACE_VFORK)
1054 return PTRACE_EVENT_VFORK;
1055 } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1056 if (current->ptrace & PT_TRACE_CLONE)
1057 return PTRACE_EVENT_CLONE;
1058 } else if (current->ptrace & PT_TRACE_FORK)
1059 return PTRACE_EVENT_FORK;
1061 return 0;
1065 * Ok, this is the main fork-routine.
1067 * It copies the process, and if successful kick-starts
1068 * it and waits for it to finish using the VM if required.
1070 long do_fork(unsigned long clone_flags,
1071 unsigned long stack_start,
1072 struct pt_regs *regs,
1073 unsigned long stack_size,
1074 int __user *parent_tidptr,
1075 int __user *child_tidptr)
1077 struct task_struct *p;
1078 int trace = 0;
1079 long pid;
1081 if (unlikely(current->ptrace)) {
1082 trace = fork_traceflag (clone_flags);
1083 if (trace)
1084 clone_flags |= CLONE_PTRACE;
1087 p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr);
1089 * Do this prior waking up the new thread - the thread pointer
1090 * might get invalid after that point, if the thread exits quickly.
1092 pid = IS_ERR(p) ? PTR_ERR(p) : p->pid;
1094 if (!IS_ERR(p)) {
1095 struct completion vfork;
1097 if (clone_flags & CLONE_VFORK) {
1098 p->vfork_done = &vfork;
1099 init_completion(&vfork);
1102 if (p->ptrace & PT_PTRACED) {
1104 * We'll start up with an immediate SIGSTOP.
1106 sigaddset(&p->pending.signal, SIGSTOP);
1107 set_tsk_thread_flag(p, TIF_SIGPENDING);
1110 wake_up_forked_process(p); /* do this last */
1111 ++total_forks;
1113 if (unlikely (trace)) {
1114 current->ptrace_message = pid;
1115 ptrace_notify ((trace << 8) | SIGTRAP);
1118 if (clone_flags & CLONE_VFORK) {
1119 wait_for_completion(&vfork);
1120 if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1121 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1122 } else
1124 * Let the child process run first, to avoid most of the
1125 * COW overhead when the child exec()s afterwards.
1127 set_need_resched();
1129 return pid;
1132 /* SLAB cache for signal_struct structures (tsk->signal) */
1133 kmem_cache_t *signal_cachep;
1135 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1136 kmem_cache_t *sighand_cachep;
1138 /* SLAB cache for files_struct structures (tsk->files) */
1139 kmem_cache_t *files_cachep;
1141 /* SLAB cache for fs_struct structures (tsk->fs) */
1142 kmem_cache_t *fs_cachep;
1144 /* SLAB cache for vm_area_struct structures */
1145 kmem_cache_t *vm_area_cachep;
1147 /* SLAB cache for mm_struct structures (tsk->mm) */
1148 kmem_cache_t *mm_cachep;
1150 void __init proc_caches_init(void)
1152 sighand_cachep = kmem_cache_create("sighand_cache",
1153 sizeof(struct sighand_struct), 0,
1154 SLAB_HWCACHE_ALIGN, NULL, NULL);
1155 if (!sighand_cachep)
1156 panic("Cannot create sighand SLAB cache");
1158 signal_cachep = kmem_cache_create("signal_cache",
1159 sizeof(struct signal_struct), 0,
1160 SLAB_HWCACHE_ALIGN, NULL, NULL);
1161 if (!signal_cachep)
1162 panic("Cannot create signal SLAB cache");
1164 files_cachep = kmem_cache_create("files_cache",
1165 sizeof(struct files_struct), 0,
1166 SLAB_HWCACHE_ALIGN, NULL, NULL);
1167 if (!files_cachep)
1168 panic("Cannot create files SLAB cache");
1170 fs_cachep = kmem_cache_create("fs_cache",
1171 sizeof(struct fs_struct), 0,
1172 SLAB_HWCACHE_ALIGN, NULL, NULL);
1173 if (!fs_cachep)
1174 panic("Cannot create fs_struct SLAB cache");
1176 vm_area_cachep = kmem_cache_create("vm_area_struct",
1177 sizeof(struct vm_area_struct), 0,
1178 0, NULL, NULL);
1179 if(!vm_area_cachep)
1180 panic("vma_init: Cannot alloc vm_area_struct SLAB cache");
1182 mm_cachep = kmem_cache_create("mm_struct",
1183 sizeof(struct mm_struct), 0,
1184 SLAB_HWCACHE_ALIGN, NULL, NULL);
1185 if(!mm_cachep)
1186 panic("vma_init: Cannot alloc mm_struct SLAB cache");