fork.c 57 KB

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