fork.c 51 KB

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