fork.c 63 KB

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