fork.c 65 KB

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