fork.c 62 KB

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