fork.c 32 KB

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