fork.c 59 KB

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