fork.c 56 KB

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