fork.c 59 KB

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