fork.c 63 KB

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