fork.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /*
  2. * linux/kernel/fork.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * 'fork.c' contains the help-routines for the 'fork' system call
  8. * (see also entry.S and others).
  9. * Fork is rather simple, once you get the hang of it, but the memory
  10. * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
  11. */
  12. #include <linux/config.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/unistd.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/module.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/completion.h>
  20. #include <linux/namespace.h>
  21. #include <linux/personality.h>
  22. #include <linux/mempolicy.h>
  23. #include <linux/sem.h>
  24. #include <linux/file.h>
  25. #include <linux/key.h>
  26. #include <linux/binfmts.h>
  27. #include <linux/mman.h>
  28. #include <linux/fs.h>
  29. #include <linux/cpu.h>
  30. #include <linux/cpuset.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/acct.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/mmu_context.h>
  46. #include <asm/cacheflush.h>
  47. #include <asm/tlbflush.h>
  48. /*
  49. * Protected counters by write_lock_irq(&tasklist_lock)
  50. */
  51. unsigned long total_forks; /* Handle normal Linux uptimes. */
  52. int nr_threads; /* The idle threads do not count.. */
  53. int max_threads; /* tunable limit on nr_threads */
  54. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  55. __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  56. EXPORT_SYMBOL(tasklist_lock);
  57. int nr_processes(void)
  58. {
  59. int cpu;
  60. int total = 0;
  61. for_each_online_cpu(cpu)
  62. total += per_cpu(process_counts, cpu);
  63. return total;
  64. }
  65. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  66. # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
  67. # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
  68. static kmem_cache_t *task_struct_cachep;
  69. #endif
  70. /* SLAB cache for signal_struct structures (tsk->signal) */
  71. kmem_cache_t *signal_cachep;
  72. /* SLAB cache for sighand_struct structures (tsk->sighand) */
  73. kmem_cache_t *sighand_cachep;
  74. /* SLAB cache for files_struct structures (tsk->files) */
  75. kmem_cache_t *files_cachep;
  76. /* SLAB cache for fs_struct structures (tsk->fs) */
  77. kmem_cache_t *fs_cachep;
  78. /* SLAB cache for vm_area_struct structures */
  79. kmem_cache_t *vm_area_cachep;
  80. /* SLAB cache for mm_struct structures (tsk->mm) */
  81. static kmem_cache_t *mm_cachep;
  82. void free_task(struct task_struct *tsk)
  83. {
  84. free_thread_info(tsk->thread_info);
  85. free_task_struct(tsk);
  86. }
  87. EXPORT_SYMBOL(free_task);
  88. void __put_task_struct(struct task_struct *tsk)
  89. {
  90. WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
  91. WARN_ON(atomic_read(&tsk->usage));
  92. WARN_ON(tsk == current);
  93. if (unlikely(tsk->audit_context))
  94. audit_free(tsk);
  95. security_task_free(tsk);
  96. free_uid(tsk->user);
  97. put_group_info(tsk->group_info);
  98. if (!profile_handoff_task(tsk))
  99. free_task(tsk);
  100. }
  101. void __init fork_init(unsigned long mempages)
  102. {
  103. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  104. #ifndef ARCH_MIN_TASKALIGN
  105. #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
  106. #endif
  107. /* create a slab on which task_structs can be allocated */
  108. task_struct_cachep =
  109. kmem_cache_create("task_struct", sizeof(struct task_struct),
  110. ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
  111. #endif
  112. /*
  113. * The default maximum number of threads is set to a safe
  114. * value: the thread structures can take up at most half
  115. * of memory.
  116. */
  117. max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
  118. /*
  119. * we need to allow at least 20 threads to boot a system
  120. */
  121. if(max_threads < 20)
  122. max_threads = 20;
  123. init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
  124. init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
  125. init_task.signal->rlim[RLIMIT_SIGPENDING] =
  126. init_task.signal->rlim[RLIMIT_NPROC];
  127. }
  128. static struct task_struct *dup_task_struct(struct task_struct *orig)
  129. {
  130. struct task_struct *tsk;
  131. struct thread_info *ti;
  132. prepare_to_copy(orig);
  133. tsk = alloc_task_struct();
  134. if (!tsk)
  135. return NULL;
  136. ti = alloc_thread_info(tsk);
  137. if (!ti) {
  138. free_task_struct(tsk);
  139. return NULL;
  140. }
  141. *ti = *orig->thread_info;
  142. *tsk = *orig;
  143. tsk->thread_info = ti;
  144. ti->task = tsk;
  145. /* One for us, one for whoever does the "release_task()" (usually parent) */
  146. atomic_set(&tsk->usage,2);
  147. atomic_set(&tsk->fs_excl, 0);
  148. return tsk;
  149. }
  150. #ifdef CONFIG_MMU
  151. static inline int dup_mmap(struct mm_struct * mm, struct mm_struct * oldmm)
  152. {
  153. struct vm_area_struct * mpnt, *tmp, **pprev;
  154. struct rb_node **rb_link, *rb_parent;
  155. int retval;
  156. unsigned long charge;
  157. struct mempolicy *pol;
  158. down_write(&oldmm->mmap_sem);
  159. flush_cache_mm(current->mm);
  160. mm->locked_vm = 0;
  161. mm->mmap = NULL;
  162. mm->mmap_cache = NULL;
  163. mm->free_area_cache = oldmm->mmap_base;
  164. mm->cached_hole_size = ~0UL;
  165. mm->map_count = 0;
  166. set_mm_counter(mm, rss, 0);
  167. set_mm_counter(mm, anon_rss, 0);
  168. cpus_clear(mm->cpu_vm_mask);
  169. mm->mm_rb = RB_ROOT;
  170. rb_link = &mm->mm_rb.rb_node;
  171. rb_parent = NULL;
  172. pprev = &mm->mmap;
  173. for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
  174. struct file *file;
  175. if (mpnt->vm_flags & VM_DONTCOPY) {
  176. long pages = vma_pages(mpnt);
  177. mm->total_vm -= pages;
  178. __vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
  179. -pages);
  180. continue;
  181. }
  182. charge = 0;
  183. if (mpnt->vm_flags & VM_ACCOUNT) {
  184. unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
  185. if (security_vm_enough_memory(len))
  186. goto fail_nomem;
  187. charge = len;
  188. }
  189. tmp = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  190. if (!tmp)
  191. goto fail_nomem;
  192. *tmp = *mpnt;
  193. pol = mpol_copy(vma_policy(mpnt));
  194. retval = PTR_ERR(pol);
  195. if (IS_ERR(pol))
  196. goto fail_nomem_policy;
  197. vma_set_policy(tmp, pol);
  198. tmp->vm_flags &= ~VM_LOCKED;
  199. tmp->vm_mm = mm;
  200. tmp->vm_next = NULL;
  201. anon_vma_link(tmp);
  202. file = tmp->vm_file;
  203. if (file) {
  204. struct inode *inode = file->f_dentry->d_inode;
  205. get_file(file);
  206. if (tmp->vm_flags & VM_DENYWRITE)
  207. atomic_dec(&inode->i_writecount);
  208. /* insert tmp into the share list, just after mpnt */
  209. spin_lock(&file->f_mapping->i_mmap_lock);
  210. tmp->vm_truncate_count = mpnt->vm_truncate_count;
  211. flush_dcache_mmap_lock(file->f_mapping);
  212. vma_prio_tree_add(tmp, mpnt);
  213. flush_dcache_mmap_unlock(file->f_mapping);
  214. spin_unlock(&file->f_mapping->i_mmap_lock);
  215. }
  216. /*
  217. * Link in the new vma and copy the page table entries:
  218. * link in first so that swapoff can see swap entries.
  219. * Note that, exceptionally, here the vma is inserted
  220. * without holding mm->mmap_sem.
  221. */
  222. spin_lock(&mm->page_table_lock);
  223. *pprev = tmp;
  224. pprev = &tmp->vm_next;
  225. __vma_link_rb(mm, tmp, rb_link, rb_parent);
  226. rb_link = &tmp->vm_rb.rb_right;
  227. rb_parent = &tmp->vm_rb;
  228. mm->map_count++;
  229. retval = copy_page_range(mm, current->mm, tmp);
  230. spin_unlock(&mm->page_table_lock);
  231. if (tmp->vm_ops && tmp->vm_ops->open)
  232. tmp->vm_ops->open(tmp);
  233. if (retval)
  234. goto out;
  235. }
  236. retval = 0;
  237. out:
  238. flush_tlb_mm(current->mm);
  239. up_write(&oldmm->mmap_sem);
  240. return retval;
  241. fail_nomem_policy:
  242. kmem_cache_free(vm_area_cachep, tmp);
  243. fail_nomem:
  244. retval = -ENOMEM;
  245. vm_unacct_memory(charge);
  246. goto out;
  247. }
  248. static inline int mm_alloc_pgd(struct mm_struct * mm)
  249. {
  250. mm->pgd = pgd_alloc(mm);
  251. if (unlikely(!mm->pgd))
  252. return -ENOMEM;
  253. return 0;
  254. }
  255. static inline void mm_free_pgd(struct mm_struct * mm)
  256. {
  257. pgd_free(mm->pgd);
  258. }
  259. #else
  260. #define dup_mmap(mm, oldmm) (0)
  261. #define mm_alloc_pgd(mm) (0)
  262. #define mm_free_pgd(mm)
  263. #endif /* CONFIG_MMU */
  264. __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
  265. #define allocate_mm() (kmem_cache_alloc(mm_cachep, SLAB_KERNEL))
  266. #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
  267. #include <linux/init_task.h>
  268. static struct mm_struct * mm_init(struct mm_struct * mm)
  269. {
  270. atomic_set(&mm->mm_users, 1);
  271. atomic_set(&mm->mm_count, 1);
  272. init_rwsem(&mm->mmap_sem);
  273. INIT_LIST_HEAD(&mm->mmlist);
  274. mm->core_waiters = 0;
  275. mm->nr_ptes = 0;
  276. spin_lock_init(&mm->page_table_lock);
  277. rwlock_init(&mm->ioctx_list_lock);
  278. mm->ioctx_list = NULL;
  279. mm->default_kioctx = (struct kioctx)INIT_KIOCTX(mm->default_kioctx, *mm);
  280. mm->free_area_cache = TASK_UNMAPPED_BASE;
  281. mm->cached_hole_size = ~0UL;
  282. if (likely(!mm_alloc_pgd(mm))) {
  283. mm->def_flags = 0;
  284. return mm;
  285. }
  286. free_mm(mm);
  287. return NULL;
  288. }
  289. /*
  290. * Allocate and initialize an mm_struct.
  291. */
  292. struct mm_struct * mm_alloc(void)
  293. {
  294. struct mm_struct * mm;
  295. mm = allocate_mm();
  296. if (mm) {
  297. memset(mm, 0, sizeof(*mm));
  298. mm = mm_init(mm);
  299. }
  300. return mm;
  301. }
  302. /*
  303. * Called when the last reference to the mm
  304. * is dropped: either by a lazy thread or by
  305. * mmput. Free the page directory and the mm.
  306. */
  307. void fastcall __mmdrop(struct mm_struct *mm)
  308. {
  309. BUG_ON(mm == &init_mm);
  310. mm_free_pgd(mm);
  311. destroy_context(mm);
  312. free_mm(mm);
  313. }
  314. /*
  315. * Decrement the use count and release all resources for an mm.
  316. */
  317. void mmput(struct mm_struct *mm)
  318. {
  319. if (atomic_dec_and_test(&mm->mm_users)) {
  320. exit_aio(mm);
  321. exit_mmap(mm);
  322. if (!list_empty(&mm->mmlist)) {
  323. spin_lock(&mmlist_lock);
  324. list_del(&mm->mmlist);
  325. spin_unlock(&mmlist_lock);
  326. }
  327. put_swap_token(mm);
  328. mmdrop(mm);
  329. }
  330. }
  331. EXPORT_SYMBOL_GPL(mmput);
  332. /**
  333. * get_task_mm - acquire a reference to the task's mm
  334. *
  335. * Returns %NULL if the task has no mm. Checks PF_BORROWED_MM (meaning
  336. * this kernel workthread has transiently adopted a user mm with use_mm,
  337. * to do its AIO) is not set and if so returns a reference to it, after
  338. * bumping up the use count. User must release the mm via mmput()
  339. * after use. Typically used by /proc and ptrace.
  340. */
  341. struct mm_struct *get_task_mm(struct task_struct *task)
  342. {
  343. struct mm_struct *mm;
  344. task_lock(task);
  345. mm = task->mm;
  346. if (mm) {
  347. if (task->flags & PF_BORROWED_MM)
  348. mm = NULL;
  349. else
  350. atomic_inc(&mm->mm_users);
  351. }
  352. task_unlock(task);
  353. return mm;
  354. }
  355. EXPORT_SYMBOL_GPL(get_task_mm);
  356. /* Please note the differences between mmput and mm_release.
  357. * mmput is called whenever we stop holding onto a mm_struct,
  358. * error success whatever.
  359. *
  360. * mm_release is called after a mm_struct has been removed
  361. * from the current process.
  362. *
  363. * This difference is important for error handling, when we
  364. * only half set up a mm_struct for a new process and need to restore
  365. * the old one. Because we mmput the new mm_struct before
  366. * restoring the old one. . .
  367. * Eric Biederman 10 January 1998
  368. */
  369. void mm_release(struct task_struct *tsk, struct mm_struct *mm)
  370. {
  371. struct completion *vfork_done = tsk->vfork_done;
  372. /* Get rid of any cached register state */
  373. deactivate_mm(tsk, mm);
  374. /* notify parent sleeping on vfork() */
  375. if (vfork_done) {
  376. tsk->vfork_done = NULL;
  377. complete(vfork_done);
  378. }
  379. if (tsk->clear_child_tid && atomic_read(&mm->mm_users) > 1) {
  380. u32 __user * tidptr = tsk->clear_child_tid;
  381. tsk->clear_child_tid = NULL;
  382. /*
  383. * We don't check the error code - if userspace has
  384. * not set up a proper pointer then tough luck.
  385. */
  386. put_user(0, tidptr);
  387. sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
  388. }
  389. }
  390. static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
  391. {
  392. struct mm_struct * mm, *oldmm;
  393. int retval;
  394. tsk->min_flt = tsk->maj_flt = 0;
  395. tsk->nvcsw = tsk->nivcsw = 0;
  396. tsk->mm = NULL;
  397. tsk->active_mm = NULL;
  398. /*
  399. * Are we cloning a kernel thread?
  400. *
  401. * We need to steal a active VM for that..
  402. */
  403. oldmm = current->mm;
  404. if (!oldmm)
  405. return 0;
  406. if (clone_flags & CLONE_VM) {
  407. atomic_inc(&oldmm->mm_users);
  408. mm = oldmm;
  409. /*
  410. * There are cases where the PTL is held to ensure no
  411. * new threads start up in user mode using an mm, which
  412. * allows optimizing out ipis; the tlb_gather_mmu code
  413. * is an example.
  414. */
  415. spin_unlock_wait(&oldmm->page_table_lock);
  416. goto good_mm;
  417. }
  418. retval = -ENOMEM;
  419. mm = allocate_mm();
  420. if (!mm)
  421. goto fail_nomem;
  422. /* Copy the current MM stuff.. */
  423. memcpy(mm, oldmm, sizeof(*mm));
  424. if (!mm_init(mm))
  425. goto fail_nomem;
  426. if (init_new_context(tsk,mm))
  427. goto fail_nocontext;
  428. retval = dup_mmap(mm, oldmm);
  429. if (retval)
  430. goto free_pt;
  431. mm->hiwater_rss = get_mm_counter(mm,rss);
  432. mm->hiwater_vm = mm->total_vm;
  433. good_mm:
  434. tsk->mm = mm;
  435. tsk->active_mm = mm;
  436. return 0;
  437. free_pt:
  438. mmput(mm);
  439. fail_nomem:
  440. return retval;
  441. fail_nocontext:
  442. /*
  443. * If init_new_context() failed, we cannot use mmput() to free the mm
  444. * because it calls destroy_context()
  445. */
  446. mm_free_pgd(mm);
  447. free_mm(mm);
  448. return retval;
  449. }
  450. static inline struct fs_struct *__copy_fs_struct(struct fs_struct *old)
  451. {
  452. struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
  453. /* We don't need to lock fs - think why ;-) */
  454. if (fs) {
  455. atomic_set(&fs->count, 1);
  456. rwlock_init(&fs->lock);
  457. fs->umask = old->umask;
  458. read_lock(&old->lock);
  459. fs->rootmnt = mntget(old->rootmnt);
  460. fs->root = dget(old->root);
  461. fs->pwdmnt = mntget(old->pwdmnt);
  462. fs->pwd = dget(old->pwd);
  463. if (old->altroot) {
  464. fs->altrootmnt = mntget(old->altrootmnt);
  465. fs->altroot = dget(old->altroot);
  466. } else {
  467. fs->altrootmnt = NULL;
  468. fs->altroot = NULL;
  469. }
  470. read_unlock(&old->lock);
  471. }
  472. return fs;
  473. }
  474. struct fs_struct *copy_fs_struct(struct fs_struct *old)
  475. {
  476. return __copy_fs_struct(old);
  477. }
  478. EXPORT_SYMBOL_GPL(copy_fs_struct);
  479. static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
  480. {
  481. if (clone_flags & CLONE_FS) {
  482. atomic_inc(&current->fs->count);
  483. return 0;
  484. }
  485. tsk->fs = __copy_fs_struct(current->fs);
  486. if (!tsk->fs)
  487. return -ENOMEM;
  488. return 0;
  489. }
  490. static int count_open_files(struct files_struct *files, int size)
  491. {
  492. int i;
  493. struct fdtable *fdt;
  494. /* Find the last open fd */
  495. fdt = files_fdtable(files);
  496. for (i = size/(8*sizeof(long)); i > 0; ) {
  497. if (fdt->open_fds->fds_bits[--i])
  498. break;
  499. }
  500. i = (i+1) * 8 * sizeof(long);
  501. return i;
  502. }
  503. static struct files_struct *alloc_files(void)
  504. {
  505. struct files_struct *newf;
  506. struct fdtable *fdt;
  507. newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
  508. if (!newf)
  509. goto out;
  510. atomic_set(&newf->count, 1);
  511. spin_lock_init(&newf->file_lock);
  512. fdt = files_fdtable(newf);
  513. fdt->next_fd = 0;
  514. fdt->max_fds = NR_OPEN_DEFAULT;
  515. fdt->max_fdset = __FD_SETSIZE;
  516. fdt->close_on_exec = &newf->close_on_exec_init;
  517. fdt->open_fds = &newf->open_fds_init;
  518. fdt->fd = &newf->fd_array[0];
  519. out:
  520. return newf;
  521. }
  522. static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
  523. {
  524. struct files_struct *oldf, *newf;
  525. struct file **old_fds, **new_fds;
  526. int open_files, size, i, error = 0, expand;
  527. struct fdtable *old_fdt, *new_fdt;
  528. /*
  529. * A background process may not have any files ...
  530. */
  531. oldf = current->files;
  532. if (!oldf)
  533. goto out;
  534. if (clone_flags & CLONE_FILES) {
  535. atomic_inc(&oldf->count);
  536. goto out;
  537. }
  538. /*
  539. * Note: we may be using current for both targets (See exec.c)
  540. * This works because we cache current->files (old) as oldf. Don't
  541. * break this.
  542. */
  543. tsk->files = NULL;
  544. error = -ENOMEM;
  545. newf = alloc_files();
  546. if (!newf)
  547. goto out;
  548. spin_lock(&oldf->file_lock);
  549. old_fdt = files_fdtable(oldf);
  550. new_fdt = files_fdtable(newf);
  551. size = old_fdt->max_fdset;
  552. open_files = count_open_files(oldf, old_fdt->max_fdset);
  553. expand = 0;
  554. /*
  555. * Check whether we need to allocate a larger fd array or fd set.
  556. * Note: we're not a clone task, so the open count won't change.
  557. */
  558. if (open_files > new_fdt->max_fdset) {
  559. new_fdt->max_fdset = 0;
  560. expand = 1;
  561. }
  562. if (open_files > new_fdt->max_fds) {
  563. new_fdt->max_fds = 0;
  564. expand = 1;
  565. }
  566. /* if the old fdset gets grown now, we'll only copy up to "size" fds */
  567. if (expand) {
  568. spin_unlock(&oldf->file_lock);
  569. spin_lock(&newf->file_lock);
  570. error = expand_files(newf, open_files-1);
  571. spin_unlock(&newf->file_lock);
  572. if (error < 0)
  573. goto out_release;
  574. spin_lock(&oldf->file_lock);
  575. }
  576. old_fds = old_fdt->fd;
  577. new_fds = new_fdt->fd;
  578. memcpy(new_fdt->open_fds->fds_bits, old_fdt->open_fds->fds_bits, open_files/8);
  579. memcpy(new_fdt->close_on_exec->fds_bits, old_fdt->close_on_exec->fds_bits, open_files/8);
  580. for (i = open_files; i != 0; i--) {
  581. struct file *f = *old_fds++;
  582. if (f) {
  583. get_file(f);
  584. } else {
  585. /*
  586. * The fd may be claimed in the fd bitmap but not yet
  587. * instantiated in the files array if a sibling thread
  588. * is partway through open(). So make sure that this
  589. * fd is available to the new process.
  590. */
  591. FD_CLR(open_files - i, new_fdt->open_fds);
  592. }
  593. *new_fds++ = f;
  594. }
  595. spin_unlock(&oldf->file_lock);
  596. /* compute the remainder to be cleared */
  597. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  598. /* This is long word aligned thus could use a optimized version */
  599. memset(new_fds, 0, size);
  600. if (new_fdt->max_fdset > open_files) {
  601. int left = (new_fdt->max_fdset-open_files)/8;
  602. int start = open_files / (8 * sizeof(unsigned long));
  603. memset(&new_fdt->open_fds->fds_bits[start], 0, left);
  604. memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
  605. }
  606. tsk->files = newf;
  607. error = 0;
  608. out:
  609. return error;
  610. out_release:
  611. free_fdset (new_fdt->close_on_exec, new_fdt->max_fdset);
  612. free_fdset (new_fdt->open_fds, new_fdt->max_fdset);
  613. free_fd_array(new_fdt->fd, new_fdt->max_fds);
  614. kmem_cache_free(files_cachep, newf);
  615. goto out;
  616. }
  617. /*
  618. * Helper to unshare the files of the current task.
  619. * We don't want to expose copy_files internals to
  620. * the exec layer of the kernel.
  621. */
  622. int unshare_files(void)
  623. {
  624. struct files_struct *files = current->files;
  625. int rc;
  626. if(!files)
  627. BUG();
  628. /* This can race but the race causes us to copy when we don't
  629. need to and drop the copy */
  630. if(atomic_read(&files->count) == 1)
  631. {
  632. atomic_inc(&files->count);
  633. return 0;
  634. }
  635. rc = copy_files(0, current);
  636. if(rc)
  637. current->files = files;
  638. return rc;
  639. }
  640. EXPORT_SYMBOL(unshare_files);
  641. static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
  642. {
  643. struct sighand_struct *sig;
  644. if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
  645. atomic_inc(&current->sighand->count);
  646. return 0;
  647. }
  648. sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  649. tsk->sighand = sig;
  650. if (!sig)
  651. return -ENOMEM;
  652. spin_lock_init(&sig->siglock);
  653. atomic_set(&sig->count, 1);
  654. memcpy(sig->action, current->sighand->action, sizeof(sig->action));
  655. return 0;
  656. }
  657. static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
  658. {
  659. struct signal_struct *sig;
  660. int ret;
  661. if (clone_flags & CLONE_THREAD) {
  662. atomic_inc(&current->signal->count);
  663. atomic_inc(&current->signal->live);
  664. return 0;
  665. }
  666. sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
  667. tsk->signal = sig;
  668. if (!sig)
  669. return -ENOMEM;
  670. ret = copy_thread_group_keys(tsk);
  671. if (ret < 0) {
  672. kmem_cache_free(signal_cachep, sig);
  673. return ret;
  674. }
  675. atomic_set(&sig->count, 1);
  676. atomic_set(&sig->live, 1);
  677. init_waitqueue_head(&sig->wait_chldexit);
  678. sig->flags = 0;
  679. sig->group_exit_code = 0;
  680. sig->group_exit_task = NULL;
  681. sig->group_stop_count = 0;
  682. sig->curr_target = NULL;
  683. init_sigpending(&sig->shared_pending);
  684. INIT_LIST_HEAD(&sig->posix_timers);
  685. sig->it_real_value = sig->it_real_incr = 0;
  686. sig->real_timer.function = it_real_fn;
  687. sig->real_timer.data = (unsigned long) tsk;
  688. init_timer(&sig->real_timer);
  689. sig->it_virt_expires = cputime_zero;
  690. sig->it_virt_incr = cputime_zero;
  691. sig->it_prof_expires = cputime_zero;
  692. sig->it_prof_incr = cputime_zero;
  693. sig->tty = current->signal->tty;
  694. sig->pgrp = process_group(current);
  695. sig->session = current->signal->session;
  696. sig->leader = 0; /* session leadership doesn't inherit */
  697. sig->tty_old_pgrp = 0;
  698. sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
  699. sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
  700. sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
  701. sig->sched_time = 0;
  702. INIT_LIST_HEAD(&sig->cpu_timers[0]);
  703. INIT_LIST_HEAD(&sig->cpu_timers[1]);
  704. INIT_LIST_HEAD(&sig->cpu_timers[2]);
  705. task_lock(current->group_leader);
  706. memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
  707. task_unlock(current->group_leader);
  708. if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
  709. /*
  710. * New sole thread in the process gets an expiry time
  711. * of the whole CPU time limit.
  712. */
  713. tsk->it_prof_expires =
  714. secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
  715. }
  716. return 0;
  717. }
  718. static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
  719. {
  720. unsigned long new_flags = p->flags;
  721. new_flags &= ~PF_SUPERPRIV;
  722. new_flags |= PF_FORKNOEXEC;
  723. if (!(clone_flags & CLONE_PTRACE))
  724. p->ptrace = 0;
  725. p->flags = new_flags;
  726. }
  727. asmlinkage long sys_set_tid_address(int __user *tidptr)
  728. {
  729. current->clear_child_tid = tidptr;
  730. return current->pid;
  731. }
  732. /*
  733. * This creates a new process as a copy of the old one,
  734. * but does not actually start it yet.
  735. *
  736. * It copies the registers, and all the appropriate
  737. * parts of the process environment (as per the clone
  738. * flags). The actual kick-off is left to the caller.
  739. */
  740. static task_t *copy_process(unsigned long clone_flags,
  741. unsigned long stack_start,
  742. struct pt_regs *regs,
  743. unsigned long stack_size,
  744. int __user *parent_tidptr,
  745. int __user *child_tidptr,
  746. int pid)
  747. {
  748. int retval;
  749. struct task_struct *p = NULL;
  750. if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
  751. return ERR_PTR(-EINVAL);
  752. /*
  753. * Thread groups must share signals as well, and detached threads
  754. * can only be started up within the thread group.
  755. */
  756. if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
  757. return ERR_PTR(-EINVAL);
  758. /*
  759. * Shared signal handlers imply shared VM. By way of the above,
  760. * thread groups also imply shared VM. Blocking this case allows
  761. * for various simplifications in other code.
  762. */
  763. if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
  764. return ERR_PTR(-EINVAL);
  765. retval = security_task_create(clone_flags);
  766. if (retval)
  767. goto fork_out;
  768. retval = -ENOMEM;
  769. p = dup_task_struct(current);
  770. if (!p)
  771. goto fork_out;
  772. retval = -EAGAIN;
  773. if (atomic_read(&p->user->processes) >=
  774. p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
  775. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
  776. p->user != &root_user)
  777. goto bad_fork_free;
  778. }
  779. atomic_inc(&p->user->__count);
  780. atomic_inc(&p->user->processes);
  781. get_group_info(p->group_info);
  782. /*
  783. * If multiple threads are within copy_process(), then this check
  784. * triggers too late. This doesn't hurt, the check is only there
  785. * to stop root fork bombs.
  786. */
  787. if (nr_threads >= max_threads)
  788. goto bad_fork_cleanup_count;
  789. if (!try_module_get(p->thread_info->exec_domain->module))
  790. goto bad_fork_cleanup_count;
  791. if (p->binfmt && !try_module_get(p->binfmt->module))
  792. goto bad_fork_cleanup_put_domain;
  793. p->did_exec = 0;
  794. copy_flags(clone_flags, p);
  795. p->pid = pid;
  796. retval = -EFAULT;
  797. if (clone_flags & CLONE_PARENT_SETTID)
  798. if (put_user(p->pid, parent_tidptr))
  799. goto bad_fork_cleanup;
  800. p->proc_dentry = NULL;
  801. INIT_LIST_HEAD(&p->children);
  802. INIT_LIST_HEAD(&p->sibling);
  803. p->vfork_done = NULL;
  804. spin_lock_init(&p->alloc_lock);
  805. spin_lock_init(&p->proc_lock);
  806. clear_tsk_thread_flag(p, TIF_SIGPENDING);
  807. init_sigpending(&p->pending);
  808. p->utime = cputime_zero;
  809. p->stime = cputime_zero;
  810. p->sched_time = 0;
  811. p->rchar = 0; /* I/O counter: bytes read */
  812. p->wchar = 0; /* I/O counter: bytes written */
  813. p->syscr = 0; /* I/O counter: read syscalls */
  814. p->syscw = 0; /* I/O counter: write syscalls */
  815. acct_clear_integrals(p);
  816. p->it_virt_expires = cputime_zero;
  817. p->it_prof_expires = cputime_zero;
  818. p->it_sched_expires = 0;
  819. INIT_LIST_HEAD(&p->cpu_timers[0]);
  820. INIT_LIST_HEAD(&p->cpu_timers[1]);
  821. INIT_LIST_HEAD(&p->cpu_timers[2]);
  822. p->lock_depth = -1; /* -1 = no lock */
  823. do_posix_clock_monotonic_gettime(&p->start_time);
  824. p->security = NULL;
  825. p->io_context = NULL;
  826. p->io_wait = NULL;
  827. p->audit_context = NULL;
  828. #ifdef CONFIG_NUMA
  829. p->mempolicy = mpol_copy(p->mempolicy);
  830. if (IS_ERR(p->mempolicy)) {
  831. retval = PTR_ERR(p->mempolicy);
  832. p->mempolicy = NULL;
  833. goto bad_fork_cleanup;
  834. }
  835. #endif
  836. p->tgid = p->pid;
  837. if (clone_flags & CLONE_THREAD)
  838. p->tgid = current->tgid;
  839. if ((retval = security_task_alloc(p)))
  840. goto bad_fork_cleanup_policy;
  841. if ((retval = audit_alloc(p)))
  842. goto bad_fork_cleanup_security;
  843. /* copy all the process information */
  844. if ((retval = copy_semundo(clone_flags, p)))
  845. goto bad_fork_cleanup_audit;
  846. if ((retval = copy_files(clone_flags, p)))
  847. goto bad_fork_cleanup_semundo;
  848. if ((retval = copy_fs(clone_flags, p)))
  849. goto bad_fork_cleanup_files;
  850. if ((retval = copy_sighand(clone_flags, p)))
  851. goto bad_fork_cleanup_fs;
  852. if ((retval = copy_signal(clone_flags, p)))
  853. goto bad_fork_cleanup_sighand;
  854. if ((retval = copy_mm(clone_flags, p)))
  855. goto bad_fork_cleanup_signal;
  856. if ((retval = copy_keys(clone_flags, p)))
  857. goto bad_fork_cleanup_mm;
  858. if ((retval = copy_namespace(clone_flags, p)))
  859. goto bad_fork_cleanup_keys;
  860. retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
  861. if (retval)
  862. goto bad_fork_cleanup_namespace;
  863. p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
  864. /*
  865. * Clear TID on mm_release()?
  866. */
  867. p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
  868. /*
  869. * Syscall tracing should be turned off in the child regardless
  870. * of CLONE_PTRACE.
  871. */
  872. clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
  873. #ifdef TIF_SYSCALL_EMU
  874. clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
  875. #endif
  876. /* Our parent execution domain becomes current domain
  877. These must match for thread signalling to apply */
  878. p->parent_exec_id = p->self_exec_id;
  879. /* ok, now we should be set up.. */
  880. p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
  881. p->pdeath_signal = 0;
  882. p->exit_state = 0;
  883. /*
  884. * Ok, make it visible to the rest of the system.
  885. * We dont wake it up yet.
  886. */
  887. p->group_leader = p;
  888. INIT_LIST_HEAD(&p->ptrace_children);
  889. INIT_LIST_HEAD(&p->ptrace_list);
  890. /* Perform scheduler related setup. Assign this task to a CPU. */
  891. sched_fork(p, clone_flags);
  892. /* Need tasklist lock for parent etc handling! */
  893. write_lock_irq(&tasklist_lock);
  894. /*
  895. * The task hasn't been attached yet, so its cpus_allowed mask will
  896. * not be changed, nor will its assigned CPU.
  897. *
  898. * The cpus_allowed mask of the parent may have changed after it was
  899. * copied first time - so re-copy it here, then check the child's CPU
  900. * to ensure it is on a valid CPU (and if not, just force it back to
  901. * parent's CPU). This avoids alot of nasty races.
  902. */
  903. p->cpus_allowed = current->cpus_allowed;
  904. if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed)))
  905. set_task_cpu(p, smp_processor_id());
  906. /*
  907. * Check for pending SIGKILL! The new thread should not be allowed
  908. * to slip out of an OOM kill. (or normal SIGKILL.)
  909. */
  910. if (sigismember(&current->pending.signal, SIGKILL)) {
  911. write_unlock_irq(&tasklist_lock);
  912. retval = -EINTR;
  913. goto bad_fork_cleanup_namespace;
  914. }
  915. /* CLONE_PARENT re-uses the old parent */
  916. if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
  917. p->real_parent = current->real_parent;
  918. else
  919. p->real_parent = current;
  920. p->parent = p->real_parent;
  921. if (clone_flags & CLONE_THREAD) {
  922. spin_lock(&current->sighand->siglock);
  923. /*
  924. * Important: if an exit-all has been started then
  925. * do not create this new thread - the whole thread
  926. * group is supposed to exit anyway.
  927. */
  928. if (current->signal->flags & SIGNAL_GROUP_EXIT) {
  929. spin_unlock(&current->sighand->siglock);
  930. write_unlock_irq(&tasklist_lock);
  931. retval = -EAGAIN;
  932. goto bad_fork_cleanup_namespace;
  933. }
  934. p->group_leader = current->group_leader;
  935. if (current->signal->group_stop_count > 0) {
  936. /*
  937. * There is an all-stop in progress for the group.
  938. * We ourselves will stop as soon as we check signals.
  939. * Make the new thread part of that group stop too.
  940. */
  941. current->signal->group_stop_count++;
  942. set_tsk_thread_flag(p, TIF_SIGPENDING);
  943. }
  944. if (!cputime_eq(current->signal->it_virt_expires,
  945. cputime_zero) ||
  946. !cputime_eq(current->signal->it_prof_expires,
  947. cputime_zero) ||
  948. current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
  949. !list_empty(&current->signal->cpu_timers[0]) ||
  950. !list_empty(&current->signal->cpu_timers[1]) ||
  951. !list_empty(&current->signal->cpu_timers[2])) {
  952. /*
  953. * Have child wake up on its first tick to check
  954. * for process CPU timers.
  955. */
  956. p->it_prof_expires = jiffies_to_cputime(1);
  957. }
  958. spin_unlock(&current->sighand->siglock);
  959. }
  960. /*
  961. * inherit ioprio
  962. */
  963. p->ioprio = current->ioprio;
  964. SET_LINKS(p);
  965. if (unlikely(p->ptrace & PT_PTRACED))
  966. __ptrace_link(p, current->parent);
  967. cpuset_fork(p);
  968. attach_pid(p, PIDTYPE_PID, p->pid);
  969. attach_pid(p, PIDTYPE_TGID, p->tgid);
  970. if (thread_group_leader(p)) {
  971. attach_pid(p, PIDTYPE_PGID, process_group(p));
  972. attach_pid(p, PIDTYPE_SID, p->signal->session);
  973. if (p->pid)
  974. __get_cpu_var(process_counts)++;
  975. }
  976. if (!current->signal->tty && p->signal->tty)
  977. p->signal->tty = NULL;
  978. nr_threads++;
  979. total_forks++;
  980. write_unlock_irq(&tasklist_lock);
  981. retval = 0;
  982. fork_out:
  983. if (retval)
  984. return ERR_PTR(retval);
  985. return p;
  986. bad_fork_cleanup_namespace:
  987. exit_namespace(p);
  988. bad_fork_cleanup_keys:
  989. exit_keys(p);
  990. bad_fork_cleanup_mm:
  991. if (p->mm)
  992. mmput(p->mm);
  993. bad_fork_cleanup_signal:
  994. exit_signal(p);
  995. bad_fork_cleanup_sighand:
  996. exit_sighand(p);
  997. bad_fork_cleanup_fs:
  998. exit_fs(p); /* blocking */
  999. bad_fork_cleanup_files:
  1000. exit_files(p); /* blocking */
  1001. bad_fork_cleanup_semundo:
  1002. exit_sem(p);
  1003. bad_fork_cleanup_audit:
  1004. audit_free(p);
  1005. bad_fork_cleanup_security:
  1006. security_task_free(p);
  1007. bad_fork_cleanup_policy:
  1008. #ifdef CONFIG_NUMA
  1009. mpol_free(p->mempolicy);
  1010. #endif
  1011. bad_fork_cleanup:
  1012. if (p->binfmt)
  1013. module_put(p->binfmt->module);
  1014. bad_fork_cleanup_put_domain:
  1015. module_put(p->thread_info->exec_domain->module);
  1016. bad_fork_cleanup_count:
  1017. put_group_info(p->group_info);
  1018. atomic_dec(&p->user->processes);
  1019. free_uid(p->user);
  1020. bad_fork_free:
  1021. free_task(p);
  1022. goto fork_out;
  1023. }
  1024. struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
  1025. {
  1026. memset(regs, 0, sizeof(struct pt_regs));
  1027. return regs;
  1028. }
  1029. task_t * __devinit fork_idle(int cpu)
  1030. {
  1031. task_t *task;
  1032. struct pt_regs regs;
  1033. task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0);
  1034. if (!task)
  1035. return ERR_PTR(-ENOMEM);
  1036. init_idle(task, cpu);
  1037. unhash_process(task);
  1038. return task;
  1039. }
  1040. static inline int fork_traceflag (unsigned clone_flags)
  1041. {
  1042. if (clone_flags & CLONE_UNTRACED)
  1043. return 0;
  1044. else if (clone_flags & CLONE_VFORK) {
  1045. if (current->ptrace & PT_TRACE_VFORK)
  1046. return PTRACE_EVENT_VFORK;
  1047. } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
  1048. if (current->ptrace & PT_TRACE_CLONE)
  1049. return PTRACE_EVENT_CLONE;
  1050. } else if (current->ptrace & PT_TRACE_FORK)
  1051. return PTRACE_EVENT_FORK;
  1052. return 0;
  1053. }
  1054. /*
  1055. * Ok, this is the main fork-routine.
  1056. *
  1057. * It copies the process, and if successful kick-starts
  1058. * it and waits for it to finish using the VM if required.
  1059. */
  1060. long do_fork(unsigned long clone_flags,
  1061. unsigned long stack_start,
  1062. struct pt_regs *regs,
  1063. unsigned long stack_size,
  1064. int __user *parent_tidptr,
  1065. int __user *child_tidptr)
  1066. {
  1067. struct task_struct *p;
  1068. int trace = 0;
  1069. long pid = alloc_pidmap();
  1070. if (pid < 0)
  1071. return -EAGAIN;
  1072. if (unlikely(current->ptrace)) {
  1073. trace = fork_traceflag (clone_flags);
  1074. if (trace)
  1075. clone_flags |= CLONE_PTRACE;
  1076. }
  1077. p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
  1078. /*
  1079. * Do this prior waking up the new thread - the thread pointer
  1080. * might get invalid after that point, if the thread exits quickly.
  1081. */
  1082. if (!IS_ERR(p)) {
  1083. struct completion vfork;
  1084. if (clone_flags & CLONE_VFORK) {
  1085. p->vfork_done = &vfork;
  1086. init_completion(&vfork);
  1087. }
  1088. if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
  1089. /*
  1090. * We'll start up with an immediate SIGSTOP.
  1091. */
  1092. sigaddset(&p->pending.signal, SIGSTOP);
  1093. set_tsk_thread_flag(p, TIF_SIGPENDING);
  1094. }
  1095. if (!(clone_flags & CLONE_STOPPED))
  1096. wake_up_new_task(p, clone_flags);
  1097. else
  1098. p->state = TASK_STOPPED;
  1099. if (unlikely (trace)) {
  1100. current->ptrace_message = pid;
  1101. ptrace_notify ((trace << 8) | SIGTRAP);
  1102. }
  1103. if (clone_flags & CLONE_VFORK) {
  1104. wait_for_completion(&vfork);
  1105. if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE))
  1106. ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
  1107. }
  1108. } else {
  1109. free_pidmap(pid);
  1110. pid = PTR_ERR(p);
  1111. }
  1112. return pid;
  1113. }
  1114. void __init proc_caches_init(void)
  1115. {
  1116. sighand_cachep = kmem_cache_create("sighand_cache",
  1117. sizeof(struct sighand_struct), 0,
  1118. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1119. signal_cachep = kmem_cache_create("signal_cache",
  1120. sizeof(struct signal_struct), 0,
  1121. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1122. files_cachep = kmem_cache_create("files_cache",
  1123. sizeof(struct files_struct), 0,
  1124. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1125. fs_cachep = kmem_cache_create("fs_cache",
  1126. sizeof(struct fs_struct), 0,
  1127. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1128. vm_area_cachep = kmem_cache_create("vm_area_struct",
  1129. sizeof(struct vm_area_struct), 0,
  1130. SLAB_PANIC, NULL, NULL);
  1131. mm_cachep = kmem_cache_create("mm_struct",
  1132. sizeof(struct mm_struct), 0,
  1133. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1134. }