fork.c 40 KB

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