fork.c 57 KB

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