fork.c 43 KB

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