fork.c 52 KB

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