fork.c 43 KB

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