PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/fork.c

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