PageRenderTime 73ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/kernel/fork.c

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