Advertisement
Guest User

Untitled

a guest
May 12th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 43.86 KB | None | 0 0
  1. /*
  2. * linux/kernel/fork.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6.  
  7. /*
  8. * 'fork.c' contains the help-routines for the 'fork' system call
  9. * (see also entry.S and others).
  10. * Fork is rather simple, once you get the hang of it, but the memory
  11. * management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
  12. */
  13.  
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/unistd.h>
  17. #include <linux/module.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/completion.h>
  20. #include <linux/mnt_namespace.h>
  21. #include <linux/personality.h>
  22. #include <linux/mempolicy.h>
  23. #include <linux/sem.h>
  24. #include <linux/file.h>
  25. #include <linux/key.h>
  26. #include <linux/binfmts.h>
  27. #include <linux/mman.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/swap.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/futex.h>
  38. #include <linux/task_io_accounting_ops.h>
  39. #include <linux/rcupdate.h>
  40. #include <linux/ptrace.h>
  41. #include <linux/mount.h>
  42. #include <linux/audit.h>
  43. #include <linux/profile.h>
  44. #include <linux/rmap.h>
  45. #include <linux/acct.h>
  46. #include <linux/tsacct_kern.h>
  47. #include <linux/cn_proc.h>
  48. #include <linux/freezer.h>
  49. #include <linux/delayacct.h>
  50. #include <linux/taskstats_kern.h>
  51. #include <linux/random.h>
  52. #include <linux/tty.h>
  53. #include <linux/proc_fs.h>
  54.  
  55. #include <asm/pgtable.h>
  56. #include <asm/pgalloc.h>
  57. #include <asm/uaccess.h>
  58. #include <asm/mmu_context.h>
  59. #include <asm/cacheflush.h>
  60. #include <asm/tlbflush.h>
  61.  
  62. /*
  63. * Protected counters by write_lock_irq(&tasklist_lock)
  64. */
  65. unsigned long total_forks; /* Handle normal Linux uptimes. */
  66. int nr_threads; /* The idle threads do not count.. */
  67.  
  68. int max_threads; /* tunable limit on nr_threads */
  69.  
  70. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  71.  
  72. __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  73.  
  74. int nr_processes(void)
  75. {
  76. int cpu;
  77. int total = 0;
  78.  
  79. for_each_online_cpu(cpu)
  80. total += per_cpu(process_counts, cpu);
  81.  
  82. return total;
  83. }
  84.  
  85. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  86. # define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
  87. # define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk))
  88. static struct kmem_cache *task_struct_cachep;
  89. #endif
  90.  
  91. /* SLAB cache for signal_struct structures (tsk->signal) */
  92. static struct kmem_cache *signal_cachep;
  93.  
  94. /* SLAB cache for sighand_struct structures (tsk->sighand) */
  95. struct kmem_cache *sighand_cachep;
  96.  
  97. /* SLAB cache for files_struct structures (tsk->files) */
  98. struct kmem_cache *files_cachep;
  99.  
  100. /* SLAB cache for fs_struct structures (tsk->fs) */
  101. struct kmem_cache *fs_cachep;
  102.  
  103. /* SLAB cache for vm_area_struct structures */
  104. struct kmem_cache *vm_area_cachep;
  105.  
  106. /* SLAB cache for mm_struct structures (tsk->mm) */
  107. static struct kmem_cache *mm_cachep;
  108.  
  109. void free_task(struct task_struct *tsk)
  110. {
  111. prop_local_destroy_single(&tsk->dirties);
  112. free_thread_info(tsk->stack);
  113. rt_mutex_debug_task_free(tsk);
  114. free_task_struct(tsk);
  115. }
  116. EXPORT_SYMBOL(free_task);
  117.  
  118. void __put_task_struct(struct task_struct *tsk)
  119. {
  120. WARN_ON(!tsk->exit_state);
  121. WARN_ON(atomic_read(&tsk->usage));
  122. WARN_ON(tsk == current);
  123.  
  124. security_task_free(tsk);
  125. free_uid(tsk->user);
  126. put_group_info(tsk->group_info);
  127. delayacct_tsk_free(tsk);
  128.  
  129. if (!profile_handoff_task(tsk))
  130. free_task(tsk);
  131. }
  132.  
  133. void __init fork_init(unsigned long mempages)
  134. {
  135. #ifndef __HAVE_ARCH_TASK_STRUCT_ALLOCATOR
  136. #ifndef ARCH_MIN_TASKALIGN
  137. #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
  138. #endif
  139. /* create a slab on which task_structs can be allocated */
  140. task_struct_cachep =
  141. kmem_cache_create("task_struct", sizeof(struct task_struct),
  142. ARCH_MIN_TASKALIGN, SLAB_PANIC, NULL);
  143. #endif
  144.  
  145. /*
  146. * The default maximum number of threads is set to a safe
  147. * value: the thread structures can take up at most half
  148. * of memory.
  149. */
  150. max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
  151.  
  152. /*
  153. * we need to allow at least 20 threads to boot a system
  154. */
  155. if(max_threads < 20)
  156. max_threads = 20;
  157.  
  158. init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
  159. init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
  160. init_task.signal->rlim[RLIMIT_SIGPENDING] =
  161. init_task.signal->rlim[RLIMIT_NPROC];
  162. }
  163.  
  164. static double time = 0;
  165.  
  166. static struct task_struct *dup_task_struct(struct task_struct *orig)
  167. {
  168. struct task_struct *tsk;
  169. struct thread_info *ti;
  170. static struct timeval tstart;
  171. static struct timeval tstop;
  172. int err;
  173.  
  174. prepare_to_copy(orig);
  175.  
  176. gettimeofday(&tstart, NULL);
  177. tsk = alloc_task_struct();
  178. gettimeofday(&tstop, NULL);
  179. time += tstop.tv_sec - tstart.tv_sec + 1e-6*(tstop.tv_usec - tstart.tv_usec);
  180.  
  181. if (!tsk)
  182. return NULL;
  183.  
  184.  
  185. ti = alloc_thread_info(tsk);
  186. if (!ti) {
  187. free_task_struct(tsk);
  188. return NULL;
  189. }
  190.  
  191. *tsk = *orig;
  192. tsk->stack = ti;
  193.  
  194. err = prop_local_init_single(&tsk->dirties);
  195. if (err) {
  196. free_thread_info(ti);
  197. free_task_struct(tsk);
  198. return NULL;
  199. }
  200.  
  201. setup_thread_stack(tsk, orig);
  202.  
  203. #ifdef CONFIG_CC_STACKPROTECTOR
  204. tsk->stack_canary = get_random_int();
  205. #endif
  206.  
  207. /* One for us, one for whoever does the "release_task()" (usually parent) */
  208. atomic_set(&tsk->usage,2);
  209. atomic_set(&tsk->fs_excl, 0);
  210. #ifdef CONFIG_BLK_DEV_IO_TRACE
  211. tsk->btrace_seq = 0;
  212. #endif
  213. tsk->splice_pipe = NULL;
  214. return tsk;
  215. }
  216.  
  217. #ifdef CONFIG_MMU
  218. static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
  219. {
  220. struct vm_area_struct *mpnt, *tmp, **pprev;
  221. struct rb_node **rb_link, *rb_parent;
  222. int retval;
  223. unsigned long charge;
  224. struct mempolicy *pol;
  225.  
  226. down_write(&oldmm->mmap_sem);
  227. flush_cache_dup_mm(oldmm);
  228. /*
  229. * Not linked in yet - no deadlock potential:
  230. */
  231. down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
  232.  
  233. mm->locked_vm = 0;
  234. mm->mmap = NULL;
  235. mm->mmap_cache = NULL;
  236. mm->free_area_cache = oldmm->mmap_base;
  237. mm->cached_hole_size = ~0UL;
  238. mm->map_count = 0;
  239. cpus_clear(mm->cpu_vm_mask);
  240. mm->mm_rb = RB_ROOT;
  241. rb_link = &mm->mm_rb.rb_node;
  242. rb_parent = NULL;
  243. pprev = &mm->mmap;
  244.  
  245. for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
  246. struct file *file;
  247.  
  248. if (mpnt->vm_flags & VM_DONTCOPY) {
  249. long pages = vma_pages(mpnt);
  250. mm->total_vm -= pages;
  251. vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
  252. -pages);
  253. continue;
  254. }
  255. charge = 0;
  256. if (mpnt->vm_flags & VM_ACCOUNT) {
  257. unsigned int len = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
  258. if (security_vm_enough_memory(len))
  259. goto fail_nomem;
  260. charge = len;
  261. }
  262. tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  263. if (!tmp)
  264. goto fail_nomem;
  265. *tmp = *mpnt;
  266. pol = mpol_copy(vma_policy(mpnt));
  267. retval = PTR_ERR(pol);
  268. if (IS_ERR(pol))
  269. goto fail_nomem_policy;
  270. vma_set_policy(tmp, pol);
  271. tmp->vm_flags &= ~VM_LOCKED;
  272. tmp->vm_mm = mm;
  273. tmp->vm_next = NULL;
  274. anon_vma_link(tmp);
  275. file = tmp->vm_file;
  276. if (file) {
  277. struct inode *inode = file->f_path.dentry->d_inode;
  278. get_file(file);
  279. if (tmp->vm_flags & VM_DENYWRITE)
  280. atomic_dec(&inode->i_writecount);
  281.  
  282. /* insert tmp into the share list, just after mpnt */
  283. spin_lock(&file->f_mapping->i_mmap_lock);
  284. tmp->vm_truncate_count = mpnt->vm_truncate_count;
  285. flush_dcache_mmap_lock(file->f_mapping);
  286. vma_prio_tree_add(tmp, mpnt);
  287. flush_dcache_mmap_unlock(file->f_mapping);
  288. spin_unlock(&file->f_mapping->i_mmap_lock);
  289. }
  290.  
  291. /*
  292. * Link in the new vma and copy the page table entries.
  293. */
  294. *pprev = tmp;
  295. pprev = &tmp->vm_next;
  296.  
  297. __vma_link_rb(mm, tmp, rb_link, rb_parent);
  298. rb_link = &tmp->vm_rb.rb_right;
  299. rb_parent = &tmp->vm_rb;
  300.  
  301. mm->map_count++;
  302. retval = copy_page_range(mm, oldmm, mpnt);
  303.  
  304. if (tmp->vm_ops && tmp->vm_ops->open)
  305. tmp->vm_ops->open(tmp);
  306.  
  307. if (retval)
  308. goto out;
  309. }
  310. /* a new mm has just been created */
  311. arch_dup_mmap(oldmm, mm);
  312. retval = 0;
  313. out:
  314. up_write(&mm->mmap_sem);
  315. flush_tlb_mm(oldmm);
  316. up_write(&oldmm->mmap_sem);
  317. return retval;
  318. fail_nomem_policy:
  319. kmem_cache_free(vm_area_cachep, tmp);
  320. fail_nomem:
  321. retval = -ENOMEM;
  322. vm_unacct_memory(charge);
  323. goto out;
  324. }
  325.  
  326. static inline int mm_alloc_pgd(struct mm_struct * mm)
  327. {
  328. mm->pgd = pgd_alloc(mm);
  329. if (unlikely(!mm->pgd))
  330. return -ENOMEM;
  331. return 0;
  332. }
  333.  
  334. static inline void mm_free_pgd(struct mm_struct * mm)
  335. {
  336. pgd_free(mm->pgd);
  337. }
  338. #else
  339. #define dup_mmap(mm, oldmm) (0)
  340. #define mm_alloc_pgd(mm) (0)
  341. #define mm_free_pgd(mm)
  342. #endif /* CONFIG_MMU */
  343.  
  344. __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
  345.  
  346. #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
  347. #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
  348.  
  349. #include <linux/init_task.h>
  350.  
  351. static struct mm_struct * mm_init(struct mm_struct * mm)
  352. {
  353. atomic_set(&mm->mm_users, 1);
  354. atomic_set(&mm->mm_count, 1);
  355. init_rwsem(&mm->mmap_sem);
  356. INIT_LIST_HEAD(&mm->mmlist);
  357. mm->flags = (current->mm) ? current->mm->flags
  358. : MMF_DUMP_FILTER_DEFAULT;
  359. mm->core_waiters = 0;
  360. mm->nr_ptes = 0;
  361. set_mm_counter(mm, file_rss, 0);
  362. set_mm_counter(mm, anon_rss, 0);
  363. spin_lock_init(&mm->page_table_lock);
  364. rwlock_init(&mm->ioctx_list_lock);
  365. mm->ioctx_list = NULL;
  366. mm->free_area_cache = TASK_UNMAPPED_BASE;
  367. mm->cached_hole_size = ~0UL;
  368.  
  369. if (likely(!mm_alloc_pgd(mm))) {
  370. mm->def_flags = 0;
  371. return mm;
  372. }
  373. free_mm(mm);
  374. return NULL;
  375. }
  376.  
  377. /*
  378. * Allocate and initialize an mm_struct.
  379. */
  380. struct mm_struct * mm_alloc(void)
  381. {
  382. struct mm_struct * mm;
  383.  
  384. mm = allocate_mm();
  385. if (mm) {
  386. memset(mm, 0, sizeof(*mm));
  387. mm = mm_init(mm);
  388. }
  389. return mm;
  390. }
  391.  
  392. /*
  393. * Called when the last reference to the mm
  394. * is dropped: either by a lazy thread or by
  395. * mmput. Free the page directory and the mm.
  396. */
  397. void fastcall __mmdrop(struct mm_struct *mm)
  398. {
  399. BUG_ON(mm == &init_mm);
  400. mm_free_pgd(mm);
  401. destroy_context(mm);
  402. free_mm(mm);
  403. }
  404. EXPORT_SYMBOL_GPL(__mmdrop);
  405.  
  406. /*
  407. * Decrement the use count and release all resources for an mm.
  408. */
  409. void mmput(struct mm_struct *mm)
  410. {
  411. might_sleep();
  412.  
  413. if (atomic_dec_and_test(&mm->mm_users)) {
  414. exit_aio(mm);
  415. exit_mmap(mm);
  416. if (!list_empty(&mm->mmlist)) {
  417. spin_lock(&mmlist_lock);
  418. list_del(&mm->mmlist);
  419. spin_unlock(&mmlist_lock);
  420. }
  421. put_swap_token(mm);
  422. mmdrop(mm);
  423. }
  424. }
  425. EXPORT_SYMBOL_GPL(mmput);
  426.  
  427. /**
  428. * get_task_mm - acquire a reference to the task's mm
  429. *
  430. * Returns %NULL if the task has no mm. Checks PF_BORROWED_MM (meaning
  431. * this kernel workthread has transiently adopted a user mm with use_mm,
  432. * to do its AIO) is not set and if so returns a reference to it, after
  433. * bumping up the use count. User must release the mm via mmput()
  434. * after use. Typically used by /proc and ptrace.
  435. */
  436. struct mm_struct *get_task_mm(struct task_struct *task)
  437. {
  438. struct mm_struct *mm;
  439.  
  440. task_lock(task);
  441. mm = task->mm;
  442. if (mm) {
  443. if (task->flags & PF_BORROWED_MM)
  444. mm = NULL;
  445. else
  446. atomic_inc(&mm->mm_users);
  447. }
  448. task_unlock(task);
  449. return mm;
  450. }
  451. EXPORT_SYMBOL_GPL(get_task_mm);
  452.  
  453. /* Please note the differences between mmput and mm_release.
  454. * mmput is called whenever we stop holding onto a mm_struct,
  455. * error success whatever.
  456. *
  457. * mm_release is called after a mm_struct has been removed
  458. * from the current process.
  459. *
  460. * This difference is important for error handling, when we
  461. * only half set up a mm_struct for a new process and need to restore
  462. * the old one. Because we mmput the new mm_struct before
  463. * restoring the old one. . .
  464. * Eric Biederman 10 January 1998
  465. */
  466. void mm_release(struct task_struct *tsk, struct mm_struct *mm)
  467. {
  468. struct completion *vfork_done = tsk->vfork_done;
  469.  
  470. /* Get rid of any cached register state */
  471. deactivate_mm(tsk, mm);
  472.  
  473. /* notify parent sleeping on vfork() */
  474. if (vfork_done) {
  475. tsk->vfork_done = NULL;
  476. complete(vfork_done);
  477. }
  478.  
  479. /*
  480. * If we're exiting normally, clear a user-space tid field if
  481. * requested. We leave this alone when dying by signal, to leave
  482. * the value intact in a core dump, and to save the unnecessary
  483. * trouble otherwise. Userland only wants this done for a sys_exit.
  484. */
  485. if (tsk->clear_child_tid
  486. && !(tsk->flags & PF_SIGNALED)
  487. && atomic_read(&mm->mm_users) > 1) {
  488. u32 __user * tidptr = tsk->clear_child_tid;
  489. tsk->clear_child_tid = NULL;
  490.  
  491. /*
  492. * We don't check the error code - if userspace has
  493. * not set up a proper pointer then tough luck.
  494. */
  495. put_user(0, tidptr);
  496. sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
  497. }
  498. }
  499.  
  500. /*
  501. * Allocate a new mm structure and copy contents from the
  502. * mm structure of the passed in task structure.
  503. */
  504. static struct mm_struct *dup_mm(struct task_struct *tsk)
  505. {
  506. struct mm_struct *mm, *oldmm = current->mm;
  507. int err;
  508.  
  509. if (!oldmm)
  510. return NULL;
  511.  
  512. mm = allocate_mm();
  513. if (!mm)
  514. goto fail_nomem;
  515.  
  516. memcpy(mm, oldmm, sizeof(*mm));
  517.  
  518. /* Initializing for Swap token stuff */
  519. mm->token_priority = 0;
  520. mm->last_interval = 0;
  521.  
  522. if (!mm_init(mm))
  523. goto fail_nomem;
  524.  
  525. if (init_new_context(tsk, mm))
  526. goto fail_nocontext;
  527.  
  528. err = dup_mmap(mm, oldmm);
  529. if (err)
  530. goto free_pt;
  531.  
  532. mm->hiwater_rss = get_mm_rss(mm);
  533. mm->hiwater_vm = mm->total_vm;
  534.  
  535. return mm;
  536.  
  537. free_pt:
  538. mmput(mm);
  539.  
  540. fail_nomem:
  541. return NULL;
  542.  
  543. fail_nocontext:
  544. /*
  545. * If init_new_context() failed, we cannot use mmput() to free the mm
  546. * because it calls destroy_context()
  547. */
  548. mm_free_pgd(mm);
  549. free_mm(mm);
  550. return NULL;
  551. }
  552.  
  553. static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
  554. {
  555. struct mm_struct * mm, *oldmm;
  556. int retval;
  557.  
  558. tsk->min_flt = tsk->maj_flt = 0;
  559. tsk->nvcsw = tsk->nivcsw = 0;
  560.  
  561. tsk->mm = NULL;
  562. tsk->active_mm = NULL;
  563.  
  564. /*
  565. * Are we cloning a kernel thread?
  566. *
  567. * We need to steal a active VM for that..
  568. */
  569. oldmm = current->mm;
  570. if (!oldmm)
  571. return 0;
  572.  
  573. if (clone_flags & CLONE_VM) {
  574. atomic_inc(&oldmm->mm_users);
  575. mm = oldmm;
  576. goto good_mm;
  577. }
  578.  
  579. retval = -ENOMEM;
  580. mm = dup_mm(tsk);
  581. if (!mm)
  582. goto fail_nomem;
  583.  
  584. good_mm:
  585. /* Initializing for Swap token stuff */
  586. mm->token_priority = 0;
  587. mm->last_interval = 0;
  588.  
  589. tsk->mm = mm;
  590. tsk->active_mm = mm;
  591. return 0;
  592.  
  593. fail_nomem:
  594. return retval;
  595. }
  596.  
  597. static struct fs_struct *__copy_fs_struct(struct fs_struct *old)
  598. {
  599. struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
  600. /* We don't need to lock fs - think why ;-) */
  601. if (fs) {
  602. atomic_set(&fs->count, 1);
  603. rwlock_init(&fs->lock);
  604. fs->umask = old->umask;
  605. read_lock(&old->lock);
  606. fs->rootmnt = mntget(old->rootmnt);
  607. fs->root = dget(old->root);
  608. fs->pwdmnt = mntget(old->pwdmnt);
  609. fs->pwd = dget(old->pwd);
  610. if (old->altroot) {
  611. fs->altrootmnt = mntget(old->altrootmnt);
  612. fs->altroot = dget(old->altroot);
  613. } else {
  614. fs->altrootmnt = NULL;
  615. fs->altroot = NULL;
  616. }
  617. read_unlock(&old->lock);
  618. }
  619. return fs;
  620. }
  621.  
  622. struct fs_struct *copy_fs_struct(struct fs_struct *old)
  623. {
  624. return __copy_fs_struct(old);
  625. }
  626.  
  627. EXPORT_SYMBOL_GPL(copy_fs_struct);
  628.  
  629. static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
  630. {
  631. if (clone_flags & CLONE_FS) {
  632. atomic_inc(&current->fs->count);
  633. return 0;
  634. }
  635. tsk->fs = __copy_fs_struct(current->fs);
  636. if (!tsk->fs)
  637. return -ENOMEM;
  638. return 0;
  639. }
  640.  
  641. static int count_open_files(struct fdtable *fdt)
  642. {
  643. int size = fdt->max_fds;
  644. int i;
  645.  
  646. /* Find the last open fd */
  647. for (i = size/(8*sizeof(long)); i > 0; ) {
  648. if (fdt->open_fds->fds_bits[--i])
  649. break;
  650. }
  651. i = (i+1) * 8 * sizeof(long);
  652. return i;
  653. }
  654.  
  655. static struct files_struct *alloc_files(void)
  656. {
  657. struct files_struct *newf;
  658. struct fdtable *fdt;
  659.  
  660. newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
  661. if (!newf)
  662. goto out;
  663.  
  664. atomic_set(&newf->count, 1);
  665.  
  666. spin_lock_init(&newf->file_lock);
  667. newf->next_fd = 0;
  668. fdt = &newf->fdtab;
  669. fdt->max_fds = NR_OPEN_DEFAULT;
  670. fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
  671. fdt->open_fds = (fd_set *)&newf->open_fds_init;
  672. fdt->fd = &newf->fd_array[0];
  673. INIT_RCU_HEAD(&fdt->rcu);
  674. fdt->next = NULL;
  675. rcu_assign_pointer(newf->fdt, fdt);
  676. out:
  677. return newf;
  678. }
  679.  
  680. /*
  681. * Allocate a new files structure and copy contents from the
  682. * passed in files structure.
  683. * errorp will be valid only when the returned files_struct is NULL.
  684. */
  685. static struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
  686. {
  687. struct files_struct *newf;
  688. struct file **old_fds, **new_fds;
  689. int open_files, size, i;
  690. struct fdtable *old_fdt, *new_fdt;
  691.  
  692. *errorp = -ENOMEM;
  693. newf = alloc_files();
  694. if (!newf)
  695. goto out;
  696.  
  697. spin_lock(&oldf->file_lock);
  698. old_fdt = files_fdtable(oldf);
  699. new_fdt = files_fdtable(newf);
  700. open_files = count_open_files(old_fdt);
  701.  
  702. /*
  703. * Check whether we need to allocate a larger fd array and fd set.
  704. * Note: we're not a clone task, so the open count won't change.
  705. */
  706. if (open_files > new_fdt->max_fds) {
  707. new_fdt->max_fds = 0;
  708. spin_unlock(&oldf->file_lock);
  709. spin_lock(&newf->file_lock);
  710. *errorp = expand_files(newf, open_files-1);
  711. spin_unlock(&newf->file_lock);
  712. if (*errorp < 0)
  713. goto out_release;
  714. new_fdt = files_fdtable(newf);
  715. /*
  716. * Reacquire the oldf lock and a pointer to its fd table
  717. * who knows it may have a new bigger fd table. We need
  718. * the latest pointer.
  719. */
  720. spin_lock(&oldf->file_lock);
  721. old_fdt = files_fdtable(oldf);
  722. }
  723.  
  724. old_fds = old_fdt->fd;
  725. new_fds = new_fdt->fd;
  726.  
  727. memcpy(new_fdt->open_fds->fds_bits,
  728. old_fdt->open_fds->fds_bits, open_files/8);
  729. memcpy(new_fdt->close_on_exec->fds_bits,
  730. old_fdt->close_on_exec->fds_bits, open_files/8);
  731.  
  732. for (i = open_files; i != 0; i--) {
  733. struct file *f = *old_fds++;
  734. if (f) {
  735. get_file(f);
  736. } else {
  737. /*
  738. * The fd may be claimed in the fd bitmap but not yet
  739. * instantiated in the files array if a sibling thread
  740. * is partway through open(). So make sure that this
  741. * fd is available to the new process.
  742. */
  743. FD_CLR(open_files - i, new_fdt->open_fds);
  744. }
  745. rcu_assign_pointer(*new_fds++, f);
  746. }
  747. spin_unlock(&oldf->file_lock);
  748.  
  749. /* compute the remainder to be cleared */
  750. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  751.  
  752. /* This is long word aligned thus could use a optimized version */
  753. memset(new_fds, 0, size);
  754.  
  755. if (new_fdt->max_fds > open_files) {
  756. int left = (new_fdt->max_fds-open_files)/8;
  757. int start = open_files / (8 * sizeof(unsigned long));
  758.  
  759. memset(&new_fdt->open_fds->fds_bits[start], 0, left);
  760. memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
  761. }
  762.  
  763. return newf;
  764.  
  765. out_release:
  766. kmem_cache_free(files_cachep, newf);
  767. out:
  768. return NULL;
  769. }
  770.  
  771. static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
  772. {
  773. struct files_struct *oldf, *newf;
  774. int error = 0;
  775.  
  776. /*
  777. * A background process may not have any files ...
  778. */
  779. oldf = current->files;
  780. if (!oldf)
  781. goto out;
  782.  
  783. if (clone_flags & CLONE_FILES) {
  784. atomic_inc(&oldf->count);
  785. goto out;
  786. }
  787.  
  788. /*
  789. * Note: we may be using current for both targets (See exec.c)
  790. * This works because we cache current->files (old) as oldf. Don't
  791. * break this.
  792. */
  793. tsk->files = NULL;
  794. newf = dup_fd(oldf, &error);
  795. if (!newf)
  796. goto out;
  797.  
  798. tsk->files = newf;
  799. error = 0;
  800. out:
  801. return error;
  802. }
  803.  
  804. /*
  805. * Helper to unshare the files of the current task.
  806. * We don't want to expose copy_files internals to
  807. * the exec layer of the kernel.
  808. */
  809.  
  810. int unshare_files(void)
  811. {
  812. struct files_struct *files = current->files;
  813. int rc;
  814.  
  815. BUG_ON(!files);
  816.  
  817. /* This can race but the race causes us to copy when we don't
  818. need to and drop the copy */
  819. if(atomic_read(&files->count) == 1)
  820. {
  821. atomic_inc(&files->count);
  822. return 0;
  823. }
  824. rc = copy_files(0, current);
  825. if(rc)
  826. current->files = files;
  827. return rc;
  828. }
  829.  
  830. EXPORT_SYMBOL(unshare_files);
  831.  
  832. static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
  833. {
  834. struct sighand_struct *sig;
  835.  
  836. if (clone_flags & (CLONE_SIGHAND | CLONE_THREAD)) {
  837. atomic_inc(&current->sighand->count);
  838. return 0;
  839. }
  840. sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  841. rcu_assign_pointer(tsk->sighand, sig);
  842. if (!sig)
  843. return -ENOMEM;
  844. atomic_set(&sig->count, 1);
  845. memcpy(sig->action, current->sighand->action, sizeof(sig->action));
  846. return 0;
  847. }
  848.  
  849. void __cleanup_sighand(struct sighand_struct *sighand)
  850. {
  851. if (atomic_dec_and_test(&sighand->count))
  852. kmem_cache_free(sighand_cachep, sighand);
  853. }
  854.  
  855. static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
  856. {
  857. struct signal_struct *sig;
  858. int ret;
  859.  
  860. if (clone_flags & CLONE_THREAD) {
  861. atomic_inc(&current->signal->count);
  862. atomic_inc(&current->signal->live);
  863. return 0;
  864. }
  865. sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
  866. tsk->signal = sig;
  867. if (!sig)
  868. return -ENOMEM;
  869.  
  870. ret = copy_thread_group_keys(tsk);
  871. if (ret < 0) {
  872. kmem_cache_free(signal_cachep, sig);
  873. return ret;
  874. }
  875.  
  876. atomic_set(&sig->count, 1);
  877. atomic_set(&sig->live, 1);
  878. init_waitqueue_head(&sig->wait_chldexit);
  879. sig->flags = 0;
  880. sig->group_exit_code = 0;
  881. sig->group_exit_task = NULL;
  882. sig->group_stop_count = 0;
  883. sig->curr_target = NULL;
  884. init_sigpending(&sig->shared_pending);
  885. INIT_LIST_HEAD(&sig->posix_timers);
  886.  
  887. hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  888. sig->it_real_incr.tv64 = 0;
  889. sig->real_timer.function = it_real_fn;
  890. sig->tsk = tsk;
  891.  
  892. sig->it_virt_expires = cputime_zero;
  893. sig->it_virt_incr = cputime_zero;
  894. sig->it_prof_expires = cputime_zero;
  895. sig->it_prof_incr = cputime_zero;
  896.  
  897. sig->leader = 0; /* session leadership doesn't inherit */
  898. sig->tty_old_pgrp = NULL;
  899.  
  900. sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
  901. sig->gtime = cputime_zero;
  902. sig->cgtime = cputime_zero;
  903. sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
  904. sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
  905. sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0;
  906. sig->sum_sched_runtime = 0;
  907. INIT_LIST_HEAD(&sig->cpu_timers[0]);
  908. INIT_LIST_HEAD(&sig->cpu_timers[1]);
  909. INIT_LIST_HEAD(&sig->cpu_timers[2]);
  910. taskstats_tgid_init(sig);
  911.  
  912. task_lock(current->group_leader);
  913. memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
  914. task_unlock(current->group_leader);
  915.  
  916. if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
  917. /*
  918. * New sole thread in the process gets an expiry time
  919. * of the whole CPU time limit.
  920. */
  921. tsk->it_prof_expires =
  922. secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
  923. }
  924. acct_init_pacct(&sig->pacct);
  925.  
  926. tty_audit_fork(sig);
  927.  
  928. return 0;
  929. }
  930.  
  931. void __cleanup_signal(struct signal_struct *sig)
  932. {
  933. exit_thread_group_keys(sig);
  934. kmem_cache_free(signal_cachep, sig);
  935. }
  936.  
  937. static void cleanup_signal(struct task_struct *tsk)
  938. {
  939. struct signal_struct *sig = tsk->signal;
  940.  
  941. atomic_dec(&sig->live);
  942.  
  943. if (atomic_dec_and_test(&sig->count))
  944. __cleanup_signal(sig);
  945. }
  946.  
  947. static void copy_flags(unsigned long clone_flags, struct task_struct *p)
  948. {
  949. unsigned long new_flags = p->flags;
  950.  
  951. new_flags &= ~PF_SUPERPRIV;
  952. new_flags |= PF_FORKNOEXEC;
  953. if (!(clone_flags & CLONE_PTRACE))
  954. p->ptrace = 0;
  955. p->flags = new_flags;
  956. clear_freeze_flag(p);
  957. }
  958.  
  959. asmlinkage long sys_set_tid_address(int __user *tidptr)
  960. {
  961. current->clear_child_tid = tidptr;
  962.  
  963. return task_pid_vnr(current);
  964. }
  965.  
  966. static void rt_mutex_init_task(struct task_struct *p)
  967. {
  968. spin_lock_init(&p->pi_lock);
  969. #ifdef CONFIG_RT_MUTEXES
  970. plist_head_init(&p->pi_waiters, &p->pi_lock);
  971. p->pi_blocked_on = NULL;
  972. #endif
  973. }
  974.  
  975. /*
  976. * This creates a new process as a copy of the old one,
  977. * but does not actually start it yet.
  978. *
  979. * It copies the registers, and all the appropriate
  980. * parts of the process environment (as per the clone
  981. * flags). The actual kick-off is left to the caller.
  982. */
  983. static struct task_struct *copy_process(unsigned long clone_flags,
  984. unsigned long stack_start,
  985. struct pt_regs *regs,
  986. unsigned long stack_size,
  987. int __user *child_tidptr,
  988. struct pid *pid)
  989. {
  990. int retval;
  991. struct task_struct *p;
  992. int cgroup_callbacks_done = 0;
  993.  
  994. if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
  995. return ERR_PTR(-EINVAL);
  996.  
  997. /*
  998. * Thread groups must share signals as well, and detached threads
  999. * can only be started up within the thread group.
  1000. */
  1001. if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
  1002. return ERR_PTR(-EINVAL);
  1003.  
  1004. /*
  1005. * Shared signal handlers imply shared VM. By way of the above,
  1006. * thread groups also imply shared VM. Blocking this case allows
  1007. * for various simplifications in other code.
  1008. */
  1009. if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
  1010. return ERR_PTR(-EINVAL);
  1011.  
  1012. retval = security_task_create(clone_flags);
  1013. if (retval)
  1014. goto fork_out;
  1015.  
  1016. retval = -ENOMEM;
  1017. p = dup_task_struct(current);
  1018. if (!p)
  1019. goto fork_out;
  1020.  
  1021. rt_mutex_init_task(p);
  1022.  
  1023. #ifdef CONFIG_TRACE_IRQFLAGS
  1024. DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
  1025. DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
  1026. #endif
  1027. retval = -EAGAIN;
  1028. if (atomic_read(&p->user->processes) >=
  1029. p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
  1030. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
  1031. p->user != current->nsproxy->user_ns->root_user)
  1032. goto bad_fork_free;
  1033. }
  1034.  
  1035. atomic_inc(&p->user->__count);
  1036. atomic_inc(&p->user->processes);
  1037. get_group_info(p->group_info);
  1038.  
  1039. /*
  1040. * If multiple threads are within copy_process(), then this check
  1041. * triggers too late. This doesn't hurt, the check is only there
  1042. * to stop root fork bombs.
  1043. */
  1044. if (nr_threads >= max_threads)
  1045. goto bad_fork_cleanup_count;
  1046.  
  1047. if (!try_module_get(task_thread_info(p)->exec_domain->module))
  1048. goto bad_fork_cleanup_count;
  1049.  
  1050. if (p->binfmt && !try_module_get(p->binfmt->module))
  1051. goto bad_fork_cleanup_put_domain;
  1052.  
  1053. p->did_exec = 0;
  1054. delayacct_tsk_init(p); /* Must remain after dup_task_struct() */
  1055. copy_flags(clone_flags, p);
  1056. INIT_LIST_HEAD(&p->children);
  1057. INIT_LIST_HEAD(&p->sibling);
  1058. p->vfork_done = NULL;
  1059. spin_lock_init(&p->alloc_lock);
  1060.  
  1061. clear_tsk_thread_flag(p, TIF_SIGPENDING);
  1062. init_sigpending(&p->pending);
  1063.  
  1064. p->utime = cputime_zero;
  1065. p->stime = cputime_zero;
  1066. p->gtime = cputime_zero;
  1067. p->utimescaled = cputime_zero;
  1068. p->stimescaled = cputime_zero;
  1069. p->prev_utime = cputime_zero;
  1070. p->prev_stime = cputime_zero;
  1071.  
  1072. #ifdef CONFIG_TASK_XACCT
  1073. p->rchar = 0; /* I/O counter: bytes read */
  1074. p->wchar = 0; /* I/O counter: bytes written */
  1075. p->syscr = 0; /* I/O counter: read syscalls */
  1076. p->syscw = 0; /* I/O counter: write syscalls */
  1077. #endif
  1078. task_io_accounting_init(p);
  1079. acct_clear_integrals(p);
  1080.  
  1081. p->it_virt_expires = cputime_zero;
  1082. p->it_prof_expires = cputime_zero;
  1083. p->it_sched_expires = 0;
  1084. INIT_LIST_HEAD(&p->cpu_timers[0]);
  1085. INIT_LIST_HEAD(&p->cpu_timers[1]);
  1086. INIT_LIST_HEAD(&p->cpu_timers[2]);
  1087.  
  1088. p->lock_depth = -1; /* -1 = no lock */
  1089. do_posix_clock_monotonic_gettime(&p->start_time);
  1090. p->real_start_time = p->start_time;
  1091. monotonic_to_bootbased(&p->real_start_time);
  1092. #ifdef CONFIG_SECURITY
  1093. p->security = NULL;
  1094. #endif
  1095. p->io_context = NULL;
  1096. p->audit_context = NULL;
  1097. cgroup_fork(p);
  1098. #ifdef CONFIG_NUMA
  1099. p->mempolicy = mpol_copy(p->mempolicy);
  1100. if (IS_ERR(p->mempolicy)) {
  1101. retval = PTR_ERR(p->mempolicy);
  1102. p->mempolicy = NULL;
  1103. goto bad_fork_cleanup_cgroup;
  1104. }
  1105. mpol_fix_fork_child_flag(p);
  1106. #endif
  1107. #ifdef CONFIG_TRACE_IRQFLAGS
  1108. p->irq_events = 0;
  1109. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  1110. p->hardirqs_enabled = 1;
  1111. #else
  1112. p->hardirqs_enabled = 0;
  1113. #endif
  1114. p->hardirq_enable_ip = 0;
  1115. p->hardirq_enable_event = 0;
  1116. p->hardirq_disable_ip = _THIS_IP_;
  1117. p->hardirq_disable_event = 0;
  1118. p->softirqs_enabled = 1;
  1119. p->softirq_enable_ip = _THIS_IP_;
  1120. p->softirq_enable_event = 0;
  1121. p->softirq_disable_ip = 0;
  1122. p->softirq_disable_event = 0;
  1123. p->hardirq_context = 0;
  1124. p->softirq_context = 0;
  1125. #endif
  1126. #ifdef CONFIG_LOCKDEP
  1127. p->lockdep_depth = 0; /* no locks held yet */
  1128. p->curr_chain_key = 0;
  1129. p->lockdep_recursion = 0;
  1130. #endif
  1131.  
  1132. #ifdef CONFIG_DEBUG_MUTEXES
  1133. p->blocked_on = NULL; /* not blocked yet */
  1134. #endif
  1135.  
  1136. /* Perform scheduler related setup. Assign this task to a CPU. */
  1137. sched_fork(p, clone_flags);
  1138.  
  1139. if ((retval = security_task_alloc(p)))
  1140. goto bad_fork_cleanup_policy;
  1141. if ((retval = audit_alloc(p)))
  1142. goto bad_fork_cleanup_security;
  1143. /* copy all the process information */
  1144. if ((retval = copy_semundo(clone_flags, p)))
  1145. goto bad_fork_cleanup_audit;
  1146. if ((retval = copy_files(clone_flags, p)))
  1147. goto bad_fork_cleanup_semundo;
  1148. if ((retval = copy_fs(clone_flags, p)))
  1149. goto bad_fork_cleanup_files;
  1150. if ((retval = copy_sighand(clone_flags, p)))
  1151. goto bad_fork_cleanup_fs;
  1152. if ((retval = copy_signal(clone_flags, p)))
  1153. goto bad_fork_cleanup_sighand;
  1154. if ((retval = copy_mm(clone_flags, p)))
  1155. goto bad_fork_cleanup_signal;
  1156. if ((retval = copy_keys(clone_flags, p)))
  1157. goto bad_fork_cleanup_mm;
  1158. if ((retval = copy_namespaces(clone_flags, p)))
  1159. goto bad_fork_cleanup_keys;
  1160. retval = copy_thread(0, clone_flags, stack_start, stack_size, p, regs);
  1161. if (retval)
  1162. goto bad_fork_cleanup_namespaces;
  1163.  
  1164. if (pid != &init_struct_pid) {
  1165. retval = -ENOMEM;
  1166. pid = alloc_pid(task_active_pid_ns(p));
  1167. if (!pid)
  1168. goto bad_fork_cleanup_namespaces;
  1169.  
  1170. if (clone_flags & CLONE_NEWPID) {
  1171. retval = pid_ns_prepare_proc(task_active_pid_ns(p));
  1172. if (retval < 0)
  1173. goto bad_fork_free_pid;
  1174. }
  1175. }
  1176.  
  1177. p->pid = pid_nr(pid);
  1178. p->tgid = p->pid;
  1179. if (clone_flags & CLONE_THREAD)
  1180. p->tgid = current->tgid;
  1181.  
  1182. p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
  1183. /*
  1184. * Clear TID on mm_release()?
  1185. */
  1186. p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;
  1187. #ifdef CONFIG_FUTEX
  1188. p->robust_list = NULL;
  1189. #ifdef CONFIG_COMPAT
  1190. p->compat_robust_list = NULL;
  1191. #endif
  1192. INIT_LIST_HEAD(&p->pi_state_list);
  1193. p->pi_state_cache = NULL;
  1194. #endif
  1195. /*
  1196. * sigaltstack should be cleared when sharing the same VM
  1197. */
  1198. if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
  1199. p->sas_ss_sp = p->sas_ss_size = 0;
  1200.  
  1201. /*
  1202. * Syscall tracing should be turned off in the child regardless
  1203. * of CLONE_PTRACE.
  1204. */
  1205. clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
  1206. #ifdef TIF_SYSCALL_EMU
  1207. clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
  1208. #endif
  1209. clear_all_latency_tracing(p);
  1210.  
  1211. /* ok, now we should be set up.. */
  1212. p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL);
  1213. p->pdeath_signal = 0;
  1214. p->exit_state = 0;
  1215.  
  1216. /*
  1217. * Ok, make it visible to the rest of the system.
  1218. * We dont wake it up yet.
  1219. */
  1220. p->group_leader = p;
  1221. INIT_LIST_HEAD(&p->thread_group);
  1222. INIT_LIST_HEAD(&p->ptrace_children);
  1223. INIT_LIST_HEAD(&p->ptrace_list);
  1224.  
  1225. /* Now that the task is set up, run cgroup callbacks if
  1226. * necessary. We need to run them before the task is visible
  1227. * on the tasklist. */
  1228. cgroup_fork_callbacks(p);
  1229. cgroup_callbacks_done = 1;
  1230.  
  1231. /* Need tasklist lock for parent etc handling! */
  1232. write_lock_irq(&tasklist_lock);
  1233.  
  1234. /* for sys_ioprio_set(IOPRIO_WHO_PGRP) */
  1235. p->ioprio = current->ioprio;
  1236.  
  1237. /*
  1238. * The task hasn't been attached yet, so its cpus_allowed mask will
  1239. * not be changed, nor will its assigned CPU.
  1240. *
  1241. * The cpus_allowed mask of the parent may have changed after it was
  1242. * copied first time - so re-copy it here, then check the child's CPU
  1243. * to ensure it is on a valid CPU (and if not, just force it back to
  1244. * parent's CPU). This avoids alot of nasty races.
  1245. */
  1246. p->cpus_allowed = current->cpus_allowed;
  1247. if (unlikely(!cpu_isset(task_cpu(p), p->cpus_allowed) ||
  1248. !cpu_online(task_cpu(p))))
  1249. set_task_cpu(p, smp_processor_id());
  1250.  
  1251. /* CLONE_PARENT re-uses the old parent */
  1252. if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
  1253. p->real_parent = current->real_parent;
  1254. p->parent_exec_id = current->parent_exec_id;
  1255. } else {
  1256. p->real_parent = current;
  1257. p->parent_exec_id = current->self_exec_id;
  1258. }
  1259. p->parent = p->real_parent;
  1260.  
  1261. spin_lock(&current->sighand->siglock);
  1262.  
  1263. /*
  1264. * Process group and session signals need to be delivered to just the
  1265. * parent before the fork or both the parent and the child after the
  1266. * fork. Restart if a signal comes in before we add the new process to
  1267. * it's process group.
  1268. * A fatal signal pending means that current will exit, so the new
  1269. * thread can't slip out of an OOM kill (or normal SIGKILL).
  1270. */
  1271. recalc_sigpending();
  1272. if (signal_pending(current)) {
  1273. spin_unlock(&current->sighand->siglock);
  1274. write_unlock_irq(&tasklist_lock);
  1275. retval = -ERESTARTNOINTR;
  1276. goto bad_fork_free_pid;
  1277. }
  1278.  
  1279. if (clone_flags & CLONE_THREAD) {
  1280. p->group_leader = current->group_leader;
  1281. list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
  1282.  
  1283. if (!cputime_eq(current->signal->it_virt_expires,
  1284. cputime_zero) ||
  1285. !cputime_eq(current->signal->it_prof_expires,
  1286. cputime_zero) ||
  1287. current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY ||
  1288. !list_empty(&current->signal->cpu_timers[0]) ||
  1289. !list_empty(&current->signal->cpu_timers[1]) ||
  1290. !list_empty(&current->signal->cpu_timers[2])) {
  1291. /*
  1292. * Have child wake up on its first tick to check
  1293. * for process CPU timers.
  1294. */
  1295. p->it_prof_expires = jiffies_to_cputime(1);
  1296. }
  1297. }
  1298.  
  1299. if (likely(p->pid)) {
  1300. add_parent(p);
  1301. if (unlikely(p->ptrace & PT_PTRACED))
  1302. __ptrace_link(p, current->parent);
  1303.  
  1304. if (thread_group_leader(p)) {
  1305. if (clone_flags & CLONE_NEWPID)
  1306. p->nsproxy->pid_ns->child_reaper = p;
  1307.  
  1308. p->signal->tty = current->signal->tty;
  1309. set_task_pgrp(p, task_pgrp_nr(current));
  1310. set_task_session(p, task_session_nr(current));
  1311. attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
  1312. attach_pid(p, PIDTYPE_SID, task_session(current));
  1313. list_add_tail_rcu(&p->tasks, &init_task.tasks);
  1314. __get_cpu_var(process_counts)++;
  1315. }
  1316. attach_pid(p, PIDTYPE_PID, pid);
  1317. nr_threads++;
  1318. }
  1319.  
  1320. total_forks++;
  1321. spin_unlock(&current->sighand->siglock);
  1322. write_unlock_irq(&tasklist_lock);
  1323. proc_fork_connector(p);
  1324. cgroup_post_fork(p);
  1325. return p;
  1326.  
  1327. bad_fork_free_pid:
  1328. if (pid != &init_struct_pid)
  1329. free_pid(pid);
  1330. bad_fork_cleanup_namespaces:
  1331. exit_task_namespaces(p);
  1332. bad_fork_cleanup_keys:
  1333. exit_keys(p);
  1334. bad_fork_cleanup_mm:
  1335. if (p->mm)
  1336. mmput(p->mm);
  1337. bad_fork_cleanup_signal:
  1338. cleanup_signal(p);
  1339. bad_fork_cleanup_sighand:
  1340. __cleanup_sighand(p->sighand);
  1341. bad_fork_cleanup_fs:
  1342. exit_fs(p); /* blocking */
  1343. bad_fork_cleanup_files:
  1344. exit_files(p); /* blocking */
  1345. bad_fork_cleanup_semundo:
  1346. exit_sem(p);
  1347. bad_fork_cleanup_audit:
  1348. audit_free(p);
  1349. bad_fork_cleanup_security:
  1350. security_task_free(p);
  1351. bad_fork_cleanup_policy:
  1352. #ifdef CONFIG_NUMA
  1353. mpol_free(p->mempolicy);
  1354. bad_fork_cleanup_cgroup:
  1355. #endif
  1356. cgroup_exit(p, cgroup_callbacks_done);
  1357. delayacct_tsk_free(p);
  1358. if (p->binfmt)
  1359. module_put(p->binfmt->module);
  1360. bad_fork_cleanup_put_domain:
  1361. module_put(task_thread_info(p)->exec_domain->module);
  1362. bad_fork_cleanup_count:
  1363. put_group_info(p->group_info);
  1364. atomic_dec(&p->user->processes);
  1365. free_uid(p->user);
  1366. bad_fork_free:
  1367. free_task(p);
  1368. fork_out:
  1369. return ERR_PTR(retval);
  1370. }
  1371.  
  1372. noinline struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_regs *regs)
  1373. {
  1374. memset(regs, 0, sizeof(struct pt_regs));
  1375. return regs;
  1376. }
  1377.  
  1378. struct task_struct * __cpuinit fork_idle(int cpu)
  1379. {
  1380. struct task_struct *task;
  1381. struct pt_regs regs;
  1382.  
  1383. task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL,
  1384. &init_struct_pid);
  1385. if (!IS_ERR(task))
  1386. init_idle(task, cpu);
  1387.  
  1388. return task;
  1389. }
  1390.  
  1391. static int fork_traceflag(unsigned clone_flags)
  1392. {
  1393. if (clone_flags & CLONE_UNTRACED)
  1394. return 0;
  1395. else if (clone_flags & CLONE_VFORK) {
  1396. if (current->ptrace & PT_TRACE_VFORK)
  1397. return PTRACE_EVENT_VFORK;
  1398. } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
  1399. if (current->ptrace & PT_TRACE_CLONE)
  1400. return PTRACE_EVENT_CLONE;
  1401. } else if (current->ptrace & PT_TRACE_FORK)
  1402. return PTRACE_EVENT_FORK;
  1403.  
  1404. return 0;
  1405. }
  1406.  
  1407. /*
  1408. * Ok, this is the main fork-routine.
  1409. *
  1410. * It copies the process, and if successful kick-starts
  1411. * it and waits for it to finish using the VM if required.
  1412. */
  1413. long do_fork(unsigned long clone_flags,
  1414. unsigned long stack_start,
  1415. struct pt_regs *regs,
  1416. unsigned long stack_size,
  1417. int __user *parent_tidptr,
  1418. int __user *child_tidptr)
  1419. {
  1420. struct task_struct *p;
  1421. int trace = 0;
  1422. long nr;
  1423.  
  1424. if (unlikely(current->ptrace)) {
  1425. trace = fork_traceflag (clone_flags);
  1426. if (trace)
  1427. clone_flags |= CLONE_PTRACE;
  1428. }
  1429.  
  1430. p = copy_process(clone_flags, stack_start, regs, stack_size,
  1431. child_tidptr, NULL);
  1432. /*
  1433. * Do this prior waking up the new thread - the thread pointer
  1434. * might get invalid after that point, if the thread exits quickly.
  1435. */
  1436. if (!IS_ERR(p)) {
  1437. struct completion vfork;
  1438.  
  1439. /*
  1440. * this is enough to call pid_nr_ns here, but this if
  1441. * improves optimisation of regular fork()
  1442. */
  1443. nr = (clone_flags & CLONE_NEWPID) ?
  1444. task_pid_nr_ns(p, current->nsproxy->pid_ns) :
  1445. task_pid_vnr(p);
  1446.  
  1447. if (clone_flags & CLONE_PARENT_SETTID)
  1448. put_user(nr, parent_tidptr);
  1449.  
  1450. if (clone_flags & CLONE_VFORK) {
  1451. p->vfork_done = &vfork;
  1452. init_completion(&vfork);
  1453. }
  1454.  
  1455. if ((p->ptrace & PT_PTRACED) || (clone_flags & CLONE_STOPPED)) {
  1456. /*
  1457. * We'll start up with an immediate SIGSTOP.
  1458. */
  1459. sigaddset(&p->pending.signal, SIGSTOP);
  1460. set_tsk_thread_flag(p, TIF_SIGPENDING);
  1461. }
  1462.  
  1463. if (!(clone_flags & CLONE_STOPPED))
  1464. wake_up_new_task(p, clone_flags);
  1465. else
  1466. p->state = TASK_STOPPED;
  1467.  
  1468. if (unlikely (trace)) {
  1469. current->ptrace_message = nr;
  1470. ptrace_notify ((trace << 8) | SIGTRAP);
  1471. }
  1472.  
  1473. if (clone_flags & CLONE_VFORK) {
  1474. freezer_do_not_count();
  1475. wait_for_completion(&vfork);
  1476. freezer_count();
  1477. if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) {
  1478. current->ptrace_message = nr;
  1479. ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
  1480. }
  1481. }
  1482. } else {
  1483. nr = PTR_ERR(p);
  1484. }
  1485. return nr;
  1486. }
  1487.  
  1488. #ifndef ARCH_MIN_MMSTRUCT_ALIGN
  1489. #define ARCH_MIN_MMSTRUCT_ALIGN 0
  1490. #endif
  1491.  
  1492. static void sighand_ctor(struct kmem_cache *cachep, void *data)
  1493. {
  1494. struct sighand_struct *sighand = data;
  1495.  
  1496. spin_lock_init(&sighand->siglock);
  1497. init_waitqueue_head(&sighand->signalfd_wqh);
  1498. }
  1499.  
  1500. void __init proc_caches_init(void)
  1501. {
  1502. sighand_cachep = kmem_cache_create("sighand_cache",
  1503. sizeof(struct sighand_struct), 0,
  1504. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU,
  1505. sighand_ctor);
  1506. signal_cachep = kmem_cache_create("signal_cache",
  1507. sizeof(struct signal_struct), 0,
  1508. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1509. files_cachep = kmem_cache_create("files_cache",
  1510. sizeof(struct files_struct), 0,
  1511. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1512. fs_cachep = kmem_cache_create("fs_cache",
  1513. sizeof(struct fs_struct), 0,
  1514. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1515. vm_area_cachep = kmem_cache_create("vm_area_struct",
  1516. sizeof(struct vm_area_struct), 0,
  1517. SLAB_PANIC, NULL);
  1518. mm_cachep = kmem_cache_create("mm_struct",
  1519. sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
  1520. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1521. }
  1522.  
  1523. /*
  1524. * Check constraints on flags passed to the unshare system call and
  1525. * force unsharing of additional process context as appropriate.
  1526. */
  1527. static void check_unshare_flags(unsigned long *flags_ptr)
  1528. {
  1529. /*
  1530. * If unsharing a thread from a thread group, must also
  1531. * unshare vm.
  1532. */
  1533. if (*flags_ptr & CLONE_THREAD)
  1534. *flags_ptr |= CLONE_VM;
  1535.  
  1536. /*
  1537. * If unsharing vm, must also unshare signal handlers.
  1538. */
  1539. if (*flags_ptr & CLONE_VM)
  1540. *flags_ptr |= CLONE_SIGHAND;
  1541.  
  1542. /*
  1543. * If unsharing signal handlers and the task was created
  1544. * using CLONE_THREAD, then must unshare the thread
  1545. */
  1546. if ((*flags_ptr & CLONE_SIGHAND) &&
  1547. (atomic_read(&current->signal->count) > 1))
  1548. *flags_ptr |= CLONE_THREAD;
  1549.  
  1550. /*
  1551. * If unsharing namespace, must also unshare filesystem information.
  1552. */
  1553. if (*flags_ptr & CLONE_NEWNS)
  1554. *flags_ptr |= CLONE_FS;
  1555. }
  1556.  
  1557. /*
  1558. * Unsharing of tasks created with CLONE_THREAD is not supported yet
  1559. */
  1560. static int unshare_thread(unsigned long unshare_flags)
  1561. {
  1562. if (unshare_flags & CLONE_THREAD)
  1563. return -EINVAL;
  1564.  
  1565. return 0;
  1566. }
  1567.  
  1568. /*
  1569. * Unshare the filesystem structure if it is being shared
  1570. */
  1571. static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
  1572. {
  1573. struct fs_struct *fs = current->fs;
  1574.  
  1575. if ((unshare_flags & CLONE_FS) &&
  1576. (fs && atomic_read(&fs->count) > 1)) {
  1577. *new_fsp = __copy_fs_struct(current->fs);
  1578. if (!*new_fsp)
  1579. return -ENOMEM;
  1580. }
  1581.  
  1582. return 0;
  1583. }
  1584.  
  1585. /*
  1586. * Unsharing of sighand is not supported yet
  1587. */
  1588. static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
  1589. {
  1590. struct sighand_struct *sigh = current->sighand;
  1591.  
  1592. if ((unshare_flags & CLONE_SIGHAND) && atomic_read(&sigh->count) > 1)
  1593. return -EINVAL;
  1594. else
  1595. return 0;
  1596. }
  1597.  
  1598. /*
  1599. * Unshare vm if it is being shared
  1600. */
  1601. static int unshare_vm(unsigned long unshare_flags, struct mm_struct **new_mmp)
  1602. {
  1603. struct mm_struct *mm = current->mm;
  1604.  
  1605. if ((unshare_flags & CLONE_VM) &&
  1606. (mm && atomic_read(&mm->mm_users) > 1)) {
  1607. return -EINVAL;
  1608. }
  1609.  
  1610. return 0;
  1611. }
  1612.  
  1613. /*
  1614. * Unshare file descriptor table if it is being shared
  1615. */
  1616. static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
  1617. {
  1618. struct files_struct *fd = current->files;
  1619. int error = 0;
  1620.  
  1621. if ((unshare_flags & CLONE_FILES) &&
  1622. (fd && atomic_read(&fd->count) > 1)) {
  1623. *new_fdp = dup_fd(fd, &error);
  1624. if (!*new_fdp)
  1625. return error;
  1626. }
  1627.  
  1628. return 0;
  1629. }
  1630.  
  1631. /*
  1632. * Unsharing of semundo for tasks created with CLONE_SYSVSEM is not
  1633. * supported yet
  1634. */
  1635. static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **new_ulistp)
  1636. {
  1637. if (unshare_flags & CLONE_SYSVSEM)
  1638. return -EINVAL;
  1639.  
  1640. return 0;
  1641. }
  1642.  
  1643. /*
  1644. * unshare allows a process to 'unshare' part of the process
  1645. * context which was originally shared using clone. copy_*
  1646. * functions used by do_fork() cannot be used here directly
  1647. * because they modify an inactive task_struct that is being
  1648. * constructed. Here we are modifying the current, active,
  1649. * task_struct.
  1650. */
  1651. asmlinkage long sys_unshare(unsigned long unshare_flags)
  1652. {
  1653. int err = 0;
  1654. struct fs_struct *fs, *new_fs = NULL;
  1655. struct sighand_struct *new_sigh = NULL;
  1656. struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
  1657. struct files_struct *fd, *new_fd = NULL;
  1658. struct sem_undo_list *new_ulist = NULL;
  1659. struct nsproxy *new_nsproxy = NULL;
  1660.  
  1661. check_unshare_flags(&unshare_flags);
  1662.  
  1663. /* Return -EINVAL for all unsupported flags */
  1664. err = -EINVAL;
  1665. if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
  1666. CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
  1667. CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWUSER|
  1668. CLONE_NEWNET))
  1669. goto bad_unshare_out;
  1670.  
  1671. if ((err = unshare_thread(unshare_flags)))
  1672. goto bad_unshare_out;
  1673. if ((err = unshare_fs(unshare_flags, &new_fs)))
  1674. goto bad_unshare_cleanup_thread;
  1675. if ((err = unshare_sighand(unshare_flags, &new_sigh)))
  1676. goto bad_unshare_cleanup_fs;
  1677. if ((err = unshare_vm(unshare_flags, &new_mm)))
  1678. goto bad_unshare_cleanup_sigh;
  1679. if ((err = unshare_fd(unshare_flags, &new_fd)))
  1680. goto bad_unshare_cleanup_vm;
  1681. if ((err = unshare_semundo(unshare_flags, &new_ulist)))
  1682. goto bad_unshare_cleanup_fd;
  1683. if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
  1684. new_fs)))
  1685. goto bad_unshare_cleanup_semundo;
  1686.  
  1687. if (new_fs || new_mm || new_fd || new_ulist || new_nsproxy) {
  1688.  
  1689. if (new_nsproxy) {
  1690. switch_task_namespaces(current, new_nsproxy);
  1691. new_nsproxy = NULL;
  1692. }
  1693.  
  1694. task_lock(current);
  1695.  
  1696. if (new_fs) {
  1697. fs = current->fs;
  1698. current->fs = new_fs;
  1699. new_fs = fs;
  1700. }
  1701.  
  1702. if (new_mm) {
  1703. mm = current->mm;
  1704. active_mm = current->active_mm;
  1705. current->mm = new_mm;
  1706. current->active_mm = new_mm;
  1707. activate_mm(active_mm, new_mm);
  1708. new_mm = mm;
  1709. }
  1710.  
  1711. if (new_fd) {
  1712. fd = current->files;
  1713. current->files = new_fd;
  1714. new_fd = fd;
  1715. }
  1716.  
  1717. task_unlock(current);
  1718. }
  1719.  
  1720. if (new_nsproxy)
  1721. put_nsproxy(new_nsproxy);
  1722.  
  1723. bad_unshare_cleanup_semundo:
  1724. bad_unshare_cleanup_fd:
  1725. if (new_fd)
  1726. put_files_struct(new_fd);
  1727.  
  1728. bad_unshare_cleanup_vm:
  1729. if (new_mm)
  1730. mmput(new_mm);
  1731.  
  1732. bad_unshare_cleanup_sigh:
  1733. if (new_sigh)
  1734. if (atomic_dec_and_test(&new_sigh->count))
  1735. kmem_cache_free(sighand_cachep, new_sigh);
  1736.  
  1737. bad_unshare_cleanup_fs:
  1738. if (new_fs)
  1739. put_fs_struct(new_fs);
  1740.  
  1741. bad_unshare_cleanup_thread:
  1742. bad_unshare_out:
  1743. return err;
  1744. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement