fork.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605
  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/sched/autogroup.h>
  14. #include <linux/sched/mm.h>
  15. #include <linux/sched/coredump.h>
  16. #include <linux/sched/user.h>
  17. #include <linux/sched/numa_balancing.h>
  18. #include <linux/sched/stat.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/sched/task_stack.h>
  21. #include <linux/sched/cputime.h>
  22. #include <linux/rtmutex.h>
  23. #include <linux/init.h>
  24. #include <linux/unistd.h>
  25. #include <linux/module.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/completion.h>
  28. #include <linux/personality.h>
  29. #include <linux/mempolicy.h>
  30. #include <linux/sem.h>
  31. #include <linux/file.h>
  32. #include <linux/fdtable.h>
  33. #include <linux/iocontext.h>
  34. #include <linux/key.h>
  35. #include <linux/binfmts.h>
  36. #include <linux/mman.h>
  37. #include <linux/mmu_notifier.h>
  38. #include <linux/hmm.h>
  39. #include <linux/fs.h>
  40. #include <linux/mm.h>
  41. #include <linux/vmacache.h>
  42. #include <linux/nsproxy.h>
  43. #include <linux/capability.h>
  44. #include <linux/cpu.h>
  45. #include <linux/cgroup.h>
  46. #include <linux/security.h>
  47. #include <linux/hugetlb.h>
  48. #include <linux/seccomp.h>
  49. #include <linux/swap.h>
  50. #include <linux/syscalls.h>
  51. #include <linux/jiffies.h>
  52. #include <linux/futex.h>
  53. #include <linux/compat.h>
  54. #include <linux/kthread.h>
  55. #include <linux/task_io_accounting_ops.h>
  56. #include <linux/rcupdate.h>
  57. #include <linux/ptrace.h>
  58. #include <linux/mount.h>
  59. #include <linux/audit.h>
  60. #include <linux/memcontrol.h>
  61. #include <linux/ftrace.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/profile.h>
  64. #include <linux/rmap.h>
  65. #include <linux/ksm.h>
  66. #include <linux/acct.h>
  67. #include <linux/userfaultfd_k.h>
  68. #include <linux/tsacct_kern.h>
  69. #include <linux/cn_proc.h>
  70. #include <linux/freezer.h>
  71. #include <linux/delayacct.h>
  72. #include <linux/taskstats_kern.h>
  73. #include <linux/random.h>
  74. #include <linux/tty.h>
  75. #include <linux/blkdev.h>
  76. #include <linux/fs_struct.h>
  77. #include <linux/magic.h>
  78. #include <linux/sched/mm.h>
  79. #include <linux/perf_event.h>
  80. #include <linux/posix-timers.h>
  81. #include <linux/user-return-notifier.h>
  82. #include <linux/oom.h>
  83. #include <linux/khugepaged.h>
  84. #include <linux/signalfd.h>
  85. #include <linux/uprobes.h>
  86. #include <linux/aio.h>
  87. #include <linux/compiler.h>
  88. #include <linux/sysctl.h>
  89. #include <linux/kcov.h>
  90. #include <linux/livepatch.h>
  91. #include <linux/thread_info.h>
  92. #include <asm/pgtable.h>
  93. #include <asm/pgalloc.h>
  94. #include <linux/uaccess.h>
  95. #include <asm/mmu_context.h>
  96. #include <asm/cacheflush.h>
  97. #include <asm/tlbflush.h>
  98. #include <trace/events/sched.h>
  99. #define CREATE_TRACE_POINTS
  100. #include <trace/events/task.h>
  101. /*
  102. * Minimum number of threads to boot the kernel
  103. */
  104. #define MIN_THREADS 20
  105. /*
  106. * Maximum number of threads
  107. */
  108. #define MAX_THREADS FUTEX_TID_MASK
  109. /*
  110. * Protected counters by write_lock_irq(&tasklist_lock)
  111. */
  112. unsigned long total_forks; /* Handle normal Linux uptimes. */
  113. int nr_threads; /* The idle threads do not count.. */
  114. int max_threads; /* tunable limit on nr_threads */
  115. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  116. __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  117. #ifdef CONFIG_PROVE_RCU
  118. int lockdep_tasklist_lock_is_held(void)
  119. {
  120. return lockdep_is_held(&tasklist_lock);
  121. }
  122. EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
  123. #endif /* #ifdef CONFIG_PROVE_RCU */
  124. int nr_processes(void)
  125. {
  126. int cpu;
  127. int total = 0;
  128. for_each_possible_cpu(cpu)
  129. total += per_cpu(process_counts, cpu);
  130. return total;
  131. }
  132. void __weak arch_release_task_struct(struct task_struct *tsk)
  133. {
  134. }
  135. #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
  136. static struct kmem_cache *task_struct_cachep;
  137. static inline struct task_struct *alloc_task_struct_node(int node)
  138. {
  139. return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node);
  140. }
  141. static inline void free_task_struct(struct task_struct *tsk)
  142. {
  143. kmem_cache_free(task_struct_cachep, tsk);
  144. }
  145. #endif
  146. void __weak arch_release_thread_stack(unsigned long *stack)
  147. {
  148. }
  149. #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR
  150. /*
  151. * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
  152. * kmemcache based allocator.
  153. */
  154. # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)
  155. #ifdef CONFIG_VMAP_STACK
  156. /*
  157. * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB
  158. * flush. Try to minimize the number of calls by caching stacks.
  159. */
  160. #define NR_CACHED_STACKS 2
  161. static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]);
  162. static int free_vm_stack_cache(unsigned int cpu)
  163. {
  164. struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu);
  165. int i;
  166. for (i = 0; i < NR_CACHED_STACKS; i++) {
  167. struct vm_struct *vm_stack = cached_vm_stacks[i];
  168. if (!vm_stack)
  169. continue;
  170. vfree(vm_stack->addr);
  171. cached_vm_stacks[i] = NULL;
  172. }
  173. return 0;
  174. }
  175. #endif
  176. static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
  177. {
  178. #ifdef CONFIG_VMAP_STACK
  179. void *stack;
  180. int i;
  181. for (i = 0; i < NR_CACHED_STACKS; i++) {
  182. struct vm_struct *s;
  183. s = this_cpu_xchg(cached_stacks[i], NULL);
  184. if (!s)
  185. continue;
  186. /* Clear stale pointers from reused stack. */
  187. memset(s->addr, 0, THREAD_SIZE);
  188. tsk->stack_vm_area = s;
  189. return s->addr;
  190. }
  191. stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN,
  192. VMALLOC_START, VMALLOC_END,
  193. THREADINFO_GFP,
  194. PAGE_KERNEL,
  195. 0, node, __builtin_return_address(0));
  196. /*
  197. * We can't call find_vm_area() in interrupt context, and
  198. * free_thread_stack() can be called in interrupt context,
  199. * so cache the vm_struct.
  200. */
  201. if (stack)
  202. tsk->stack_vm_area = find_vm_area(stack);
  203. return stack;
  204. #else
  205. struct page *page = alloc_pages_node(node, THREADINFO_GFP,
  206. THREAD_SIZE_ORDER);
  207. return page ? page_address(page) : NULL;
  208. #endif
  209. }
  210. static inline void free_thread_stack(struct task_struct *tsk)
  211. {
  212. #ifdef CONFIG_VMAP_STACK
  213. if (task_stack_vm_area(tsk)) {
  214. int i;
  215. for (i = 0; i < NR_CACHED_STACKS; i++) {
  216. if (this_cpu_cmpxchg(cached_stacks[i],
  217. NULL, tsk->stack_vm_area) != NULL)
  218. continue;
  219. return;
  220. }
  221. vfree_atomic(tsk->stack);
  222. return;
  223. }
  224. #endif
  225. __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER);
  226. }
  227. # else
  228. static struct kmem_cache *thread_stack_cache;
  229. static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
  230. int node)
  231. {
  232. return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
  233. }
  234. static void free_thread_stack(struct task_struct *tsk)
  235. {
  236. kmem_cache_free(thread_stack_cache, tsk->stack);
  237. }
  238. void thread_stack_cache_init(void)
  239. {
  240. thread_stack_cache = kmem_cache_create_usercopy("thread_stack",
  241. THREAD_SIZE, THREAD_SIZE, 0, 0,
  242. THREAD_SIZE, NULL);
  243. BUG_ON(thread_stack_cache == NULL);
  244. }
  245. # endif
  246. #endif
  247. /* SLAB cache for signal_struct structures (tsk->signal) */
  248. static struct kmem_cache *signal_cachep;
  249. /* SLAB cache for sighand_struct structures (tsk->sighand) */
  250. struct kmem_cache *sighand_cachep;
  251. /* SLAB cache for files_struct structures (tsk->files) */
  252. struct kmem_cache *files_cachep;
  253. /* SLAB cache for fs_struct structures (tsk->fs) */
  254. struct kmem_cache *fs_cachep;
  255. /* SLAB cache for vm_area_struct structures */
  256. static struct kmem_cache *vm_area_cachep;
  257. /* SLAB cache for mm_struct structures (tsk->mm) */
  258. static struct kmem_cache *mm_cachep;
  259. struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)
  260. {
  261. struct vm_area_struct *vma;
  262. vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  263. if (vma)
  264. vma_init(vma, mm);
  265. return vma;
  266. }
  267. struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)
  268. {
  269. struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  270. if (new) {
  271. *new = *orig;
  272. INIT_LIST_HEAD(&new->anon_vma_chain);
  273. }
  274. return new;
  275. }
  276. void vm_area_free(struct vm_area_struct *vma)
  277. {
  278. kmem_cache_free(vm_area_cachep, vma);
  279. }
  280. static void account_kernel_stack(struct task_struct *tsk, int account)
  281. {
  282. void *stack = task_stack_page(tsk);
  283. struct vm_struct *vm = task_stack_vm_area(tsk);
  284. BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0);
  285. if (vm) {
  286. int i;
  287. BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
  288. for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
  289. mod_zone_page_state(page_zone(vm->pages[i]),
  290. NR_KERNEL_STACK_KB,
  291. PAGE_SIZE / 1024 * account);
  292. }
  293. /* All stack pages belong to the same memcg. */
  294. mod_memcg_page_state(vm->pages[0], MEMCG_KERNEL_STACK_KB,
  295. account * (THREAD_SIZE / 1024));
  296. } else {
  297. /*
  298. * All stack pages are in the same zone and belong to the
  299. * same memcg.
  300. */
  301. struct page *first_page = virt_to_page(stack);
  302. mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB,
  303. THREAD_SIZE / 1024 * account);
  304. mod_memcg_page_state(first_page, MEMCG_KERNEL_STACK_KB,
  305. account * (THREAD_SIZE / 1024));
  306. }
  307. }
  308. static void release_task_stack(struct task_struct *tsk)
  309. {
  310. if (WARN_ON(tsk->state != TASK_DEAD))
  311. return; /* Better to leak the stack than to free prematurely */
  312. account_kernel_stack(tsk, -1);
  313. arch_release_thread_stack(tsk->stack);
  314. free_thread_stack(tsk);
  315. tsk->stack = NULL;
  316. #ifdef CONFIG_VMAP_STACK
  317. tsk->stack_vm_area = NULL;
  318. #endif
  319. }
  320. #ifdef CONFIG_THREAD_INFO_IN_TASK
  321. void put_task_stack(struct task_struct *tsk)
  322. {
  323. if (atomic_dec_and_test(&tsk->stack_refcount))
  324. release_task_stack(tsk);
  325. }
  326. #endif
  327. void free_task(struct task_struct *tsk)
  328. {
  329. #ifndef CONFIG_THREAD_INFO_IN_TASK
  330. /*
  331. * The task is finally done with both the stack and thread_info,
  332. * so free both.
  333. */
  334. release_task_stack(tsk);
  335. #else
  336. /*
  337. * If the task had a separate stack allocation, it should be gone
  338. * by now.
  339. */
  340. WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0);
  341. #endif
  342. rt_mutex_debug_task_free(tsk);
  343. ftrace_graph_exit_task(tsk);
  344. put_seccomp_filter(tsk);
  345. arch_release_task_struct(tsk);
  346. if (tsk->flags & PF_KTHREAD)
  347. free_kthread_struct(tsk);
  348. free_task_struct(tsk);
  349. }
  350. EXPORT_SYMBOL(free_task);
  351. #ifdef CONFIG_MMU
  352. static __latent_entropy int dup_mmap(struct mm_struct *mm,
  353. struct mm_struct *oldmm)
  354. {
  355. struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
  356. struct rb_node **rb_link, *rb_parent;
  357. int retval;
  358. unsigned long charge;
  359. LIST_HEAD(uf);
  360. uprobe_start_dup_mmap();
  361. if (down_write_killable(&oldmm->mmap_sem)) {
  362. retval = -EINTR;
  363. goto fail_uprobe_end;
  364. }
  365. flush_cache_dup_mm(oldmm);
  366. uprobe_dup_mmap(oldmm, mm);
  367. /*
  368. * Not linked in yet - no deadlock potential:
  369. */
  370. down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
  371. /* No ordering required: file already has been exposed. */
  372. RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
  373. mm->total_vm = oldmm->total_vm;
  374. mm->data_vm = oldmm->data_vm;
  375. mm->exec_vm = oldmm->exec_vm;
  376. mm->stack_vm = oldmm->stack_vm;
  377. rb_link = &mm->mm_rb.rb_node;
  378. rb_parent = NULL;
  379. pprev = &mm->mmap;
  380. retval = ksm_fork(mm, oldmm);
  381. if (retval)
  382. goto out;
  383. retval = khugepaged_fork(mm, oldmm);
  384. if (retval)
  385. goto out;
  386. prev = NULL;
  387. for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
  388. struct file *file;
  389. if (mpnt->vm_flags & VM_DONTCOPY) {
  390. vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt));
  391. continue;
  392. }
  393. charge = 0;
  394. /*
  395. * Don't duplicate many vmas if we've been oom-killed (for
  396. * example)
  397. */
  398. if (fatal_signal_pending(current)) {
  399. retval = -EINTR;
  400. goto out;
  401. }
  402. if (mpnt->vm_flags & VM_ACCOUNT) {
  403. unsigned long len = vma_pages(mpnt);
  404. if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
  405. goto fail_nomem;
  406. charge = len;
  407. }
  408. tmp = vm_area_dup(mpnt);
  409. if (!tmp)
  410. goto fail_nomem;
  411. retval = vma_dup_policy(mpnt, tmp);
  412. if (retval)
  413. goto fail_nomem_policy;
  414. tmp->vm_mm = mm;
  415. retval = dup_userfaultfd(tmp, &uf);
  416. if (retval)
  417. goto fail_nomem_anon_vma_fork;
  418. if (tmp->vm_flags & VM_WIPEONFORK) {
  419. /* VM_WIPEONFORK gets a clean slate in the child. */
  420. tmp->anon_vma = NULL;
  421. if (anon_vma_prepare(tmp))
  422. goto fail_nomem_anon_vma_fork;
  423. } else if (anon_vma_fork(tmp, mpnt))
  424. goto fail_nomem_anon_vma_fork;
  425. tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT);
  426. tmp->vm_next = tmp->vm_prev = NULL;
  427. file = tmp->vm_file;
  428. if (file) {
  429. struct inode *inode = file_inode(file);
  430. struct address_space *mapping = file->f_mapping;
  431. get_file(file);
  432. if (tmp->vm_flags & VM_DENYWRITE)
  433. atomic_dec(&inode->i_writecount);
  434. i_mmap_lock_write(mapping);
  435. if (tmp->vm_flags & VM_SHARED)
  436. atomic_inc(&mapping->i_mmap_writable);
  437. flush_dcache_mmap_lock(mapping);
  438. /* insert tmp into the share list, just after mpnt */
  439. vma_interval_tree_insert_after(tmp, mpnt,
  440. &mapping->i_mmap);
  441. flush_dcache_mmap_unlock(mapping);
  442. i_mmap_unlock_write(mapping);
  443. }
  444. /*
  445. * Clear hugetlb-related page reserves for children. This only
  446. * affects MAP_PRIVATE mappings. Faults generated by the child
  447. * are not guaranteed to succeed, even if read-only
  448. */
  449. if (is_vm_hugetlb_page(tmp))
  450. reset_vma_resv_huge_pages(tmp);
  451. /*
  452. * Link in the new vma and copy the page table entries.
  453. */
  454. *pprev = tmp;
  455. pprev = &tmp->vm_next;
  456. tmp->vm_prev = prev;
  457. prev = tmp;
  458. __vma_link_rb(mm, tmp, rb_link, rb_parent);
  459. rb_link = &tmp->vm_rb.rb_right;
  460. rb_parent = &tmp->vm_rb;
  461. mm->map_count++;
  462. if (!(tmp->vm_flags & VM_WIPEONFORK))
  463. retval = copy_page_range(mm, oldmm, mpnt);
  464. if (tmp->vm_ops && tmp->vm_ops->open)
  465. tmp->vm_ops->open(tmp);
  466. if (retval)
  467. goto out;
  468. }
  469. /* a new mm has just been created */
  470. retval = arch_dup_mmap(oldmm, mm);
  471. out:
  472. up_write(&mm->mmap_sem);
  473. flush_tlb_mm(oldmm);
  474. up_write(&oldmm->mmap_sem);
  475. dup_userfaultfd_complete(&uf);
  476. fail_uprobe_end:
  477. uprobe_end_dup_mmap();
  478. return retval;
  479. fail_nomem_anon_vma_fork:
  480. mpol_put(vma_policy(tmp));
  481. fail_nomem_policy:
  482. vm_area_free(tmp);
  483. fail_nomem:
  484. retval = -ENOMEM;
  485. vm_unacct_memory(charge);
  486. goto out;
  487. }
  488. static inline int mm_alloc_pgd(struct mm_struct *mm)
  489. {
  490. mm->pgd = pgd_alloc(mm);
  491. if (unlikely(!mm->pgd))
  492. return -ENOMEM;
  493. return 0;
  494. }
  495. static inline void mm_free_pgd(struct mm_struct *mm)
  496. {
  497. pgd_free(mm, mm->pgd);
  498. }
  499. #else
  500. static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
  501. {
  502. down_write(&oldmm->mmap_sem);
  503. RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
  504. up_write(&oldmm->mmap_sem);
  505. return 0;
  506. }
  507. #define mm_alloc_pgd(mm) (0)
  508. #define mm_free_pgd(mm)
  509. #endif /* CONFIG_MMU */
  510. static void check_mm(struct mm_struct *mm)
  511. {
  512. int i;
  513. for (i = 0; i < NR_MM_COUNTERS; i++) {
  514. long x = atomic_long_read(&mm->rss_stat.count[i]);
  515. if (unlikely(x))
  516. printk(KERN_ALERT "BUG: Bad rss-counter state "
  517. "mm:%p idx:%d val:%ld\n", mm, i, x);
  518. }
  519. if (mm_pgtables_bytes(mm))
  520. pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n",
  521. mm_pgtables_bytes(mm));
  522. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
  523. VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
  524. #endif
  525. }
  526. #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
  527. #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
  528. /*
  529. * Called when the last reference to the mm
  530. * is dropped: either by a lazy thread or by
  531. * mmput. Free the page directory and the mm.
  532. */
  533. void __mmdrop(struct mm_struct *mm)
  534. {
  535. BUG_ON(mm == &init_mm);
  536. WARN_ON_ONCE(mm == current->mm);
  537. WARN_ON_ONCE(mm == current->active_mm);
  538. mm_free_pgd(mm);
  539. destroy_context(mm);
  540. hmm_mm_destroy(mm);
  541. mmu_notifier_mm_destroy(mm);
  542. check_mm(mm);
  543. put_user_ns(mm->user_ns);
  544. free_mm(mm);
  545. }
  546. EXPORT_SYMBOL_GPL(__mmdrop);
  547. static void mmdrop_async_fn(struct work_struct *work)
  548. {
  549. struct mm_struct *mm;
  550. mm = container_of(work, struct mm_struct, async_put_work);
  551. __mmdrop(mm);
  552. }
  553. static void mmdrop_async(struct mm_struct *mm)
  554. {
  555. if (unlikely(atomic_dec_and_test(&mm->mm_count))) {
  556. INIT_WORK(&mm->async_put_work, mmdrop_async_fn);
  557. schedule_work(&mm->async_put_work);
  558. }
  559. }
  560. static inline void free_signal_struct(struct signal_struct *sig)
  561. {
  562. taskstats_tgid_free(sig);
  563. sched_autogroup_exit(sig);
  564. /*
  565. * __mmdrop is not safe to call from softirq context on x86 due to
  566. * pgd_dtor so postpone it to the async context
  567. */
  568. if (sig->oom_mm)
  569. mmdrop_async(sig->oom_mm);
  570. kmem_cache_free(signal_cachep, sig);
  571. }
  572. static inline void put_signal_struct(struct signal_struct *sig)
  573. {
  574. if (atomic_dec_and_test(&sig->sigcnt))
  575. free_signal_struct(sig);
  576. }
  577. void __put_task_struct(struct task_struct *tsk)
  578. {
  579. WARN_ON(!tsk->exit_state);
  580. WARN_ON(atomic_read(&tsk->usage));
  581. WARN_ON(tsk == current);
  582. cgroup_free(tsk);
  583. task_numa_free(tsk);
  584. security_task_free(tsk);
  585. exit_creds(tsk);
  586. delayacct_tsk_free(tsk);
  587. put_signal_struct(tsk->signal);
  588. if (!profile_handoff_task(tsk))
  589. free_task(tsk);
  590. }
  591. EXPORT_SYMBOL_GPL(__put_task_struct);
  592. void __init __weak arch_task_cache_init(void) { }
  593. /*
  594. * set_max_threads
  595. */
  596. static void set_max_threads(unsigned int max_threads_suggested)
  597. {
  598. u64 threads;
  599. /*
  600. * The number of threads shall be limited such that the thread
  601. * structures may only consume a small part of the available memory.
  602. */
  603. if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64)
  604. threads = MAX_THREADS;
  605. else
  606. threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
  607. (u64) THREAD_SIZE * 8UL);
  608. if (threads > max_threads_suggested)
  609. threads = max_threads_suggested;
  610. max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS);
  611. }
  612. #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
  613. /* Initialized by the architecture: */
  614. int arch_task_struct_size __read_mostly;
  615. #endif
  616. static void task_struct_whitelist(unsigned long *offset, unsigned long *size)
  617. {
  618. /* Fetch thread_struct whitelist for the architecture. */
  619. arch_thread_struct_whitelist(offset, size);
  620. /*
  621. * Handle zero-sized whitelist or empty thread_struct, otherwise
  622. * adjust offset to position of thread_struct in task_struct.
  623. */
  624. if (unlikely(*size == 0))
  625. *offset = 0;
  626. else
  627. *offset += offsetof(struct task_struct, thread);
  628. }
  629. void __init fork_init(void)
  630. {
  631. int i;
  632. #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
  633. #ifndef ARCH_MIN_TASKALIGN
  634. #define ARCH_MIN_TASKALIGN 0
  635. #endif
  636. int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN);
  637. unsigned long useroffset, usersize;
  638. /* create a slab on which task_structs can be allocated */
  639. task_struct_whitelist(&useroffset, &usersize);
  640. task_struct_cachep = kmem_cache_create_usercopy("task_struct",
  641. arch_task_struct_size, align,
  642. SLAB_PANIC|SLAB_ACCOUNT,
  643. useroffset, usersize, NULL);
  644. #endif
  645. /* do the arch specific task caches init */
  646. arch_task_cache_init();
  647. set_max_threads(MAX_THREADS);
  648. init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
  649. init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
  650. init_task.signal->rlim[RLIMIT_SIGPENDING] =
  651. init_task.signal->rlim[RLIMIT_NPROC];
  652. for (i = 0; i < UCOUNT_COUNTS; i++) {
  653. init_user_ns.ucount_max[i] = max_threads/2;
  654. }
  655. #ifdef CONFIG_VMAP_STACK
  656. cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache",
  657. NULL, free_vm_stack_cache);
  658. #endif
  659. lockdep_init_task(&init_task);
  660. }
  661. int __weak arch_dup_task_struct(struct task_struct *dst,
  662. struct task_struct *src)
  663. {
  664. *dst = *src;
  665. return 0;
  666. }
  667. void set_task_stack_end_magic(struct task_struct *tsk)
  668. {
  669. unsigned long *stackend;
  670. stackend = end_of_stack(tsk);
  671. *stackend = STACK_END_MAGIC; /* for overflow detection */
  672. }
  673. static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
  674. {
  675. struct task_struct *tsk;
  676. unsigned long *stack;
  677. struct vm_struct *stack_vm_area;
  678. int err;
  679. if (node == NUMA_NO_NODE)
  680. node = tsk_fork_get_node(orig);
  681. tsk = alloc_task_struct_node(node);
  682. if (!tsk)
  683. return NULL;
  684. stack = alloc_thread_stack_node(tsk, node);
  685. if (!stack)
  686. goto free_tsk;
  687. stack_vm_area = task_stack_vm_area(tsk);
  688. err = arch_dup_task_struct(tsk, orig);
  689. /*
  690. * arch_dup_task_struct() clobbers the stack-related fields. Make
  691. * sure they're properly initialized before using any stack-related
  692. * functions again.
  693. */
  694. tsk->stack = stack;
  695. #ifdef CONFIG_VMAP_STACK
  696. tsk->stack_vm_area = stack_vm_area;
  697. #endif
  698. #ifdef CONFIG_THREAD_INFO_IN_TASK
  699. atomic_set(&tsk->stack_refcount, 1);
  700. #endif
  701. if (err)
  702. goto free_stack;
  703. #ifdef CONFIG_SECCOMP
  704. /*
  705. * We must handle setting up seccomp filters once we're under
  706. * the sighand lock in case orig has changed between now and
  707. * then. Until then, filter must be NULL to avoid messing up
  708. * the usage counts on the error path calling free_task.
  709. */
  710. tsk->seccomp.filter = NULL;
  711. #endif
  712. setup_thread_stack(tsk, orig);
  713. clear_user_return_notifier(tsk);
  714. clear_tsk_need_resched(tsk);
  715. set_task_stack_end_magic(tsk);
  716. #ifdef CONFIG_STACKPROTECTOR
  717. tsk->stack_canary = get_random_canary();
  718. #endif
  719. /*
  720. * One for us, one for whoever does the "release_task()" (usually
  721. * parent)
  722. */
  723. atomic_set(&tsk->usage, 2);
  724. #ifdef CONFIG_BLK_DEV_IO_TRACE
  725. tsk->btrace_seq = 0;
  726. #endif
  727. tsk->splice_pipe = NULL;
  728. tsk->task_frag.page = NULL;
  729. tsk->wake_q.next = NULL;
  730. account_kernel_stack(tsk, 1);
  731. kcov_task_init(tsk);
  732. #ifdef CONFIG_FAULT_INJECTION
  733. tsk->fail_nth = 0;
  734. #endif
  735. #ifdef CONFIG_BLK_CGROUP
  736. tsk->throttle_queue = NULL;
  737. tsk->use_memdelay = 0;
  738. #endif
  739. #ifdef CONFIG_MEMCG
  740. tsk->active_memcg = NULL;
  741. #endif
  742. return tsk;
  743. free_stack:
  744. free_thread_stack(tsk);
  745. free_tsk:
  746. free_task_struct(tsk);
  747. return NULL;
  748. }
  749. __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
  750. static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
  751. static int __init coredump_filter_setup(char *s)
  752. {
  753. default_dump_filter =
  754. (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
  755. MMF_DUMP_FILTER_MASK;
  756. return 1;
  757. }
  758. __setup("coredump_filter=", coredump_filter_setup);
  759. #include <linux/init_task.h>
  760. static void mm_init_aio(struct mm_struct *mm)
  761. {
  762. #ifdef CONFIG_AIO
  763. spin_lock_init(&mm->ioctx_lock);
  764. mm->ioctx_table = NULL;
  765. #endif
  766. }
  767. static void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
  768. {
  769. #ifdef CONFIG_MEMCG
  770. mm->owner = p;
  771. #endif
  772. }
  773. static void mm_init_uprobes_state(struct mm_struct *mm)
  774. {
  775. #ifdef CONFIG_UPROBES
  776. mm->uprobes_state.xol_area = NULL;
  777. #endif
  778. }
  779. static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
  780. struct user_namespace *user_ns)
  781. {
  782. mm->mmap = NULL;
  783. mm->mm_rb = RB_ROOT;
  784. mm->vmacache_seqnum = 0;
  785. atomic_set(&mm->mm_users, 1);
  786. atomic_set(&mm->mm_count, 1);
  787. init_rwsem(&mm->mmap_sem);
  788. INIT_LIST_HEAD(&mm->mmlist);
  789. mm->core_state = NULL;
  790. mm_pgtables_bytes_init(mm);
  791. mm->map_count = 0;
  792. mm->locked_vm = 0;
  793. mm->pinned_vm = 0;
  794. memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
  795. spin_lock_init(&mm->page_table_lock);
  796. spin_lock_init(&mm->arg_lock);
  797. mm_init_cpumask(mm);
  798. mm_init_aio(mm);
  799. mm_init_owner(mm, p);
  800. RCU_INIT_POINTER(mm->exe_file, NULL);
  801. mmu_notifier_mm_init(mm);
  802. hmm_mm_init(mm);
  803. init_tlb_flush_pending(mm);
  804. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
  805. mm->pmd_huge_pte = NULL;
  806. #endif
  807. mm_init_uprobes_state(mm);
  808. if (current->mm) {
  809. mm->flags = current->mm->flags & MMF_INIT_MASK;
  810. mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
  811. } else {
  812. mm->flags = default_dump_filter;
  813. mm->def_flags = 0;
  814. }
  815. if (mm_alloc_pgd(mm))
  816. goto fail_nopgd;
  817. if (init_new_context(p, mm))
  818. goto fail_nocontext;
  819. mm->user_ns = get_user_ns(user_ns);
  820. return mm;
  821. fail_nocontext:
  822. mm_free_pgd(mm);
  823. fail_nopgd:
  824. free_mm(mm);
  825. return NULL;
  826. }
  827. /*
  828. * Allocate and initialize an mm_struct.
  829. */
  830. struct mm_struct *mm_alloc(void)
  831. {
  832. struct mm_struct *mm;
  833. mm = allocate_mm();
  834. if (!mm)
  835. return NULL;
  836. memset(mm, 0, sizeof(*mm));
  837. return mm_init(mm, current, current_user_ns());
  838. }
  839. static inline void __mmput(struct mm_struct *mm)
  840. {
  841. VM_BUG_ON(atomic_read(&mm->mm_users));
  842. uprobe_clear_state(mm);
  843. exit_aio(mm);
  844. ksm_exit(mm);
  845. khugepaged_exit(mm); /* must run before exit_mmap */
  846. exit_mmap(mm);
  847. mm_put_huge_zero_page(mm);
  848. set_mm_exe_file(mm, NULL);
  849. if (!list_empty(&mm->mmlist)) {
  850. spin_lock(&mmlist_lock);
  851. list_del(&mm->mmlist);
  852. spin_unlock(&mmlist_lock);
  853. }
  854. if (mm->binfmt)
  855. module_put(mm->binfmt->module);
  856. mmdrop(mm);
  857. }
  858. /*
  859. * Decrement the use count and release all resources for an mm.
  860. */
  861. void mmput(struct mm_struct *mm)
  862. {
  863. might_sleep();
  864. if (atomic_dec_and_test(&mm->mm_users))
  865. __mmput(mm);
  866. }
  867. EXPORT_SYMBOL_GPL(mmput);
  868. #ifdef CONFIG_MMU
  869. static void mmput_async_fn(struct work_struct *work)
  870. {
  871. struct mm_struct *mm = container_of(work, struct mm_struct,
  872. async_put_work);
  873. __mmput(mm);
  874. }
  875. void mmput_async(struct mm_struct *mm)
  876. {
  877. if (atomic_dec_and_test(&mm->mm_users)) {
  878. INIT_WORK(&mm->async_put_work, mmput_async_fn);
  879. schedule_work(&mm->async_put_work);
  880. }
  881. }
  882. #endif
  883. /**
  884. * set_mm_exe_file - change a reference to the mm's executable file
  885. *
  886. * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
  887. *
  888. * Main users are mmput() and sys_execve(). Callers prevent concurrent
  889. * invocations: in mmput() nobody alive left, in execve task is single
  890. * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the
  891. * mm->exe_file, but does so without using set_mm_exe_file() in order
  892. * to do avoid the need for any locks.
  893. */
  894. void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
  895. {
  896. struct file *old_exe_file;
  897. /*
  898. * It is safe to dereference the exe_file without RCU as
  899. * this function is only called if nobody else can access
  900. * this mm -- see comment above for justification.
  901. */
  902. old_exe_file = rcu_dereference_raw(mm->exe_file);
  903. if (new_exe_file)
  904. get_file(new_exe_file);
  905. rcu_assign_pointer(mm->exe_file, new_exe_file);
  906. if (old_exe_file)
  907. fput(old_exe_file);
  908. }
  909. /**
  910. * get_mm_exe_file - acquire a reference to the mm's executable file
  911. *
  912. * Returns %NULL if mm has no associated executable file.
  913. * User must release file via fput().
  914. */
  915. struct file *get_mm_exe_file(struct mm_struct *mm)
  916. {
  917. struct file *exe_file;
  918. rcu_read_lock();
  919. exe_file = rcu_dereference(mm->exe_file);
  920. if (exe_file && !get_file_rcu(exe_file))
  921. exe_file = NULL;
  922. rcu_read_unlock();
  923. return exe_file;
  924. }
  925. EXPORT_SYMBOL(get_mm_exe_file);
  926. /**
  927. * get_task_exe_file - acquire a reference to the task's executable file
  928. *
  929. * Returns %NULL if task's mm (if any) has no associated executable file or
  930. * this is a kernel thread with borrowed mm (see the comment above get_task_mm).
  931. * User must release file via fput().
  932. */
  933. struct file *get_task_exe_file(struct task_struct *task)
  934. {
  935. struct file *exe_file = NULL;
  936. struct mm_struct *mm;
  937. task_lock(task);
  938. mm = task->mm;
  939. if (mm) {
  940. if (!(task->flags & PF_KTHREAD))
  941. exe_file = get_mm_exe_file(mm);
  942. }
  943. task_unlock(task);
  944. return exe_file;
  945. }
  946. EXPORT_SYMBOL(get_task_exe_file);
  947. /**
  948. * get_task_mm - acquire a reference to the task's mm
  949. *
  950. * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning
  951. * this kernel workthread has transiently adopted a user mm with use_mm,
  952. * to do its AIO) is not set and if so returns a reference to it, after
  953. * bumping up the use count. User must release the mm via mmput()
  954. * after use. Typically used by /proc and ptrace.
  955. */
  956. struct mm_struct *get_task_mm(struct task_struct *task)
  957. {
  958. struct mm_struct *mm;
  959. task_lock(task);
  960. mm = task->mm;
  961. if (mm) {
  962. if (task->flags & PF_KTHREAD)
  963. mm = NULL;
  964. else
  965. mmget(mm);
  966. }
  967. task_unlock(task);
  968. return mm;
  969. }
  970. EXPORT_SYMBOL_GPL(get_task_mm);
  971. struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
  972. {
  973. struct mm_struct *mm;
  974. int err;
  975. err = mutex_lock_killable(&task->signal->cred_guard_mutex);
  976. if (err)
  977. return ERR_PTR(err);
  978. mm = get_task_mm(task);
  979. if (mm && mm != current->mm &&
  980. !ptrace_may_access(task, mode)) {
  981. mmput(mm);
  982. mm = ERR_PTR(-EACCES);
  983. }
  984. mutex_unlock(&task->signal->cred_guard_mutex);
  985. return mm;
  986. }
  987. static void complete_vfork_done(struct task_struct *tsk)
  988. {
  989. struct completion *vfork;
  990. task_lock(tsk);
  991. vfork = tsk->vfork_done;
  992. if (likely(vfork)) {
  993. tsk->vfork_done = NULL;
  994. complete(vfork);
  995. }
  996. task_unlock(tsk);
  997. }
  998. static int wait_for_vfork_done(struct task_struct *child,
  999. struct completion *vfork)
  1000. {
  1001. int killed;
  1002. freezer_do_not_count();
  1003. killed = wait_for_completion_killable(vfork);
  1004. freezer_count();
  1005. if (killed) {
  1006. task_lock(child);
  1007. child->vfork_done = NULL;
  1008. task_unlock(child);
  1009. }
  1010. put_task_struct(child);
  1011. return killed;
  1012. }
  1013. /* Please note the differences between mmput and mm_release.
  1014. * mmput is called whenever we stop holding onto a mm_struct,
  1015. * error success whatever.
  1016. *
  1017. * mm_release is called after a mm_struct has been removed
  1018. * from the current process.
  1019. *
  1020. * This difference is important for error handling, when we
  1021. * only half set up a mm_struct for a new process and need to restore
  1022. * the old one. Because we mmput the new mm_struct before
  1023. * restoring the old one. . .
  1024. * Eric Biederman 10 January 1998
  1025. */
  1026. void mm_release(struct task_struct *tsk, struct mm_struct *mm)
  1027. {
  1028. /* Get rid of any futexes when releasing the mm */
  1029. #ifdef CONFIG_FUTEX
  1030. if (unlikely(tsk->robust_list)) {
  1031. exit_robust_list(tsk);
  1032. tsk->robust_list = NULL;
  1033. }
  1034. #ifdef CONFIG_COMPAT
  1035. if (unlikely(tsk->compat_robust_list)) {
  1036. compat_exit_robust_list(tsk);
  1037. tsk->compat_robust_list = NULL;
  1038. }
  1039. #endif
  1040. if (unlikely(!list_empty(&tsk->pi_state_list)))
  1041. exit_pi_state_list(tsk);
  1042. #endif
  1043. uprobe_free_utask(tsk);
  1044. /* Get rid of any cached register state */
  1045. deactivate_mm(tsk, mm);
  1046. /*
  1047. * Signal userspace if we're not exiting with a core dump
  1048. * because we want to leave the value intact for debugging
  1049. * purposes.
  1050. */
  1051. if (tsk->clear_child_tid) {
  1052. if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) &&
  1053. atomic_read(&mm->mm_users) > 1) {
  1054. /*
  1055. * We don't check the error code - if userspace has
  1056. * not set up a proper pointer then tough luck.
  1057. */
  1058. put_user(0, tsk->clear_child_tid);
  1059. do_futex(tsk->clear_child_tid, FUTEX_WAKE,
  1060. 1, NULL, NULL, 0, 0);
  1061. }
  1062. tsk->clear_child_tid = NULL;
  1063. }
  1064. /*
  1065. * All done, finally we can wake up parent and return this mm to him.
  1066. * Also kthread_stop() uses this completion for synchronization.
  1067. */
  1068. if (tsk->vfork_done)
  1069. complete_vfork_done(tsk);
  1070. }
  1071. /*
  1072. * Allocate a new mm structure and copy contents from the
  1073. * mm structure of the passed in task structure.
  1074. */
  1075. static struct mm_struct *dup_mm(struct task_struct *tsk)
  1076. {
  1077. struct mm_struct *mm, *oldmm = current->mm;
  1078. int err;
  1079. mm = allocate_mm();
  1080. if (!mm)
  1081. goto fail_nomem;
  1082. memcpy(mm, oldmm, sizeof(*mm));
  1083. if (!mm_init(mm, tsk, mm->user_ns))
  1084. goto fail_nomem;
  1085. err = dup_mmap(mm, oldmm);
  1086. if (err)
  1087. goto free_pt;
  1088. mm->hiwater_rss = get_mm_rss(mm);
  1089. mm->hiwater_vm = mm->total_vm;
  1090. if (mm->binfmt && !try_module_get(mm->binfmt->module))
  1091. goto free_pt;
  1092. return mm;
  1093. free_pt:
  1094. /* don't put binfmt in mmput, we haven't got module yet */
  1095. mm->binfmt = NULL;
  1096. mmput(mm);
  1097. fail_nomem:
  1098. return NULL;
  1099. }
  1100. static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
  1101. {
  1102. struct mm_struct *mm, *oldmm;
  1103. int retval;
  1104. tsk->min_flt = tsk->maj_flt = 0;
  1105. tsk->nvcsw = tsk->nivcsw = 0;
  1106. #ifdef CONFIG_DETECT_HUNG_TASK
  1107. tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
  1108. tsk->last_switch_time = 0;
  1109. #endif
  1110. tsk->mm = NULL;
  1111. tsk->active_mm = NULL;
  1112. /*
  1113. * Are we cloning a kernel thread?
  1114. *
  1115. * We need to steal a active VM for that..
  1116. */
  1117. oldmm = current->mm;
  1118. if (!oldmm)
  1119. return 0;
  1120. /* initialize the new vmacache entries */
  1121. vmacache_flush(tsk);
  1122. if (clone_flags & CLONE_VM) {
  1123. mmget(oldmm);
  1124. mm = oldmm;
  1125. goto good_mm;
  1126. }
  1127. retval = -ENOMEM;
  1128. mm = dup_mm(tsk);
  1129. if (!mm)
  1130. goto fail_nomem;
  1131. good_mm:
  1132. tsk->mm = mm;
  1133. tsk->active_mm = mm;
  1134. return 0;
  1135. fail_nomem:
  1136. return retval;
  1137. }
  1138. static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
  1139. {
  1140. struct fs_struct *fs = current->fs;
  1141. if (clone_flags & CLONE_FS) {
  1142. /* tsk->fs is already what we want */
  1143. spin_lock(&fs->lock);
  1144. if (fs->in_exec) {
  1145. spin_unlock(&fs->lock);
  1146. return -EAGAIN;
  1147. }
  1148. fs->users++;
  1149. spin_unlock(&fs->lock);
  1150. return 0;
  1151. }
  1152. tsk->fs = copy_fs_struct(fs);
  1153. if (!tsk->fs)
  1154. return -ENOMEM;
  1155. return 0;
  1156. }
  1157. static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
  1158. {
  1159. struct files_struct *oldf, *newf;
  1160. int error = 0;
  1161. /*
  1162. * A background process may not have any files ...
  1163. */
  1164. oldf = current->files;
  1165. if (!oldf)
  1166. goto out;
  1167. if (clone_flags & CLONE_FILES) {
  1168. atomic_inc(&oldf->count);
  1169. goto out;
  1170. }
  1171. newf = dup_fd(oldf, &error);
  1172. if (!newf)
  1173. goto out;
  1174. tsk->files = newf;
  1175. error = 0;
  1176. out:
  1177. return error;
  1178. }
  1179. static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
  1180. {
  1181. #ifdef CONFIG_BLOCK
  1182. struct io_context *ioc = current->io_context;
  1183. struct io_context *new_ioc;
  1184. if (!ioc)
  1185. return 0;
  1186. /*
  1187. * Share io context with parent, if CLONE_IO is set
  1188. */
  1189. if (clone_flags & CLONE_IO) {
  1190. ioc_task_link(ioc);
  1191. tsk->io_context = ioc;
  1192. } else if (ioprio_valid(ioc->ioprio)) {
  1193. new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
  1194. if (unlikely(!new_ioc))
  1195. return -ENOMEM;
  1196. new_ioc->ioprio = ioc->ioprio;
  1197. put_io_context(new_ioc);
  1198. }
  1199. #endif
  1200. return 0;
  1201. }
  1202. static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
  1203. {
  1204. struct sighand_struct *sig;
  1205. if (clone_flags & CLONE_SIGHAND) {
  1206. atomic_inc(&current->sighand->count);
  1207. return 0;
  1208. }
  1209. sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  1210. rcu_assign_pointer(tsk->sighand, sig);
  1211. if (!sig)
  1212. return -ENOMEM;
  1213. atomic_set(&sig->count, 1);
  1214. spin_lock_irq(&current->sighand->siglock);
  1215. memcpy(sig->action, current->sighand->action, sizeof(sig->action));
  1216. spin_unlock_irq(&current->sighand->siglock);
  1217. return 0;
  1218. }
  1219. void __cleanup_sighand(struct sighand_struct *sighand)
  1220. {
  1221. if (atomic_dec_and_test(&sighand->count)) {
  1222. signalfd_cleanup(sighand);
  1223. /*
  1224. * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it
  1225. * without an RCU grace period, see __lock_task_sighand().
  1226. */
  1227. kmem_cache_free(sighand_cachep, sighand);
  1228. }
  1229. }
  1230. #ifdef CONFIG_POSIX_TIMERS
  1231. /*
  1232. * Initialize POSIX timer handling for a thread group.
  1233. */
  1234. static void posix_cpu_timers_init_group(struct signal_struct *sig)
  1235. {
  1236. unsigned long cpu_limit;
  1237. cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
  1238. if (cpu_limit != RLIM_INFINITY) {
  1239. sig->cputime_expires.prof_exp = cpu_limit * NSEC_PER_SEC;
  1240. sig->cputimer.running = true;
  1241. }
  1242. /* The timer lists. */
  1243. INIT_LIST_HEAD(&sig->cpu_timers[0]);
  1244. INIT_LIST_HEAD(&sig->cpu_timers[1]);
  1245. INIT_LIST_HEAD(&sig->cpu_timers[2]);
  1246. }
  1247. #else
  1248. static inline void posix_cpu_timers_init_group(struct signal_struct *sig) { }
  1249. #endif
  1250. static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
  1251. {
  1252. struct signal_struct *sig;
  1253. if (clone_flags & CLONE_THREAD)
  1254. return 0;
  1255. sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
  1256. tsk->signal = sig;
  1257. if (!sig)
  1258. return -ENOMEM;
  1259. sig->nr_threads = 1;
  1260. atomic_set(&sig->live, 1);
  1261. atomic_set(&sig->sigcnt, 1);
  1262. /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
  1263. sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node);
  1264. tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head);
  1265. init_waitqueue_head(&sig->wait_chldexit);
  1266. sig->curr_target = tsk;
  1267. init_sigpending(&sig->shared_pending);
  1268. INIT_HLIST_HEAD(&sig->multiprocess);
  1269. seqlock_init(&sig->stats_lock);
  1270. prev_cputime_init(&sig->prev_cputime);
  1271. #ifdef CONFIG_POSIX_TIMERS
  1272. INIT_LIST_HEAD(&sig->posix_timers);
  1273. hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1274. sig->real_timer.function = it_real_fn;
  1275. #endif
  1276. task_lock(current->group_leader);
  1277. memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
  1278. task_unlock(current->group_leader);
  1279. posix_cpu_timers_init_group(sig);
  1280. tty_audit_fork(sig);
  1281. sched_autogroup_fork(sig);
  1282. sig->oom_score_adj = current->signal->oom_score_adj;
  1283. sig->oom_score_adj_min = current->signal->oom_score_adj_min;
  1284. mutex_init(&sig->cred_guard_mutex);
  1285. return 0;
  1286. }
  1287. static void copy_seccomp(struct task_struct *p)
  1288. {
  1289. #ifdef CONFIG_SECCOMP
  1290. /*
  1291. * Must be called with sighand->lock held, which is common to
  1292. * all threads in the group. Holding cred_guard_mutex is not
  1293. * needed because this new task is not yet running and cannot
  1294. * be racing exec.
  1295. */
  1296. assert_spin_locked(&current->sighand->siglock);
  1297. /* Ref-count the new filter user, and assign it. */
  1298. get_seccomp_filter(current);
  1299. p->seccomp = current->seccomp;
  1300. /*
  1301. * Explicitly enable no_new_privs here in case it got set
  1302. * between the task_struct being duplicated and holding the
  1303. * sighand lock. The seccomp state and nnp must be in sync.
  1304. */
  1305. if (task_no_new_privs(current))
  1306. task_set_no_new_privs(p);
  1307. /*
  1308. * If the parent gained a seccomp mode after copying thread
  1309. * flags and between before we held the sighand lock, we have
  1310. * to manually enable the seccomp thread flag here.
  1311. */
  1312. if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
  1313. set_tsk_thread_flag(p, TIF_SECCOMP);
  1314. #endif
  1315. }
  1316. SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
  1317. {
  1318. current->clear_child_tid = tidptr;
  1319. return task_pid_vnr(current);
  1320. }
  1321. static void rt_mutex_init_task(struct task_struct *p)
  1322. {
  1323. raw_spin_lock_init(&p->pi_lock);
  1324. #ifdef CONFIG_RT_MUTEXES
  1325. p->pi_waiters = RB_ROOT_CACHED;
  1326. p->pi_top_task = NULL;
  1327. p->pi_blocked_on = NULL;
  1328. #endif
  1329. }
  1330. #ifdef CONFIG_POSIX_TIMERS
  1331. /*
  1332. * Initialize POSIX timer handling for a single task.
  1333. */
  1334. static void posix_cpu_timers_init(struct task_struct *tsk)
  1335. {
  1336. tsk->cputime_expires.prof_exp = 0;
  1337. tsk->cputime_expires.virt_exp = 0;
  1338. tsk->cputime_expires.sched_exp = 0;
  1339. INIT_LIST_HEAD(&tsk->cpu_timers[0]);
  1340. INIT_LIST_HEAD(&tsk->cpu_timers[1]);
  1341. INIT_LIST_HEAD(&tsk->cpu_timers[2]);
  1342. }
  1343. #else
  1344. static inline void posix_cpu_timers_init(struct task_struct *tsk) { }
  1345. #endif
  1346. static inline void init_task_pid_links(struct task_struct *task)
  1347. {
  1348. enum pid_type type;
  1349. for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
  1350. INIT_HLIST_NODE(&task->pid_links[type]);
  1351. }
  1352. }
  1353. static inline void
  1354. init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
  1355. {
  1356. if (type == PIDTYPE_PID)
  1357. task->thread_pid = pid;
  1358. else
  1359. task->signal->pids[type] = pid;
  1360. }
  1361. static inline void rcu_copy_process(struct task_struct *p)
  1362. {
  1363. #ifdef CONFIG_PREEMPT_RCU
  1364. p->rcu_read_lock_nesting = 0;
  1365. p->rcu_read_unlock_special.s = 0;
  1366. p->rcu_blocked_node = NULL;
  1367. INIT_LIST_HEAD(&p->rcu_node_entry);
  1368. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  1369. #ifdef CONFIG_TASKS_RCU
  1370. p->rcu_tasks_holdout = false;
  1371. INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
  1372. p->rcu_tasks_idle_cpu = -1;
  1373. #endif /* #ifdef CONFIG_TASKS_RCU */
  1374. }
  1375. /*
  1376. * This creates a new process as a copy of the old one,
  1377. * but does not actually start it yet.
  1378. *
  1379. * It copies the registers, and all the appropriate
  1380. * parts of the process environment (as per the clone
  1381. * flags). The actual kick-off is left to the caller.
  1382. */
  1383. static __latent_entropy struct task_struct *copy_process(
  1384. unsigned long clone_flags,
  1385. unsigned long stack_start,
  1386. unsigned long stack_size,
  1387. int __user *child_tidptr,
  1388. struct pid *pid,
  1389. int trace,
  1390. unsigned long tls,
  1391. int node)
  1392. {
  1393. int retval;
  1394. struct task_struct *p;
  1395. struct multiprocess_signals delayed;
  1396. /*
  1397. * Don't allow sharing the root directory with processes in a different
  1398. * namespace
  1399. */
  1400. if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
  1401. return ERR_PTR(-EINVAL);
  1402. if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
  1403. return ERR_PTR(-EINVAL);
  1404. /*
  1405. * Thread groups must share signals as well, and detached threads
  1406. * can only be started up within the thread group.
  1407. */
  1408. if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
  1409. return ERR_PTR(-EINVAL);
  1410. /*
  1411. * Shared signal handlers imply shared VM. By way of the above,
  1412. * thread groups also imply shared VM. Blocking this case allows
  1413. * for various simplifications in other code.
  1414. */
  1415. if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
  1416. return ERR_PTR(-EINVAL);
  1417. /*
  1418. * Siblings of global init remain as zombies on exit since they are
  1419. * not reaped by their parent (swapper). To solve this and to avoid
  1420. * multi-rooted process trees, prevent global and container-inits
  1421. * from creating siblings.
  1422. */
  1423. if ((clone_flags & CLONE_PARENT) &&
  1424. current->signal->flags & SIGNAL_UNKILLABLE)
  1425. return ERR_PTR(-EINVAL);
  1426. /*
  1427. * If the new process will be in a different pid or user namespace
  1428. * do not allow it to share a thread group with the forking task.
  1429. */
  1430. if (clone_flags & CLONE_THREAD) {
  1431. if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
  1432. (task_active_pid_ns(current) !=
  1433. current->nsproxy->pid_ns_for_children))
  1434. return ERR_PTR(-EINVAL);
  1435. }
  1436. /*
  1437. * Force any signals received before this point to be delivered
  1438. * before the fork happens. Collect up signals sent to multiple
  1439. * processes that happen during the fork and delay them so that
  1440. * they appear to happen after the fork.
  1441. */
  1442. sigemptyset(&delayed.signal);
  1443. INIT_HLIST_NODE(&delayed.node);
  1444. spin_lock_irq(&current->sighand->siglock);
  1445. if (!(clone_flags & CLONE_THREAD))
  1446. hlist_add_head(&delayed.node, &current->signal->multiprocess);
  1447. recalc_sigpending();
  1448. spin_unlock_irq(&current->sighand->siglock);
  1449. retval = -ERESTARTNOINTR;
  1450. if (signal_pending(current))
  1451. goto fork_out;
  1452. retval = -ENOMEM;
  1453. p = dup_task_struct(current, node);
  1454. if (!p)
  1455. goto fork_out;
  1456. /*
  1457. * This _must_ happen before we call free_task(), i.e. before we jump
  1458. * to any of the bad_fork_* labels. This is to avoid freeing
  1459. * p->set_child_tid which is (ab)used as a kthread's data pointer for
  1460. * kernel threads (PF_KTHREAD).
  1461. */
  1462. p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
  1463. /*
  1464. * Clear TID on mm_release()?
  1465. */
  1466. p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
  1467. ftrace_graph_init_task(p);
  1468. rt_mutex_init_task(p);
  1469. #ifdef CONFIG_PROVE_LOCKING
  1470. DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
  1471. DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
  1472. #endif
  1473. retval = -EAGAIN;
  1474. if (atomic_read(&p->real_cred->user->processes) >=
  1475. task_rlimit(p, RLIMIT_NPROC)) {
  1476. if (p->real_cred->user != INIT_USER &&
  1477. !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
  1478. goto bad_fork_free;
  1479. }
  1480. current->flags &= ~PF_NPROC_EXCEEDED;
  1481. retval = copy_creds(p, clone_flags);
  1482. if (retval < 0)
  1483. goto bad_fork_free;
  1484. /*
  1485. * If multiple threads are within copy_process(), then this check
  1486. * triggers too late. This doesn't hurt, the check is only there
  1487. * to stop root fork bombs.
  1488. */
  1489. retval = -EAGAIN;
  1490. if (nr_threads >= max_threads)
  1491. goto bad_fork_cleanup_count;
  1492. delayacct_tsk_init(p); /* Must remain after dup_task_struct() */
  1493. p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE);
  1494. p->flags |= PF_FORKNOEXEC;
  1495. INIT_LIST_HEAD(&p->children);
  1496. INIT_LIST_HEAD(&p->sibling);
  1497. rcu_copy_process(p);
  1498. p->vfork_done = NULL;
  1499. spin_lock_init(&p->alloc_lock);
  1500. init_sigpending(&p->pending);
  1501. p->utime = p->stime = p->gtime = 0;
  1502. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  1503. p->utimescaled = p->stimescaled = 0;
  1504. #endif
  1505. prev_cputime_init(&p->prev_cputime);
  1506. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  1507. seqcount_init(&p->vtime.seqcount);
  1508. p->vtime.starttime = 0;
  1509. p->vtime.state = VTIME_INACTIVE;
  1510. #endif
  1511. #if defined(SPLIT_RSS_COUNTING)
  1512. memset(&p->rss_stat, 0, sizeof(p->rss_stat));
  1513. #endif
  1514. p->default_timer_slack_ns = current->timer_slack_ns;
  1515. task_io_accounting_init(&p->ioac);
  1516. acct_clear_integrals(p);
  1517. posix_cpu_timers_init(p);
  1518. p->start_time = ktime_get_ns();
  1519. p->real_start_time = ktime_get_boot_ns();
  1520. p->io_context = NULL;
  1521. audit_set_context(p, NULL);
  1522. cgroup_fork(p);
  1523. #ifdef CONFIG_NUMA
  1524. p->mempolicy = mpol_dup(p->mempolicy);
  1525. if (IS_ERR(p->mempolicy)) {
  1526. retval = PTR_ERR(p->mempolicy);
  1527. p->mempolicy = NULL;
  1528. goto bad_fork_cleanup_threadgroup_lock;
  1529. }
  1530. #endif
  1531. #ifdef CONFIG_CPUSETS
  1532. p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
  1533. p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
  1534. seqcount_init(&p->mems_allowed_seq);
  1535. #endif
  1536. #ifdef CONFIG_TRACE_IRQFLAGS
  1537. p->irq_events = 0;
  1538. p->hardirqs_enabled = 0;
  1539. p->hardirq_enable_ip = 0;
  1540. p->hardirq_enable_event = 0;
  1541. p->hardirq_disable_ip = _THIS_IP_;
  1542. p->hardirq_disable_event = 0;
  1543. p->softirqs_enabled = 1;
  1544. p->softirq_enable_ip = _THIS_IP_;
  1545. p->softirq_enable_event = 0;
  1546. p->softirq_disable_ip = 0;
  1547. p->softirq_disable_event = 0;
  1548. p->hardirq_context = 0;
  1549. p->softirq_context = 0;
  1550. #endif
  1551. p->pagefault_disabled = 0;
  1552. #ifdef CONFIG_LOCKDEP
  1553. p->lockdep_depth = 0; /* no locks held yet */
  1554. p->curr_chain_key = 0;
  1555. p->lockdep_recursion = 0;
  1556. lockdep_init_task(p);
  1557. #endif
  1558. #ifdef CONFIG_DEBUG_MUTEXES
  1559. p->blocked_on = NULL; /* not blocked yet */
  1560. #endif
  1561. #ifdef CONFIG_BCACHE
  1562. p->sequential_io = 0;
  1563. p->sequential_io_avg = 0;
  1564. #endif
  1565. /* Perform scheduler related setup. Assign this task to a CPU. */
  1566. retval = sched_fork(clone_flags, p);
  1567. if (retval)
  1568. goto bad_fork_cleanup_policy;
  1569. retval = perf_event_init_task(p);
  1570. if (retval)
  1571. goto bad_fork_cleanup_policy;
  1572. retval = audit_alloc(p);
  1573. if (retval)
  1574. goto bad_fork_cleanup_perf;
  1575. /* copy all the process information */
  1576. shm_init_task(p);
  1577. retval = security_task_alloc(p, clone_flags);
  1578. if (retval)
  1579. goto bad_fork_cleanup_audit;
  1580. retval = copy_semundo(clone_flags, p);
  1581. if (retval)
  1582. goto bad_fork_cleanup_security;
  1583. retval = copy_files(clone_flags, p);
  1584. if (retval)
  1585. goto bad_fork_cleanup_semundo;
  1586. retval = copy_fs(clone_flags, p);
  1587. if (retval)
  1588. goto bad_fork_cleanup_files;
  1589. retval = copy_sighand(clone_flags, p);
  1590. if (retval)
  1591. goto bad_fork_cleanup_fs;
  1592. retval = copy_signal(clone_flags, p);
  1593. if (retval)
  1594. goto bad_fork_cleanup_sighand;
  1595. retval = copy_mm(clone_flags, p);
  1596. if (retval)
  1597. goto bad_fork_cleanup_signal;
  1598. retval = copy_namespaces(clone_flags, p);
  1599. if (retval)
  1600. goto bad_fork_cleanup_mm;
  1601. retval = copy_io(clone_flags, p);
  1602. if (retval)
  1603. goto bad_fork_cleanup_namespaces;
  1604. retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls);
  1605. if (retval)
  1606. goto bad_fork_cleanup_io;
  1607. if (pid != &init_struct_pid) {
  1608. pid = alloc_pid(p->nsproxy->pid_ns_for_children);
  1609. if (IS_ERR(pid)) {
  1610. retval = PTR_ERR(pid);
  1611. goto bad_fork_cleanup_thread;
  1612. }
  1613. }
  1614. #ifdef CONFIG_BLOCK
  1615. p->plug = NULL;
  1616. #endif
  1617. #ifdef CONFIG_FUTEX
  1618. p->robust_list = NULL;
  1619. #ifdef CONFIG_COMPAT
  1620. p->compat_robust_list = NULL;
  1621. #endif
  1622. INIT_LIST_HEAD(&p->pi_state_list);
  1623. p->pi_state_cache = NULL;
  1624. #endif
  1625. /*
  1626. * sigaltstack should be cleared when sharing the same VM
  1627. */
  1628. if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
  1629. sas_ss_reset(p);
  1630. /*
  1631. * Syscall tracing and stepping should be turned off in the
  1632. * child regardless of CLONE_PTRACE.
  1633. */
  1634. user_disable_single_step(p);
  1635. clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
  1636. #ifdef TIF_SYSCALL_EMU
  1637. clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
  1638. #endif
  1639. clear_all_latency_tracing(p);
  1640. /* ok, now we should be set up.. */
  1641. p->pid = pid_nr(pid);
  1642. if (clone_flags & CLONE_THREAD) {
  1643. p->exit_signal = -1;
  1644. p->group_leader = current->group_leader;
  1645. p->tgid = current->tgid;
  1646. } else {
  1647. if (clone_flags & CLONE_PARENT)
  1648. p->exit_signal = current->group_leader->exit_signal;
  1649. else
  1650. p->exit_signal = (clone_flags & CSIGNAL);
  1651. p->group_leader = p;
  1652. p->tgid = p->pid;
  1653. }
  1654. p->nr_dirtied = 0;
  1655. p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
  1656. p->dirty_paused_when = 0;
  1657. p->pdeath_signal = 0;
  1658. INIT_LIST_HEAD(&p->thread_group);
  1659. p->task_works = NULL;
  1660. cgroup_threadgroup_change_begin(current);
  1661. /*
  1662. * Ensure that the cgroup subsystem policies allow the new process to be
  1663. * forked. It should be noted the the new process's css_set can be changed
  1664. * between here and cgroup_post_fork() if an organisation operation is in
  1665. * progress.
  1666. */
  1667. retval = cgroup_can_fork(p);
  1668. if (retval)
  1669. goto bad_fork_free_pid;
  1670. /*
  1671. * Make it visible to the rest of the system, but dont wake it up yet.
  1672. * Need tasklist lock for parent etc handling!
  1673. */
  1674. write_lock_irq(&tasklist_lock);
  1675. /* CLONE_PARENT re-uses the old parent */
  1676. if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
  1677. p->real_parent = current->real_parent;
  1678. p->parent_exec_id = current->parent_exec_id;
  1679. } else {
  1680. p->real_parent = current;
  1681. p->parent_exec_id = current->self_exec_id;
  1682. }
  1683. klp_copy_process(p);
  1684. spin_lock(&current->sighand->siglock);
  1685. /*
  1686. * Copy seccomp details explicitly here, in case they were changed
  1687. * before holding sighand lock.
  1688. */
  1689. copy_seccomp(p);
  1690. rseq_fork(p, clone_flags);
  1691. /* Don't start children in a dying pid namespace */
  1692. if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {
  1693. retval = -ENOMEM;
  1694. goto bad_fork_cancel_cgroup;
  1695. }
  1696. /* Let kill terminate clone/fork in the middle */
  1697. if (fatal_signal_pending(current)) {
  1698. retval = -EINTR;
  1699. goto bad_fork_cancel_cgroup;
  1700. }
  1701. init_task_pid_links(p);
  1702. if (likely(p->pid)) {
  1703. ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
  1704. init_task_pid(p, PIDTYPE_PID, pid);
  1705. if (thread_group_leader(p)) {
  1706. init_task_pid(p, PIDTYPE_TGID, pid);
  1707. init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
  1708. init_task_pid(p, PIDTYPE_SID, task_session(current));
  1709. if (is_child_reaper(pid)) {
  1710. ns_of_pid(pid)->child_reaper = p;
  1711. p->signal->flags |= SIGNAL_UNKILLABLE;
  1712. }
  1713. p->signal->shared_pending.signal = delayed.signal;
  1714. p->signal->tty = tty_kref_get(current->signal->tty);
  1715. /*
  1716. * Inherit has_child_subreaper flag under the same
  1717. * tasklist_lock with adding child to the process tree
  1718. * for propagate_has_child_subreaper optimization.
  1719. */
  1720. p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper ||
  1721. p->real_parent->signal->is_child_subreaper;
  1722. list_add_tail(&p->sibling, &p->real_parent->children);
  1723. list_add_tail_rcu(&p->tasks, &init_task.tasks);
  1724. attach_pid(p, PIDTYPE_TGID);
  1725. attach_pid(p, PIDTYPE_PGID);
  1726. attach_pid(p, PIDTYPE_SID);
  1727. __this_cpu_inc(process_counts);
  1728. } else {
  1729. current->signal->nr_threads++;
  1730. atomic_inc(&current->signal->live);
  1731. atomic_inc(&current->signal->sigcnt);
  1732. task_join_group_stop(p);
  1733. list_add_tail_rcu(&p->thread_group,
  1734. &p->group_leader->thread_group);
  1735. list_add_tail_rcu(&p->thread_node,
  1736. &p->signal->thread_head);
  1737. }
  1738. attach_pid(p, PIDTYPE_PID);
  1739. nr_threads++;
  1740. }
  1741. total_forks++;
  1742. hlist_del_init(&delayed.node);
  1743. spin_unlock(&current->sighand->siglock);
  1744. syscall_tracepoint_update(p);
  1745. write_unlock_irq(&tasklist_lock);
  1746. proc_fork_connector(p);
  1747. cgroup_post_fork(p);
  1748. cgroup_threadgroup_change_end(current);
  1749. perf_event_fork(p);
  1750. trace_task_newtask(p, clone_flags);
  1751. uprobe_copy_process(p, clone_flags);
  1752. return p;
  1753. bad_fork_cancel_cgroup:
  1754. spin_unlock(&current->sighand->siglock);
  1755. write_unlock_irq(&tasklist_lock);
  1756. cgroup_cancel_fork(p);
  1757. bad_fork_free_pid:
  1758. cgroup_threadgroup_change_end(current);
  1759. if (pid != &init_struct_pid)
  1760. free_pid(pid);
  1761. bad_fork_cleanup_thread:
  1762. exit_thread(p);
  1763. bad_fork_cleanup_io:
  1764. if (p->io_context)
  1765. exit_io_context(p);
  1766. bad_fork_cleanup_namespaces:
  1767. exit_task_namespaces(p);
  1768. bad_fork_cleanup_mm:
  1769. if (p->mm)
  1770. mmput(p->mm);
  1771. bad_fork_cleanup_signal:
  1772. if (!(clone_flags & CLONE_THREAD))
  1773. free_signal_struct(p->signal);
  1774. bad_fork_cleanup_sighand:
  1775. __cleanup_sighand(p->sighand);
  1776. bad_fork_cleanup_fs:
  1777. exit_fs(p); /* blocking */
  1778. bad_fork_cleanup_files:
  1779. exit_files(p); /* blocking */
  1780. bad_fork_cleanup_semundo:
  1781. exit_sem(p);
  1782. bad_fork_cleanup_security:
  1783. security_task_free(p);
  1784. bad_fork_cleanup_audit:
  1785. audit_free(p);
  1786. bad_fork_cleanup_perf:
  1787. perf_event_free_task(p);
  1788. bad_fork_cleanup_policy:
  1789. lockdep_free_task(p);
  1790. #ifdef CONFIG_NUMA
  1791. mpol_put(p->mempolicy);
  1792. bad_fork_cleanup_threadgroup_lock:
  1793. #endif
  1794. delayacct_tsk_free(p);
  1795. bad_fork_cleanup_count:
  1796. atomic_dec(&p->cred->user->processes);
  1797. exit_creds(p);
  1798. bad_fork_free:
  1799. p->state = TASK_DEAD;
  1800. put_task_stack(p);
  1801. free_task(p);
  1802. fork_out:
  1803. spin_lock_irq(&current->sighand->siglock);
  1804. hlist_del_init(&delayed.node);
  1805. spin_unlock_irq(&current->sighand->siglock);
  1806. return ERR_PTR(retval);
  1807. }
  1808. static inline void init_idle_pids(struct task_struct *idle)
  1809. {
  1810. enum pid_type type;
  1811. for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
  1812. INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */
  1813. init_task_pid(idle, type, &init_struct_pid);
  1814. }
  1815. }
  1816. struct task_struct *fork_idle(int cpu)
  1817. {
  1818. struct task_struct *task;
  1819. task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0,
  1820. cpu_to_node(cpu));
  1821. if (!IS_ERR(task)) {
  1822. init_idle_pids(task);
  1823. init_idle(task, cpu);
  1824. }
  1825. return task;
  1826. }
  1827. /*
  1828. * Ok, this is the main fork-routine.
  1829. *
  1830. * It copies the process, and if successful kick-starts
  1831. * it and waits for it to finish using the VM if required.
  1832. */
  1833. long _do_fork(unsigned long clone_flags,
  1834. unsigned long stack_start,
  1835. unsigned long stack_size,
  1836. int __user *parent_tidptr,
  1837. int __user *child_tidptr,
  1838. unsigned long tls)
  1839. {
  1840. struct completion vfork;
  1841. struct pid *pid;
  1842. struct task_struct *p;
  1843. int trace = 0;
  1844. long nr;
  1845. /*
  1846. * Determine whether and which event to report to ptracer. When
  1847. * called from kernel_thread or CLONE_UNTRACED is explicitly
  1848. * requested, no event is reported; otherwise, report if the event
  1849. * for the type of forking is enabled.
  1850. */
  1851. if (!(clone_flags & CLONE_UNTRACED)) {
  1852. if (clone_flags & CLONE_VFORK)
  1853. trace = PTRACE_EVENT_VFORK;
  1854. else if ((clone_flags & CSIGNAL) != SIGCHLD)
  1855. trace = PTRACE_EVENT_CLONE;
  1856. else
  1857. trace = PTRACE_EVENT_FORK;
  1858. if (likely(!ptrace_event_enabled(current, trace)))
  1859. trace = 0;
  1860. }
  1861. p = copy_process(clone_flags, stack_start, stack_size,
  1862. child_tidptr, NULL, trace, tls, NUMA_NO_NODE);
  1863. add_latent_entropy();
  1864. if (IS_ERR(p))
  1865. return PTR_ERR(p);
  1866. /*
  1867. * Do this prior waking up the new thread - the thread pointer
  1868. * might get invalid after that point, if the thread exits quickly.
  1869. */
  1870. trace_sched_process_fork(current, p);
  1871. pid = get_task_pid(p, PIDTYPE_PID);
  1872. nr = pid_vnr(pid);
  1873. if (clone_flags & CLONE_PARENT_SETTID)
  1874. put_user(nr, parent_tidptr);
  1875. if (clone_flags & CLONE_VFORK) {
  1876. p->vfork_done = &vfork;
  1877. init_completion(&vfork);
  1878. get_task_struct(p);
  1879. }
  1880. wake_up_new_task(p);
  1881. /* forking complete and child started to run, tell ptracer */
  1882. if (unlikely(trace))
  1883. ptrace_event_pid(trace, pid);
  1884. if (clone_flags & CLONE_VFORK) {
  1885. if (!wait_for_vfork_done(p, &vfork))
  1886. ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);
  1887. }
  1888. put_pid(pid);
  1889. return nr;
  1890. }
  1891. #ifndef CONFIG_HAVE_COPY_THREAD_TLS
  1892. /* For compatibility with architectures that call do_fork directly rather than
  1893. * using the syscall entry points below. */
  1894. long do_fork(unsigned long clone_flags,
  1895. unsigned long stack_start,
  1896. unsigned long stack_size,
  1897. int __user *parent_tidptr,
  1898. int __user *child_tidptr)
  1899. {
  1900. return _do_fork(clone_flags, stack_start, stack_size,
  1901. parent_tidptr, child_tidptr, 0);
  1902. }
  1903. #endif
  1904. /*
  1905. * Create a kernel thread.
  1906. */
  1907. pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  1908. {
  1909. return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
  1910. (unsigned long)arg, NULL, NULL, 0);
  1911. }
  1912. #ifdef __ARCH_WANT_SYS_FORK
  1913. SYSCALL_DEFINE0(fork)
  1914. {
  1915. #ifdef CONFIG_MMU
  1916. return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0);
  1917. #else
  1918. /* can not support in nommu mode */
  1919. return -EINVAL;
  1920. #endif
  1921. }
  1922. #endif
  1923. #ifdef __ARCH_WANT_SYS_VFORK
  1924. SYSCALL_DEFINE0(vfork)
  1925. {
  1926. return _do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0,
  1927. 0, NULL, NULL, 0);
  1928. }
  1929. #endif
  1930. #ifdef __ARCH_WANT_SYS_CLONE
  1931. #ifdef CONFIG_CLONE_BACKWARDS
  1932. SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
  1933. int __user *, parent_tidptr,
  1934. unsigned long, tls,
  1935. int __user *, child_tidptr)
  1936. #elif defined(CONFIG_CLONE_BACKWARDS2)
  1937. SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
  1938. int __user *, parent_tidptr,
  1939. int __user *, child_tidptr,
  1940. unsigned long, tls)
  1941. #elif defined(CONFIG_CLONE_BACKWARDS3)
  1942. SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
  1943. int, stack_size,
  1944. int __user *, parent_tidptr,
  1945. int __user *, child_tidptr,
  1946. unsigned long, tls)
  1947. #else
  1948. SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
  1949. int __user *, parent_tidptr,
  1950. int __user *, child_tidptr,
  1951. unsigned long, tls)
  1952. #endif
  1953. {
  1954. return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls);
  1955. }
  1956. #endif
  1957. void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data)
  1958. {
  1959. struct task_struct *leader, *parent, *child;
  1960. int res;
  1961. read_lock(&tasklist_lock);
  1962. leader = top = top->group_leader;
  1963. down:
  1964. for_each_thread(leader, parent) {
  1965. list_for_each_entry(child, &parent->children, sibling) {
  1966. res = visitor(child, data);
  1967. if (res) {
  1968. if (res < 0)
  1969. goto out;
  1970. leader = child;
  1971. goto down;
  1972. }
  1973. up:
  1974. ;
  1975. }
  1976. }
  1977. if (leader != top) {
  1978. child = leader;
  1979. parent = child->real_parent;
  1980. leader = parent->group_leader;
  1981. goto up;
  1982. }
  1983. out:
  1984. read_unlock(&tasklist_lock);
  1985. }
  1986. #ifndef ARCH_MIN_MMSTRUCT_ALIGN
  1987. #define ARCH_MIN_MMSTRUCT_ALIGN 0
  1988. #endif
  1989. static void sighand_ctor(void *data)
  1990. {
  1991. struct sighand_struct *sighand = data;
  1992. spin_lock_init(&sighand->siglock);
  1993. init_waitqueue_head(&sighand->signalfd_wqh);
  1994. }
  1995. void __init proc_caches_init(void)
  1996. {
  1997. unsigned int mm_size;
  1998. sighand_cachep = kmem_cache_create("sighand_cache",
  1999. sizeof(struct sighand_struct), 0,
  2000. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU|
  2001. SLAB_ACCOUNT, sighand_ctor);
  2002. signal_cachep = kmem_cache_create("signal_cache",
  2003. sizeof(struct signal_struct), 0,
  2004. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
  2005. NULL);
  2006. files_cachep = kmem_cache_create("files_cache",
  2007. sizeof(struct files_struct), 0,
  2008. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
  2009. NULL);
  2010. fs_cachep = kmem_cache_create("fs_cache",
  2011. sizeof(struct fs_struct), 0,
  2012. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
  2013. NULL);
  2014. /*
  2015. * The mm_cpumask is located at the end of mm_struct, and is
  2016. * dynamically sized based on the maximum CPU number this system
  2017. * can have, taking hotplug into account (nr_cpu_ids).
  2018. */
  2019. mm_size = sizeof(struct mm_struct) + cpumask_size();
  2020. mm_cachep = kmem_cache_create_usercopy("mm_struct",
  2021. mm_size, ARCH_MIN_MMSTRUCT_ALIGN,
  2022. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
  2023. offsetof(struct mm_struct, saved_auxv),
  2024. sizeof_field(struct mm_struct, saved_auxv),
  2025. NULL);
  2026. vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT);
  2027. mmap_init();
  2028. nsproxy_cache_init();
  2029. }
  2030. /*
  2031. * Check constraints on flags passed to the unshare system call.
  2032. */
  2033. static int check_unshare_flags(unsigned long unshare_flags)
  2034. {
  2035. if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
  2036. CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
  2037. CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
  2038. CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP))
  2039. return -EINVAL;
  2040. /*
  2041. * Not implemented, but pretend it works if there is nothing
  2042. * to unshare. Note that unsharing the address space or the
  2043. * signal handlers also need to unshare the signal queues (aka
  2044. * CLONE_THREAD).
  2045. */
  2046. if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
  2047. if (!thread_group_empty(current))
  2048. return -EINVAL;
  2049. }
  2050. if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {
  2051. if (atomic_read(&current->sighand->count) > 1)
  2052. return -EINVAL;
  2053. }
  2054. if (unshare_flags & CLONE_VM) {
  2055. if (!current_is_single_threaded())
  2056. return -EINVAL;
  2057. }
  2058. return 0;
  2059. }
  2060. /*
  2061. * Unshare the filesystem structure if it is being shared
  2062. */
  2063. static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
  2064. {
  2065. struct fs_struct *fs = current->fs;
  2066. if (!(unshare_flags & CLONE_FS) || !fs)
  2067. return 0;
  2068. /* don't need lock here; in the worst case we'll do useless copy */
  2069. if (fs->users == 1)
  2070. return 0;
  2071. *new_fsp = copy_fs_struct(fs);
  2072. if (!*new_fsp)
  2073. return -ENOMEM;
  2074. return 0;
  2075. }
  2076. /*
  2077. * Unshare file descriptor table if it is being shared
  2078. */
  2079. static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
  2080. {
  2081. struct files_struct *fd = current->files;
  2082. int error = 0;
  2083. if ((unshare_flags & CLONE_FILES) &&
  2084. (fd && atomic_read(&fd->count) > 1)) {
  2085. *new_fdp = dup_fd(fd, &error);
  2086. if (!*new_fdp)
  2087. return error;
  2088. }
  2089. return 0;
  2090. }
  2091. /*
  2092. * unshare allows a process to 'unshare' part of the process
  2093. * context which was originally shared using clone. copy_*
  2094. * functions used by do_fork() cannot be used here directly
  2095. * because they modify an inactive task_struct that is being
  2096. * constructed. Here we are modifying the current, active,
  2097. * task_struct.
  2098. */
  2099. int ksys_unshare(unsigned long unshare_flags)
  2100. {
  2101. struct fs_struct *fs, *new_fs = NULL;
  2102. struct files_struct *fd, *new_fd = NULL;
  2103. struct cred *new_cred = NULL;
  2104. struct nsproxy *new_nsproxy = NULL;
  2105. int do_sysvsem = 0;
  2106. int err;
  2107. /*
  2108. * If unsharing a user namespace must also unshare the thread group
  2109. * and unshare the filesystem root and working directories.
  2110. */
  2111. if (unshare_flags & CLONE_NEWUSER)
  2112. unshare_flags |= CLONE_THREAD | CLONE_FS;
  2113. /*
  2114. * If unsharing vm, must also unshare signal handlers.
  2115. */
  2116. if (unshare_flags & CLONE_VM)
  2117. unshare_flags |= CLONE_SIGHAND;
  2118. /*
  2119. * If unsharing a signal handlers, must also unshare the signal queues.
  2120. */
  2121. if (unshare_flags & CLONE_SIGHAND)
  2122. unshare_flags |= CLONE_THREAD;
  2123. /*
  2124. * If unsharing namespace, must also unshare filesystem information.
  2125. */
  2126. if (unshare_flags & CLONE_NEWNS)
  2127. unshare_flags |= CLONE_FS;
  2128. err = check_unshare_flags(unshare_flags);
  2129. if (err)
  2130. goto bad_unshare_out;
  2131. /*
  2132. * CLONE_NEWIPC must also detach from the undolist: after switching
  2133. * to a new ipc namespace, the semaphore arrays from the old
  2134. * namespace are unreachable.
  2135. */
  2136. if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
  2137. do_sysvsem = 1;
  2138. err = unshare_fs(unshare_flags, &new_fs);
  2139. if (err)
  2140. goto bad_unshare_out;
  2141. err = unshare_fd(unshare_flags, &new_fd);
  2142. if (err)
  2143. goto bad_unshare_cleanup_fs;
  2144. err = unshare_userns(unshare_flags, &new_cred);
  2145. if (err)
  2146. goto bad_unshare_cleanup_fd;
  2147. err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
  2148. new_cred, new_fs);
  2149. if (err)
  2150. goto bad_unshare_cleanup_cred;
  2151. if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
  2152. if (do_sysvsem) {
  2153. /*
  2154. * CLONE_SYSVSEM is equivalent to sys_exit().
  2155. */
  2156. exit_sem(current);
  2157. }
  2158. if (unshare_flags & CLONE_NEWIPC) {
  2159. /* Orphan segments in old ns (see sem above). */
  2160. exit_shm(current);
  2161. shm_init_task(current);
  2162. }
  2163. if (new_nsproxy)
  2164. switch_task_namespaces(current, new_nsproxy);
  2165. task_lock(current);
  2166. if (new_fs) {
  2167. fs = current->fs;
  2168. spin_lock(&fs->lock);
  2169. current->fs = new_fs;
  2170. if (--fs->users)
  2171. new_fs = NULL;
  2172. else
  2173. new_fs = fs;
  2174. spin_unlock(&fs->lock);
  2175. }
  2176. if (new_fd) {
  2177. fd = current->files;
  2178. current->files = new_fd;
  2179. new_fd = fd;
  2180. }
  2181. task_unlock(current);
  2182. if (new_cred) {
  2183. /* Install the new user namespace */
  2184. commit_creds(new_cred);
  2185. new_cred = NULL;
  2186. }
  2187. }
  2188. perf_event_namespaces(current);
  2189. bad_unshare_cleanup_cred:
  2190. if (new_cred)
  2191. put_cred(new_cred);
  2192. bad_unshare_cleanup_fd:
  2193. if (new_fd)
  2194. put_files_struct(new_fd);
  2195. bad_unshare_cleanup_fs:
  2196. if (new_fs)
  2197. free_fs_struct(new_fs);
  2198. bad_unshare_out:
  2199. return err;
  2200. }
  2201. SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
  2202. {
  2203. return ksys_unshare(unshare_flags);
  2204. }
  2205. /*
  2206. * Helper to unshare the files of the current task.
  2207. * We don't want to expose copy_files internals to
  2208. * the exec layer of the kernel.
  2209. */
  2210. int unshare_files(struct files_struct **displaced)
  2211. {
  2212. struct task_struct *task = current;
  2213. struct files_struct *copy = NULL;
  2214. int error;
  2215. error = unshare_fd(CLONE_FILES, &copy);
  2216. if (error || !copy) {
  2217. *displaced = NULL;
  2218. return error;
  2219. }
  2220. *displaced = task->files;
  2221. task_lock(task);
  2222. task->files = copy;
  2223. task_unlock(task);
  2224. return 0;
  2225. }
  2226. int sysctl_max_threads(struct ctl_table *table, int write,
  2227. void __user *buffer, size_t *lenp, loff_t *ppos)
  2228. {
  2229. struct ctl_table t;
  2230. int ret;
  2231. int threads = max_threads;
  2232. int min = MIN_THREADS;
  2233. int max = MAX_THREADS;
  2234. t = *table;
  2235. t.data = &threads;
  2236. t.extra1 = &min;
  2237. t.extra2 = &max;
  2238. ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
  2239. if (ret || !write)
  2240. return ret;
  2241. set_max_threads(threads);
  2242. return 0;
  2243. }