fork.c 59 KB

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