fork.c 60 KB

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