fork.c 60 KB

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