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 /* SLAB cache for mm_struct's. */
36 kmem_cache_t
*mm_cachep
;
38 /* SLAB cache for files structs */
39 kmem_cache_t
*files_cachep
;
41 struct task_struct
*pidhash
[PIDHASH_SZ
];
43 /* UID task count cache, to prevent walking entire process list every
44 * single fork() operation.
46 #define UIDHASH_SZ (PIDHASH_SZ >> 2)
48 static struct user_struct
{
50 struct user_struct
*next
, **pprev
;
52 } *uidhash
[UIDHASH_SZ
];
54 spinlock_t uidhash_lock
= SPIN_LOCK_UNLOCKED
;
56 kmem_cache_t
*uid_cachep
;
58 #define uidhashfn(uid) (((uid >> 8) ^ uid) & (UIDHASH_SZ - 1))
61 * These routines must be called with the uidhash spinlock held!
63 static inline void uid_hash_insert(struct user_struct
*up
, unsigned int hashent
)
65 if((up
->next
= uidhash
[hashent
]) != NULL
)
66 uidhash
[hashent
]->pprev
= &up
->next
;
67 up
->pprev
= &uidhash
[hashent
];
68 uidhash
[hashent
] = up
;
71 static inline void uid_hash_remove(struct user_struct
*up
)
74 up
->next
->pprev
= up
->pprev
;
75 *up
->pprev
= up
->next
;
78 static inline struct user_struct
*uid_hash_find(unsigned short uid
, unsigned int hashent
)
80 struct user_struct
*up
, *next
;
82 next
= uidhash
[hashent
];
89 atomic_inc(&up
->count
);
97 * For SMP, we need to re-test the user struct counter
98 * after having aquired the spinlock. This allows us to do
99 * the common case (not freeing anything) without having
103 #define uid_hash_free(up) (!atomic_read(&(up)->count))
105 #define uid_hash_free(up) (1)
108 void free_uid(struct task_struct
*p
)
110 struct user_struct
*up
= p
->user
;
114 if (atomic_dec_and_test(&up
->count
)) {
115 spin_lock(&uidhash_lock
);
116 if (uid_hash_free(up
)) {
118 kmem_cache_free(uid_cachep
, up
);
120 spin_unlock(&uidhash_lock
);
125 int alloc_uid(struct task_struct
*p
)
127 unsigned int hashent
= uidhashfn(p
->uid
);
128 struct user_struct
*up
;
130 spin_lock(&uidhash_lock
);
131 up
= uid_hash_find(p
->uid
, hashent
);
132 spin_unlock(&uidhash_lock
);
135 struct user_struct
*new;
137 new = kmem_cache_alloc(uid_cachep
, SLAB_KERNEL
);
141 atomic_set(&new->count
, 1);
144 * Before adding this, check whether we raced
145 * on adding the same user already..
147 spin_lock(&uidhash_lock
);
148 up
= uid_hash_find(p
->uid
, hashent
);
150 kmem_cache_free(uid_cachep
, new);
152 uid_hash_insert(new, hashent
);
155 spin_unlock(&uidhash_lock
);
162 void add_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
)
166 wq_write_lock_irqsave(&q
->lock
, flags
);
167 __add_wait_queue(q
, wait
);
168 wq_write_unlock_irqrestore(&q
->lock
, flags
);
171 void add_wait_queue_exclusive(wait_queue_head_t
*q
, wait_queue_t
* wait
)
175 wq_write_lock_irqsave(&q
->lock
, flags
);
176 __add_wait_queue_tail(q
, wait
);
177 wq_write_unlock_irqrestore(&q
->lock
, flags
);
180 void remove_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
)
184 wq_write_lock_irqsave(&q
->lock
, flags
);
185 __remove_wait_queue(q
, wait
);
186 wq_write_unlock_irqrestore(&q
->lock
, flags
);
189 void __init
fork_init(unsigned long mempages
)
193 uid_cachep
= kmem_cache_create("uid_cache", sizeof(struct user_struct
),
195 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
197 panic("Cannot create uid taskcount SLAB cache\n");
199 for(i
= 0; i
< UIDHASH_SZ
; i
++)
203 * The default maximum number of threads is set to a safe
204 * value: the thread structures can take up at most half
207 max_threads
= mempages
/ (THREAD_SIZE
/PAGE_SIZE
) / 2;
209 init_task
.rlim
[RLIMIT_NPROC
].rlim_cur
= max_threads
/2;
210 init_task
.rlim
[RLIMIT_NPROC
].rlim_max
= max_threads
/2;
213 /* Protects next_safe and last_pid. */
214 spinlock_t lastpid_lock
= SPIN_LOCK_UNLOCKED
;
216 static int get_pid(unsigned long flags
)
218 static int next_safe
= PID_MAX
;
219 struct task_struct
*p
;
221 if (flags
& CLONE_PID
)
224 spin_lock(&lastpid_lock
);
225 if((++last_pid
) & 0xffff8000) {
226 last_pid
= 300; /* Skip daemons etc. */
229 if(last_pid
>= next_safe
) {
232 read_lock(&tasklist_lock
);
235 if(p
->pid
== last_pid
||
236 p
->pgrp
== last_pid
||
237 p
->session
== last_pid
) {
238 if(++last_pid
>= next_safe
) {
239 if(last_pid
& 0xffff8000)
245 if(p
->pid
> last_pid
&& next_safe
> p
->pid
)
247 if(p
->pgrp
> last_pid
&& next_safe
> p
->pgrp
)
249 if(p
->session
> last_pid
&& next_safe
> p
->session
)
250 next_safe
= p
->session
;
252 read_unlock(&tasklist_lock
);
254 spin_unlock(&lastpid_lock
);
259 static inline int dup_mmap(struct mm_struct
* mm
)
261 struct vm_area_struct
* mpnt
, *tmp
, **pprev
;
264 /* Kill me slowly. UGLY! FIXME! */
265 memcpy(&mm
->start_code
, ¤t
->mm
->start_code
, 15*sizeof(unsigned long));
267 flush_cache_mm(current
->mm
);
269 for (mpnt
= current
->mm
->mmap
; mpnt
; mpnt
= mpnt
->vm_next
) {
273 if(mpnt
->vm_flags
& VM_DONTCOPY
)
275 tmp
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
279 tmp
->vm_flags
&= ~VM_LOCKED
;
285 struct inode
*inode
= file
->f_dentry
->d_inode
;
287 if (tmp
->vm_flags
& VM_DENYWRITE
)
288 atomic_dec(&inode
->i_writecount
);
290 /* insert tmp into the share list, just after mpnt */
291 spin_lock(&inode
->i_mapping
->i_shared_lock
);
292 if((tmp
->vm_next_share
= mpnt
->vm_next_share
) != NULL
)
293 mpnt
->vm_next_share
->vm_pprev_share
=
295 mpnt
->vm_next_share
= tmp
;
296 tmp
->vm_pprev_share
= &mpnt
->vm_next_share
;
297 spin_unlock(&inode
->i_mapping
->i_shared_lock
);
300 /* Copy the pages, but defer checking for errors */
301 retval
= copy_page_range(mm
, current
->mm
, tmp
);
302 if (!retval
&& tmp
->vm_ops
&& tmp
->vm_ops
->open
)
303 tmp
->vm_ops
->open(tmp
);
306 * Link in the new vma even if an error occurred,
307 * so that exit_mmap() can clean up the mess.
309 tmp
->vm_next
= *pprev
;
312 pprev
= &tmp
->vm_next
;
317 if (mm
->map_count
>= AVL_MIN_MAP_COUNT
)
321 flush_tlb_mm(current
->mm
);
326 * Allocate and initialize an mm_struct.
328 struct mm_struct
* mm_alloc(void)
330 struct mm_struct
* mm
;
332 mm
= kmem_cache_alloc(mm_cachep
, SLAB_KERNEL
);
334 memset(mm
, 0, sizeof(*mm
));
335 atomic_set(&mm
->mm_users
, 1);
336 atomic_set(&mm
->mm_count
, 1);
337 init_MUTEX(&mm
->mmap_sem
);
338 mm
->page_table_lock
= SPIN_LOCK_UNLOCKED
;
339 mm
->pgd
= pgd_alloc();
342 kmem_cache_free(mm_cachep
, mm
);
348 * Called when the last reference to the mm
349 * is dropped: either by a lazy thread or by
350 * mmput. Free the page directory and the mm.
352 inline void __mmdrop(struct mm_struct
*mm
)
354 if (mm
== &init_mm
) BUG();
357 kmem_cache_free(mm_cachep
, mm
);
361 * Decrement the use count and release all resources for an mm.
363 void mmput(struct mm_struct
*mm
)
365 if (atomic_dec_and_test(&mm
->mm_users
)) {
371 /* Please note the differences between mmput and mm_release.
372 * mmput is called whenever we stop holding onto a mm_struct,
373 * error success whatever.
375 * mm_release is called after a mm_struct has been removed
376 * from the current process.
378 * This difference is important for error handling, when we
379 * only half set up a mm_struct for a new process and need to restore
380 * the old one. Because we mmput the new mm_struct before
381 * restoring the old one. . .
382 * Eric Biederman 10 January 1998
384 void mm_release(void)
386 struct task_struct
*tsk
= current
;
388 /* notify parent sleeping on vfork() */
389 if (tsk
->flags
& PF_VFORK
) {
390 tsk
->flags
&= ~PF_VFORK
;
391 up(tsk
->p_opptr
->vfork_sem
);
395 static inline int copy_mm(unsigned long clone_flags
, struct task_struct
* tsk
)
397 struct mm_struct
* mm
;
400 tsk
->min_flt
= tsk
->maj_flt
= 0;
401 tsk
->cmin_flt
= tsk
->cmaj_flt
= 0;
402 tsk
->nswap
= tsk
->cnswap
= 0;
405 tsk
->active_mm
= NULL
;
408 * Are we cloning a kernel thread?
410 * We need to steal a active VM for that..
416 if (clone_flags
& CLONE_VM
) {
417 atomic_inc(&mm
->mm_users
);
430 * child gets a private LDT (if there was an LDT in the parent)
432 copy_segments(tsk
, mm
);
434 down(¤t
->mm
->mmap_sem
);
435 retval
= dup_mmap(mm
);
436 up(¤t
->mm
->mmap_sem
);
443 init_new_context(tsk
,mm
);
452 static inline struct fs_struct
*__copy_fs_struct(struct fs_struct
*old
)
454 struct fs_struct
*fs
= kmalloc(sizeof(*old
), GFP_KERNEL
);
455 /* We don't need to lock fs - think why ;-) */
457 atomic_set(&fs
->count
, 1);
458 fs
->lock
= RW_LOCK_UNLOCKED
;
459 fs
->umask
= old
->umask
;
460 read_lock(&old
->lock
);
461 fs
->rootmnt
= mntget(old
->rootmnt
);
462 fs
->root
= dget(old
->root
);
463 fs
->pwdmnt
= mntget(old
->pwdmnt
);
464 fs
->pwd
= dget(old
->pwd
);
466 fs
->altrootmnt
= mntget(old
->altrootmnt
);
467 fs
->altroot
= dget(old
->altroot
);
469 fs
->altrootmnt
= NULL
;
472 read_unlock(&old
->lock
);
477 struct fs_struct
*copy_fs_struct(struct fs_struct
*old
)
479 return __copy_fs_struct(old
);
482 static inline int copy_fs(unsigned long clone_flags
, struct task_struct
* tsk
)
484 if (clone_flags
& CLONE_FS
) {
485 atomic_inc(¤t
->fs
->count
);
488 tsk
->fs
= __copy_fs_struct(current
->fs
);
494 static int count_open_files(struct files_struct
*files
, int size
)
498 /* Find the last open fd */
499 for (i
= size
/(8*sizeof(long)); i
> 0; ) {
500 if (files
->open_fds
->fds_bits
[--i
])
503 i
= (i
+1) * 8 * sizeof(long);
507 static int copy_files(unsigned long clone_flags
, struct task_struct
* tsk
)
509 struct files_struct
*oldf
, *newf
;
510 struct file
**old_fds
, **new_fds
;
511 int open_files
, nfds
, size
, i
, error
= 0;
514 * A background process may not have any files ...
516 oldf
= current
->files
;
520 if (clone_flags
& CLONE_FILES
) {
521 atomic_inc(&oldf
->count
);
527 newf
= kmem_cache_alloc(files_cachep
, SLAB_KERNEL
);
531 atomic_set(&newf
->count
, 1);
533 newf
->file_lock
= RW_LOCK_UNLOCKED
;
535 newf
->max_fds
= NR_OPEN_DEFAULT
;
536 newf
->max_fdset
= __FD_SETSIZE
;
537 newf
->close_on_exec
= &newf
->close_on_exec_init
;
538 newf
->open_fds
= &newf
->open_fds_init
;
539 newf
->fd
= &newf
->fd_array
[0];
541 /* We don't yet have the oldf readlock, but even if the old
542 fdset gets grown now, we'll only copy up to "size" fds */
543 size
= oldf
->max_fdset
;
544 if (size
> __FD_SETSIZE
) {
546 write_lock(&newf
->file_lock
);
547 error
= expand_fdset(newf
, size
);
548 write_unlock(&newf
->file_lock
);
552 read_lock(&oldf
->file_lock
);
554 open_files
= count_open_files(oldf
, size
);
557 * Check whether we need to allocate a larger fd array.
558 * Note: we're not a clone task, so the open count won't
561 nfds
= NR_OPEN_DEFAULT
;
562 if (open_files
> nfds
) {
563 read_unlock(&oldf
->file_lock
);
565 write_lock(&newf
->file_lock
);
566 error
= expand_fd_array(newf
, open_files
);
567 write_unlock(&newf
->file_lock
);
570 nfds
= newf
->max_fds
;
571 read_lock(&oldf
->file_lock
);
577 memcpy(newf
->open_fds
->fds_bits
, oldf
->open_fds
->fds_bits
, open_files
/8);
578 memcpy(newf
->close_on_exec
->fds_bits
, oldf
->close_on_exec
->fds_bits
, open_files
/8);
580 for (i
= open_files
; i
!= 0; i
--) {
581 struct file
*f
= *old_fds
++;
586 read_unlock(&oldf
->file_lock
);
588 /* compute the remainder to be cleared */
589 size
= (newf
->max_fds
- open_files
) * sizeof(struct file
*);
591 /* This is long word aligned thus could use a optimized version */
592 memset(new_fds
, 0, size
);
594 if (newf
->max_fdset
> open_files
) {
595 int left
= (newf
->max_fdset
-open_files
)/8;
596 int start
= open_files
/ (8 * sizeof(unsigned long));
598 memset(&newf
->open_fds
->fds_bits
[start
], 0, left
);
599 memset(&newf
->close_on_exec
->fds_bits
[start
], 0, left
);
608 free_fdset (newf
->close_on_exec
, newf
->max_fdset
);
609 free_fdset (newf
->open_fds
, newf
->max_fdset
);
610 kmem_cache_free(files_cachep
, newf
);
614 static inline int copy_sighand(unsigned long clone_flags
, struct task_struct
* tsk
)
616 if (clone_flags
& CLONE_SIGHAND
) {
617 atomic_inc(¤t
->sig
->count
);
620 tsk
->sig
= kmalloc(sizeof(*tsk
->sig
), GFP_KERNEL
);
623 spin_lock_init(&tsk
->sig
->siglock
);
624 atomic_set(&tsk
->sig
->count
, 1);
625 memcpy(tsk
->sig
->action
, current
->sig
->action
, sizeof(tsk
->sig
->action
));
629 static inline void copy_flags(unsigned long clone_flags
, struct task_struct
*p
)
631 unsigned long new_flags
= p
->flags
;
633 new_flags
&= ~(PF_SUPERPRIV
| PF_USEDFPU
| PF_VFORK
);
634 new_flags
|= PF_FORKNOEXEC
;
635 if (!(clone_flags
& CLONE_PTRACE
))
637 if (clone_flags
& CLONE_VFORK
)
638 new_flags
|= PF_VFORK
;
639 p
->flags
= new_flags
;
643 * Ok, this is the main fork-routine. It copies the system process
644 * information (task[nr]) and sets up the necessary registers. It
645 * also copies the data segment in its entirety.
647 int do_fork(unsigned long clone_flags
, unsigned long usp
, struct pt_regs
*regs
)
649 int retval
= -ENOMEM
;
650 struct task_struct
*p
;
651 DECLARE_MUTEX_LOCKED(sem
);
653 if (clone_flags
& CLONE_PID
) {
654 /* This is only allowed from the boot up thread */
659 current
->vfork_sem
= &sem
;
661 p
= alloc_task_struct();
671 if (atomic_read(&p
->user
->count
) >= p
->rlim
[RLIMIT_NPROC
].rlim_cur
)
673 atomic_inc(&p
->user
->count
);
677 * Counter increases are protected by
678 * the kernel lock so nr_threads can't
679 * increase under us (but it may decrease).
681 if (nr_threads
>= max_threads
)
682 goto bad_fork_cleanup_count
;
684 if (p
->exec_domain
&& p
->exec_domain
->module
)
685 __MOD_INC_USE_COUNT(p
->exec_domain
->module
);
686 if (p
->binfmt
&& p
->binfmt
->module
)
687 __MOD_INC_USE_COUNT(p
->binfmt
->module
);
691 p
->state
= TASK_UNINTERRUPTIBLE
;
693 copy_flags(clone_flags
, p
);
694 p
->pid
= get_pid(clone_flags
);
696 p
->run_list
.next
= NULL
;
697 p
->run_list
.prev
= NULL
;
699 if ((clone_flags
& CLONE_VFORK
) || !(clone_flags
& CLONE_PARENT
)) {
700 p
->p_opptr
= current
;
701 if (!(p
->ptrace
& PT_PTRACED
))
705 init_waitqueue_head(&p
->wait_chldexit
);
707 spin_lock_init(&p
->alloc_lock
);
710 sigemptyset(&p
->signal
);
712 p
->sigqueue_tail
= &p
->sigqueue
;
714 p
->it_real_value
= p
->it_virt_value
= p
->it_prof_value
= 0;
715 p
->it_real_incr
= p
->it_virt_incr
= p
->it_prof_incr
= 0;
716 init_timer(&p
->real_timer
);
717 p
->real_timer
.data
= (unsigned long) p
;
719 p
->leader
= 0; /* session leadership doesn't inherit */
721 p
->times
.tms_utime
= p
->times
.tms_stime
= 0;
722 p
->times
.tms_cutime
= p
->times
.tms_cstime
= 0;
727 p
->processor
= current
->processor
;
728 /* ?? should we just memset this ?? */
729 for(i
= 0; i
< smp_num_cpus
; i
++)
730 p
->per_cpu_utime
[i
] = p
->per_cpu_stime
[i
] = 0;
731 spin_lock_init(&p
->sigmask_lock
);
734 p
->lock_depth
= -1; /* -1 = no lock */
735 p
->start_time
= jiffies
;
738 /* copy all the process information */
739 if (copy_files(clone_flags
, p
))
740 goto bad_fork_cleanup
;
741 if (copy_fs(clone_flags
, p
))
742 goto bad_fork_cleanup_files
;
743 if (copy_sighand(clone_flags
, p
))
744 goto bad_fork_cleanup_fs
;
745 if (copy_mm(clone_flags
, p
))
746 goto bad_fork_cleanup_sighand
;
747 retval
= copy_thread(0, clone_flags
, usp
, p
, regs
);
749 goto bad_fork_cleanup_sighand
;
752 /* Our parent execution domain becomes current domain
753 These must match for thread signalling to apply */
755 p
->parent_exec_id
= p
->self_exec_id
;
757 /* ok, now we should be set up.. */
759 p
->exit_signal
= clone_flags
& CSIGNAL
;
760 p
->pdeath_signal
= 0;
763 * "share" dynamic priority between parent and child, thus the
764 * total amount of dynamic priorities in the system doesnt change,
765 * more scheduling fairness. This is only important in the first
766 * timeslice, on the long run the scheduling behaviour is unchanged.
768 p
->counter
= (current
->counter
+ 1) >> 1;
769 current
->counter
>>= 1;
770 if (!current
->counter
)
771 current
->need_resched
= 1;
774 * Ok, add it to the run-queues and make it
775 * visible to the rest of the system.
780 write_lock_irq(&tasklist_lock
);
784 write_unlock_irq(&tasklist_lock
);
786 wake_up_process(p
); /* do this last */
792 if ((clone_flags
& CLONE_VFORK
) && (retval
> 0))
796 bad_fork_cleanup_sighand
:
799 exit_fs(p
); /* blocking */
800 bad_fork_cleanup_files
:
801 exit_files(p
); /* blocking */
803 put_exec_domain(p
->exec_domain
);
804 if (p
->binfmt
&& p
->binfmt
->module
)
805 __MOD_DEC_USE_COUNT(p
->binfmt
->module
);
806 bad_fork_cleanup_count
:
814 void __init
filescache_init(void)
816 files_cachep
= kmem_cache_create("files_cache",
817 sizeof(struct files_struct
),
822 panic("Cannot create files cache");