fork.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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/capability.h>
  30. #include <linux/cpu.h>
  31. #include <linux/cpuset.h>
  32. #include <linux/security.h>
  33. #include <linux/swap.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/jiffies.h>
  36. #include <linux/futex.h>
  37. #include <linux/rcupdate.h>
  38. #include <linux/ptrace.h>
  39. #include <linux/mount.h>
  40. #include <linux/audit.h>
  41. #include <linux/profile.h>
  42. #include <linux/rmap.h>
  43. #include <linux/acct.h>
  44. #include <linux/cn_proc.h>
  45. #include <asm/pgtable.h>
  46. #include <asm/pgalloc.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/mmu_context.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/tlbflush.h>
  51. /*
  52. * Protected counters by write_lock_irq(&tasklist_lock)
  53. */
  54. unsigned long total_forks; /* Handle normal Linux uptimes. */
  55. int nr_threads; /* The idle threads do not count.. */
  56. int max_threads; /* tunable limit on nr_threads */
  57. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  58. __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  59. EXPORT_SYMBOL(tasklist_lock);
  60. int nr_processes(void)
  61. {
  62. int cpu;
  63. int total = 0;
  64. for_each_online_cpu(cpu)
  65. total += per_cpu(process_counts, cpu);
  66. return total;
  67. }
  68. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  69. # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
  70. # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
  71. static kmem_cache_t *task_struct_cachep;
  72. #endif
  73. /* SLAB cache for signal_struct structures (tsk->signal) */
  74. kmem_cache_t *signal_cachep;
  75. /* SLAB cache for sighand_struct structures (tsk->sighand) */
  76. kmem_cache_t *sighand_cachep;
  77. /* SLAB cache for files_struct structures (tsk->files) */
  78. kmem_cache_t *files_cachep;
  79. /* SLAB cache for fs_struct structures (tsk->fs) */
  80. kmem_cache_t *fs_cachep;
  81. /* SLAB cache for vm_area_struct structures */
  82. kmem_cache_t *vm_area_cachep;
  83. /* SLAB cache for mm_struct structures (tsk->mm) */
  84. static kmem_cache_t *mm_cachep;
  85. void free_task(struct task_struct *tsk)
  86. {
  87. free_thread_info(tsk->thread_info);
  88. free_task_struct(tsk);
  89. }
  90. EXPORT_SYMBOL(free_task);
  91. void __put_task_struct(struct task_struct *tsk)
  92. {
  93. WARN_ON(!(tsk->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)));
  94. WARN_ON(atomic_read(&tsk->usage));
  95. WARN_ON(tsk == current);
  96. if (unlikely(tsk->audit_context))
  97. audit_free(tsk);
  98. security_task_free(tsk);
  99. free_uid(tsk->user);
  100. put_group_info(tsk->group_info);
  101. if (!profile_handoff_task(tsk))
  102. free_task(tsk);
  103. }
  104. void __init fork_init(unsigned long mempages)
  105. {
  106. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  107. #ifndef ARCH_MIN_TASKALIGN
  108. #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
  109. #endif
  110. /* create a slab on which task_structs can be allocated */
  111. task_struct_cachep =
  112. kmem_cache_create("task_struct", sizeof(struct task_struct),
  113. ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL, NULL);
  114. #endif
  115. /*
  116. * The default maximum number of threads is set to a safe
  117. * value: the thread structures can take up at most half
  118. * of memory.
  119. */
  120. max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
  121. /*
  122. * we need to allow at least 20 threads to boot a system
  123. */
  124. if(max_threads < 20)
  125. max_threads = 20;
  126. init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
  127. init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
  128. init_task.signal->rlim[RLIMIT_SIGPENDING] =
  129. init_task.signal->rlim[RLIMIT_NPROC];
  130. }
  131. static struct task_struct *dup_task_struct(struct task_struct *orig)
  132. {
  133. struct task_struct *tsk;
  134. struct thread_info *ti;
  135. prepare_to_copy(orig);
  136. tsk = alloc_task_struct();
  137. if (!tsk)
  138. return NULL;
  139. ti = alloc_thread_info(tsk);
  140. if (!ti) {
  141. free_task_struct(tsk);
  142. return NULL;
  143. }
  144. *tsk = *orig;
  145. tsk->thread_info = ti;
  146. setup_thread_stack(tsk, orig);
  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, mpnt);
  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->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. void sighand_free_cb(struct rcu_head *rhp)
  643. {
  644. struct sighand_struct *sp;
  645. sp = container_of(rhp, struct sighand_struct, rcu);
  646. kmem_cache_free(sighand_cachep, sp);
  647. }
  648. static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
  649. {
  650. struct sighand_struct *sig;
  651. if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
  652. atomic_inc(&current->sighand->count);
  653. return 0;
  654. }
  655. sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  656. rcu_assign_pointer(tsk->sighand, sig);
  657. if (!sig)
  658. return -ENOMEM;
  659. spin_lock_init(&sig->siglock);
  660. atomic_set(&sig->count, 1);
  661. memcpy(sig->action, current->sighand->action, sizeof(sig->action));
  662. return 0;
  663. }
  664. static inline int copy_signal(unsigned long clone_flags, struct task_struct * tsk)
  665. {
  666. struct signal_struct *sig;
  667. int ret;
  668. if (clone_flags & CLONE_THREAD) {
  669. atomic_inc(&current->signal->count);
  670. atomic_inc(&current->signal->live);
  671. return 0;
  672. }
  673. sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
  674. tsk->signal = sig;
  675. if (!sig)
  676. return -ENOMEM;
  677. ret = copy_thread_group_keys(tsk);
  678. if (ret < 0) {
  679. kmem_cache_free(signal_cachep, sig);
  680. return ret;
  681. }
  682. atomic_set(&sig->count, 1);
  683. atomic_set(&sig->live, 1);
  684. init_waitqueue_head(&sig->wait_chldexit);
  685. sig->flags = 0;
  686. sig->group_exit_code = 0;
  687. sig->group_exit_task = NULL;
  688. sig->group_stop_count = 0;
  689. sig->curr_target = NULL;
  690. init_sigpending(&sig->shared_pending);
  691. INIT_LIST_HEAD(&sig->posix_timers);
  692. hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_REL);
  693. sig->it_real_incr.tv64 = 0;
  694. sig->real_timer.function = it_real_fn;
  695. sig->real_timer.data = tsk;
  696. sig->it_virt_expires = cputime_zero;
  697. sig->it_virt_incr = cputime_zero;
  698. sig->it_prof_expires = cputime_zero;
  699. sig->it_prof_incr = cputime_zero;
  700. sig->leader = 0; /* session leadership doesn't inherit */
  701. sig->tty_old_pgrp = 0;
  702. sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
  703. sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
  704. sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
  705. sig->sched_time = 0;
  706. INIT_LIST_HEAD(&sig->cpu_timers[0]);
  707. INIT_LIST_HEAD(&sig->cpu_timers[1]);
  708. INIT_LIST_HEAD(&sig->cpu_timers[2]);
  709. task_lock(current->group_leader);
  710. memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
  711. task_unlock(current->group_leader);
  712. if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
  713. /*
  714. * New sole thread in the process gets an expiry time
  715. * of the whole CPU time limit.
  716. */
  717. tsk->it_prof_expires =
  718. secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
  719. }
  720. return 0;
  721. }
  722. static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
  723. {
  724. unsigned long new_flags = p->flags;
  725. new_flags &= ~(PF_SUPERPRIV | PF_NOFREEZE);
  726. new_flags |= PF_FORKNOEXEC;
  727. if (!(clone_flags & CLONE_PTRACE))
  728. p->ptrace = 0;
  729. p->flags = new_flags;
  730. }
  731. asmlinkage long sys_set_tid_address(int __user *tidptr)
  732. {
  733. current->clear_child_tid = tidptr;
  734. return current->pid;
  735. }
  736. /*
  737. * This creates a new process as a copy of the old one,
  738. * but does not actually start it yet.
  739. *
  740. * It copies the registers, and all the appropriate
  741. * parts of the process environment (as per the clone
  742. * flags). The actual kick-off is left to the caller.
  743. */
  744. static task_t *copy_process(unsigned long clone_flags,
  745. unsigned long stack_start,
  746. struct pt_regs *regs,
  747. unsigned long stack_size,
  748. int __user *parent_tidptr,
  749. int __user *child_tidptr,
  750. int pid)
  751. {
  752. int retval;
  753. struct task_struct *p = NULL;
  754. if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
  755. return ERR_PTR(-EINVAL);
  756. /*
  757. * Thread groups must share signals as well, and detached threads
  758. * can only be started up within the thread group.
  759. */
  760. if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
  761. return ERR_PTR(-EINVAL);
  762. /*
  763. * Shared signal handlers imply shared VM. By way of the above,
  764. * thread groups also imply shared VM. Blocking this case allows
  765. * for various simplifications in other code.
  766. */
  767. if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
  768. return ERR_PTR(-EINVAL);
  769. retval = security_task_create(clone_flags);
  770. if (retval)
  771. goto fork_out;
  772. retval = -ENOMEM;
  773. p = dup_task_struct(current);
  774. if (!p)
  775. goto fork_out;
  776. retval = -EAGAIN;
  777. if (atomic_read(&p->user->processes) >=
  778. p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
  779. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
  780. p->user != &root_user)
  781. goto bad_fork_free;
  782. }
  783. atomic_inc(&p->user->__count);
  784. atomic_inc(&p->user->processes);
  785. get_group_info(p->group_info);
  786. /*
  787. * If multiple threads are within copy_process(), then this check
  788. * triggers too late. This doesn't hurt, the check is only there
  789. * to stop root fork bombs.
  790. */
  791. if (nr_threads >= max_threads)
  792. goto bad_fork_cleanup_count;
  793. if (!try_module_get(task_thread_info(p)->exec_domain->module))
  794. goto bad_fork_cleanup_count;
  795. if (p->binfmt && !try_module_get(p->binfmt->module))
  796. goto bad_fork_cleanup_put_domain;
  797. p->did_exec = 0;
  798. copy_flags(clone_flags, p);
  799. p->pid = pid;
  800. retval = -EFAULT;
  801. if (clone_flags & CLONE_PARENT_SETTID)
  802. if (put_user(p->pid, parent_tidptr))
  803. goto bad_fork_cleanup;
  804. p->proc_dentry = NULL;
  805. INIT_LIST_HEAD(&p->children);
  806. INIT_LIST_HEAD(&p->sibling);
  807. p->vfork_done = NULL;
  808. spin_lock_init(&p->alloc_lock);
  809. spin_lock_init(&p->proc_lock);
  810. clear_tsk_thread_flag(p, TIF_SIGPENDING);
  811. init_sigpending(&p->pending);
  812. p->utime = cputime_zero;
  813. p->stime = cputime_zero;
  814. p->sched_time = 0;
  815. p->rchar = 0; /* I/O counter: bytes read */
  816. p->wchar = 0; /* I/O counter: bytes written */
  817. p->syscr = 0; /* I/O counter: read syscalls */
  818. p->syscw = 0; /* I/O counter: write syscalls */
  819. acct_clear_integrals(p);
  820. p->it_virt_expires = cputime_zero;
  821. p->it_prof_expires = cputime_zero;
  822. p->it_sched_expires = 0;
  823. INIT_LIST_HEAD(&p->cpu_timers[0]);
  824. INIT_LIST_HEAD(&p->cpu_timers[1]);
  825. INIT_LIST_HEAD(&p->cpu_timers[2]);
  826. p->lock_depth = -1; /* -1 = no lock */
  827. do_posix_clock_monotonic_gettime(&p->start_time);
  828. p->security = NULL;
  829. p->io_context = NULL;
  830. p->io_wait = NULL;
  831. p->audit_context = NULL;
  832. cpuset_fork(p);
  833. #ifdef CONFIG_NUMA
  834. p->mempolicy = mpol_copy(p->mempolicy);
  835. if (IS_ERR(p->mempolicy)) {
  836. retval = PTR_ERR(p->mempolicy);
  837. p->mempolicy = NULL;
  838. goto bad_fork_cleanup_cpuset;
  839. }
  840. #endif
  841. #ifdef CONFIG_DEBUG_MUTEXES
  842. p->blocked_on = NULL; /* not blocked yet */
  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. attach_pid(p, PIDTYPE_PID, p->pid);
  977. attach_pid(p, PIDTYPE_TGID, p->tgid);
  978. if (thread_group_leader(p)) {
  979. p->signal->tty = current->signal->tty;
  980. p->signal->pgrp = process_group(current);
  981. p->signal->session = current->signal->session;
  982. attach_pid(p, PIDTYPE_PGID, process_group(p));
  983. attach_pid(p, PIDTYPE_SID, p->signal->session);
  984. if (p->pid)
  985. __get_cpu_var(process_counts)++;
  986. }
  987. nr_threads++;
  988. total_forks++;
  989. write_unlock_irq(&tasklist_lock);
  990. proc_fork_connector(p);
  991. return p;
  992. bad_fork_cleanup_namespace:
  993. exit_namespace(p);
  994. bad_fork_cleanup_keys:
  995. exit_keys(p);
  996. bad_fork_cleanup_mm:
  997. if (p->mm)
  998. mmput(p->mm);
  999. bad_fork_cleanup_signal:
  1000. exit_signal(p);
  1001. bad_fork_cleanup_sighand:
  1002. exit_sighand(p);
  1003. bad_fork_cleanup_fs:
  1004. exit_fs(p); /* blocking */
  1005. bad_fork_cleanup_files:
  1006. exit_files(p); /* blocking */
  1007. bad_fork_cleanup_semundo:
  1008. exit_sem(p);
  1009. bad_fork_cleanup_audit:
  1010. audit_free(p);
  1011. bad_fork_cleanup_security:
  1012. security_task_free(p);
  1013. bad_fork_cleanup_policy:
  1014. #ifdef CONFIG_NUMA
  1015. mpol_free(p->mempolicy);
  1016. bad_fork_cleanup_cpuset:
  1017. #endif
  1018. cpuset_exit(p);
  1019. bad_fork_cleanup:
  1020. if (p->binfmt)
  1021. module_put(p->binfmt->module);
  1022. bad_fork_cleanup_put_domain:
  1023. module_put(task_thread_info(p)->exec_domain->module);
  1024. bad_fork_cleanup_count:
  1025. put_group_info(p->group_info);
  1026. atomic_dec(&p->user->processes);
  1027. free_uid(p->user);
  1028. bad_fork_free:
  1029. free_task(p);
  1030. fork_out:
  1031. return ERR_PTR(retval);
  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. #ifndef ARCH_MIN_MMSTRUCT_ALIGN
  1124. #define ARCH_MIN_MMSTRUCT_ALIGN 0
  1125. #endif
  1126. void __init proc_caches_init(void)
  1127. {
  1128. sighand_cachep = kmem_cache_create("sighand_cache",
  1129. sizeof(struct sighand_struct), 0,
  1130. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1131. signal_cachep = kmem_cache_create("signal_cache",
  1132. sizeof(struct signal_struct), 0,
  1133. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1134. files_cachep = kmem_cache_create("files_cache",
  1135. sizeof(struct files_struct), 0,
  1136. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1137. fs_cachep = kmem_cache_create("fs_cache",
  1138. sizeof(struct fs_struct), 0,
  1139. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1140. vm_area_cachep = kmem_cache_create("vm_area_struct",
  1141. sizeof(struct vm_area_struct), 0,
  1142. SLAB_PANIC, NULL, NULL);
  1143. mm_cachep = kmem_cache_create("mm_struct",
  1144. sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
  1145. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1146. }