fork.c 33 KB

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