fork.c 32 KB

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