fork.c 41 KB

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