fork.c 63 KB

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