fork.c 32 KB

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