fork.c 51 KB

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