- pre6:
[davej-history.git] / kernel / fork.c
blobd85c3494a810ad5dd01e83c1bb9ab5c5cfbc803a
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 wait->flags = 0;
43 __add_wait_queue(q, wait);
44 wq_write_unlock_irqrestore(&q->lock, flags);
47 void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
49 unsigned long flags;
51 wq_write_lock_irqsave(&q->lock, flags);
52 wait->flags = WQ_FLAG_EXCLUSIVE;
53 __add_wait_queue_tail(q, wait);
54 wq_write_unlock_irqrestore(&q->lock, flags);
57 void remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
59 unsigned long flags;
61 wq_write_lock_irqsave(&q->lock, flags);
62 __remove_wait_queue(q, wait);
63 wq_write_unlock_irqrestore(&q->lock, flags);
66 void __init fork_init(unsigned long mempages)
69 * The default maximum number of threads is set to a safe
70 * value: the thread structures can take up at most half
71 * of memory.
73 max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 2;
75 init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
76 init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
79 /* Protects next_safe and last_pid. */
80 spinlock_t lastpid_lock = SPIN_LOCK_UNLOCKED;
82 static int get_pid(unsigned long flags)
84 static int next_safe = PID_MAX;
85 struct task_struct *p;
87 if (flags & CLONE_PID)
88 return current->pid;
90 spin_lock(&lastpid_lock);
91 if((++last_pid) & 0xffff8000) {
92 last_pid = 300; /* Skip daemons etc. */
93 goto inside;
95 if(last_pid >= next_safe) {
96 inside:
97 next_safe = PID_MAX;
98 read_lock(&tasklist_lock);
99 repeat:
100 for_each_task(p) {
101 if(p->pid == last_pid ||
102 p->pgrp == last_pid ||
103 p->session == last_pid) {
104 if(++last_pid >= next_safe) {
105 if(last_pid & 0xffff8000)
106 last_pid = 300;
107 next_safe = PID_MAX;
109 goto repeat;
111 if(p->pid > last_pid && next_safe > p->pid)
112 next_safe = p->pid;
113 if(p->pgrp > last_pid && next_safe > p->pgrp)
114 next_safe = p->pgrp;
115 if(p->session > last_pid && next_safe > p->session)
116 next_safe = p->session;
118 read_unlock(&tasklist_lock);
120 spin_unlock(&lastpid_lock);
122 return last_pid;
125 static inline int dup_mmap(struct mm_struct * mm)
127 struct vm_area_struct * mpnt, *tmp, **pprev;
128 int retval;
130 flush_cache_mm(current->mm);
131 mm->locked_vm = 0;
132 mm->mmap = NULL;
133 mm->mmap_avl = NULL;
134 mm->mmap_cache = NULL;
135 mm->map_count = 0;
136 mm->context = 0;
137 mm->cpu_vm_mask = 0;
138 mm->swap_cnt = 0;
139 mm->swap_address = 0;
140 mm->segments = NULL;
141 pprev = &mm->mmap;
142 for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
143 struct file *file;
145 retval = -ENOMEM;
146 if(mpnt->vm_flags & VM_DONTCOPY)
147 continue;
148 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
149 if (!tmp)
150 goto fail_nomem;
151 *tmp = *mpnt;
152 tmp->vm_flags &= ~VM_LOCKED;
153 tmp->vm_mm = mm;
154 mm->map_count++;
155 tmp->vm_next = NULL;
156 file = tmp->vm_file;
157 if (file) {
158 struct inode *inode = file->f_dentry->d_inode;
159 get_file(file);
160 if (tmp->vm_flags & VM_DENYWRITE)
161 atomic_dec(&inode->i_writecount);
163 /* insert tmp into the share list, just after mpnt */
164 spin_lock(&inode->i_mapping->i_shared_lock);
165 if((tmp->vm_next_share = mpnt->vm_next_share) != NULL)
166 mpnt->vm_next_share->vm_pprev_share =
167 &tmp->vm_next_share;
168 mpnt->vm_next_share = tmp;
169 tmp->vm_pprev_share = &mpnt->vm_next_share;
170 spin_unlock(&inode->i_mapping->i_shared_lock);
173 /* Copy the pages, but defer checking for errors */
174 retval = copy_page_range(mm, current->mm, tmp);
175 if (!retval && tmp->vm_ops && tmp->vm_ops->open)
176 tmp->vm_ops->open(tmp);
179 * Link in the new vma even if an error occurred,
180 * so that exit_mmap() can clean up the mess.
182 tmp->vm_next = *pprev;
183 *pprev = tmp;
185 pprev = &tmp->vm_next;
186 if (retval)
187 goto fail_nomem;
189 retval = 0;
190 if (mm->map_count >= AVL_MIN_MAP_COUNT)
191 build_mmap_avl(mm);
193 fail_nomem:
194 flush_tlb_mm(current->mm);
195 return retval;
198 #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
200 static struct mm_struct * mm_init(struct mm_struct * mm)
202 atomic_set(&mm->mm_users, 1);
203 atomic_set(&mm->mm_count, 1);
204 init_MUTEX(&mm->mmap_sem);
205 mm->page_table_lock = SPIN_LOCK_UNLOCKED;
206 mm->pgd = pgd_alloc();
207 if (mm->pgd)
208 return mm;
209 kmem_cache_free(mm_cachep, mm);
210 return NULL;
215 * Allocate and initialize an mm_struct.
217 struct mm_struct * mm_alloc(void)
219 struct mm_struct * mm;
221 mm = allocate_mm();
222 if (mm) {
223 memset(mm, 0, sizeof(*mm));
224 return mm_init(mm);
226 return NULL;
230 * Called when the last reference to the mm
231 * is dropped: either by a lazy thread or by
232 * mmput. Free the page directory and the mm.
234 inline void __mmdrop(struct mm_struct *mm)
236 if (mm == &init_mm) BUG();
237 pgd_free(mm->pgd);
238 destroy_context(mm);
239 kmem_cache_free(mm_cachep, mm);
243 * Decrement the use count and release all resources for an mm.
245 void mmput(struct mm_struct *mm)
247 if (atomic_dec_and_test(&mm->mm_users)) {
248 exit_mmap(mm);
249 mmdrop(mm);
253 /* Please note the differences between mmput and mm_release.
254 * mmput is called whenever we stop holding onto a mm_struct,
255 * error success whatever.
257 * mm_release is called after a mm_struct has been removed
258 * from the current process.
260 * This difference is important for error handling, when we
261 * only half set up a mm_struct for a new process and need to restore
262 * the old one. Because we mmput the new mm_struct before
263 * restoring the old one. . .
264 * Eric Biederman 10 January 1998
266 void mm_release(void)
268 struct task_struct *tsk = current;
270 /* notify parent sleeping on vfork() */
271 if (tsk->flags & PF_VFORK) {
272 tsk->flags &= ~PF_VFORK;
273 up(tsk->p_opptr->vfork_sem);
277 static inline int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
279 struct mm_struct * mm;
280 int retval;
282 tsk->min_flt = tsk->maj_flt = 0;
283 tsk->cmin_flt = tsk->cmaj_flt = 0;
284 tsk->nswap = tsk->cnswap = 0;
286 tsk->mm = NULL;
287 tsk->active_mm = NULL;
290 * Are we cloning a kernel thread?
292 * We need to steal a active VM for that..
294 mm = current->mm;
295 if (!mm)
296 return 0;
298 if (clone_flags & CLONE_VM) {
299 atomic_inc(&mm->mm_users);
300 goto good_mm;
303 retval = -ENOMEM;
304 mm = allocate_mm();
305 if (!mm)
306 goto fail_nomem;
308 /* Copy the current MM stuff.. */
309 memcpy(mm, current->mm, sizeof(*mm));
310 if (!mm_init(mm))
311 goto fail_nomem;
313 tsk->mm = mm;
314 tsk->active_mm = mm;
316 down(&current->mm->mmap_sem);
317 retval = dup_mmap(mm);
318 up(&current->mm->mmap_sem);
319 if (retval)
320 goto free_pt;
323 * child gets a private LDT (if there was an LDT in the parent)
325 copy_segments(tsk, mm);
327 if (init_new_context(tsk,mm))
328 goto free_pt;
330 good_mm:
331 tsk->mm = mm;
332 tsk->active_mm = mm;
333 return 0;
335 free_pt:
336 mmput(mm);
337 fail_nomem:
338 return retval;
341 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
343 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
344 /* We don't need to lock fs - think why ;-) */
345 if (fs) {
346 atomic_set(&fs->count, 1);
347 fs->lock = RW_LOCK_UNLOCKED;
348 fs->umask = old->umask;
349 read_lock(&old->lock);
350 fs->rootmnt = mntget(old->rootmnt);
351 fs->root = dget(old->root);
352 fs->pwdmnt = mntget(old->pwdmnt);
353 fs->pwd = dget(old->pwd);
354 if (old->altroot) {
355 fs->altrootmnt = mntget(old->altrootmnt);
356 fs->altroot = dget(old->altroot);
357 } else {
358 fs->altrootmnt = NULL;
359 fs->altroot = NULL;
361 read_unlock(&old->lock);
363 return fs;
366 struct fs_struct *copy_fs_struct(struct fs_struct *old)
368 return __copy_fs_struct(old);
371 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
373 if (clone_flags & CLONE_FS) {
374 atomic_inc(&current->fs->count);
375 return 0;
377 tsk->fs = __copy_fs_struct(current->fs);
378 if (!tsk->fs)
379 return -1;
380 return 0;
383 static int count_open_files(struct files_struct *files, int size)
385 int i;
387 /* Find the last open fd */
388 for (i = size/(8*sizeof(long)); i > 0; ) {
389 if (files->open_fds->fds_bits[--i])
390 break;
392 i = (i+1) * 8 * sizeof(long);
393 return i;
396 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
398 struct files_struct *oldf, *newf;
399 struct file **old_fds, **new_fds;
400 int open_files, nfds, size, i, error = 0;
403 * A background process may not have any files ...
405 oldf = current->files;
406 if (!oldf)
407 goto out;
409 if (clone_flags & CLONE_FILES) {
410 atomic_inc(&oldf->count);
411 goto out;
414 tsk->files = NULL;
415 error = -ENOMEM;
416 newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
417 if (!newf)
418 goto out;
420 atomic_set(&newf->count, 1);
422 newf->file_lock = RW_LOCK_UNLOCKED;
423 newf->next_fd = 0;
424 newf->max_fds = NR_OPEN_DEFAULT;
425 newf->max_fdset = __FD_SETSIZE;
426 newf->close_on_exec = &newf->close_on_exec_init;
427 newf->open_fds = &newf->open_fds_init;
428 newf->fd = &newf->fd_array[0];
430 /* We don't yet have the oldf readlock, but even if the old
431 fdset gets grown now, we'll only copy up to "size" fds */
432 size = oldf->max_fdset;
433 if (size > __FD_SETSIZE) {
434 newf->max_fdset = 0;
435 write_lock(&newf->file_lock);
436 error = expand_fdset(newf, size);
437 write_unlock(&newf->file_lock);
438 if (error)
439 goto out_release;
441 read_lock(&oldf->file_lock);
443 open_files = count_open_files(oldf, size);
446 * Check whether we need to allocate a larger fd array.
447 * Note: we're not a clone task, so the open count won't
448 * change.
450 nfds = NR_OPEN_DEFAULT;
451 if (open_files > nfds) {
452 read_unlock(&oldf->file_lock);
453 newf->max_fds = 0;
454 write_lock(&newf->file_lock);
455 error = expand_fd_array(newf, open_files);
456 write_unlock(&newf->file_lock);
457 if (error)
458 goto out_release;
459 nfds = newf->max_fds;
460 read_lock(&oldf->file_lock);
463 old_fds = oldf->fd;
464 new_fds = newf->fd;
466 memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
467 memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
469 for (i = open_files; i != 0; i--) {
470 struct file *f = *old_fds++;
471 if (f)
472 get_file(f);
473 *new_fds++ = f;
475 read_unlock(&oldf->file_lock);
477 /* compute the remainder to be cleared */
478 size = (newf->max_fds - open_files) * sizeof(struct file *);
480 /* This is long word aligned thus could use a optimized version */
481 memset(new_fds, 0, size);
483 if (newf->max_fdset > open_files) {
484 int left = (newf->max_fdset-open_files)/8;
485 int start = open_files / (8 * sizeof(unsigned long));
487 memset(&newf->open_fds->fds_bits[start], 0, left);
488 memset(&newf->close_on_exec->fds_bits[start], 0, left);
491 tsk->files = newf;
492 error = 0;
493 out:
494 return error;
496 out_release:
497 free_fdset (newf->close_on_exec, newf->max_fdset);
498 free_fdset (newf->open_fds, newf->max_fdset);
499 kmem_cache_free(files_cachep, newf);
500 goto out;
503 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
505 struct signal_struct *sig;
507 if (clone_flags & CLONE_SIGHAND) {
508 atomic_inc(&current->sig->count);
509 return 0;
511 sig = kmem_cache_alloc(sigact_cachep, GFP_KERNEL);
512 tsk->sig = sig;
513 if (!sig)
514 return -1;
515 spin_lock_init(&sig->siglock);
516 atomic_set(&sig->count, 1);
517 memcpy(tsk->sig->action, current->sig->action, sizeof(tsk->sig->action));
518 return 0;
521 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
523 unsigned long new_flags = p->flags;
525 new_flags &= ~(PF_SUPERPRIV | PF_USEDFPU | PF_VFORK);
526 new_flags |= PF_FORKNOEXEC;
527 if (!(clone_flags & CLONE_PTRACE))
528 p->ptrace = 0;
529 if (clone_flags & CLONE_VFORK)
530 new_flags |= PF_VFORK;
531 p->flags = new_flags;
535 * Ok, this is the main fork-routine. It copies the system process
536 * information (task[nr]) and sets up the necessary registers. It also
537 * copies the data segment in its entirety. The "stack_start" and
538 * "stack_top" arguments are simply passed along to the platform
539 * specific copy_thread() routine. Most platforms ignore stack_top.
540 * For an example that's using stack_top, see
541 * arch/ia64/kernel/process.c.
543 int do_fork(unsigned long clone_flags, unsigned long stack_start,
544 struct pt_regs *regs, unsigned long stack_top)
546 int retval = -ENOMEM;
547 struct task_struct *p;
548 DECLARE_MUTEX_LOCKED(sem);
550 if (clone_flags & CLONE_PID) {
551 /* This is only allowed from the boot up thread */
552 if (current->pid)
553 return -EPERM;
556 current->vfork_sem = &sem;
558 p = alloc_task_struct();
559 if (!p)
560 goto fork_out;
562 *p = *current;
564 retval = -EAGAIN;
565 if (atomic_read(&p->user->processes) >= p->rlim[RLIMIT_NPROC].rlim_cur)
566 goto bad_fork_free;
567 atomic_inc(&p->user->__count);
568 atomic_inc(&p->user->processes);
571 * Counter increases are protected by
572 * the kernel lock so nr_threads can't
573 * increase under us (but it may decrease).
575 if (nr_threads >= max_threads)
576 goto bad_fork_cleanup_count;
578 get_exec_domain(p->exec_domain);
580 if (p->binfmt && p->binfmt->module)
581 __MOD_INC_USE_COUNT(p->binfmt->module);
583 p->did_exec = 0;
584 p->swappable = 0;
585 p->state = TASK_UNINTERRUPTIBLE;
587 copy_flags(clone_flags, p);
588 p->pid = get_pid(clone_flags);
590 p->run_list.next = NULL;
591 p->run_list.prev = NULL;
593 if ((clone_flags & CLONE_VFORK) || !(clone_flags & CLONE_PARENT)) {
594 p->p_opptr = current;
595 if (!(p->ptrace & PT_PTRACED))
596 p->p_pptr = current;
598 p->p_cptr = NULL;
599 init_waitqueue_head(&p->wait_chldexit);
600 p->vfork_sem = NULL;
601 spin_lock_init(&p->alloc_lock);
603 p->sigpending = 0;
604 init_sigpending(&p->pending);
606 p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
607 p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
608 init_timer(&p->real_timer);
609 p->real_timer.data = (unsigned long) p;
611 p->leader = 0; /* session leadership doesn't inherit */
612 p->tty_old_pgrp = 0;
613 p->times.tms_utime = p->times.tms_stime = 0;
614 p->times.tms_cutime = p->times.tms_cstime = 0;
615 #ifdef CONFIG_SMP
617 int i;
618 p->has_cpu = 0;
619 p->processor = current->processor;
620 /* ?? should we just memset this ?? */
621 for(i = 0; i < smp_num_cpus; i++)
622 p->per_cpu_utime[i] = p->per_cpu_stime[i] = 0;
623 spin_lock_init(&p->sigmask_lock);
625 #endif
626 p->lock_depth = -1; /* -1 = no lock */
627 p->start_time = jiffies;
629 retval = -ENOMEM;
630 /* copy all the process information */
631 if (copy_files(clone_flags, p))
632 goto bad_fork_cleanup;
633 if (copy_fs(clone_flags, p))
634 goto bad_fork_cleanup_files;
635 if (copy_sighand(clone_flags, p))
636 goto bad_fork_cleanup_fs;
637 if (copy_mm(clone_flags, p))
638 goto bad_fork_cleanup_sighand;
639 retval = copy_thread(0, clone_flags, stack_start, stack_top, p, regs);
640 if (retval)
641 goto bad_fork_cleanup_sighand;
642 p->semundo = NULL;
644 /* Our parent execution domain becomes current domain
645 These must match for thread signalling to apply */
647 p->parent_exec_id = p->self_exec_id;
649 /* ok, now we should be set up.. */
650 p->swappable = 1;
651 p->exit_signal = clone_flags & CSIGNAL;
652 p->pdeath_signal = 0;
655 * "share" dynamic priority between parent and child, thus the
656 * total amount of dynamic priorities in the system doesnt change,
657 * more scheduling fairness. This is only important in the first
658 * timeslice, on the long run the scheduling behaviour is unchanged.
660 p->counter = (current->counter + 1) >> 1;
661 current->counter >>= 1;
662 if (!current->counter)
663 current->need_resched = 1;
666 * Ok, add it to the run-queues and make it
667 * visible to the rest of the system.
669 * Let it rip!
671 retval = p->pid;
672 p->tgid = retval;
673 INIT_LIST_HEAD(&p->thread_group);
674 write_lock_irq(&tasklist_lock);
675 if (clone_flags & CLONE_THREAD) {
676 p->tgid = current->tgid;
677 list_add(&p->thread_group, &current->thread_group);
679 SET_LINKS(p);
680 hash_pid(p);
681 nr_threads++;
682 write_unlock_irq(&tasklist_lock);
684 if (p->ptrace & PT_PTRACED)
685 send_sig(SIGSTOP, p, 1);
687 wake_up_process(p); /* do this last */
688 ++total_forks;
690 fork_out:
691 if ((clone_flags & CLONE_VFORK) && (retval > 0))
692 down(&sem);
693 return retval;
695 bad_fork_cleanup_sighand:
696 exit_sighand(p);
697 bad_fork_cleanup_fs:
698 exit_fs(p); /* blocking */
699 bad_fork_cleanup_files:
700 exit_files(p); /* blocking */
701 bad_fork_cleanup:
702 put_exec_domain(p->exec_domain);
703 if (p->binfmt && p->binfmt->module)
704 __MOD_DEC_USE_COUNT(p->binfmt->module);
705 bad_fork_cleanup_count:
706 atomic_dec(&p->user->processes);
707 free_uid(p->user);
708 bad_fork_free:
709 free_task_struct(p);
710 goto fork_out;
713 /* SLAB cache for signal_struct structures (tsk->sig) */
714 kmem_cache_t *sigact_cachep;
716 /* SLAB cache for files_struct structures (tsk->files) */
717 kmem_cache_t *files_cachep;
719 /* SLAB cache for fs_struct structures (tsk->fs) */
720 kmem_cache_t *fs_cachep;
722 /* SLAB cache for vm_area_struct structures */
723 kmem_cache_t *vm_area_cachep;
725 /* SLAB cache for mm_struct structures (tsk->mm) */
726 kmem_cache_t *mm_cachep;
728 void __init proc_caches_init(void)
730 sigact_cachep = kmem_cache_create("signal_act",
731 sizeof(struct signal_struct), 0,
732 SLAB_HWCACHE_ALIGN, NULL, NULL);
733 if (!sigact_cachep)
734 panic("Cannot create signal action SLAB cache");
736 files_cachep = kmem_cache_create("files_cache",
737 sizeof(struct files_struct), 0,
738 SLAB_HWCACHE_ALIGN, NULL, NULL);
739 if (!files_cachep)
740 panic("Cannot create files SLAB cache");
742 fs_cachep = kmem_cache_create("fs_cache",
743 sizeof(struct fs_struct), 0,
744 SLAB_HWCACHE_ALIGN, NULL, NULL);
745 if (!fs_cachep)
746 panic("Cannot create fs_struct SLAB cache");
748 vm_area_cachep = kmem_cache_create("vm_area_struct",
749 sizeof(struct vm_area_struct), 0,
750 SLAB_HWCACHE_ALIGN, NULL, NULL);
751 if(!vm_area_cachep)
752 panic("vma_init: Cannot alloc vm_area_struct SLAB cache");
754 mm_cachep = kmem_cache_create("mm_struct",
755 sizeof(struct mm_struct), 0,
756 SLAB_HWCACHE_ALIGN, NULL, NULL);
757 if(!mm_cachep)
758 panic("vma_init: Cannot alloc mm_struct SLAB cache");