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_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.. */
32 unsigned long total_forks
; /* Handle normal Linux uptimes. */
35 struct task_struct
*pidhash
[PIDHASH_SZ
];
37 void add_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
)
41 wq_write_lock_irqsave(&q
->lock
, flags
);
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
)
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
)
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
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
)
90 spin_lock(&lastpid_lock
);
91 if((++last_pid
) & 0xffff8000) {
92 last_pid
= 300; /* Skip daemons etc. */
95 if(last_pid
>= next_safe
) {
98 read_lock(&tasklist_lock
);
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)
111 if(p
->pid
> last_pid
&& next_safe
> p
->pid
)
113 if(p
->pgrp
> last_pid
&& 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
);
125 static inline int dup_mmap(struct mm_struct
* mm
)
127 struct vm_area_struct
* mpnt
, *tmp
, **pprev
;
130 flush_cache_mm(current
->mm
);
134 mm
->mmap_cache
= NULL
;
139 mm
->swap_address
= 0;
142 for (mpnt
= current
->mm
->mmap
; mpnt
; mpnt
= mpnt
->vm_next
) {
146 if(mpnt
->vm_flags
& VM_DONTCOPY
)
148 tmp
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
152 tmp
->vm_flags
&= ~VM_LOCKED
;
158 struct inode
*inode
= file
->f_dentry
->d_inode
;
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
=
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
;
185 pprev
= &tmp
->vm_next
;
190 if (mm
->map_count
>= AVL_MIN_MAP_COUNT
)
194 flush_tlb_mm(current
->mm
);
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();
209 kmem_cache_free(mm_cachep
, mm
);
215 * Allocate and initialize an mm_struct.
217 struct mm_struct
* mm_alloc(void)
219 struct mm_struct
* mm
;
223 memset(mm
, 0, sizeof(*mm
));
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();
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
)) {
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
;
282 tsk
->min_flt
= tsk
->maj_flt
= 0;
283 tsk
->cmin_flt
= tsk
->cmaj_flt
= 0;
284 tsk
->nswap
= tsk
->cnswap
= 0;
287 tsk
->active_mm
= NULL
;
290 * Are we cloning a kernel thread?
292 * We need to steal a active VM for that..
298 if (clone_flags
& CLONE_VM
) {
299 atomic_inc(&mm
->mm_users
);
308 /* Copy the current MM stuff.. */
309 memcpy(mm
, current
->mm
, sizeof(*mm
));
316 down(¤t
->mm
->mmap_sem
);
317 retval
= dup_mmap(mm
);
318 up(¤t
->mm
->mmap_sem
);
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
))
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 ;-) */
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
);
355 fs
->altrootmnt
= mntget(old
->altrootmnt
);
356 fs
->altroot
= dget(old
->altroot
);
358 fs
->altrootmnt
= NULL
;
361 read_unlock(&old
->lock
);
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(¤t
->fs
->count
);
377 tsk
->fs
= __copy_fs_struct(current
->fs
);
383 static int count_open_files(struct files_struct
*files
, int size
)
387 /* Find the last open fd */
388 for (i
= size
/(8*sizeof(long)); i
> 0; ) {
389 if (files
->open_fds
->fds_bits
[--i
])
392 i
= (i
+1) * 8 * sizeof(long);
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
;
409 if (clone_flags
& CLONE_FILES
) {
410 atomic_inc(&oldf
->count
);
416 newf
= kmem_cache_alloc(files_cachep
, SLAB_KERNEL
);
420 atomic_set(&newf
->count
, 1);
422 newf
->file_lock
= RW_LOCK_UNLOCKED
;
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
) {
435 write_lock(&newf
->file_lock
);
436 error
= expand_fdset(newf
, size
);
437 write_unlock(&newf
->file_lock
);
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
450 nfds
= NR_OPEN_DEFAULT
;
451 if (open_files
> nfds
) {
452 read_unlock(&oldf
->file_lock
);
454 write_lock(&newf
->file_lock
);
455 error
= expand_fd_array(newf
, open_files
);
456 write_unlock(&newf
->file_lock
);
459 nfds
= newf
->max_fds
;
460 read_lock(&oldf
->file_lock
);
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
++;
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
);
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
);
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(¤t
->sig
->count
);
511 sig
= kmem_cache_alloc(sigact_cachep
, GFP_KERNEL
);
515 spin_lock_init(&sig
->siglock
);
516 atomic_set(&sig
->count
, 1);
517 memcpy(tsk
->sig
->action
, current
->sig
->action
, sizeof(tsk
->sig
->action
));
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
))
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 */
556 current
->vfork_sem
= &sem
;
558 p
= alloc_task_struct();
565 if (atomic_read(&p
->user
->processes
) >= p
->rlim
[RLIMIT_NPROC
].rlim_cur
)
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
);
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
))
599 init_waitqueue_head(&p
->wait_chldexit
);
601 spin_lock_init(&p
->alloc_lock
);
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 */
613 p
->times
.tms_utime
= p
->times
.tms_stime
= 0;
614 p
->times
.tms_cutime
= p
->times
.tms_cstime
= 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
);
626 p
->lock_depth
= -1; /* -1 = no lock */
627 p
->start_time
= jiffies
;
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
);
641 goto bad_fork_cleanup_sighand
;
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.. */
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.
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
, ¤t
->thread_group
);
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 */
691 if ((clone_flags
& CLONE_VFORK
) && (retval
> 0))
695 bad_fork_cleanup_sighand
:
698 exit_fs(p
); /* blocking */
699 bad_fork_cleanup_files
:
700 exit_files(p
); /* blocking */
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
);
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
);
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
);
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
);
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
);
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
);
758 panic("vma_init: Cannot alloc mm_struct SLAB cache");