fork.c 49 KB

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