This commit was manufactured by cvs2svn to create tag
[linux-2.6.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/mempolicy.h>
25 #include <linux/sem.h>
26 #include <linux/file.h>
27 #include <linux/binfmts.h>
28 #include <linux/mman.h>
29 #include <linux/fs.h>
30 #include <linux/cpu.h>
31 #include <linux/security.h>
32 #include <linux/swap.h>
33 #include <linux/syscalls.h>
34 #include <linux/jiffies.h>
35 #include <linux/futex.h>
36 #include <linux/ptrace.h>
37 #include <linux/mount.h>
38 #include <linux/audit.h>
39 #include <linux/profile.h>
40 #include <linux/rmap.h>
41 #include <linux/vs_network.h>
42 #include <linux/vs_limit.h>
43 #include <linux/vs_memory.h>
44 #include <linux/ckrm.h>
45 #include <linux/ckrm_tsk.h>
46 #include <linux/ckrm_mem_inline.h>
47
48 #include <asm/pgtable.h>
49 #include <asm/pgalloc.h>
50 #include <asm/uaccess.h>
51 #include <asm/mmu_context.h>
52 #include <asm/cacheflush.h>
53 #include <asm/tlbflush.h>
54
55 /* The idle threads do not count..
56  * Protected by write_lock_irq(&tasklist_lock)
57  */
58 int nr_threads;
59
60 int max_threads;
61 unsigned long total_forks;      /* Handle normal Linux uptimes. */
62
63 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
64
65 rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED;  /* outer */
66
67 EXPORT_SYMBOL(tasklist_lock);
68
69 int nr_processes(void)
70 {
71         int cpu;
72         int total = 0;
73
74         for_each_online_cpu(cpu)
75                 total += per_cpu(process_counts, cpu);
76
77         return total;
78 }
79
80 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
81 # define alloc_task_struct()    kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
82 # define free_task_struct(tsk)  kmem_cache_free(task_struct_cachep, (tsk))
83 static kmem_cache_t *task_struct_cachep;
84 #endif
85
86 void free_task(struct task_struct *tsk)
87 {
88         free_thread_info(tsk->thread_info);
89         clr_vx_info(&tsk->vx_info);
90         clr_nx_info(&tsk->nx_info);
91         free_task_struct(tsk);
92 }
93 EXPORT_SYMBOL(free_task);
94
95 void __put_task_struct(struct task_struct *tsk)
96 {
97         WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
98         WARN_ON(atomic_read(&tsk->usage));
99         WARN_ON(tsk == current);
100
101         if (unlikely(tsk->audit_context))
102                 audit_free(tsk);
103         security_task_free(tsk);
104         free_uid(tsk->user);
105         put_group_info(tsk->group_info);
106
107         if (!profile_handoff_task(tsk))
108                 free_task(tsk);
109 }
110
111 void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
112 {
113         unsigned long flags;
114
115         wait->flags &= ~WQ_FLAG_EXCLUSIVE;
116         spin_lock_irqsave(&q->lock, flags);
117         __add_wait_queue(q, wait);
118         spin_unlock_irqrestore(&q->lock, flags);
119 }
120
121 EXPORT_SYMBOL(add_wait_queue);
122
123 void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait)
124 {
125         unsigned long flags;
126
127         wait->flags |= WQ_FLAG_EXCLUSIVE;
128         spin_lock_irqsave(&q->lock, flags);
129         __add_wait_queue_tail(q, wait);
130         spin_unlock_irqrestore(&q->lock, flags);
131 }
132
133 EXPORT_SYMBOL(add_wait_queue_exclusive);
134
135 void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait)
136 {
137         unsigned long flags;
138
139         spin_lock_irqsave(&q->lock, flags);
140         __remove_wait_queue(q, wait);
141         spin_unlock_irqrestore(&q->lock, flags);
142 }
143
144 EXPORT_SYMBOL(remove_wait_queue);
145
146
147 /*
148  * Note: we use "set_current_state()" _after_ the wait-queue add,
149  * because we need a memory barrier there on SMP, so that any
150  * wake-function that tests for the wait-queue being active
151  * will be guaranteed to see waitqueue addition _or_ subsequent
152  * tests in this thread will see the wakeup having taken place.
153  *
154  * The spin_unlock() itself is semi-permeable and only protects
155  * one way (it only protects stuff inside the critical region and
156  * stops them from bleeding out - it would still allow subsequent
157  * loads to move into the the critical region).
158  */
159 void fastcall prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
160 {
161         unsigned long flags;
162
163         wait->flags &= ~WQ_FLAG_EXCLUSIVE;
164         spin_lock_irqsave(&q->lock, flags);
165         if (list_empty(&wait->task_list))
166                 __add_wait_queue(q, wait);
167         /*
168          * don't alter the task state if this is just going to
169          * queue an async wait queue callback
170          */
171         if (is_sync_wait(wait))
172                 set_current_state(state);
173         spin_unlock_irqrestore(&q->lock, flags);
174 }
175
176 EXPORT_SYMBOL(prepare_to_wait);
177
178 void fastcall
179 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
180 {
181         unsigned long flags;
182
183         wait->flags |= WQ_FLAG_EXCLUSIVE;
184         spin_lock_irqsave(&q->lock, flags);
185         if (list_empty(&wait->task_list))
186                 __add_wait_queue_tail(q, wait);
187         /*
188          * don't alter the task state if this is just going to
189          * queue an async wait queue callback
190          */
191         if (is_sync_wait(wait))
192                 set_current_state(state);
193         spin_unlock_irqrestore(&q->lock, flags);
194 }
195
196 EXPORT_SYMBOL(prepare_to_wait_exclusive);
197
198 void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
199 {
200         unsigned long flags;
201
202         __set_current_state(TASK_RUNNING);
203         /*
204          * We can check for list emptiness outside the lock
205          * IFF:
206          *  - we use the "careful" check that verifies both
207          *    the next and prev pointers, so that there cannot
208          *    be any half-pending updates in progress on other
209          *    CPU's that we haven't seen yet (and that might
210          *    still change the stack area.
211          * and
212          *  - all other users take the lock (ie we can only
213          *    have _one_ other CPU that looks at or modifies
214          *    the list).
215          */
216         if (!list_empty_careful(&wait->task_list)) {
217                 spin_lock_irqsave(&q->lock, flags);
218                 list_del_init(&wait->task_list);
219                 spin_unlock_irqrestore(&q->lock, flags);
220         }
221 }
222
223 EXPORT_SYMBOL(finish_wait);
224
225 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
226 {
227         int ret = default_wake_function(wait, mode, sync, key);
228
229         if (ret)
230                 list_del_init(&wait->task_list);
231         return ret;
232 }
233
234 EXPORT_SYMBOL(autoremove_wake_function);
235
236 void __init fork_init(unsigned long mempages)
237 {
238 #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
239 #ifndef ARCH_MIN_TASKALIGN
240 #define ARCH_MIN_TASKALIGN      L1_CACHE_BYTES
241 #endif
242         /* create a slab on which task_structs can be allocated */
243         task_struct_cachep =
244                 kmem_cache_create("task_struct", sizeof(struct task_struct),
245                         ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
246 #endif
247
248         /*
249          * The default maximum number of threads is set to a safe
250          * value: the thread structures can take up at most half
251          * of memory.
252          */
253         max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8;
254         /*
255          * we need to allow at least 20 threads to boot a system
256          */
257         if(max_threads < 20)
258                 max_threads = 20;
259
260         init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
261         init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
262 }
263
264 static struct task_struct *dup_task_struct(struct task_struct *orig)
265 {
266         struct task_struct *tsk;
267         struct thread_info *ti;
268
269         prepare_to_copy(orig);
270
271         tsk = alloc_task_struct();
272         if (!tsk)
273                 return NULL;
274
275         ti = alloc_thread_info(tsk);
276         if (!ti) {
277                 free_task_struct(tsk);
278                 return NULL;
279         }
280
281         *ti = *orig->thread_info;
282         *tsk = *orig;
283         tsk->thread_info = ti;
284         ti->task = tsk;
285
286         ckrm_cb_newtask(tsk);
287         /* One for us, one for whoever does the "release_task()" (usually parent) */
288         atomic_set(&tsk->usage,2);
289 #ifdef CONFIG_CKRM_RES_MEM      
290         INIT_LIST_HEAD(&tsk->mm_peers);
291 #endif
292         return tsk;
293 }
294
295 #ifdef CONFIG_MMU
296 static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
297 {
298         struct vm_area_struct * mpnt, *tmp, **pprev;
299         struct rb_node **rb_link, *rb_parent;
300         int retval;
301         unsigned long charge;
302         struct mempolicy *pol;
303
304         down_write(&oldmm->mmap_sem);
305         flush_cache_mm(current->mm);
306         mm->locked_vm = 0;
307         mm->mmap = NULL;
308         mm->mmap_cache = NULL;
309         mm->free_area_cache = oldmm->mmap_base;
310         mm->map_count = 0;
311         mm->rss = 0;
312         mm->anon_rss = 0;
313         cpus_clear(mm->cpu_vm_mask);
314         mm->mm_rb = RB_ROOT;
315         rb_link = &mm->mm_rb.rb_node;
316         rb_parent = NULL;
317         pprev = &mm->mmap;
318
319         /*
320          * Add it to the mmlist after the parent.
321          * Doing it this way means that we can order the list,
322          * and fork() won't mess up the ordering significantly.
323          * Add it first so that swapoff can see any swap entries.
324          */
325         spin_lock(&mmlist_lock);
326         list_add(&mm->mmlist, &current->mm->mmlist);
327         mmlist_nr++;
328         spin_unlock(&mmlist_lock);
329
330         for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
331                 struct file *file;
332
333                 if (mpnt->vm_flags & VM_DONTCOPY) {
334                         __vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
335                                                         -vma_pages(mpnt));
336                         continue;
337                 }
338                 charge = 0;
339                 if (mpnt->vm_flags & VM_ACCOUNT) {
340                         unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
341                         if (security_vm_enough_memory(len))
342                                 goto fail_nomem;
343                         charge = len;
344                 }
345                 tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
346                 if (!tmp)
347                         goto fail_nomem;
348                 *tmp = *mpnt;
349                 pol = mpol_copy(vma_policy(mpnt));
350                 retval = PTR_ERR(pol);
351                 if (IS_ERR(pol))
352                         goto fail_nomem_policy;
353                 vma_set_policy(tmp, pol);
354                 tmp->vm_flags &= ~VM_LOCKED;
355                 tmp->vm_mm = mm;
356                 tmp->vm_next = NULL;
357                 anon_vma_link(tmp);
358                 file = tmp->vm_file;
359                 if (file) {
360                         struct inode *inode = file->f_dentry->d_inode;
361                         get_file(file);
362                         if (tmp->vm_flags & VM_DENYWRITE)
363                                 atomic_dec(&inode->i_writecount);
364       
365                         /* insert tmp into the share list, just after mpnt */
366                         spin_lock(&file->f_mapping->i_mmap_lock);
367                         flush_dcache_mmap_lock(file->f_mapping);
368                         vma_prio_tree_add(tmp, mpnt);
369                         flush_dcache_mmap_unlock(file->f_mapping);
370                         spin_unlock(&file->f_mapping->i_mmap_lock);
371                 }
372
373                 /*
374                  * Link in the new vma and copy the page table entries:
375                  * link in first so that swapoff can see swap entries,
376                  * and try_to_unmap_one's find_vma find the new vma.
377                  */
378                 spin_lock(&mm->page_table_lock);
379                 *pprev = tmp;
380                 pprev = &tmp->vm_next;
381
382                 __vma_link_rb(mm, tmp, rb_link, rb_parent);
383                 rb_link = &tmp->vm_rb.rb_right;
384                 rb_parent = &tmp->vm_rb;
385
386                 mm->map_count++;
387                 retval = copy_page_range(mm, current->mm, tmp);
388                 spin_unlock(&mm->page_table_lock);
389
390                 if (tmp->vm_ops && tmp->vm_ops->open)
391                         tmp->vm_ops->open(tmp);
392
393                 if (retval)
394                         goto out;
395         }
396         retval = 0;
397
398 out:
399         flush_tlb_mm(current->mm);
400         up_write(&oldmm->mmap_sem);
401         return retval;
402 fail_nomem_policy:
403         kmem_cache_free(vm_area_cachep, tmp);
404 fail_nomem:
405         retval = -ENOMEM;
406         vm_unacct_memory(charge);
407         goto out;
408 }
409
410 static inline int mm_alloc_pgd(struct mm_struct * mm)
411 {
412         mm->pgd = pgd_alloc(mm);
413         if (unlikely(!mm->pgd))
414                 return -ENOMEM;
415         return 0;
416 }
417
418 static inline void mm_free_pgd(struct mm_struct * mm)
419 {
420         pgd_free(mm->pgd);
421 }
422 #else
423 #define dup_mmap(mm, oldmm)     (0)
424 #define mm_alloc_pgd(mm)        (0)
425 #define mm_free_pgd(mm)
426 #endif /* CONFIG_MMU */
427
428 spinlock_t mmlist_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
429 int mmlist_nr;
430
431 #define allocate_mm()   (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
432 #define free_mm(mm)     (kmem_cache_free(mm_cachep, (mm)))
433
434 #include <linux/init_task.h>
435
436 static struct mm_struct * mm_init(struct mm_struct * mm)
437 {
438         atomic_set(&mm->mm_users, 1);
439         atomic_set(&mm->mm_count, 1);
440         init_rwsem(&mm->mmap_sem);
441         mm->core_waiters = 0;
442         mm->page_table_lock = SPIN_LOCK_UNLOCKED;
443         mm->ioctx_list_lock = RW_LOCK_UNLOCKED;
444         mm->ioctx_list = NULL;
445         mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
446         mm->free_area_cache = TASK_UNMAPPED_BASE;
447 #ifdef CONFIG_CKRM_RES_MEM
448         INIT_LIST_HEAD(&mm->tasklist);
449         mm->peertask_lock = SPIN_LOCK_UNLOCKED;
450 #endif
451
452         if (likely(!mm_alloc_pgd(mm))) {
453                 mm->def_flags = 0;
454                 set_vx_info(&mm->mm_vx_info, current->vx_info);
455                 return mm;
456         }
457         free_mm(mm);
458         return NULL;
459 }
460
461 /*
462  * Allocate and initialize an mm_struct.
463  */
464 struct mm_struct * mm_alloc(void)
465 {
466         struct mm_struct * mm;
467
468         mm = allocate_mm();
469         if (mm) {
470                 memset(mm, 0, sizeof(*mm));
471                 mm = mm_init(mm);
472 #ifdef CONFIG_CKRM_RES_MEM
473                 mm->memclass = GET_MEM_CLASS(current);
474                 mem_class_get(mm->memclass);
475 #endif
476         }
477         return mm;
478 }
479
480 /*
481  * Called when the last reference to the mm
482  * is dropped: either by a lazy thread or by
483  * mmput. Free the page directory and the mm.
484  */
485 void fastcall __mmdrop(struct mm_struct *mm)
486 {
487         BUG_ON(mm == &init_mm);
488         mm_free_pgd(mm);
489         destroy_context(mm);
490         clr_vx_info(&mm->mm_vx_info);
491 #ifdef CONFIG_CKRM_RES_MEM
492         /* class can be null and mm's tasklist can be empty here */
493         if (mm->memclass) {
494                 mem_class_put(mm->memclass);
495                 mm->memclass = NULL;
496         }
497 #endif
498         free_mm(mm);
499 }
500
501 /*
502  * Decrement the use count and release all resources for an mm.
503  */
504 void mmput(struct mm_struct *mm)
505 {
506         if (atomic_dec_and_lock(&mm->mm_users, &mmlist_lock)) {
507                 list_del(&mm->mmlist);
508                 mmlist_nr--;
509                 spin_unlock(&mmlist_lock);
510                 exit_aio(mm);
511                 exit_mmap(mm);
512                 put_swap_token(mm);
513                 mmdrop(mm);
514         }
515 }
516 EXPORT_SYMBOL_GPL(mmput);
517
518 /**
519  * get_task_mm - acquire a reference to the task's mm
520  *
521  * Returns %NULL if the task has no mm.  Checks if the use count
522  * of the mm is non-zero and if so returns a reference to it, after
523  * bumping up the use count.  User must release the mm via mmput()
524  * after use.  Typically used by /proc and ptrace.
525  *
526  * If the use count is zero, it means that this mm is going away,
527  * so return %NULL.  This only happens in the case of an AIO daemon
528  * which has temporarily adopted an mm (see use_mm), in the course
529  * of its final mmput, before exit_aio has completed.
530  */
531 struct mm_struct *get_task_mm(struct task_struct *task)
532 {
533         struct mm_struct *mm;
534
535         task_lock(task);
536         mm = task->mm;
537         if (mm) {
538                 spin_lock(&mmlist_lock);
539                 if (!atomic_read(&mm->mm_users))
540                         mm = NULL;
541                 else
542                         atomic_inc(&mm->mm_users);
543                 spin_unlock(&mmlist_lock);
544         }
545         task_unlock(task);
546         return mm;
547 }
548 EXPORT_SYMBOL_GPL(get_task_mm);
549
550 /* Please note the differences between mmput and mm_release.
551  * mmput is called whenever we stop holding onto a mm_struct,
552  * error success whatever.
553  *
554  * mm_release is called after a mm_struct has been removed
555  * from the current process.
556  *
557  * This difference is important for error handling, when we
558  * only half set up a mm_struct for a new process and need to restore
559  * the old one.  Because we mmput the new mm_struct before
560  * restoring the old one. . .
561  * Eric Biederman 10 January 1998
562  */
563 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
564 {
565         struct completion *vfork_done = tsk->vfork_done;
566
567         /* Get rid of any cached register state */
568         deactivate_mm(tsk, mm);
569
570         /* notify parent sleeping on vfork() */
571         if (vfork_done) {
572                 tsk->vfork_done = NULL;
573                 complete(vfork_done);
574         }
575         if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
576                 u32 __user * tidptr = tsk->clear_child_tid;
577                 tsk->clear_child_tid = NULL;
578
579                 /*
580                  * We don't check the error code - if userspace has
581                  * not set up a proper pointer then tough luck.
582                  */
583                 put_user(0, tidptr);
584                 sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
585         }
586 }
587
588 static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
589 {
590         struct mm_struct * mm, *oldmm;
591         int retval;
592
593         tsk->min_flt = tsk->maj_flt = 0;
594         tsk->nvcsw = tsk->nivcsw = 0;
595
596         tsk->mm = NULL;
597         tsk->active_mm = NULL;
598
599         /*
600          * Are we cloning a kernel thread?
601          *
602          * We need to steal a active VM for that..
603          */
604         oldmm = current->mm;
605         if (!oldmm)
606                 return 0;
607
608         if (clone_flags & CLONE_VM) {
609                 atomic_inc(&oldmm->mm_users);
610                 mm = oldmm;
611                 /*
612                  * There are cases where the PTL is held to ensure no
613                  * new threads start up in user mode using an mm, which
614                  * allows optimizing out ipis; the tlb_gather_mmu code
615                  * is an example.
616                  */
617                 spin_unlock_wait(&oldmm->page_table_lock);
618                 goto good_mm;
619         }
620
621         retval = -ENOMEM;
622         mm = allocate_mm();
623         if (!mm)
624                 goto fail_nomem;
625
626         /* Copy the current MM stuff.. */
627         memcpy(mm, oldmm, sizeof(*mm));
628         mm->mm_vx_info = NULL;
629         if (!mm_init(mm))
630                 goto fail_nomem;
631
632         if (init_new_context(tsk,mm))
633                 goto fail_nocontext;
634
635         retval = dup_mmap(mm, oldmm);
636         if (retval)
637                 goto free_pt;
638
639 good_mm:
640         tsk->mm = mm;
641         tsk->active_mm = mm;
642         ckrm_init_mm_to_task(mm, tsk);
643         return 0;
644
645 free_pt:
646         mmput(mm);
647 fail_nomem:
648         return retval;
649
650 fail_nocontext:
651         /*
652          * If init_new_context() failed, we cannot use mmput() to free the mm
653          * because it calls destroy_context()
654          */
655         mm_free_pgd(mm);
656         free_mm(mm);
657         return retval;
658 }
659
660 static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
661 {
662         struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
663         /* We don't need to lock fs - think why ;-) */
664         if (fs) {
665                 atomic_set(&fs->count, 1);
666                 fs->lock = RW_LOCK_UNLOCKED;
667                 fs->umask = old->umask;
668                 read_lock(&old->lock);
669                 fs->rootmnt = mntget(old->rootmnt);
670                 fs->root = dget(old->root);
671                 fs->pwdmnt = mntget(old->pwdmnt);
672                 fs->pwd = dget(old->pwd);
673                 if (old->altroot) {
674                         fs->altrootmnt = mntget(old->altrootmnt);
675                         fs->altroot = dget(old->altroot);
676                 } else {
677                         fs->altrootmnt = NULL;
678                         fs->altroot = NULL;
679                 }
680                 read_unlock(&old->lock);
681         }
682         return fs;
683 }
684
685 struct fs_struct *copy_fs_struct(struct fs_struct *old)
686 {
687         return __copy_fs_struct(old);
688 }
689
690 EXPORT_SYMBOL_GPL(copy_fs_struct);
691
692 static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
693 {
694         if (clone_flags & CLONE_FS) {
695                 atomic_inc(&current->fs->count);
696                 return 0;
697         }
698         tsk->fs = __copy_fs_struct(current->fs);
699         if (!tsk->fs)
700                 return -ENOMEM;
701         return 0;
702 }
703
704 static int count_open_files(struct files_struct *files, int size)
705 {
706         int i;
707
708         /* Find the last open fd */
709         for (i = size/(8*sizeof(long)); i > 0; ) {
710                 if (files->open_fds->fds_bits[--i])
711                         break;
712         }
713         i = (i+1) * 8 * sizeof(long);
714         return i;
715 }
716
717 static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
718 {
719         struct files_struct *oldf, *newf;
720         struct file **old_fds, **new_fds;
721         int open_files, nfds, size, i, error = 0;
722
723         /*
724          * A background process may not have any files ...
725          */
726         oldf = current->files;
727         if (!oldf)
728                 goto out;
729
730         if (clone_flags & CLONE_FILES) {
731                 atomic_inc(&oldf->count);
732                 goto out;
733         }
734
735         /*
736          * Note: we may be using current for both targets (See exec.c)
737          * This works because we cache current->files (old) as oldf. Don't
738          * break this.
739          */
740         tsk->files = NULL;
741         error = -ENOMEM;
742         newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
743         if (!newf) 
744                 goto out;
745
746         atomic_set(&newf->count, 1);
747
748         newf->file_lock     = SPIN_LOCK_UNLOCKED;
749         newf->next_fd       = 0;
750         newf->max_fds       = NR_OPEN_DEFAULT;
751         newf->max_fdset     = __FD_SETSIZE;
752         newf->close_on_exec = &newf->close_on_exec_init;
753         newf->open_fds      = &newf->open_fds_init;
754         newf->fd            = &newf->fd_array[0];
755
756         /* We don't yet have the oldf readlock, but even if the old
757            fdset gets grown now, we'll only copy up to "size" fds */
758         size = oldf->max_fdset;
759         if (size > __FD_SETSIZE) {
760                 newf->max_fdset = 0;
761                 spin_lock(&newf->file_lock);
762                 error = expand_fdset(newf, size-1);
763                 spin_unlock(&newf->file_lock);
764                 if (error)
765                         goto out_release;
766         }
767         spin_lock(&oldf->file_lock);
768
769         open_files = count_open_files(oldf, size);
770
771         /*
772          * Check whether we need to allocate a larger fd array.
773          * Note: we're not a clone task, so the open count won't
774          * change.
775          */
776         nfds = NR_OPEN_DEFAULT;
777         if (open_files > nfds) {
778                 spin_unlock(&oldf->file_lock);
779                 newf->max_fds = 0;
780                 spin_lock(&newf->file_lock);
781                 error = expand_fd_array(newf, open_files-1);
782                 spin_unlock(&newf->file_lock);
783                 if (error) 
784                         goto out_release;
785                 nfds = newf->max_fds;
786                 spin_lock(&oldf->file_lock);
787         }
788
789         old_fds = oldf->fd;
790         new_fds = newf->fd;
791
792         memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8);
793         memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8);
794
795         for (i = open_files; i != 0; i--) {
796                 struct file *f = *old_fds++;
797                 if (f)
798                         get_file(f);
799                 *new_fds++ = f;
800         }
801         spin_unlock(&oldf->file_lock);
802
803         /* compute the remainder to be cleared */
804         size = (newf->max_fds - open_files) * sizeof(struct file *);
805
806         /* This is long word aligned thus could use a optimized version */ 
807         memset(new_fds, 0, size); 
808
809         if (newf->max_fdset > open_files) {
810                 int left = (newf->max_fdset-open_files)/8;
811                 int start = open_files / (8 * sizeof(unsigned long));
812
813                 memset(&newf->open_fds->fds_bits[start], 0, left);
814                 memset(&newf->close_on_exec->fds_bits[start], 0, left);
815         }
816
817         tsk->files = newf;
818         error = 0;
819 out:
820         return error;
821
822 out_release:
823         free_fdset (newf->close_on_exec, newf->max_fdset);
824         free_fdset (newf->open_fds, newf->max_fdset);
825         kmem_cache_free(files_cachep, newf);
826         goto out;
827 }
828
829 /*
830  *      Helper to unshare the files of the current task.
831  *      We don't want to expose copy_files internals to
832  *      the exec layer of the kernel.
833  */
834
835 int unshare_files(void)
836 {
837         struct files_struct *files  = current->files;
838         int rc;
839
840         if(!files)
841                 BUG();
842
843         /* This can race but the race causes us to copy when we don't
844            need to and drop the copy */
845         if(atomic_read(&files->count) == 1)
846         {
847                 atomic_inc(&files->count);
848                 return 0;
849         }
850         rc = copy_files(0, current);
851         if(rc)
852                 current->files = files;
853         return rc;
854 }
855
856 EXPORT_SYMBOL(unshare_files);
857
858 static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
859 {
860         struct sighand_struct *sig;
861
862         if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
863                 atomic_inc(&current->sighand->count);
864                 return 0;
865         }
866         sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
867         tsk->sighand = sig;
868         if (!sig)
869                 return -ENOMEM;
870         spin_lock_init(&sig->siglock);
871         atomic_set(&sig->count, 1);
872         memcpy(sig->action, current->sighand->action, sizeof(sig->action));
873         return 0;
874 }
875
876 static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
877 {
878         struct signal_struct *sig;
879
880         if (clone_flags & CLONE_THREAD) {
881                 atomic_inc(&current->signal->count);
882                 return 0;
883         }
884         sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
885         tsk->signal = sig;
886         if (!sig)
887                 return -ENOMEM;
888         atomic_set(&sig->count, 1);
889         sig->group_exit = 0;
890         sig->group_exit_code = 0;
891         sig->group_exit_task = NULL;
892         sig->group_stop_count = 0;
893         sig->curr_target = NULL;
894         init_sigpending(&sig->shared_pending);
895         INIT_LIST_HEAD(&sig->posix_timers);
896
897         sig->tty = current->signal->tty;
898         sig->pgrp = process_group(current);
899         sig->session = current->signal->session;
900         sig->leader = 0;        /* session leadership doesn't inherit */
901         sig->tty_old_pgrp = 0;
902
903         sig->utime = sig->stime = sig->cutime = sig->cstime = 0;
904         sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
905         sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
906
907         return 0;
908 }
909
910 static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
911 {
912         unsigned long new_flags = p->flags;
913
914         new_flags &= ~PF_SUPERPRIV;
915         new_flags |= PF_FORKNOEXEC;
916         if (!(clone_flags & CLONE_PTRACE))
917                 p->ptrace = 0;
918         p->flags = new_flags;
919 }
920
921 asmlinkage long sys_set_tid_address(int __user *tidptr)
922 {
923         current->clear_child_tid = tidptr;
924
925         return current->pid;
926 }
927
928 /*
929  * This creates a new process as a copy of the old one,
930  * but does not actually start it yet.
931  *
932  * It copies the registers, and all the appropriate
933  * parts of the process environment (as per the clone
934  * flags). The actual kick-off is left to the caller.
935  */
936 static task_t *copy_process(unsigned long clone_flags,
937                                  unsigned long stack_start,
938                                  struct pt_regs *regs,
939                                  unsigned long stack_size,
940                                  int __user *parent_tidptr,
941                                  int __user *child_tidptr,
942                                  int pid)
943 {
944         int retval;
945         struct task_struct *p = NULL;
946         struct vx_info *vxi;
947
948         if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
949                 return ERR_PTR(-EINVAL);
950
951         /*
952          * Thread groups must share signals as well, and detached threads
953          * can only be started up within the thread group.
954          */
955         if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
956                 return ERR_PTR(-EINVAL);
957
958         /*
959          * Shared signal handlers imply shared VM. By way of the above,
960          * thread groups also imply shared VM. Blocking this case allows
961          * for various simplifications in other code.
962          */
963         if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
964                 return ERR_PTR(-EINVAL);
965
966         retval = security_task_create(clone_flags);
967         if (retval)
968                 goto fork_out;
969
970         retval = -ENOMEM;
971         p = dup_task_struct(current);
972         if (!p)
973                 goto fork_out;
974         p->tux_info = NULL;
975
976         p->vx_info = NULL;
977         set_vx_info(&p->vx_info, current->vx_info);
978         p->nx_info = NULL;
979         set_nx_info(&p->nx_info, current->nx_info);
980
981         /* check vserver memory */
982         if (p->mm && !(clone_flags & CLONE_VM)) {
983                 if (vx_vmpages_avail(p->mm, p->mm->total_vm))
984                         vx_pages_add(p->mm->mm_vx_info, RLIMIT_AS, p->mm->total_vm);
985                 else
986                         goto bad_fork_free;
987         }
988         if (p->mm && vx_flags(VXF_FORK_RSS, 0)) {
989                 if (!vx_rsspages_avail(p->mm, p->mm->rss))
990                         goto bad_fork_cleanup_vm;
991         }
992
993         retval = -EAGAIN;
994         if (!vx_nproc_avail(1))
995                 goto bad_fork_cleanup_vm;
996
997         if (atomic_read(&p->user->processes) >=
998                         p->rlim[RLIMIT_NPROC].rlim_cur) {
999                 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
1000                                 p->user != &root_user)
1001                         goto bad_fork_cleanup_vm;
1002         }
1003
1004         atomic_inc(&p->user->__count);
1005         atomic_inc(&p->user->processes);
1006         get_group_info(p->group_info);
1007
1008         /*
1009          * If multiple threads are within copy_process(), then this check
1010          * triggers too late. This doesn't hurt, the check is only there
1011          * to stop root fork bombs.
1012          */
1013         if (nr_threads >= max_threads)
1014                 goto bad_fork_cleanup_count;
1015
1016         if (!try_module_get(p->thread_info->exec_domain->module))
1017                 goto bad_fork_cleanup_count;
1018
1019         if (p->binfmt && !try_module_get(p->binfmt->module))
1020                 goto bad_fork_cleanup_put_domain;
1021
1022         init_delays(p);
1023         p->did_exec = 0;
1024         copy_flags(clone_flags, p);
1025         p->pid = pid;
1026         retval = -EFAULT;
1027         if (clone_flags & CLONE_PARENT_SETTID)
1028                 if (put_user(p->pid, parent_tidptr))
1029                         goto bad_fork_cleanup;
1030
1031         p->proc_dentry = NULL;
1032
1033         INIT_LIST_HEAD(&p->children);
1034         INIT_LIST_HEAD(&p->sibling);
1035         init_waitqueue_head(&p->wait_chldexit);
1036         p->vfork_done = NULL;
1037         spin_lock_init(&p->alloc_lock);
1038         spin_lock_init(&p->proc_lock);
1039
1040         clear_tsk_thread_flag(p, TIF_SIGPENDING);
1041         init_sigpending(&p->pending);
1042
1043         p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
1044         p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
1045         init_timer(&p->real_timer);
1046         p->real_timer.data = (unsigned long) p;
1047
1048         p->utime = p->stime = 0;
1049         p->lock_depth = -1;             /* -1 = no lock */
1050         do_posix_clock_monotonic_gettime(&p->start_time);
1051         p->security = NULL;
1052         p->io_context = NULL;
1053         p->io_wait = NULL;
1054         p->audit_context = NULL;
1055 #ifdef CONFIG_NUMA
1056         p->mempolicy = mpol_copy(p->mempolicy);
1057         if (IS_ERR(p->mempolicy)) {
1058                 retval = PTR_ERR(p->mempolicy);
1059                 p->mempolicy = NULL;
1060                 goto bad_fork_cleanup;
1061         }
1062 #endif
1063
1064         if ((retval = security_task_alloc(p)))
1065                 goto bad_fork_cleanup_policy;
1066         if ((retval = audit_alloc(p)))
1067                 goto bad_fork_cleanup_security;
1068         /* copy all the process information */
1069         if ((retval = copy_semundo(clone_flags, p)))
1070                 goto bad_fork_cleanup_audit;
1071         if ((retval = copy_files(clone_flags, p)))
1072                 goto bad_fork_cleanup_semundo;
1073         if ((retval = copy_fs(clone_flags, p)))
1074                 goto bad_fork_cleanup_files;
1075         if ((retval = copy_sighand(clone_flags, p)))
1076                 goto bad_fork_cleanup_fs;
1077         if ((retval = copy_signal(clone_flags, p)))
1078                 goto bad_fork_cleanup_sighand;
1079         if ((retval = copy_mm(clone_flags, p)))
1080                 goto bad_fork_cleanup_signal;
1081         if ((retval = copy_namespace(clone_flags, p)))
1082                 goto bad_fork_cleanup_mm;
1083         retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
1084         if (retval)
1085                 goto bad_fork_cleanup_namespace;
1086
1087         p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
1088         /*
1089          * Clear TID on mm_release()?
1090          */
1091         p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
1092
1093         /*
1094          * Syscall tracing should be turned off in the child regardless
1095          * of CLONE_PTRACE.
1096          */
1097         clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1098
1099         /* Our parent execution domain becomes current domain
1100            These must match for thread signalling to apply */
1101            
1102         p->parent_exec_id = p->self_exec_id;
1103
1104         /* ok, now we should be set up.. */
1105         p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
1106         p->pdeath_signal = 0;
1107         p->exit_state = 0;
1108
1109         /* Perform scheduler related setup */
1110         sched_fork(p);
1111
1112         /*
1113          * Ok, make it visible to the rest of the system.
1114          * We dont wake it up yet.
1115          */
1116         p->tgid = p->pid;
1117         p->group_leader = p;
1118         INIT_LIST_HEAD(&p->ptrace_children);
1119         INIT_LIST_HEAD(&p->ptrace_list);
1120
1121         /* Need tasklist lock for parent etc handling! */
1122         write_lock_irq(&tasklist_lock);
1123
1124         /*
1125          * The task hasn't been attached yet, so cpus_allowed mask cannot
1126          * have changed. The cpus_allowed mask of the parent may have
1127          * changed after it was copied first time, and it may then move to
1128          * another CPU - so we re-copy it here and set the child's CPU to
1129          * the parent's CPU. This avoids alot of nasty races.
1130          */
1131         p->cpus_allowed = current->cpus_allowed;
1132         set_task_cpu(p, smp_processor_id());
1133
1134         /*
1135          * Check for pending SIGKILL! The new thread should not be allowed
1136          * to slip out of an OOM kill. (or normal SIGKILL.)
1137          */
1138         if (sigismember(&current->pending.signal, SIGKILL)) {
1139                 write_unlock_irq(&tasklist_lock);
1140                 retval = -EINTR;
1141                 goto bad_fork_cleanup_namespace;
1142         }
1143
1144         /* CLONE_PARENT re-uses the old parent */
1145         if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
1146                 p->real_parent = current->real_parent;
1147         else
1148                 p->real_parent = current;
1149         p->parent = p->real_parent;
1150
1151         if (clone_flags & CLONE_THREAD) {
1152                 spin_lock(&current->sighand->siglock);
1153                 /*
1154                  * Important: if an exit-all has been started then
1155                  * do not create this new thread - the whole thread
1156                  * group is supposed to exit anyway.
1157                  */
1158                 if (current->signal->group_exit) {
1159                         spin_unlock(&current->sighand->siglock);
1160                         write_unlock_irq(&tasklist_lock);
1161                         retval = -EAGAIN;
1162                         goto bad_fork_cleanup_namespace;
1163                 }
1164                 p->tgid = current->tgid;
1165                 p->group_leader = current->group_leader;
1166
1167                 if (current->signal->group_stop_count > 0) {
1168                         /*
1169                          * There is an all-stop in progress for the group.
1170                          * We ourselves will stop as soon as we check signals.
1171                          * Make the new thread part of that group stop too.
1172                          */
1173                         current->signal->group_stop_count++;
1174                         set_tsk_thread_flag(p, TIF_SIGPENDING);
1175                 }
1176
1177                 spin_unlock(&current->sighand->siglock);
1178         }
1179
1180         SET_LINKS(p);
1181         if (unlikely(p->ptrace & PT_PTRACED))
1182                 __ptrace_link(p, current->parent);
1183
1184         attach_pid(p, PIDTYPE_PID, p->pid);
1185         attach_pid(p, PIDTYPE_TGID, p->tgid);
1186         if (thread_group_leader(p)) {
1187                 attach_pid(p, PIDTYPE_PGID, process_group(p));
1188                 attach_pid(p, PIDTYPE_SID, p->signal->session);
1189                 if (p->pid)
1190                         __get_cpu_var(process_counts)++;
1191         }
1192
1193         p->ioprio = current->ioprio;
1194         nr_threads++;
1195         /* p is copy of current */
1196         vxi = p->vx_info;
1197         if (vxi) {
1198                 atomic_inc(&vxi->cvirt.nr_threads);
1199                 vx_nproc_inc(p);
1200         }
1201         write_unlock_irq(&tasklist_lock);
1202         retval = 0;
1203
1204 fork_out:
1205         if (retval)
1206                 return ERR_PTR(retval);
1207         return p;
1208
1209 bad_fork_cleanup_namespace:
1210         exit_namespace(p);
1211 bad_fork_cleanup_mm:
1212         if (p->mm)
1213                 mmput(p->mm);
1214 bad_fork_cleanup_signal:
1215         exit_signal(p);
1216 bad_fork_cleanup_sighand:
1217         exit_sighand(p);
1218 bad_fork_cleanup_fs:
1219         exit_fs(p); /* blocking */
1220 bad_fork_cleanup_files:
1221         exit_files(p); /* blocking */
1222 bad_fork_cleanup_semundo:
1223         exit_sem(p);
1224 bad_fork_cleanup_audit:
1225         audit_free(p);
1226 bad_fork_cleanup_security:
1227         security_task_free(p);
1228 bad_fork_cleanup_policy:
1229 #ifdef CONFIG_NUMA
1230         mpol_free(p->mempolicy);
1231 #endif
1232 bad_fork_cleanup:
1233         if (p->binfmt)
1234                 module_put(p->binfmt->module);
1235 bad_fork_cleanup_put_domain:
1236         module_put(p->thread_info->exec_domain->module);
1237 bad_fork_cleanup_count:
1238         put_group_info(p->group_info);
1239         atomic_dec(&p->user->processes);
1240         free_uid(p->user);
1241 bad_fork_cleanup_vm:
1242         if (p->mm && !(clone_flags & CLONE_VM))
1243                 vx_pages_sub(p->mm->mm_vx_info, RLIMIT_AS, p->mm->total_vm);
1244 bad_fork_free:
1245         free_task(p);
1246         goto fork_out;
1247 }
1248
1249 struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
1250 {
1251         memset(regs, 0, sizeof(struct pt_regs));
1252         return regs;
1253 }
1254
1255 task_t * __devinit fork_idle(int cpu)
1256 {
1257         task_t *task;
1258         struct pt_regs regs;
1259
1260         task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0);
1261         if (!task)
1262                 return ERR_PTR(-ENOMEM);
1263         init_idle(task, cpu);
1264         unhash_process(task);
1265         return task;
1266 }
1267
1268 static inline int fork_traceflag (unsigned clone_flags)
1269 {
1270         if (clone_flags & CLONE_UNTRACED)
1271                 return 0;
1272         else if (clone_flags & CLONE_VFORK) {
1273                 if (current->ptrace & PT_TRACE_VFORK)
1274                         return PTRACE_EVENT_VFORK;
1275         } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
1276                 if (current->ptrace & PT_TRACE_CLONE)
1277                         return PTRACE_EVENT_CLONE;
1278         } else if (current->ptrace & PT_TRACE_FORK)
1279                 return PTRACE_EVENT_FORK;
1280
1281         return 0;
1282 }
1283
1284 /*
1285  *  Ok, this is the main fork-routine.
1286  *
1287  * It copies the process, and if successful kick-starts
1288  * it and waits for it to finish using the VM if required.
1289  */
1290 long do_fork(unsigned long clone_flags,
1291               unsigned long stack_start,
1292               struct pt_regs *regs,
1293               unsigned long stack_size,
1294               int __user *parent_tidptr,
1295               int __user *child_tidptr)
1296 {
1297         struct task_struct *p;
1298         int trace = 0;
1299         long pid = alloc_pidmap();
1300
1301         if (pid < 0)
1302                 return -EAGAIN;
1303         if (unlikely(current->ptrace)) {
1304                 trace = fork_traceflag (clone_flags);
1305                 if (trace)
1306                         clone_flags |= CLONE_PTRACE;
1307         }
1308
1309 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
1310         if (numtasks_get_ref(current->taskclass, 0) == 0) {
1311                 return -ENOMEM;
1312         }
1313 #endif
1314
1315         p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
1316         /*
1317          * Do this prior waking up the new thread - the thread pointer
1318          * might get invalid after that point, if the thread exits quickly.
1319          */
1320         if (!IS_ERR(p)) {
1321                 struct completion vfork;
1322
1323                 ckrm_cb_fork(p);
1324
1325                 if (clone_flags & CLONE_VFORK) {
1326                         p->vfork_done = &vfork;
1327                         init_completion(&vfork);
1328                 }
1329
1330                 if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
1331                         /*
1332                          * We'll start up with an immediate SIGSTOP.
1333                          */
1334                         sigaddset(&p->pending.signal, SIGSTOP);
1335                         set_tsk_thread_flag(p, TIF_SIGPENDING);
1336                 }
1337
1338                 if (!(clone_flags & CLONE_STOPPED))
1339                         wake_up_new_task(p, clone_flags);
1340                 else
1341                         p->state = TASK_STOPPED;
1342                 ++total_forks;
1343
1344                 if (unlikely (trace)) {
1345                         current->ptrace_message = pid;
1346                         ptrace_notify ((trace << 8) | SIGTRAP);
1347                 }
1348
1349                 if (clone_flags & CLONE_VFORK) {
1350                         wait_for_completion(&vfork);
1351                         if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
1352                                 ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
1353                 }
1354         } else {
1355 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
1356                 numtasks_put_ref(current->taskclass);
1357 #endif
1358                 free_pidmap(pid);
1359                 pid = PTR_ERR(p);
1360         }
1361         return pid;
1362 }
1363
1364 /* SLAB cache for signal_struct structures (tsk->signal) */
1365 kmem_cache_t *signal_cachep;
1366
1367 /* SLAB cache for sighand_struct structures (tsk->sighand) */
1368 kmem_cache_t *sighand_cachep;
1369
1370 /* SLAB cache for files_struct structures (tsk->files) */
1371 kmem_cache_t *files_cachep;
1372
1373 /* SLAB cache for fs_struct structures (tsk->fs) */
1374 kmem_cache_t *fs_cachep;
1375
1376 /* SLAB cache for vm_area_struct structures */
1377 kmem_cache_t *vm_area_cachep;
1378
1379 /* SLAB cache for mm_struct structures (tsk->mm) */
1380 kmem_cache_t *mm_cachep;
1381
1382 void __init proc_caches_init(void)
1383 {
1384         sighand_cachep = kmem_cache_create("sighand_cache",
1385                         sizeof(struct sighand_struct), 0,
1386                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1387         signal_cachep = kmem_cache_create("signal_cache",
1388                         sizeof(struct signal_struct), 0,
1389                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1390         files_cachep = kmem_cache_create("files_cache", 
1391                         sizeof(struct files_struct), 0,
1392                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1393         fs_cachep = kmem_cache_create("fs_cache", 
1394                         sizeof(struct fs_struct), 0,
1395                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1396         vm_area_cachep = kmem_cache_create("vm_area_struct",
1397                         sizeof(struct vm_area_struct), 0,
1398                         SLAB_PANIC, NULL, NULL);
1399         mm_cachep = kmem_cache_create("mm_struct",
1400                         sizeof(struct mm_struct), 0,
1401                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1402 }