fork.c 48 KB

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