fork.c 56 KB

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