fork.c 42 KB

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