PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/fork.c

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