fork.c 56 KB

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