fork.c 32 KB

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