fork.c 40 KB

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