fork.c 57 KB

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