fork.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  1. /*
  2. * linux/kernel/fork.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * 'fork.c' contains the help-routines for the 'fork' system call
  8. * (see also entry.S and others).
  9. * Fork is rather simple, once you get the hang of it, but the memory
  10. * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/sched/autogroup.h>
  14. #include <linux/sched/mm.h>
  15. #include <linux/sched/coredump.h>
  16. #include <linux/sched/user.h>
  17. #include <linux/sched/numa_balancing.h>
  18. #include <linux/sched/stat.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/sched/task_stack.h>
  21. #include <linux/sched/cputime.h>
  22. #include <linux/rtmutex.h>
  23. #include <linux/init.h>
  24. #include <linux/unistd.h>
  25. #include <linux/module.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/completion.h>
  28. #include <linux/personality.h>
  29. #include <linux/mempolicy.h>
  30. #include <linux/sem.h>
  31. #include <linux/file.h>
  32. #include <linux/fdtable.h>
  33. #include <linux/iocontext.h>
  34. #include <linux/key.h>
  35. #include <linux/binfmts.h>
  36. #include <linux/mman.h>
  37. #include <linux/mmu_notifier.h>
  38. #include <linux/hmm.h>
  39. #include <linux/fs.h>
  40. #include <linux/mm.h>
  41. #include <linux/vmacache.h>
  42. #include <linux/nsproxy.h>
  43. #include <linux/capability.h>
  44. #include <linux/cpu.h>
  45. #include <linux/cgroup.h>
  46. #include <linux/security.h>
  47. #include <linux/hugetlb.h>
  48. #include <linux/seccomp.h>
  49. #include <linux/swap.h>
  50. #include <linux/syscalls.h>
  51. #include <linux/jiffies.h>
  52. #include <linux/futex.h>
  53. #include <linux/compat.h>
  54. #include <linux/kthread.h>
  55. #include <linux/task_io_accounting_ops.h>
  56. #include <linux/rcupdate.h>
  57. #include <linux/ptrace.h>
  58. #include <linux/mount.h>
  59. #include <linux/audit.h>
  60. #include <linux/memcontrol.h>
  61. #include <linux/ftrace.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/profile.h>
  64. #include <linux/rmap.h>
  65. #include <linux/ksm.h>
  66. #include <linux/acct.h>
  67. #include <linux/userfaultfd_k.h>
  68. #include <linux/tsacct_kern.h>
  69. #include <linux/cn_proc.h>
  70. #include <linux/freezer.h>
  71. #include <linux/delayacct.h>
  72. #include <linux/taskstats_kern.h>
  73. #include <linux/random.h>
  74. #include <linux/tty.h>
  75. #include <linux/blkdev.h>
  76. #include <linux/fs_struct.h>
  77. #include <linux/magic.h>
  78. #include <linux/sched/mm.h>
  79. #include <linux/perf_event.h>
  80. #include <linux/posix-timers.h>
  81. #include <linux/user-return-notifier.h>
  82. #include <linux/oom.h>
  83. #include <linux/khugepaged.h>
  84. #include <linux/signalfd.h>
  85. #include <linux/uprobes.h>
  86. #include <linux/aio.h>
  87. #include <linux/compiler.h>
  88. #include <linux/sysctl.h>
  89. #include <linux/kcov.h>
  90. #include <linux/livepatch.h>
  91. #include <linux/thread_info.h>
  92. #include <asm/pgtable.h>
  93. #include <asm/pgalloc.h>
  94. #include <linux/uaccess.h>
  95. #include <asm/mmu_context.h>
  96. #include <asm/cacheflush.h>
  97. #include <asm/tlbflush.h>
  98. #include <trace/events/sched.h>
  99. #define CREATE_TRACE_POINTS
  100. #include <trace/events/task.h>
  101. /*
  102. * Minimum number of threads to boot the kernel
  103. */
  104. #define MIN_THREADS 20
  105. /*
  106. * Maximum number of threads
  107. */
  108. #define MAX_THREADS FUTEX_TID_MASK
  109. /*
  110. * Protected counters by write_lock_irq(&tasklist_lock)
  111. */
  112. unsigned long total_forks; /* Handle normal Linux uptimes. */
  113. int nr_threads; /* The idle threads do not count.. */
  114. int max_threads; /* tunable limit on nr_threads */
  115. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  116. __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  117. #ifdef CONFIG_PROVE_RCU
  118. int lockdep_tasklist_lock_is_held(void)
  119. {
  120. return lockdep_is_held(&tasklist_lock);
  121. }
  122. EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
  123. #endif /* #ifdef CONFIG_PROVE_RCU */
  124. int nr_processes(void)
  125. {
  126. int cpu;
  127. int total = 0;
  128. for_each_possible_cpu(cpu)
  129. total += per_cpu(process_counts, cpu);
  130. return total;
  131. }
  132. void __weak arch_release_task_struct(struct task_struct *tsk)
  133. {
  134. }
  135. #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
  136. static struct kmem_cache *task_struct_cachep;
  137. static inline struct task_struct *alloc_task_struct_node(int node)
  138. {
  139. return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node);
  140. }
  141. static inline void free_task_struct(struct task_struct *tsk)
  142. {
  143. kmem_cache_free(task_struct_cachep, tsk);
  144. }
  145. #endif
  146. void __weak arch_release_thread_stack(unsigned long *stack)
  147. {
  148. }
  149. #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR
  150. /*
  151. * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
  152. * kmemcache based allocator.
  153. */
  154. # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)
  155. #ifdef CONFIG_VMAP_STACK
  156. /*
  157. * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB
  158. * flush. Try to minimize the number of calls by caching stacks.
  159. */
  160. #define NR_CACHED_STACKS 2
  161. static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]);
  162. static int free_vm_stack_cache(unsigned int cpu)
  163. {
  164. struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu);
  165. int i;
  166. for (i = 0; i < NR_CACHED_STACKS; i++) {
  167. struct vm_struct *vm_stack = cached_vm_stacks[i];
  168. if (!vm_stack)
  169. continue;
  170. vfree(vm_stack->addr);
  171. cached_vm_stacks[i] = NULL;
  172. }
  173. return 0;
  174. }
  175. #endif
  176. static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
  177. {
  178. #ifdef CONFIG_VMAP_STACK
  179. void *stack;
  180. int i;
  181. for (i = 0; i < NR_CACHED_STACKS; i++) {
  182. struct vm_struct *s;
  183. s = this_cpu_xchg(cached_stacks[i], NULL);
  184. if (!s)
  185. continue;
  186. /* Clear stale pointers from reused stack. */
  187. memset(s->addr, 0, THREAD_SIZE);
  188. tsk->stack_vm_area = s;
  189. return s->addr;
  190. }
  191. stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN,
  192. VMALLOC_START, VMALLOC_END,
  193. THREADINFO_GFP,
  194. PAGE_KERNEL,
  195. 0, node, __builtin_return_address(0));
  196. /*
  197. * We can't call find_vm_area() in interrupt context, and
  198. * free_thread_stack() can be called in interrupt context,
  199. * so cache the vm_struct.
  200. */
  201. if (stack)
  202. tsk->stack_vm_area = find_vm_area(stack);
  203. return stack;
  204. #else
  205. struct page *page = alloc_pages_node(node, THREADINFO_GFP,
  206. THREAD_SIZE_ORDER);
  207. return page ? page_address(page) : NULL;
  208. #endif
  209. }
  210. static inline void free_thread_stack(struct task_struct *tsk)
  211. {
  212. #ifdef CONFIG_VMAP_STACK
  213. if (task_stack_vm_area(tsk)) {
  214. int i;
  215. for (i = 0; i < NR_CACHED_STACKS; i++) {
  216. if (this_cpu_cmpxchg(cached_stacks[i],
  217. NULL, tsk->stack_vm_area) != NULL)
  218. continue;
  219. return;
  220. }
  221. vfree_atomic(tsk->stack);
  222. return;
  223. }
  224. #endif
  225. __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER);
  226. }
  227. # else
  228. static struct kmem_cache *thread_stack_cache;
  229. static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
  230. int node)
  231. {
  232. return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
  233. }
  234. static void free_thread_stack(struct task_struct *tsk)
  235. {
  236. kmem_cache_free(thread_stack_cache, tsk->stack);
  237. }
  238. void thread_stack_cache_init(void)
  239. {
  240. thread_stack_cache = kmem_cache_create_usercopy("thread_stack",
  241. THREAD_SIZE, THREAD_SIZE, 0, 0,
  242. THREAD_SIZE, NULL);
  243. BUG_ON(thread_stack_cache == NULL);
  244. }
  245. # endif
  246. #endif
  247. /* SLAB cache for signal_struct structures (tsk->signal) */
  248. static struct kmem_cache *signal_cachep;
  249. /* SLAB cache for sighand_struct structures (tsk->sighand) */
  250. struct kmem_cache *sighand_cachep;
  251. /* SLAB cache for files_struct structures (tsk->files) */
  252. struct kmem_cache *files_cachep;
  253. /* SLAB cache for fs_struct structures (tsk->fs) */
  254. struct kmem_cache *fs_cachep;
  255. /* SLAB cache for vm_area_struct structures */
  256. struct kmem_cache *vm_area_cachep;
  257. /* SLAB cache for mm_struct structures (tsk->mm) */
  258. static struct kmem_cache *mm_cachep;
  259. static void account_kernel_stack(struct task_struct *tsk, int account)
  260. {
  261. void *stack = task_stack_page(tsk);
  262. struct vm_struct *vm = task_stack_vm_area(tsk);
  263. BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0);
  264. if (vm) {
  265. int i;
  266. BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
  267. for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
  268. mod_zone_page_state(page_zone(vm->pages[i]),
  269. NR_KERNEL_STACK_KB,
  270. PAGE_SIZE / 1024 * account);
  271. }
  272. /* All stack pages belong to the same memcg. */
  273. mod_memcg_page_state(vm->pages[0], MEMCG_KERNEL_STACK_KB,
  274. account * (THREAD_SIZE / 1024));
  275. } else {
  276. /*
  277. * All stack pages are in the same zone and belong to the
  278. * same memcg.
  279. */
  280. struct page *first_page = virt_to_page(stack);
  281. mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB,
  282. THREAD_SIZE / 1024 * account);
  283. mod_memcg_page_state(first_page, MEMCG_KERNEL_STACK_KB,
  284. account * (THREAD_SIZE / 1024));
  285. }
  286. }
  287. static void release_task_stack(struct task_struct *tsk)
  288. {
  289. if (WARN_ON(tsk->state != TASK_DEAD))
  290. return; /* Better to leak the stack than to free prematurely */
  291. account_kernel_stack(tsk, -1);
  292. arch_release_thread_stack(tsk->stack);
  293. free_thread_stack(tsk);
  294. tsk->stack = NULL;
  295. #ifdef CONFIG_VMAP_STACK
  296. tsk->stack_vm_area = NULL;
  297. #endif
  298. }
  299. #ifdef CONFIG_THREAD_INFO_IN_TASK
  300. void put_task_stack(struct task_struct *tsk)
  301. {
  302. if (atomic_dec_and_test(&tsk->stack_refcount))
  303. release_task_stack(tsk);
  304. }
  305. #endif
  306. void free_task(struct task_struct *tsk)
  307. {
  308. #ifndef CONFIG_THREAD_INFO_IN_TASK
  309. /*
  310. * The task is finally done with both the stack and thread_info,
  311. * so free both.
  312. */
  313. release_task_stack(tsk);
  314. #else
  315. /*
  316. * If the task had a separate stack allocation, it should be gone
  317. * by now.
  318. */
  319. WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0);
  320. #endif
  321. rt_mutex_debug_task_free(tsk);
  322. ftrace_graph_exit_task(tsk);
  323. put_seccomp_filter(tsk);
  324. arch_release_task_struct(tsk);
  325. if (tsk->flags & PF_KTHREAD)
  326. free_kthread_struct(tsk);
  327. free_task_struct(tsk);
  328. }
  329. EXPORT_SYMBOL(free_task);
  330. #ifdef CONFIG_MMU
  331. static __latent_entropy int dup_mmap(struct mm_struct *mm,
  332. struct mm_struct *oldmm)
  333. {
  334. struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
  335. struct rb_node **rb_link, *rb_parent;
  336. int retval;
  337. unsigned long charge;
  338. LIST_HEAD(uf);
  339. uprobe_start_dup_mmap();
  340. if (down_write_killable(&oldmm->mmap_sem)) {
  341. retval = -EINTR;
  342. goto fail_uprobe_end;
  343. }
  344. flush_cache_dup_mm(oldmm);
  345. uprobe_dup_mmap(oldmm, mm);
  346. /*
  347. * Not linked in yet - no deadlock potential:
  348. */
  349. down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
  350. /* No ordering required: file already has been exposed. */
  351. RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
  352. mm->total_vm = oldmm->total_vm;
  353. mm->data_vm = oldmm->data_vm;
  354. mm->exec_vm = oldmm->exec_vm;
  355. mm->stack_vm = oldmm->stack_vm;
  356. rb_link = &mm->mm_rb.rb_node;
  357. rb_parent = NULL;
  358. pprev = &mm->mmap;
  359. retval = ksm_fork(mm, oldmm);
  360. if (retval)
  361. goto out;
  362. retval = khugepaged_fork(mm, oldmm);
  363. if (retval)
  364. goto out;
  365. prev = NULL;
  366. for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
  367. struct file *file;
  368. if (mpnt->vm_flags & VM_DONTCOPY) {
  369. vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt));
  370. continue;
  371. }
  372. charge = 0;
  373. /*
  374. * Don't duplicate many vmas if we've been oom-killed (for
  375. * example)
  376. */
  377. if (fatal_signal_pending(current)) {
  378. retval = -EINTR;
  379. goto out;
  380. }
  381. if (mpnt->vm_flags & VM_ACCOUNT) {
  382. unsigned long len = vma_pages(mpnt);
  383. if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
  384. goto fail_nomem;
  385. charge = len;
  386. }
  387. tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  388. if (!tmp)
  389. goto fail_nomem;
  390. *tmp = *mpnt;
  391. INIT_LIST_HEAD(&tmp->anon_vma_chain);
  392. retval = vma_dup_policy(mpnt, tmp);
  393. if (retval)
  394. goto fail_nomem_policy;
  395. tmp->vm_mm = mm;
  396. retval = dup_userfaultfd(tmp, &uf);
  397. if (retval)
  398. goto fail_nomem_anon_vma_fork;
  399. if (tmp->vm_flags & VM_WIPEONFORK) {
  400. /* VM_WIPEONFORK gets a clean slate in the child. */
  401. tmp->anon_vma = NULL;
  402. if (anon_vma_prepare(tmp))
  403. goto fail_nomem_anon_vma_fork;
  404. } else if (anon_vma_fork(tmp, mpnt))
  405. goto fail_nomem_anon_vma_fork;
  406. tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT);
  407. tmp->vm_next = tmp->vm_prev = NULL;
  408. file = tmp->vm_file;
  409. if (file) {
  410. struct inode *inode = file_inode(file);
  411. struct address_space *mapping = file->f_mapping;
  412. get_file(file);
  413. if (tmp->vm_flags & VM_DENYWRITE)
  414. atomic_dec(&inode->i_writecount);
  415. i_mmap_lock_write(mapping);
  416. if (tmp->vm_flags & VM_SHARED)
  417. atomic_inc(&mapping->i_mmap_writable);
  418. flush_dcache_mmap_lock(mapping);
  419. /* insert tmp into the share list, just after mpnt */
  420. vma_interval_tree_insert_after(tmp, mpnt,
  421. &mapping->i_mmap);
  422. flush_dcache_mmap_unlock(mapping);
  423. i_mmap_unlock_write(mapping);
  424. }
  425. /*
  426. * Clear hugetlb-related page reserves for children. This only
  427. * affects MAP_PRIVATE mappings. Faults generated by the child
  428. * are not guaranteed to succeed, even if read-only
  429. */
  430. if (is_vm_hugetlb_page(tmp))
  431. reset_vma_resv_huge_pages(tmp);
  432. /*
  433. * Link in the new vma and copy the page table entries.
  434. */
  435. *pprev = tmp;
  436. pprev = &tmp->vm_next;
  437. tmp->vm_prev = prev;
  438. prev = tmp;
  439. __vma_link_rb(mm, tmp, rb_link, rb_parent);
  440. rb_link = &tmp->vm_rb.rb_right;
  441. rb_parent = &tmp->vm_rb;
  442. mm->map_count++;
  443. if (!(tmp->vm_flags & VM_WIPEONFORK))
  444. retval = copy_page_range(mm, oldmm, mpnt);
  445. if (tmp->vm_ops && tmp->vm_ops->open)
  446. tmp->vm_ops->open(tmp);
  447. if (retval)
  448. goto out;
  449. }
  450. /* a new mm has just been created */
  451. arch_dup_mmap(oldmm, mm);
  452. retval = 0;
  453. out:
  454. up_write(&mm->mmap_sem);
  455. flush_tlb_mm(oldmm);
  456. up_write(&oldmm->mmap_sem);
  457. dup_userfaultfd_complete(&uf);
  458. fail_uprobe_end:
  459. uprobe_end_dup_mmap();
  460. return retval;
  461. fail_nomem_anon_vma_fork:
  462. mpol_put(vma_policy(tmp));
  463. fail_nomem_policy:
  464. kmem_cache_free(vm_area_cachep, tmp);
  465. fail_nomem:
  466. retval = -ENOMEM;
  467. vm_unacct_memory(charge);
  468. goto out;
  469. }
  470. static inline int mm_alloc_pgd(struct mm_struct *mm)
  471. {
  472. mm->pgd = pgd_alloc(mm);
  473. if (unlikely(!mm->pgd))
  474. return -ENOMEM;
  475. return 0;
  476. }
  477. static inline void mm_free_pgd(struct mm_struct *mm)
  478. {
  479. pgd_free(mm, mm->pgd);
  480. }
  481. #else
  482. static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
  483. {
  484. down_write(&oldmm->mmap_sem);
  485. RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm));
  486. up_write(&oldmm->mmap_sem);
  487. return 0;
  488. }
  489. #define mm_alloc_pgd(mm) (0)
  490. #define mm_free_pgd(mm)
  491. #endif /* CONFIG_MMU */
  492. static void check_mm(struct mm_struct *mm)
  493. {
  494. int i;
  495. for (i = 0; i < NR_MM_COUNTERS; i++) {
  496. long x = atomic_long_read(&mm->rss_stat.count[i]);
  497. if (unlikely(x))
  498. printk(KERN_ALERT "BUG: Bad rss-counter state "
  499. "mm:%p idx:%d val:%ld\n", mm, i, x);
  500. }
  501. if (mm_pgtables_bytes(mm))
  502. pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n",
  503. mm_pgtables_bytes(mm));
  504. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
  505. VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
  506. #endif
  507. }
  508. #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
  509. #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
  510. /*
  511. * Called when the last reference to the mm
  512. * is dropped: either by a lazy thread or by
  513. * mmput. Free the page directory and the mm.
  514. */
  515. void __mmdrop(struct mm_struct *mm)
  516. {
  517. BUG_ON(mm == &init_mm);
  518. WARN_ON_ONCE(mm == current->mm);
  519. WARN_ON_ONCE(mm == current->active_mm);
  520. mm_free_pgd(mm);
  521. destroy_context(mm);
  522. hmm_mm_destroy(mm);
  523. mmu_notifier_mm_destroy(mm);
  524. check_mm(mm);
  525. put_user_ns(mm->user_ns);
  526. free_mm(mm);
  527. }
  528. EXPORT_SYMBOL_GPL(__mmdrop);
  529. static void mmdrop_async_fn(struct work_struct *work)
  530. {
  531. struct mm_struct *mm;
  532. mm = container_of(work, struct mm_struct, async_put_work);
  533. __mmdrop(mm);
  534. }
  535. static void mmdrop_async(struct mm_struct *mm)
  536. {
  537. if (unlikely(atomic_dec_and_test(&mm->mm_count))) {
  538. INIT_WORK(&mm->async_put_work, mmdrop_async_fn);
  539. schedule_work(&mm->async_put_work);
  540. }
  541. }
  542. static inline void free_signal_struct(struct signal_struct *sig)
  543. {
  544. taskstats_tgid_free(sig);
  545. sched_autogroup_exit(sig);
  546. /*
  547. * __mmdrop is not safe to call from softirq context on x86 due to
  548. * pgd_dtor so postpone it to the async context
  549. */
  550. if (sig->oom_mm)
  551. mmdrop_async(sig->oom_mm);
  552. kmem_cache_free(signal_cachep, sig);
  553. }
  554. static inline void put_signal_struct(struct signal_struct *sig)
  555. {
  556. if (atomic_dec_and_test(&sig->sigcnt))
  557. free_signal_struct(sig);
  558. }
  559. void __put_task_struct(struct task_struct *tsk)
  560. {
  561. WARN_ON(!tsk->exit_state);
  562. WARN_ON(atomic_read(&tsk->usage));
  563. WARN_ON(tsk == current);
  564. cgroup_free(tsk);
  565. task_numa_free(tsk);
  566. security_task_free(tsk);
  567. exit_creds(tsk);
  568. delayacct_tsk_free(tsk);
  569. put_signal_struct(tsk->signal);
  570. if (!profile_handoff_task(tsk))
  571. free_task(tsk);
  572. }
  573. EXPORT_SYMBOL_GPL(__put_task_struct);
  574. void __init __weak arch_task_cache_init(void) { }
  575. /*
  576. * set_max_threads
  577. */
  578. static void set_max_threads(unsigned int max_threads_suggested)
  579. {
  580. u64 threads;
  581. /*
  582. * The number of threads shall be limited such that the thread
  583. * structures may only consume a small part of the available memory.
  584. */
  585. if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64)
  586. threads = MAX_THREADS;
  587. else
  588. threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE,
  589. (u64) THREAD_SIZE * 8UL);
  590. if (threads > max_threads_suggested)
  591. threads = max_threads_suggested;
  592. max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS);
  593. }
  594. #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
  595. /* Initialized by the architecture: */
  596. int arch_task_struct_size __read_mostly;
  597. #endif
  598. static void task_struct_whitelist(unsigned long *offset, unsigned long *size)
  599. {
  600. /* Fetch thread_struct whitelist for the architecture. */
  601. arch_thread_struct_whitelist(offset, size);
  602. /*
  603. * Handle zero-sized whitelist or empty thread_struct, otherwise
  604. * adjust offset to position of thread_struct in task_struct.
  605. */
  606. if (unlikely(*size == 0))
  607. *offset = 0;
  608. else
  609. *offset += offsetof(struct task_struct, thread);
  610. }
  611. void __init fork_init(void)
  612. {
  613. int i;
  614. #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
  615. #ifndef ARCH_MIN_TASKALIGN
  616. #define ARCH_MIN_TASKALIGN 0
  617. #endif
  618. int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN);
  619. unsigned long useroffset, usersize;
  620. /* create a slab on which task_structs can be allocated */
  621. task_struct_whitelist(&useroffset, &usersize);
  622. task_struct_cachep = kmem_cache_create_usercopy("task_struct",
  623. arch_task_struct_size, align,
  624. SLAB_PANIC|SLAB_ACCOUNT,
  625. useroffset, usersize, NULL);
  626. #endif
  627. /* do the arch specific task caches init */
  628. arch_task_cache_init();
  629. set_max_threads(MAX_THREADS);
  630. init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
  631. init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
  632. init_task.signal->rlim[RLIMIT_SIGPENDING] =
  633. init_task.signal->rlim[RLIMIT_NPROC];
  634. for (i = 0; i < UCOUNT_COUNTS; i++) {
  635. init_user_ns.ucount_max[i] = max_threads/2;
  636. }
  637. #ifdef CONFIG_VMAP_STACK
  638. cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache",
  639. NULL, free_vm_stack_cache);
  640. #endif
  641. lockdep_init_task(&init_task);
  642. }
  643. int __weak arch_dup_task_struct(struct task_struct *dst,
  644. struct task_struct *src)
  645. {
  646. *dst = *src;
  647. return 0;
  648. }
  649. void set_task_stack_end_magic(struct task_struct *tsk)
  650. {
  651. unsigned long *stackend;
  652. stackend = end_of_stack(tsk);
  653. *stackend = STACK_END_MAGIC; /* for overflow detection */
  654. }
  655. static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
  656. {
  657. struct task_struct *tsk;
  658. unsigned long *stack;
  659. struct vm_struct *stack_vm_area;
  660. int err;
  661. if (node == NUMA_NO_NODE)
  662. node = tsk_fork_get_node(orig);
  663. tsk = alloc_task_struct_node(node);
  664. if (!tsk)
  665. return NULL;
  666. stack = alloc_thread_stack_node(tsk, node);
  667. if (!stack)
  668. goto free_tsk;
  669. stack_vm_area = task_stack_vm_area(tsk);
  670. err = arch_dup_task_struct(tsk, orig);
  671. /*
  672. * arch_dup_task_struct() clobbers the stack-related fields. Make
  673. * sure they're properly initialized before using any stack-related
  674. * functions again.
  675. */
  676. tsk->stack = stack;
  677. #ifdef CONFIG_VMAP_STACK
  678. tsk->stack_vm_area = stack_vm_area;
  679. #endif
  680. #ifdef CONFIG_THREAD_INFO_IN_TASK
  681. atomic_set(&tsk->stack_refcount, 1);
  682. #endif
  683. if (err)
  684. goto free_stack;
  685. #ifdef CONFIG_SECCOMP
  686. /*
  687. * We must handle setting up seccomp filters once we're under
  688. * the sighand lock in case orig has changed between now and
  689. * then. Until then, filter must be NULL to avoid messing up
  690. * the usage counts on the error path calling free_task.
  691. */
  692. tsk->seccomp.filter = NULL;
  693. #endif
  694. setup_thread_stack(tsk, orig);
  695. clear_user_return_notifier(tsk);
  696. clear_tsk_need_resched(tsk);
  697. set_task_stack_end_magic(tsk);
  698. #ifdef CONFIG_STACKPROTECTOR
  699. tsk->stack_canary = get_random_canary();
  700. #endif
  701. /*
  702. * One for us, one for whoever does the "release_task()" (usually
  703. * parent)
  704. */
  705. atomic_set(&tsk->usage, 2);
  706. #ifdef CONFIG_BLK_DEV_IO_TRACE
  707. tsk->btrace_seq = 0;
  708. #endif
  709. tsk->splice_pipe = NULL;
  710. tsk->task_frag.page = NULL;
  711. tsk->wake_q.next = NULL;
  712. account_kernel_stack(tsk, 1);
  713. kcov_task_init(tsk);
  714. #ifdef CONFIG_FAULT_INJECTION
  715. tsk->fail_nth = 0;
  716. #endif
  717. return tsk;
  718. free_stack:
  719. free_thread_stack(tsk);
  720. free_tsk:
  721. free_task_struct(tsk);
  722. return NULL;
  723. }
  724. __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
  725. static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
  726. static int __init coredump_filter_setup(char *s)
  727. {
  728. default_dump_filter =
  729. (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
  730. MMF_DUMP_FILTER_MASK;
  731. return 1;
  732. }
  733. __setup("coredump_filter=", coredump_filter_setup);
  734. #include <linux/init_task.h>
  735. static void mm_init_aio(struct mm_struct *mm)
  736. {
  737. #ifdef CONFIG_AIO
  738. spin_lock_init(&mm->ioctx_lock);
  739. mm->ioctx_table = NULL;
  740. #endif
  741. }
  742. static void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
  743. {
  744. #ifdef CONFIG_MEMCG
  745. mm->owner = p;
  746. #endif
  747. }
  748. static void mm_init_uprobes_state(struct mm_struct *mm)
  749. {
  750. #ifdef CONFIG_UPROBES
  751. mm->uprobes_state.xol_area = NULL;
  752. #endif
  753. }
  754. static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
  755. struct user_namespace *user_ns)
  756. {
  757. mm->mmap = NULL;
  758. mm->mm_rb = RB_ROOT;
  759. mm->vmacache_seqnum = 0;
  760. atomic_set(&mm->mm_users, 1);
  761. atomic_set(&mm->mm_count, 1);
  762. init_rwsem(&mm->mmap_sem);
  763. INIT_LIST_HEAD(&mm->mmlist);
  764. mm->core_state = NULL;
  765. mm_pgtables_bytes_init(mm);
  766. mm->map_count = 0;
  767. mm->locked_vm = 0;
  768. mm->pinned_vm = 0;
  769. memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
  770. spin_lock_init(&mm->page_table_lock);
  771. spin_lock_init(&mm->arg_lock);
  772. mm_init_cpumask(mm);
  773. mm_init_aio(mm);
  774. mm_init_owner(mm, p);
  775. RCU_INIT_POINTER(mm->exe_file, NULL);
  776. mmu_notifier_mm_init(mm);
  777. hmm_mm_init(mm);
  778. init_tlb_flush_pending(mm);
  779. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
  780. mm->pmd_huge_pte = NULL;
  781. #endif
  782. mm_init_uprobes_state(mm);
  783. if (current->mm) {
  784. mm->flags = current->mm->flags & MMF_INIT_MASK;
  785. mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
  786. } else {
  787. mm->flags = default_dump_filter;
  788. mm->def_flags = 0;
  789. }
  790. if (mm_alloc_pgd(mm))
  791. goto fail_nopgd;
  792. if (init_new_context(p, mm))
  793. goto fail_nocontext;
  794. mm->user_ns = get_user_ns(user_ns);
  795. return mm;
  796. fail_nocontext:
  797. mm_free_pgd(mm);
  798. fail_nopgd:
  799. free_mm(mm);
  800. return NULL;
  801. }
  802. /*
  803. * Allocate and initialize an mm_struct.
  804. */
  805. struct mm_struct *mm_alloc(void)
  806. {
  807. struct mm_struct *mm;
  808. mm = allocate_mm();
  809. if (!mm)
  810. return NULL;
  811. memset(mm, 0, sizeof(*mm));
  812. return mm_init(mm, current, current_user_ns());
  813. }
  814. static inline void __mmput(struct mm_struct *mm)
  815. {
  816. VM_BUG_ON(atomic_read(&mm->mm_users));
  817. uprobe_clear_state(mm);
  818. exit_aio(mm);
  819. ksm_exit(mm);
  820. khugepaged_exit(mm); /* must run before exit_mmap */
  821. exit_mmap(mm);
  822. mm_put_huge_zero_page(mm);
  823. set_mm_exe_file(mm, NULL);
  824. if (!list_empty(&mm->mmlist)) {
  825. spin_lock(&mmlist_lock);
  826. list_del(&mm->mmlist);
  827. spin_unlock(&mmlist_lock);
  828. }
  829. if (mm->binfmt)
  830. module_put(mm->binfmt->module);
  831. mmdrop(mm);
  832. }
  833. /*
  834. * Decrement the use count and release all resources for an mm.
  835. */
  836. void mmput(struct mm_struct *mm)
  837. {
  838. might_sleep();
  839. if (atomic_dec_and_test(&mm->mm_users))
  840. __mmput(mm);
  841. }
  842. EXPORT_SYMBOL_GPL(mmput);
  843. #ifdef CONFIG_MMU
  844. static void mmput_async_fn(struct work_struct *work)
  845. {
  846. struct mm_struct *mm = container_of(work, struct mm_struct,
  847. async_put_work);
  848. __mmput(mm);
  849. }
  850. void mmput_async(struct mm_struct *mm)
  851. {
  852. if (atomic_dec_and_test(&mm->mm_users)) {
  853. INIT_WORK(&mm->async_put_work, mmput_async_fn);
  854. schedule_work(&mm->async_put_work);
  855. }
  856. }
  857. #endif
  858. /**
  859. * set_mm_exe_file - change a reference to the mm's executable file
  860. *
  861. * This changes mm's executable file (shown as symlink /proc/[pid]/exe).
  862. *
  863. * Main users are mmput() and sys_execve(). Callers prevent concurrent
  864. * invocations: in mmput() nobody alive left, in execve task is single
  865. * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the
  866. * mm->exe_file, but does so without using set_mm_exe_file() in order
  867. * to do avoid the need for any locks.
  868. */
  869. void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
  870. {
  871. struct file *old_exe_file;
  872. /*
  873. * It is safe to dereference the exe_file without RCU as
  874. * this function is only called if nobody else can access
  875. * this mm -- see comment above for justification.
  876. */
  877. old_exe_file = rcu_dereference_raw(mm->exe_file);
  878. if (new_exe_file)
  879. get_file(new_exe_file);
  880. rcu_assign_pointer(mm->exe_file, new_exe_file);
  881. if (old_exe_file)
  882. fput(old_exe_file);
  883. }
  884. /**
  885. * get_mm_exe_file - acquire a reference to the mm's executable file
  886. *
  887. * Returns %NULL if mm has no associated executable file.
  888. * User must release file via fput().
  889. */
  890. struct file *get_mm_exe_file(struct mm_struct *mm)
  891. {
  892. struct file *exe_file;
  893. rcu_read_lock();
  894. exe_file = rcu_dereference(mm->exe_file);
  895. if (exe_file && !get_file_rcu(exe_file))
  896. exe_file = NULL;
  897. rcu_read_unlock();
  898. return exe_file;
  899. }
  900. EXPORT_SYMBOL(get_mm_exe_file);
  901. /**
  902. * get_task_exe_file - acquire a reference to the task's executable file
  903. *
  904. * Returns %NULL if task's mm (if any) has no associated executable file or
  905. * this is a kernel thread with borrowed mm (see the comment above get_task_mm).
  906. * User must release file via fput().
  907. */
  908. struct file *get_task_exe_file(struct task_struct *task)
  909. {
  910. struct file *exe_file = NULL;
  911. struct mm_struct *mm;
  912. task_lock(task);
  913. mm = task->mm;
  914. if (mm) {
  915. if (!(task->flags & PF_KTHREAD))
  916. exe_file = get_mm_exe_file(mm);
  917. }
  918. task_unlock(task);
  919. return exe_file;
  920. }
  921. EXPORT_SYMBOL(get_task_exe_file);
  922. /**
  923. * get_task_mm - acquire a reference to the task's mm
  924. *
  925. * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning
  926. * this kernel workthread has transiently adopted a user mm with use_mm,
  927. * to do its AIO) is not set and if so returns a reference to it, after
  928. * bumping up the use count. User must release the mm via mmput()
  929. * after use. Typically used by /proc and ptrace.
  930. */
  931. struct mm_struct *get_task_mm(struct task_struct *task)
  932. {
  933. struct mm_struct *mm;
  934. task_lock(task);
  935. mm = task->mm;
  936. if (mm) {
  937. if (task->flags & PF_KTHREAD)
  938. mm = NULL;
  939. else
  940. mmget(mm);
  941. }
  942. task_unlock(task);
  943. return mm;
  944. }
  945. EXPORT_SYMBOL_GPL(get_task_mm);
  946. struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
  947. {
  948. struct mm_struct *mm;
  949. int err;
  950. err = mutex_lock_killable(&task->signal->cred_guard_mutex);
  951. if (err)
  952. return ERR_PTR(err);
  953. mm = get_task_mm(task);
  954. if (mm && mm != current->mm &&
  955. !ptrace_may_access(task, mode)) {
  956. mmput(mm);
  957. mm = ERR_PTR(-EACCES);
  958. }
  959. mutex_unlock(&task->signal->cred_guard_mutex);
  960. return mm;
  961. }
  962. static void complete_vfork_done(struct task_struct *tsk)
  963. {
  964. struct completion *vfork;
  965. task_lock(tsk);
  966. vfork = tsk->vfork_done;
  967. if (likely(vfork)) {
  968. tsk->vfork_done = NULL;
  969. complete(vfork);
  970. }
  971. task_unlock(tsk);
  972. }
  973. static int wait_for_vfork_done(struct task_struct *child,
  974. struct completion *vfork)
  975. {
  976. int killed;
  977. freezer_do_not_count();
  978. killed = wait_for_completion_killable(vfork);
  979. freezer_count();
  980. if (killed) {
  981. task_lock(child);
  982. child->vfork_done = NULL;
  983. task_unlock(child);
  984. }
  985. put_task_struct(child);
  986. return killed;
  987. }
  988. /* Please note the differences between mmput and mm_release.
  989. * mmput is called whenever we stop holding onto a mm_struct,
  990. * error success whatever.
  991. *
  992. * mm_release is called after a mm_struct has been removed
  993. * from the current process.
  994. *
  995. * This difference is important for error handling, when we
  996. * only half set up a mm_struct for a new process and need to restore
  997. * the old one. Because we mmput the new mm_struct before
  998. * restoring the old one. . .
  999. * Eric Biederman 10 January 1998
  1000. */
  1001. void mm_release(struct task_struct *tsk, struct mm_struct *mm)
  1002. {
  1003. /* Get rid of any futexes when releasing the mm */
  1004. #ifdef CONFIG_FUTEX
  1005. if (unlikely(tsk->robust_list)) {
  1006. exit_robust_list(tsk);
  1007. tsk->robust_list = NULL;
  1008. }
  1009. #ifdef CONFIG_COMPAT
  1010. if (unlikely(tsk->compat_robust_list)) {
  1011. compat_exit_robust_list(tsk);
  1012. tsk->compat_robust_list = NULL;
  1013. }
  1014. #endif
  1015. if (unlikely(!list_empty(&tsk->pi_state_list)))
  1016. exit_pi_state_list(tsk);
  1017. #endif
  1018. uprobe_free_utask(tsk);
  1019. /* Get rid of any cached register state */
  1020. deactivate_mm(tsk, mm);
  1021. /*
  1022. * Signal userspace if we're not exiting with a core dump
  1023. * because we want to leave the value intact for debugging
  1024. * purposes.
  1025. */
  1026. if (tsk->clear_child_tid) {
  1027. if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) &&
  1028. atomic_read(&mm->mm_users) > 1) {
  1029. /*
  1030. * We don't check the error code - if userspace has
  1031. * not set up a proper pointer then tough luck.
  1032. */
  1033. put_user(0, tsk->clear_child_tid);
  1034. do_futex(tsk->clear_child_tid, FUTEX_WAKE,
  1035. 1, NULL, NULL, 0, 0);
  1036. }
  1037. tsk->clear_child_tid = NULL;
  1038. }
  1039. /*
  1040. * All done, finally we can wake up parent and return this mm to him.
  1041. * Also kthread_stop() uses this completion for synchronization.
  1042. */
  1043. if (tsk->vfork_done)
  1044. complete_vfork_done(tsk);
  1045. }
  1046. /*
  1047. * Allocate a new mm structure and copy contents from the
  1048. * mm structure of the passed in task structure.
  1049. */
  1050. static struct mm_struct *dup_mm(struct task_struct *tsk)
  1051. {
  1052. struct mm_struct *mm, *oldmm = current->mm;
  1053. int err;
  1054. mm = allocate_mm();
  1055. if (!mm)
  1056. goto fail_nomem;
  1057. memcpy(mm, oldmm, sizeof(*mm));
  1058. if (!mm_init(mm, tsk, mm->user_ns))
  1059. goto fail_nomem;
  1060. err = dup_mmap(mm, oldmm);
  1061. if (err)
  1062. goto free_pt;
  1063. mm->hiwater_rss = get_mm_rss(mm);
  1064. mm->hiwater_vm = mm->total_vm;
  1065. if (mm->binfmt && !try_module_get(mm->binfmt->module))
  1066. goto free_pt;
  1067. return mm;
  1068. free_pt:
  1069. /* don't put binfmt in mmput, we haven't got module yet */
  1070. mm->binfmt = NULL;
  1071. mmput(mm);
  1072. fail_nomem:
  1073. return NULL;
  1074. }
  1075. static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
  1076. {
  1077. struct mm_struct *mm, *oldmm;
  1078. int retval;
  1079. tsk->min_flt = tsk->maj_flt = 0;
  1080. tsk->nvcsw = tsk->nivcsw = 0;
  1081. #ifdef CONFIG_DETECT_HUNG_TASK
  1082. tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
  1083. #endif
  1084. tsk->mm = NULL;
  1085. tsk->active_mm = NULL;
  1086. /*
  1087. * Are we cloning a kernel thread?
  1088. *
  1089. * We need to steal a active VM for that..
  1090. */
  1091. oldmm = current->mm;
  1092. if (!oldmm)
  1093. return 0;
  1094. /* initialize the new vmacache entries */
  1095. vmacache_flush(tsk);
  1096. if (clone_flags & CLONE_VM) {
  1097. mmget(oldmm);
  1098. mm = oldmm;
  1099. goto good_mm;
  1100. }
  1101. retval = -ENOMEM;
  1102. mm = dup_mm(tsk);
  1103. if (!mm)
  1104. goto fail_nomem;
  1105. good_mm:
  1106. tsk->mm = mm;
  1107. tsk->active_mm = mm;
  1108. return 0;
  1109. fail_nomem:
  1110. return retval;
  1111. }
  1112. static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
  1113. {
  1114. struct fs_struct *fs = current->fs;
  1115. if (clone_flags & CLONE_FS) {
  1116. /* tsk->fs is already what we want */
  1117. spin_lock(&fs->lock);
  1118. if (fs->in_exec) {
  1119. spin_unlock(&fs->lock);
  1120. return -EAGAIN;
  1121. }
  1122. fs->users++;
  1123. spin_unlock(&fs->lock);
  1124. return 0;
  1125. }
  1126. tsk->fs = copy_fs_struct(fs);
  1127. if (!tsk->fs)
  1128. return -ENOMEM;
  1129. return 0;
  1130. }
  1131. static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
  1132. {
  1133. struct files_struct *oldf, *newf;
  1134. int error = 0;
  1135. /*
  1136. * A background process may not have any files ...
  1137. */
  1138. oldf = current->files;
  1139. if (!oldf)
  1140. goto out;
  1141. if (clone_flags & CLONE_FILES) {
  1142. atomic_inc(&oldf->count);
  1143. goto out;
  1144. }
  1145. newf = dup_fd(oldf, &error);
  1146. if (!newf)
  1147. goto out;
  1148. tsk->files = newf;
  1149. error = 0;
  1150. out:
  1151. return error;
  1152. }
  1153. static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
  1154. {
  1155. #ifdef CONFIG_BLOCK
  1156. struct io_context *ioc = current->io_context;
  1157. struct io_context *new_ioc;
  1158. if (!ioc)
  1159. return 0;
  1160. /*
  1161. * Share io context with parent, if CLONE_IO is set
  1162. */
  1163. if (clone_flags & CLONE_IO) {
  1164. ioc_task_link(ioc);
  1165. tsk->io_context = ioc;
  1166. } else if (ioprio_valid(ioc->ioprio)) {
  1167. new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
  1168. if (unlikely(!new_ioc))
  1169. return -ENOMEM;
  1170. new_ioc->ioprio = ioc->ioprio;
  1171. put_io_context(new_ioc);
  1172. }
  1173. #endif
  1174. return 0;
  1175. }
  1176. static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
  1177. {
  1178. struct sighand_struct *sig;
  1179. if (clone_flags & CLONE_SIGHAND) {
  1180. atomic_inc(&current->sighand->count);
  1181. return 0;
  1182. }
  1183. sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  1184. rcu_assign_pointer(tsk->sighand, sig);
  1185. if (!sig)
  1186. return -ENOMEM;
  1187. atomic_set(&sig->count, 1);
  1188. memcpy(sig->action, current->sighand->action, sizeof(sig->action));
  1189. return 0;
  1190. }
  1191. void __cleanup_sighand(struct sighand_struct *sighand)
  1192. {
  1193. if (atomic_dec_and_test(&sighand->count)) {
  1194. signalfd_cleanup(sighand);
  1195. /*
  1196. * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it
  1197. * without an RCU grace period, see __lock_task_sighand().
  1198. */
  1199. kmem_cache_free(sighand_cachep, sighand);
  1200. }
  1201. }
  1202. #ifdef CONFIG_POSIX_TIMERS
  1203. /*
  1204. * Initialize POSIX timer handling for a thread group.
  1205. */
  1206. static void posix_cpu_timers_init_group(struct signal_struct *sig)
  1207. {
  1208. unsigned long cpu_limit;
  1209. cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
  1210. if (cpu_limit != RLIM_INFINITY) {
  1211. sig->cputime_expires.prof_exp = cpu_limit * NSEC_PER_SEC;
  1212. sig->cputimer.running = true;
  1213. }
  1214. /* The timer lists. */
  1215. INIT_LIST_HEAD(&sig->cpu_timers[0]);
  1216. INIT_LIST_HEAD(&sig->cpu_timers[1]);
  1217. INIT_LIST_HEAD(&sig->cpu_timers[2]);
  1218. }
  1219. #else
  1220. static inline void posix_cpu_timers_init_group(struct signal_struct *sig) { }
  1221. #endif
  1222. static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
  1223. {
  1224. struct signal_struct *sig;
  1225. if (clone_flags & CLONE_THREAD)
  1226. return 0;
  1227. sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
  1228. tsk->signal = sig;
  1229. if (!sig)
  1230. return -ENOMEM;
  1231. sig->nr_threads = 1;
  1232. atomic_set(&sig->live, 1);
  1233. atomic_set(&sig->sigcnt, 1);
  1234. /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
  1235. sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node);
  1236. tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head);
  1237. init_waitqueue_head(&sig->wait_chldexit);
  1238. sig->curr_target = tsk;
  1239. init_sigpending(&sig->shared_pending);
  1240. seqlock_init(&sig->stats_lock);
  1241. prev_cputime_init(&sig->prev_cputime);
  1242. #ifdef CONFIG_POSIX_TIMERS
  1243. INIT_LIST_HEAD(&sig->posix_timers);
  1244. hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1245. sig->real_timer.function = it_real_fn;
  1246. #endif
  1247. task_lock(current->group_leader);
  1248. memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
  1249. task_unlock(current->group_leader);
  1250. posix_cpu_timers_init_group(sig);
  1251. tty_audit_fork(sig);
  1252. sched_autogroup_fork(sig);
  1253. sig->oom_score_adj = current->signal->oom_score_adj;
  1254. sig->oom_score_adj_min = current->signal->oom_score_adj_min;
  1255. mutex_init(&sig->cred_guard_mutex);
  1256. return 0;
  1257. }
  1258. static void copy_seccomp(struct task_struct *p)
  1259. {
  1260. #ifdef CONFIG_SECCOMP
  1261. /*
  1262. * Must be called with sighand->lock held, which is common to
  1263. * all threads in the group. Holding cred_guard_mutex is not
  1264. * needed because this new task is not yet running and cannot
  1265. * be racing exec.
  1266. */
  1267. assert_spin_locked(&current->sighand->siglock);
  1268. /* Ref-count the new filter user, and assign it. */
  1269. get_seccomp_filter(current);
  1270. p->seccomp = current->seccomp;
  1271. /*
  1272. * Explicitly enable no_new_privs here in case it got set
  1273. * between the task_struct being duplicated and holding the
  1274. * sighand lock. The seccomp state and nnp must be in sync.
  1275. */
  1276. if (task_no_new_privs(current))
  1277. task_set_no_new_privs(p);
  1278. /*
  1279. * If the parent gained a seccomp mode after copying thread
  1280. * flags and between before we held the sighand lock, we have
  1281. * to manually enable the seccomp thread flag here.
  1282. */
  1283. if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
  1284. set_tsk_thread_flag(p, TIF_SECCOMP);
  1285. #endif
  1286. }
  1287. SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
  1288. {
  1289. current->clear_child_tid = tidptr;
  1290. return task_pid_vnr(current);
  1291. }
  1292. static void rt_mutex_init_task(struct task_struct *p)
  1293. {
  1294. raw_spin_lock_init(&p->pi_lock);
  1295. #ifdef CONFIG_RT_MUTEXES
  1296. p->pi_waiters = RB_ROOT_CACHED;
  1297. p->pi_top_task = NULL;
  1298. p->pi_blocked_on = NULL;
  1299. #endif
  1300. }
  1301. #ifdef CONFIG_POSIX_TIMERS
  1302. /*
  1303. * Initialize POSIX timer handling for a single task.
  1304. */
  1305. static void posix_cpu_timers_init(struct task_struct *tsk)
  1306. {
  1307. tsk->cputime_expires.prof_exp = 0;
  1308. tsk->cputime_expires.virt_exp = 0;
  1309. tsk->cputime_expires.sched_exp = 0;
  1310. INIT_LIST_HEAD(&tsk->cpu_timers[0]);
  1311. INIT_LIST_HEAD(&tsk->cpu_timers[1]);
  1312. INIT_LIST_HEAD(&tsk->cpu_timers[2]);
  1313. }
  1314. #else
  1315. static inline void posix_cpu_timers_init(struct task_struct *tsk) { }
  1316. #endif
  1317. static inline void
  1318. init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
  1319. {
  1320. task->pids[type].pid = pid;
  1321. }
  1322. static inline void rcu_copy_process(struct task_struct *p)
  1323. {
  1324. #ifdef CONFIG_PREEMPT_RCU
  1325. p->rcu_read_lock_nesting = 0;
  1326. p->rcu_read_unlock_special.s = 0;
  1327. p->rcu_blocked_node = NULL;
  1328. INIT_LIST_HEAD(&p->rcu_node_entry);
  1329. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  1330. #ifdef CONFIG_TASKS_RCU
  1331. p->rcu_tasks_holdout = false;
  1332. INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
  1333. p->rcu_tasks_idle_cpu = -1;
  1334. #endif /* #ifdef CONFIG_TASKS_RCU */
  1335. }
  1336. /*
  1337. * This creates a new process as a copy of the old one,
  1338. * but does not actually start it yet.
  1339. *
  1340. * It copies the registers, and all the appropriate
  1341. * parts of the process environment (as per the clone
  1342. * flags). The actual kick-off is left to the caller.
  1343. */
  1344. static __latent_entropy struct task_struct *copy_process(
  1345. unsigned long clone_flags,
  1346. unsigned long stack_start,
  1347. unsigned long stack_size,
  1348. int __user *child_tidptr,
  1349. struct pid *pid,
  1350. int trace,
  1351. unsigned long tls,
  1352. int node)
  1353. {
  1354. int retval;
  1355. struct task_struct *p;
  1356. /*
  1357. * Don't allow sharing the root directory with processes in a different
  1358. * namespace
  1359. */
  1360. if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
  1361. return ERR_PTR(-EINVAL);
  1362. if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
  1363. return ERR_PTR(-EINVAL);
  1364. /*
  1365. * Thread groups must share signals as well, and detached threads
  1366. * can only be started up within the thread group.
  1367. */
  1368. if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
  1369. return ERR_PTR(-EINVAL);
  1370. /*
  1371. * Shared signal handlers imply shared VM. By way of the above,
  1372. * thread groups also imply shared VM. Blocking this case allows
  1373. * for various simplifications in other code.
  1374. */
  1375. if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
  1376. return ERR_PTR(-EINVAL);
  1377. /*
  1378. * Siblings of global init remain as zombies on exit since they are
  1379. * not reaped by their parent (swapper). To solve this and to avoid
  1380. * multi-rooted process trees, prevent global and container-inits
  1381. * from creating siblings.
  1382. */
  1383. if ((clone_flags & CLONE_PARENT) &&
  1384. current->signal->flags & SIGNAL_UNKILLABLE)
  1385. return ERR_PTR(-EINVAL);
  1386. /*
  1387. * If the new process will be in a different pid or user namespace
  1388. * do not allow it to share a thread group with the forking task.
  1389. */
  1390. if (clone_flags & CLONE_THREAD) {
  1391. if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
  1392. (task_active_pid_ns(current) !=
  1393. current->nsproxy->pid_ns_for_children))
  1394. return ERR_PTR(-EINVAL);
  1395. }
  1396. retval = -ENOMEM;
  1397. p = dup_task_struct(current, node);
  1398. if (!p)
  1399. goto fork_out;
  1400. /*
  1401. * This _must_ happen before we call free_task(), i.e. before we jump
  1402. * to any of the bad_fork_* labels. This is to avoid freeing
  1403. * p->set_child_tid which is (ab)used as a kthread's data pointer for
  1404. * kernel threads (PF_KTHREAD).
  1405. */
  1406. p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
  1407. /*
  1408. * Clear TID on mm_release()?
  1409. */
  1410. p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
  1411. ftrace_graph_init_task(p);
  1412. rt_mutex_init_task(p);
  1413. #ifdef CONFIG_PROVE_LOCKING
  1414. DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
  1415. DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
  1416. #endif
  1417. retval = -EAGAIN;
  1418. if (atomic_read(&p->real_cred->user->processes) >=
  1419. task_rlimit(p, RLIMIT_NPROC)) {
  1420. if (p->real_cred->user != INIT_USER &&
  1421. !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
  1422. goto bad_fork_free;
  1423. }
  1424. current->flags &= ~PF_NPROC_EXCEEDED;
  1425. retval = copy_creds(p, clone_flags);
  1426. if (retval < 0)
  1427. goto bad_fork_free;
  1428. /*
  1429. * If multiple threads are within copy_process(), then this check
  1430. * triggers too late. This doesn't hurt, the check is only there
  1431. * to stop root fork bombs.
  1432. */
  1433. retval = -EAGAIN;
  1434. if (nr_threads >= max_threads)
  1435. goto bad_fork_cleanup_count;
  1436. delayacct_tsk_init(p); /* Must remain after dup_task_struct() */
  1437. p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE);
  1438. p->flags |= PF_FORKNOEXEC;
  1439. INIT_LIST_HEAD(&p->children);
  1440. INIT_LIST_HEAD(&p->sibling);
  1441. rcu_copy_process(p);
  1442. p->vfork_done = NULL;
  1443. spin_lock_init(&p->alloc_lock);
  1444. init_sigpending(&p->pending);
  1445. p->utime = p->stime = p->gtime = 0;
  1446. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  1447. p->utimescaled = p->stimescaled = 0;
  1448. #endif
  1449. prev_cputime_init(&p->prev_cputime);
  1450. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  1451. seqcount_init(&p->vtime.seqcount);
  1452. p->vtime.starttime = 0;
  1453. p->vtime.state = VTIME_INACTIVE;
  1454. #endif
  1455. #if defined(SPLIT_RSS_COUNTING)
  1456. memset(&p->rss_stat, 0, sizeof(p->rss_stat));
  1457. #endif
  1458. p->default_timer_slack_ns = current->timer_slack_ns;
  1459. task_io_accounting_init(&p->ioac);
  1460. acct_clear_integrals(p);
  1461. posix_cpu_timers_init(p);
  1462. p->start_time = ktime_get_ns();
  1463. p->real_start_time = ktime_get_boot_ns();
  1464. p->io_context = NULL;
  1465. audit_set_context(p, NULL);
  1466. cgroup_fork(p);
  1467. #ifdef CONFIG_NUMA
  1468. p->mempolicy = mpol_dup(p->mempolicy);
  1469. if (IS_ERR(p->mempolicy)) {
  1470. retval = PTR_ERR(p->mempolicy);
  1471. p->mempolicy = NULL;
  1472. goto bad_fork_cleanup_threadgroup_lock;
  1473. }
  1474. #endif
  1475. #ifdef CONFIG_CPUSETS
  1476. p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
  1477. p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
  1478. seqcount_init(&p->mems_allowed_seq);
  1479. #endif
  1480. #ifdef CONFIG_TRACE_IRQFLAGS
  1481. p->irq_events = 0;
  1482. p->hardirqs_enabled = 0;
  1483. p->hardirq_enable_ip = 0;
  1484. p->hardirq_enable_event = 0;
  1485. p->hardirq_disable_ip = _THIS_IP_;
  1486. p->hardirq_disable_event = 0;
  1487. p->softirqs_enabled = 1;
  1488. p->softirq_enable_ip = _THIS_IP_;
  1489. p->softirq_enable_event = 0;
  1490. p->softirq_disable_ip = 0;
  1491. p->softirq_disable_event = 0;
  1492. p->hardirq_context = 0;
  1493. p->softirq_context = 0;
  1494. #endif
  1495. p->pagefault_disabled = 0;
  1496. #ifdef CONFIG_LOCKDEP
  1497. p->lockdep_depth = 0; /* no locks held yet */
  1498. p->curr_chain_key = 0;
  1499. p->lockdep_recursion = 0;
  1500. lockdep_init_task(p);
  1501. #endif
  1502. #ifdef CONFIG_DEBUG_MUTEXES
  1503. p->blocked_on = NULL; /* not blocked yet */
  1504. #endif
  1505. #ifdef CONFIG_BCACHE
  1506. p->sequential_io = 0;
  1507. p->sequential_io_avg = 0;
  1508. #endif
  1509. /* Perform scheduler related setup. Assign this task to a CPU. */
  1510. retval = sched_fork(clone_flags, p);
  1511. if (retval)
  1512. goto bad_fork_cleanup_policy;
  1513. retval = perf_event_init_task(p);
  1514. if (retval)
  1515. goto bad_fork_cleanup_policy;
  1516. retval = audit_alloc(p);
  1517. if (retval)
  1518. goto bad_fork_cleanup_perf;
  1519. /* copy all the process information */
  1520. shm_init_task(p);
  1521. retval = security_task_alloc(p, clone_flags);
  1522. if (retval)
  1523. goto bad_fork_cleanup_audit;
  1524. retval = copy_semundo(clone_flags, p);
  1525. if (retval)
  1526. goto bad_fork_cleanup_security;
  1527. retval = copy_files(clone_flags, p);
  1528. if (retval)
  1529. goto bad_fork_cleanup_semundo;
  1530. retval = copy_fs(clone_flags, p);
  1531. if (retval)
  1532. goto bad_fork_cleanup_files;
  1533. retval = copy_sighand(clone_flags, p);
  1534. if (retval)
  1535. goto bad_fork_cleanup_fs;
  1536. retval = copy_signal(clone_flags, p);
  1537. if (retval)
  1538. goto bad_fork_cleanup_sighand;
  1539. retval = copy_mm(clone_flags, p);
  1540. if (retval)
  1541. goto bad_fork_cleanup_signal;
  1542. retval = copy_namespaces(clone_flags, p);
  1543. if (retval)
  1544. goto bad_fork_cleanup_mm;
  1545. retval = copy_io(clone_flags, p);
  1546. if (retval)
  1547. goto bad_fork_cleanup_namespaces;
  1548. retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls);
  1549. if (retval)
  1550. goto bad_fork_cleanup_io;
  1551. if (pid != &init_struct_pid) {
  1552. pid = alloc_pid(p->nsproxy->pid_ns_for_children);
  1553. if (IS_ERR(pid)) {
  1554. retval = PTR_ERR(pid);
  1555. goto bad_fork_cleanup_thread;
  1556. }
  1557. }
  1558. #ifdef CONFIG_BLOCK
  1559. p->plug = NULL;
  1560. #endif
  1561. #ifdef CONFIG_FUTEX
  1562. p->robust_list = NULL;
  1563. #ifdef CONFIG_COMPAT
  1564. p->compat_robust_list = NULL;
  1565. #endif
  1566. INIT_LIST_HEAD(&p->pi_state_list);
  1567. p->pi_state_cache = NULL;
  1568. #endif
  1569. /*
  1570. * sigaltstack should be cleared when sharing the same VM
  1571. */
  1572. if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
  1573. sas_ss_reset(p);
  1574. /*
  1575. * Syscall tracing and stepping should be turned off in the
  1576. * child regardless of CLONE_PTRACE.
  1577. */
  1578. user_disable_single_step(p);
  1579. clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
  1580. #ifdef TIF_SYSCALL_EMU
  1581. clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
  1582. #endif
  1583. clear_all_latency_tracing(p);
  1584. /* ok, now we should be set up.. */
  1585. p->pid = pid_nr(pid);
  1586. if (clone_flags & CLONE_THREAD) {
  1587. p->exit_signal = -1;
  1588. p->group_leader = current->group_leader;
  1589. p->tgid = current->tgid;
  1590. } else {
  1591. if (clone_flags & CLONE_PARENT)
  1592. p->exit_signal = current->group_leader->exit_signal;
  1593. else
  1594. p->exit_signal = (clone_flags & CSIGNAL);
  1595. p->group_leader = p;
  1596. p->tgid = p->pid;
  1597. }
  1598. p->nr_dirtied = 0;
  1599. p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
  1600. p->dirty_paused_when = 0;
  1601. p->pdeath_signal = 0;
  1602. INIT_LIST_HEAD(&p->thread_group);
  1603. p->task_works = NULL;
  1604. cgroup_threadgroup_change_begin(current);
  1605. /*
  1606. * Ensure that the cgroup subsystem policies allow the new process to be
  1607. * forked. It should be noted the the new process's css_set can be changed
  1608. * between here and cgroup_post_fork() if an organisation operation is in
  1609. * progress.
  1610. */
  1611. retval = cgroup_can_fork(p);
  1612. if (retval)
  1613. goto bad_fork_free_pid;
  1614. /*
  1615. * Make it visible to the rest of the system, but dont wake it up yet.
  1616. * Need tasklist lock for parent etc handling!
  1617. */
  1618. write_lock_irq(&tasklist_lock);
  1619. /* CLONE_PARENT re-uses the old parent */
  1620. if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
  1621. p->real_parent = current->real_parent;
  1622. p->parent_exec_id = current->parent_exec_id;
  1623. } else {
  1624. p->real_parent = current;
  1625. p->parent_exec_id = current->self_exec_id;
  1626. }
  1627. klp_copy_process(p);
  1628. spin_lock(&current->sighand->siglock);
  1629. /*
  1630. * Copy seccomp details explicitly here, in case they were changed
  1631. * before holding sighand lock.
  1632. */
  1633. copy_seccomp(p);
  1634. rseq_fork(p, clone_flags);
  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. int ksys_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. SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
  2145. {
  2146. return ksys_unshare(unshare_flags);
  2147. }
  2148. /*
  2149. * Helper to unshare the files of the current task.
  2150. * We don't want to expose copy_files internals to
  2151. * the exec layer of the kernel.
  2152. */
  2153. int unshare_files(struct files_struct **displaced)
  2154. {
  2155. struct task_struct *task = current;
  2156. struct files_struct *copy = NULL;
  2157. int error;
  2158. error = unshare_fd(CLONE_FILES, &copy);
  2159. if (error || !copy) {
  2160. *displaced = NULL;
  2161. return error;
  2162. }
  2163. *displaced = task->files;
  2164. task_lock(task);
  2165. task->files = copy;
  2166. task_unlock(task);
  2167. return 0;
  2168. }
  2169. int sysctl_max_threads(struct ctl_table *table, int write,
  2170. void __user *buffer, size_t *lenp, loff_t *ppos)
  2171. {
  2172. struct ctl_table t;
  2173. int ret;
  2174. int threads = max_threads;
  2175. int min = MIN_THREADS;
  2176. int max = MAX_THREADS;
  2177. t = *table;
  2178. t.data = &threads;
  2179. t.extra1 = &min;
  2180. t.extra2 = &max;
  2181. ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
  2182. if (ret || !write)
  2183. return ret;
  2184. set_max_threads(threads);
  2185. return 0;
  2186. }