fork.c 62 KB

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