- pre3:
[davej-history.git] / kernel / fork.c
blob07e0e96852d16a7b3f7e8e81915f0b09aa8f45ef
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_tables()'
14 #include <linux/config.h>
15 #include <linux/malloc.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>
22 #include <asm/pgtable.h>
23 #include <asm/pgalloc.h>
24 #include <asm/uaccess.h>
25 #include <asm/mmu_context.h>
27 /* The idle threads do not count.. */
28 int nr_threads;
29 int nr_running;
31 int max_threads;
32 unsigned long total_forks; /* Handle normal Linux uptimes. */
33 int last_pid;
35 struct task_struct *pidhash[PIDHASH_SZ];
37 void add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
39 unsigned long flags;
41 wq_write_lock_irqsave(&q->lock, flags);
42 __add_wait_queue(q, wait);
43 wq_write_unlock_irqrestore(&q->lock, flags);
46 void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
48 unsigned long flags;
50 wq_write_lock_irqsave(&q->lock, flags);
51 __add_wait_queue_tail(q, wait);
52 wq_write_unlock_irqrestore(&q->lock, flags);
55 void remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
57 unsigned long flags;
59 wq_write_lock_irqsave(&q->lock, flags);
60 __remove_wait_queue(q, wait);
61 wq_write_unlock_irqrestore(&q->lock, flags);
64 void __init fork_init(unsigned long mempages)
67 * The default maximum number of threads is set to a safe
68 * value: the thread structures can take up at most half
69 * of memory.
71 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 2;
73 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
74 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
77 /* Protects next_safe and last_pid. */
78 spinlock_t lastpid_lock = SPIN_LOCK_UNLOCKED;
80 static int get_pid(unsigned long flags)
82 static int next_safe = PID_MAX;
83 struct task_struct *p;
85 if (flags & CLONE_PID)
86 return current->pid;
88 spin_lock(&lastpid_lock);
89 if((++last_pid) & 0xffff8000) {
90 last_pid = 300; /* Skip daemons etc. */
91 goto inside;
93 if(last_pid >= next_safe) {
94 inside:
95 next_safe = PID_MAX;
96 read_lock(&tasklist_lock);
97 repeat:
98 for_each_task(p) {
99 if(p->pid == last_pid ||
100 p->pgrp == last_pid ||
101 p->session == last_pid) {
102 if(++last_pid >= next_safe) {
103 if(last_pid & 0xffff8000)
104 last_pid = 300;
105 next_safe = PID_MAX;
107 goto repeat;
109 if(p->pid > last_pid && next_safe > p->pid)
110 next_safe = p->pid;
111 if(p->pgrp > last_pid && next_safe > p->pgrp)
112 next_safe = p->pgrp;
113 if(p->session > last_pid && next_safe > p->session)
114 next_safe = p->session;
116 read_unlock(&tasklist_lock);
118 spin_unlock(&lastpid_lock);
120 return last_pid;
123 static inline int dup_mmap(struct mm_struct * mm)
125 struct vm_area_struct * mpnt, *tmp, **pprev;
126 int retval;
128 flush_cache_mm(current->mm);
129 mm->locked_vm = 0;
130 mm->mmap = NULL;
131 mm->mmap_avl = NULL;
132 mm->mmap_cache = NULL;
133 mm->map_count = 0;
134 mm->context = 0;
135 mm->cpu_vm_mask = 0;
136 mm->swap_cnt = 0;
137 mm->swap_address = 0;
138 mm->segments = NULL;
139 pprev = &mm->mmap;
140 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
141 struct file *file;
143 retval = -ENOMEM;
144 if(mpnt->vm_flags & VM_DONTCOPY)
145 continue;
146 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
147 if (!tmp)
148 goto fail_nomem;
149 *tmp = *mpnt;
150 tmp->vm_flags &= ~VM_LOCKED;
151 tmp->vm_mm = mm;
152 mm->map_count++;
153 tmp->vm_next = NULL;
154 file = tmp->vm_file;
155 if (file) {
156 struct inode *inode = file->f_dentry->d_inode;
157 get_file(file);
158 if (tmp->vm_flags & VM_DENYWRITE)
159 atomic_dec(&inode->i_writecount);
161 /* insert tmp into the share list, just after mpnt */
162 spin_lock(&inode->i_mapping->i_shared_lock);
163 if((tmp->vm_next_share = mpnt->vm_next_share) != NULL)
164 mpnt->vm_next_share->vm_pprev_share =
165 &tmp->vm_next_share;
166 mpnt->vm_next_share = tmp;
167 tmp->vm_pprev_share = &mpnt->vm_next_share;
168 spin_unlock(&inode->i_mapping->i_shared_lock);
171 /* Copy the pages, but defer checking for errors */
172 retval = copy_page_range(mm, current->mm, tmp);
173 if (!retval && tmp->vm_ops && tmp->vm_ops->open)
174 tmp->vm_ops->open(tmp);
177 * Link in the new vma even if an error occurred,
178 * so that exit_mmap() can clean up the mess.
180 tmp->vm_next = *pprev;
181 *pprev = tmp;
183 pprev = &tmp->vm_next;
184 if (retval)
185 goto fail_nomem;
187 retval = 0;
188 if (mm->map_count >= AVL_MIN_MAP_COUNT)
189 build_mmap_avl(mm);
191 fail_nomem:
192 flush_tlb_mm(current->mm);
193 return retval;
196 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
198 static struct mm_struct * mm_init(struct mm_struct * mm)
200 atomic_set(&mm->mm_users, 1);
201 atomic_set(&mm->mm_count, 1);
202 init_MUTEX(&mm->mmap_sem);
203 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
204 mm->pgd = pgd_alloc();
205 if (mm->pgd)
206 return mm;
207 kmem_cache_free(mm_cachep, mm);
208 return NULL;
213 * Allocate and initialize an mm_struct.
215 struct mm_struct * mm_alloc(void)
217 struct mm_struct * mm;
219 mm = allocate_mm();
220 if (mm) {
221 memset(mm, 0, sizeof(*mm));
222 return mm_init(mm);
224 return NULL;
228 * Called when the last reference to the mm
229 * is dropped: either by a lazy thread or by
230 * mmput. Free the page directory and the mm.
232 inline void __mmdrop(struct mm_struct *mm)
234 if (mm == &init_mm) BUG();
235 pgd_free(mm->pgd);
236 destroy_context(mm);
237 kmem_cache_free(mm_cachep, mm);
241 * Decrement the use count and release all resources for an mm.
243 void mmput(struct mm_struct *mm)
245 if (atomic_dec_and_test(&mm->mm_users)) {
246 exit_mmap(mm);
247 mmdrop(mm);
251 /* Please note the differences between mmput and mm_release.
252 * mmput is called whenever we stop holding onto a mm_struct,
253 * error success whatever.
255 * mm_release is called after a mm_struct has been removed
256 * from the current process.
258 * This difference is important for error handling, when we
259 * only half set up a mm_struct for a new process and need to restore
260 * the old one. Because we mmput the new mm_struct before
261 * restoring the old one. . .
262 * Eric Biederman 10 January 1998
264 void mm_release(void)
266 struct task_struct *tsk = current;
268 /* notify parent sleeping on vfork() */
269 if (tsk->flags & PF_VFORK) {
270 tsk->flags &= ~PF_VFORK;
271 up(tsk->p_opptr->vfork_sem);
275 static inline int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
277 struct mm_struct * mm;
278 int retval;
280 tsk->min_flt = tsk->maj_flt = 0;
281 tsk->cmin_flt = tsk->cmaj_flt = 0;
282 tsk->nswap = tsk->cnswap = 0;
284 tsk->mm = NULL;
285 tsk->active_mm = NULL;
288 * Are we cloning a kernel thread?
290 * We need to steal a active VM for that..
292 mm = current->mm;
293 if (!mm)
294 return 0;
296 if (clone_flags & CLONE_VM) {
297 atomic_inc(&mm->mm_users);
298 goto good_mm;
301 retval = -ENOMEM;
302 mm = allocate_mm();
303 if (!mm)
304 goto fail_nomem;
306 /* Copy the current MM stuff.. */
307 memcpy(mm, current->mm, sizeof(*mm));
308 if (!mm_init(mm))
309 goto fail_nomem;
311 tsk->mm = mm;
312 tsk->active_mm = mm;
314 down(&current->mm->mmap_sem);
315 retval = dup_mmap(mm);
316 up(&current->mm->mmap_sem);
317 if (retval)
318 goto free_pt;
321 * child gets a private LDT (if there was an LDT in the parent)
323 copy_segments(tsk, mm);
325 if (init_new_context(tsk,mm))
326 goto free_pt;
328 good_mm:
329 tsk->mm = mm;
330 tsk->active_mm = mm;
331 return 0;
333 free_pt:
334 mmput(mm);
335 fail_nomem:
336 return retval;
339 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
341 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
342 /* We don't need to lock fs - think why ;-) */
343 if (fs) {
344 atomic_set(&fs->count, 1);
345 fs->lock = RW_LOCK_UNLOCKED;
346 fs->umask = old->umask;
347 read_lock(&old->lock);
348 fs->rootmnt = mntget(old->rootmnt);
349 fs->root = dget(old->root);
350 fs->pwdmnt = mntget(old->pwdmnt);
351 fs->pwd = dget(old->pwd);
352 if (old->altroot) {
353 fs->altrootmnt = mntget(old->altrootmnt);
354 fs->altroot = dget(old->altroot);
355 } else {
356 fs->altrootmnt = NULL;
357 fs->altroot = NULL;
359 read_unlock(&old->lock);
361 return fs;
364 struct fs_struct *copy_fs_struct(struct fs_struct *old)
366 return __copy_fs_struct(old);
369 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
371 if (clone_flags & CLONE_FS) {
372 atomic_inc(&current->fs->count);
373 return 0;
375 tsk->fs = __copy_fs_struct(current->fs);
376 if (!tsk->fs)
377 return -1;
378 return 0;
381 static int count_open_files(struct files_struct *files, int size)
383 int i;
385 /* Find the last open fd */
386 for (i = size/(8*sizeof(long)); i > 0; ) {
387 if (files->open_fds->fds_bits[--i])
388 break;
390 i = (i+1) * 8 * sizeof(long);
391 return i;
394 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
396 struct files_struct *oldf, *newf;
397 struct file **old_fds, **new_fds;
398 int open_files, nfds, size, i, error = 0;
401 * A background process may not have any files ...
403 oldf = current->files;
404 if (!oldf)
405 goto out;
407 if (clone_flags & CLONE_FILES) {
408 atomic_inc(&oldf->count);
409 goto out;
412 tsk->files = NULL;
413 error = -ENOMEM;
414 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
415 if (!newf)
416 goto out;
418 atomic_set(&newf->count, 1);
420 newf->file_lock = RW_LOCK_UNLOCKED;
421 newf->next_fd = 0;
422 newf->max_fds = NR_OPEN_DEFAULT;
423 newf->max_fdset = __FD_SETSIZE;
424 newf->close_on_exec = &newf->close_on_exec_init;
425 newf->open_fds = &newf->open_fds_init;
426 newf->fd = &newf->fd_array[0];
428 /* We don't yet have the oldf readlock, but even if the old
429 fdset gets grown now, we'll only copy up to "size" fds */
430 size = oldf->max_fdset;
431 if (size > __FD_SETSIZE) {
432 newf->max_fdset = 0;
433 write_lock(&newf->file_lock);
434 error = expand_fdset(newf, size);
435 write_unlock(&newf->file_lock);
436 if (error)
437 goto out_release;
439 read_lock(&oldf->file_lock);
441 open_files = count_open_files(oldf, size);
444 * Check whether we need to allocate a larger fd array.
445 * Note: we're not a clone task, so the open count won't
446 * change.
448 nfds = NR_OPEN_DEFAULT;
449 if (open_files > nfds) {
450 read_unlock(&oldf->file_lock);
451 newf->max_fds = 0;
452 write_lock(&newf->file_lock);
453 error = expand_fd_array(newf, open_files);
454 write_unlock(&newf->file_lock);
455 if (error)
456 goto out_release;
457 nfds = newf->max_fds;
458 read_lock(&oldf->file_lock);
461 old_fds = oldf->fd;
462 new_fds = newf->fd;
464 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
465 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
467 for (i = open_files; i != 0; i--) {
468 struct file *f = *old_fds++;
469 if (f)
470 get_file(f);
471 *new_fds++ = f;
473 read_unlock(&oldf->file_lock);
475 /* compute the remainder to be cleared */
476 size = (newf->max_fds - open_files) * sizeof(struct file *);
478 /* This is long word aligned thus could use a optimized version */
479 memset(new_fds, 0, size);
481 if (newf->max_fdset > open_files) {
482 int left = (newf->max_fdset-open_files)/8;
483 int start = open_files / (8 * sizeof(unsigned long));
485 memset(&newf->open_fds->fds_bits[start], 0, left);
486 memset(&newf->close_on_exec->fds_bits[start], 0, left);
489 tsk->files = newf;
490 error = 0;
491 out:
492 return error;
494 out_release:
495 free_fdset (newf->close_on_exec, newf->max_fdset);
496 free_fdset (newf->open_fds, newf->max_fdset);
497 kmem_cache_free(files_cachep, newf);
498 goto out;
501 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
503 struct signal_struct *sig;
505 if (clone_flags & CLONE_SIGNAL) {
506 atomic_inc(&current->sig->count);
507 return 0;
509 sig = kmem_cache_alloc(sigact_cachep, GFP_KERNEL);
510 tsk->sig = sig;
511 if (!sig)
512 return -1;
513 spin_lock_init(&sig->siglock);
514 atomic_set(&sig->count, 1);
515 memcpy(tsk->sig->action, current->sig->action, sizeof(tsk->sig->action));
516 return 0;
519 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
521 unsigned long new_flags = p->flags;
523 new_flags &= ~(PF_SUPERPRIV | PF_USEDFPU | PF_VFORK);
524 new_flags |= PF_FORKNOEXEC;
525 if (!(clone_flags & CLONE_PTRACE))
526 p->ptrace = 0;
527 if (clone_flags & CLONE_VFORK)
528 new_flags |= PF_VFORK;
529 p->flags = new_flags;
533 * Ok, this is the main fork-routine. It copies the system process
534 * information (task[nr]) and sets up the necessary registers. It
535 * also copies the data segment in its entirety.
537 int do_fork(unsigned long clone_flags, unsigned long usp, struct pt_regs *regs)
539 int retval = -ENOMEM;
540 struct task_struct *p;
541 DECLARE_MUTEX_LOCKED(sem);
543 if (clone_flags & CLONE_PID) {
544 /* This is only allowed from the boot up thread */
545 if (current->pid)
546 return -EPERM;
549 current->vfork_sem = &sem;
551 p = alloc_task_struct();
552 if (!p)
553 goto fork_out;
555 *p = *current;
557 retval = -EAGAIN;
558 if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur)
559 goto bad_fork_free;
560 atomic_inc(&p->user->__count);
561 atomic_inc(&p->user->processes);
564 * Counter increases are protected by
565 * the kernel lock so nr_threads can't
566 * increase under us (but it may decrease).
568 if (nr_threads >= max_threads)
569 goto bad_fork_cleanup_count;
571 if (p->exec_domain && p->exec_domain->module)
572 __MOD_INC_USE_COUNT(p->exec_domain->module);
573 if (p->binfmt && p->binfmt->module)
574 __MOD_INC_USE_COUNT(p->binfmt->module);
576 p->did_exec = 0;
577 p->swappable = 0;
578 p->state = TASK_UNINTERRUPTIBLE;
580 copy_flags(clone_flags, p);
581 p->pid = get_pid(clone_flags);
583 p->run_list.next = NULL;
584 p->run_list.prev = NULL;
586 if ((clone_flags & CLONE_VFORK) || !(clone_flags & CLONE_PARENT)) {
587 p->p_opptr = current;
588 if (!(p->ptrace & PT_PTRACED))
589 p->p_pptr = current;
591 p->p_cptr = NULL;
592 init_waitqueue_head(&p->wait_chldexit);
593 p->vfork_sem = NULL;
594 spin_lock_init(&p->alloc_lock);
596 p->sigpending = 0;
597 init_sigpending(&p->pending);
599 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
600 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
601 init_timer(&p->real_timer);
602 p->real_timer.data = (unsigned long) p;
604 p->leader = 0; /* session leadership doesn't inherit */
605 p->tty_old_pgrp = 0;
606 p->times.tms_utime = p->times.tms_stime = 0;
607 p->times.tms_cutime = p->times.tms_cstime = 0;
608 #ifdef CONFIG_SMP
610 int i;
611 p->has_cpu = 0;
612 p->processor = current->processor;
613 /* ?? should we just memset this ?? */
614 for(i = 0; i < smp_num_cpus; i++)
615 p->per_cpu_utime[i] = p->per_cpu_stime[i] = 0;
616 spin_lock_init(&p->sigmask_lock);
618 #endif
619 p->lock_depth = -1; /* -1 = no lock */
620 p->start_time = jiffies;
622 retval = -ENOMEM;
623 /* copy all the process information */
624 if (copy_files(clone_flags, p))
625 goto bad_fork_cleanup;
626 if (copy_fs(clone_flags, p))
627 goto bad_fork_cleanup_files;
628 if (copy_sighand(clone_flags, p))
629 goto bad_fork_cleanup_fs;
630 if (copy_mm(clone_flags, p))
631 goto bad_fork_cleanup_sighand;
632 retval = copy_thread(0, clone_flags, usp, p, regs);
633 if (retval)
634 goto bad_fork_cleanup_sighand;
635 p->semundo = NULL;
637 /* Our parent execution domain becomes current domain
638 These must match for thread signalling to apply */
640 p->parent_exec_id = p->self_exec_id;
642 /* ok, now we should be set up.. */
643 p->swappable = 1;
644 p->exit_signal = clone_flags & CSIGNAL;
645 p->pdeath_signal = 0;
648 * "share" dynamic priority between parent and child, thus the
649 * total amount of dynamic priorities in the system doesnt change,
650 * more scheduling fairness. This is only important in the first
651 * timeslice, on the long run the scheduling behaviour is unchanged.
653 p->counter = (current->counter + 1) >> 1;
654 current->counter >>= 1;
655 if (!current->counter)
656 current->need_resched = 1;
659 * Ok, add it to the run-queues and make it
660 * visible to the rest of the system.
662 * Let it rip!
664 retval = p->pid;
665 p->tgid = retval;
666 INIT_LIST_HEAD(&p->thread_group);
667 write_lock_irq(&tasklist_lock);
668 if (clone_flags & CLONE_SIGNAL) {
669 p->tgid = current->tgid;
670 list_add(&p->thread_group, &current->thread_group);
672 SET_LINKS(p);
673 hash_pid(p);
674 nr_threads++;
675 write_unlock_irq(&tasklist_lock);
677 if (p->ptrace & PT_PTRACED)
678 send_sig(SIGSTOP, p, 1);
680 wake_up_process(p); /* do this last */
681 ++total_forks;
683 fork_out:
684 if ((clone_flags & CLONE_VFORK) && (retval > 0))
685 down(&sem);
686 return retval;
688 bad_fork_cleanup_sighand:
689 exit_sighand(p);
690 bad_fork_cleanup_fs:
691 exit_fs(p); /* blocking */
692 bad_fork_cleanup_files:
693 exit_files(p); /* blocking */
694 bad_fork_cleanup:
695 put_exec_domain(p->exec_domain);
696 if (p->binfmt && p->binfmt->module)
697 __MOD_DEC_USE_COUNT(p->binfmt->module);
698 bad_fork_cleanup_count:
699 atomic_dec(&p->user->processes);
700 free_uid(p->user);
701 bad_fork_free:
702 free_task_struct(p);
703 goto fork_out;
706 /* SLAB cache for signal_struct structures (tsk->sig) */
707 kmem_cache_t *sigact_cachep;
709 /* SLAB cache for files_struct structures (tsk->files) */
710 kmem_cache_t *files_cachep;
712 /* SLAB cache for fs_struct structures (tsk->fs) */
713 kmem_cache_t *fs_cachep;
715 /* SLAB cache for vm_area_struct structures */
716 kmem_cache_t *vm_area_cachep;
718 /* SLAB cache for mm_struct structures (tsk->mm) */
719 kmem_cache_t *mm_cachep;
721 void __init proc_caches_init(void)
723 sigact_cachep = kmem_cache_create("signal_act",
724 sizeof(struct signal_struct), 0,
725 SLAB_HWCACHE_ALIGN, NULL, NULL);
726 if (!sigact_cachep)
727 panic("Cannot create signal action SLAB cache");
729 files_cachep = kmem_cache_create("files_cache",
730 sizeof(struct files_struct), 0,
731 SLAB_HWCACHE_ALIGN, NULL, NULL);
732 if (!files_cachep)
733 panic("Cannot create files SLAB cache");
735 fs_cachep = kmem_cache_create("fs_cache",
736 sizeof(struct fs_struct), 0,
737 SLAB_HWCACHE_ALIGN, NULL, NULL);
738 if (!fs_cachep)
739 panic("Cannot create fs_struct SLAB cache");
741 vm_area_cachep = kmem_cache_create("vm_area_struct",
742 sizeof(struct vm_area_struct), 0,
743 SLAB_HWCACHE_ALIGN, NULL, NULL);
744 if(!vm_area_cachep)
745 panic("vma_init: Cannot alloc vm_area_struct SLAB cache");
747 mm_cachep = kmem_cache_create("mm_struct",
748 sizeof(struct mm_struct), 0,
749 SLAB_HWCACHE_ALIGN, NULL, NULL);
750 if(!mm_cachep)
751 panic("vma_init: Cannot alloc mm_struct SLAB cache");