fork.c 76 KB

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