fork.c 42 KB

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