fork.c 56 KB

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