fork.c 58 KB

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