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