fork.c 57 KB

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