fork.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  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/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/unistd.h>
  15. #include <linux/module.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/completion.h>
  18. #include <linux/mnt_namespace.h>
  19. #include <linux/personality.h>
  20. #include <linux/mempolicy.h>
  21. #include <linux/sem.h>
  22. #include <linux/file.h>
  23. #include <linux/fdtable.h>
  24. #include <linux/iocontext.h>
  25. #include <linux/key.h>
  26. #include <linux/binfmts.h>
  27. #include <linux/mman.h>
  28. #include <linux/fs.h>
  29. #include <linux/nsproxy.h>
  30. #include <linux/capability.h>
  31. #include <linux/cpu.h>
  32. #include <linux/cgroup.h>
  33. #include <linux/security.h>
  34. #include <linux/hugetlb.h>
  35. #include <linux/swap.h>
  36. #include <linux/syscalls.h>
  37. #include <linux/jiffies.h>
  38. #include <linux/futex.h>
  39. #include <linux/task_io_accounting_ops.h>
  40. #include <linux/rcupdate.h>
  41. #include <linux/ptrace.h>
  42. #include <linux/mount.h>
  43. #include <linux/audit.h>
  44. #include <linux/memcontrol.h>
  45. #include <linux/profile.h>
  46. #include <linux/rmap.h>
  47. #include <linux/acct.h>
  48. #include <linux/tsacct_kern.h>
  49. #include <linux/cn_proc.h>
  50. #include <linux/freezer.h>
  51. #include <linux/delayacct.h>
  52. #include <linux/taskstats_kern.h>
  53. #include <linux/random.h>
  54. #include <linux/tty.h>
  55. #include <linux/proc_fs.h>
  56. #include <linux/blkdev.h>
  57. #include <asm/pgtable.h>
  58. #include <asm/pgalloc.h>
  59. #include <asm/uaccess.h>
  60. #include <asm/mmu_context.h>
  61. #include <asm/cacheflush.h>
  62. #include <asm/tlbflush.h>
  63. /*
  64. * Protected counters by write_lock_irq(&tasklist_lock)
  65. */
  66. unsigned long total_forks; /* Handle normal Linux uptimes. */
  67. int nr_threads; /* The idle threads do not count.. */
  68. int max_threads; /* tunable limit on nr_threads */
  69. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  70. __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  71. int nr_processes(void)
  72. {
  73. int cpu;
  74. int total = 0;
  75. for_each_online_cpu(cpu)
  76. total += per_cpu(process_counts, cpu);
  77. return total;
  78. }
  79. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  80. # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
  81. # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
  82. static struct kmem_cache *task_struct_cachep;
  83. #endif
  84. #ifndef __HAVE_ARCH_THREAD_INFO_ALLOCATOR
  85. static inline struct thread_info *alloc_thread_info(struct task_struct *tsk)
  86. {
  87. #ifdef CONFIG_DEBUG_STACK_USAGE
  88. gfp_t mask = GFP_KERNEL | __GFP_ZERO;
  89. #else
  90. gfp_t mask = GFP_KERNEL;
  91. #endif
  92. return (struct thread_info *)__get_free_pages(mask, THREAD_SIZE_ORDER);
  93. }
  94. static inline void free_thread_info(struct thread_info *ti)
  95. {
  96. free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
  97. }
  98. #endif
  99. /* SLAB cache for signal_struct structures (tsk->signal) */
  100. static struct kmem_cache *signal_cachep;
  101. /* SLAB cache for sighand_struct structures (tsk->sighand) */
  102. struct kmem_cache *sighand_cachep;
  103. /* SLAB cache for files_struct structures (tsk->files) */
  104. struct kmem_cache *files_cachep;
  105. /* SLAB cache for fs_struct structures (tsk->fs) */
  106. struct kmem_cache *fs_cachep;
  107. /* SLAB cache for vm_area_struct structures */
  108. struct kmem_cache *vm_area_cachep;
  109. /* SLAB cache for mm_struct structures (tsk->mm) */
  110. static struct kmem_cache *mm_cachep;
  111. void free_task(struct task_struct *tsk)
  112. {
  113. prop_local_destroy_single(&tsk->dirties);
  114. free_thread_info(tsk->stack);
  115. rt_mutex_debug_task_free(tsk);
  116. free_task_struct(tsk);
  117. }
  118. EXPORT_SYMBOL(free_task);
  119. void __put_task_struct(struct task_struct *tsk)
  120. {
  121. WARN_ON(!tsk->exit_state);
  122. WARN_ON(atomic_read(&tsk->usage));
  123. WARN_ON(tsk == current);
  124. security_task_free(tsk);
  125. free_uid(tsk->user);
  126. put_group_info(tsk->group_info);
  127. delayacct_tsk_free(tsk);
  128. if (!profile_handoff_task(tsk))
  129. free_task(tsk);
  130. }
  131. /*
  132. * macro override instead of weak attribute alias, to workaround
  133. * gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
  134. */
  135. #ifndef arch_task_cache_init
  136. #define arch_task_cache_init()
  137. #endif
  138. void __init fork_init(unsigned long mempages)
  139. {
  140. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  141. #ifndef ARCH_MIN_TASKALIGN
  142. #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
  143. #endif
  144. /* create a slab on which task_structs can be allocated */
  145. task_struct_cachep =
  146. kmem_cache_create("task_struct", sizeof(struct task_struct),
  147. ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL);
  148. #endif
  149. /* do the arch specific task caches init */
  150. arch_task_cache_init();
  151. /*
  152. * The default maximum number of threads is set to a safe
  153. * value: the thread structures can take up at most half
  154. * of memory.
  155. */
  156. max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
  157. /*
  158. * we need to allow at least 20 threads to boot a system
  159. */
  160. if(max_threads < 20)
  161. max_threads = 20;
  162. init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
  163. init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
  164. init_task.signal->rlim[RLIMIT_SIGPENDING] =
  165. init_task.signal->rlim[RLIMIT_NPROC];
  166. }
  167. int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
  168. struct task_struct *src)
  169. {
  170. *dst = *src;
  171. return 0;
  172. }
  173. static struct task_struct *dup_task_struct(struct task_struct *orig)
  174. {
  175. struct task_struct *tsk;
  176. struct thread_info *ti;
  177. int err;
  178. prepare_to_copy(orig);
  179. tsk = alloc_task_struct();
  180. if (!tsk)
  181. return NULL;
  182. ti = alloc_thread_info(tsk);
  183. if (!ti) {
  184. free_task_struct(tsk);
  185. return NULL;
  186. }
  187. err = arch_dup_task_struct(tsk, orig);
  188. if (err)
  189. goto out;
  190. tsk->stack = ti;
  191. err = prop_local_init_single(&tsk->dirties);
  192. if (err)
  193. goto out;
  194. setup_thread_stack(tsk, orig);
  195. #ifdef CONFIG_CC_STACKPROTECTOR
  196. tsk->stack_canary = get_random_int();
  197. #endif
  198. /* One for us, one for whoever does the "release_task()" (usually parent) */
  199. atomic_set(&tsk->usage,2);
  200. atomic_set(&tsk->fs_excl, 0);
  201. #ifdef CONFIG_BLK_DEV_IO_TRACE
  202. tsk->btrace_seq = 0;
  203. #endif
  204. tsk->splice_pipe = NULL;
  205. return tsk;
  206. out:
  207. free_thread_info(ti);
  208. free_task_struct(tsk);
  209. return NULL;
  210. }
  211. #ifdef CONFIG_MMU
  212. static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
  213. {
  214. struct vm_area_struct *mpnt, *tmp, **pprev;
  215. struct rb_node **rb_link, *rb_parent;
  216. int retval;
  217. unsigned long charge;
  218. struct mempolicy *pol;
  219. down_write(&oldmm->mmap_sem);
  220. flush_cache_dup_mm(oldmm);
  221. /*
  222. * Not linked in yet - no deadlock potential:
  223. */
  224. down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
  225. mm->locked_vm = 0;
  226. mm->mmap = NULL;
  227. mm->mmap_cache = NULL;
  228. mm->free_area_cache = oldmm->mmap_base;
  229. mm->cached_hole_size = ~0UL;
  230. mm->map_count = 0;
  231. cpus_clear(mm->cpu_vm_mask);
  232. mm->mm_rb = RB_ROOT;
  233. rb_link = &mm->mm_rb.rb_node;
  234. rb_parent = NULL;
  235. pprev = &mm->mmap;
  236. for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
  237. struct file *file;
  238. if (mpnt->vm_flags & VM_DONTCOPY) {
  239. long pages = vma_pages(mpnt);
  240. mm->total_vm -= pages;
  241. vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
  242. -pages);
  243. continue;
  244. }
  245. charge = 0;
  246. if (mpnt->vm_flags & VM_ACCOUNT) {
  247. unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
  248. if (security_vm_enough_memory(len))
  249. goto fail_nomem;
  250. charge = len;
  251. }
  252. tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  253. if (!tmp)
  254. goto fail_nomem;
  255. *tmp = *mpnt;
  256. pol = mpol_dup(vma_policy(mpnt));
  257. retval = PTR_ERR(pol);
  258. if (IS_ERR(pol))
  259. goto fail_nomem_policy;
  260. vma_set_policy(tmp, pol);
  261. tmp->vm_flags &= ~VM_LOCKED;
  262. tmp->vm_mm = mm;
  263. tmp->vm_next = NULL;
  264. anon_vma_link(tmp);
  265. file = tmp->vm_file;
  266. if (file) {
  267. struct inode *inode = file->f_path.dentry->d_inode;
  268. get_file(file);
  269. if (tmp->vm_flags & VM_DENYWRITE)
  270. atomic_dec(&inode->i_writecount);
  271. /* insert tmp into the share list, just after mpnt */
  272. spin_lock(&file->f_mapping->i_mmap_lock);
  273. tmp->vm_truncate_count = mpnt->vm_truncate_count;
  274. flush_dcache_mmap_lock(file->f_mapping);
  275. vma_prio_tree_add(tmp, mpnt);
  276. flush_dcache_mmap_unlock(file->f_mapping);
  277. spin_unlock(&file->f_mapping->i_mmap_lock);
  278. }
  279. /*
  280. * Clear hugetlb-related page reserves for children. This only
  281. * affects MAP_PRIVATE mappings. Faults generated by the child
  282. * are not guaranteed to succeed, even if read-only
  283. */
  284. if (is_vm_hugetlb_page(tmp))
  285. reset_vma_resv_huge_pages(tmp);
  286. /*
  287. * Link in the new vma and copy the page table entries.
  288. */
  289. *pprev = tmp;
  290. pprev = &tmp->vm_next;
  291. __vma_link_rb(mm, tmp, rb_link, rb_parent);
  292. rb_link = &tmp->vm_rb.rb_right;
  293. rb_parent = &tmp->vm_rb;
  294. mm->map_count++;
  295. retval = copy_page_range(mm, oldmm, mpnt);
  296. if (tmp->vm_ops && tmp->vm_ops->open)
  297. tmp->vm_ops->open(tmp);
  298. if (retval)
  299. goto out;
  300. }
  301. /* a new mm has just been created */
  302. arch_dup_mmap(oldmm, mm);
  303. retval = 0;
  304. out:
  305. up_write(&mm->mmap_sem);
  306. flush_tlb_mm(oldmm);
  307. up_write(&oldmm->mmap_sem);
  308. return retval;
  309. fail_nomem_policy:
  310. kmem_cache_free(vm_area_cachep, tmp);
  311. fail_nomem:
  312. retval = -ENOMEM;
  313. vm_unacct_memory(charge);
  314. goto out;
  315. }
  316. static inline int mm_alloc_pgd(struct mm_struct * mm)
  317. {
  318. mm->pgd = pgd_alloc(mm);
  319. if (unlikely(!mm->pgd))
  320. return -ENOMEM;
  321. return 0;
  322. }
  323. static inline void mm_free_pgd(struct mm_struct * mm)
  324. {
  325. pgd_free(mm, mm->pgd);
  326. }
  327. #else
  328. #define dup_mmap(mm, oldmm) (0)
  329. #define mm_alloc_pgd(mm) (0)
  330. #define mm_free_pgd(mm)
  331. #endif /* CONFIG_MMU */
  332. __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
  333. #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
  334. #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
  335. #include <linux/init_task.h>
  336. static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
  337. {
  338. atomic_set(&mm->mm_users, 1);
  339. atomic_set(&mm->mm_count, 1);
  340. init_rwsem(&mm->mmap_sem);
  341. INIT_LIST_HEAD(&mm->mmlist);
  342. mm->flags = (current->mm) ? current->mm->flags
  343. : MMF_DUMP_FILTER_DEFAULT;
  344. mm->core_waiters = 0;
  345. mm->nr_ptes = 0;
  346. set_mm_counter(mm, file_rss, 0);
  347. set_mm_counter(mm, anon_rss, 0);
  348. spin_lock_init(&mm->page_table_lock);
  349. rwlock_init(&mm->ioctx_list_lock);
  350. mm->ioctx_list = NULL;
  351. mm->free_area_cache = TASK_UNMAPPED_BASE;
  352. mm->cached_hole_size = ~0UL;
  353. mm_init_owner(mm, p);
  354. if (likely(!mm_alloc_pgd(mm))) {
  355. mm->def_flags = 0;
  356. return mm;
  357. }
  358. free_mm(mm);
  359. return NULL;
  360. }
  361. /*
  362. * Allocate and initialize an mm_struct.
  363. */
  364. struct mm_struct * mm_alloc(void)
  365. {
  366. struct mm_struct * mm;
  367. mm = allocate_mm();
  368. if (mm) {
  369. memset(mm, 0, sizeof(*mm));
  370. mm = mm_init(mm, current);
  371. }
  372. return mm;
  373. }
  374. /*
  375. * Called when the last reference to the mm
  376. * is dropped: either by a lazy thread or by
  377. * mmput. Free the page directory and the mm.
  378. */
  379. void __mmdrop(struct mm_struct *mm)
  380. {
  381. BUG_ON(mm == &init_mm);
  382. mm_free_pgd(mm);
  383. destroy_context(mm);
  384. free_mm(mm);
  385. }
  386. EXPORT_SYMBOL_GPL(__mmdrop);
  387. /*
  388. * Decrement the use count and release all resources for an mm.
  389. */
  390. void mmput(struct mm_struct *mm)
  391. {
  392. might_sleep();
  393. if (atomic_dec_and_test(&mm->mm_users)) {
  394. exit_aio(mm);
  395. exit_mmap(mm);
  396. set_mm_exe_file(mm, NULL);
  397. if (!list_empty(&mm->mmlist)) {
  398. spin_lock(&mmlist_lock);
  399. list_del(&mm->mmlist);
  400. spin_unlock(&mmlist_lock);
  401. }
  402. put_swap_token(mm);
  403. mmdrop(mm);
  404. }
  405. }
  406. EXPORT_SYMBOL_GPL(mmput);
  407. /**
  408. * get_task_mm - acquire a reference to the task's mm
  409. *
  410. * Returns %NULL if the task has no mm. Checks PF_BORROWED_MM (meaning
  411. * this kernel workthread has transiently adopted a user mm with use_mm,
  412. * to do its AIO) is not set and if so returns a reference to it, after
  413. * bumping up the use count. User must release the mm via mmput()
  414. * after use. Typically used by /proc and ptrace.
  415. */
  416. struct mm_struct *get_task_mm(struct task_struct *task)
  417. {
  418. struct mm_struct *mm;
  419. task_lock(task);
  420. mm = task->mm;
  421. if (mm) {
  422. if (task->flags & PF_BORROWED_MM)
  423. mm = NULL;
  424. else
  425. atomic_inc(&mm->mm_users);
  426. }
  427. task_unlock(task);
  428. return mm;
  429. }
  430. EXPORT_SYMBOL_GPL(get_task_mm);
  431. /* Please note the differences between mmput and mm_release.
  432. * mmput is called whenever we stop holding onto a mm_struct,
  433. * error success whatever.
  434. *
  435. * mm_release is called after a mm_struct has been removed
  436. * from the current process.
  437. *
  438. * This difference is important for error handling, when we
  439. * only half set up a mm_struct for a new process and need to restore
  440. * the old one. Because we mmput the new mm_struct before
  441. * restoring the old one. . .
  442. * Eric Biederman 10 January 1998
  443. */
  444. void mm_release(struct task_struct *tsk, struct mm_struct *mm)
  445. {
  446. struct completion *vfork_done = tsk->vfork_done;
  447. /* Get rid of any cached register state */
  448. deactivate_mm(tsk, mm);
  449. /* notify parent sleeping on vfork() */
  450. if (vfork_done) {
  451. tsk->vfork_done = NULL;
  452. complete(vfork_done);
  453. }
  454. /*
  455. * If we're exiting normally, clear a user-space tid field if
  456. * requested. We leave this alone when dying by signal, to leave
  457. * the value intact in a core dump, and to save the unnecessary
  458. * trouble otherwise. Userland only wants this done for a sys_exit.
  459. */
  460. if (tsk->clear_child_tid
  461. && !(tsk->flags & PF_SIGNALED)
  462. && atomic_read(&mm->mm_users) > 1) {
  463. u32 __user * tidptr = tsk->clear_child_tid;
  464. tsk->clear_child_tid = NULL;
  465. /*
  466. * We don't check the error code - if userspace has
  467. * not set up a proper pointer then tough luck.
  468. */
  469. put_user(0, tidptr);
  470. sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
  471. }
  472. }
  473. /*
  474. * Allocate a new mm structure and copy contents from the
  475. * mm structure of the passed in task structure.
  476. */
  477. struct mm_struct *dup_mm(struct task_struct *tsk)
  478. {
  479. struct mm_struct *mm, *oldmm = current->mm;
  480. int err;
  481. if (!oldmm)
  482. return NULL;
  483. mm = allocate_mm();
  484. if (!mm)
  485. goto fail_nomem;
  486. memcpy(mm, oldmm, sizeof(*mm));
  487. /* Initializing for Swap token stuff */
  488. mm->token_priority = 0;
  489. mm->last_interval = 0;
  490. if (!mm_init(mm, tsk))
  491. goto fail_nomem;
  492. if (init_new_context(tsk, mm))
  493. goto fail_nocontext;
  494. dup_mm_exe_file(oldmm, mm);
  495. err = dup_mmap(mm, oldmm);
  496. if (err)
  497. goto free_pt;
  498. mm->hiwater_rss = get_mm_rss(mm);
  499. mm->hiwater_vm = mm->total_vm;
  500. return mm;
  501. free_pt:
  502. mmput(mm);
  503. fail_nomem:
  504. return NULL;
  505. fail_nocontext:
  506. /*
  507. * If init_new_context() failed, we cannot use mmput() to free the mm
  508. * because it calls destroy_context()
  509. */
  510. mm_free_pgd(mm);
  511. free_mm(mm);
  512. return NULL;
  513. }
  514. static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
  515. {
  516. struct mm_struct * mm, *oldmm;
  517. int retval;
  518. tsk->min_flt = tsk->maj_flt = 0;
  519. tsk->nvcsw = tsk->nivcsw = 0;
  520. tsk->mm = NULL;
  521. tsk->active_mm = NULL;
  522. /*
  523. * Are we cloning a kernel thread?
  524. *
  525. * We need to steal a active VM for that..
  526. */
  527. oldmm = current->mm;
  528. if (!oldmm)
  529. return 0;
  530. if (clone_flags & CLONE_VM) {
  531. atomic_inc(&oldmm->mm_users);
  532. mm = oldmm;
  533. goto good_mm;
  534. }
  535. retval = -ENOMEM;
  536. mm = dup_mm(tsk);
  537. if (!mm)
  538. goto fail_nomem;
  539. good_mm:
  540. /* Initializing for Swap token stuff */
  541. mm->token_priority = 0;
  542. mm->last_interval = 0;
  543. tsk->mm = mm;
  544. tsk->active_mm = mm;
  545. return 0;
  546. fail_nomem:
  547. return retval;
  548. }
  549. static struct fs_struct *__copy_fs_struct(struct fs_struct *old)
  550. {
  551. struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
  552. /* We don't need to lock fs - think why ;-) */
  553. if (fs) {
  554. atomic_set(&fs->count, 1);
  555. rwlock_init(&fs->lock);
  556. fs->umask = old->umask;
  557. read_lock(&old->lock);
  558. fs->root = old->root;
  559. path_get(&old->root);
  560. fs->pwd = old->pwd;
  561. path_get(&old->pwd);
  562. if (old->altroot.dentry) {
  563. fs->altroot = old->altroot;
  564. path_get(&old->altroot);
  565. } else {
  566. fs->altroot.mnt = NULL;
  567. fs->altroot.dentry = NULL;
  568. }
  569. read_unlock(&old->lock);
  570. }
  571. return fs;
  572. }
  573. struct fs_struct *copy_fs_struct(struct fs_struct *old)
  574. {
  575. return __copy_fs_struct(old);
  576. }
  577. EXPORT_SYMBOL_GPL(copy_fs_struct);
  578. static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
  579. {
  580. if (clone_flags & CLONE_FS) {
  581. atomic_inc(&current->fs->count);
  582. return 0;
  583. }
  584. tsk->fs = __copy_fs_struct(current->fs);
  585. if (!tsk->fs)
  586. return -ENOMEM;
  587. return 0;
  588. }
  589. static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
  590. {
  591. struct files_struct *oldf, *newf;
  592. int error = 0;
  593. /*
  594. * A background process may not have any files ...
  595. */
  596. oldf = current->files;
  597. if (!oldf)
  598. goto out;
  599. if (clone_flags & CLONE_FILES) {
  600. atomic_inc(&oldf->count);
  601. goto out;
  602. }
  603. newf = dup_fd(oldf, &error);
  604. if (!newf)
  605. goto out;
  606. tsk->files = newf;
  607. error = 0;
  608. out:
  609. return error;
  610. }
  611. static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
  612. {
  613. #ifdef CONFIG_BLOCK
  614. struct io_context *ioc = current->io_context;
  615. if (!ioc)
  616. return 0;
  617. /*
  618. * Share io context with parent, if CLONE_IO is set
  619. */
  620. if (clone_flags & CLONE_IO) {
  621. tsk->io_context = ioc_task_link(ioc);
  622. if (unlikely(!tsk->io_context))
  623. return -ENOMEM;
  624. } else if (ioprio_valid(ioc->ioprio)) {
  625. tsk->io_context = alloc_io_context(GFP_KERNEL, -1);
  626. if (unlikely(!tsk->io_context))
  627. return -ENOMEM;
  628. tsk->io_context->ioprio = ioc->ioprio;
  629. }
  630. #endif
  631. return 0;
  632. }
  633. static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
  634. {
  635. struct sighand_struct *sig;
  636. if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
  637. atomic_inc(&current->sighand->count);
  638. return 0;
  639. }
  640. sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  641. rcu_assign_pointer(tsk->sighand, sig);
  642. if (!sig)
  643. return -ENOMEM;
  644. atomic_set(&sig->count, 1);
  645. memcpy(sig->action, current->sighand->action, sizeof(sig->action));
  646. return 0;
  647. }
  648. void __cleanup_sighand(struct sighand_struct *sighand)
  649. {
  650. if (atomic_dec_and_test(&sighand->count))
  651. kmem_cache_free(sighand_cachep, sighand);
  652. }
  653. static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
  654. {
  655. struct signal_struct *sig;
  656. int ret;
  657. if (clone_flags & CLONE_THREAD) {
  658. atomic_inc(&current->signal->count);
  659. atomic_inc(&current->signal->live);
  660. return 0;
  661. }
  662. sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
  663. tsk->signal = sig;
  664. if (!sig)
  665. return -ENOMEM;
  666. ret = copy_thread_group_keys(tsk);
  667. if (ret < 0) {
  668. kmem_cache_free(signal_cachep, sig);
  669. return ret;
  670. }
  671. atomic_set(&sig->count, 1);
  672. atomic_set(&sig->live, 1);
  673. init_waitqueue_head(&sig->wait_chldexit);
  674. sig->flags = 0;
  675. sig->group_exit_code = 0;
  676. sig->group_exit_task = NULL;
  677. sig->group_stop_count = 0;
  678. sig->curr_target = tsk;
  679. init_sigpending(&sig->shared_pending);
  680. INIT_LIST_HEAD(&sig->posix_timers);
  681. hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  682. sig->it_real_incr.tv64 = 0;
  683. sig->real_timer.function = it_real_fn;
  684. sig->it_virt_expires = cputime_zero;
  685. sig->it_virt_incr = cputime_zero;
  686. sig->it_prof_expires = cputime_zero;
  687. sig->it_prof_incr = cputime_zero;
  688. sig->leader = 0; /* session leadership doesn't inherit */
  689. sig->tty_old_pgrp = NULL;
  690. sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
  691. sig->gtime = cputime_zero;
  692. sig->cgtime = cputime_zero;
  693. sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
  694. sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
  695. sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0;
  696. sig->sum_sched_runtime = 0;
  697. INIT_LIST_HEAD(&sig->cpu_timers[0]);
  698. INIT_LIST_HEAD(&sig->cpu_timers[1]);
  699. INIT_LIST_HEAD(&sig->cpu_timers[2]);
  700. taskstats_tgid_init(sig);
  701. task_lock(current->group_leader);
  702. memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
  703. task_unlock(current->group_leader);
  704. if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
  705. /*
  706. * New sole thread in the process gets an expiry time
  707. * of the whole CPU time limit.
  708. */
  709. tsk->it_prof_expires =
  710. secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
  711. }
  712. acct_init_pacct(&sig->pacct);
  713. tty_audit_fork(sig);
  714. return 0;
  715. }
  716. void __cleanup_signal(struct signal_struct *sig)
  717. {
  718. exit_thread_group_keys(sig);
  719. kmem_cache_free(signal_cachep, sig);
  720. }
  721. static void cleanup_signal(struct task_struct *tsk)
  722. {
  723. struct signal_struct *sig = tsk->signal;
  724. atomic_dec(&sig->live);
  725. if (atomic_dec_and_test(&sig->count))
  726. __cleanup_signal(sig);
  727. }
  728. static void copy_flags(unsigned long clone_flags, struct task_struct *p)
  729. {
  730. unsigned long new_flags = p->flags;
  731. new_flags &= ~PF_SUPERPRIV;
  732. new_flags |= PF_FORKNOEXEC;
  733. if (!(clone_flags & CLONE_PTRACE))
  734. p->ptrace = 0;
  735. p->flags = new_flags;
  736. clear_freeze_flag(p);
  737. }
  738. asmlinkage long sys_set_tid_address(int __user *tidptr)
  739. {
  740. current->clear_child_tid = tidptr;
  741. return task_pid_vnr(current);
  742. }
  743. static void rt_mutex_init_task(struct task_struct *p)
  744. {
  745. spin_lock_init(&p->pi_lock);
  746. #ifdef CONFIG_RT_MUTEXES
  747. plist_head_init(&p->pi_waiters, &p->pi_lock);
  748. p->pi_blocked_on = NULL;
  749. #endif
  750. }
  751. #ifdef CONFIG_MM_OWNER
  752. void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
  753. {
  754. mm->owner = p;
  755. }
  756. #endif /* CONFIG_MM_OWNER */
  757. /*
  758. * This creates a new process as a copy of the old one,
  759. * but does not actually start it yet.
  760. *
  761. * It copies the registers, and all the appropriate
  762. * parts of the process environment (as per the clone
  763. * flags). The actual kick-off is left to the caller.
  764. */
  765. static struct task_struct *copy_process(unsigned long clone_flags,
  766. unsigned long stack_start,
  767. struct pt_regs *regs,
  768. unsigned long stack_size,
  769. int __user *child_tidptr,
  770. struct pid *pid)
  771. {
  772. int retval;
  773. struct task_struct *p;
  774. int cgroup_callbacks_done = 0;
  775. if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
  776. return ERR_PTR(-EINVAL);
  777. /*
  778. * Thread groups must share signals as well, and detached threads
  779. * can only be started up within the thread group.
  780. */
  781. if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
  782. return ERR_PTR(-EINVAL);
  783. /*
  784. * Shared signal handlers imply shared VM. By way of the above,
  785. * thread groups also imply shared VM. Blocking this case allows
  786. * for various simplifications in other code.
  787. */
  788. if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
  789. return ERR_PTR(-EINVAL);
  790. retval = security_task_create(clone_flags);
  791. if (retval)
  792. goto fork_out;
  793. retval = -ENOMEM;
  794. p = dup_task_struct(current);
  795. if (!p)
  796. goto fork_out;
  797. rt_mutex_init_task(p);
  798. #ifdef CONFIG_PROVE_LOCKING
  799. DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
  800. DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
  801. #endif
  802. retval = -EAGAIN;
  803. if (atomic_read(&p->user->processes) >=
  804. p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
  805. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
  806. p->user != current->nsproxy->user_ns->root_user)
  807. goto bad_fork_free;
  808. }
  809. atomic_inc(&p->user->__count);
  810. atomic_inc(&p->user->processes);
  811. get_group_info(p->group_info);
  812. /*
  813. * If multiple threads are within copy_process(), then this check
  814. * triggers too late. This doesn't hurt, the check is only there
  815. * to stop root fork bombs.
  816. */
  817. if (nr_threads >= max_threads)
  818. goto bad_fork_cleanup_count;
  819. if (!try_module_get(task_thread_info(p)->exec_domain->module))
  820. goto bad_fork_cleanup_count;
  821. if (p->binfmt && !try_module_get(p->binfmt->module))
  822. goto bad_fork_cleanup_put_domain;
  823. p->did_exec = 0;
  824. delayacct_tsk_init(p); /* Must remain after dup_task_struct() */
  825. copy_flags(clone_flags, p);
  826. INIT_LIST_HEAD(&p->children);
  827. INIT_LIST_HEAD(&p->sibling);
  828. #ifdef CONFIG_PREEMPT_RCU
  829. p->rcu_read_lock_nesting = 0;
  830. p->rcu_flipctr_idx = 0;
  831. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  832. p->vfork_done = NULL;
  833. spin_lock_init(&p->alloc_lock);
  834. clear_tsk_thread_flag(p, TIF_SIGPENDING);
  835. init_sigpending(&p->pending);
  836. p->utime = cputime_zero;
  837. p->stime = cputime_zero;
  838. p->gtime = cputime_zero;
  839. p->utimescaled = cputime_zero;
  840. p->stimescaled = cputime_zero;
  841. p->prev_utime = cputime_zero;
  842. p->prev_stime = cputime_zero;
  843. #ifdef CONFIG_DETECT_SOFTLOCKUP
  844. p->last_switch_count = 0;
  845. p->last_switch_timestamp = 0;
  846. #endif
  847. #ifdef CONFIG_TASK_XACCT
  848. p->rchar = 0; /* I/O counter: bytes read */
  849. p->wchar = 0; /* I/O counter: bytes written */
  850. p->syscr = 0; /* I/O counter: read syscalls */
  851. p->syscw = 0; /* I/O counter: write syscalls */
  852. #endif
  853. task_io_accounting_init(p);
  854. acct_clear_integrals(p);
  855. p->it_virt_expires = cputime_zero;
  856. p->it_prof_expires = cputime_zero;
  857. p->it_sched_expires = 0;
  858. INIT_LIST_HEAD(&p->cpu_timers[0]);
  859. INIT_LIST_HEAD(&p->cpu_timers[1]);
  860. INIT_LIST_HEAD(&p->cpu_timers[2]);
  861. p->lock_depth = -1; /* -1 = no lock */
  862. do_posix_clock_monotonic_gettime(&p->start_time);
  863. p->real_start_time = p->start_time;
  864. monotonic_to_bootbased(&p->real_start_time);
  865. #ifdef CONFIG_SECURITY
  866. p->security = NULL;
  867. #endif
  868. p->cap_bset = current->cap_bset;
  869. p->io_context = NULL;
  870. p->audit_context = NULL;
  871. cgroup_fork(p);
  872. #ifdef CONFIG_NUMA
  873. p->mempolicy = mpol_dup(p->mempolicy);
  874. if (IS_ERR(p->mempolicy)) {
  875. retval = PTR_ERR(p->mempolicy);
  876. p->mempolicy = NULL;
  877. goto bad_fork_cleanup_cgroup;
  878. }
  879. mpol_fix_fork_child_flag(p);
  880. #endif
  881. #ifdef CONFIG_TRACE_IRQFLAGS
  882. p->irq_events = 0;
  883. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  884. p->hardirqs_enabled = 1;
  885. #else
  886. p->hardirqs_enabled = 0;
  887. #endif
  888. p->hardirq_enable_ip = 0;
  889. p->hardirq_enable_event = 0;
  890. p->hardirq_disable_ip = _THIS_IP_;
  891. p->hardirq_disable_event = 0;
  892. p->softirqs_enabled = 1;
  893. p->softirq_enable_ip = _THIS_IP_;
  894. p->softirq_enable_event = 0;
  895. p->softirq_disable_ip = 0;
  896. p->softirq_disable_event = 0;
  897. p->hardirq_context = 0;
  898. p->softirq_context = 0;
  899. #endif
  900. #ifdef CONFIG_LOCKDEP
  901. p->lockdep_depth = 0; /* no locks held yet */
  902. p->curr_chain_key = 0;
  903. p->lockdep_recursion = 0;
  904. #endif
  905. #ifdef CONFIG_DEBUG_MUTEXES
  906. p->blocked_on = NULL; /* not blocked yet */
  907. #endif
  908. /* Perform scheduler related setup. Assign this task to a CPU. */
  909. sched_fork(p, clone_flags);
  910. if ((retval = security_task_alloc(p)))
  911. goto bad_fork_cleanup_policy;
  912. if ((retval = audit_alloc(p)))
  913. goto bad_fork_cleanup_security;
  914. /* copy all the process information */
  915. if ((retval = copy_semundo(clone_flags, p)))
  916. goto bad_fork_cleanup_audit;
  917. if ((retval = copy_files(clone_flags, p)))
  918. goto bad_fork_cleanup_semundo;
  919. if ((retval = copy_fs(clone_flags, p)))
  920. goto bad_fork_cleanup_files;
  921. if ((retval = copy_sighand(clone_flags, p)))
  922. goto bad_fork_cleanup_fs;
  923. if ((retval = copy_signal(clone_flags, p)))
  924. goto bad_fork_cleanup_sighand;
  925. if ((retval = copy_mm(clone_flags, p)))
  926. goto bad_fork_cleanup_signal;
  927. if ((retval = copy_keys(clone_flags, p)))
  928. goto bad_fork_cleanup_mm;
  929. if ((retval = copy_namespaces(clone_flags, p)))
  930. goto bad_fork_cleanup_keys;
  931. if ((retval = copy_io(clone_flags, p)))
  932. goto bad_fork_cleanup_namespaces;
  933. retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
  934. if (retval)
  935. goto bad_fork_cleanup_io;
  936. if (pid != &init_struct_pid) {
  937. retval = -ENOMEM;
  938. pid = alloc_pid(task_active_pid_ns(p));
  939. if (!pid)
  940. goto bad_fork_cleanup_io;
  941. if (clone_flags & CLONE_NEWPID) {
  942. retval = pid_ns_prepare_proc(task_active_pid_ns(p));
  943. if (retval < 0)
  944. goto bad_fork_free_pid;
  945. }
  946. }
  947. p->pid = pid_nr(pid);
  948. p->tgid = p->pid;
  949. if (clone_flags & CLONE_THREAD)
  950. p->tgid = current->tgid;
  951. p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
  952. /*
  953. * Clear TID on mm_release()?
  954. */
  955. p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
  956. #ifdef CONFIG_FUTEX
  957. p->robust_list = NULL;
  958. #ifdef CONFIG_COMPAT
  959. p->compat_robust_list = NULL;
  960. #endif
  961. INIT_LIST_HEAD(&p->pi_state_list);
  962. p->pi_state_cache = NULL;
  963. #endif
  964. /*
  965. * sigaltstack should be cleared when sharing the same VM
  966. */
  967. if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
  968. p->sas_ss_sp = p->sas_ss_size = 0;
  969. /*
  970. * Syscall tracing should be turned off in the child regardless
  971. * of CLONE_PTRACE.
  972. */
  973. clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
  974. #ifdef TIF_SYSCALL_EMU
  975. clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
  976. #endif
  977. clear_all_latency_tracing(p);
  978. /* Our parent execution domain becomes current domain
  979. These must match for thread signalling to apply */
  980. p->parent_exec_id = p->self_exec_id;
  981. /* ok, now we should be set up.. */
  982. p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
  983. p->pdeath_signal = 0;
  984. p->exit_state = 0;
  985. /*
  986. * Ok, make it visible to the rest of the system.
  987. * We dont wake it up yet.
  988. */
  989. p->group_leader = p;
  990. INIT_LIST_HEAD(&p->thread_group);
  991. INIT_LIST_HEAD(&p->ptrace_entry);
  992. INIT_LIST_HEAD(&p->ptraced);
  993. /* Now that the task is set up, run cgroup callbacks if
  994. * necessary. We need to run them before the task is visible
  995. * on the tasklist. */
  996. cgroup_fork_callbacks(p);
  997. cgroup_callbacks_done = 1;
  998. /* Need tasklist lock for parent etc handling! */
  999. write_lock_irq(&tasklist_lock);
  1000. /*
  1001. * The task hasn't been attached yet, so its cpus_allowed mask will
  1002. * not be changed, nor will its assigned CPU.
  1003. *
  1004. * The cpus_allowed mask of the parent may have changed after it was
  1005. * copied first time - so re-copy it here, then check the child's CPU
  1006. * to ensure it is on a valid CPU (and if not, just force it back to
  1007. * parent's CPU). This avoids alot of nasty races.
  1008. */
  1009. p->cpus_allowed = current->cpus_allowed;
  1010. p->rt.nr_cpus_allowed = current->rt.nr_cpus_allowed;
  1011. if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
  1012. !cpu_online(task_cpu(p))))
  1013. set_task_cpu(p, smp_processor_id());
  1014. /* CLONE_PARENT re-uses the old parent */
  1015. if (clone_flags & (CLONE_PARENT|CLONE_THREAD))
  1016. p->real_parent = current->real_parent;
  1017. else
  1018. p->real_parent = current;
  1019. p->parent = p->real_parent;
  1020. spin_lock(&current->sighand->siglock);
  1021. /*
  1022. * Process group and session signals need to be delivered to just the
  1023. * parent before the fork or both the parent and the child after the
  1024. * fork. Restart if a signal comes in before we add the new process to
  1025. * it's process group.
  1026. * A fatal signal pending means that current will exit, so the new
  1027. * thread can't slip out of an OOM kill (or normal SIGKILL).
  1028. */
  1029. recalc_sigpending();
  1030. if (signal_pending(current)) {
  1031. spin_unlock(&current->sighand->siglock);
  1032. write_unlock_irq(&tasklist_lock);
  1033. retval = -ERESTARTNOINTR;
  1034. goto bad_fork_free_pid;
  1035. }
  1036. if (clone_flags & CLONE_THREAD) {
  1037. p->group_leader = current->group_leader;
  1038. list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
  1039. if (!cputime_eq(current->signal->it_virt_expires,
  1040. cputime_zero) ||
  1041. !cputime_eq(current->signal->it_prof_expires,
  1042. cputime_zero) ||
  1043. current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
  1044. !list_empty(&current->signal->cpu_timers[0]) ||
  1045. !list_empty(&current->signal->cpu_timers[1]) ||
  1046. !list_empty(&current->signal->cpu_timers[2])) {
  1047. /*
  1048. * Have child wake up on its first tick to check
  1049. * for process CPU timers.
  1050. */
  1051. p->it_prof_expires = jiffies_to_cputime(1);
  1052. }
  1053. }
  1054. if (likely(p->pid)) {
  1055. list_add_tail(&p->sibling, &p->real_parent->children);
  1056. if (unlikely(p->ptrace & PT_PTRACED))
  1057. __ptrace_link(p, current->parent);
  1058. if (thread_group_leader(p)) {
  1059. if (clone_flags & CLONE_NEWPID)
  1060. p->nsproxy->pid_ns->child_reaper = p;
  1061. p->signal->leader_pid = pid;
  1062. p->signal->tty = current->signal->tty;
  1063. set_task_pgrp(p, task_pgrp_nr(current));
  1064. set_task_session(p, task_session_nr(current));
  1065. attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
  1066. attach_pid(p, PIDTYPE_SID, task_session(current));
  1067. list_add_tail_rcu(&p->tasks, &init_task.tasks);
  1068. __get_cpu_var(process_counts)++;
  1069. }
  1070. attach_pid(p, PIDTYPE_PID, pid);
  1071. nr_threads++;
  1072. }
  1073. total_forks++;
  1074. spin_unlock(&current->sighand->siglock);
  1075. write_unlock_irq(&tasklist_lock);
  1076. proc_fork_connector(p);
  1077. cgroup_post_fork(p);
  1078. return p;
  1079. bad_fork_free_pid:
  1080. if (pid != &init_struct_pid)
  1081. free_pid(pid);
  1082. bad_fork_cleanup_io:
  1083. put_io_context(p->io_context);
  1084. bad_fork_cleanup_namespaces:
  1085. exit_task_namespaces(p);
  1086. bad_fork_cleanup_keys:
  1087. exit_keys(p);
  1088. bad_fork_cleanup_mm:
  1089. if (p->mm)
  1090. mmput(p->mm);
  1091. bad_fork_cleanup_signal:
  1092. cleanup_signal(p);
  1093. bad_fork_cleanup_sighand:
  1094. __cleanup_sighand(p->sighand);
  1095. bad_fork_cleanup_fs:
  1096. exit_fs(p); /* blocking */
  1097. bad_fork_cleanup_files:
  1098. exit_files(p); /* blocking */
  1099. bad_fork_cleanup_semundo:
  1100. exit_sem(p);
  1101. bad_fork_cleanup_audit:
  1102. audit_free(p);
  1103. bad_fork_cleanup_security:
  1104. security_task_free(p);
  1105. bad_fork_cleanup_policy:
  1106. #ifdef CONFIG_NUMA
  1107. mpol_put(p->mempolicy);
  1108. bad_fork_cleanup_cgroup:
  1109. #endif
  1110. cgroup_exit(p, cgroup_callbacks_done);
  1111. delayacct_tsk_free(p);
  1112. if (p->binfmt)
  1113. module_put(p->binfmt->module);
  1114. bad_fork_cleanup_put_domain:
  1115. module_put(task_thread_info(p)->exec_domain->module);
  1116. bad_fork_cleanup_count:
  1117. put_group_info(p->group_info);
  1118. atomic_dec(&p->user->processes);
  1119. free_uid(p->user);
  1120. bad_fork_free:
  1121. free_task(p);
  1122. fork_out:
  1123. return ERR_PTR(retval);
  1124. }
  1125. noinline struct pt_regs * __cpuinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
  1126. {
  1127. memset(regs, 0, sizeof(struct pt_regs));
  1128. return regs;
  1129. }
  1130. struct task_struct * __cpuinit fork_idle(int cpu)
  1131. {
  1132. struct task_struct *task;
  1133. struct pt_regs regs;
  1134. task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL,
  1135. &init_struct_pid);
  1136. if (!IS_ERR(task))
  1137. init_idle(task, cpu);
  1138. return task;
  1139. }
  1140. static int fork_traceflag(unsigned clone_flags)
  1141. {
  1142. if (clone_flags & CLONE_UNTRACED)
  1143. return 0;
  1144. else if (clone_flags & CLONE_VFORK) {
  1145. if (current->ptrace & PT_TRACE_VFORK)
  1146. return PTRACE_EVENT_VFORK;
  1147. } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
  1148. if (current->ptrace & PT_TRACE_CLONE)
  1149. return PTRACE_EVENT_CLONE;
  1150. } else if (current->ptrace & PT_TRACE_FORK)
  1151. return PTRACE_EVENT_FORK;
  1152. return 0;
  1153. }
  1154. /*
  1155. * Ok, this is the main fork-routine.
  1156. *
  1157. * It copies the process, and if successful kick-starts
  1158. * it and waits for it to finish using the VM if required.
  1159. */
  1160. long do_fork(unsigned long clone_flags,
  1161. unsigned long stack_start,
  1162. struct pt_regs *regs,
  1163. unsigned long stack_size,
  1164. int __user *parent_tidptr,
  1165. int __user *child_tidptr)
  1166. {
  1167. struct task_struct *p;
  1168. int trace = 0;
  1169. long nr;
  1170. /*
  1171. * We hope to recycle these flags after 2.6.26
  1172. */
  1173. if (unlikely(clone_flags & CLONE_STOPPED)) {
  1174. static int __read_mostly count = 100;
  1175. if (count > 0 && printk_ratelimit()) {
  1176. char comm[TASK_COMM_LEN];
  1177. count--;
  1178. printk(KERN_INFO "fork(): process `%s' used deprecated "
  1179. "clone flags 0x%lx\n",
  1180. get_task_comm(comm, current),
  1181. clone_flags & CLONE_STOPPED);
  1182. }
  1183. }
  1184. if (unlikely(current->ptrace)) {
  1185. trace = fork_traceflag (clone_flags);
  1186. if (trace)
  1187. clone_flags |= CLONE_PTRACE;
  1188. }
  1189. p = copy_process(clone_flags, stack_start, regs, stack_size,
  1190. child_tidptr, NULL);
  1191. /*
  1192. * Do this prior waking up the new thread - the thread pointer
  1193. * might get invalid after that point, if the thread exits quickly.
  1194. */
  1195. if (!IS_ERR(p)) {
  1196. struct completion vfork;
  1197. nr = task_pid_vnr(p);
  1198. if (clone_flags & CLONE_PARENT_SETTID)
  1199. put_user(nr, parent_tidptr);
  1200. if (clone_flags & CLONE_VFORK) {
  1201. p->vfork_done = &vfork;
  1202. init_completion(&vfork);
  1203. }
  1204. if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
  1205. /*
  1206. * We'll start up with an immediate SIGSTOP.
  1207. */
  1208. sigaddset(&p->pending.signal, SIGSTOP);
  1209. set_tsk_thread_flag(p, TIF_SIGPENDING);
  1210. }
  1211. if (!(clone_flags & CLONE_STOPPED))
  1212. wake_up_new_task(p, clone_flags);
  1213. else
  1214. __set_task_state(p, TASK_STOPPED);
  1215. if (unlikely (trace)) {
  1216. current->ptrace_message = nr;
  1217. ptrace_notify ((trace << 8) | SIGTRAP);
  1218. }
  1219. if (clone_flags & CLONE_VFORK) {
  1220. freezer_do_not_count();
  1221. wait_for_completion(&vfork);
  1222. freezer_count();
  1223. if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) {
  1224. current->ptrace_message = nr;
  1225. ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
  1226. }
  1227. }
  1228. } else {
  1229. nr = PTR_ERR(p);
  1230. }
  1231. return nr;
  1232. }
  1233. #ifndef ARCH_MIN_MMSTRUCT_ALIGN
  1234. #define ARCH_MIN_MMSTRUCT_ALIGN 0
  1235. #endif
  1236. static void sighand_ctor(struct kmem_cache *cachep, void *data)
  1237. {
  1238. struct sighand_struct *sighand = data;
  1239. spin_lock_init(&sighand->siglock);
  1240. init_waitqueue_head(&sighand->signalfd_wqh);
  1241. }
  1242. void __init proc_caches_init(void)
  1243. {
  1244. sighand_cachep = kmem_cache_create("sighand_cache",
  1245. sizeof(struct sighand_struct), 0,
  1246. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU,
  1247. sighand_ctor);
  1248. signal_cachep = kmem_cache_create("signal_cache",
  1249. sizeof(struct signal_struct), 0,
  1250. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1251. files_cachep = kmem_cache_create("files_cache",
  1252. sizeof(struct files_struct), 0,
  1253. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1254. fs_cachep = kmem_cache_create("fs_cache",
  1255. sizeof(struct fs_struct), 0,
  1256. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1257. vm_area_cachep = kmem_cache_create("vm_area_struct",
  1258. sizeof(struct vm_area_struct), 0,
  1259. SLAB_PANIC, NULL);
  1260. mm_cachep = kmem_cache_create("mm_struct",
  1261. sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
  1262. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1263. }
  1264. /*
  1265. * Check constraints on flags passed to the unshare system call and
  1266. * force unsharing of additional process context as appropriate.
  1267. */
  1268. static void check_unshare_flags(unsigned long *flags_ptr)
  1269. {
  1270. /*
  1271. * If unsharing a thread from a thread group, must also
  1272. * unshare vm.
  1273. */
  1274. if (*flags_ptr & CLONE_THREAD)
  1275. *flags_ptr |= CLONE_VM;
  1276. /*
  1277. * If unsharing vm, must also unshare signal handlers.
  1278. */
  1279. if (*flags_ptr & CLONE_VM)
  1280. *flags_ptr |= CLONE_SIGHAND;
  1281. /*
  1282. * If unsharing signal handlers and the task was created
  1283. * using CLONE_THREAD, then must unshare the thread
  1284. */
  1285. if ((*flags_ptr & CLONE_SIGHAND) &&
  1286. (atomic_read(&current->signal->count) > 1))
  1287. *flags_ptr |= CLONE_THREAD;
  1288. /*
  1289. * If unsharing namespace, must also unshare filesystem information.
  1290. */
  1291. if (*flags_ptr & CLONE_NEWNS)
  1292. *flags_ptr |= CLONE_FS;
  1293. }
  1294. /*
  1295. * Unsharing of tasks created with CLONE_THREAD is not supported yet
  1296. */
  1297. static int unshare_thread(unsigned long unshare_flags)
  1298. {
  1299. if (unshare_flags & CLONE_THREAD)
  1300. return -EINVAL;
  1301. return 0;
  1302. }
  1303. /*
  1304. * Unshare the filesystem structure if it is being shared
  1305. */
  1306. static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
  1307. {
  1308. struct fs_struct *fs = current->fs;
  1309. if ((unshare_flags & CLONE_FS) &&
  1310. (fs && atomic_read(&fs->count) > 1)) {
  1311. *new_fsp = __copy_fs_struct(current->fs);
  1312. if (!*new_fsp)
  1313. return -ENOMEM;
  1314. }
  1315. return 0;
  1316. }
  1317. /*
  1318. * Unsharing of sighand is not supported yet
  1319. */
  1320. static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
  1321. {
  1322. struct sighand_struct *sigh = current->sighand;
  1323. if ((unshare_flags & CLONE_SIGHAND) && atomic_read(&sigh->count) > 1)
  1324. return -EINVAL;
  1325. else
  1326. return 0;
  1327. }
  1328. /*
  1329. * Unshare vm if it is being shared
  1330. */
  1331. static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp)
  1332. {
  1333. struct mm_struct *mm = current->mm;
  1334. if ((unshare_flags & CLONE_VM) &&
  1335. (mm && atomic_read(&mm->mm_users) > 1)) {
  1336. return -EINVAL;
  1337. }
  1338. return 0;
  1339. }
  1340. /*
  1341. * Unshare file descriptor table if it is being shared
  1342. */
  1343. static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
  1344. {
  1345. struct files_struct *fd = current->files;
  1346. int error = 0;
  1347. if ((unshare_flags & CLONE_FILES) &&
  1348. (fd && atomic_read(&fd->count) > 1)) {
  1349. *new_fdp = dup_fd(fd, &error);
  1350. if (!*new_fdp)
  1351. return error;
  1352. }
  1353. return 0;
  1354. }
  1355. /*
  1356. * unshare allows a process to 'unshare' part of the process
  1357. * context which was originally shared using clone. copy_*
  1358. * functions used by do_fork() cannot be used here directly
  1359. * because they modify an inactive task_struct that is being
  1360. * constructed. Here we are modifying the current, active,
  1361. * task_struct.
  1362. */
  1363. asmlinkage long sys_unshare(unsigned long unshare_flags)
  1364. {
  1365. int err = 0;
  1366. struct fs_struct *fs, *new_fs = NULL;
  1367. struct sighand_struct *new_sigh = NULL;
  1368. struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
  1369. struct files_struct *fd, *new_fd = NULL;
  1370. struct nsproxy *new_nsproxy = NULL;
  1371. int do_sysvsem = 0;
  1372. check_unshare_flags(&unshare_flags);
  1373. /* Return -EINVAL for all unsupported flags */
  1374. err = -EINVAL;
  1375. if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
  1376. CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
  1377. CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWUSER|
  1378. CLONE_NEWNET))
  1379. goto bad_unshare_out;
  1380. /*
  1381. * CLONE_NEWIPC must also detach from the undolist: after switching
  1382. * to a new ipc namespace, the semaphore arrays from the old
  1383. * namespace are unreachable.
  1384. */
  1385. if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
  1386. do_sysvsem = 1;
  1387. if ((err = unshare_thread(unshare_flags)))
  1388. goto bad_unshare_out;
  1389. if ((err = unshare_fs(unshare_flags, &new_fs)))
  1390. goto bad_unshare_cleanup_thread;
  1391. if ((err = unshare_sighand(unshare_flags, &new_sigh)))
  1392. goto bad_unshare_cleanup_fs;
  1393. if ((err = unshare_vm(unshare_flags, &new_mm)))
  1394. goto bad_unshare_cleanup_sigh;
  1395. if ((err = unshare_fd(unshare_flags, &new_fd)))
  1396. goto bad_unshare_cleanup_vm;
  1397. if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
  1398. new_fs)))
  1399. goto bad_unshare_cleanup_fd;
  1400. if (new_fs || new_mm || new_fd || do_sysvsem || new_nsproxy) {
  1401. if (do_sysvsem) {
  1402. /*
  1403. * CLONE_SYSVSEM is equivalent to sys_exit().
  1404. */
  1405. exit_sem(current);
  1406. }
  1407. if (new_nsproxy) {
  1408. switch_task_namespaces(current, new_nsproxy);
  1409. new_nsproxy = NULL;
  1410. }
  1411. task_lock(current);
  1412. if (new_fs) {
  1413. fs = current->fs;
  1414. current->fs = new_fs;
  1415. new_fs = fs;
  1416. }
  1417. if (new_mm) {
  1418. mm = current->mm;
  1419. active_mm = current->active_mm;
  1420. current->mm = new_mm;
  1421. current->active_mm = new_mm;
  1422. activate_mm(active_mm, new_mm);
  1423. new_mm = mm;
  1424. }
  1425. if (new_fd) {
  1426. fd = current->files;
  1427. current->files = new_fd;
  1428. new_fd = fd;
  1429. }
  1430. task_unlock(current);
  1431. }
  1432. if (new_nsproxy)
  1433. put_nsproxy(new_nsproxy);
  1434. bad_unshare_cleanup_fd:
  1435. if (new_fd)
  1436. put_files_struct(new_fd);
  1437. bad_unshare_cleanup_vm:
  1438. if (new_mm)
  1439. mmput(new_mm);
  1440. bad_unshare_cleanup_sigh:
  1441. if (new_sigh)
  1442. if (atomic_dec_and_test(&new_sigh->count))
  1443. kmem_cache_free(sighand_cachep, new_sigh);
  1444. bad_unshare_cleanup_fs:
  1445. if (new_fs)
  1446. put_fs_struct(new_fs);
  1447. bad_unshare_cleanup_thread:
  1448. bad_unshare_out:
  1449. return err;
  1450. }
  1451. /*
  1452. * Helper to unshare the files of the current task.
  1453. * We don't want to expose copy_files internals to
  1454. * the exec layer of the kernel.
  1455. */
  1456. int unshare_files(struct files_struct **displaced)
  1457. {
  1458. struct task_struct *task = current;
  1459. struct files_struct *copy = NULL;
  1460. int error;
  1461. error = unshare_fd(CLONE_FILES, &copy);
  1462. if (error || !copy) {
  1463. *displaced = NULL;
  1464. return error;
  1465. }
  1466. *displaced = task->files;
  1467. task_lock(task);
  1468. task->files = copy;
  1469. task_unlock(task);
  1470. return 0;
  1471. }