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